source: src/Parser/TypeData.cc @ 37bf576

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 37bf576 was a5a71d0, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'fix-memory-error' into ctor

Conflicts:

src/CodeGen/CodeGenerator.cc
src/Makefile.in
src/Parser/DeclarationNode.cc
src/Parser/ParseNode.h
src/Parser/TypeData.cc
src/Parser/parser.cc
src/Parser/parser.yy
src/ResolvExpr/Resolver.cc
src/SymTab/Validate.cc
src/SynTree/Declaration.h
src/SynTree/Mutator.cc
src/SynTree/Mutator.h
src/SynTree/SynTree.h
src/SynTree/Visitor.cc
src/SynTree/Visitor.h
src/libcfa/prelude.cf

  • Property mode set to 100644
File size: 29.2 KB
RevLine 
[b87a5ed]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//
[f1e012b]7// TypeData.cc --
[b87a5ed]8//
9// Author           : Rodolfo G. Esteves
10// Created On       : Sat May 16 15:12:51 2015
[f1e012b]11// Last Modified By : Rob Schluntz
[a5a71d0]12// Last Modified On : Wed Apr 06 16:57:53 2016
[4040425]13// Update Count     : 49
[b87a5ed]14//
15
[51b7345]16#include <cassert>
17#include <algorithm>
18#include <iterator>
[d3b7937]19#include "Common/utility.h"
[51b7345]20#include "TypeData.h"
21#include "SynTree/Type.h"
22#include "SynTree/Declaration.h"
23#include "SynTree/Expression.h"
24#include "SynTree/Statement.h"
[90c3b1c]25#include "SynTree/Initializer.h"
[51b7345]26
[c8ffe20b]27TypeData::TypeData( Kind k ) : kind( k ), base( 0 ), forall( 0 ) {
[b87a5ed]28        switch ( kind ) {
29          case Unknown:
30          case Pointer:
31          case EnumConstant:
32                // nothing else to initialize
33                break;
34          case Basic:
35                basic = new Basic_t;
36                break;
37          case Array:
38                array = new Array_t;
39                array->dimension = 0;
40                array->isVarLen = false;
41                array->isStatic = false;
42                break;
43          case Function:
44                function = new Function_t;
45                function->params = 0;
46                function->idList = 0;
47                function->oldDeclList = 0;
48                function->body = 0;
49                function->hasBody = false;
50                function->newStyle = false;
51                break;
52          case Aggregate:
53                aggregate = new Aggregate_t;
54                aggregate->params = 0;
55                aggregate->actuals = 0;
[721f17a]56                aggregate->fields = 0;
[b87a5ed]57                break;
58          case AggregateInst:
59                aggInst = new AggInst_t;
60                aggInst->aggregate = 0;
61                aggInst->params = 0;
62                break;
63          case Enum:
64                enumeration = new Enumeration_t;
65                enumeration->constants = 0;
66                break;
67          case Symbolic:
68          case SymbolicInst:
69                symbolic = new Symbolic_t;
70                symbolic->params = 0;
71                symbolic->actuals = 0;
72                symbolic->assertions = 0;
73                break;
74          case Variable:
75                variable = new Variable_t;
76                variable->tyClass = DeclarationNode::Type;
77                variable->assertions = 0;
78                break;
79          case Tuple:
80                tuple = new Tuple_t;
81                tuple->members = 0;
82                break;
83          case Typeof:
84                typeexpr = new Typeof_t;
85                typeexpr->expr = 0;
86                break;
[90c3b1c]87          case Builtin:
88                builtin = new Builtin_t;
89                break;
[b87a5ed]90          case Attr:
91                attr = new Attr_t;
92                attr->expr = 0;
93                attr->type = 0;
94                break;
[68cd1ce]95        } // switch
[51b7345]96}
97
[c8ffe20b]98TypeData::~TypeData() {
[b87a5ed]99        delete base;
100        delete forall;
101
102        switch ( kind ) {
103          case Unknown:
104          case Pointer:
105          case EnumConstant:
106                // nothing to destroy
107                break;
108          case Basic:
109                delete basic;
110                break;
111          case Array:
112                delete array->dimension;
113                delete array;
114                break;
115          case Function:
116                delete function->params;
117                delete function->idList;
118                delete function->oldDeclList;
119                delete function->body;
120                delete function;
121                break;
122          case Aggregate:
123                delete aggregate->params;
124                delete aggregate->actuals;
[721f17a]125                delete aggregate->fields;
[b87a5ed]126                delete aggregate;
127                break;
128          case AggregateInst:
129                delete aggInst->aggregate;
130                delete aggInst->params;
131                delete aggInst;
132                break;
133          case Enum:
134                delete enumeration->constants;
135                delete enumeration;
136                break;
137          case Symbolic:
138          case SymbolicInst:
139                delete symbolic->params;
140                delete symbolic->actuals;
141                delete symbolic->assertions;
142                delete symbolic;
143                break;
144          case Variable:
145                delete variable->assertions;
146                delete variable;
147                break;
148          case Tuple:
149                delete tuple->members;
150                delete tuple;
151                break;
152          case Typeof:
153                delete typeexpr->expr;
154                delete typeexpr;
155                break;
[90c3b1c]156          case Builtin:
157                delete builtin;
158                break;
[b87a5ed]159          case Attr:
160                delete attr->expr;
161                delete attr->type;
162                delete attr;
163                break;
[68cd1ce]164        } // switch
[51b7345]165}
166
[c8ffe20b]167TypeData *TypeData::clone() const {
[b87a5ed]168        TypeData *newtype = new TypeData( kind );
169        newtype->qualifiers = qualifiers;
170        newtype->base = maybeClone( base );
171        newtype->forall = maybeClone( forall );
172
173        switch ( kind ) {
174          case Unknown:
175          case EnumConstant:
176          case Pointer:
177                // nothing else to copy
178                break;
179          case Basic:
180                newtype->basic->typeSpec = basic->typeSpec;
181                newtype->basic->modifiers = basic->modifiers;
182                break;
183          case Array:
184                newtype->array->dimension = maybeClone( array->dimension );
185                newtype->array->isVarLen = array->isVarLen;
186                newtype->array->isStatic = array->isStatic;
187                break;
188          case Function:
189                newtype->function->params = maybeClone( function->params );
190                newtype->function->idList = maybeClone( function->idList );
191                newtype->function->oldDeclList = maybeClone( function->oldDeclList );
192                newtype->function->body = maybeClone( function->body );
193                newtype->function->hasBody = function->hasBody;
194                newtype->function->newStyle = function->newStyle;
195                break;
196          case Aggregate:
197                newtype->aggregate->params = maybeClone( aggregate->params );
198                newtype->aggregate->actuals = maybeClone( aggregate->actuals );
[721f17a]199                newtype->aggregate->fields = maybeClone( aggregate->fields );
[b87a5ed]200                newtype->aggregate->name = aggregate->name;
201                newtype->aggregate->kind = aggregate->kind;
202                break;
203          case AggregateInst:
204                newtype->aggInst->aggregate = maybeClone( aggInst->aggregate );
205                newtype->aggInst->params = maybeClone( aggInst->params );
206                break;
207          case Enum:
208                newtype->enumeration->name = enumeration->name;
209                newtype->enumeration->constants = maybeClone( enumeration->constants );
210                break;
211          case Symbolic:
212          case SymbolicInst:
213                newtype->symbolic->params = maybeClone( symbolic->params );
214                newtype->symbolic->actuals = maybeClone( symbolic->actuals );
215                newtype->symbolic->assertions = maybeClone( symbolic->assertions );
216                newtype->symbolic->isTypedef = symbolic->isTypedef;
217                newtype->symbolic->name = symbolic->name;
218                break;
219          case Variable:
220                newtype->variable->assertions = maybeClone( variable->assertions );
221                newtype->variable->name = variable->name;
222                newtype->variable->tyClass = variable->tyClass;
223                break;
224          case Tuple:
225                newtype->tuple->members = maybeClone( tuple->members );
226                break;
227          case Typeof:
228                newtype->typeexpr->expr = maybeClone( typeexpr->expr );
229                break;
[90c3b1c]230          case Builtin:
231                newtype->builtin->type = builtin->type;
232                break;
[b87a5ed]233          case Attr:
234                newtype->attr->expr = maybeClone( attr->expr );
235                newtype->attr->type = maybeClone( attr->type );
236                break;
[68cd1ce]237        } // switch
[b87a5ed]238        return newtype;
[c8ffe20b]239}
[51b7345]240
[c8ffe20b]241void TypeData::print( std::ostream &os, int indent ) const {
[b87a5ed]242        using std::endl;
243        using std::string;
244
245        printEnums( qualifiers.begin(), qualifiers.end(), DeclarationNode::qualifierName, os );
246
247        if ( forall ) {
248                os << "forall " << endl;
[721f17a]249                forall->printList( os, indent + 4 );
[68cd1ce]250        } // if
[b87a5ed]251
252        switch ( kind ) {
253          case Unknown:
254                os << "entity of unknown type ";
255                break;
256          case Pointer:
257                os << "pointer ";
258                if ( base ) {
259                        os << "to ";
260                        base->print( os, indent );
[68cd1ce]261                } // if
[b87a5ed]262                break;
263          case EnumConstant:
264                os << "enumeration constant ";
265                break;
266          case Basic:
267                printEnums( basic->modifiers.begin(), basic->modifiers.end(), DeclarationNode::modifierName, os );
268                printEnums( basic->typeSpec.begin(), basic->typeSpec.end(), DeclarationNode::basicTypeName, os );
269                break;
270          case Array:
271                if ( array->isStatic ) {
272                        os << "static ";
[68cd1ce]273                } // if
[b87a5ed]274                if ( array->dimension ) {
275                        os << "array of ";
276                        array->dimension->printOneLine( os, indent );
277                } else if ( array->isVarLen ) {
278                        os << "variable-length array of ";
279                } else {
280                        os << "open array of ";
[68cd1ce]281                } // if
[b87a5ed]282                if ( base ) {
283                        base->print( os, indent );
[68cd1ce]284                } // if
[b87a5ed]285                break;
286          case Function:
287                os << "function" << endl;
288                if ( function->params ) {
[721f17a]289                        os << string( indent + 2, ' ' ) << "with parameters " << endl;
290                        function->params->printList( os, indent + 4 );
[b87a5ed]291                } else {
[721f17a]292                        os << string( indent + 2, ' ' ) << "with no parameters " << endl;
[68cd1ce]293                } // if
[b87a5ed]294                if ( function->idList ) {
[721f17a]295                        os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
296                        function->idList->printList( os, indent + 4 );
[68cd1ce]297                } // if
[b87a5ed]298                if ( function->oldDeclList ) {
[721f17a]299                        os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
300                        function->oldDeclList->printList( os, indent + 4 );
[68cd1ce]301                } // if
[721f17a]302                os << string( indent + 2, ' ' ) << "returning ";
[b87a5ed]303                if ( base ) {
[721f17a]304                        base->print( os, indent + 4 );
[b87a5ed]305                } else {
306                        os << "nothing ";
[68cd1ce]307                } // if
[b87a5ed]308                os << endl;
309                if ( function->hasBody ) {
[721f17a]310                        os << string( indent + 2, ' ' ) << "with body " << endl;
[68cd1ce]311                } // if
[b87a5ed]312                if ( function->body ) {
[721f17a]313                        function->body->printList( os, indent + 2 );
[68cd1ce]314                } // if
[b87a5ed]315                break;
316          case Aggregate:
[68cd1ce]317                os << DeclarationNode::aggregateName[ aggregate->kind ] << ' ' << aggregate->name << endl;
[b87a5ed]318                if ( aggregate->params ) {
[721f17a]319                        os << string( indent + 2, ' ' ) << "with type parameters " << endl;
320                        aggregate->params->printList( os, indent + 4 );
[68cd1ce]321                } // if
[b87a5ed]322                if ( aggregate->actuals ) {
[721f17a]323                        os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl;
324                        aggregate->actuals->printList( os, indent + 4 );
[68cd1ce]325                } // if
[721f17a]326                if ( aggregate->fields ) {
327                        os << string( indent + 2, ' ' ) << "with members " << endl;
328                        aggregate->fields->printList( os, indent + 4 );
[51b7345]329///     } else {
[721f17a]330///       os << string( indent + 2, ' ' ) << "with no members " << endl;
[68cd1ce]331                } // if
[b87a5ed]332                break;
333          case AggregateInst:
334                if ( aggInst->aggregate ) {
335                        os << "instance of " ;
336                        aggInst->aggregate->print( os, indent );
337                } else {
338                        os << "instance of an unspecified aggregate ";
[68cd1ce]339                } // if
[b87a5ed]340                if ( aggInst->params ) {
[721f17a]341                        os << string( indent + 2, ' ' ) << "with parameters " << endl;
342                        aggInst->params->printList( os, indent + 2 );
[68cd1ce]343                } // if
[b87a5ed]344                break;
345          case Enum:
346                os << "enumeration ";
347                if ( enumeration->constants ) {
348                        os << "with constants" << endl;
[721f17a]349                        enumeration->constants->printList( os, indent + 2 );
[68cd1ce]350                } // if
[b87a5ed]351                break;
352          case SymbolicInst:
353                os << "instance of type " << symbolic->name;
354                if ( symbolic->actuals ) {
355                        os << " with parameters" << endl;
356                        symbolic->actuals->printList( os, indent + 2 );
[68cd1ce]357                } // if
[b87a5ed]358                break;
359          case Symbolic:
360                if ( symbolic->isTypedef ) {
361                        os << "typedef definition ";
362                } else {
363                        os << "type definition ";
[68cd1ce]364                } // if
[b87a5ed]365                if ( symbolic->params ) {
[721f17a]366                        os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
[b87a5ed]367                        symbolic->params->printList( os, indent + 2 );
[68cd1ce]368                } // if
[b87a5ed]369                if ( symbolic->assertions ) {
[721f17a]370                        os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
[b87a5ed]371                        symbolic->assertions->printList( os, indent + 4 );
[721f17a]372                        os << string( indent + 2, ' ' );
[68cd1ce]373                } // if
[b87a5ed]374                if ( base ) {
375                        os << "for ";
376                        base->print( os, indent + 2 );
[68cd1ce]377                } // if
[b87a5ed]378                break;
379          case Variable:
380                os << DeclarationNode::typeClassName[ variable->tyClass ] << " variable ";
381                if ( variable->assertions ) {
[721f17a]382                        os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
[b87a5ed]383                        variable->assertions->printList( os, indent + 4 );
[721f17a]384                        os << string( indent + 2, ' ' );
[68cd1ce]385                } // if
[b87a5ed]386                break;
387          case Tuple:
388                os << "tuple ";
389                if ( tuple->members ) {
390                        os << "with members " << endl;
391                        tuple->members->printList( os, indent + 2 );
[68cd1ce]392                } // if
[b87a5ed]393                break;
394          case Typeof:
395                os << "type-of expression ";
396                if ( typeexpr->expr ) {
397                        typeexpr->expr->print( os, indent + 2 );
[68cd1ce]398                } // if
[b87a5ed]399                break;
400          case Attr:
401                os << "attribute type decl " << attr->name << " applied to ";
402                if ( attr->expr ) {
403                        attr->expr->print( os, indent + 2 );
[68cd1ce]404                } // if
[b87a5ed]405                if ( attr->type ) {
406                        attr->type->print( os, indent + 2 );
[68cd1ce]407                } // if
[b87a5ed]408                break;
[90c3b1c]409          case Builtin:
410                os << "gcc builtin type";
411                break;
[1db21619]412          default:
413                os << "internal error: TypeData::print " << kind  << endl;
414                assert( false );
[68cd1ce]415        } // switch
[51b7345]416}
417
[c8ffe20b]418TypeData *TypeData::extractAggregate( bool toplevel ) const {
[b87a5ed]419        TypeData *ret = 0;
[51b7345]420
[b87a5ed]421        switch ( kind ) {
422          case Aggregate:
[721f17a]423                if ( ! toplevel && aggregate->fields ) {
[b87a5ed]424                        ret = clone();
425                        ret->qualifiers.clear();
[68cd1ce]426                } // if
[b87a5ed]427                break;
428          case Enum:
[a32b204]429                if ( ! toplevel && enumeration->constants ) {
[b87a5ed]430                        ret = clone();
431                        ret->qualifiers.clear();
[68cd1ce]432                } // if
[b87a5ed]433                break;
434          case AggregateInst:
435                if ( aggInst->aggregate ) {
436                        ret = aggInst->aggregate->extractAggregate( false );
[68cd1ce]437                } // if
[b87a5ed]438                break;
439          default:
440                if ( base ) {
441                        ret = base->extractAggregate( false );
[68cd1ce]442                } // if
443        } // switch
[b87a5ed]444        return ret;
[51b7345]445}
446
[c8ffe20b]447void buildForall( const DeclarationNode *firstNode, std::list< TypeDecl* > &outputList ) {
[b87a5ed]448        buildList( firstNode, outputList );
449        for ( std::list< TypeDecl* >::iterator i = outputList.begin(); i != outputList.end(); ++i ) {
450                if ( (*i)->get_kind() == TypeDecl::Any ) {
[6943f051]451                        // add assertion parameters to `type' tyvars in reverse order
452                        // add dtor:  void ^?{}(T *)
453                        FunctionType *dtorType = new FunctionType( Type::Qualifiers(), false );
454                        dtorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) );
455                        (*i)->get_assertions().push_front( new FunctionDecl( "^?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, dtorType, 0, false, false ) );
[4cc4286]456
[a9a259c]457                        // add copy ctor:  void ?{}(T *, T)
458                        FunctionType *copyCtorType = new FunctionType( Type::Qualifiers(), false );
459                        copyCtorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) );
460                        copyCtorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) );
461                        (*i)->get_assertions().push_front( new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, copyCtorType, 0, false, false ) );
462
[6943f051]463                        // add default ctor:  void ?{}(T *)
464                        FunctionType *ctorType = new FunctionType( Type::Qualifiers(), false );
465                        ctorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) );
466                        (*i)->get_assertions().push_front( new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, ctorType, 0, false, false ) );
467
468                        // add assignment operator:  T * ?=?(T *, T)
469                        FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
470                        assignType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) );
471                        assignType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) );
472                        assignType->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) );
473                        (*i)->get_assertions().push_front( new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, assignType, 0, false, false ) );
[68cd1ce]474                } // if
475        } // for
[51b7345]476}
477
[de62360d]478Declaration *TypeData::buildDecl( std::string name, DeclarationNode::StorageClass sc, Expression *bitfieldWidth, bool isInline, bool isNoreturn, LinkageSpec::Type linkage, Initializer *init ) const {
[b87a5ed]479        if ( kind == TypeData::Function ) {
480                FunctionDecl *decl;
481                if ( function->hasBody ) {
482                        if ( function->body ) {
483                                Statement *stmt = function->body->build();
484                                CompoundStmt *body = dynamic_cast< CompoundStmt* >( stmt );
485                                assert( body );
[de62360d]486                                decl = new FunctionDecl( name, sc, linkage, buildFunction(), body, isInline, isNoreturn );
[b87a5ed]487                        } else {
488                                // std::list<Label> ls;
[de62360d]489                                decl = new FunctionDecl( name, sc, linkage, buildFunction(), new CompoundStmt( std::list<Label>() ), isInline, isNoreturn );
[68cd1ce]490                        } // if
[b87a5ed]491                } else {
[de62360d]492                        decl = new FunctionDecl( name, sc, linkage, buildFunction(), 0, isInline, isNoreturn );
[68cd1ce]493                } // if
[b87a5ed]494                for ( DeclarationNode *cur = function->idList; cur != 0; cur = dynamic_cast< DeclarationNode* >( cur->get_link() ) ) {
495                        if ( cur->get_name() != "" ) {
496                                decl->get_oldIdents().insert( decl->get_oldIdents().end(), cur->get_name() );
[68cd1ce]497                        } // if
498                } // for
[b87a5ed]499                buildList( function->oldDeclList, decl->get_oldDecls() );
500                return decl;
501        } else if ( kind == TypeData::Aggregate ) {
502                return buildAggregate();
503        } else if ( kind == TypeData::Enum ) {
504                return buildEnum();
505        } else if ( kind == TypeData::Symbolic ) {
506                return buildSymbolic( name, sc );
507        } else if ( kind == TypeData::Variable ) {
508                return buildVariable();
[c8ffe20b]509        } else {
[1db21619]510                return new ObjectDecl( name, sc, linkage, bitfieldWidth, build(), init, isInline, isNoreturn );
[68cd1ce]511        } // if
[b87a5ed]512        return 0;
[51b7345]513}
514
[c8ffe20b]515Type *TypeData::build() const {
[b87a5ed]516        switch ( kind ) {
517          case Unknown:
518                // fill in implicit int
519                return new BasicType( buildQualifiers(), BasicType::SignedInt );
520          case Basic:
521                return buildBasicType();
522          case Pointer:
523                return buildPointer();
524          case Array:
525                return buildArray();
526          case Function:
527                return buildFunction();
528          case AggregateInst:
529                return buildAggInst();
530          case EnumConstant:
531                // the name gets filled in later -- by SymTab::Validate
532                return new EnumInstType( buildQualifiers(), "" );
533          case SymbolicInst:
534                return buildSymbolicInst();;
535          case Tuple:
536                return buildTuple();
537          case Typeof:
538                return buildTypeof();
[90c3b1c]539          case Builtin:
[8f610e85]540                return new VarArgsType( buildQualifiers() );
[b87a5ed]541          case Attr:
542                return buildAttr();
543          case Symbolic:
544          case Enum:
545          case Aggregate:
546          case Variable:
547                assert( false );
[68cd1ce]548        } // switch
[b87a5ed]549        return 0;
[c8ffe20b]550}
[51b7345]551
[c8ffe20b]552Type::Qualifiers TypeData::buildQualifiers() const {
[b87a5ed]553        Type::Qualifiers q;
554        for ( std::list< DeclarationNode::Qualifier >::const_iterator i = qualifiers.begin(); i != qualifiers.end(); ++i ) {
555                switch ( *i ) {
556                  case DeclarationNode::Const:
557                        q.isConst = true;
558                        break;
559                  case DeclarationNode::Volatile:
560                        q.isVolatile = true;
561                        break;
562                  case DeclarationNode::Restrict:
563                        q.isRestrict = true;
564                        break;
565                  case DeclarationNode::Lvalue:
566                        q.isLvalue = true;
567                        break;
568                  case DeclarationNode::Atomic:
569                        q.isAtomic = true;
570                        break;
[68cd1ce]571                } // switch
572        } // for
[b87a5ed]573        return q;
[c8ffe20b]574}
[51b7345]575
[c8ffe20b]576Type *TypeData::buildBasicType() const {
[b87a5ed]577        static const BasicType::Kind kindMap[] = { BasicType::Char, BasicType::SignedInt, BasicType::Float, BasicType::Double,
578                                                                                           BasicType::Char /* void */, BasicType::Bool, BasicType::DoubleComplex,
579                                                                                           BasicType::DoubleImaginary };
580        bool init = false;
581        bool sawDouble = false;
582        bool sawSigned = false;
583        BasicType::Kind ret;
584
585        for ( std::list< DeclarationNode::BasicType >::const_iterator i = basic->typeSpec.begin(); i != basic->typeSpec.end(); ++i ) {
[a32b204]586                if ( ! init ) {
[b87a5ed]587                        init = true;
588                        if ( *i == DeclarationNode::Void ) {
[a32b204]589                                if ( basic->typeSpec.size() != 1 || ! basic->modifiers.empty() ) {
[b87a5ed]590                                        throw SemanticError( "invalid type specifier \"void\" in type: ", this );
591                                } else {
592                                        return new VoidType( buildQualifiers() );
[68cd1ce]593                                } // if
[b87a5ed]594                        } else {
595                                ret = kindMap[ *i ];
[68cd1ce]596                        } // if
[c8ffe20b]597                } else {
[b87a5ed]598                        switch ( *i ) {
599                          case DeclarationNode::Float:
600                                if ( sawDouble ) {
601                                        throw SemanticError( "invalid type specifier \"float\" in type: ", this );
602                                } else {
603                                        switch ( ret ) {
604                                          case BasicType::DoubleComplex:
605                                                ret = BasicType::FloatComplex;
606                                                break;
607                                          case BasicType::DoubleImaginary:
608                                                ret = BasicType::FloatImaginary;
609                                                break;
610                                          default:
611                                                throw SemanticError( "invalid type specifier \"float\" in type: ", this );
[68cd1ce]612                                        } // switch
613                                } // if
[b87a5ed]614                                break;
615                          case DeclarationNode::Double:
616                                if ( sawDouble ) {
617                                        throw SemanticError( "duplicate type specifier \"double\" in type: ", this );
618                                } else {
619                                        switch ( ret ) {
620                                          case BasicType::DoubleComplex:
621                                          case BasicType::DoubleImaginary:
622                                                break;
623                                          default:
624                                                throw SemanticError( "invalid type specifier \"double\" in type: ", this );
[68cd1ce]625                                        } // switch
626                                } // if
[b87a5ed]627                                break;
628                          case DeclarationNode::Complex:
629                                switch ( ret ) {
630                                  case BasicType::Float:
631                                        ret = BasicType::FloatComplex;
632                                        break;
633                                  case BasicType::Double:
634                                        ret = BasicType::DoubleComplex;
635                                        break;
636                                  default:
637                                        throw SemanticError( "invalid type specifier \"_Complex\" in type: ", this );
[68cd1ce]638                                } // switch
[b87a5ed]639                                break;
640                          case DeclarationNode::Imaginary:
641                                switch ( ret ) {
642                                  case BasicType::Float:
643                                        ret = BasicType::FloatImaginary;
644                                        break;
645                                  case BasicType::Double:
646                                        ret = BasicType::DoubleImaginary;
647                                        break;
648                                  default:
649                                        throw SemanticError( "invalid type specifier \"_Imaginary\" in type: ", this );
[68cd1ce]650                                } // switch
[b87a5ed]651                                break;
652                          default:
653                                throw SemanticError( std::string( "invalid type specifier \"" ) + DeclarationNode::basicTypeName[ *i ] + "\" in type: ", this );
[68cd1ce]654                        } // switch
655                } // if
[b87a5ed]656                if ( *i == DeclarationNode::Double ) {
657                        sawDouble = true;
[68cd1ce]658                } // if
659        } // for
[b87a5ed]660
661        for ( std::list< DeclarationNode::Modifier >::const_iterator i = basic->modifiers.begin(); i != basic->modifiers.end(); ++i ) {
662                switch ( *i ) {
663                  case DeclarationNode::Long:
[a32b204]664                        if ( ! init ) {
[b87a5ed]665                                init = true;
666                                ret = BasicType::LongSignedInt;
667                        } else {
668                                switch ( ret ) {
669                                  case BasicType::SignedInt:
670                                        ret = BasicType::LongSignedInt;
671                                        break;
672                                  case BasicType::UnsignedInt:
673                                        ret = BasicType::LongUnsignedInt;
674                                        break;
675                                  case BasicType::LongSignedInt:
676                                        ret = BasicType::LongLongSignedInt;
677                                        break;
678                                  case BasicType::LongUnsignedInt:
679                                        ret = BasicType::LongLongUnsignedInt;
680                                        break;
681                                  case BasicType::Double:
682                                        ret = BasicType::LongDouble;
683                                        break;
684                                  case BasicType::DoubleComplex:
685                                        ret = BasicType::LongDoubleComplex;
686                                        break;
687                                  case BasicType::DoubleImaginary:
688                                        ret = BasicType::LongDoubleImaginary;
689                                        break;
690                                  default:
691                                        throw SemanticError( "invalid type modifier \"long\" in type: ", this );
[68cd1ce]692                                } // switch
693                        } // if
[c8ffe20b]694                        break;
[b87a5ed]695                  case DeclarationNode::Short:
[a32b204]696                        if ( ! init ) {
[b87a5ed]697                                init = true;
698                                ret = BasicType::ShortSignedInt;
699                        } else {
700                                switch ( ret ) {
701                                  case BasicType::SignedInt:
702                                        ret = BasicType::ShortSignedInt;
703                                        break;
704                                  case BasicType::UnsignedInt:
705                                        ret = BasicType::ShortUnsignedInt;
706                                        break;
707                                  default:
708                                        throw SemanticError( "invalid type modifier \"short\" in type: ", this );
[68cd1ce]709                                } // switch
710                        } // if
[c8ffe20b]711                        break;
[b87a5ed]712                  case DeclarationNode::Signed:
[a32b204]713                        if ( ! init ) {
[b87a5ed]714                                init = true;
715                                ret = BasicType::SignedInt;
716                        } else if ( sawSigned ) {
717                                throw SemanticError( "duplicate type modifer \"signed\" in type: ", this );
718                        } else {
719                                switch ( ret ) {
[59db689]720                                  case BasicType::LongLongSignedInt:
[b87a5ed]721                                        ret = BasicType::LongLongUnsignedInt;
722                                        break;
723                                  case BasicType::LongSignedInt:
724                                        ret = BasicType::LongUnsignedInt;
725                                        break;
726                                  case BasicType::SignedInt:
727                                  case BasicType::ShortSignedInt:
728                                        break;
729                                  case BasicType::Char:
730                                        ret = BasicType::SignedChar;
731                                        break;
732                                  default:
733                                        throw SemanticError( "invalid type modifer \"signed\" in type: ", this );
[68cd1ce]734                                } // switch
735                        } // if
[b87a5ed]736                        break;
737                  case DeclarationNode::Unsigned:
[a32b204]738                        if ( ! init ) {
[b87a5ed]739                                init = true;
740                                ret = BasicType::UnsignedInt;
741                        } else if ( sawSigned ) {
742                                throw SemanticError( "invalid type modifer \"unsigned\" in type: ", this );
743                        } else {
744                                switch ( ret ) {
[59db689]745                                  case BasicType::LongLongSignedInt:
[b87a5ed]746                                        ret = BasicType::LongLongUnsignedInt;
747                                        break;
748                                  case BasicType::LongSignedInt:
749                                        ret = BasicType::LongUnsignedInt;
750                                        break;
751                                  case BasicType::SignedInt:
752                                        ret = BasicType::UnsignedInt;
753                                        break;
754                                  case BasicType::ShortSignedInt:
755                                        ret = BasicType::ShortUnsignedInt;
756                                        break;
757                                  case BasicType::Char:
758                                        ret = BasicType::UnsignedChar;
759                                        break;
760                                  default:
761                                        throw SemanticError( "invalid type modifer \"unsigned\" in type: ", this );
[68cd1ce]762                                } // switch
763                        } // if
[c8ffe20b]764                        break;
[68cd1ce]765                } // switch
[b87a5ed]766
767                if ( *i == DeclarationNode::Signed ) {
768                        sawSigned = true;
[68cd1ce]769                } // if
770        } // for
[51b7345]771
[b87a5ed]772        BasicType *bt;
[a32b204]773        if ( ! init ) {
[b87a5ed]774                bt = new BasicType( buildQualifiers(), BasicType::SignedInt );
775        } else {
776                bt = new BasicType( buildQualifiers(), ret );
[68cd1ce]777        } // if
[b87a5ed]778        buildForall( forall, bt->get_forall() );
779        return bt;
[51b7345]780}
781
782
[c8ffe20b]783PointerType *TypeData::buildPointer() const {
[b87a5ed]784        PointerType *pt;
785        if ( base ) {
786                pt = new PointerType( buildQualifiers(), base->build() );
787        } else {
788                pt = new PointerType( buildQualifiers(), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
[68cd1ce]789        } // if
[b87a5ed]790        buildForall( forall, pt->get_forall() );
791        return pt;
[51b7345]792}
793
[c8ffe20b]794ArrayType *TypeData::buildArray() const {
[b87a5ed]795        ArrayType *at;
796        if ( base ) {
797                at = new ArrayType( buildQualifiers(), base->build(), maybeBuild< Expression >( array->dimension ),
798                                                        array->isVarLen, array->isStatic );
799        } else {
800                at = new ArrayType( buildQualifiers(), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
801                                                        maybeBuild< Expression >( array->dimension ), array->isVarLen, array->isStatic );
[68cd1ce]802        } // if
[b87a5ed]803        buildForall( forall, at->get_forall() );
804        return at;
[51b7345]805}
806
[c8ffe20b]807FunctionType *TypeData::buildFunction() const {
[b87a5ed]808        assert( kind == Function );
809        bool hasEllipsis = function->params ? function->params->get_hasEllipsis() : true;
[a32b204]810        if ( ! function->params ) hasEllipsis = ! function->newStyle;
[b87a5ed]811        FunctionType *ft = new FunctionType( buildQualifiers(), hasEllipsis );
812        buildList( function->params, ft->get_parameters() );
813        buildForall( forall, ft->get_forall() );
814        if ( base ) {
815                switch ( base->kind ) {
816                  case Tuple:
817                        buildList( base->tuple->members, ft->get_returnVals() );
818                        break;
819                  default:
[de62360d]820                        ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType* >( base->buildDecl( "", DeclarationNode::NoStorageClass, 0, false, false, LinkageSpec::Cforall ) ) );
[68cd1ce]821                } // switch
[b87a5ed]822        } else {
[68cd1ce]823                ft->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 ) );
824        } // if
[b87a5ed]825        return ft;
[51b7345]826}
827
[c8ffe20b]828AggregateDecl *TypeData::buildAggregate() const {
[b87a5ed]829        assert( kind == Aggregate );
830        AggregateDecl *at;
831        switch ( aggregate->kind ) {
832          case DeclarationNode::Struct:
833                at = new StructDecl( aggregate->name );
[37a3b8f9]834                buildForall( aggregate->params, at->get_parameters() );
[b87a5ed]835                break;
836          case DeclarationNode::Union:
837                at = new UnionDecl( aggregate->name );
[37a3b8f9]838                buildForall( aggregate->params, at->get_parameters() );
[b87a5ed]839                break;
[4040425]840          case DeclarationNode::Trait:
841                at = new TraitDecl( aggregate->name );
[37a3b8f9]842                buildList( aggregate->params, at->get_parameters() );
[b87a5ed]843                break;
844          default:
845                assert( false );
[68cd1ce]846        } // switch
[37a3b8f9]847//      buildList( aggregate->params, at->get_parameters() );
[721f17a]848        buildList( aggregate->fields, at->get_members() );
[b87a5ed]849
850        return at;
[51b7345]851}
852
853/// namespace {
854/// Type*
855/// makeType( Declaration* decl )
856/// {
[c8ffe20b]857///   if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( decl ) ) {
[51b7345]858///     return dwt->get_type()->clone();
859///   } else {
860///     return 0;
861///   }
862/// }
863/// }
864
[c8ffe20b]865ReferenceToType *TypeData::buildAggInst() const {
[b87a5ed]866        assert( kind == AggregateInst );
867
868        ReferenceToType *ret;
869        if ( aggInst->aggregate->kind == Enum ) {
870                ret = new EnumInstType( buildQualifiers(), aggInst->aggregate->enumeration->name );
871        } else {
872                assert( aggInst->aggregate->kind == Aggregate );
873                switch ( aggInst->aggregate->aggregate->kind ) {
874                  case DeclarationNode::Struct:
875                        ret = new StructInstType( buildQualifiers(), aggInst->aggregate->aggregate->name );
876                        break;
877                  case DeclarationNode::Union:
878                        ret = new UnionInstType( buildQualifiers(), aggInst->aggregate->aggregate->name );
879                        break;
[4040425]880                  case DeclarationNode::Trait:
881                        ret = new TraitInstType( buildQualifiers(), aggInst->aggregate->aggregate->name );
[b87a5ed]882                        break;
883                  default:
884                        assert( false );
[68cd1ce]885                } // switch
886        } // if
[b87a5ed]887        buildList( aggInst->params, ret->get_parameters() );
888        buildForall( forall, ret->get_forall() );
889        return ret;
[51b7345]890}
891
[68cd1ce]892NamedTypeDecl *TypeData::buildSymbolic( const std::string &name, DeclarationNode::StorageClass sc ) const {
[b87a5ed]893        assert( kind == Symbolic );
894        NamedTypeDecl *ret;
895        if ( symbolic->isTypedef ) {
896                ret = new TypedefDecl( name, sc, maybeBuild< Type >( base ) );
897        } else {
898                ret = new TypeDecl( name, sc, maybeBuild< Type >( base ), TypeDecl::Any );
[68cd1ce]899        } // if
[b87a5ed]900        buildList( symbolic->params, ret->get_parameters() );
901        buildList( symbolic->assertions, ret->get_assertions() );
902        return ret;
[51b7345]903}
904
[c8ffe20b]905TypeDecl *TypeData::buildVariable() const {
[b87a5ed]906        assert( kind == Variable );
907        static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype };
[51b7345]908
[68cd1ce]909        TypeDecl *ret = new TypeDecl( variable->name, DeclarationNode::NoStorageClass, 0, kindMap[ variable->tyClass ] );
[b87a5ed]910        buildList( variable->assertions, ret->get_assertions() );
911        return ret;
[51b7345]912}
913
[c8ffe20b]914EnumDecl *TypeData::buildEnum() const {
[b87a5ed]915        assert( kind == Enum );
916        EnumDecl *ret = new EnumDecl( enumeration->name );
917        buildList( enumeration->constants, ret->get_members() );
[90c3b1c]918        std::list< Declaration * >::iterator members = ret->get_members().begin();
919        for ( const DeclarationNode *cur = enumeration->constants; cur != NULL; cur = dynamic_cast<DeclarationNode *>( cur->get_link() ), ++members ) {
920                if ( cur->get_enumeratorValue() != NULL ) {
921                        ObjectDecl *member = dynamic_cast<ObjectDecl *>(*members);
[a5a71d0]922                        member->set_init( new SingleInit( maybeBuild< Expression >( cur->get_enumeratorValue() ), std::list< Expression * >() ) );
[90c3b1c]923                } // if
924        } // for
[b87a5ed]925        return ret;
[51b7345]926}
927
[c8ffe20b]928TypeInstType *TypeData::buildSymbolicInst() const {
[b87a5ed]929        assert( kind == SymbolicInst );
930        TypeInstType *ret = new TypeInstType( buildQualifiers(), symbolic->name, false );
931        buildList( symbolic->actuals, ret->get_parameters() );
932        buildForall( forall, ret->get_forall() );
933        return ret;
[51b7345]934}
935
[c8ffe20b]936TupleType *TypeData::buildTuple() const {
[b87a5ed]937        assert( kind == Tuple );
938        TupleType *ret = new TupleType( buildQualifiers() );
939        buildTypeList( tuple->members, ret->get_types() );
940        buildForall( forall, ret->get_forall() );
941        return ret;
[51b7345]942}
943
[c8ffe20b]944TypeofType *TypeData::buildTypeof() const {
[b87a5ed]945        assert( kind == Typeof );
946        assert( typeexpr );
947        assert( typeexpr->expr );
948        TypeofType *ret = new TypeofType( buildQualifiers(), typeexpr->expr->build() );
949        return ret;
[51b7345]950}
951
[c8ffe20b]952AttrType *TypeData::buildAttr() const {
[b87a5ed]953        assert( kind == Attr );
954        assert( attr );
955        AttrType *ret;
956        if ( attr->expr ) {
957                ret = new AttrType( buildQualifiers(), attr->name, attr->expr->build() );
958        } else {
959                assert( attr->type );
960                ret = new AttrType( buildQualifiers(), attr->name, attr->type->buildType() );
[68cd1ce]961        } // if
[b87a5ed]962        return ret;
[51b7345]963}
[b87a5ed]964
965// Local Variables: //
966// tab-width: 4 //
967// mode: c++ //
968// compile-command: "make install" //
969// End: //
Note: See TracBrowser for help on using the repository browser.