source: src/Parser/TypeData.cc @ 16c95e3

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 16c95e3 was 409433da, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

support coroutine, monitor, thread as kind of structure

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