source: src/CodeGen/GenType.cc@ 0e76cf4f

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 0e76cf4f was 23b6643f, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

Merge branch 'master' into tuples

Conflicts:

src/Makefile.in
src/ResolvExpr/Unify.cc
src/SynTree/Type.h

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