source: src/CodeGen/GenType.cc @ 8c49c0e

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 8c49c0e was 3c13c03, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

expand TupleExpr? and TupleIndexExpr?, add UniqueExpr?

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