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.

77 lines
1.6 KiB

// IR 导向符号表:符号条目可直接挂接 IR 实体。
#pragma once
#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
namespace antlr4 {
class ParserRuleContext;
} // namespace antlr4
namespace ir {
class Type;
class Value;
class Function;
} // namespace ir
enum class SymbolKind {
Variable,
Constant,
Function,
Parameter,
};
enum class SymbolDataType {
Unknown,
Void,
Int,
Float,
Bool,
};
struct SymbolEntry {
std::string name;
SymbolKind kind = SymbolKind::Variable;
SymbolDataType data_type = SymbolDataType::Unknown;
std::shared_ptr<ir::Type> type;
ir::Value* ir_value = nullptr;
ir::Function* ir_function = nullptr;
bool is_const = false;
bool is_global = false;
bool has_initializer = false;
bool is_array = false;
std::vector<int64_t> array_dims;
bool has_constexpr_value = false;
int64_t const_int_value = 0;
double const_float_value = 0.0;
std::vector<int64_t> const_int_init;
std::vector<double> const_float_init;
std::vector<SymbolDataType> param_types;
std::vector<bool> param_is_array;
const antlr4::ParserRuleContext* decl_ctx = nullptr;
};
class SymbolTable {
public:
SymbolTable();
void EnterScope();
void ExitScope();
bool Insert(const SymbolEntry* symbol);
bool Contains(const std::string& name) const;
bool ContainsCurrentScope(const std::string& name) const;
const SymbolEntry* Lookup(const std::string& name) const;
const SymbolEntry* LookupCurrentScope(const std::string& name) const;
private:
using Scope = std::unordered_map<std::string, const SymbolEntry*>;
std::vector<Scope> scopes_;
};