source: src/CodeGen/GenType.cc @ e76acbe

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 e76acbe was 486341f, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

add option to CodeGen? to output unmangled name, add ctorWarnings test

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[51587aa]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//
[486341f]7// GenType.cc --
[51587aa]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[1db21619]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Jul  9 16:43:52 2015
13// Update Count     : 13
[51587aa]14//
15
[5f2f2d7]16#include <sstream>
[51b7345]17#include <cassert>
18
19#include "GenType.h"
[a61fea9a]20#include "CodeGenerator.h"
[e8032b0]21
22#include "SynTree/Declaration.h"
[51b7345]23#include "SynTree/Expression.h"
[e8032b0]24#include "SynTree/Type.h"
25#include "SynTree/Visitor.h"
[51b7345]26
27namespace CodeGen {
[51587aa]28        class GenType : public Visitor {
29          public:
[486341f]30                GenType( const std::string &typeString, bool mangle = true );
[51587aa]31                std::string get_typeString() const { return typeString; }
32                void set_typeString( const std::string &newValue ) { typeString = newValue; }
[486341f]33
[51587aa]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 );
[44b7088]43                virtual void visit( VarArgsType *varArgsType );
[486341f]44
[51587aa]45          private:
46                void handleQualifiers( Type *type );
47                void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
[486341f]48
[51587aa]49                std::string typeString;
[486341f]50                bool mangle = true;
[51587aa]51        };
52
[486341f]53        std::string genType( Type *type, const std::string &baseString, bool mangle ) {
54                GenType gt( baseString, mangle );
[51587aa]55                type->accept( gt );
56                return gt.get_typeString();
57        }
58
[486341f]59        GenType::GenType( const std::string &typeString, bool mangle ) : typeString( typeString ), mangle( mangle ) {}
[51587aa]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 ) {
[5f2f2d7]74                std::ostringstream os;
[51587aa]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
[1db21619]99                if ( qualifiers.isAttribute ) {
100                        os << "__attribute(( )) ";
101                } // if
[51587aa]102                if ( dimension != 0 ) {
[486341f]103                        CodeGenerator cg( os, mangle );
[51587aa]104                        dimension->accept( cg );
[71bd8c6]105                } else if ( isVarLen ) {
[1db21619]106                        // no dimension expression on a VLA means it came in with the * token
[71bd8c6]107                        os << "*";
[51587aa]108                } // if
109                os << "]";
110
[5f2f2d7]111                typeString = os.str();
[486341f]112
[51587aa]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
[a08ba92]131        void GenType::visit( ArrayType *arrayType ) {
[51587aa]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 ) {
[5f2f2d7]136                std::ostringstream os;
[51587aa]137
138                if ( typeString != "" ) {
139                        if ( typeString[ 0 ] == '*' ) {
140                                os << "(" << typeString << ")";
141                        } else {
142                                os << typeString;
143                        } // if
144                } // if
[486341f]145
[51587aa]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 {
[486341f]157                        CodeGenerator cg( os, mangle );
[51587aa]158                        os << "(" ;
159
160                        cg.genCommaList( pars.begin(), pars.end() );
161
[a08ba92]162                        if ( funcType->get_isVarArgs() ) {
[51587aa]163                                os << ", ...";
164                        } // if
165                        os << ")";
166                } // if
[486341f]167
[5f2f2d7]168                typeString = os.str();
[51587aa]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
[44b7088]197        void GenType::visit( VarArgsType *varArgsType ) {
198                typeString = "__builtin_va_list " + typeString;
[540ddb7d]199                handleQualifiers( varArgsType );
[44b7088]200        }
201
[51587aa]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
[1db21619]215                if ( type->get_isAttribute() ) {
216                        typeString = "__attribute(( )) " + typeString;
217                } // if
[51587aa]218        }
[51b7345]219} // namespace CodeGen
[51587aa]220
221// Local Variables: //
222// tab-width: 4 //
223// mode: c++ //
224// compile-command: "make install" //
225// End: //
Note: See TracBrowser for help on using the repository browser.