source: src/Parser/DeclarationNode.cc @ ba7aa2d

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

more refactoring of parser code

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