source: src/Parser/DeclarationNode.cc@ 20737104

ADT ast-experimental pthread-emulation
Last change on this file since 20737104 was 4520b77e, checked in by JiadaL <j82liang@…>, 3 years ago

Merge to Master Sept 19

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