source: src/Parser/DeclarationNode.cc @ e994912

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

code generation for external asm statement (declaration)

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