1
2
3
4
5
6 #ifndef SNGCPP_SYMBOLS_SYMBOL_TABLE_INCLUDED
7 #define SNGCPP_SYMBOLS_SYMBOL_TABLE_INCLUDED
8 #include <sngcpp/symbols/NamespaceSymbol.hpp>
9 #include <sngcpp/symbols/ClassTypeSymbol.hpp>
10 #include <sngcpp/symbols/EnumerationSymbol.hpp>
11 #include <sngcpp/symbols/FunctionSymbol.hpp>
12 #include <sngcpp/symbols/DeclarationBlockSymbol.hpp>
13 #include <sngcpp/symbols/SimpleTypeSymbol.hpp>
14 #include <sngcpp/symbols/DerivedTypeSymbol.hpp>
15 #include <sngcpp/symbols/RuleSymbol.hpp>
16 #include <sngcpp/ast/Class.hpp>
17 #include <sngcpp/ast/Enumeration.hpp>
18 #include <sngcpp/ast/Function.hpp>
19 #include <sngcpp/ast/Declaration.hpp>
20 #include <sngcpp/ast/Declarator.hpp>
21 #include <sngcpp/ast/SimpleType.hpp>
22 #include <sngcpp/ast/Expression.hpp>
23 #include <sngcpp/ast/Template.hpp>
24 #include <sngcpp/ast/TypeExpr.hpp>
25 #include <sngxml/dom/Document.hpp>
26 #include <sngxml/dom/Element.hpp>
27 #include <stack>
28
29 namespace sngcpp { namespace symbols {
30
31 using namespace sngcpp::ast;
32
33 class SymbolTable
34 {
35 public:
36 SymbolTable();
37 std::unique_ptr<sngxml::dom::Document> ToDomDocument();
38 std::unique_ptr<sngxml::dom::Element> ToDomElement();
39 std::unique_ptr<sngxml::dom::Element> CreateTypesElement();
40 void BeginContainer(ContainerSymbol* container_);
41 void EndContainer();
42 int BeginNameSequence(const std::std::vector<std::std::pair<std::u32string, int>>&nameSequence);
43 void EndNameSequence(int n);
44 void BeginNamespace(const std::u32string& namespaceName, const std::u32string& projectName);
45 void EndNamespace();
46 void BeginClass(ClassNode* classNode, const std::u32string& className, std::std::vector<std::std::unique_ptr<TypeSymbol>>&templateParameters, conststd::u32string&projectName);
47 void EndClass(const std::u32string& projectName);
48 void BeginLambdaExpression(LambdaExpressionNode* lambdaExpressionNode);
49 void EndLambdaExpression();
50 void BeginGrammar(const Span& span, const std::u32string& grammarName, const std::u32string& projectName);
51 void EndGrammar(const std::u32string& projectName);
52 RuleSymbol* AddRule(const Span& span, const std::u32string& ruleName);
53 void BeginEnumType(EnumTypeNode* enumTypeNode, const std::u32string& enumTypeName, const std::u32string& projectName);
54 void EndEnumType(const std::u32string& projectName);
55 void AddEnumerator(EnumeratorNode* enumeratorNode, const std::u32string& enumeratorName, const std::u32string& enumeratorValue);
56 void BeginFunction(FunctionDeclaratorNode* functionDeclaratorNode, const std::u32string& groupName, const std::u32string& functionName,
57 std::std::vector<std::std::unique_ptr<TypeSymbol>>&templateParameters, Specifierspecifiers, conststd::u32string&projectName);
58 void EndFunction(const std::u32string& projectName);
59 void BeginFunctionDeclaration(FunctionDeclaratorNode* functionDeclaratorNode, const std::u32string& groupName, const std::u32string& functionName,
60 std::std::vector<std::std::unique_ptr<TypeSymbol>>&templateParameters, Specifierspecifiers, conststd::u32string&projectName);
61 void EndFunctionDeclaration();
62 void AddParameter(ParameterNode* parameterNode, const std::u32string& parameterName);
63 void BeginDeclarationBlock(Node* blockNode);
64 void EndDeclarationBlock();
65 void AddTypedef(TypedefNode* node, const std::u32string& typedefName, const std::u32string& projectName);
66 void AddVariable(Node* node, const std::u32string& variableName, const std::u32string& projectName);
67 void MapNode(Node* node, Symbol* symbol);
68 void MapIdNodeSequence(Node* node, const std::std::vector<IdentifierNode*>&idNodeSequence);
69 void MapTemplateIdToTemplateArgumentNodes(TemplateIdNode* templateIdNode, const std::std::vector<Node*>&templateArgumentNodes);
70 Symbol* GetSymbolNothrow(Node* node);
71 Symbol* GetSymbol(Node* node);
72 const std::std::vector<IdentifierNode*>&GetIdNodeSequence(Node*node);
73 const std::std::vector<Node*>&GetTemplateArgumentNodes(TemplateIdNode*templateIdNode);
74 NamespaceSymbol* GlobalNs() { return &globalNs; }
75 ContainerScope* GetContainerScope();
76 TypeSymbol* GetBoolType();
77 TypeSymbol* GetLongType();
78 TypeSymbol* GetDoubleType();
79 TypeSymbol* GetIntType();
80 TypeSymbol* GetUIntType();
81 TypeSymbol* GetCharType();
82 TypeSymbol* GetChar16Type();
83 TypeSymbol* GetChar32Type();
84 TypeSymbol* GetVoidType();
85 TypeSymbol* MakeSimpleTypeSymbol(SimpleTypeNode& simpleTypeNode);
86 TypeSymbol* MakeElaborateClassTypeSymbol(ClassKey classKey, TypeSymbol* classType);
87 TypeSymbol* MakeDerivedTypeSymbol(std::std::vector<Derivation>&derivations, TypeSymbol*baseType);
88 TypeSymbol* MakeExternalTypeSymbol(const Span& span, const std::u32string& name, ClassKey classKey);
89 TypeSymbol* MakeClassGroupTypeSymbol(ClassGroupSymbol* classGroup);
90 TypeSymbol* MakeClassTemplateSpecializationSymbol(const Span& span, TypeSymbol* primaryClassTemplate, const std::std::vector<TypeSymbol*>&templateArguments);
91 TypeSymbol* MakeIntegerLiteralTypeSymbol(const Span& span, const std::u32string& valueName);
92 void SetGendocMode() { gendocMode = true; }
93 bool InGendocMode() const { return gendocMode; }
94 private:
95 NamespaceSymbol globalNs;
96 ContainerSymbol* container;
97 std::stack<ContainerSymbol*> containerStack;
98 std::unordered_map<Node*, Symbol*> nodeSymbolMap;
99 std::unordered_map<Node*, std::std::vector<IdentifierNode*>>idNodeSequenceMap;
100 std::unordered_map<TemplateIdNode*, std::std::vector<Node*>>templateIdTemplateArgumentNodeMap;
101 int blockNumber;
102 std::unordered_map<std::u32string, TypeSymbol*> idTypeMap;
103 std::vector<std::std::unique_ptr<TypeSymbol>>types;
104 std::vector<std::std::unique_ptr<Node>>ownedNodes;
105 bool gendocMode;
106 };
107
108 } }
109
110 #endif // SNGCPP_SYMBOLS_SYMBOL_TABLE_INCLUDED