source: src/Parser/DeclarationNode.cc @ d63eeb0

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

propagate maybeConstructed flag through system, begin create constructor/destructor statements for further processing by Resolver

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