source: src/Parser/DeclarationNode.cc@ 1b2adec

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 1b2adec was 374cb117, checked in by JiadaL <j82liang@…>, 3 years ago

Replace the interface for EnumDecl node construction to support generic enum types

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