source: src/Parser/TypeData.cc @ e496303

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 e496303 was e496303, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change type of type qualifiers to bit fields and fix uninitialized fields in aggregate

  • 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 : Tue Mar 14 15:01:44 2017
13// Update Count     : 548
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->typeQualifiers = typeQualifiers;
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 < DeclarationNode::NoTypeQualifier; i += 1 ) {
229                if ( typeQualifiers[i] ) os << DeclarationNode::typeQualifierNames[ 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( "", DeclarationNode::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( "^?{}", DeclarationNode::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( "", DeclarationNode::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( "", DeclarationNode::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
407                        td->get_assertions().push_front( new FunctionDecl( "?{}", DeclarationNode::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( "", DeclarationNode::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( "?{}", DeclarationNode::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( "", DeclarationNode::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( "", DeclarationNode::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
418                        assignType->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
419                        td->get_assertions().push_front( new FunctionDecl( "?=?", DeclarationNode::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        Type::Qualifiers q;
496        q.isConst = td->typeQualifiers.is_const;
497        q.isVolatile = td->typeQualifiers.is_volatile;
498        q.isRestrict = td->typeQualifiers.is_restrict;
499        q.isLvalue = td->typeQualifiers.is_lvalue;
500        q.isAtomic = td->typeQualifiers.is_atomic;
501        return q;
502} // buildQualifiers
503
504Type * buildBasicType( const TypeData * td ) {
505        BasicType::Kind ret;
506
507        switch ( td->basictype ) {
508          case DeclarationNode::Void:
509                if ( td->signedness != DeclarationNode::NoSignedness && td->length != DeclarationNode::NoLength ) {
510                        throw SemanticError( "invalid type specifier \"void\" in type: ", td );
511                } // if
512
513                return new VoidType( buildQualifiers( td ) );
514                break;
515
516          case DeclarationNode::Bool:
517                if ( td->signedness != DeclarationNode::NoSignedness ) {
518                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessNames[ td->signedness ] + " in type: ", td );
519                } // if
520                if ( td->length != DeclarationNode::NoLength ) {
521                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
522                } // if
523
524                ret = BasicType::Bool;
525                break;
526
527          case DeclarationNode::Char:
528                // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
529                // character types. The implementation shall define char to have the same range, representation, and behavior as
530                // either signed char or unsigned char.
531                static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
532
533                if ( td->length != DeclarationNode::NoLength ) {
534                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
535                } // if
536
537                ret = chartype[ td->signedness ];
538                break;
539
540          case DeclarationNode::Int:
541                static BasicType::Kind inttype[2][4] = {
542                        { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
543                        { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
544                };
545
546          Integral: ;
547                if ( td->signedness == DeclarationNode::NoSignedness ) {
548                        const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
549                } // if
550                ret = inttype[ td->signedness ][ td->length ];
551                break;
552
553          case DeclarationNode::Float:
554          case DeclarationNode::Double:
555          case DeclarationNode::LongDouble:                                     // not set until below
556                static BasicType::Kind floattype[3][3] = {
557                        { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
558                        { BasicType::FloatImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary },
559                        { BasicType::Float, BasicType::Double, BasicType::LongDouble },
560                };
561
562          FloatingPoint: ;
563                if ( td->signedness != DeclarationNode::NoSignedness ) {
564                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessNames[ td->signedness ] + " in type: ", td );
565                } // if
566                if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
567                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
568                } // if
569                if ( td->basictype == DeclarationNode::Float && td->length == DeclarationNode::Long ) {
570                        throw SemanticError( "invalid type specifier \"long\" in type: ", td );
571                } // if
572                if ( td->length == DeclarationNode::Long ) {
573                        const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
574                } // if
575
576                ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
577                break;
578
579          case DeclarationNode::NoBasicType:
580                // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
581                if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
582                        const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
583                        goto FloatingPoint;
584                } // if
585
586                const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
587                goto Integral;
588          default:
589                assert(false);
590                return nullptr;
591        } // switch
592
593        BasicType * bt = new BasicType( buildQualifiers( td ), ret );
594        buildForall( td->forall, bt->get_forall() );
595        return bt;
596} // buildBasicType
597
598PointerType * buildPointer( const TypeData * td ) {
599        PointerType * pt;
600        if ( td->base ) {
601                pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
602        } else {
603                pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
604        } // if
605        buildForall( td->forall, pt->get_forall() );
606        return pt;
607} // buildPointer
608
609ArrayType * buildArray( const TypeData * td ) {
610        ArrayType * at;
611        if ( td->base ) {
612                at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
613                                                        td->array.isVarLen, td->array.isStatic );
614        } else {
615                at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
616                                                        maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
617        } // if
618        buildForall( td->forall, at->get_forall() );
619        return at;
620} // buildPointer
621
622AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes ) {
623        assert( td->kind == TypeData::Aggregate );
624        AggregateDecl * at;
625        switch ( td->aggregate.kind ) {
626          case DeclarationNode::Struct:
627                at = new StructDecl( *td->aggregate.name, attributes );
628                buildForall( td->aggregate.params, at->get_parameters() );
629                break;
630          case DeclarationNode::Union:
631                at = new UnionDecl( *td->aggregate.name, attributes );
632                buildForall( td->aggregate.params, at->get_parameters() );
633                break;
634          case DeclarationNode::Trait:
635                at = new TraitDecl( *td->aggregate.name, attributes );
636                buildList( td->aggregate.params, at->get_parameters() );
637                break;
638          default:
639                assert( false );
640        } // switch
641
642        buildList( td->aggregate.fields, at->get_members() );
643        at->set_body( td->aggregate.body );
644
645        return at;
646} // buildAggregate
647
648ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes ) {
649        switch ( type->kind ) {
650          case TypeData::Enum: {
651                  if ( type->enumeration.body ) {
652                          EnumDecl * typedecl = buildEnum( type, attributes );
653                          return new EnumInstType( buildQualifiers( type ), typedecl );
654                  } else {
655                          return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
656                  } // if
657          }
658          case TypeData::Aggregate: {
659                  ReferenceToType * ret;
660                  if ( type->aggregate.body ) {
661                          AggregateDecl * typedecl = buildAggregate( type, attributes );
662                          switch ( type->aggregate.kind ) {
663                                case DeclarationNode::Struct:
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                                  ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
680                                  break;
681                                case DeclarationNode::Union:
682                                  ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
683                                  break;
684                                case DeclarationNode::Trait:
685                                  ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
686                                  break;
687                                default:
688                                  assert( false );
689                          } // switch
690                  } // if
691                  return ret;
692          }
693          default:
694                assert( false );
695        } // switch
696} // buildAggInst
697
698ReferenceToType * buildAggInst( const TypeData * td ) {
699        assert( td->kind == TypeData::AggregateInst );
700
701        // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
702        ReferenceToType * ret = nullptr;
703        TypeData * type = td->aggInst.aggregate;
704        switch ( type->kind ) {
705          case TypeData::Enum: {
706                  return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
707          }
708          case TypeData::Aggregate: {
709                  switch ( type->aggregate.kind ) {
710                        case DeclarationNode::Struct:
711                          ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
712                          break;
713                        case DeclarationNode::Union:
714                          ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
715                          break;
716                        case DeclarationNode::Trait:
717                          ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
718                          break;
719                        default:
720                          assert( false );
721                  } // switch
722          }
723          break;
724          default:
725                assert( false );
726        } // switch
727
728        ret->set_hoistType( td->aggInst.hoistType );
729        buildList( td->aggInst.params, ret->get_parameters() );
730        buildForall( td->forall, ret->get_forall() );
731        return ret;
732} // buildAggInst
733
734NamedTypeDecl * buildSymbolic( const TypeData * td, const string & name, DeclarationNode::StorageClasses scs ) {
735        assert( td->kind == TypeData::Symbolic );
736        NamedTypeDecl * ret;
737        assert( td->base );
738        if ( td->symbolic.isTypedef ) {
739                ret = new TypedefDecl( name, scs, typebuild( td->base ) );
740        } else {
741                ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Any );
742        } // if
743        buildList( td->symbolic.params, ret->get_parameters() );
744        buildList( td->symbolic.assertions, ret->get_assertions() );
745        return ret;
746} // buildSymbolic
747
748EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes ) {
749        assert( td->kind == TypeData::Enum );
750        EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes );
751        buildList( td->enumeration.constants, ret->get_members() );
752        list< Declaration * >::iterator members = ret->get_members().begin();
753        for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
754                if ( cur->has_enumeratorValue() ) {
755                        ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
756                        member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ), list< Expression * >() ) );
757                } // if
758        } // for
759        ret->set_body( td->enumeration.body );
760        return ret;
761} // buildEnum
762
763TypeInstType * buildSymbolicInst( const TypeData * td ) {
764        assert( td->kind == TypeData::SymbolicInst );
765        TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
766        buildList( td->symbolic.actuals, ret->get_parameters() );
767        buildForall( td->forall, ret->get_forall() );
768        return ret;
769} // buildSymbolicInst
770
771TupleType * buildTuple( const TypeData * td ) {
772        assert( td->kind == TypeData::Tuple );
773        TupleType * ret = new TupleType( buildQualifiers( td ) );
774        buildTypeList( td->tuple, ret->get_types() );
775        buildForall( td->forall, ret->get_forall() );
776        return ret;
777} // buildTuple
778
779TypeofType * buildTypeof( const TypeData * td ) {
780        assert( td->kind == TypeData::Typeof );
781        assert( td->typeexpr );
782        // assert( td->typeexpr->expr );
783        return new TypeofType( buildQualifiers( td ), td->typeexpr->build() );
784} // buildTypeof
785
786Declaration * buildDecl( const TypeData * td, const string &name, DeclarationNode::StorageClasses scs, Expression * bitfieldWidth, DeclarationNode::FuncSpecifiers funcSpec, LinkageSpec::Spec linkage, ConstantExpr *asmName, Initializer * init, std::list< Attribute * > attributes ) {
787        if ( td->kind == TypeData::Function ) {
788                if ( td->function.idList ) {                                    // KR function ?
789                        buildKRFunction( td->function );                        // transform into C11 function
790                } // if
791
792                FunctionDecl * decl;
793                Statement * stmt = maybeBuild<Statement>( td->function.body );
794                CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
795                decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
796                return decl->set_asmName( asmName );
797        } else if ( td->kind == TypeData::Aggregate ) {
798                return buildAggregate( td, attributes );
799        } else if ( td->kind == TypeData::Enum ) {
800                return buildEnum( td, attributes );
801        } else if ( td->kind == TypeData::Symbolic ) {
802                return buildSymbolic( td, name, scs );
803        } else {
804                return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
805        } // if
806        return nullptr;
807} // buildDecl
808
809FunctionType * buildFunction( const TypeData * td ) {
810        assert( td->kind == TypeData::Function );
811        bool hasEllipsis = td->function.params ? td->function.params->get_hasEllipsis() : true;
812        if ( ! td->function.params ) hasEllipsis = ! td->function.newStyle;
813        FunctionType * ft = new FunctionType( buildQualifiers( td ), hasEllipsis );
814        buildList( td->function.params, ft->get_parameters() );
815        buildForall( td->forall, ft->get_forall() );
816        if ( td->base ) {
817                switch ( td->base->kind ) {
818                  case TypeData::Tuple:
819                        buildList( td->base->tuple, ft->get_returnVals() );
820                        break;
821                  default:
822                        ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", DeclarationNode::StorageClasses(), nullptr, DeclarationNode::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
823                } // switch
824        } else {
825                ft->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
826        } // if
827        return ft;
828} // buildFunction
829
830// Transform KR routine declarations into C99 routine declarations:
831//
832//    rtn( a, b, c ) int a, c; double b {}  =>  int rtn( int a, double c, int b ) {}
833//
834// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
835// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
836// extra names are disallowed.
837//
838// Note, there is no KR routine-prototype syntax:
839//
840//    rtn( a, b, c ) int a, c; double b; // invalid KR prototype
841//    rtn(); // valid KR prototype
842
843void buildKRFunction( const TypeData::Function_t & function ) {
844        assert( ! function.params );
845        // loop over declaration first as it is easier to spot errors
846        for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
847                // scan ALL parameter names for each declaration name to check for duplicates
848                for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
849                        if ( *decl->name == *param->name ) {
850                                // type set => parameter name already transformed by a declaration names so there is a duplicate
851                                // declaration name attempting a second transformation
852                                if ( param->type ) throw SemanticError( string( "duplicate declaration name " ) + *param->name );
853                                // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
854                                // parameter name attempting a second transformation
855                                if ( ! decl->type ) throw SemanticError( string( "duplicate parameter name " ) + *param->name );
856                                param->type = decl->type;                               // set copy declaration type to parameter type
857                                decl->type = nullptr;                                   // reset declaration type
858                                param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
859                        } // if
860                } // for
861                // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
862                if ( decl->type ) throw SemanticError( string( "missing name in parameter list " ) + *decl->name );
863        } // for
864
865        // Parameter names without a declaration default to type int:
866        //
867        //    rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
868
869        for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
870                if ( ! param->type ) {                                                  // generate type int for empty parameter type
871                        param->type = new TypeData( TypeData::Basic );
872                        param->type->basictype = DeclarationNode::Int;
873                } // if
874        } // for
875
876        function.params = function.idList;                                      // newly modified idList becomes parameters
877        function.idList = nullptr;                                                      // idList now empty
878        delete function.oldDeclList;                                            // deletes entire list
879        function.oldDeclList = nullptr;                                         // reset
880} // buildKRFunction
881
882// Local Variables: //
883// tab-width: 4 //
884// mode: c++ //
885// compile-command: "make install" //
886// End: //
Note: See TracBrowser for help on using the repository browser.