1 // =================================
  2 // Copyright (c) 2021 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 #ifndef CMAJOR_BINDER_STATEMENT_BINDER_INCLUDED
  7 #define CMAJOR_BINDER_STATEMENT_BINDER_INCLUDED
  8 #include <cmajor/binder/BoundCompileUnit.hpp>
  9 #include <cmajor/binder/BoundStatement.hpp>
 10 #include <sngcm/ast/Visitor.hpp>
 11 
 12 namespace cmajor { namespace binder {
 13 
 14 using namespace sngcm::ast;
 15 
 16 class BoundGotoCaseStatement;
 17 class BoundGotoDefaultStatement;
 18 class BoundClass;
 19 class BoundFunction;
 20 
 21 void CheckFunctionReturnPaths(FunctionSymbol* functionSymbolFunctionNode& functionNodeContainerScope* containerScopeBoundCompileUnit& boundCompileUnit);
 22 
 23 class StatementBinder public Visitor
 24 {
 25 public:
 26     StatementBinder(BoundCompileUnit& boundCompileUnit_);
 27     void Visit(CompileUnitNode& compileUnitNode) override;
 28     void Visit(NamespaceNode& namespaceNode) override;
 29     void Visit(EnumTypeNode& enumTypeNode) override;
 30     void Visit(ClassNode& classNode) override;
 31     void Visit(MemberVariableNode& memberVariableNode) override;
 32     void Visit(FunctionNode& functionNode) override;
 33     void Visit(FullInstantiationRequestNode& fullInstantiationRequestNode) override;
 34     void Visit(StaticConstructorNode& staticConstructorNode) override;
 35     void Visit(ConstructorNode& constructorNode) override;
 36     void Visit(DestructorNode& destructorNode) override;
 37     void Visit(MemberFunctionNode& memberFunctionNode) override;
 38     void Visit(ConversionFunctionNode& conversionFunctionNode) override;
 39     void Visit(CompoundStatementNode& compoundStatementNode) override;
 40     void Visit(ReturnStatementNode& returnStatementNode) override;
 41     void Visit(IfStatementNode& ifStatementNode) override;
 42     void Visit(WhileStatementNode& whileStatementNode) override;
 43     void Visit(DoStatementNode& doStatementNode) override;
 44     void Visit(ForStatementNode& forStatementNode) override;
 45     void Visit(BreakStatementNode& breakStatementNode) override;
 46     void Visit(ContinueStatementNode& continueStatementNode) override;
 47     void Visit(GotoStatementNode& gotoStatementNode) override;
 48     void Visit(ConstructionStatementNode& constructionStatementNode) override;
 49     void Visit(DeleteStatementNode& deleteStatementNode) override;
 50     void Visit(DestroyStatementNode& destroyStatementNode) override;
 51     void Visit(AssignmentStatementNode& assignmentStatementNode) override;
 52     void Visit(ExpressionStatementNode& expressionStatementNode) override;
 53     void Visit(EmptyStatementNode& emptyStatementNode) override;
 54     void Visit(RangeForStatementNode& rangeForStatementNode) override;
 55     void Visit(SwitchStatementNode& switchStatementNode) override;
 56     void Visit(CaseStatementNode& caseStatementNode) override;
 57     void Visit(DefaultStatementNode& defaultStatementNode) override;
 58     void Visit(GotoCaseStatementNode& gotoCaseStatementNode) override;
 59     void Visit(GotoDefaultStatementNode& gotoDefaultStatementNode) override;
 60     void Visit(ThrowStatementNode& throwStatementNode) override;
 61     void Visit(TryStatementNode& tryStatementNode) override;
 62     void Visit(CatchNode& catchNode) override;
 63     void Visit(AssertStatementNode& assertStatementNode) override;
 64     void Visit(ConditionalCompilationPartNode& conditionalCompilationPartNode) override;
 65     void Visit(ConditionalCompilationDisjunctionNode& conditionalCompilationDisjunctionNode) override;
 66     void Visit(ConditionalCompilationConjunctionNode& conditionalCompilationConjunctionNode) override;
 67     void Visit(ConditionalCompilationNotNode& conditionalCompilationNotNode) override;
 68     void Visit(ConditionalCompilationPrimaryNode& conditionalCompilationPrimaryNode) override;
 69     void Visit(ConditionalCompilationStatementNode& conditionalCompilationStatementNode) override;
 70     void CompileStatement(Node* statementNodebool setPostfix);
 71     BoundCompileUnit& GetBoundCompileUnit() { return boundCompileUnit; }
 72     void SetCurrentClass(BoundClass* currentClass_) { currentClass = currentClass_; }
 73     BoundClass* CurrentClass() const { return currentClass;  }
 74     ContainerScope* GetContainerScope() { return containerScope; }
 75     void SetContainerScope(ContainerScope* containerScope_) { containerScope = containerScope_; }
 76     BoundFunction* CurrentFunction() { return currentFunction; }
 77     void SetCurrentFunction(BoundFunction* currentFunction_) { currentFunction = currentFunction_; }
 78     void SetCurrentConstructor(ConstructorSymbol* currentConstructorSymbol_ConstructorNode* currentConstructorNode_);
 79     ConstructorSymbol* CurrentConstructorSymbol() { return currentConstructorSymbol; }
 80     ConstructorNode* CurrentConstructorNode() { return currentConstructorNode; }
 81     void SetCurrentDestructor(DestructorSymbol* currentDestructorSymbol_DestructorNode* currentDestructorNode_);
 82     void SetCurrentMemberFunction(MemberFunctionSymbol* currentMemberFunctionSymbol_MemberFunctionNode* currentMemberFunctionNode_);
 83     BoundStatement* ReleaseStatement() { return statement.release(); }
 84     bool CompilingThrow() const { return compilingThrow; }
 85     bool InsideCatch() const { return insideCatch; }
 86     void GenerateEnterAndExitFunctionCode(BoundFunction* boundFunction);
 87 private:
 88     BoundCompileUnit& boundCompileUnit;
 89     SymbolTable& symbolTable;
 90     Module* module;
 91     ContainerScope* containerScope;
 92     std::unique_ptr<BoundStatement> statement;
 93     int compoundLevel;
 94     bool insideCatch;
 95     BoundClass* currentClass;
 96     BoundFunction* currentFunction;
 97     StaticConstructorSymbol* currentStaticConstructorSymbol;
 98     StaticConstructorNode* currentStaticConstructorNode;
 99     ConstructorSymbol* currentConstructorSymbol;
100     ConstructorNode* currentConstructorNode;
101     DestructorSymbol* currentDestructorSymbol;
102     DestructorNode* currentDestructorNode;
103     MemberFunctionSymbol* currentMemberFunctionSymbol;
104     MemberFunctionNode* currentMemberFunctionNode;
105     TypeSymbol* switchConditionType;
106     std::std::unordered_map<IntegralValueCaseStatementNode*IntegralValueHash>*currentCaseValueMap;
107     std::std::vector<std::std::pair<BoundGotoCaseStatement*IntegralValue>>*currentGotoCaseStatements;
108     std::std::vector<BoundGotoDefaultStatement*>*currentGotoDefaultStatements;
109     bool postfix;
110     bool compilingThrow;
111     bool compilingReleaseExceptionStatement;
112     bool dontCheckDuplicateFunctionSymbols;
113     std::stack<bool> conditionalCompilationStack;
114     void AddStatement(BoundStatement* boundStatement);
115     void AddReleaseExceptionStatement(const Span& spanconst boost::uuids::uuid& moduleId);
116 };
117 
118 } } // namespace cmajor::binder
119 
120 #endif // CMAJOR_BINDER_STATEMENT_BINDER_INCLUDED