// IR 导向符号表:符号条目可直接挂接 IR 实体。 #pragma once #include #include #include #include #include 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 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 array_dims; bool has_constexpr_value = false; int64_t const_int_value = 0; double const_float_value = 0.0; std::vector const_int_init; std::vector const_float_init; std::vector param_types; std::vector 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::vector scopes_; };