source: src/Parser/DeclarationNode.cc @ bac5158

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 bac5158 was 6ea87486, checked in by Andrew Beach <ajbeach@…>, 7 years ago

That should be all the base code for 'tree structures' to work.

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