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 Jul 14 14:46:32 2015
|
---|
13 | // Update Count : 126
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <string>
|
---|
17 | #include <list>
|
---|
18 | #include <iterator>
|
---|
19 | #include <algorithm>
|
---|
20 | #include <cassert>
|
---|
21 |
|
---|
22 | #include "TypeData.h"
|
---|
23 |
|
---|
24 | #include "SynTree/Declaration.h"
|
---|
25 | #include "SynTree/Expression.h"
|
---|
26 |
|
---|
27 | #include "Parser.h"
|
---|
28 | #include "TypedefTable.h"
|
---|
29 | extern TypedefTable typedefTable;
|
---|
30 |
|
---|
31 | using namespace std;
|
---|
32 |
|
---|
33 | // These must remain in the same order as the corresponding DeclarationNode enumerations.
|
---|
34 | const char *DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "" };
|
---|
35 | const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic" };
|
---|
36 | const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary" };
|
---|
37 | const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" };
|
---|
38 | const char *DeclarationNode::aggregateName[] = { "struct", "union", "context" };
|
---|
39 | const char *DeclarationNode::typeClassName[] = { "type", "dtype", "ftype" };
|
---|
40 |
|
---|
41 | UniqueName DeclarationNode::anonymous( "__anonymous" );
|
---|
42 |
|
---|
43 | extern LinkageSpec::Type linkage; /* defined in cfa.y */
|
---|
44 |
|
---|
45 | DeclarationNode *DeclarationNode::clone() const {
|
---|
46 | DeclarationNode *newnode = new DeclarationNode;
|
---|
47 | newnode->type = maybeClone( type );
|
---|
48 | newnode->name = name;
|
---|
49 | newnode->storageClasses = storageClasses;
|
---|
50 | newnode->bitfieldWidth = maybeClone( bitfieldWidth );
|
---|
51 | newnode->hasEllipsis = hasEllipsis;
|
---|
52 | newnode->initializer = initializer;
|
---|
53 | newnode->next = maybeClone( next );
|
---|
54 | newnode->linkage = linkage;
|
---|
55 | return newnode;
|
---|
56 | }
|
---|
57 |
|
---|
58 | DeclarationNode::DeclarationNode() : type( 0 ), bitfieldWidth( 0 ), initializer( 0 ), hasEllipsis( false ), linkage( ::linkage ) {
|
---|
59 | }
|
---|
60 |
|
---|
61 | DeclarationNode::~DeclarationNode() {
|
---|
62 | delete type;
|
---|
63 | delete bitfieldWidth;
|
---|
64 | delete initializer;
|
---|
65 | }
|
---|
66 |
|
---|
67 | bool DeclarationNode::get_hasEllipsis() const {
|
---|
68 | return hasEllipsis;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void DeclarationNode::print( std::ostream &os, int indent ) const {
|
---|
72 | os << string( indent, ' ' );
|
---|
73 | if ( name == "" ) {
|
---|
74 | os << "unnamed: ";
|
---|
75 | } else {
|
---|
76 | os << name << ": ";
|
---|
77 | } // if
|
---|
78 |
|
---|
79 | if ( linkage != LinkageSpec::Cforall ) {
|
---|
80 | os << LinkageSpec::toString( linkage ) << " ";
|
---|
81 | } // if
|
---|
82 |
|
---|
83 | printEnums( storageClasses.begin(), storageClasses.end(), DeclarationNode::storageName, os );
|
---|
84 | if ( type ) {
|
---|
85 | type->print( os, indent );
|
---|
86 | } else {
|
---|
87 | os << "untyped entity ";
|
---|
88 | } // if
|
---|
89 |
|
---|
90 | if ( bitfieldWidth ) {
|
---|
91 | os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
|
---|
92 | bitfieldWidth->printOneLine( os );
|
---|
93 | } // if
|
---|
94 |
|
---|
95 | if ( initializer != 0 ) {
|
---|
96 | os << endl << string( indent + 2, ' ' ) << "with initializer ";
|
---|
97 | initializer->printOneLine( os );
|
---|
98 | } // if
|
---|
99 |
|
---|
100 | os << endl;
|
---|
101 | }
|
---|
102 |
|
---|
103 | void DeclarationNode::printList( std::ostream &os, int indent ) const {
|
---|
104 | ParseNode::printList( os, indent );
|
---|
105 | if ( hasEllipsis ) {
|
---|
106 | os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
|
---|
107 | } // if
|
---|
108 | }
|
---|
109 |
|
---|
110 | DeclarationNode *DeclarationNode::newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle ) {
|
---|
111 | DeclarationNode *newnode = new DeclarationNode;
|
---|
112 | newnode->name = assign_strptr( name );
|
---|
113 |
|
---|
114 | newnode->type = new TypeData( TypeData::Function );
|
---|
115 | newnode->type->function->params = param;
|
---|
116 | newnode->type->function->newStyle = newStyle;
|
---|
117 | newnode->type->function->body = body;
|
---|
118 |
|
---|
119 | if ( body ) {
|
---|
120 | newnode->type->function->hasBody = true;
|
---|
121 | } // if
|
---|
122 |
|
---|
123 | if ( ret ) {
|
---|
124 | newnode->type->base = ret->type;
|
---|
125 | ret->type = 0;
|
---|
126 | delete ret;
|
---|
127 | } // if
|
---|
128 |
|
---|
129 | return newnode;
|
---|
130 | }
|
---|
131 |
|
---|
132 | DeclarationNode *DeclarationNode::newQualifier( Qualifier q ) {
|
---|
133 | DeclarationNode *newnode = new DeclarationNode;
|
---|
134 | newnode->type = new TypeData();
|
---|
135 | newnode->type->qualifiers.push_back( q );
|
---|
136 | return newnode;
|
---|
137 | }
|
---|
138 |
|
---|
139 | DeclarationNode *DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
|
---|
140 | DeclarationNode *newnode = new DeclarationNode;
|
---|
141 | newnode->storageClasses.push_back( sc );
|
---|
142 | return newnode;
|
---|
143 | }
|
---|
144 |
|
---|
145 | DeclarationNode *DeclarationNode::newBasicType( BasicType bt ) {
|
---|
146 | DeclarationNode *newnode = new DeclarationNode;
|
---|
147 | newnode->type = new TypeData( TypeData::Basic );
|
---|
148 | newnode->type->basic->typeSpec.push_back( bt );
|
---|
149 | return newnode;
|
---|
150 | }
|
---|
151 |
|
---|
152 | DeclarationNode *DeclarationNode::newModifier( Modifier mod ) {
|
---|
153 | DeclarationNode *newnode = new DeclarationNode;
|
---|
154 | newnode->type = new TypeData( TypeData::Basic );
|
---|
155 | newnode->type->basic->modifiers.push_back( mod );
|
---|
156 | return newnode;
|
---|
157 | }
|
---|
158 |
|
---|
159 | DeclarationNode *DeclarationNode::newForall( DeclarationNode *forall ) {
|
---|
160 | DeclarationNode *newnode = new DeclarationNode;
|
---|
161 | newnode->type = new TypeData( TypeData::Unknown );
|
---|
162 | newnode->type->forall = forall;
|
---|
163 | return newnode;
|
---|
164 | }
|
---|
165 |
|
---|
166 | DeclarationNode *DeclarationNode::newFromTypedef( std::string *name ) {
|
---|
167 | DeclarationNode *newnode = new DeclarationNode;
|
---|
168 | newnode->type = new TypeData( TypeData::SymbolicInst );
|
---|
169 | newnode->type->symbolic->name = assign_strptr( name );
|
---|
170 | newnode->type->symbolic->isTypedef = true;
|
---|
171 | newnode->type->symbolic->params = 0;
|
---|
172 | return newnode;
|
---|
173 | }
|
---|
174 |
|
---|
175 | DeclarationNode *DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields ) {
|
---|
176 | DeclarationNode *newnode = new DeclarationNode;
|
---|
177 | newnode->type = new TypeData( TypeData::Aggregate );
|
---|
178 | newnode->type->aggregate->kind = kind;
|
---|
179 | newnode->type->aggregate->name = assign_strptr( name );
|
---|
180 | if ( newnode->type->aggregate->name == "" ) { // anonymous aggregate ?
|
---|
181 | newnode->type->aggregate->name = DeclarationNode::anonymous.newName();
|
---|
182 | } else {
|
---|
183 | // SKULLDUGGERY: generate a typedef for the aggregate name so that the aggregate does not have to be qualified
|
---|
184 | // by "struct"
|
---|
185 | typedefTable.addToEnclosingScope( newnode->type->aggregate->name, TypedefTable::TD );
|
---|
186 | DeclarationNode *typedf = new DeclarationNode;
|
---|
187 | typedf->name = newnode->type->aggregate->name;
|
---|
188 | newnode->appendList( typedf->addType( newnode->clone() )->addTypedef() );
|
---|
189 | } // if
|
---|
190 | newnode->type->aggregate->actuals = actuals;
|
---|
191 | newnode->type->aggregate->fields = fields;
|
---|
192 | return newnode;
|
---|
193 | }
|
---|
194 |
|
---|
195 | DeclarationNode *DeclarationNode::newEnum( std::string *name, DeclarationNode *constants ) {
|
---|
196 | DeclarationNode *newnode = new DeclarationNode;
|
---|
197 | newnode->name = assign_strptr( name );
|
---|
198 | newnode->type = new TypeData( TypeData::Enum );
|
---|
199 | newnode->type->enumeration->name = newnode->name;
|
---|
200 | if ( newnode->type->enumeration->name == "" ) { // anonymous enumeration ?
|
---|
201 | newnode->type->enumeration->name = DeclarationNode::anonymous.newName();
|
---|
202 | } else {
|
---|
203 | // SKULLDUGGERY: generate a typedef for the enumeration name so that the enumeration does not have to be
|
---|
204 | // qualified by "enum"
|
---|
205 | typedefTable.addToEnclosingScope( newnode->type->enumeration->name, TypedefTable::TD );
|
---|
206 | DeclarationNode *typedf = new DeclarationNode;
|
---|
207 | typedf->name = newnode->type->enumeration->name;
|
---|
208 | newnode->appendList( typedf->addType( newnode->clone() )->addTypedef() );
|
---|
209 | } // if
|
---|
210 | newnode->type->enumeration->constants = constants;
|
---|
211 | return newnode;
|
---|
212 | }
|
---|
213 |
|
---|
214 | DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) {
|
---|
215 | DeclarationNode *newnode = new DeclarationNode;
|
---|
216 | newnode->name = assign_strptr( name );
|
---|
217 | // do something with the constant
|
---|
218 | return newnode;
|
---|
219 | }
|
---|
220 |
|
---|
221 | DeclarationNode *DeclarationNode::newName( std::string *name ) {
|
---|
222 | DeclarationNode *newnode = new DeclarationNode;
|
---|
223 | newnode->name = assign_strptr( name );
|
---|
224 | return newnode;
|
---|
225 | }
|
---|
226 |
|
---|
227 | DeclarationNode *DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) {
|
---|
228 | DeclarationNode *newnode = new DeclarationNode;
|
---|
229 | newnode->type = new TypeData( TypeData::SymbolicInst );
|
---|
230 | newnode->type->symbolic->name = assign_strptr( name );
|
---|
231 | newnode->type->symbolic->isTypedef = false;
|
---|
232 | newnode->type->symbolic->actuals = params;
|
---|
233 | return newnode;
|
---|
234 | }
|
---|
235 |
|
---|
236 | DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string *name ) {
|
---|
237 | DeclarationNode *newnode = new DeclarationNode;
|
---|
238 | newnode->name = assign_strptr( name );
|
---|
239 | newnode->type = new TypeData( TypeData::Variable );
|
---|
240 | newnode->type->variable->tyClass = tc;
|
---|
241 | newnode->type->variable->name = newnode->name;
|
---|
242 | return newnode;
|
---|
243 | }
|
---|
244 |
|
---|
245 | DeclarationNode *DeclarationNode::newContext( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) {
|
---|
246 | DeclarationNode *newnode = new DeclarationNode;
|
---|
247 | newnode->type = new TypeData( TypeData::Aggregate );
|
---|
248 | newnode->type->aggregate->kind = Context;
|
---|
249 | newnode->type->aggregate->params = params;
|
---|
250 | newnode->type->aggregate->fields = asserts;
|
---|
251 | newnode->type->aggregate->name = assign_strptr( name );
|
---|
252 | return newnode;
|
---|
253 | }
|
---|
254 |
|
---|
255 | DeclarationNode *DeclarationNode::newContextUse( std::string *name, ExpressionNode *params ) {
|
---|
256 | DeclarationNode *newnode = new DeclarationNode;
|
---|
257 | newnode->type = new TypeData( TypeData::AggregateInst );
|
---|
258 | newnode->type->aggInst->aggregate = new TypeData( TypeData::Aggregate );
|
---|
259 | newnode->type->aggInst->aggregate->aggregate->kind = Context;
|
---|
260 | newnode->type->aggInst->aggregate->aggregate->name = assign_strptr( name );
|
---|
261 | newnode->type->aggInst->params = params;
|
---|
262 | return newnode;
|
---|
263 | }
|
---|
264 |
|
---|
265 | DeclarationNode *DeclarationNode::newTypeDecl( std::string *name, DeclarationNode *typeParams ) {
|
---|
266 | DeclarationNode *newnode = new DeclarationNode;
|
---|
267 | newnode->name = assign_strptr( name );
|
---|
268 | newnode->type = new TypeData( TypeData::Symbolic );
|
---|
269 | newnode->type->symbolic->isTypedef = false;
|
---|
270 | newnode->type->symbolic->params = typeParams;
|
---|
271 | newnode->type->symbolic->name = newnode->name;
|
---|
272 | return newnode;
|
---|
273 | }
|
---|
274 |
|
---|
275 | DeclarationNode *DeclarationNode::newPointer( DeclarationNode *qualifiers ) {
|
---|
276 | DeclarationNode *newnode = new DeclarationNode;
|
---|
277 | newnode->type = new TypeData( TypeData::Pointer );
|
---|
278 | return newnode->addQualifiers( qualifiers );
|
---|
279 | }
|
---|
280 |
|
---|
281 | DeclarationNode *DeclarationNode::newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ) {
|
---|
282 | DeclarationNode *newnode = new DeclarationNode;
|
---|
283 | newnode->type = new TypeData( TypeData::Array );
|
---|
284 | newnode->type->array->dimension = size;
|
---|
285 | newnode->type->array->isStatic = isStatic;
|
---|
286 | if ( newnode->type->array->dimension == 0 || dynamic_cast<ConstantNode *>( newnode->type->array->dimension ) ) {
|
---|
287 | newnode->type->array->isVarLen = false;
|
---|
288 | } else {
|
---|
289 | newnode->type->array->isVarLen = true;
|
---|
290 | } // if
|
---|
291 | return newnode->addQualifiers( qualifiers );
|
---|
292 | }
|
---|
293 |
|
---|
294 | DeclarationNode *DeclarationNode::newVarArray( DeclarationNode *qualifiers ) {
|
---|
295 | DeclarationNode *newnode = new DeclarationNode;
|
---|
296 | newnode->type = new TypeData( TypeData::Array );
|
---|
297 | newnode->type->array->dimension = 0;
|
---|
298 | newnode->type->array->isStatic = false;
|
---|
299 | newnode->type->array->isVarLen = true;
|
---|
300 | return newnode->addQualifiers( qualifiers );
|
---|
301 | }
|
---|
302 |
|
---|
303 | DeclarationNode *DeclarationNode::newBitfield( ExpressionNode *size ) {
|
---|
304 | DeclarationNode *newnode = new DeclarationNode;
|
---|
305 | newnode->bitfieldWidth = size;
|
---|
306 | return newnode;
|
---|
307 | }
|
---|
308 |
|
---|
309 | DeclarationNode *DeclarationNode::newTuple( DeclarationNode *members ) {
|
---|
310 | DeclarationNode *newnode = new DeclarationNode;
|
---|
311 | newnode->type = new TypeData( TypeData::Tuple );
|
---|
312 | newnode->type->tuple->members = members;
|
---|
313 | return newnode;
|
---|
314 | }
|
---|
315 |
|
---|
316 | DeclarationNode *DeclarationNode::newTypeof( ExpressionNode *expr ) {
|
---|
317 | DeclarationNode *newnode = new DeclarationNode;
|
---|
318 | newnode->type = new TypeData( TypeData::Typeof );
|
---|
319 | newnode->type->typeexpr->expr = expr;
|
---|
320 | return newnode;
|
---|
321 | }
|
---|
322 |
|
---|
323 | DeclarationNode *DeclarationNode::newAttr( std::string *name, ExpressionNode *expr ) {
|
---|
324 | DeclarationNode *newnode = new DeclarationNode;
|
---|
325 | newnode->type = new TypeData( TypeData::Attr );
|
---|
326 | newnode->type->attr->name = assign_strptr( name );
|
---|
327 | newnode->type->attr->expr = expr;
|
---|
328 | return newnode;
|
---|
329 | }
|
---|
330 |
|
---|
331 | DeclarationNode *DeclarationNode::newAttr( std::string *name, DeclarationNode *type ) {
|
---|
332 | DeclarationNode *newnode = new DeclarationNode;
|
---|
333 | newnode->type = new TypeData( TypeData::Attr );
|
---|
334 | newnode->type->attr->name = assign_strptr( name );
|
---|
335 | newnode->type->attr->type = type;
|
---|
336 | return newnode;
|
---|
337 | }
|
---|
338 |
|
---|
339 | static void addQualifiersToType( TypeData *&src, TypeData *dst ) {
|
---|
340 | if ( src && dst ) {
|
---|
341 | if ( src->forall && dst->kind == TypeData::Function ) {
|
---|
342 | if ( dst->forall ) {
|
---|
343 | dst->forall->appendList( src->forall );
|
---|
344 | } else {
|
---|
345 | dst->forall = src->forall;
|
---|
346 | } // if
|
---|
347 | src->forall = 0;
|
---|
348 | } // if
|
---|
349 | if ( dst->base ) {
|
---|
350 | addQualifiersToType( src, dst->base );
|
---|
351 | } else if ( dst->kind == TypeData::Function ) {
|
---|
352 | dst->base = src;
|
---|
353 | src = 0;
|
---|
354 | } else {
|
---|
355 | dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
|
---|
356 | } // if
|
---|
357 | } // if
|
---|
358 | }
|
---|
359 |
|
---|
360 | DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) {
|
---|
361 | if ( q ) {
|
---|
362 | storageClasses.splice( storageClasses.end(), q->storageClasses );
|
---|
363 | if ( q->type ) {
|
---|
364 | if ( ! type ) {
|
---|
365 | type = new TypeData;
|
---|
366 | } // if
|
---|
367 | addQualifiersToType( q->type, type );
|
---|
368 | if ( q->type && q->type->forall ) {
|
---|
369 | if ( type->forall ) {
|
---|
370 | type->forall->appendList( q->type->forall );
|
---|
371 | } else {
|
---|
372 | if ( type->kind == TypeData::Aggregate ) {
|
---|
373 | type->aggregate->params = q->type->forall;
|
---|
374 | // change implicit typedef from TYPEDEFname to TYPEGENname
|
---|
375 | typedefTable.changeKind( type->aggregate->name, TypedefTable::TG );
|
---|
376 | } else {
|
---|
377 | type->forall = q->type->forall;
|
---|
378 | } // if
|
---|
379 | } // if
|
---|
380 | q->type->forall = 0;
|
---|
381 | } // if
|
---|
382 | } // if
|
---|
383 | } // if
|
---|
384 | delete q;
|
---|
385 | return this;
|
---|
386 | }
|
---|
387 |
|
---|
388 | DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
|
---|
389 | storageClasses = q->storageClasses;
|
---|
390 | return this;
|
---|
391 | }
|
---|
392 |
|
---|
393 | static void addTypeToType( TypeData *&src, TypeData *&dst ) {
|
---|
394 | if ( src && dst ) {
|
---|
395 | if ( src->forall && dst->kind == TypeData::Function ) {
|
---|
396 | if ( dst->forall ) {
|
---|
397 | dst->forall->appendList( src->forall );
|
---|
398 | } else {
|
---|
399 | dst->forall = src->forall;
|
---|
400 | } // if
|
---|
401 | src->forall = 0;
|
---|
402 | } // if
|
---|
403 | if ( dst->base ) {
|
---|
404 | addTypeToType( src, dst->base );
|
---|
405 | } else {
|
---|
406 | switch ( dst->kind ) {
|
---|
407 | case TypeData::Unknown:
|
---|
408 | src->qualifiers.splice( src->qualifiers.end(), dst->qualifiers );
|
---|
409 | dst = src;
|
---|
410 | src = 0;
|
---|
411 | break;
|
---|
412 | case TypeData::Basic:
|
---|
413 | dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
|
---|
414 | if ( src->kind != TypeData::Unknown ) {
|
---|
415 | assert( src->kind == TypeData::Basic );
|
---|
416 | dst->basic->modifiers.splice( dst->basic->modifiers.end(), src->basic->modifiers );
|
---|
417 | dst->basic->typeSpec.splice( dst->basic->typeSpec.end(), src->basic->typeSpec );
|
---|
418 | } // if
|
---|
419 | break;
|
---|
420 | default:
|
---|
421 | switch ( src->kind ) {
|
---|
422 | case TypeData::Aggregate:
|
---|
423 | case TypeData::Enum:
|
---|
424 | dst->base = new TypeData( TypeData::AggregateInst );
|
---|
425 | dst->base->aggInst->aggregate = src;
|
---|
426 | if ( src->kind == TypeData::Aggregate ) {
|
---|
427 | dst->base->aggInst->params = maybeClone( src->aggregate->actuals );
|
---|
428 | } // if
|
---|
429 | dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers );
|
---|
430 | src = 0;
|
---|
431 | break;
|
---|
432 | default:
|
---|
433 | if ( dst->forall ) {
|
---|
434 | dst->forall->appendList( src->forall );
|
---|
435 | } else {
|
---|
436 | dst->forall = src->forall;
|
---|
437 | } // if
|
---|
438 | src->forall = 0;
|
---|
439 | dst->base = src;
|
---|
440 | src = 0;
|
---|
441 | } // switch
|
---|
442 | } // switch
|
---|
443 | } // if
|
---|
444 | } // if
|
---|
445 | }
|
---|
446 |
|
---|
447 | DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
|
---|
448 | if ( o ) {
|
---|
449 | storageClasses.splice( storageClasses.end(), o->storageClasses );
|
---|
450 | if ( o->type ) {
|
---|
451 | if ( ! type ) {
|
---|
452 | if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
|
---|
453 | type = new TypeData( TypeData::AggregateInst );
|
---|
454 | type->aggInst->aggregate = o->type;
|
---|
455 | if ( o->type->kind == TypeData::Aggregate ) {
|
---|
456 | type->aggInst->params = maybeClone( o->type->aggregate->actuals );
|
---|
457 | } // if
|
---|
458 | type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers );
|
---|
459 | } else {
|
---|
460 | type = o->type;
|
---|
461 | } // if
|
---|
462 | o->type = 0;
|
---|
463 | } else {
|
---|
464 | addTypeToType( o->type, type );
|
---|
465 | } // if
|
---|
466 | } // if
|
---|
467 | if ( o->bitfieldWidth ) {
|
---|
468 | bitfieldWidth = o->bitfieldWidth;
|
---|
469 | } // if
|
---|
470 |
|
---|
471 | // there may be typedefs chained onto the type
|
---|
472 | if ( o->get_link() ) {
|
---|
473 | set_link( o->get_link()->clone() );
|
---|
474 | } // if
|
---|
475 | } // if
|
---|
476 | delete o;
|
---|
477 | return this;
|
---|
478 | }
|
---|
479 |
|
---|
480 | DeclarationNode *DeclarationNode::addTypedef() {
|
---|
481 | TypeData *newtype = new TypeData( TypeData::Symbolic );
|
---|
482 | newtype->symbolic->params = 0;
|
---|
483 | newtype->symbolic->isTypedef = true;
|
---|
484 | newtype->symbolic->name = name;
|
---|
485 | newtype->base = type;
|
---|
486 | type = newtype;
|
---|
487 | return this;
|
---|
488 | }
|
---|
489 |
|
---|
490 | DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) {
|
---|
491 | assert( type );
|
---|
492 | switch ( type->kind ) {
|
---|
493 | case TypeData::Symbolic:
|
---|
494 | if ( type->symbolic->assertions ) {
|
---|
495 | type->symbolic->assertions->appendList( assertions );
|
---|
496 | } else {
|
---|
497 | type->symbolic->assertions = assertions;
|
---|
498 | } // if
|
---|
499 | break;
|
---|
500 | case TypeData::Variable:
|
---|
501 | if ( type->variable->assertions ) {
|
---|
502 | type->variable->assertions->appendList( assertions );
|
---|
503 | } else {
|
---|
504 | type->variable->assertions = assertions;
|
---|
505 | } // if
|
---|
506 | break;
|
---|
507 | default:
|
---|
508 | assert( false );
|
---|
509 | } // switch
|
---|
510 |
|
---|
511 | return this;
|
---|
512 | }
|
---|
513 |
|
---|
514 | DeclarationNode *DeclarationNode::addName( std::string *newname ) {
|
---|
515 | name = assign_strptr( newname );
|
---|
516 | return this;
|
---|
517 | }
|
---|
518 |
|
---|
519 | DeclarationNode *DeclarationNode::addBitfield( ExpressionNode *size ) {
|
---|
520 | bitfieldWidth = size;
|
---|
521 | return this;
|
---|
522 | }
|
---|
523 |
|
---|
524 | DeclarationNode *DeclarationNode::addVarArgs() {
|
---|
525 | assert( type );
|
---|
526 | hasEllipsis = true;
|
---|
527 | return this;
|
---|
528 | }
|
---|
529 |
|
---|
530 | DeclarationNode *DeclarationNode::addFunctionBody( StatementNode *body ) {
|
---|
531 | assert( type );
|
---|
532 | assert( type->kind == TypeData::Function );
|
---|
533 | assert( type->function->body == 0 );
|
---|
534 | type->function->body = body;
|
---|
535 | type->function->hasBody = true;
|
---|
536 | return this;
|
---|
537 | }
|
---|
538 |
|
---|
539 | DeclarationNode *DeclarationNode::addOldDeclList( DeclarationNode *list ) {
|
---|
540 | assert( type );
|
---|
541 | assert( type->kind == TypeData::Function );
|
---|
542 | assert( type->function->oldDeclList == 0 );
|
---|
543 | type->function->oldDeclList = list;
|
---|
544 | return this;
|
---|
545 | }
|
---|
546 |
|
---|
547 | static void setBase( TypeData *&type, TypeData *newType ) {
|
---|
548 | if ( type ) {
|
---|
549 | TypeData *prevBase = type;
|
---|
550 | TypeData *curBase = type->base;
|
---|
551 | while ( curBase != 0 ) {
|
---|
552 | prevBase = curBase;
|
---|
553 | curBase = curBase->base;
|
---|
554 | } // while
|
---|
555 | prevBase->base = newType;
|
---|
556 | } else {
|
---|
557 | type = newType;
|
---|
558 | } // if
|
---|
559 | }
|
---|
560 |
|
---|
561 | DeclarationNode *DeclarationNode::addPointer( DeclarationNode *p ) {
|
---|
562 | if ( p ) {
|
---|
563 | assert( p->type->kind == TypeData::Pointer );
|
---|
564 | setBase( type, p->type );
|
---|
565 | p->type = 0;
|
---|
566 | delete p;
|
---|
567 | } // if
|
---|
568 | return this;
|
---|
569 | }
|
---|
570 |
|
---|
571 | DeclarationNode *DeclarationNode::addArray( DeclarationNode *a ) {
|
---|
572 | if ( a ) {
|
---|
573 | assert( a->type->kind == TypeData::Array );
|
---|
574 | setBase( type, a->type );
|
---|
575 | a->type = 0;
|
---|
576 | delete a;
|
---|
577 | } // if
|
---|
578 | return this;
|
---|
579 | }
|
---|
580 |
|
---|
581 | DeclarationNode *DeclarationNode::addNewPointer( DeclarationNode *p ) {
|
---|
582 | if ( p ) {
|
---|
583 | assert( p->type->kind == TypeData::Pointer );
|
---|
584 | if ( type ) {
|
---|
585 | switch ( type->kind ) {
|
---|
586 | case TypeData::Aggregate:
|
---|
587 | case TypeData::Enum:
|
---|
588 | p->type->base = new TypeData( TypeData::AggregateInst );
|
---|
589 | p->type->base->aggInst->aggregate = type;
|
---|
590 | if ( type->kind == TypeData::Aggregate ) {
|
---|
591 | p->type->base->aggInst->params = maybeClone( type->aggregate->actuals );
|
---|
592 | } // if
|
---|
593 | p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers );
|
---|
594 | break;
|
---|
595 |
|
---|
596 | default:
|
---|
597 | p->type->base = type;
|
---|
598 | } // switch
|
---|
599 | type = 0;
|
---|
600 | } // if
|
---|
601 | delete this;
|
---|
602 | return p;
|
---|
603 | } else {
|
---|
604 | return this;
|
---|
605 | } // if
|
---|
606 | }
|
---|
607 |
|
---|
608 | static TypeData *findLast( TypeData *a ) {
|
---|
609 | assert( a );
|
---|
610 | TypeData *cur = a;
|
---|
611 | while ( cur->base ) {
|
---|
612 | cur = cur->base;
|
---|
613 | } // while
|
---|
614 | return cur;
|
---|
615 | }
|
---|
616 |
|
---|
617 | DeclarationNode *DeclarationNode::addNewArray( DeclarationNode *a ) {
|
---|
618 | if ( a ) {
|
---|
619 | assert( a->type->kind == TypeData::Array );
|
---|
620 | TypeData *lastArray = findLast( a->type );
|
---|
621 | if ( type ) {
|
---|
622 | switch ( type->kind ) {
|
---|
623 | case TypeData::Aggregate:
|
---|
624 | case TypeData::Enum:
|
---|
625 | lastArray->base = new TypeData( TypeData::AggregateInst );
|
---|
626 | lastArray->base->aggInst->aggregate = type;
|
---|
627 | if ( type->kind == TypeData::Aggregate ) {
|
---|
628 | lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals );
|
---|
629 | } // if
|
---|
630 | lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers );
|
---|
631 | break;
|
---|
632 | default:
|
---|
633 | lastArray->base = type;
|
---|
634 | } // switch
|
---|
635 | type = 0;
|
---|
636 | } // if
|
---|
637 | delete this;
|
---|
638 | return a;
|
---|
639 | } else {
|
---|
640 | return this;
|
---|
641 | } // if
|
---|
642 | }
|
---|
643 |
|
---|
644 | DeclarationNode *DeclarationNode::addParamList( DeclarationNode *params ) {
|
---|
645 | TypeData *ftype = new TypeData( TypeData::Function );
|
---|
646 | ftype->function->params = params;
|
---|
647 | setBase( type, ftype );
|
---|
648 | return this;
|
---|
649 | }
|
---|
650 |
|
---|
651 | static TypeData *addIdListToType( TypeData *type, DeclarationNode *ids ) {
|
---|
652 | if ( type ) {
|
---|
653 | if ( type->kind != TypeData::Function ) {
|
---|
654 | type->base = addIdListToType( type->base, ids );
|
---|
655 | } else {
|
---|
656 | type->function->idList = ids;
|
---|
657 | } // if
|
---|
658 | return type;
|
---|
659 | } else {
|
---|
660 | TypeData *newtype = new TypeData( TypeData::Function );
|
---|
661 | newtype->function->idList = ids;
|
---|
662 | return newtype;
|
---|
663 | } // if
|
---|
664 | }
|
---|
665 |
|
---|
666 | DeclarationNode *DeclarationNode::addIdList( DeclarationNode *ids ) {
|
---|
667 | type = addIdListToType( type, ids );
|
---|
668 | return this;
|
---|
669 | }
|
---|
670 |
|
---|
671 | DeclarationNode *DeclarationNode::addInitializer( InitializerNode *init ) {
|
---|
672 | //assert
|
---|
673 | initializer = init;
|
---|
674 | return this;
|
---|
675 | }
|
---|
676 |
|
---|
677 | DeclarationNode *DeclarationNode::cloneBaseType( string *newName ) {
|
---|
678 | DeclarationNode *newnode = new DeclarationNode;
|
---|
679 | TypeData *srcType = type;
|
---|
680 | while ( srcType->base ) {
|
---|
681 | srcType = srcType->base;
|
---|
682 | } // while
|
---|
683 | newnode->type = maybeClone( srcType );
|
---|
684 | if ( newnode->type->kind == TypeData::AggregateInst ) {
|
---|
685 | // don't duplicate members
|
---|
686 | if ( newnode->type->aggInst->aggregate->kind == TypeData::Enum ) {
|
---|
687 | delete newnode->type->aggInst->aggregate->enumeration->constants;
|
---|
688 | newnode->type->aggInst->aggregate->enumeration->constants = 0;
|
---|
689 | } else {
|
---|
690 | assert( newnode->type->aggInst->aggregate->kind == TypeData::Aggregate );
|
---|
691 | delete newnode->type->aggInst->aggregate->aggregate->fields;
|
---|
692 | newnode->type->aggInst->aggregate->aggregate->fields = 0;
|
---|
693 | } // if
|
---|
694 | } // if
|
---|
695 | newnode->type->forall = maybeClone( type->forall );
|
---|
696 | newnode->storageClasses = storageClasses;
|
---|
697 | newnode->name = assign_strptr( newName );
|
---|
698 | return newnode;
|
---|
699 | }
|
---|
700 |
|
---|
701 | DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) {
|
---|
702 | if ( o ) {
|
---|
703 | o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
|
---|
704 | if ( type ) {
|
---|
705 | TypeData *srcType = type;
|
---|
706 | while ( srcType->base ) {
|
---|
707 | srcType = srcType->base;
|
---|
708 | } // while
|
---|
709 | TypeData *newType = srcType->clone();
|
---|
710 | if ( newType->kind == TypeData::AggregateInst ) {
|
---|
711 | // don't duplicate members
|
---|
712 | if ( newType->aggInst->aggregate->kind == TypeData::Enum ) {
|
---|
713 | delete newType->aggInst->aggregate->enumeration->constants;
|
---|
714 | newType->aggInst->aggregate->enumeration->constants = 0;
|
---|
715 | } else {
|
---|
716 | assert( newType->aggInst->aggregate->kind == TypeData::Aggregate );
|
---|
717 | delete newType->aggInst->aggregate->aggregate->fields;
|
---|
718 | newType->aggInst->aggregate->aggregate->fields = 0;
|
---|
719 | } // if
|
---|
720 | } // if
|
---|
721 | newType->forall = maybeClone( type->forall );
|
---|
722 | if ( ! o->type ) {
|
---|
723 | o->type = newType;
|
---|
724 | } else {
|
---|
725 | addTypeToType( newType, o->type );
|
---|
726 | delete newType;
|
---|
727 | } // if
|
---|
728 | } // if
|
---|
729 | } // if
|
---|
730 | return o;
|
---|
731 | }
|
---|
732 |
|
---|
733 | DeclarationNode *DeclarationNode::cloneType( string *newName ) {
|
---|
734 | DeclarationNode *newnode = new DeclarationNode;
|
---|
735 | newnode->type = maybeClone( type );
|
---|
736 | newnode->storageClasses = storageClasses;
|
---|
737 | newnode->name = assign_strptr( newName );
|
---|
738 | return newnode;
|
---|
739 | }
|
---|
740 |
|
---|
741 | DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) {
|
---|
742 | if ( o ) {
|
---|
743 | o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
|
---|
744 | if ( type ) {
|
---|
745 | TypeData *newType = type->clone();
|
---|
746 | if ( ! o->type ) {
|
---|
747 | o->type = newType;
|
---|
748 | } else {
|
---|
749 | addTypeToType( newType, o->type );
|
---|
750 | delete newType;
|
---|
751 | } // if
|
---|
752 | } // if
|
---|
753 | } // if
|
---|
754 | return o;
|
---|
755 | }
|
---|
756 |
|
---|
757 | DeclarationNode *DeclarationNode::appendList( DeclarationNode *node ) {
|
---|
758 | if ( node != 0 ) {
|
---|
759 | set_link( node );
|
---|
760 | } // if
|
---|
761 | return this;
|
---|
762 | }
|
---|
763 |
|
---|
764 | DeclarationNode *DeclarationNode::extractAggregate() const {
|
---|
765 | if ( type ) {
|
---|
766 | TypeData *ret = type->extractAggregate();
|
---|
767 | if ( ret ) {
|
---|
768 | DeclarationNode *newnode = new DeclarationNode;
|
---|
769 | newnode->type = ret;
|
---|
770 | return newnode;
|
---|
771 | } // if
|
---|
772 | } // if
|
---|
773 | return 0;
|
---|
774 | }
|
---|
775 |
|
---|
776 | void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {
|
---|
777 | SemanticError errors;
|
---|
778 | std::back_insert_iterator< std::list< Declaration *> > out( outputList );
|
---|
779 | const DeclarationNode *cur = firstNode;
|
---|
780 | while ( cur ) {
|
---|
781 | try {
|
---|
782 | if ( DeclarationNode *extr = cur->extractAggregate() ) {
|
---|
783 | // handle the case where a structure declaration is contained within an object or type declaration
|
---|
784 | Declaration *decl = extr->build();
|
---|
785 | if ( decl ) {
|
---|
786 | *out++ = decl;
|
---|
787 | } // if
|
---|
788 | } // if
|
---|
789 | Declaration *decl = cur->build();
|
---|
790 | if ( decl ) {
|
---|
791 | *out++ = decl;
|
---|
792 | } // if
|
---|
793 | } catch( SemanticError &e ) {
|
---|
794 | errors.append( e );
|
---|
795 | } // try
|
---|
796 | cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
|
---|
797 | } // while
|
---|
798 | if ( ! errors.isEmpty() ) {
|
---|
799 | throw errors;
|
---|
800 | } // if
|
---|
801 | }
|
---|
802 |
|
---|
803 | void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ) {
|
---|
804 | SemanticError errors;
|
---|
805 | std::back_insert_iterator< std::list< DeclarationWithType *> > out( outputList );
|
---|
806 | const DeclarationNode *cur = firstNode;
|
---|
807 | while ( cur ) {
|
---|
808 | try {
|
---|
809 | /// if ( DeclarationNode *extr = cur->extractAggregate() ) {
|
---|
810 | /// // handle the case where a structure declaration is contained within an object or type
|
---|
811 | /// // declaration
|
---|
812 | /// Declaration *decl = extr->build();
|
---|
813 | /// if ( decl ) {
|
---|
814 | /// *out++ = decl;
|
---|
815 | /// }
|
---|
816 | /// }
|
---|
817 | Declaration *decl = cur->build();
|
---|
818 | if ( decl ) {
|
---|
819 | if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType *>( decl ) ) {
|
---|
820 | *out++ = dwt;
|
---|
821 | } else if ( StructDecl *agg = dynamic_cast< StructDecl *>( decl ) ) {
|
---|
822 | StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
|
---|
823 | *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
|
---|
824 | delete agg;
|
---|
825 | } else if ( UnionDecl *agg = dynamic_cast< UnionDecl *>( decl ) ) {
|
---|
826 | UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
|
---|
827 | *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
|
---|
828 | } // if
|
---|
829 | } // if
|
---|
830 | } catch( SemanticError &e ) {
|
---|
831 | errors.append( e );
|
---|
832 | } // try
|
---|
833 | cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
|
---|
834 | } // while
|
---|
835 | if ( ! errors.isEmpty() ) {
|
---|
836 | throw errors;
|
---|
837 | } // if
|
---|
838 | }
|
---|
839 |
|
---|
840 | void buildTypeList( const DeclarationNode *firstNode, std::list< Type *> &outputList ) {
|
---|
841 | SemanticError errors;
|
---|
842 | std::back_insert_iterator< std::list< Type *> > out( outputList );
|
---|
843 | const DeclarationNode *cur = firstNode;
|
---|
844 | while ( cur ) {
|
---|
845 | try {
|
---|
846 | *out++ = cur->buildType();
|
---|
847 | } catch( SemanticError &e ) {
|
---|
848 | errors.append( e );
|
---|
849 | } // try
|
---|
850 | cur = dynamic_cast< DeclarationNode *>( cur->get_link() );
|
---|
851 | } // while
|
---|
852 | if ( ! errors.isEmpty() ) {
|
---|
853 | throw errors;
|
---|
854 | } // if
|
---|
855 | }
|
---|
856 |
|
---|
857 | Declaration *DeclarationNode::build() const {
|
---|
858 | if ( type ) {
|
---|
859 | Declaration *newDecl = type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildFuncSpecifier( Inline ), buildFuncSpecifier( Noreturn ), linkage, maybeBuild< Initializer >(initializer) );
|
---|
860 | return newDecl;
|
---|
861 | } // if
|
---|
862 | if ( ! buildFuncSpecifier( Inline ) && ! buildFuncSpecifier( Noreturn ) ) {
|
---|
863 | return new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) );
|
---|
864 | } // if
|
---|
865 | throw SemanticError( "invalid function specifier in declaration of ", this );
|
---|
866 | }
|
---|
867 |
|
---|
868 | Type *DeclarationNode::buildType() const {
|
---|
869 | assert( type );
|
---|
870 |
|
---|
871 | switch ( type->kind ) {
|
---|
872 | case TypeData::Enum:
|
---|
873 | return new EnumInstType( type->buildQualifiers(), type->enumeration->name );
|
---|
874 | case TypeData::Aggregate: {
|
---|
875 | ReferenceToType *ret;
|
---|
876 | switch ( type->aggregate->kind ) {
|
---|
877 | case DeclarationNode::Struct:
|
---|
878 | ret = new StructInstType( type->buildQualifiers(), type->aggregate->name );
|
---|
879 | break;
|
---|
880 | case DeclarationNode::Union:
|
---|
881 | ret = new UnionInstType( type->buildQualifiers(), type->aggregate->name );
|
---|
882 | break;
|
---|
883 | case DeclarationNode::Context:
|
---|
884 | ret = new ContextInstType( type->buildQualifiers(), type->aggregate->name );
|
---|
885 | break;
|
---|
886 | default:
|
---|
887 | assert( false );
|
---|
888 | } // switch
|
---|
889 | buildList( type->aggregate->actuals, ret->get_parameters() );
|
---|
890 | return ret;
|
---|
891 | }
|
---|
892 | case TypeData::Symbolic: {
|
---|
893 | TypeInstType *ret = new TypeInstType( type->buildQualifiers(), type->symbolic->name, false );
|
---|
894 | buildList( type->symbolic->actuals, ret->get_parameters() );
|
---|
895 | return ret;
|
---|
896 | }
|
---|
897 | default:
|
---|
898 | return type->build();
|
---|
899 | } // switch
|
---|
900 | }
|
---|
901 |
|
---|
902 | DeclarationNode::StorageClass DeclarationNode::buildStorageClass() const {
|
---|
903 | DeclarationNode::StorageClass ret = DeclarationNode::NoStorageClass;
|
---|
904 | for ( std::list< DeclarationNode::StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
|
---|
905 | if ( *i == DeclarationNode::Inline || *i == DeclarationNode::Noreturn ) continue; // ignore function specifiers
|
---|
906 | if ( ret != DeclarationNode::NoStorageClass ) { // already have a valid storage class ?
|
---|
907 | throw SemanticError( "invalid combination of storage classes in declaration of ", this );
|
---|
908 | } // if
|
---|
909 | ret = *i;
|
---|
910 | } // for
|
---|
911 | return ret;
|
---|
912 | }
|
---|
913 |
|
---|
914 | bool DeclarationNode::buildFuncSpecifier( DeclarationNode::StorageClass key ) const {
|
---|
915 | std::list< DeclarationNode::StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), key );
|
---|
916 | if ( first == storageClasses.end() ) return false; // not found
|
---|
917 | first = std::find( ++first, storageClasses.end(), key ); // found
|
---|
918 | if ( first == storageClasses.end() ) return true; // not found again
|
---|
919 | throw SemanticError( "duplicate function specifier in declaration of ", this );
|
---|
920 | }
|
---|
921 |
|
---|
922 | // Local Variables: //
|
---|
923 | // tab-width: 4 //
|
---|
924 | // mode: c++ //
|
---|
925 | // compile-command: "make install" //
|
---|
926 | // End: //
|
---|