CppParser
Loading...
Searching...
No Matches
cppwriter.h
Go to the documentation of this file.
1/*
2The MIT License (MIT)
3
4Copyright (c) 2018 Satya Das
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in
8 the Software without restriction, including without limitation the rights to
9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 the Software, and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#pragma once
25
26#include "cppast.h"
27#include "cppindent.h"
28
29#include <string>
30
32
40{
41public:
42 CppWriter();
43
45 {
48 kSource
49 };
52
53public:
54 // This only delegate to other emit() method based on type.
55 virtual void emit(CppObj* cppObj, std::ostream& stm, CppIndent indentation = CppIndent()) const;
56 virtual void emitVar(CppVar* varObj, std::ostream& stm, CppIndent indentation = CppIndent()) const;
57 virtual void emitVarList(CppVarList* varListObj, std::ostream& stm, CppIndent indentation = CppIndent()) const;
58 virtual void emitEnum(CppEnum* enmObj,
59 std::ostream& stm,
60 bool emitNewLine,
61 CppIndent indentation = CppIndent()) const;
62 virtual void emitTypedef(CppTypedefName* typedefName,
63 std::ostream& stm,
64 CppIndent indentation = CppIndent()) const;
65 virtual void emitUsingDecl(CppUsingDecl* usingDecl,
66 std::ostream& stm,
67 CppIndent indentation = CppIndent()) const;
68 virtual void emitTypedefList(CppTypedefList* typedefList,
69 std::ostream& stm,
70 CppIndent indentation = CppIndent()) const;
71 virtual void emitFwdDecl(CppFwdClsDecl* fwdClsDeclObj,
72 std::ostream& stm,
73 CppIndent indentation = CppIndent()) const;
74 virtual void emitMacroCall(CppMacroCall* macroCallObj,
75 std::ostream& stm,
76 CppIndent indentation = CppIndent()) const;
77 virtual void emitCompound(CppCompound* compoundObj,
78 std::ostream& stm,
79 CppIndent indentation = CppIndent(),
80 bool emitNewLine = true) const;
81 virtual void emitFunction(CppFunction* funcObj,
82 std::ostream& stm,
83 bool emitNewLine,
84 CppIndent indentation = CppIndent()) const;
85 virtual void emitFunctionPtr(CppFunctionPointer* funcPtrObj,
86 std::ostream& stm,
87 bool emitNewLine,
88 CppIndent indentation = CppIndent()) const;
89 virtual void emitConstructor(CppConstructor* ctorObj,
90 std::ostream& stm,
91 CppIndent indentation = CppIndent()) const;
92 virtual void emitDestructor(CppDestructor* dtorObj,
93 std::ostream& stm,
94 CppIndent indentation = CppIndent()) const;
95 virtual void emitTypeConverter(CppTypeConverter* typeConverterObj,
96 std::ostream& stm,
97 CppIndent indentation = CppIndent()) const;
98 virtual void emitDocComment(CppDocComment* docCommentObj,
99 std::ostream& stm,
100 CppIndent indentation = CppIndent()) const;
101
102 virtual void emitExpr(CppExpr* exprObj, std::ostream& stm, CppIndent indentation = CppIndent()) const;
103 virtual void emitDefine(CppDefine* defineObj, std::ostream& stm) const;
104 virtual void emitUndef(CppUndef* undefObj, std::ostream& stm) const;
105 virtual void emitInclude(CppInclude* includeObj, std::ostream& stm) const;
106 virtual void emitHashIf(CppHashIf* hashIfObj, std::ostream& stm) const;
107 virtual void emitHashIf(CppHashIf::CondType condType, const std::string& cond, std::ostream& stm) const;
108 virtual void emitEndIf(std::ostream& stm) const;
109 virtual void emitPragma(CppPragma* pragmaObj, std::ostream& stm) const;
110 virtual void emitBlob(CppBlob* blobObj, std::ostream& stm, bool formatLineStarts, CppIndent indentation) const;
111
112 virtual void emitVarType(CppVarType* varTypeObj, std::ostream& stm) const;
113 virtual void emitParamList(CppParamVector* paramListObj, std::ostream& stm) const;
114 virtual void emitExprAtom(CppExprAtom& exprObj, std::ostream& stm, CppIndent indentation = CppIndent()) const;
115
116 virtual void emitIfBlock(CppIfBlock* ifBlock, std::ostream& stm, CppIndent indentation = CppIndent()) const;
117 virtual void emitWhileBlock(CppWhileBlock* whileBlock,
118 std::ostream& stm,
119 CppIndent indentation = CppIndent()) const;
120 virtual void emitDoBlock(CppDoWhileBlock* doBlock,
121 std::ostream& stm,
122 CppIndent indentation = CppIndent()) const;
123 virtual void emitForBlock(CppForBlock* forBlock, std::ostream& stm, CppIndent indentation = CppIndent()) const;
124 virtual void emitSwitchBlock(CppSwitchBlock* switchBlock,
125 std::ostream& stm,
126 CppIndent indentation = CppIndent()) const;
127
128public:
129 void emitVar(CppVar* varObj, std::ostream& stm, bool skipName) const;
130 void emitParamList(CppParamVector* paramListObj, std::ostream& stm, bool skipName) const;
131 void emitFunctionPtr(CppFunctionPointer* funcPtrObj, std::ostream& stm, bool skipName, bool emitNewLine) const;
132 void emitFunction(CppFunction* funcObj, std::ostream& stm, bool skipParamName, bool emitNewLine) const;
133 void emitConstructor(CppConstructor* ctorObj, std::ostream& stm, bool skipParamName) const;
134 void emitTemplSpec(CppTemplateParamList* templSpec, std::ostream& stm, CppIndent indentation) const;
135
136private:
137 void emit(CppObj* cppObj, std::ostream& stm, CppIndent indentation, bool noNewLine) const;
138 void emitVar(CppVar* varObj, std::ostream& stm, CppIndent indentation, bool skipName) const;
140 std::ostream& stm,
141 CppIndent indentation,
142 bool skipName,
143 bool emitNewLine) const;
144 void emitFunction(CppFunction* funcObj,
145 std::ostream& stm,
146 CppIndent indentation,
147 bool skipName,
148 bool skipParamName,
149 bool emitNewLine) const;
150 void emitConstructor(CppConstructor* ctorObj,
151 std::ostream& stm,
152 CppIndent indentation,
153 bool skipParamName) const;
154
155 void emitVarDecl(std::ostream& stm, CppVarDecl& varDecl, bool skipName) const;
156
157private:
159
161};
162
164 : emittingType_(kRaw)
165{
166}
167
169{
170 emittingType_ = emittingType;
171}
172
174{
175 return emittingType_;
176}
177
178inline void CppWriter::emitVar(CppVar* varObj, std::ostream& stm, bool skipName) const
179{
180 emitVar(varObj, stm, CppIndent(), skipName);
181}
182
184 std::ostream& stm,
185 bool skipName,
186 bool emitNewLine) const
187{
188 if (funcPtrObj->attr() & kTypedef)
189 return; // Typedef of funcptr without name makes no sense.
190 emitFunction(funcPtrObj, stm, CppIndent(), skipName, skipName, emitNewLine);
191}
192
194 std::ostream& stm,
195 bool skipParamName,
196 bool emitNewLine) const
197{
198 emitFunction(funcObj, stm, CppIndent(), false, skipParamName, emitNewLine);
199}
200
201inline void CppWriter::emitConstructor(CppConstructor* ctorObj, std::ostream& stm, bool skipParamName) const
202{
203 emitConstructor(ctorObj, stm, CppIndent(), skipParamName);
204}
205
206inline std::ostream& operator<<(std::ostream& stm, CppIndent& indentation)
207{
208 indentation.emit(stm);
209 return stm;
210}
211
212inline std::ostream& operator<<(std::ostream& stm, CppAccessType accessType)
213{
214 switch (accessType)
215 {
217 stm << "public";
218 break;
220 stm << "protected";
221 break;
223 stm << "private";
224 break;
225
226 default:
227 break;
228 }
229 return stm;
230}
231
232inline std::ostream& operator<<(std::ostream& stm, CppCompoundType cmpndType)
233{
234 switch (cmpndType)
235 {
237 stm << "namespace";
238 break;
240 stm << "class";
241 break;
243 stm << "struct";
244 break;
246 stm << "union";
247 break;
248
249 default:
250 break;
251 }
252 return stm;
253}
Helper class to manage indentation while writing C++ file from AST.
Definition: cppindent.h:33
void emit(std::ostream &stm) const
Definition: cppindent.h:100
Responsible for emitting C/C++ source from CppAst data structure.
Definition: cppwriter.h:40
virtual void emitMacroCall(CppMacroCall *macroCallObj, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:433
virtual void emitEnum(CppEnum *enmObj, std::ostream &stm, bool emitNewLine, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:334
virtual void emitFwdDecl(CppFwdClsDecl *fwdClsDeclObj, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:417
CppIndent preproIndent_
Definition: cppwriter.h:158
virtual void emitVarType(CppVarType *varTypeObj, std::ostream &stm) const
Definition: cppwriter.cpp:251
virtual void emitHashIf(CppHashIf *hashIfObj, std::ostream &stm) const
Definition: cppwriter.cpp:176
virtual void emitIfBlock(CppIfBlock *ifBlock, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:1067
virtual void emitParamList(CppParamVector *paramListObj, std::ostream &stm) const
Definition: cppwriter.cpp:538
virtual void emitUsingDecl(CppUsingDecl *usingDecl, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:394
@ kHeader
No function definition is emitted unless it is explicitly inline.
Definition: cppwriter.h:47
@ kRaw
No intelligence is applied and things are emitted as is.
Definition: cppwriter.h:46
@ kSource
No method declaration is emitted unless it is part of the class that is defined in the same file.
Definition: cppwriter.h:48
virtual void emitEndIf(std::ostream &stm) const
Definition: cppwriter.cpp:181
virtual void emitConstructor(CppConstructor *ctorObj, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:726
EmittingType getEmittingType() const
Definition: cppwriter.h:173
void emitVarDecl(std::ostream &stm, CppVarDecl &varDecl, bool skipName) const
Definition: cppwriter.cpp:274
virtual void emitExpr(CppExpr *exprObj, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:946
virtual void emitDoBlock(CppDoWhileBlock *doBlock, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:1104
virtual void emitSwitchBlock(CppSwitchBlock *switchBlock, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:1143
virtual void emitInclude(CppInclude *includeObj, std::ostream &stm) const
Definition: cppwriter.cpp:172
virtual void emitTypeConverter(CppTypeConverter *typeConverterObj, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:760
virtual void emitDestructor(CppDestructor *dtorObj, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:731
void emitTemplSpec(CppTemplateParamList *templSpec, std::ostream &stm, CppIndent indentation) const
Definition: cppwriter.cpp:440
virtual void emitTypedefList(CppTypedefList *typedefList, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:409
virtual void emitDefine(CppDefine *defineObj, std::ostream &stm) const
Definition: cppwriter.cpp:150
virtual void emitBlob(CppBlob *blobObj, std::ostream &stm, bool formatLineStarts, CppIndent indentation) const
Definition: cppwriter.cpp:226
EmittingType emittingType_
Definition: cppwriter.h:160
virtual void emitDocComment(CppDocComment *docCommentObj, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:786
virtual void emitTypedef(CppTypedefName *typedefName, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:385
virtual void emitCompound(CppCompound *compoundObj, std::ostream &stm, CppIndent indentation=CppIndent(), bool emitNewLine=true) const
Definition: cppwriter.cpp:473
virtual void emitFunctionPtr(CppFunctionPointer *funcPtrObj, std::ostream &stm, bool emitNewLine, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:658
virtual void emitVarList(CppVarList *varListObj, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:319
void setEmittingType(EmittingType type)
Definition: cppwriter.h:168
virtual void emitPragma(CppPragma *pragmaObj, std::ostream &stm) const
Definition: cppwriter.cpp:221
virtual void emitUndef(CppUndef *undefObj, std::ostream &stm) const
Definition: cppwriter.cpp:167
virtual void emitForBlock(CppForBlock *forBlock, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:1117
virtual void emitVar(CppVar *varObj, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:267
virtual void emitExprAtom(CppExprAtom &exprObj, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:928
virtual void emitWhileBlock(CppWhileBlock *whileBlock, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:1090
virtual void emit(CppObj *cppObj, std::ostream &stm, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:70
void emitFunctionPtr(CppFunctionPointer *funcPtrObj, std::ostream &stm, CppIndent indentation, bool skipName, bool emitNewLine) const
virtual void emitFunction(CppFunction *funcObj, std::ostream &stm, bool emitNewLine, CppIndent indentation=CppIndent()) const
Definition: cppwriter.cpp:653
std::vector< std::unique_ptr< CppTemplateParam > > CppTemplateParamList
Definition: cppast.h:556
CppAccessType
Definition: cppconst.h:103
@ kTypedef
Definition: cppconst.h:198
CppCompoundType
Definition: cppconst.h:90
@ kNamespace
Definition: cppconst.h:94
@ kUnion
Definition: cppconst.h:97
@ kClass
Definition: cppconst.h:95
@ kStruct
Definition: cppconst.h:96
CppAccessType accessType(CppObj *cppObj)
std::vector< CppObjPtr > CppParamVector
Definition: cpptoken.h:110
std::ostream & operator<<(std::ostream &stm, CppIndent &indentation)
Definition: cppwriter.h:206
A stream of text that represents some content in a C++ program.
Definition: cppast.h:167
Some blocks have common structure like if, while, and do-while.
Definition: cppast.h:1923
All classes, structs, unions, and namespaces can be classified as a Compound object.
Definition: cppast.h:927
An individual expression.
Definition: cppast.h:1671
An expression in a C/C++ program.
Definition: cppast.h:1750
std::uint32_t attr()
Definition: cppast.h:1198
Function pointer type definition using typedef, e.g.
Definition: cppast.h:1390
Represents all variants of #if preprocessors.
Definition: cppast.h:276
An abstract class that is used as base class of all other classes.
Definition: cppast.h:134
List of variables declared in a line without repeating its type, e.g.
Definition: cppast.h:699
Class to represent C++ variable definition.
Definition: cppast.h:565