source: src/Parser/DeclarationNode.cc @ 68fe077a

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

move type StorageClasses? from DeclarationNode? to Type

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