source: src/Parser/DeclarationNode.cc @ bfae637

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 bfae637 was 984dce6, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

only implicitly generate typedef for structures if name not in use and overwrite typedef name if explicit name appears, upate parser symbol table

  • Property mode set to 100644
File size: 30.3 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// DeclarationNode.cc --
8//
9// Author           : Rodolfo G. Esteves
10// Created On       : Sat May 16 12:34:05 2015
[1db21619]11// Last Modified By : Peter A. Buhr
[984dce6]12// Last Modified On : Mon Mar 21 21:04:23 2016
13// Update Count     : 142
[b87a5ed]14//
15
[51b7345]16#include <string>
17#include <list>
18#include <iterator>
19#include <algorithm>
20#include <cassert>
21
22#include "TypeData.h"
[bdd516a]23
[68cd1ce]24#include "SynTree/Declaration.h"
25#include "SynTree/Expression.h"
[51b7345]26
[de62360d]27#include "Parser.h"
28#include "TypedefTable.h"
29extern TypedefTable typedefTable;
30
[51b7345]31using namespace std;
32
[bdd516a]33// These must remain in the same order as the corresponding DeclarationNode enumerations.
[68cd1ce]34const char *DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "" };
[bdd516a]35const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic" };
[90c3b1c]36const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary",  };
[68cd1ce]37const char *DeclarationNode::modifierName[]  = { "signed", "unsigned", "short", "long" };
38const char *DeclarationNode::aggregateName[] = { "struct", "union", "context" };
[51b7345]39const char *DeclarationNode::typeClassName[] = { "type", "dtype", "ftype" };
[90c3b1c]40const char *DeclarationNode::builtinTypeName[] = { "__builtin_va_list" };
[51b7345]41
42UniqueName DeclarationNode::anonymous( "__anonymous" );
43
[984dce6]44extern LinkageSpec::Type linkage;                                               // defined in parser.yy
[51b7345]45
[3848e0e]46DeclarationNode *DeclarationNode::clone() const {
[b87a5ed]47        DeclarationNode *newnode = new DeclarationNode;
48        newnode->type = maybeClone( type );
49        newnode->name = name;
50        newnode->storageClasses = storageClasses;
51        newnode->bitfieldWidth = maybeClone( bitfieldWidth );
52        newnode->hasEllipsis = hasEllipsis;
53        newnode->initializer = initializer;
54        newnode->next = maybeClone( next );
55        newnode->linkage = linkage;
56        return newnode;
[984dce6]57} // DeclarationNode::clone
[3848e0e]58
59DeclarationNode::DeclarationNode() : type( 0 ), bitfieldWidth( 0 ), initializer( 0 ), hasEllipsis( false ), linkage( ::linkage ) {
60}
61
62DeclarationNode::~DeclarationNode() {
[b87a5ed]63        delete type;
64        delete bitfieldWidth;
65        delete initializer;
[3848e0e]66}
67
68bool DeclarationNode::get_hasEllipsis() const {
[b87a5ed]69        return hasEllipsis;
[3848e0e]70}
71
72void DeclarationNode::print( std::ostream &os, int indent ) const {
[59db689]73        os << string( indent, ' ' );
[b87a5ed]74        if ( name == "" ) {
75                os << "unnamed: ";
76        } else {
77                os << name << ": ";
[68cd1ce]78        } // if
[51b7345]79
[b87a5ed]80        if ( linkage != LinkageSpec::Cforall ) {
81                os << LinkageSpec::toString( linkage ) << " ";
[68cd1ce]82        } // if
[3848e0e]83
[68cd1ce]84        printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );
[b87a5ed]85        if ( type ) {
86                type->print( os, indent );
87        } else {
88                os << "untyped entity ";
[68cd1ce]89        } // if
[3848e0e]90
[b87a5ed]91        if ( bitfieldWidth ) {
[59db689]92                os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
[b87a5ed]93                bitfieldWidth->printOneLine( os );
[68cd1ce]94        } // if
[3848e0e]95
[b87a5ed]96        if ( initializer != 0 ) {
[59db689]97                os << endl << string( indent + 2, ' ' ) << "with initializer ";
[b87a5ed]98                initializer->printOneLine( os );
[68cd1ce]99        } // if
[3848e0e]100
[b87a5ed]101        os << endl;
[51b7345]102}
103
[3848e0e]104void DeclarationNode::printList( std::ostream &os, int indent ) const {
[b87a5ed]105        ParseNode::printList( os, indent );
106        if ( hasEllipsis ) {
107                os << string( indent, ' ' )  << "and a variable number of other arguments" << endl;
[68cd1ce]108        } // if
[51b7345]109}
110
[d9a0e76]111DeclarationNode *DeclarationNode::newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle ) {
[b87a5ed]112        DeclarationNode *newnode = new DeclarationNode;
113        newnode->name = assign_strptr( name );
[3848e0e]114
[b87a5ed]115        newnode->type = new TypeData( TypeData::Function );
116        newnode->type->function->params = param;
117        newnode->type->function->newStyle = newStyle;
118        newnode->type->function->body = body;
[984dce6]119        typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );
[3848e0e]120
[b87a5ed]121        if ( body ) {
122                newnode->type->function->hasBody = true;
[68cd1ce]123        } // if
[51b7345]124
[b87a5ed]125        if ( ret ) {
126                newnode->type->base = ret->type;
127                ret->type = 0;
128                delete ret;
[68cd1ce]129        } // if
[51b7345]130
[b87a5ed]131        return newnode;
[984dce6]132} // DeclarationNode::newFunction
[3848e0e]133
134DeclarationNode *DeclarationNode::newQualifier( Qualifier q ) {
[b87a5ed]135        DeclarationNode *newnode = new DeclarationNode;
136        newnode->type = new TypeData();
137        newnode->type->qualifiers.push_back( q );
138        return newnode;
[984dce6]139} // DeclarationNode::newQualifier
[3848e0e]140
[68cd1ce]141DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
[b87a5ed]142        DeclarationNode *newnode = new DeclarationNode;
143        newnode->storageClasses.push_back( sc );
144        return newnode;
[984dce6]145} // DeclarationNode::newStorageClass
[3848e0e]146
147DeclarationNode *DeclarationNode::newBasicType( BasicType bt ) {
[b87a5ed]148        DeclarationNode *newnode = new DeclarationNode;
149        newnode->type = new TypeData( TypeData::Basic );
150        newnode->type->basic->typeSpec.push_back( bt );
151        return newnode;
[984dce6]152} // DeclarationNode::newBasicType
[3848e0e]153
[90c3b1c]154DeclarationNode *DeclarationNode::newBuiltinType( BuiltinType bt ) {
155        DeclarationNode *newnode = new DeclarationNode;
156        newnode->type = new TypeData( TypeData::Builtin );
157        newnode->type->builtin->type = bt;
158        return newnode;
[984dce6]159} // DeclarationNode::newBuiltinType
[90c3b1c]160
[3848e0e]161DeclarationNode *DeclarationNode::newModifier( Modifier mod ) {
[b87a5ed]162        DeclarationNode *newnode = new DeclarationNode;
163        newnode->type = new TypeData( TypeData::Basic );
164        newnode->type->basic->modifiers.push_back( mod );
165        return newnode;
[984dce6]166} // DeclarationNode::newModifier
[3848e0e]167
[59db689]168DeclarationNode *DeclarationNode::newForall( DeclarationNode *forall ) {
[b87a5ed]169        DeclarationNode *newnode = new DeclarationNode;
170        newnode->type = new TypeData( TypeData::Unknown );
171        newnode->type->forall = forall;
172        return newnode;
[984dce6]173} // DeclarationNode::newForall
[3848e0e]174
[59db689]175DeclarationNode *DeclarationNode::newFromTypedef( std::string *name ) {
[b87a5ed]176        DeclarationNode *newnode = new DeclarationNode;
177        newnode->type = new TypeData( TypeData::SymbolicInst );
178        newnode->type->symbolic->name = assign_strptr( name );
179        newnode->type->symbolic->isTypedef = true;
180        newnode->type->symbolic->params = 0;
181        return newnode;
[984dce6]182} // DeclarationNode::newFromTypedef
[3848e0e]183
[2871210]184DeclarationNode *DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields ) {
[b87a5ed]185        DeclarationNode *newnode = new DeclarationNode;
186        newnode->type = new TypeData( TypeData::Aggregate );
187        newnode->type->aggregate->kind = kind;
188        newnode->type->aggregate->name = assign_strptr( name );
[2871210]189        if ( newnode->type->aggregate->name == "" ) {           // anonymous aggregate ?
[b87a5ed]190                newnode->type->aggregate->name = DeclarationNode::anonymous.newName();
[984dce6]191        } else if ( ! typedefTable.exists( newnode->type->aggregate->name ) ) {
192                // SKULLDUGGERY: Generate a typedef for the aggregate name so the aggregate does not have to be qualified by
193                // "struct". Only generate the typedef, if the name is not in use. The typedef is implicitly (silently) removed
194                // if the name is explicitly used.
[2871210]195                typedefTable.addToEnclosingScope( newnode->type->aggregate->name, TypedefTable::TD );
196                DeclarationNode *typedf = new DeclarationNode;
197                typedf->name = newnode->type->aggregate->name;
198                newnode->appendList( typedf->addType( newnode->clone() )->addTypedef() );
[68cd1ce]199        } // if
[b87a5ed]200        newnode->type->aggregate->actuals = actuals;
[721f17a]201        newnode->type->aggregate->fields = fields;
[b87a5ed]202        return newnode;
[984dce6]203} // DeclarationNode::newAggregate
[3848e0e]204
205DeclarationNode *DeclarationNode::newEnum( std::string *name, DeclarationNode *constants ) {
[b87a5ed]206        DeclarationNode *newnode = new DeclarationNode;
207        newnode->name = assign_strptr( name );
208        newnode->type = new TypeData( TypeData::Enum );
209        newnode->type->enumeration->name = newnode->name;
[2871210]210        if ( newnode->type->enumeration->name == "" ) {         // anonymous enumeration ?
[b87a5ed]211                newnode->type->enumeration->name = DeclarationNode::anonymous.newName();
[984dce6]212        } else if ( ! typedefTable.exists( newnode->type->enumeration->name ) ) {
213                // SKULLDUGGERY: Generate a typedef for the enumeration name so the enumeration does not have to be qualified by
214                // "enum". Only generate the typedef, if the name is not in use. The typedef is implicitly (silently) removed if
215                // the name is explicitly used.
[2871210]216                typedefTable.addToEnclosingScope( newnode->type->enumeration->name, TypedefTable::TD );
217                DeclarationNode *typedf = new DeclarationNode;
218                typedf->name = newnode->type->enumeration->name;
219                newnode->appendList( typedf->addType( newnode->clone() )->addTypedef() );
[68cd1ce]220        } // if
[b87a5ed]221        newnode->type->enumeration->constants = constants;
222        return newnode;
[984dce6]223} // DeclarationNode::newEnum
[3848e0e]224
[59db689]225DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) {
[b87a5ed]226        DeclarationNode *newnode = new DeclarationNode;
227        newnode->name = assign_strptr( name );
[90c3b1c]228        newnode->enumeratorValue = constant;
[984dce6]229        typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );
[b87a5ed]230        return newnode;
[984dce6]231} // DeclarationNode::newEnumConstant
[3848e0e]232
[59db689]233DeclarationNode *DeclarationNode::newName( std::string *name ) {
[b87a5ed]234        DeclarationNode *newnode = new DeclarationNode;
235        newnode->name = assign_strptr( name );
236        return newnode;
[984dce6]237} // DeclarationNode::newName
[3848e0e]238
[59db689]239DeclarationNode *DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) {
[b87a5ed]240        DeclarationNode *newnode = new DeclarationNode;
241        newnode->type = new TypeData( TypeData::SymbolicInst );
242        newnode->type->symbolic->name = assign_strptr( name );
243        newnode->type->symbolic->isTypedef = false;
244        newnode->type->symbolic->actuals = params;
245        return newnode;
[984dce6]246} // DeclarationNode::newFromTypeGen
[3848e0e]247
[59db689]248DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string *name ) {
[b87a5ed]249        DeclarationNode *newnode = new DeclarationNode;
250        newnode->name = assign_strptr( name );
251        newnode->type = new TypeData( TypeData::Variable );
252        newnode->type->variable->tyClass = tc;
253        newnode->type->variable->name = newnode->name;
254        return newnode;
[984dce6]255} // DeclarationNode::newTypeParam
[3848e0e]256
[4040425]257DeclarationNode *DeclarationNode::newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) {
[b87a5ed]258        DeclarationNode *newnode = new DeclarationNode;
259        newnode->type = new TypeData( TypeData::Aggregate );
[4040425]260        newnode->type->aggregate->kind = Trait;
[b87a5ed]261        newnode->type->aggregate->params = params;
[721f17a]262        newnode->type->aggregate->fields = asserts;
[b87a5ed]263        newnode->type->aggregate->name = assign_strptr( name );
264        return newnode;
[984dce6]265} // DeclarationNode::newTrait
[3848e0e]266
[4040425]267DeclarationNode *DeclarationNode::newTraitUse( std::string *name, ExpressionNode *params ) {
[b87a5ed]268        DeclarationNode *newnode = new DeclarationNode;
269        newnode->type = new TypeData( TypeData::AggregateInst );
270        newnode->type->aggInst->aggregate = new TypeData( TypeData::Aggregate );
[4040425]271        newnode->type->aggInst->aggregate->aggregate->kind = Trait;
[b87a5ed]272        newnode->type->aggInst->aggregate->aggregate->name = assign_strptr( name );
273        newnode->type->aggInst->params = params;
274        return newnode;
[984dce6]275} // DeclarationNode::newTraitUse
[3848e0e]276
277DeclarationNode *DeclarationNode::newTypeDecl( std::string *name, DeclarationNode *typeParams ) {
[b87a5ed]278        DeclarationNode *newnode = new DeclarationNode;
279        newnode->name = assign_strptr( name );
280        newnode->type = new TypeData( TypeData::Symbolic );
281        newnode->type->symbolic->isTypedef = false;
282        newnode->type->symbolic->params = typeParams;
283        newnode->type->symbolic->name = newnode->name;
284        return newnode;
[984dce6]285} // DeclarationNode::newTypeDecl
[3848e0e]286
287DeclarationNode *DeclarationNode::newPointer( DeclarationNode *qualifiers ) {
[b87a5ed]288        DeclarationNode *newnode = new DeclarationNode;
289        newnode->type = new TypeData( TypeData::Pointer );
290        return newnode->addQualifiers( qualifiers );
[984dce6]291} // DeclarationNode::newPointer
[3848e0e]292
293DeclarationNode *DeclarationNode::newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ) {
[b87a5ed]294        DeclarationNode *newnode = new DeclarationNode;
295        newnode->type = new TypeData( TypeData::Array );
296        newnode->type->array->dimension = size;
297        newnode->type->array->isStatic = isStatic;
[71bd8c6]298        if ( newnode->type->array->dimension == 0 || dynamic_cast<ConstantNode *>( newnode->type->array->dimension ) ) {
299                newnode->type->array->isVarLen = false;
300        } else {
301                newnode->type->array->isVarLen = true;
302        } // if
[b87a5ed]303        return newnode->addQualifiers( qualifiers );
[984dce6]304} // DeclarationNode::newArray
[3848e0e]305
306DeclarationNode *DeclarationNode::newVarArray( DeclarationNode *qualifiers ) {
[b87a5ed]307        DeclarationNode *newnode = new DeclarationNode;
308        newnode->type = new TypeData( TypeData::Array );
309        newnode->type->array->dimension = 0;
310        newnode->type->array->isStatic = false;
311        newnode->type->array->isVarLen = true;
312        return newnode->addQualifiers( qualifiers );
[3848e0e]313}
314
315DeclarationNode *DeclarationNode::newBitfield( ExpressionNode *size ) {
[b87a5ed]316        DeclarationNode *newnode = new DeclarationNode;
317        newnode->bitfieldWidth = size;
318        return newnode;
[3848e0e]319}
320
321DeclarationNode *DeclarationNode::newTuple( DeclarationNode *members ) {
[b87a5ed]322        DeclarationNode *newnode = new DeclarationNode;
323        newnode->type = new TypeData( TypeData::Tuple );
324        newnode->type->tuple->members = members;
325        return newnode;
[3848e0e]326}
327
328DeclarationNode *DeclarationNode::newTypeof( ExpressionNode *expr ) {
[b87a5ed]329        DeclarationNode *newnode = new DeclarationNode;
330        newnode->type = new TypeData( TypeData::Typeof );
331        newnode->type->typeexpr->expr = expr;
332        return newnode;
[3848e0e]333}
334
335DeclarationNode *DeclarationNode::newAttr( std::string *name, ExpressionNode *expr ) {
[b87a5ed]336        DeclarationNode *newnode = new DeclarationNode;
337        newnode->type = new TypeData( TypeData::Attr );
338        newnode->type->attr->name = assign_strptr( name );
339        newnode->type->attr->expr = expr;
340        return newnode;
[3848e0e]341}
342
343DeclarationNode *DeclarationNode::newAttr( std::string *name, DeclarationNode *type ) {
[b87a5ed]344        DeclarationNode *newnode = new DeclarationNode;
345        newnode->type = new TypeData( TypeData::Attr );
346        newnode->type->attr->name = assign_strptr( name );
347        newnode->type->attr->type = type;
348        return newnode;
[3848e0e]349}
350
351static void addQualifiersToType( TypeData *&src, TypeData *dst ) {
[b87a5ed]352        if ( src && dst ) {
353                if ( src->forall && dst->kind == TypeData::Function ) {
354                        if ( dst->forall ) {
355                                dst->forall->appendList( src->forall );
356                        } else {
357                                dst->forall = src->forall;
[68cd1ce]358                        } // if
[b87a5ed]359                        src->forall = 0;
[68cd1ce]360                } // if
[b87a5ed]361                if ( dst->base ) {
362                        addQualifiersToType( src, dst->base );
363                } else if ( dst->kind == TypeData::Function ) {
364                        dst->base = src;
365                        src = 0;
366                } else {
367                        dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
[68cd1ce]368                } // if
369        } // if
[3848e0e]370}
[b87a5ed]371         
[3848e0e]372DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) {
[b87a5ed]373        if ( q ) {
374                storageClasses.splice( storageClasses.end(), q->storageClasses );
375                if ( q->type ) {
376                        if ( ! type ) {
377                                type = new TypeData;
[68cd1ce]378                        } // if
[b87a5ed]379                        addQualifiersToType( q->type, type );
380                        if ( q->type && q->type->forall ) {
381                                if ( type->forall ) {
382                                        type->forall->appendList( q->type->forall );
383                                } else {
[68cd1ce]384                                        if ( type->kind == TypeData::Aggregate ) {
385                                                type->aggregate->params = q->type->forall;
[721f17a]386                                                // change implicit typedef from TYPEDEFname to TYPEGENname
387                                                typedefTable.changeKind( type->aggregate->name, TypedefTable::TG );
[68cd1ce]388                                        } else {
389                                                type->forall = q->type->forall;
390                                        } // if
391                                } // if
[b87a5ed]392                                q->type->forall = 0;
[68cd1ce]393                        } // if
394                } // if
395        } // if
[b87a5ed]396        delete q;
397        return this;
[3848e0e]398}
399
400DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
[b87a5ed]401        storageClasses = q->storageClasses;
402        return this;
[3848e0e]403}
404
405static void addTypeToType( TypeData *&src, TypeData *&dst ) {
[b87a5ed]406        if ( src && dst ) {
407                if ( src->forall && dst->kind == TypeData::Function ) {
408                        if ( dst->forall ) {
409                                dst->forall->appendList( src->forall );
410                        } else {
411                                dst->forall = src->forall;
[68cd1ce]412                        } // if
[b87a5ed]413                        src->forall = 0;
[68cd1ce]414                } // if
[b87a5ed]415                if ( dst->base ) {
416                        addTypeToType( src, dst->base );
417                } else {
418                        switch ( dst->kind ) {
419                          case TypeData::Unknown:
420                                src->qualifiers.splice( src->qualifiers.end(), dst->qualifiers );
421                                dst = src;
422                                src = 0;
423                                break;
424                          case TypeData::Basic:
425                                dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
426                                if ( src->kind != TypeData::Unknown ) {
427                                        assert( src->kind == TypeData::Basic );
428                                        dst->basic->modifiers.splice( dst->basic->modifiers.end(), src->basic->modifiers );
429                                        dst->basic->typeSpec.splice( dst->basic->typeSpec.end(), src->basic->typeSpec );
[68cd1ce]430                                } // if
[b87a5ed]431                                break;
432                          default:
433                                switch ( src->kind ) {
434                                  case TypeData::Aggregate:
435                                  case TypeData::Enum:
436                                        dst->base = new TypeData( TypeData::AggregateInst );
437                                        dst->base->aggInst->aggregate = src;
438                                        if ( src->kind == TypeData::Aggregate ) {
439                                                dst->base->aggInst->params = maybeClone( src->aggregate->actuals );
[68cd1ce]440                                        } // if
[b87a5ed]441                                        dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers );
442                                        src = 0;
443                                        break;
444                                  default:
445                                        if ( dst->forall ) {
446                                                dst->forall->appendList( src->forall );
447                                        } else {
448                                                dst->forall = src->forall;
[68cd1ce]449                                        } // if
[b87a5ed]450                                        src->forall = 0;
451                                        dst->base = src;
452                                        src = 0;
[68cd1ce]453                                } // switch
454                        } // switch
455                } // if
456        } // if
[3848e0e]457}
458
459DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
[b87a5ed]460        if ( o ) {
461                storageClasses.splice( storageClasses.end(), o->storageClasses );
462                if ( o->type ) {
463                        if ( ! type ) {
464                                if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
465                                        type = new TypeData( TypeData::AggregateInst );
466                                        type->aggInst->aggregate = o->type;
467                                        if ( o->type->kind == TypeData::Aggregate ) {
468                                                type->aggInst->params = maybeClone( o->type->aggregate->actuals );
[68cd1ce]469                                        } // if
[b87a5ed]470                                        type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers );
471                                } else {
472                                        type = o->type;
[68cd1ce]473                                } // if
[b87a5ed]474                                o->type = 0;
475                        } else {
476                                addTypeToType( o->type, type );
[68cd1ce]477                        } // if
478                } // if
[b87a5ed]479                if ( o->bitfieldWidth ) {
480                        bitfieldWidth = o->bitfieldWidth;
[68cd1ce]481                } // if
[71bd8c6]482
483                // there may be typedefs chained onto the type
484                if ( o->get_link() ) {
485                        set_link( o->get_link()->clone() );
[1db21619]486                } // if
[68cd1ce]487        } // if
[b87a5ed]488        delete o;
489        return this;
[3848e0e]490}
491
492DeclarationNode *DeclarationNode::addTypedef() {
[b87a5ed]493        TypeData *newtype = new TypeData( TypeData::Symbolic );
494        newtype->symbolic->params = 0;
495        newtype->symbolic->isTypedef = true;
496        newtype->symbolic->name = name;
497        newtype->base = type;
498        type = newtype;
499        return this;
[3848e0e]500}
501
[59db689]502DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) {
[b87a5ed]503        assert( type );
504        switch ( type->kind ) {
505          case TypeData::Symbolic:
506                if ( type->symbolic->assertions ) {
507                        type->symbolic->assertions->appendList( assertions );
508                } else {
509                        type->symbolic->assertions = assertions;
[68cd1ce]510                } // if
[b87a5ed]511                break;
512          case TypeData::Variable:
513                if ( type->variable->assertions ) {
514                        type->variable->assertions->appendList( assertions );
515                } else {
516                        type->variable->assertions = assertions;
[68cd1ce]517                } // if
[b87a5ed]518                break;
519          default:
520                assert( false );
[68cd1ce]521        } // switch
[b87a5ed]522       
523        return this;
[51b7345]524}
525
[59db689]526DeclarationNode *DeclarationNode::addName( std::string *newname ) {
[b87a5ed]527        name = assign_strptr( newname );
528        return this;
[51b7345]529}
530
[3848e0e]531DeclarationNode *DeclarationNode::addBitfield( ExpressionNode *size ) {
[b87a5ed]532        bitfieldWidth = size;
533        return this;
[51b7345]534}
535
[3848e0e]536DeclarationNode *DeclarationNode::addVarArgs() {
[b87a5ed]537        assert( type );
538        hasEllipsis = true;
539        return this;
[51b7345]540}
541
[3848e0e]542DeclarationNode *DeclarationNode::addFunctionBody( StatementNode *body ) {
[b87a5ed]543        assert( type );
544        assert( type->kind == TypeData::Function );
545        assert( type->function->body == 0 );
546        type->function->body = body;
547        type->function->hasBody = true;
548        return this;
[51b7345]549}
550
[3848e0e]551DeclarationNode *DeclarationNode::addOldDeclList( DeclarationNode *list ) {
[b87a5ed]552        assert( type );
553        assert( type->kind == TypeData::Function );
554        assert( type->function->oldDeclList == 0 );
555        type->function->oldDeclList = list;
556        return this;
[51b7345]557}
558
[68cd1ce]559static void setBase( TypeData *&type, TypeData *newType ) {
[b87a5ed]560        if ( type ) {
561                TypeData *prevBase = type;
562                TypeData *curBase = type->base;
[a32b204]563                while ( curBase != 0 ) {
[b87a5ed]564                        prevBase = curBase;
565                        curBase = curBase->base;
[68cd1ce]566                } // while
[b87a5ed]567                prevBase->base = newType;
568        } else {
569                type = newType;
[68cd1ce]570        } // if
[3848e0e]571}
572
573DeclarationNode *DeclarationNode::addPointer( DeclarationNode *p ) {
[b87a5ed]574        if ( p ) {
575                assert( p->type->kind == TypeData::Pointer );
576                setBase( type, p->type );
577                p->type = 0;
578                delete p;
[68cd1ce]579        } // if
[b87a5ed]580        return this;
[3848e0e]581}
582
583DeclarationNode *DeclarationNode::addArray( DeclarationNode *a ) {
[b87a5ed]584        if ( a ) {
585                assert( a->type->kind == TypeData::Array );
586                setBase( type, a->type );
587                a->type = 0;
588                delete a;
[68cd1ce]589        } // if
[b87a5ed]590        return this;
[51b7345]591}
592
[3848e0e]593DeclarationNode *DeclarationNode::addNewPointer( DeclarationNode *p ) {
[b87a5ed]594        if ( p ) {
595                assert( p->type->kind == TypeData::Pointer );
596                if ( type ) {
597                        switch ( type->kind ) {
598                          case TypeData::Aggregate:
599                          case TypeData::Enum:
600                                p->type->base = new TypeData( TypeData::AggregateInst );
601                                p->type->base->aggInst->aggregate = type;
602                                if ( type->kind == TypeData::Aggregate ) {
603                                        p->type->base->aggInst->params = maybeClone( type->aggregate->actuals );
[68cd1ce]604                                } // if
[b87a5ed]605                                p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers );
606                                break;
607
608                          default:
609                                p->type->base = type;
[68cd1ce]610                        } // switch
[b87a5ed]611                        type = 0;
[68cd1ce]612                } // if
[b87a5ed]613                delete this;
614                return p;
615        } else {
616                return this;
[68cd1ce]617        } // if
[51b7345]618}
619
[3848e0e]620static TypeData *findLast( TypeData *a ) {
[b87a5ed]621        assert( a );
622        TypeData *cur = a;
[a32b204]623        while ( cur->base ) {
[b87a5ed]624                cur = cur->base;
[68cd1ce]625        } // while
[b87a5ed]626        return cur;
[3848e0e]627}
628
629DeclarationNode *DeclarationNode::addNewArray( DeclarationNode *a ) {
[b87a5ed]630        if ( a ) {
631                assert( a->type->kind == TypeData::Array );
632                TypeData *lastArray = findLast( a->type );
633                if ( type ) { 
634                        switch ( type->kind ) {
635                          case TypeData::Aggregate:
636                          case TypeData::Enum:
637                                lastArray->base = new TypeData( TypeData::AggregateInst );
638                                lastArray->base->aggInst->aggregate = type;
639                                if ( type->kind == TypeData::Aggregate ) {
640                                        lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals );
[68cd1ce]641                                } // if
[b87a5ed]642                                lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers );
643                                break;
644                          default:
645                                lastArray->base = type;
[68cd1ce]646                        } // switch
[b87a5ed]647                        type = 0;
[68cd1ce]648                } // if
[b87a5ed]649                delete this;
650                return a;
651        } else {
652                return this;
[68cd1ce]653        } // if
[51b7345]654}
[3848e0e]655
656DeclarationNode *DeclarationNode::addParamList( DeclarationNode *params ) {
[b87a5ed]657        TypeData *ftype = new TypeData( TypeData::Function );
658        ftype->function->params = params;
659        setBase( type, ftype );
660        return this;
[3848e0e]661}
662
663static TypeData *addIdListToType( TypeData *type, DeclarationNode *ids ) {
[b87a5ed]664        if ( type ) {
665                if ( type->kind != TypeData::Function ) {
666                        type->base = addIdListToType( type->base, ids );
667                } else {
668                        type->function->idList = ids;
[68cd1ce]669                } // if
[b87a5ed]670                return type;
[3848e0e]671        } else {
[b87a5ed]672                TypeData *newtype = new TypeData( TypeData::Function );
673                newtype->function->idList = ids;
674                return newtype;
[68cd1ce]675        } // if
[3848e0e]676}
[b87a5ed]677       
[3848e0e]678DeclarationNode *DeclarationNode::addIdList( DeclarationNode *ids ) {
[b87a5ed]679        type = addIdListToType( type, ids );
680        return this;
[3848e0e]681}
682
683DeclarationNode *DeclarationNode::addInitializer( InitializerNode *init ) {
[b87a5ed]684        //assert
685        initializer = init;
686        return this;
[3848e0e]687}
688
689DeclarationNode *DeclarationNode::cloneBaseType( string *newName ) {
[b87a5ed]690        DeclarationNode *newnode = new DeclarationNode;
691        TypeData *srcType = type;
[a32b204]692        while ( srcType->base ) {
[b87a5ed]693                srcType = srcType->base;
[68cd1ce]694        } // while
[b87a5ed]695        newnode->type = maybeClone( srcType );
696        if ( newnode->type->kind == TypeData::AggregateInst ) {
697                // don't duplicate members
698                if ( newnode->type->aggInst->aggregate->kind == TypeData::Enum ) {
699                        delete newnode->type->aggInst->aggregate->enumeration->constants;
700                        newnode->type->aggInst->aggregate->enumeration->constants = 0;
701                } else {
702                        assert( newnode->type->aggInst->aggregate->kind == TypeData::Aggregate );
[721f17a]703                        delete newnode->type->aggInst->aggregate->aggregate->fields;
704                        newnode->type->aggInst->aggregate->aggregate->fields = 0;
[68cd1ce]705                } // if
706        } // if
[b87a5ed]707        newnode->type->forall = maybeClone( type->forall );
708        newnode->storageClasses = storageClasses;
709        newnode->name = assign_strptr( newName );
710        return newnode;
[3848e0e]711}
712
713DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) {
[b87a5ed]714        if ( o ) {
715                o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
716                if ( type ) {
717                        TypeData *srcType = type;
[a32b204]718                        while ( srcType->base ) {
[b87a5ed]719                                srcType = srcType->base;
[68cd1ce]720                        } // while
[b87a5ed]721                        TypeData *newType = srcType->clone();
722                        if ( newType->kind == TypeData::AggregateInst ) {
723                                // don't duplicate members
724                                if ( newType->aggInst->aggregate->kind == TypeData::Enum ) {
725                                        delete newType->aggInst->aggregate->enumeration->constants;
726                                        newType->aggInst->aggregate->enumeration->constants = 0;
727                                } else {
728                                        assert( newType->aggInst->aggregate->kind == TypeData::Aggregate );
[721f17a]729                                        delete newType->aggInst->aggregate->aggregate->fields;
730                                        newType->aggInst->aggregate->aggregate->fields = 0;
[68cd1ce]731                                } // if
732                        } // if
[b87a5ed]733                        newType->forall = maybeClone( type->forall );
734                        if ( ! o->type ) {
735                                o->type = newType;
736                        } else {
737                                addTypeToType( newType, o->type );
738                                delete newType;
[68cd1ce]739                        } // if
740                } // if
741        } // if
[b87a5ed]742        return o;
[3848e0e]743}
744
745DeclarationNode *DeclarationNode::cloneType( string *newName ) {
[b87a5ed]746        DeclarationNode *newnode = new DeclarationNode;
747        newnode->type = maybeClone( type );
748        newnode->storageClasses = storageClasses;
749        newnode->name = assign_strptr( newName );
750        return newnode;
[3848e0e]751}
752
753DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) {
[b87a5ed]754        if ( o ) {
755                o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
756                if ( type ) {
757                        TypeData *newType = type->clone();
758                        if ( ! o->type ) {
759                                o->type = newType;
760                        } else {
761                                addTypeToType( newType, o->type );
762                                delete newType;
[68cd1ce]763                        } // if
764                } // if
765        } // if
[b87a5ed]766        return o;
[51b7345]767}
768
[3848e0e]769DeclarationNode *DeclarationNode::appendList( DeclarationNode *node ) {
[b87a5ed]770        if ( node != 0 ) {
771                set_link( node );
[68cd1ce]772        } // if
[b87a5ed]773        return this;
[51b7345]774}
775
[3848e0e]776DeclarationNode *DeclarationNode::extractAggregate() const {
[b87a5ed]777        if ( type ) {
778                TypeData *ret = type->extractAggregate();
779                if ( ret ) {
780                        DeclarationNode *newnode = new DeclarationNode;
781                        newnode->type = ret;
782                        return newnode;
[843054c2]783                } // if
784        } // if
785        return 0;
[3848e0e]786}
787
[a61fea9a]788void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {
[b87a5ed]789        SemanticError errors;
[59db689]790        std::back_insert_iterator< std::list< Declaration *> > out( outputList );
[b87a5ed]791        const DeclarationNode *cur = firstNode;
[a32b204]792        while ( cur ) {
[b87a5ed]793                try {
794                        if ( DeclarationNode *extr = cur->extractAggregate() ) {
795                                // handle the case where a structure declaration is contained within an object or type declaration
796                                Declaration *decl = extr->build();
797                                if ( decl ) {
798                                        *out++ = decl;
[843054c2]799                                } // if
800                        } // if
[b87a5ed]801                        Declaration *decl = cur->build();
802                        if ( decl ) {
803                                *out++ = decl;
[843054c2]804                        } // if
[b87a5ed]805                } catch( SemanticError &e ) {
806                        errors.append( e );
[843054c2]807                } // try
[90c3b1c]808                cur = dynamic_cast<DeclarationNode *>( cur->get_link() );
[843054c2]809        } // while
[b87a5ed]810        if ( ! errors.isEmpty() ) {
811                throw errors;
[843054c2]812        } // if
[3848e0e]813}
814
[59db689]815void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ) {
[b87a5ed]816        SemanticError errors;
[59db689]817        std::back_insert_iterator< std::list< DeclarationWithType *> > out( outputList );
[b87a5ed]818        const DeclarationNode *cur = firstNode;
[a32b204]819        while ( cur ) {
[b87a5ed]820                try {
[3848e0e]821///       if ( DeclarationNode *extr = cur->extractAggregate() ) {
[51b7345]822///     // handle the case where a structure declaration is contained within an object or type
823///     // declaration
824///     Declaration *decl = extr->build();
[3848e0e]825///     if ( decl ) {
826///          *out++ = decl;
[51b7345]827///     }
828///       }
[b87a5ed]829                        Declaration *decl = cur->build();
830                        if ( decl ) {
[59db689]831                                if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType *>( decl ) ) {
[b87a5ed]832                                        *out++ = dwt;
[59db689]833                                } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) {
[b87a5ed]834                                        StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
[68cd1ce]835                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
[b87a5ed]836                                        delete agg;
[59db689]837                                } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) {
[b87a5ed]838                                        UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
[68cd1ce]839                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
[843054c2]840                                } // if
841                        } // if
[b87a5ed]842                } catch( SemanticError &e ) {
843                        errors.append( e );
[843054c2]844                } // try
[59db689]845                cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
[843054c2]846        } // while
[b87a5ed]847        if ( ! errors.isEmpty() ) {
848                throw errors;
[843054c2]849        } // if
[3848e0e]850}
851
[59db689]852void buildTypeList( const DeclarationNode *firstNode, std::list< Type *> &outputList ) {
[b87a5ed]853        SemanticError errors;
[59db689]854        std::back_insert_iterator< std::list< Type *> > out( outputList );
[b87a5ed]855        const DeclarationNode *cur = firstNode;
[a32b204]856        while ( cur ) {
[b87a5ed]857                try {
858                        *out++ = cur->buildType();
859                } catch( SemanticError &e ) {
860                        errors.append( e );
[843054c2]861                } // try
[59db689]862                cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
[843054c2]863        } // while
[b87a5ed]864        if ( ! errors.isEmpty() ) {
865                throw errors;
[843054c2]866        } // if
[51b7345]867}
868
[3848e0e]869Declaration *DeclarationNode::build() const {
[843054c2]870        if ( type ) {
[de62360d]871                Declaration *newDecl = type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildFuncSpecifier( Inline ), buildFuncSpecifier( Noreturn ), linkage, maybeBuild< Initializer >(initializer) );
[b87a5ed]872                return newDecl;
[843054c2]873        } // if
[de62360d]874        if ( ! buildFuncSpecifier( Inline ) && ! buildFuncSpecifier( Noreturn ) ) {
[843054c2]875                return new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) );
876        } // if
[de62360d]877        throw SemanticError( "invalid function specifier in declaration of ", this );
[51b7345]878}
879
[3848e0e]880Type *DeclarationNode::buildType() const {
[b87a5ed]881        assert( type );
[51b7345]882 
[b87a5ed]883        switch ( type->kind ) {
884          case TypeData::Enum:
885                return new EnumInstType( type->buildQualifiers(), type->enumeration->name );
886          case TypeData::Aggregate: {
887                  ReferenceToType *ret;
888                  switch ( type->aggregate->kind ) {
889                        case DeclarationNode::Struct:
890                          ret = new StructInstType( type->buildQualifiers(), type->aggregate->name );
891                          break;
892                        case DeclarationNode::Union:
893                          ret = new UnionInstType( type->buildQualifiers(), type->aggregate->name );
894                          break;
[4040425]895                        case DeclarationNode::Trait:
896                          ret = new TraitInstType( type->buildQualifiers(), type->aggregate->name );
[b87a5ed]897                          break;
898                        default:
899                          assert( false );
900                  } // switch
901                  buildList( type->aggregate->actuals, ret->get_parameters() );
902                  return ret;
903          }
904          case TypeData::Symbolic: {
905                  TypeInstType *ret = new TypeInstType( type->buildQualifiers(), type->symbolic->name, false );
906                  buildList( type->symbolic->actuals, ret->get_parameters() );
907                  return ret;
908          }
909          default:
910                return type->build();
911        } // switch
[3848e0e]912}
913
[68cd1ce]914DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
915        DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
916        for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
917          if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
918          if ( ret != DeclarationNode::NoStorageClass ) {       // already have a valid storage class ?
[b87a5ed]919                        throw SemanticError( "invalid combination of storage classes in declaration of ", this );
[68cd1ce]920                } // if
921                ret = *i;
922        } // for
[b87a5ed]923        return ret;
[51b7345]924}
925
[de62360d]926bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
927        std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
928  if ( first == storageClasses.end() ) return false;    // not found
929        first = std::find( ++first, storageClasses.end(), key ); // found
930  if ( first == storageClasses.end() ) return true;             // not found again
931        throw SemanticError( "duplicate function specifier in declaration of ", this );
[51b7345]932}
[b87a5ed]933
934// Local Variables: //
935// tab-width: 4 //
936// mode: c++ //
937// compile-command: "make install" //
938// End: //
Note: See TracBrowser for help on using the repository browser.