source: src/Parser/DeclarationNode.cc @ 148f7290

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 148f7290 was 148f7290, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Propagate zero_t one_t from parser to backend

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