source: src/Parser/DeclarationNode.cc @ 850fda6

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 850fda6 was 28307be, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

more refactoring of parser code

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