source: src/CodeGen/GenType.cc @ e8032b0

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since e8032b0 was e8032b0, checked in by Aaron Moss <a3moss@…>, 8 years ago

Switch Indexer over to copy-on-write semantics for dramatic speedup

  • Property mode set to 100644
File size: 6.1 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// GenType.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Jul  9 16:43:52 2015
13// Update Count     : 13
14//
15
16#include <sstream>
17#include <cassert>
18
19#include "GenType.h"
20#include "CodeGenerator.h"
21
22#include "SynTree/Declaration.h"
23#include "SynTree/Expression.h"
24#include "SynTree/Type.h"
25#include "SynTree/Visitor.h"
26
27namespace CodeGen {
28        class GenType : public Visitor {
29          public:
30                GenType( const std::string &typeString );
31                std::string get_typeString() const { return typeString; }
32                void set_typeString( const std::string &newValue ) { typeString = newValue; }
33 
34                virtual void visit( FunctionType *funcType );
35                virtual void visit( VoidType *voidType );
36                virtual void visit( BasicType *basicType );
37                virtual void visit( PointerType *pointerType );
38                virtual void visit( ArrayType *arrayType );
39                virtual void visit( StructInstType *structInst );
40                virtual void visit( UnionInstType *unionInst );
41                virtual void visit( EnumInstType *enumInst );
42                virtual void visit( TypeInstType *typeInst );
43                virtual void visit( VarArgsType *varArgsType );
44 
45          private:
46                void handleQualifiers( Type *type );
47                void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
48 
49                std::string typeString;
50        };
51
52        std::string genType( Type *type, const std::string &baseString ) {
53                GenType gt( baseString );
54                type->accept( gt );
55                return gt.get_typeString();
56        }
57
58        GenType::GenType( const std::string &typeString ) : typeString( typeString ) {}
59
60        void GenType::visit( VoidType *voidType ) {
61                typeString = "void " + typeString;
62                handleQualifiers( voidType );
63        }
64
65        void GenType::visit( BasicType *basicType ) {
66                BasicType::Kind kind = basicType->get_kind();
67                assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
68                typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
69                handleQualifiers( basicType );
70        }
71
72        void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) {
73                std::ostringstream os;
74                if ( typeString != "" ) {
75                        if ( typeString[ 0 ] == '*' ) {
76                                os << "(" << typeString << ")";
77                        } else {
78                                os << typeString;
79                        } // if
80                } // if
81                os << "[";
82
83                if ( isStatic ) {
84                        os << "static ";
85                } // if
86                if ( qualifiers.isConst ) {
87                        os << "const ";
88                } // if
89                if ( qualifiers.isVolatile ) {
90                        os << "volatile ";
91                } // if
92                if ( qualifiers.isRestrict ) {
93                        os << "__restrict ";
94                } // if
95                if ( qualifiers.isAtomic ) {
96                        os << "_Atomic ";
97                } // if
98                if ( qualifiers.isAttribute ) {
99                        os << "__attribute(( )) ";
100                } // if
101                if ( dimension != 0 ) {
102                        CodeGenerator cg( os );
103                        dimension->accept( cg );
104                } else if ( isVarLen ) {
105                        // no dimension expression on a VLA means it came in with the * token
106                        os << "*";
107                } // if
108                os << "]";
109
110                typeString = os.str();
111 
112                base->accept( *this );
113        }
114
115        void GenType::visit( PointerType *pointerType ) {
116                assert( pointerType->get_base() != 0);
117                if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) {
118                        genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() );
119                } else {
120                        handleQualifiers( pointerType );
121                        if ( typeString[ 0 ] == '?' ) {
122                                typeString = "* " + typeString;
123                        } else {
124                                typeString = "*" + typeString;
125                        } // if
126                        pointerType->get_base()->accept( *this );
127                } // if
128        }
129
130        void GenType::visit( ArrayType *arrayType ) {
131                genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );
132        }
133
134        void GenType::visit( FunctionType *funcType ) {
135                std::ostringstream os;
136
137                if ( typeString != "" ) {
138                        if ( typeString[ 0 ] == '*' ) {
139                                os << "(" << typeString << ")";
140                        } else {
141                                os << typeString;
142                        } // if
143                } // if
144 
145                /************* parameters ***************/
146
147                const std::list<DeclarationWithType *> &pars = funcType->get_parameters();
148
149                if ( pars.empty() ) {
150                        if ( funcType->get_isVarArgs() ) {
151                                os << "()";
152                        } else {
153                                os << "(void)";
154                        } // if
155                } else {
156                        CodeGenerator cg( os );
157                        os << "(" ;
158
159                        cg.genCommaList( pars.begin(), pars.end() );
160
161                        if ( funcType->get_isVarArgs() ) {
162                                os << ", ...";
163                        } // if
164                        os << ")";
165                } // if
166 
167                typeString = os.str();
168
169                if ( funcType->get_returnVals().size() == 0 ) {
170                        typeString = "void " + typeString;
171                } else {
172                        funcType->get_returnVals().front()->get_type()->accept( *this );
173                } // if
174        }
175
176        void GenType::visit( StructInstType *structInst )  {
177                typeString = "struct " + structInst->get_name() + " " + typeString;
178                handleQualifiers( structInst );
179        }
180
181        void GenType::visit( UnionInstType *unionInst ) {
182                typeString = "union " + unionInst->get_name() + " " + typeString;
183                handleQualifiers( unionInst );
184        }
185
186        void GenType::visit( EnumInstType *enumInst ) {
187                typeString = "enum " + enumInst->get_name() + " " + typeString;
188                handleQualifiers( enumInst );
189        }
190
191        void GenType::visit( TypeInstType *typeInst ) {
192                typeString = typeInst->get_name() + " " + typeString;
193                handleQualifiers( typeInst );
194        }
195
196        void GenType::visit( VarArgsType *varArgsType ) {
197                typeString = "__builtin_va_list " + typeString;
198                handleQualifiers( varArgsType );
199        }
200
201        void GenType::handleQualifiers( Type *type ) {
202                if ( type->get_isConst() ) {
203                        typeString = "const " + typeString;
204                } // if
205                if ( type->get_isVolatile() ) {
206                        typeString = "volatile " + typeString;
207                } // if
208                if ( type->get_isRestrict() ) {
209                        typeString = "__restrict " + typeString;
210                } // if
211                if ( type->get_isAtomic() ) {
212                        typeString = "_Atomic " + typeString;
213                } // if
214                if ( type->get_isAttribute() ) {
215                        typeString = "__attribute(( )) " + typeString;
216                } // if
217        }
218} // namespace CodeGen
219
220// Local Variables: //
221// tab-width: 4 //
222// mode: c++ //
223// compile-command: "make install" //
224// End: //
Note: See TracBrowser for help on using the repository browser.