source: src/Parser/DeclarationNode.cc@ 982f95d

new-env
Last change on this file since 982f95d was eba74ba, checked in by Aaron Moss <a3moss@…>, 7 years ago

Merge remote-tracking branch 'origin/master' into with_gc

  • Property mode set to 100644
File size: 36.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// DeclarationNode.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 12:34:05 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue May 22 08:39:29 2018
13// Update Count : 1074
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( 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( 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( 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( 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( string * name ) {
287 DeclarationNode * newnode = new DeclarationNode;
288 newnode->name = name;
289 return newnode;
290} // DeclarationNode::newName
291
292DeclarationNode * DeclarationNode::newFromTypeGen( 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, 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( 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( 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( 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( 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; // make polymorphic type
547 // change implicit typedef from TYPEDEFname to TYPEGENname
548 typedefTable.changeKind( *type->aggregate.name, TYPEGENname );
549 } // if
550 } else { // not polymorphic
551 type->forall = q->type->forall; // make polymorphic routine
552 } // if
553 } // if
554 q->type->forall = nullptr; // forall qualifier moved
555 } // if
556
557 checkQualifiers( type, q->type );
558 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.val != 0 && error.length() == 0 ) {
559 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, Type::QualifiersNames[ilog2( q->type->qualifiers.val )], builtinTypeNames[builtin] );
560 } // if
561 addQualifiersToType( q->type, type );
562
563 delete q;
564 return this;
565} // addQualifiers
566
567static void addTypeToType( TypeData *&src, TypeData *&dst ) {
568 if ( src->forall && dst->kind == TypeData::Function ) {
569 if ( dst->forall ) {
570 dst->forall->appendList( src->forall );
571 } else {
572 dst->forall = src->forall;
573 } // if
574 src->forall = nullptr;
575 } // if
576 if ( dst->base ) {
577 addTypeToType( src, dst->base );
578 } else {
579 switch ( dst->kind ) {
580 case TypeData::Unknown:
581 src->qualifiers |= dst->qualifiers;
582 dst = src;
583 src = nullptr;
584 break;
585 case TypeData::Basic:
586 dst->qualifiers |= src->qualifiers;
587 if ( src->kind != TypeData::Unknown ) {
588 assert( src->kind == TypeData::Basic );
589
590 if ( dst->basictype == DeclarationNode::NoBasicType ) {
591 dst->basictype = src->basictype;
592 } else if ( src->basictype != DeclarationNode::NoBasicType )
593 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " );
594
595 if ( dst->complextype == DeclarationNode::NoComplexType ) {
596 dst->complextype = src->complextype;
597 } else if ( src->complextype != DeclarationNode::NoComplexType )
598 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " );
599
600 if ( dst->signedness == DeclarationNode::NoSignedness ) {
601 dst->signedness = src->signedness;
602 } else if ( src->signedness != DeclarationNode::NoSignedness )
603 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " );
604
605 if ( dst->length == DeclarationNode::NoLength ) {
606 dst->length = src->length;
607 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
608 dst->length = DeclarationNode::LongLong;
609 } else if ( src->length != DeclarationNode::NoLength )
610 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " );
611 } // if
612 break;
613 default:
614 switch ( src->kind ) {
615 case TypeData::Aggregate:
616 case TypeData::Enum:
617 dst->base = new TypeData( TypeData::AggregateInst );
618 dst->base->aggInst.aggregate = src;
619 if ( src->kind == TypeData::Aggregate ) {
620 dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
621 } // if
622 dst->base->qualifiers |= src->qualifiers;
623 src = nullptr;
624 break;
625 default:
626 if ( dst->forall ) {
627 dst->forall->appendList( src->forall );
628 } else {
629 dst->forall = src->forall;
630 } // if
631 src->forall = nullptr;
632 dst->base = src;
633 src = nullptr;
634 } // switch
635 } // switch
636 } // if
637}
638
639DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
640 if ( o ) {
641 checkSpecifiers( o );
642 copySpecifiers( o );
643 if ( o->type ) {
644 if ( ! type ) {
645 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
646 type = new TypeData( TypeData::AggregateInst );
647 type->aggInst.aggregate = o->type;
648 if ( o->type->kind == TypeData::Aggregate ) {
649 type->aggInst.hoistType = o->type->aggregate.body;
650 type->aggInst.params = maybeClone( o->type->aggregate.actuals );
651 } else {
652 type->aggInst.hoistType = o->type->enumeration.body;
653 } // if
654 type->qualifiers |= o->type->qualifiers;
655 } else {
656 type = o->type;
657 } // if
658 o->type = nullptr;
659 } else {
660 addTypeToType( o->type, type );
661 } // if
662 } // if
663 if ( o->bitfieldWidth ) {
664 bitfieldWidth = o->bitfieldWidth;
665 } // if
666
667 // there may be typedefs chained onto the type
668 if ( o->get_next() ) {
669 set_last( o->get_next()->clone() );
670 } // if
671 } // if
672 delete o;
673 return this;
674}
675
676DeclarationNode * DeclarationNode::addTypedef() {
677 TypeData * newtype = new TypeData( TypeData::Symbolic );
678 newtype->symbolic.params = nullptr;
679 newtype->symbolic.isTypedef = true;
680 newtype->symbolic.name = name ? new string( *name ) : nullptr;
681 newtype->base = type;
682 type = newtype;
683 return this;
684}
685
686DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
687 if ( variable.tyClass != NoTypeClass ) {
688 if ( variable.assertions ) {
689 variable.assertions->appendList( assertions );
690 } else {
691 variable.assertions = assertions;
692 } // if
693 return this;
694 } // if
695
696 assert( type );
697 switch ( type->kind ) {
698 case TypeData::Symbolic:
699 if ( type->symbolic.assertions ) {
700 type->symbolic.assertions->appendList( assertions );
701 } else {
702 type->symbolic.assertions = assertions;
703 } // if
704 break;
705 default:
706 assert( false );
707 } // switch
708
709 return this;
710}
711
712DeclarationNode * DeclarationNode::addName( string * newname ) {
713 assert( ! name );
714 name = newname;
715 return this;
716}
717
718DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
719 assert( ! asmName );
720 asmName = newname ? newname->asmName : nullptr;
721 return this->addQualifiers( newname );
722}
723
724DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
725 bitfieldWidth = size;
726 return this;
727}
728
729DeclarationNode * DeclarationNode::addVarArgs() {
730 assert( type );
731 hasEllipsis = true;
732 return this;
733}
734
735DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
736 assert( type );
737 assert( type->kind == TypeData::Function );
738 assert( ! type->function.body );
739 type->function.body = body;
740 type->function.withExprs = withExprs;
741 return this;
742}
743
744DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
745 assert( type );
746 assert( type->kind == TypeData::Function );
747 assert( ! type->function.oldDeclList );
748 type->function.oldDeclList = list;
749 return this;
750}
751
752DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
753 if ( type ) {
754 TypeData * prevBase = type;
755 TypeData * curBase = type->base;
756 while ( curBase != nullptr ) {
757 prevBase = curBase;
758 curBase = curBase->base;
759 } // while
760 prevBase->base = newType;
761 } else {
762 type = newType;
763 } // if
764 return this;
765}
766
767DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
768 if ( a ) {
769 for ( Attribute *attr: reverseIterate( a->attributes ) ) {
770 attributes.push_front( attr );
771 } // for
772 a->attributes.clear();
773 } // if
774 return this;
775} // copyAttribute
776
777DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
778 if ( p ) {
779 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
780 setBase( p->type );
781 p->type = nullptr;
782 copyAttribute( p );
783 delete p;
784 } // if
785 return this;
786}
787
788DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
789 if ( a ) {
790 assert( a->type->kind == TypeData::Array );
791 setBase( a->type );
792 a->type = nullptr;
793 copyAttribute( a );
794 delete a;
795 } // if
796 return this;
797}
798
799DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
800 if ( p ) {
801 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
802 if ( type ) {
803 switch ( type->kind ) {
804 case TypeData::Aggregate:
805 case TypeData::Enum:
806 p->type->base = new TypeData( TypeData::AggregateInst );
807 p->type->base->aggInst.aggregate = type;
808 if ( type->kind == TypeData::Aggregate ) {
809 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
810 } // if
811 p->type->base->qualifiers |= type->qualifiers;
812 break;
813
814 default:
815 p->type->base = type;
816 } // switch
817 type = nullptr;
818 } // if
819 delete this;
820 return p;
821 } else {
822 return this;
823 } // if
824}
825
826static TypeData * findLast( TypeData * a ) {
827 assert( a );
828 TypeData * cur = a;
829 while ( cur->base ) {
830 cur = cur->base;
831 } // while
832 return cur;
833}
834
835DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
836 if ( ! a ) return this;
837 assert( a->type->kind == TypeData::Array );
838 TypeData * lastArray = findLast( a->type );
839 if ( type ) {
840 switch ( type->kind ) {
841 case TypeData::Aggregate:
842 case TypeData::Enum:
843 lastArray->base = new TypeData( TypeData::AggregateInst );
844 lastArray->base->aggInst.aggregate = type;
845 if ( type->kind == TypeData::Aggregate ) {
846 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
847 } // if
848 lastArray->base->qualifiers |= type->qualifiers;
849 break;
850 default:
851 lastArray->base = type;
852 } // switch
853 type = nullptr;
854 } // if
855 delete this;
856 return a;
857}
858
859DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
860 TypeData * ftype = new TypeData( TypeData::Function );
861 ftype->function.params = params;
862 setBase( ftype );
863 return this;
864}
865
866static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
867 if ( type ) {
868 if ( type->kind != TypeData::Function ) {
869 type->base = addIdListToType( type->base, ids );
870 } else {
871 type->function.idList = ids;
872 } // if
873 return type;
874 } else {
875 TypeData * newtype = new TypeData( TypeData::Function );
876 newtype->function.idList = ids;
877 return newtype;
878 } // if
879} // addIdListToType
880
881DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
882 type = addIdListToType( type, ids );
883 return this;
884}
885
886DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
887 initializer = init;
888 return this;
889}
890
891DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
892 assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
893 variable.initializer = init;
894 return this;
895}
896
897DeclarationNode * DeclarationNode::cloneType( string * newName ) {
898 DeclarationNode * newnode = new DeclarationNode;
899 newnode->type = maybeClone( type );
900 newnode->copySpecifiers( this );
901 assert( newName );
902 newnode->name = newName;
903 return newnode;
904}
905
906DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
907 if ( ! o ) return nullptr;
908
909 o->copySpecifiers( this );
910 if ( type ) {
911 TypeData * srcType = type;
912
913 // search for the base type by scanning off pointers and array designators
914 while ( srcType->base ) {
915 srcType = srcType->base;
916 } // while
917
918 TypeData * newType = srcType->clone();
919 if ( newType->kind == TypeData::AggregateInst ) {
920 // don't duplicate members
921 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
922 delete newType->aggInst.aggregate->enumeration.constants;
923 newType->aggInst.aggregate->enumeration.constants = nullptr;
924 newType->aggInst.aggregate->enumeration.body = false;
925 } else {
926 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
927 delete newType->aggInst.aggregate->aggregate.fields;
928 newType->aggInst.aggregate->aggregate.fields = nullptr;
929 newType->aggInst.aggregate->aggregate.body = false;
930 } // if
931 // don't hoist twice
932 newType->aggInst.hoistType = false;
933 } // if
934
935 newType->forall = maybeClone( type->forall );
936 if ( ! o->type ) {
937 o->type = newType;
938 } else {
939 addTypeToType( newType, o->type );
940 delete newType;
941 } // if
942 } // if
943 return o;
944}
945
946DeclarationNode * DeclarationNode::extractAggregate() const {
947 if ( type ) {
948 TypeData * ret = typeextractAggregate( type );
949 if ( ret ) {
950 DeclarationNode * newnode = new DeclarationNode;
951 newnode->type = ret;
952 return newnode;
953 } // if
954 } // if
955 return nullptr;
956}
957
958void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
959 SemanticErrorException errors;
960 std::back_insert_iterator< std::list< Declaration * > > out( outputList );
961
962 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
963 try {
964 if ( DeclarationNode * extr = cur->extractAggregate() ) {
965 // handle the case where a structure declaration is contained within an object or type declaration
966 Declaration * decl = extr->build();
967 if ( decl ) {
968 decl->location = cur->location;
969 * out++ = decl;
970 } // if
971 delete extr;
972 } // if
973
974 Declaration * decl = cur->build();
975 if ( decl ) {
976 decl->location = cur->location;
977 * out++ = decl;
978 } // if
979 } catch( SemanticErrorException &e ) {
980 errors.append( e );
981 } // try
982 } // while
983
984 if ( ! errors.isEmpty() ) {
985 throw errors;
986 } // if
987} // buildList
988
989void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
990 SemanticErrorException errors;
991 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
992
993 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
994 try {
995 Declaration * decl = cur->build();
996 if ( decl ) {
997 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
998 dwt->location = cur->location;
999 * out++ = dwt;
1000 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
1001 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
1002 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1003 obj->location = cur->location;
1004 * out++ = obj;
1005 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
1006 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
1007 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1008 obj->location = cur->location;
1009 * out++ = obj;
1010 } // if
1011 } // if
1012 } catch( SemanticErrorException &e ) {
1013 errors.append( e );
1014 } // try
1015 } // for
1016
1017 if ( ! errors.isEmpty() ) {
1018 throw errors;
1019 } // if
1020} // buildList
1021
1022void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
1023 SemanticErrorException errors;
1024 std::back_insert_iterator< std::list< Type * > > out( outputList );
1025 const DeclarationNode * cur = firstNode;
1026
1027 while ( cur ) {
1028 try {
1029 * out++ = cur->buildType();
1030 } catch( SemanticErrorException &e ) {
1031 errors.append( e );
1032 } // try
1033 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
1034 } // while
1035
1036 if ( ! errors.isEmpty() ) {
1037 throw errors;
1038 } // if
1039} // buildTypeList
1040
1041Declaration * DeclarationNode::build() const {
1042 if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
1043
1044 if ( asmStmt ) {
1045 return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
1046 } // if
1047
1048 if ( variable.tyClass != NoTypeClass ) {
1049 // otype is internally converted to dtype + otype parameters
1050 static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1051 assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." );
1052 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
1053 TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr );
1054 buildList( variable.assertions, ret->get_assertions() );
1055 return ret;
1056 } // if
1057
1058 if ( type ) {
1059 // Function specifiers can only appear on a function definition/declaration.
1060 //
1061 // inline _Noreturn int f(); // allowed
1062 // inline _Noreturn int g( int i ); // allowed
1063 // inline _Noreturn int i; // disallowed
1064 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
1065 SemanticError( this, "invalid function specifier for " );
1066 } // if
1067 return buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
1068 } // if
1069
1070 if ( assert.condition ) {
1071 return new StaticAssertDecl( maybeBuild< Expression >( assert.condition ), strict_dynamic_cast< ConstantExpr * >( maybeClone( assert.message ) ) );
1072 }
1073
1074 // SUE's cannot have function specifiers, either
1075 //
1076 // inlne _Noreturn struct S { ... }; // disallowed
1077 // inlne _Noreturn enum E { ... }; // disallowed
1078 if ( funcSpecs.any() ) {
1079 SemanticError( this, "invalid function specifier for " );
1080 } // if
1081 assertf( name, "ObjectDecl must a have name\n" );
1082 return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
1083}
1084
1085Type * DeclarationNode::buildType() const {
1086 assert( type );
1087
1088 if ( attr.expr ) {
1089 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
1090 } else if ( attr.type ) {
1091 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
1092 } // if
1093
1094 switch ( type->kind ) {
1095 case TypeData::Enum:
1096 case TypeData::Aggregate: {
1097 ReferenceToType * ret = buildComAggInst( type, attributes, linkage );
1098 buildList( type->aggregate.actuals, ret->get_parameters() );
1099 return ret;
1100 }
1101 case TypeData::Symbolic: {
1102 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
1103 buildList( type->symbolic.actuals, ret->get_parameters() );
1104 return ret;
1105 }
1106 default:
1107 Type * simpletypes = typebuild( type );
1108 simpletypes->get_attributes() = attributes; // copy because member is const
1109 return simpletypes;
1110 } // switch
1111}
1112
1113// Local Variables: //
1114// tab-width: 4 //
1115// mode: c++ //
1116// compile-command: "make install" //
1117// End: //
Note: See TracBrowser for help on using the repository browser.