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