source: src/Parser/TypeData.cc@ e2f7bc3

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since e2f7bc3 was e612146c, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

third attempt at user-defined literals

  • Property mode set to 100644
File size: 32.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// TypeData.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 15:12:51 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Sep 1 23:13:38 2017
13// Update Count : 569
14//
15
16#include <cassert> // for assert
17#include <ostream> // for operator<<, ostream, basic_ostream
18
19#include "Common/SemanticError.h" // for SemanticError
20#include "Common/utility.h" // for maybeClone, maybeBuild, maybeMoveB...
21#include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode
22#include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, FunctionDecl
23#include "SynTree/Expression.h" // for Expression, ConstantExpr (ptr only)
24#include "SynTree/Initializer.h" // for SingleInit, Initializer (ptr only)
25#include "SynTree/Statement.h" // for CompoundStmt, Statement
26#include "SynTree/Type.h" // for BasicType, Type, Type::ForallList
27#include "TypeData.h"
28
29class Attribute;
30
31using namespace std;
32
33TypeData::TypeData( Kind k ) : kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ {
34 switch ( kind ) {
35 case Unknown:
36 case Pointer:
37 case Reference:
38 case EnumConstant:
39 // nothing else to initialize
40 break;
41 case Basic:
42 // basic = new Basic_t;
43 break;
44 case Array:
45 // array = new Array_t;
46 array.dimension = nullptr;
47 array.isVarLen = false;
48 array.isStatic = false;
49 break;
50 case Function:
51 // function = new Function_t;
52 function.params = nullptr;
53 function.idList = nullptr;
54 function.oldDeclList = nullptr;
55 function.body = nullptr;
56 function.newStyle = false;
57 break;
58 // Enum is an Aggregate, so both structures are initialized together.
59 case Enum:
60 // enumeration = new Enumeration_t;
61 enumeration.name = nullptr;
62 enumeration.constants = nullptr;
63 enumeration.body = false;
64 case Aggregate:
65 // aggregate = new Aggregate_t;
66 aggregate.name = nullptr;
67 aggregate.params = nullptr;
68 aggregate.actuals = nullptr;
69 aggregate.fields = nullptr;
70 aggregate.body = false;
71 break;
72 case AggregateInst:
73 // aggInst = new AggInst_t;
74 aggInst.aggregate = nullptr;
75 aggInst.params = nullptr;
76 aggInst.hoistType = false;;
77 break;
78 case Symbolic:
79 case SymbolicInst:
80 // symbolic = new Symbolic_t;
81 symbolic.name = nullptr;
82 symbolic.params = nullptr;
83 symbolic.actuals = nullptr;
84 symbolic.assertions = nullptr;
85 break;
86 case Tuple:
87 // tuple = new Tuple_t;
88 tuple = nullptr;
89 break;
90 case Typeof:
91 // typeexpr = new Typeof_t;
92 typeexpr = nullptr;
93 break;
94 case Builtin:
95 // builtin = new Builtin_t;
96 break;
97 } // switch
98} // TypeData::TypeData
99
100TypeData::~TypeData() {
101 delete base;
102 delete forall;
103
104 switch ( kind ) {
105 case Unknown:
106 case Pointer:
107 case Reference:
108 case EnumConstant:
109 // nothing to destroy
110 break;
111 case Basic:
112 // delete basic;
113 break;
114 case Array:
115 delete array.dimension;
116 // delete array;
117 break;
118 case Function:
119 delete function.params;
120 delete function.idList;
121 delete function.oldDeclList;
122 delete function.body;
123 // delete function;
124 break;
125 case Aggregate:
126 delete aggregate.name;
127 delete aggregate.params;
128 delete aggregate.actuals;
129 delete aggregate.fields;
130 // delete aggregate;
131 break;
132 case AggregateInst:
133 delete aggInst.aggregate;
134 delete aggInst.params;
135 // delete aggInst;
136 break;
137 case Enum:
138 delete enumeration.name;
139 delete enumeration.constants;
140 // delete enumeration;
141 break;
142 case Symbolic:
143 case SymbolicInst:
144 delete symbolic.name;
145 delete symbolic.params;
146 delete symbolic.actuals;
147 delete symbolic.assertions;
148 // delete symbolic;
149 break;
150 case Tuple:
151 // delete tuple->members;
152 delete tuple;
153 break;
154 case Typeof:
155 // delete typeexpr->expr;
156 delete typeexpr;
157 break;
158 case Builtin:
159 // delete builtin;
160 break;
161 } // switch
162} // TypeData::~TypeData
163
164TypeData * TypeData::clone() const {
165 TypeData * newtype = new TypeData( kind );
166 newtype->qualifiers = qualifiers;
167 newtype->base = maybeClone( base );
168 newtype->forall = maybeClone( forall );
169
170 switch ( kind ) {
171 case Unknown:
172 case EnumConstant:
173 case Pointer:
174 case Reference:
175 // nothing else to copy
176 break;
177 case Basic:
178 newtype->basictype = basictype;
179 newtype->complextype = complextype;
180 newtype->signedness = signedness;
181 newtype->length = length;
182 break;
183 case Array:
184 newtype->array.dimension = maybeClone( array.dimension );
185 newtype->array.isVarLen = array.isVarLen;
186 newtype->array.isStatic = array.isStatic;
187 break;
188 case Function:
189 newtype->function.params = maybeClone( function.params );
190 newtype->function.idList = maybeClone( function.idList );
191 newtype->function.oldDeclList = maybeClone( function.oldDeclList );
192 newtype->function.body = maybeClone( function.body );
193 newtype->function.newStyle = function.newStyle;
194 break;
195 case Aggregate:
196 newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
197 newtype->aggregate.params = maybeClone( aggregate.params );
198 newtype->aggregate.actuals = maybeClone( aggregate.actuals );
199 newtype->aggregate.fields = maybeClone( aggregate.fields );
200 newtype->aggregate.kind = aggregate.kind;
201 newtype->aggregate.body = aggregate.body;
202 newtype->aggregate.tagged = aggregate.tagged;
203 newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
204 break;
205 case AggregateInst:
206 newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
207 newtype->aggInst.params = maybeClone( aggInst.params );
208 newtype->aggInst.hoistType = aggInst.hoistType;
209 break;
210 case Enum:
211 newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
212 newtype->enumeration.constants = maybeClone( enumeration.constants );
213 newtype->enumeration.body = enumeration.body;
214 break;
215 case Symbolic:
216 case SymbolicInst:
217 newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
218 newtype->symbolic.params = maybeClone( symbolic.params );
219 newtype->symbolic.actuals = maybeClone( symbolic.actuals );
220 newtype->symbolic.assertions = maybeClone( symbolic.assertions );
221 newtype->symbolic.isTypedef = symbolic.isTypedef;
222 break;
223 case Tuple:
224 newtype->tuple = maybeClone( tuple );
225 break;
226 case Typeof:
227 newtype->typeexpr = maybeClone( typeexpr );
228 break;
229 case Builtin:
230 assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
231 newtype->builtintype = builtintype;
232 break;
233 } // switch
234 return newtype;
235} // TypeData::clone
236
237void TypeData::print( ostream &os, int indent ) const {
238 for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
239 if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
240 } // for
241
242 if ( forall ) {
243 os << "forall " << endl;
244 forall->printList( os, indent + 4 );
245 } // if
246
247 switch ( kind ) {
248 case Unknown:
249 os << "entity of unknown type ";
250 break;
251 case Pointer:
252 os << "pointer ";
253 if ( base ) {
254 os << "to ";
255 base->print( os, indent );
256 } // if
257 break;
258 case EnumConstant:
259 os << "enumeration constant ";
260 break;
261 case Basic:
262 if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
263 if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
264 assert( basictype != DeclarationNode::NoBasicType );
265 os << DeclarationNode::basicTypeNames[ basictype ] << " ";
266 if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " ";
267 break;
268 case Array:
269 if ( array.isStatic ) {
270 os << "static ";
271 } // if
272 if ( array.dimension ) {
273 os << "array of ";
274 array.dimension->printOneLine( os, indent );
275 } else if ( array.isVarLen ) {
276 os << "variable-length array of ";
277 } else {
278 os << "open array of ";
279 } // if
280 if ( base ) {
281 base->print( os, indent );
282 } // if
283 break;
284 case Function:
285 os << "function" << endl;
286 if ( function.params ) {
287 os << string( indent + 2, ' ' ) << "with parameters " << endl;
288 function.params->printList( os, indent + 4 );
289 } else {
290 os << string( indent + 2, ' ' ) << "with no parameters " << endl;
291 } // if
292 if ( function.idList ) {
293 os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
294 function.idList->printList( os, indent + 4 );
295 } // if
296 if ( function.oldDeclList ) {
297 os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
298 function.oldDeclList->printList( os, indent + 4 );
299 } // if
300 os << string( indent + 2, ' ' ) << "returning ";
301 if ( base ) {
302 base->print( os, indent + 4 );
303 } else {
304 os << "nothing ";
305 } // if
306 os << endl;
307 if ( function.body ) {
308 os << string( indent + 2, ' ' ) << "with body " << endl;
309 function.body->printList( os, indent + 2 );
310 } // if
311 break;
312 case Aggregate:
313 os << DeclarationNode::aggregateNames[ aggregate.kind ] << ' ' << *aggregate.name << endl;
314 if ( aggregate.params ) {
315 os << string( indent + 2, ' ' ) << "with type parameters " << endl;
316 aggregate.params->printList( os, indent + 4 );
317 } // if
318 if ( aggregate.actuals ) {
319 os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl;
320 aggregate.actuals->printList( os, indent + 4 );
321 } // if
322 if ( aggregate.fields ) {
323 os << string( indent + 2, ' ' ) << "with members " << endl;
324 aggregate.fields->printList( os, indent + 4 );
325 } // if
326 if ( aggregate.body ) {
327 os << string( indent + 2, ' ' ) << " with body " << endl;
328 } // if
329 break;
330 case AggregateInst:
331 if ( aggInst.aggregate ) {
332 os << "instance of " ;
333 aggInst.aggregate->print( os, indent );
334 } else {
335 os << "instance of an unspecified aggregate ";
336 } // if
337 if ( aggInst.params ) {
338 os << string( indent + 2, ' ' ) << "with parameters " << endl;
339 aggInst.params->printList( os, indent + 2 );
340 } // if
341 break;
342 case Enum:
343 os << "enumeration ";
344 if ( enumeration.constants ) {
345 os << "with constants" << endl;
346 enumeration.constants->printList( os, indent + 2 );
347 } // if
348 if ( enumeration.body ) {
349 os << string( indent + 2, ' ' ) << " with body " << endl;
350 } // if
351 break;
352 case SymbolicInst:
353 os << "instance of type " << *symbolic.name;
354 if ( symbolic.actuals ) {
355 os << " with parameters" << endl;
356 symbolic.actuals->printList( os, indent + 2 );
357 } // if
358 break;
359 case Symbolic:
360 if ( symbolic.isTypedef ) {
361 os << "typedef definition ";
362 } else {
363 os << "type definition ";
364 } // if
365 if ( symbolic.params ) {
366 os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
367 symbolic.params->printList( os, indent + 2 );
368 } // if
369 if ( symbolic.assertions ) {
370 os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
371 symbolic.assertions->printList( os, indent + 4 );
372 os << string( indent + 2, ' ' );
373 } // if
374 if ( base ) {
375 os << "for ";
376 base->print( os, indent + 2 );
377 } // if
378 break;
379 case Tuple:
380 os << "tuple ";
381 if ( tuple ) {
382 os << "with members " << endl;
383 tuple->printList( os, indent + 2 );
384 } // if
385 break;
386 case Typeof:
387 os << "type-of expression ";
388 if ( typeexpr ) {
389 typeexpr->print( os, indent + 2 );
390 } // if
391 break;
392 case Builtin:
393 os << "gcc builtin type";
394 break;
395 default:
396 os << "internal error: TypeData::print " << kind << endl;
397 assert( false );
398 } // switch
399} // TypeData::print
400
401template< typename ForallList >
402void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
403 buildList( firstNode, outputList );
404 for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i ) {
405 TypeDecl * td = static_cast<TypeDecl *>(*i);
406 if ( td->get_kind() == TypeDecl::Any ) {
407 // add assertion parameters to `type' tyvars in reverse order
408 // add dtor: void ^?{}(T *)
409 FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
410 dtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
411 td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
412
413 // add copy ctor: void ?{}(T *, T)
414 FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
415 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
416 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
417 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
418
419 // add default ctor: void ?{}(T *)
420 FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
421 ctorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
422 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
423
424 // add assignment operator: T * ?=?(T *, T)
425 FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
426 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
427 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
428 assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
429 td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
430 } // if
431 } // for
432}
433
434Type * typebuild( const TypeData * td ) {
435 assert( td );
436 switch ( td->kind ) {
437 case TypeData::Unknown:
438 // fill in implicit int
439 return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
440 case TypeData::Basic:
441 return buildBasicType( td );
442 case TypeData::Pointer:
443 return buildPointer( td );
444 case TypeData::Array:
445 return buildArray( td );
446 case TypeData::Reference:
447 return buildReference( td );
448 case TypeData::Function:
449 return buildFunction( td );
450 case TypeData::AggregateInst:
451 return buildAggInst( td );
452 case TypeData::EnumConstant:
453 // the name gets filled in later -- by SymTab::Validate
454 return new EnumInstType( buildQualifiers( td ), "" );
455 case TypeData::SymbolicInst:
456 return buildSymbolicInst( td );;
457 case TypeData::Tuple:
458 return buildTuple( td );
459 case TypeData::Typeof:
460 return buildTypeof( td );
461 case TypeData::Builtin:
462 if(td->builtintype == DeclarationNode::Zero) {
463 return new ZeroType( noQualifiers );
464 }
465 else if(td->builtintype == DeclarationNode::One) {
466 return new OneType( noQualifiers );
467 }
468 else {
469 return new VarArgsType( buildQualifiers( td ) );
470 }
471 case TypeData::Symbolic:
472 case TypeData::Enum:
473 case TypeData::Aggregate:
474 assert( false );
475 } // switch
476 return nullptr;
477} // typebuild
478
479TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
480 TypeData * ret = nullptr;
481
482 switch ( td->kind ) {
483 case TypeData::Aggregate:
484 if ( ! toplevel && td->aggregate.fields ) {
485 ret = td->clone();
486 } // if
487 break;
488 case TypeData::Enum:
489 if ( ! toplevel && td->enumeration.constants ) {
490 ret = td->clone();
491 } // if
492 break;
493 case TypeData::AggregateInst:
494 if ( td->aggInst.aggregate ) {
495 ret = typeextractAggregate( td->aggInst.aggregate, false );
496 } // if
497 break;
498 default:
499 if ( td->base ) {
500 ret = typeextractAggregate( td->base, false );
501 } // if
502 } // switch
503 return ret;
504} // typeextractAggregate
505
506Type::Qualifiers buildQualifiers( const TypeData * td ) {
507 return td->qualifiers;
508} // buildQualifiers
509
510Type * buildBasicType( const TypeData * td ) {
511 BasicType::Kind ret;
512
513 switch ( td->basictype ) {
514 case DeclarationNode::Void:
515 if ( td->signedness != DeclarationNode::NoSignedness && td->length != DeclarationNode::NoLength ) {
516 throw SemanticError( "invalid type specifier \"void\" in type: ", td );
517 } // if
518
519 return new VoidType( buildQualifiers( td ) );
520 break;
521
522 case DeclarationNode::Bool:
523 if ( td->signedness != DeclarationNode::NoSignedness ) {
524 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessNames[ td->signedness ] + " in type: ", td );
525 } // if
526 if ( td->length != DeclarationNode::NoLength ) {
527 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
528 } // if
529
530 ret = BasicType::Bool;
531 break;
532
533 case DeclarationNode::Char:
534 // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
535 // character types. The implementation shall define char to have the same range, representation, and behavior as
536 // either signed char or unsigned char.
537 static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
538
539 if ( td->length != DeclarationNode::NoLength ) {
540 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
541 } // if
542
543 ret = chartype[ td->signedness ];
544 break;
545
546 case DeclarationNode::Int:
547 static BasicType::Kind inttype[2][4] = {
548 { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
549 { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
550 };
551
552 Integral: ;
553 if ( td->signedness == DeclarationNode::NoSignedness ) {
554 const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
555 } // if
556 ret = inttype[ td->signedness ][ td->length ];
557 break;
558
559 case DeclarationNode::Float:
560 case DeclarationNode::Double:
561 case DeclarationNode::LongDouble: // not set until below
562 static BasicType::Kind floattype[3][3] = {
563 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
564 { BasicType::FloatImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary },
565 { BasicType::Float, BasicType::Double, BasicType::LongDouble },
566 };
567
568 FloatingPoint: ;
569 if ( td->signedness != DeclarationNode::NoSignedness ) {
570 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessNames[ td->signedness ] + " in type: ", td );
571 } // if
572 if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
573 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
574 } // if
575 if ( td->basictype == DeclarationNode::Float && td->length == DeclarationNode::Long ) {
576 throw SemanticError( "invalid type specifier \"long\" in type: ", td );
577 } // if
578 if ( td->length == DeclarationNode::Long ) {
579 const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
580 } // if
581
582 ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
583 break;
584
585 case DeclarationNode::NoBasicType:
586 // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
587 if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
588 const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
589 goto FloatingPoint;
590 } // if
591
592 const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
593 goto Integral;
594 default:
595 assert(false);
596 return nullptr;
597 } // switch
598
599 BasicType * bt = new BasicType( buildQualifiers( td ), ret );
600 buildForall( td->forall, bt->get_forall() );
601 return bt;
602} // buildBasicType
603
604PointerType * buildPointer( const TypeData * td ) {
605 PointerType * pt;
606 if ( td->base ) {
607 pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
608 } else {
609 pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
610 } // if
611 buildForall( td->forall, pt->get_forall() );
612 return pt;
613} // buildPointer
614
615ArrayType * buildArray( const TypeData * td ) {
616 ArrayType * at;
617 if ( td->base ) {
618 at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
619 td->array.isVarLen, td->array.isStatic );
620 } else {
621 at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
622 maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
623 } // if
624 buildForall( td->forall, at->get_forall() );
625 return at;
626} // buildArray
627
628ReferenceType * buildReference( const TypeData * td ) {
629 ReferenceType * rt;
630 if ( td->base ) {
631 rt = new ReferenceType( buildQualifiers( td ), typebuild( td->base ) );
632 } else {
633 rt = new ReferenceType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
634 } // if
635 buildForall( td->forall, rt->get_forall() );
636 return rt;
637} // buildReference
638
639AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
640 assert( td->kind == TypeData::Aggregate );
641 AggregateDecl * at;
642 switch ( td->aggregate.kind ) {
643 case DeclarationNode::Struct:
644 case DeclarationNode::Coroutine:
645 case DeclarationNode::Monitor:
646 case DeclarationNode::Thread:
647 at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
648 buildForall( td->aggregate.params, at->get_parameters() );
649 break;
650 case DeclarationNode::Union:
651 at = new UnionDecl( *td->aggregate.name, attributes, linkage );
652 buildForall( td->aggregate.params, at->get_parameters() );
653 break;
654 case DeclarationNode::Trait:
655 at = new TraitDecl( *td->aggregate.name, attributes, linkage );
656 buildList( td->aggregate.params, at->get_parameters() );
657 break;
658 default:
659 assert( false );
660 } // switch
661
662 buildList( td->aggregate.fields, at->get_members() );
663 at->set_body( td->aggregate.body );
664
665 return at;
666} // buildAggregate
667
668ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
669 switch ( type->kind ) {
670 case TypeData::Enum: {
671 if ( type->enumeration.body ) {
672 EnumDecl * typedecl = buildEnum( type, attributes, linkage );
673 return new EnumInstType( buildQualifiers( type ), typedecl );
674 } else {
675 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
676 } // if
677 }
678 case TypeData::Aggregate: {
679 ReferenceToType * ret;
680 if ( type->aggregate.body ) {
681 AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
682 switch ( type->aggregate.kind ) {
683 case DeclarationNode::Struct:
684 case DeclarationNode::Coroutine:
685 case DeclarationNode::Monitor:
686 case DeclarationNode::Thread:
687 ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
688 break;
689 case DeclarationNode::Union:
690 ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
691 break;
692 case DeclarationNode::Trait:
693 assert( false );
694 //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
695 break;
696 default:
697 assert( false );
698 } // switch
699 } else {
700 switch ( type->aggregate.kind ) {
701 case DeclarationNode::Struct:
702 case DeclarationNode::Coroutine:
703 case DeclarationNode::Monitor:
704 case DeclarationNode::Thread:
705 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
706 break;
707 case DeclarationNode::Union:
708 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
709 break;
710 case DeclarationNode::Trait:
711 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
712 break;
713 default:
714 assert( false );
715 } // switch
716 } // if
717 return ret;
718 }
719 default:
720 assert( false );
721 } // switch
722} // buildAggInst
723
724ReferenceToType * buildAggInst( const TypeData * td ) {
725 assert( td->kind == TypeData::AggregateInst );
726
727 // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
728 ReferenceToType * ret = nullptr;
729 TypeData * type = td->aggInst.aggregate;
730 switch ( type->kind ) {
731 case TypeData::Enum: {
732 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
733 }
734 case TypeData::Aggregate: {
735 switch ( type->aggregate.kind ) {
736 case DeclarationNode::Struct:
737 case DeclarationNode::Coroutine:
738 case DeclarationNode::Monitor:
739 case DeclarationNode::Thread:
740 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
741 break;
742 case DeclarationNode::Union:
743 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
744 break;
745 case DeclarationNode::Trait:
746 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
747 break;
748 default:
749 assert( false );
750 } // switch
751 }
752 break;
753 default:
754 assert( false );
755 } // switch
756
757 ret->set_hoistType( td->aggInst.hoistType );
758 buildList( td->aggInst.params, ret->get_parameters() );
759 buildForall( td->forall, ret->get_forall() );
760 return ret;
761} // buildAggInst
762
763NamedTypeDecl * buildSymbolic( const TypeData * td, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
764 assert( td->kind == TypeData::Symbolic );
765 NamedTypeDecl * ret;
766 assert( td->base );
767 if ( td->symbolic.isTypedef ) {
768 ret = new TypedefDecl( name, scs, typebuild( td->base ), linkage );
769 } else {
770 ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Any );
771 } // if
772 buildList( td->symbolic.params, ret->get_parameters() );
773 buildList( td->symbolic.assertions, ret->get_assertions() );
774 return ret;
775} // buildSymbolic
776
777EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
778 assert( td->kind == TypeData::Enum );
779 EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage );
780 buildList( td->enumeration.constants, ret->get_members() );
781 list< Declaration * >::iterator members = ret->get_members().begin();
782 for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
783 if ( cur->has_enumeratorValue() ) {
784 ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
785 member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
786 } // if
787 } // for
788 ret->set_body( td->enumeration.body );
789 return ret;
790} // buildEnum
791
792TypeInstType * buildSymbolicInst( const TypeData * td ) {
793 assert( td->kind == TypeData::SymbolicInst );
794 TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
795 buildList( td->symbolic.actuals, ret->get_parameters() );
796 buildForall( td->forall, ret->get_forall() );
797 return ret;
798} // buildSymbolicInst
799
800TupleType * buildTuple( const TypeData * td ) {
801 assert( td->kind == TypeData::Tuple );
802 std::list< Type * > types;
803 buildTypeList( td->tuple, types );
804 TupleType * ret = new TupleType( buildQualifiers( td ), types );
805 buildForall( td->forall, ret->get_forall() );
806 return ret;
807} // buildTuple
808
809TypeofType * buildTypeof( const TypeData * td ) {
810 assert( td->kind == TypeData::Typeof );
811 assert( td->typeexpr );
812 // assert( td->typeexpr->expr );
813 return new TypeofType( buildQualifiers( td ), td->typeexpr->build() );
814} // buildTypeof
815
816Declaration * buildDecl( const TypeData * td, const string &name, Type::StorageClasses scs, Expression * bitfieldWidth, Type::FuncSpecifiers funcSpec, LinkageSpec::Spec linkage, Expression *asmName, Initializer * init, std::list< Attribute * > attributes ) {
817 if ( td->kind == TypeData::Function ) {
818 if ( td->function.idList ) { // KR function ?
819 buildKRFunction( td->function ); // transform into C11 function
820 } // if
821
822 FunctionDecl * decl;
823 Statement * stmt = maybeBuild<Statement>( td->function.body );
824 CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
825 decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
826 return decl->set_asmName( asmName );
827 } else if ( td->kind == TypeData::Aggregate ) {
828 return buildAggregate( td, attributes, linkage );
829 } else if ( td->kind == TypeData::Enum ) {
830 return buildEnum( td, attributes, linkage );
831 } else if ( td->kind == TypeData::Symbolic ) {
832 return buildSymbolic( td, name, scs, linkage );
833 } else {
834 return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
835 } // if
836 return nullptr;
837} // buildDecl
838
839FunctionType * buildFunction( const TypeData * td ) {
840 assert( td->kind == TypeData::Function );
841 bool hasEllipsis = td->function.params ? td->function.params->get_hasEllipsis() : true;
842 if ( ! td->function.params ) hasEllipsis = ! td->function.newStyle;
843 FunctionType * ft = new FunctionType( buildQualifiers( td ), hasEllipsis );
844 buildList( td->function.params, ft->get_parameters() );
845 buildForall( td->forall, ft->get_forall() );
846 if ( td->base ) {
847 switch ( td->base->kind ) {
848 case TypeData::Tuple:
849 buildList( td->base->tuple, ft->get_returnVals() );
850 break;
851 default:
852 ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
853 } // switch
854 } else {
855 ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
856 } // if
857 return ft;
858} // buildFunction
859
860// Transform KR routine declarations into C99 routine declarations:
861//
862// rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {}
863//
864// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
865// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
866// extra names are disallowed.
867//
868// Note, there is no KR routine-prototype syntax:
869//
870// rtn( a, b, c ) int a, c; double b; // invalid KR prototype
871// rtn(); // valid KR prototype
872
873void buildKRFunction( const TypeData::Function_t & function ) {
874 assert( ! function.params );
875 // loop over declaration first as it is easier to spot errors
876 for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
877 // scan ALL parameter names for each declaration name to check for duplicates
878 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
879 if ( *decl->name == *param->name ) {
880 // type set => parameter name already transformed by a declaration names so there is a duplicate
881 // declaration name attempting a second transformation
882 if ( param->type ) throw SemanticError( string( "duplicate declaration name " ) + *param->name );
883 // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
884 // parameter name attempting a second transformation
885 if ( ! decl->type ) throw SemanticError( string( "duplicate parameter name " ) + *param->name );
886 param->type = decl->type; // set copy declaration type to parameter type
887 decl->type = nullptr; // reset declaration type
888 param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
889 } // if
890 } // for
891 // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
892 if ( decl->type ) throw SemanticError( string( "missing name in parameter list " ) + *decl->name );
893 } // for
894
895 // Parameter names without a declaration default to type int:
896 //
897 // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
898
899 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
900 if ( ! param->type ) { // generate type int for empty parameter type
901 param->type = new TypeData( TypeData::Basic );
902 param->type->basictype = DeclarationNode::Int;
903 } // if
904 } // for
905
906 function.params = function.idList; // newly modified idList becomes parameters
907 function.idList = nullptr; // idList now empty
908 delete function.oldDeclList; // deletes entire list
909 function.oldDeclList = nullptr; // reset
910} // buildKRFunction
911
912// Local Variables: //
913// tab-width: 4 //
914// mode: c++ //
915// compile-command: "make install" //
916// End: //
Note: See TracBrowser for help on using the repository browser.