source: src/Parser/TypeData.cc @ f39096c

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f39096c was f39096c, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

removed memory leaks due to extractAggregate calls

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