source: src/Parser/DeclarationNode.cc @ a7c90d4

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

change StorageClass? to bitset, support _Thread_local as separate storage-class

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