source: src/Parser/DeclarationNode.cc @ b21c77a

new-env
Last change on this file since b21c77a was 28f3a19, checked in by Aaron Moss <a3moss@…>, 6 years ago

Merge branch 'master' into with_gc

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