source: src/Parser/DeclarationNode.cc @ 9a7a3b6

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 9a7a3b6 was 47498bd, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Add nodes for global scope type

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