forked from NUDT-compiler/nudt-compiler-cpp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.4 KiB
69 lines
1.4 KiB
#pragma once
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
namespace ir {
|
|
class Function;
|
|
class Value;
|
|
}
|
|
|
|
enum class SemanticType {
|
|
Void,
|
|
Int,
|
|
Float,
|
|
};
|
|
|
|
enum class SymbolKind {
|
|
Variable,
|
|
Constant,
|
|
Function,
|
|
};
|
|
|
|
struct ConstantValue {
|
|
SemanticType type = SemanticType::Int;
|
|
int int_value = 0;
|
|
float float_value = 0.0f;
|
|
};
|
|
|
|
struct FunctionTypeInfo {
|
|
SemanticType return_type = SemanticType::Void;
|
|
std::vector<SemanticType> param_types;
|
|
std::vector<bool> param_is_array;
|
|
std::vector<std::vector<int>> param_dims;
|
|
};
|
|
|
|
struct SymbolEntry {
|
|
SymbolKind kind = SymbolKind::Variable;
|
|
SemanticType type = SemanticType::Int;
|
|
bool is_const = false;
|
|
bool is_array = false;
|
|
bool is_param_array = false;
|
|
std::vector<int> dims;
|
|
|
|
ir::Value* ir_value = nullptr;
|
|
ir::Function* function = nullptr;
|
|
|
|
std::optional<ConstantValue> const_scalar;
|
|
std::vector<ConstantValue> const_array;
|
|
FunctionTypeInfo function_type;
|
|
};
|
|
|
|
class SymbolTable {
|
|
public:
|
|
void Clear();
|
|
void EnterScope();
|
|
void ExitScope();
|
|
|
|
bool Insert(const std::string& name, const SymbolEntry& entry);
|
|
bool ContainsInCurrentScope(const std::string& name) const;
|
|
|
|
SymbolEntry* Lookup(const std::string& name);
|
|
const SymbolEntry* Lookup(const std::string& name) const;
|
|
|
|
private:
|
|
std::vector<std::unordered_map<std::string, SymbolEntry>> scopes_;
|
|
};
|