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 | |
---|
27 | namespace CodeGen { |
---|
28 | class GenType : public Visitor { |
---|
29 | public: |
---|
30 | GenType( const std::string &typeString, bool mangle = true ); |
---|
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( TupleType * tupleType ); |
---|
44 | virtual void visit( VarArgsType *varArgsType ); |
---|
45 | virtual void visit( ZeroType *zeroType ); |
---|
46 | virtual void visit( OneType *oneType ); |
---|
47 | |
---|
48 | private: |
---|
49 | void handleQualifiers( Type *type ); |
---|
50 | void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ); |
---|
51 | |
---|
52 | std::string typeString; |
---|
53 | bool mangle = true; |
---|
54 | }; |
---|
55 | |
---|
56 | std::string genType( Type *type, const std::string &baseString, bool mangle ) { |
---|
57 | GenType gt( baseString, mangle ); |
---|
58 | type->accept( gt ); |
---|
59 | return gt.get_typeString(); |
---|
60 | } |
---|
61 | |
---|
62 | GenType::GenType( const std::string &typeString, bool mangle ) : typeString( typeString ), mangle( mangle ) {} |
---|
63 | |
---|
64 | void GenType::visit( VoidType *voidType ) { |
---|
65 | typeString = "void " + typeString; |
---|
66 | handleQualifiers( voidType ); |
---|
67 | } |
---|
68 | |
---|
69 | void GenType::visit( BasicType *basicType ) { |
---|
70 | BasicType::Kind kind = basicType->get_kind(); |
---|
71 | assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES ); |
---|
72 | typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString; |
---|
73 | handleQualifiers( basicType ); |
---|
74 | } |
---|
75 | |
---|
76 | void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) { |
---|
77 | std::ostringstream os; |
---|
78 | if ( typeString != "" ) { |
---|
79 | if ( typeString[ 0 ] == '*' ) { |
---|
80 | os << "(" << typeString << ")"; |
---|
81 | } else { |
---|
82 | os << typeString; |
---|
83 | } // if |
---|
84 | } // if |
---|
85 | os << "["; |
---|
86 | |
---|
87 | if ( isStatic ) { |
---|
88 | os << "static "; |
---|
89 | } // if |
---|
90 | if ( qualifiers.isConst ) { |
---|
91 | os << "const "; |
---|
92 | } // if |
---|
93 | if ( qualifiers.isVolatile ) { |
---|
94 | os << "volatile "; |
---|
95 | } // if |
---|
96 | if ( qualifiers.isRestrict ) { |
---|
97 | os << "__restrict "; |
---|
98 | } // if |
---|
99 | if ( qualifiers.isAtomic ) { |
---|
100 | os << "_Atomic "; |
---|
101 | } // if |
---|
102 | if ( qualifiers.isAttribute ) { |
---|
103 | os << "__attribute(( )) "; |
---|
104 | } // if |
---|
105 | if ( dimension != 0 ) { |
---|
106 | CodeGenerator cg( os, mangle ); |
---|
107 | dimension->accept( cg ); |
---|
108 | } else if ( isVarLen ) { |
---|
109 | // no dimension expression on a VLA means it came in with the * token |
---|
110 | os << "*"; |
---|
111 | } // if |
---|
112 | os << "]"; |
---|
113 | |
---|
114 | typeString = os.str(); |
---|
115 | |
---|
116 | base->accept( *this ); |
---|
117 | } |
---|
118 | |
---|
119 | void GenType::visit( PointerType *pointerType ) { |
---|
120 | assert( pointerType->get_base() != 0); |
---|
121 | if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) { |
---|
122 | genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() ); |
---|
123 | } else { |
---|
124 | handleQualifiers( pointerType ); |
---|
125 | if ( typeString[ 0 ] == '?' ) { |
---|
126 | typeString = "* " + typeString; |
---|
127 | } else { |
---|
128 | typeString = "*" + typeString; |
---|
129 | } // if |
---|
130 | pointerType->get_base()->accept( *this ); |
---|
131 | } // if |
---|
132 | } |
---|
133 | |
---|
134 | void GenType::visit( ArrayType *arrayType ) { |
---|
135 | genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() ); |
---|
136 | } |
---|
137 | |
---|
138 | void GenType::visit( FunctionType *funcType ) { |
---|
139 | std::ostringstream os; |
---|
140 | |
---|
141 | if ( typeString != "" ) { |
---|
142 | if ( typeString[ 0 ] == '*' ) { |
---|
143 | os << "(" << typeString << ")"; |
---|
144 | } else { |
---|
145 | os << typeString; |
---|
146 | } // if |
---|
147 | } // if |
---|
148 | |
---|
149 | /************* parameters ***************/ |
---|
150 | |
---|
151 | const std::list<DeclarationWithType *> &pars = funcType->get_parameters(); |
---|
152 | |
---|
153 | if ( pars.empty() ) { |
---|
154 | if ( funcType->get_isVarArgs() ) { |
---|
155 | os << "()"; |
---|
156 | } else { |
---|
157 | os << "(void)"; |
---|
158 | } // if |
---|
159 | } else { |
---|
160 | CodeGenerator cg( os, mangle ); |
---|
161 | os << "(" ; |
---|
162 | |
---|
163 | cg.genCommaList( pars.begin(), pars.end() ); |
---|
164 | |
---|
165 | if ( funcType->get_isVarArgs() ) { |
---|
166 | os << ", ..."; |
---|
167 | } // if |
---|
168 | os << ")"; |
---|
169 | } // if |
---|
170 | |
---|
171 | typeString = os.str(); |
---|
172 | |
---|
173 | if ( funcType->get_returnVals().size() == 0 ) { |
---|
174 | typeString = "void " + typeString; |
---|
175 | } else { |
---|
176 | funcType->get_returnVals().front()->get_type()->accept( *this ); |
---|
177 | } // if |
---|
178 | } |
---|
179 | |
---|
180 | void GenType::visit( StructInstType *structInst ) { |
---|
181 | typeString = "struct " + structInst->get_name() + " " + typeString; |
---|
182 | handleQualifiers( structInst ); |
---|
183 | } |
---|
184 | |
---|
185 | void GenType::visit( UnionInstType *unionInst ) { |
---|
186 | typeString = "union " + unionInst->get_name() + " " + typeString; |
---|
187 | handleQualifiers( unionInst ); |
---|
188 | } |
---|
189 | |
---|
190 | void GenType::visit( EnumInstType *enumInst ) { |
---|
191 | typeString = "enum " + enumInst->get_name() + " " + typeString; |
---|
192 | handleQualifiers( enumInst ); |
---|
193 | } |
---|
194 | |
---|
195 | void GenType::visit( TypeInstType *typeInst ) { |
---|
196 | typeString = typeInst->get_name() + " " + typeString; |
---|
197 | handleQualifiers( typeInst ); |
---|
198 | } |
---|
199 | |
---|
200 | void GenType::visit( TupleType * tupleType ) { |
---|
201 | assertf( ! mangle, "Tuple types should not make it to Code Gen." ); |
---|
202 | Visitor::visit( tupleType ); |
---|
203 | } |
---|
204 | |
---|
205 | void GenType::visit( VarArgsType *varArgsType ) { |
---|
206 | typeString = "__builtin_va_list " + typeString; |
---|
207 | handleQualifiers( varArgsType ); |
---|
208 | } |
---|
209 | |
---|
210 | void GenType::visit( ZeroType *zeroType ) { |
---|
211 | // ideally these wouldn't hit codegen at all, but should be safe to make them ints |
---|
212 | typeString = "long int " + typeString; |
---|
213 | handleQualifiers( zeroType ); |
---|
214 | } |
---|
215 | |
---|
216 | void GenType::visit( OneType *oneType ) { |
---|
217 | // ideally these wouldn't hit codegen at all, but should be safe to make them ints |
---|
218 | typeString = "long int " + typeString; |
---|
219 | handleQualifiers( oneType ); |
---|
220 | } |
---|
221 | |
---|
222 | void GenType::handleQualifiers( Type *type ) { |
---|
223 | if ( type->get_isConst() ) { |
---|
224 | typeString = "const " + typeString; |
---|
225 | } // if |
---|
226 | if ( type->get_isVolatile() ) { |
---|
227 | typeString = "volatile " + typeString; |
---|
228 | } // if |
---|
229 | if ( type->get_isRestrict() ) { |
---|
230 | typeString = "__restrict " + typeString; |
---|
231 | } // if |
---|
232 | if ( type->get_isAtomic() ) { |
---|
233 | typeString = "_Atomic " + typeString; |
---|
234 | } // if |
---|
235 | } |
---|
236 | } // namespace CodeGen |
---|
237 | |
---|
238 | // Local Variables: // |
---|
239 | // tab-width: 4 // |
---|
240 | // mode: c++ // |
---|
241 | // compile-command: "make install" // |
---|
242 | // End: // |
---|