source: src/Parser/DeclarationNode.cc @ 6e8bd43

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 6e8bd43 was 6e8bd43, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

cleanup interface to qualifiers/specifiers

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