source: src/Parser/TypeData.cc @ 68cd1ce

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 68cd1ce was 68cd1ce, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

unify and fix storage class

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