1
2
3
4
5
6 #ifndef CPP2CM_CPP2CM_CONVERTER_INCLUDED
7 #define CPP2CM_CPP2CM_CONVERTER_INCLUDED
8 #include <cpp2cm/cpp2cm/Context.hpp>
9 #include <cpp2cm/cpp2cm/NothrowList.hpp>
10 #include <cpp2cm/cpp2cm/Map.hpp>
11 #include <cpp2cm/cpp2cm/SourceFile.hpp>
12 #include <sngcpp/ast/Visitor.hpp>
13 #include <sngcpp/symbols/SymbolTable.hpp>
14 #include <sngcm/ast/CompileUnit.hpp>
15 #include <sngcm/ast/Parameter.hpp>
16 #include <sngcm/ast/Statement.hpp>
17 #include <sngcm/ast/Class.hpp>
18 #include <sngcm/ast/Enumeration.hpp>
19 #include <unordered_map>
20
21 namespace cpp2cm {
22
23 const char* Cpp2CmVersion();
24
25 class Converter : public sngcpp::ast::Visitor
26 {
27 public:
28 enum class StatementContainer
29 {
30 statements, latestCase, latestDefault
31 };
32 Converter(bool verbose_, const std::string& targetDir_, sngcpp::symbols::SymbolTable& symbolTable_, Map& map_,
33 const std::std::set<std::u32string>&excludedClasses_, conststd::std::set<std::u32string>&excludedFunctions_, NothrowList¬hrowList_);
34 void Write();
35 SourceFiles& GetSourceFiles() { return sourceFiles; }
36 void InsertNamespaceImports(sngcm::ast::CompileUnitNode* cu);
37 void AddToContainer(int line, sngcm::ast::Node* node);
38 void AddStatement(int line, sngcm::ast::StatementNode* statement);
39 void WriteWarning(int line, const std::string& message);
40 void ConvertIdExpr(sngcpp::ast::Node* idExprNode);
41 void ConvertExpression(sngcpp::ast::Node* exprNode);
42 void ConvertBooleanExpression(sngcpp::ast::Node* exprNode);
43 void ConvertTypeExpression(sngcpp::ast::Node* typeExprNode);
44 void NotConverted(int line, const std::string& message);
45 sngcm::ast::NodeList<sngcm::ast::Node> ConvertExpressionList(sngcpp::ast::Node* expressions);
46 sngcpp::symbols::TypeSymbol* MakeCommonType(sngcpp::symbols::TypeSymbol* leftType, sngcpp::symbols::TypeSymbol* rightType);
47 sngcpp::symbols::TypeSymbol* AddPointer(sngcpp::symbols::TypeSymbol* pointeeType);
48 sngcpp::symbols::TypeSymbol* RemovePointer(sngcpp::symbols::TypeSymbol* ptrType);
49 sngcpp::symbols::TypeSymbol* MakeElementType(sngcpp::symbols::TypeSymbol* arrayType);
50 void Visit(sngcpp::ast::BaseClassSpecifierNode& baseClassSpecifierNode) override;
51 void Visit(sngcpp::ast::BaseClassSpecifierSequenceNode& baseClassSpecifierSequenceNode) override;
52 void Visit(sngcpp::ast::ForwardClassDeclarationNode& forwardClassDeclarationNode) override;
53 void Visit(sngcpp::ast::ElaborateClassNameNode& elaborateClassNameNode) override;
54 void Visit(sngcpp::ast::ClassNode& classNode) override;
55 void Visit(sngcpp::ast::MemberAccessDeclarationNode& memberAccessDeclarationNode) override;
56 void Visit(sngcpp::ast::MemberDeclarationNode& memberDeclarationNode) override;
57 void Visit(sngcpp::ast::SpecialMemberFunctionNode& specialMemberFunctionNode) override;
58 void Visit(sngcpp::ast::CtorInitializerNode& ctorInitializerNode) override;
59 void Visit(sngcpp::ast::MemberInitializerNode& memberInitializerNode) override;
60 void Visit(sngcpp::ast::MemberInitializerSequenceNode& memberInitializerSequenceNode) override;
61 void Visit(sngcpp::ast::SourceFileNode& sourceFileNode) override;
62 void Visit(sngcpp::ast::SourceFileSequenceNode& sourceFileSequenceNode) override;
63 void Visit(sngcpp::ast::SimpleDeclarationNode& simpleDeclarationNode) override;
64 void Visit(sngcpp::ast::AliasDeclarationNode& aliasDeclarationNode) override;
65 void Visit(sngcpp::ast::UsingDirectiveNode& usingDirectiveNode) override;
66 void Visit(sngcpp::ast::UsingDeclarationNode& usingDeclarationNode) override;
67 void Visit(sngcpp::ast::TypedefNode& typedefNode) override;
68 void Visit(sngcpp::ast::DeclarationSequenceNode& declarationSequenceNode) override;
69 void Visit(sngcpp::ast::LinkageSpecificationNode& linkageSpecificationNode) override;
70 void Visit(sngcpp::ast::IdDeclaratorNode& idDeclaratorNode) override;
71 void Visit(sngcpp::ast::ArrayDeclaratorNode& arrayDeclaratorNode) override;
72 void Visit(sngcpp::ast::FunctionDeclaratorNode& functionDeclaratorNode) override;
73 void Visit(sngcpp::ast::FunctionPtrIdNode& functionPtrIdNode) override;
74 void Visit(sngcpp::ast::MemberFunctionPtrIdNode& memberFunctionPtrIdNode) override;
75 void Visit(sngcpp::ast::InitDeclaratorNode& initDeclaratorNode) override;
76 void Visit(sngcpp::ast::AssignmentInitializerNode& assignmentInitializerNode) override;
77 void Visit(sngcpp::ast::ExpressionListInitializerNode& expressionListInitializerNode) override;
78 void Visit(sngcpp::ast::ExpressionInitializerNode& expressionInitializerNode) override;
79 void Visit(sngcpp::ast::BracedInitializerListNode& bracedInitializerListNode) override;
80 void Visit(sngcpp::ast::EnumTypeNode& enumTypeNode) override;
81 void Visit(sngcpp::ast::EnumeratorNode& enumeratorNode) override;
82 void Visit(sngcpp::ast::EnumeratorSequenceNode& enumeratorSequenceNode) override;
83 void Visit(sngcpp::ast::ExpressionSequenceNode& expressionSequenceNode) override;
84 void Visit(sngcpp::ast::CommaExpressionNode& commaExpressionNode) override;
85 void Visit(sngcpp::ast::AssignmentExpressionNode& assignmentExpressionNode) override;
86 void Visit(sngcpp::ast::ConditionalExpressionNode& conditionalExpressionNode) override;
87 void Visit(sngcpp::ast::ThrowExpressionNode& throwExpressionNode) override;
88 void Visit(sngcpp::ast::LogicalOrExpressionNode& logicalOrExpressionNode) override;
89 void Visit(sngcpp::ast::LogicalAndExpressionNode& logicalAndExpressionNode) override;
90 void Visit(sngcpp::ast::InclusiveOrExpressionNode& inclusiveOrExpressionNode) override;
91 void Visit(sngcpp::ast::ExclusiveOrExpressionNode& exclusiveOrExpressionNode) override;
92 void Visit(sngcpp::ast::AndExpressionNode& andExpressionNode) override;
93 void Visit(sngcpp::ast::EqualityExpressionNode& equalityExpressionNode) override;
94 void Visit(sngcpp::ast::RelationalExpressionNode& relationalExpressionNode) override;
95 void Visit(sngcpp::ast::ShiftExpressionNode& shiftExpressionNode) override;
96 void Visit(sngcpp::ast::AdditiveExpressionNode& additiveExpressionNode) override;
97 void Visit(sngcpp::ast::MultiplicativeExpressionNode& multiplicativeExpressionNode) override;
98 void Visit(sngcpp::ast::PMExpressionNode& pmExpressionNode) override;
99 void Visit(sngcpp::ast::CastExpressionNode& castExpressionNode) override;
100 void Visit(sngcpp::ast::UnaryExpressionNode& unaryExpressionNode) override;
101 void Visit(sngcpp::ast::NewExpressionNode& newExpressionNode) override;
102 void Visit(sngcpp::ast::DeleteExpressionNode& deleteExpressionNode) override;
103 void Visit(sngcpp::ast::SubscriptExpressionNode& subscriptExpressionNode) override;
104 void Visit(sngcpp::ast::InvokeExpressionNode& invokeExpressionNode) override;
105 void Visit(sngcpp::ast::DotNode& dotNode) override;
106 void Visit(sngcpp::ast::ArrowNode& arrowNode) override;
107 void Visit(sngcpp::ast::PostfixIncNode& postfixIncNode) override;
108 void Visit(sngcpp::ast::PostfixDecNode& postfixDecNode) override;
109 void Visit(sngcpp::ast::CppCastExpressionNode& cppCastExpressionNode) override;
110 void Visit(sngcpp::ast::TypeIdExpressionNode& typeIdExpressionNode) override;
111 void Visit(sngcpp::ast::ThisNode& thisNode) override;
112 void Visit(sngcpp::ast::ParenthesizedExprNode& parenthesizedExprNode) override;
113 void Visit(sngcpp::ast::LambdaExpressionNode& lambdaExpressionNode) override;
114 void Visit(sngcpp::ast::IdentifierNode& identifierNode) override;
115 void Visit(sngcpp::ast::OperatorFunctionIdNode& operatorFunctionIdNode) override;
116 void Visit(sngcpp::ast::ConversionFunctionIdNode& conversionFunctionIdNode) override;
117 void Visit(sngcpp::ast::DtorIdNode& dDtorIdNode) override;
118 void Visit(sngcpp::ast::NestedIdNode& nestedIdNode) override;
119 void Visit(sngcpp::ast::ParameterNode& parameterNode) override;
120 void Visit(sngcpp::ast::ParameterSequenceNode& parameterSequenceNode) override;
121 void Visit(sngcpp::ast::FunctionNode& functionNode) override;
122 void Visit(sngcpp::ast::FloatingLiteralNode& floatingLiteralNode) override;
123 void Visit(sngcpp::ast::IntegerLiteralNode& integerLiteralNode) override;
124 void Visit(sngcpp::ast::CharacterLiteralNode& characterLiteralNode) override;
125 void Visit(sngcpp::ast::StringLiteralNode& stringLiteralNode) override;
126 void Visit(sngcpp::ast::BooleanLiteralNode& booleanLiteralNode) override;
127 void Visit(sngcpp::ast::NullPtrLiteralNode& nullPtrLiteralNode) override;
128 void Visit(sngcpp::ast::NamespaceNode& namespaceNode) override;
129 void Visit(sngcpp::ast::SimpleTypeNode& simpleTypeNode) override;
130 void Visit(sngcpp::ast::LabeledStatementNode& labeledStatementNode) override;
131 void Visit(sngcpp::ast::CaseStatementNode& caseStatementNode) override;
132 void Visit(sngcpp::ast::DefaultStatementNode& defaultStatementNode) override;
133 void Visit(sngcpp::ast::ExpressionStatementNode& expressionStatementNode) override;
134 void Visit(sngcpp::ast::CompoundStatementNode& compoundStatementNode) override;
135 void Visit(sngcpp::ast::StatementSequenceNode& statementSequenceNode) override;
136 void Visit(sngcpp::ast::IfStatementNode& ifStatementNode) override;
137 void Visit(sngcpp::ast::SwitchStatementNode& switchStatementNode) override;
138 void Visit(sngcpp::ast::WhileStatementNode& whileStatementNode) override;
139 void Visit(sngcpp::ast::DoStatementNode& doStatementNode) override;
140 void Visit(sngcpp::ast::RangeForStatementNode& rangeForStatementNode) override;
141 void Visit(sngcpp::ast::ForStatementNode& forStatementNode) override;
142 void Visit(sngcpp::ast::BreakStatementNode& breakStatementNode) override;
143 void Visit(sngcpp::ast::ContinueStatementNode& continueStatementNode) override;
144 void Visit(sngcpp::ast::ReturnStatementNode& returnStatementNode) override;
145 void Visit(sngcpp::ast::GotoStatementNode& gotoStatementNode) override;
146 void Visit(sngcpp::ast::DeclarationStatementNode& declarationStatementNode) override;
147 void Visit(sngcpp::ast::TryStatementNode& tryStatementNode) override;
148 void Visit(sngcpp::ast::HandlerNode& handlerNode) override;
149 void Visit(sngcpp::ast::HandlerSequenceNode& handlerSequenceNode) override;
150 void Visit(sngcpp::ast::CatchAllNode& catchAllNode) override;
151 void Visit(sngcpp::ast::TypeParameterNode& typeParameterNode) override;
152 void Visit(sngcpp::ast::TemplateParameterSequenceNode& templateParameterSequenceNode) override;
153 void Visit(sngcpp::ast::TemplateDeclarationNode& templateDeclarationNode) override;
154 void Visit(sngcpp::ast::TemplateArgumentSequenceNode& templateArgumentSequenceNode) override;
155 void Visit(sngcpp::ast::TemplateIdNode& templateIdNode) override;
156 void Visit(sngcpp::ast::TemplateArgumentNode& templateArgumentNode) override;
157 void Visit(sngcpp::ast::ExplicitInstantiationNode& explicitInstantiationNode) override;
158 void Visit(sngcpp::ast::ExplicitSpecializationNode& explicitSpecializationNode) override;
159 void Visit(sngcpp::ast::ConstNode& constNode) override;
160 void Visit(sngcpp::ast::VolatileNode& volatileNode) override;
161 void Visit(sngcpp::ast::PointerNode& pointerNode) override;
162 void Visit(sngcpp::ast::RValueRefNode& rValueRefNode) override;
163 void Visit(sngcpp::ast::LValueRefNode& lValueRefNode) override;
164 private:
165 bool verbose;
166 std::string targetDir;
167 sngcpp::symbols::SymbolTable& symbolTable;
168 std::string currentSourceFileName;
169 std::vector<std::std::unique_ptr<sngcm::ast::CompileUnitNode>>compileUnits;
170 sngcm::ast::CompileUnitNode* currentCompileUnit;
171 sngcm::ast::NamespaceNode* currentNamespace;
172 sngcm::ast::EnumTypeNode* currentEnumType;
173 Context* currentContext;
174 sngcm::ast::Node* currentContainerNode;
175 std::unordered_map<std::string, sngcm::ast::CompileUnitNode*> compileUnitMap;
176 std::unordered_map<sngcm::ast::CompileUnitNode*, Context> contextMap;
177 std::unordered_map<sngcpp::symbols::ClassTypeSymbol*, sngcm::ast::ClassNode*> classMap;
178 Map& map;
179 std::vector<sngcpp::symbols::Symbol*> mappedSymbolSequence;
180 std::unique_ptr<sngcm::ast::Node> node;
181 std::vector<std::std::unique_ptr<sngcm::ast::ParameterNode>>parameterNodes;
182 StatementContainer statementContainer;
183 std::vector<std::std::unique_ptr<sngcm::ast::StatementNode>>statementNodes;
184 sngcm::ast::CaseStatementNode* parentCaseStatementNode;
185 std::vector<std::std::unique_ptr<sngcm::ast::CaseStatementNode>>caseStatementNodes;
186 std::unique_ptr<sngcm::ast::DefaultStatementNode> defaultStatement;
187 std::vector<std::std::unique_ptr<sngcm::ast::CatchNode>>catchNodes;
188 std::vector<std::std::unique_ptr<sngcm::ast::TemplateParameterNode>>templateParameterNodes;
189 sngcm::ast::NodeList<sngcm::ast::Node> nodes;
190 bool addToNodes;
191 sngcpp::symbols::TypeSymbol* type;
192 sngcpp::symbols::ClassTypeSymbol* currentClassType;
193 std::unique_ptr<sngcm::ast::ClassNode> cls;
194 std::unique_ptr<sngcm::ast::Node> declarator;
195 std::unique_ptr<sngcm::ast::Node> initializer;
196 bool assignment;
197 bool empty;
198 bool inFunctionBody;
199 bool assignmentStatement;
200 bool rangeFor;
201 bool catchDecl;
202 std::unique_ptr<sngcm::ast::Node> rangeForType;
203 std::unique_ptr<sngcm::ast::IdentifierNode> rangeForId;
204 std::unique_ptr<sngcm::ast::Node> catchType;
205 std::unique_ptr<sngcm::ast::IdentifierNode> catchId;
206 sngcpp::symbols::CallableSymbol* calledFunction;
207 std::u32string args;
208 SourceFiles sourceFiles;
209 const std::std::set<std::u32string>&excludedClasses;
210 const std::std::set<std::u32string>&excludedFunctions;
211 NothrowList& nothrowList;
212 Item nothrowFileItem;
213 Item nothrowClassItem;
214 Item nothrowFunctionItem;
215 };
216
217 }
218
219 #endif // CPP2CM_CPP2CM_CONVERTER_INCLUDED