source: src/Parser/TypeData.cc @ 512d937c

ADTast-experimental
Last change on this file since 512d937c was 702e826, checked in by Andrew Beach <ajbeach@…>, 16 months ago

Pre-translation pass on the parser. Entirely code readability improvements, no behaviour (on a larger scale) should be effected.

  • Property mode set to 100644
File size: 36.5 KB
RevLine 
[b87a5ed]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//
[f1e012b]7// TypeData.cc --
[b87a5ed]8//
9// Author           : Rodolfo G. Esteves
10// Created On       : Sat May 16 15:12:51 2015
[66406f3]11// Last Modified By : Peter A. Buhr
[2d37a788]12// Last Modified On : Sun Feb 19 11:00:46 2023
13// Update Count     : 679
[b87a5ed]14//
15
[d180746]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
[51b7345]27#include "TypeData.h"
[d180746]28
29class Attribute;
30
[2298f728]31using namespace std;
[51b7345]32
[0b0f1dd]33TypeData::TypeData( Kind k ) : location( yylloc ), kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ {
[b87a5ed]34        switch ( kind ) {
[702e826]35        case Unknown:
36        case Pointer:
37        case Reference:
38        case EnumConstant:
39        case GlobalScope:
40        case Basic:
41                // No unique data to initialize.
42                break;
43        case Array:
[2298f728]44                array.dimension = nullptr;
[8f6f47d7]45                array.isVarLen = false;
46                array.isStatic = false;
[b87a5ed]47                break;
[702e826]48        case Function:
[2298f728]49                function.params = nullptr;
50                function.idList = nullptr;
51                function.oldDeclList = nullptr;
52                function.body = nullptr;
[84276ba]53                function.withExprs = nullptr;
[b87a5ed]54                break;
[702e826]55        case Enum:
[e496303]56                enumeration.name = nullptr;
57                enumeration.constants = nullptr;
58                enumeration.body = false;
[3d7e53b]59                enumeration.anon = false;
[6926a6d]60                break;
[702e826]61        case Aggregate:
[312029a]62                aggregate.kind = AggregateDecl::NoAggregate;
[2298f728]63                aggregate.name = nullptr;
64                aggregate.params = nullptr;
65                aggregate.actuals = nullptr;
66                aggregate.fields = nullptr;
[4a9ccc3]67                aggregate.body = false;
[d15a45d]68                aggregate.tagged = false;
69                aggregate.parent = nullptr;
[3d7e53b]70                aggregate.anon = false;
[b87a5ed]71                break;
[702e826]72        case AggregateInst:
[2298f728]73                aggInst.aggregate = nullptr;
74                aggInst.params = nullptr;
[8f91c9ae]75                aggInst.hoistType = false;
[b87a5ed]76                break;
[702e826]77        case Symbolic:
78        case SymbolicInst:
[2298f728]79                symbolic.name = nullptr;
80                symbolic.params = nullptr;
81                symbolic.actuals = nullptr;
82                symbolic.assertions = nullptr;
[b87a5ed]83                break;
[702e826]84        case Tuple:
[8f6f47d7]85                tuple = nullptr;
[b87a5ed]86                break;
[702e826]87        case Typeof:
88        case Basetypeof:
[8f6f47d7]89                typeexpr = nullptr;
[b87a5ed]90                break;
[702e826]91        case Builtin:
92        case Vtable:
93                // No unique data to initialize.
[93bbbc4]94                break;
[702e826]95        case Qualified:
[c194661]96                qualified.parent = nullptr;
97                qualified.child = nullptr;
[b87a5ed]98                break;
[68cd1ce]99        } // switch
[413ad05]100} // TypeData::TypeData
[51b7345]101
[201aeb9]102
[c8ffe20b]103TypeData::~TypeData() {
[b87a5ed]104        delete base;
105        delete forall;
106
107        switch ( kind ) {
[702e826]108        case Unknown:
109        case Pointer:
110        case Reference:
111        case EnumConstant:
112        case GlobalScope:
113                // No unique data to deconstruct.
[b87a5ed]114                break;
[702e826]115        case Basic:
[b87a5ed]116                break;
[702e826]117        case Array:
[8f6f47d7]118                delete array.dimension;
[b87a5ed]119                break;
[702e826]120        case Function:
[8f6f47d7]121                delete function.params;
122                delete function.idList;
123                delete function.oldDeclList;
124                delete function.body;
[84276ba]125                delete function.withExprs;
[b87a5ed]126                break;
[702e826]127        case Aggregate:
[2298f728]128                delete aggregate.name;
[8f6f47d7]129                delete aggregate.params;
130                delete aggregate.actuals;
131                delete aggregate.fields;
[b87a5ed]132                break;
[702e826]133        case AggregateInst:
[8f6f47d7]134                delete aggInst.aggregate;
135                delete aggInst.params;
[b87a5ed]136                break;
[702e826]137        case Enum:
[2298f728]138                delete enumeration.name;
[8f6f47d7]139                delete enumeration.constants;
[b87a5ed]140                break;
[702e826]141        case Symbolic:
142        case SymbolicInst:
[2298f728]143                delete symbolic.name;
[8f6f47d7]144                delete symbolic.params;
145                delete symbolic.actuals;
146                delete symbolic.assertions;
[b87a5ed]147                break;
[702e826]148        case Tuple:
[b87a5ed]149                delete tuple;
150                break;
[702e826]151        case Typeof:
152        case Basetypeof:
[b87a5ed]153                delete typeexpr;
154                break;
[702e826]155        case Vtable:
156        case Builtin:
157                // No unique data to deconstruct.
[93bbbc4]158                break;
[702e826]159        case Qualified:
[c194661]160                delete qualified.parent;
161                delete qualified.child;
[702e826]162                break;
[68cd1ce]163        } // switch
[413ad05]164} // TypeData::~TypeData
[51b7345]165
[201aeb9]166
[413ad05]167TypeData * TypeData::clone() const {
168        TypeData * newtype = new TypeData( kind );
[738e304]169        newtype->qualifiers = qualifiers;
[b87a5ed]170        newtype->base = maybeClone( base );
171        newtype->forall = maybeClone( forall );
172
173        switch ( kind ) {
[702e826]174        case Unknown:
175        case EnumConstant:
176        case Pointer:
177        case Reference:
178        case GlobalScope:
[b87a5ed]179                // nothing else to copy
180                break;
[702e826]181        case Basic:
[5b639ee]182                newtype->basictype = basictype;
183                newtype->complextype = complextype;
184                newtype->signedness = signedness;
185                newtype->length = length;
[b87a5ed]186                break;
[702e826]187        case Array:
[8f6f47d7]188                newtype->array.dimension = maybeClone( array.dimension );
189                newtype->array.isVarLen = array.isVarLen;
190                newtype->array.isStatic = array.isStatic;
[b87a5ed]191                break;
[702e826]192        case Function:
[8f6f47d7]193                newtype->function.params = maybeClone( function.params );
194                newtype->function.idList = maybeClone( function.idList );
195                newtype->function.oldDeclList = maybeClone( function.oldDeclList );
196                newtype->function.body = maybeClone( function.body );
[84276ba]197                newtype->function.withExprs = maybeClone( function.withExprs );
[b87a5ed]198                break;
[702e826]199        case Aggregate:
[d15a45d]200                newtype->aggregate.kind = aggregate.kind;
[2298f728]201                newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
[8f6f47d7]202                newtype->aggregate.params = maybeClone( aggregate.params );
203                newtype->aggregate.actuals = maybeClone( aggregate.actuals );
204                newtype->aggregate.fields = maybeClone( aggregate.fields );
205                newtype->aggregate.body = aggregate.body;
[3d7e53b]206                newtype->aggregate.anon = aggregate.anon;
[6ea87486]207                newtype->aggregate.tagged = aggregate.tagged;
208                newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
[b87a5ed]209                break;
[702e826]210        case AggregateInst:
[8f6f47d7]211                newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
212                newtype->aggInst.params = maybeClone( aggInst.params );
[43c89a7]213                newtype->aggInst.hoistType = aggInst.hoistType;
[b87a5ed]214                break;
[702e826]215        case Enum:
[2298f728]216                newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
[8f6f47d7]217                newtype->enumeration.constants = maybeClone( enumeration.constants );
[ca1a547]218                newtype->enumeration.body = enumeration.body;
[3d7e53b]219                newtype->enumeration.anon = enumeration.anon;
[b87a5ed]220                break;
[702e826]221        case Symbolic:
222        case SymbolicInst:
[2298f728]223                newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
[8f6f47d7]224                newtype->symbolic.params = maybeClone( symbolic.params );
225                newtype->symbolic.actuals = maybeClone( symbolic.actuals );
226                newtype->symbolic.assertions = maybeClone( symbolic.assertions );
227                newtype->symbolic.isTypedef = symbolic.isTypedef;
[b87a5ed]228                break;
[702e826]229        case Tuple:
[8f6f47d7]230                newtype->tuple = maybeClone( tuple );
[b87a5ed]231                break;
[702e826]232        case Typeof:
233        case Basetypeof:
[8f6f47d7]234                newtype->typeexpr = maybeClone( typeexpr );
[b87a5ed]235                break;
[702e826]236        case Vtable:
[93bbbc4]237                break;
[702e826]238        case Builtin:
[4cb935e]239                assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
240                newtype->builtintype = builtintype;
[90c3b1c]241                break;
[702e826]242        case Qualified:
[c194661]243                newtype->qualified.parent = maybeClone( qualified.parent );
244                newtype->qualified.child = maybeClone( qualified.child );
245                break;
[68cd1ce]246        } // switch
[b87a5ed]247        return newtype;
[413ad05]248} // TypeData::clone
[51b7345]249
[201aeb9]250
[2298f728]251void TypeData::print( ostream &os, int indent ) const {
[738e304]252        for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
[615a096]253                if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
[c1c1112]254        } // for
[b87a5ed]255
256        if ( forall ) {
257                os << "forall " << endl;
[721f17a]258                forall->printList( os, indent + 4 );
[68cd1ce]259        } // if
[b87a5ed]260
261        switch ( kind ) {
[702e826]262        case Basic:
[f7e4db27]263                if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
264                if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
[66406f3]265                if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " ";
266                if ( basictype != DeclarationNode::NoBasicType ) os << DeclarationNode::basicTypeNames[ basictype ] << " ";
[b87a5ed]267                break;
[702e826]268        case Pointer:
[b87a5ed]269                os << "pointer ";
270                if ( base ) {
271                        os << "to ";
272                        base->print( os, indent );
[68cd1ce]273                } // if
[b87a5ed]274                break;
[702e826]275        case Reference:
[f7e4db27]276                os << "reference ";
277                if ( base ) {
278                        os << "to ";
279                        base->print( os, indent );
280                } // if
[b87a5ed]281                break;
[702e826]282        case Array:
[8f6f47d7]283                if ( array.isStatic ) {
[b87a5ed]284                        os << "static ";
[68cd1ce]285                } // if
[8f6f47d7]286                if ( array.dimension ) {
[b87a5ed]287                        os << "array of ";
[8f6f47d7]288                        array.dimension->printOneLine( os, indent );
289                } else if ( array.isVarLen ) {
[b87a5ed]290                        os << "variable-length array of ";
291                } else {
292                        os << "open array of ";
[68cd1ce]293                } // if
[b87a5ed]294                if ( base ) {
295                        base->print( os, indent );
[68cd1ce]296                } // if
[b87a5ed]297                break;
[702e826]298        case Function:
[b87a5ed]299                os << "function" << endl;
[8f6f47d7]300                if ( function.params ) {
[721f17a]301                        os << string( indent + 2, ' ' ) << "with parameters " << endl;
[8f6f47d7]302                        function.params->printList( os, indent + 4 );
[b87a5ed]303                } else {
[07ec1a2]304                        os << string( indent + 2, ' ' ) << "with no parameters" << endl;
[68cd1ce]305                } // if
[8f6f47d7]306                if ( function.idList ) {
[721f17a]307                        os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
[8f6f47d7]308                        function.idList->printList( os, indent + 4 );
[68cd1ce]309                } // if
[8f6f47d7]310                if ( function.oldDeclList ) {
[721f17a]311                        os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
[8f6f47d7]312                        function.oldDeclList->printList( os, indent + 4 );
[68cd1ce]313                } // if
[721f17a]314                os << string( indent + 2, ' ' ) << "returning ";
[b87a5ed]315                if ( base ) {
[721f17a]316                        base->print( os, indent + 4 );
[b87a5ed]317                } else {
318                        os << "nothing ";
[68cd1ce]319                } // if
[b87a5ed]320                os << endl;
[8f6f47d7]321                if ( function.body ) {
[ca1a547]322                        os << string( indent + 2, ' ' ) << "with body " << endl;
[8f6f47d7]323                        function.body->printList( os, indent + 2 );
[68cd1ce]324                } // if
[b87a5ed]325                break;
[702e826]326        case Aggregate:
[312029a]327                os << AggregateDecl::aggrString( aggregate.kind ) << ' ' << *aggregate.name << endl;
[8f6f47d7]328                if ( aggregate.params ) {
[07ec1a2]329                        os << string( indent + 2, ' ' ) << "with type parameters" << endl;
[8f6f47d7]330                        aggregate.params->printList( os, indent + 4 );
[68cd1ce]331                } // if
[8f6f47d7]332                if ( aggregate.actuals ) {
[07ec1a2]333                        os << string( indent + 2, ' ' ) << "instantiated with actual parameters" << endl;
[8f6f47d7]334                        aggregate.actuals->printList( os, indent + 4 );
[68cd1ce]335                } // if
[8f6f47d7]336                if ( aggregate.fields ) {
[07ec1a2]337                        os << string( indent + 2, ' ' ) << "with members" << endl;
[8f6f47d7]338                        aggregate.fields->printList( os, indent + 4 );
[5d125e4]339                } // if
[8f6f47d7]340                if ( aggregate.body ) {
[07ec1a2]341                        os << string( indent + 2, ' ' ) << " with body" << endl;
[68cd1ce]342                } // if
[b87a5ed]343                break;
[702e826]344        case AggregateInst:
[8f6f47d7]345                if ( aggInst.aggregate ) {
[b87a5ed]346                        os << "instance of " ;
[8f6f47d7]347                        aggInst.aggregate->print( os, indent );
[b87a5ed]348                } else {
349                        os << "instance of an unspecified aggregate ";
[68cd1ce]350                } // if
[8f6f47d7]351                if ( aggInst.params ) {
[07ec1a2]352                        os << string( indent + 2, ' ' ) << "with parameters" << endl;
[8f6f47d7]353                        aggInst.params->printList( os, indent + 2 );
[68cd1ce]354                } // if
[b87a5ed]355                break;
[702e826]356        case Enum:
[2d37a788]357                os << "enumeration " << *enumeration.name << endl;;
[8f6f47d7]358                if ( enumeration.constants ) {
[b87a5ed]359                        os << "with constants" << endl;
[8f6f47d7]360                        enumeration.constants->printList( os, indent + 2 );
[68cd1ce]361                } // if
[ca1a547]362                if ( enumeration.body ) {
[07ec1a2]363                        os << string( indent + 2, ' ' ) << " with body" << endl;
[ca1a547]364                } // if
[9e7236f4]365                if ( base ) {
366                        os << "for ";
367                        base->print( os, indent + 2 );
368                } // if
[b87a5ed]369                break;
[702e826]370        case EnumConstant:
[f7e4db27]371                os << "enumeration constant ";
[b87a5ed]372                break;
[702e826]373        case Symbolic:
[8f6f47d7]374                if ( symbolic.isTypedef ) {
[b87a5ed]375                        os << "typedef definition ";
376                } else {
377                        os << "type definition ";
[68cd1ce]378                } // if
[8f6f47d7]379                if ( symbolic.params ) {
[721f17a]380                        os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
[8f6f47d7]381                        symbolic.params->printList( os, indent + 2 );
[68cd1ce]382                } // if
[8f6f47d7]383                if ( symbolic.assertions ) {
[721f17a]384                        os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
[8f6f47d7]385                        symbolic.assertions->printList( os, indent + 4 );
[721f17a]386                        os << string( indent + 2, ' ' );
[68cd1ce]387                } // if
[b87a5ed]388                if ( base ) {
389                        os << "for ";
390                        base->print( os, indent + 2 );
[68cd1ce]391                } // if
[b87a5ed]392                break;
[702e826]393        case SymbolicInst:
[f7e4db27]394                os << *symbolic.name;
395                if ( symbolic.actuals ) {
396                        os << "(";
397                        symbolic.actuals->printList( os, indent + 2 );
398                        os << ")";
399                } // if
400                break;
[702e826]401        case Tuple:
[b87a5ed]402                os << "tuple ";
[8f6f47d7]403                if ( tuple ) {
[07ec1a2]404                        os << "with members" << endl;
[8f6f47d7]405                        tuple->printList( os, indent + 2 );
[68cd1ce]406                } // if
[b87a5ed]407                break;
[702e826]408        case Basetypeof:
[f855545]409                os << "base-";
[c45b304]410                #if defined(__GNUC__) && __GNUC__ >= 7
411                        __attribute__((fallthrough));
412                #endif
[66406f3]413                // FALL THROUGH
[702e826]414        case Typeof:
[b87a5ed]415                os << "type-of expression ";
[8f6f47d7]416                if ( typeexpr ) {
417                        typeexpr->print( os, indent + 2 );
[68cd1ce]418                } // if
[b87a5ed]419                break;
[702e826]420        case Vtable:
[66406f3]421                os << "vtable";
422                break;
[702e826]423        case Builtin:
[9dc31c10]424                os << DeclarationNode::builtinTypeNames[builtintype];
[90c3b1c]425                break;
[702e826]426        case GlobalScope:
[f7e4db27]427                break;
[702e826]428        case Qualified:
[f7e4db27]429                qualified.parent->print( os );
430                os << ".";
431                qualified.child->print( os );
432                break;
[702e826]433        case Unknown:
[f7e4db27]434                os << "entity of unknown type ";
435                break;
[702e826]436        default:
[5b639ee]437                os << "internal error: TypeData::print " << kind << endl;
[1db21619]438                assert( false );
[68cd1ce]439        } // switch
[413ad05]440} // TypeData::print
[51b7345]441
[7de22b28]442const std::string * TypeData::leafName() const {
443        switch ( kind ) {
[702e826]444        case Unknown:
445        case Pointer:
446        case Reference:
447        case EnumConstant:
448        case GlobalScope:
449        case Array:
450        case Basic:
451        case Function:
452        case AggregateInst:
453        case Tuple:
454        case Typeof:
455        case Basetypeof:
456        case Builtin:
457        case Vtable:
[7de22b28]458                assertf(false, "Tried to get leaf name from kind without a name: %d", kind);
459                break;
[702e826]460        case Aggregate:
[7de22b28]461                return aggregate.name;
[702e826]462        case Enum:
[7de22b28]463                return enumeration.name;
[702e826]464        case Symbolic:
465        case SymbolicInst:
[7de22b28]466                return symbolic.name;
[702e826]467        case Qualified:
[7de22b28]468                return qualified.child->leafName();
469        } // switch
470        assert(false);
471}
472
[201aeb9]473
[8c49c0e]474template< typename ForallList >
475void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
[b87a5ed]476        buildList( firstNode, outputList );
[f0ecf9b]477        auto n = firstNode;
478        for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i, n = (DeclarationNode*)n->get_next() ) {
[a7c90d4]479                TypeDecl * td = static_cast<TypeDecl *>(*i);
[07de76b]480                if ( n->variable.tyClass == TypeDecl::Otype ) {
[6943f051]481                        // add assertion parameters to `type' tyvars in reverse order
482                        // add dtor:  void ^?{}(T *)
[413ad05]483                        FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
[cda7889]484                        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 ) );
[68fe077a]485                        td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
[4cc4286]486
[a9a259c]487                        // add copy ctor:  void ?{}(T *, T)
[413ad05]488                        FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
[cda7889]489                        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 ) );
[68fe077a]490                        copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
491                        td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
[a9a259c]492
[6943f051]493                        // add default ctor:  void ?{}(T *)
[413ad05]494                        FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
[cda7889]495                        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 ) );
[68fe077a]496                        td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
[6943f051]497
498                        // add assignment operator:  T * ?=?(T *, T)
[413ad05]499                        FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
[cda7889]500                        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 ) );
[68fe077a]501                        assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
502                        assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
503                        td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
[68cd1ce]504                } // if
505        } // for
[201aeb9]506} // buildForall
507
[51b7345]508
[413ad05]509Type * typebuild( const TypeData * td ) {
510        assert( td );
511        switch ( td->kind ) {
[702e826]512        case TypeData::Unknown:
[07de76b]513                // fill in implicit int
514                return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
[702e826]515        case TypeData::Basic:
[07de76b]516                return buildBasicType( td );
[702e826]517        case TypeData::Pointer:
[07de76b]518                return buildPointer( td );
[702e826]519        case TypeData::Array:
[07de76b]520                return buildArray( td );
[702e826]521        case TypeData::Reference:
[07de76b]522                return buildReference( td );
[702e826]523        case TypeData::Function:
[07de76b]524                return buildFunction( td );
[702e826]525        case TypeData::AggregateInst:
[07de76b]526                return buildAggInst( td );
[702e826]527        case TypeData::EnumConstant:
[07de76b]528                return new EnumInstType( buildQualifiers( td ), "" );
[702e826]529        case TypeData::SymbolicInst:
[07de76b]530                return buildSymbolicInst( td );
[702e826]531        case TypeData::Tuple:
[07de76b]532                return buildTuple( td );
[702e826]533        case TypeData::Typeof:
534        case TypeData::Basetypeof:
[07de76b]535                return buildTypeof( td );
[702e826]536        case TypeData::Vtable:
[93bbbc4]537                return buildVtable( td );
[702e826]538        case TypeData::Builtin:
[07de76b]539                switch ( td->builtintype ) {
[702e826]540                case DeclarationNode::Zero:
[07de76b]541                        return new ZeroType( noQualifiers );
[702e826]542                case DeclarationNode::One:
[07de76b]543                        return new OneType( noQualifiers );
[702e826]544                default:
[07de76b]545                        return new VarArgsType( buildQualifiers( td ) );
546                } // switch
[702e826]547        case TypeData::GlobalScope:
[07de76b]548                return new GlobalScopeType();
[702e826]549        case TypeData::Qualified:
[07de76b]550                return new QualifiedType( buildQualifiers( td ), typebuild( td->qualified.parent ), typebuild( td->qualified.child ) );
[702e826]551        case TypeData::Symbolic:
552        case TypeData::Enum:
553        case TypeData::Aggregate:
[07de76b]554                assert( false );
[68cd1ce]555        } // switch
[c194661]556
[2298f728]557        return nullptr;
[413ad05]558} // typebuild
559
[201aeb9]560
[413ad05]561TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
[2298f728]562        TypeData * ret = nullptr;
[51b7345]563
[413ad05]564        switch ( td->kind ) {
[702e826]565        case TypeData::Aggregate:
[b2da0574]566                if ( ! toplevel && td->aggregate.body ) {
[413ad05]567                        ret = td->clone();
568                } // if
569                break;
[702e826]570        case TypeData::Enum:
[b2da0574]571                if ( ! toplevel && td->enumeration.body ) {
[413ad05]572                        ret = td->clone();
573                } // if
574                break;
[702e826]575        case TypeData::AggregateInst:
[8f6f47d7]576                if ( td->aggInst.aggregate ) {
577                        ret = typeextractAggregate( td->aggInst.aggregate, false );
[413ad05]578                } // if
579                break;
[702e826]580        default:
[413ad05]581                if ( td->base ) {
582                        ret = typeextractAggregate( td->base, false );
583                } // if
584        } // switch
585        return ret;
586} // typeextractAggregate
587
[201aeb9]588
[413ad05]589Type::Qualifiers buildQualifiers( const TypeData * td ) {
[738e304]590        return td->qualifiers;
[413ad05]591} // buildQualifiers
[51b7345]592
[201aeb9]593
594static string genTSError( string msg, DeclarationNode::BasicType basictype ) {
[a16764a6]595        SemanticError( yylloc, string( "invalid type specifier \"" ) + msg + "\" for type \"" + DeclarationNode::basicTypeNames[basictype] + "\"." );
[201aeb9]596} // genTSError
597
[413ad05]598Type * buildBasicType( const TypeData * td ) {
[b87a5ed]599        BasicType::Kind ret;
600
[5b639ee]601        switch ( td->basictype ) {
[702e826]602        case DeclarationNode::Void:
[201aeb9]603                if ( td->signedness != DeclarationNode::NoSignedness ) {
604                        genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
605                } // if
606                if ( td->length != DeclarationNode::NoLength ) {
607                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
[68cd1ce]608                } // if
[5b639ee]609                return new VoidType( buildQualifiers( td ) );
610                break;
611
[702e826]612        case DeclarationNode::Bool:
[5b639ee]613                if ( td->signedness != DeclarationNode::NoSignedness ) {
[201aeb9]614                        genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
[5b639ee]615                } // if
616                if ( td->length != DeclarationNode::NoLength ) {
[201aeb9]617                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
[68cd1ce]618                } // if
[51b7345]619
[e15853c]620                ret = BasicType::Bool;
[5b639ee]621                break;
622
[702e826]623        case DeclarationNode::Char:
[5b639ee]624                // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
625                // character types. The implementation shall define char to have the same range, representation, and behavior as
626                // either signed char or unsigned char.
[8c49c0e]627                static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
[5b639ee]628
629                if ( td->length != DeclarationNode::NoLength ) {
[201aeb9]630                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
[5b639ee]631                } // if
632
633                ret = chartype[ td->signedness ];
634                break;
635
[702e826]636        case DeclarationNode::Int:
[5b639ee]637                static BasicType::Kind inttype[2][4] = {
638                        { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
639                        { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
640                };
641
[702e826]642        Integral: ;
[5b639ee]643                if ( td->signedness == DeclarationNode::NoSignedness ) {
644                        const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
645                } // if
646                ret = inttype[ td->signedness ][ td->length ];
647                break;
648
[702e826]649        case DeclarationNode::Int128:
[4ee3b0c1]650                ret = td->signedness == DeclarationNode::Unsigned ? BasicType::UnsignedInt128 : BasicType::SignedInt128;
[201aeb9]651                if ( td->length != DeclarationNode::NoLength ) {
652                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
653                } // if
654                break;
655
[702e826]656        case DeclarationNode::Float:
657        case DeclarationNode::Double:
658        case DeclarationNode::LongDouble:                                       // not set until below
659        case DeclarationNode::uuFloat80:
660        case DeclarationNode::uuFloat128:
661        case DeclarationNode::uFloat16:
662        case DeclarationNode::uFloat32:
663        case DeclarationNode::uFloat32x:
664        case DeclarationNode::uFloat64:
665        case DeclarationNode::uFloat64x:
666        case DeclarationNode::uFloat128:
667        case DeclarationNode::uFloat128x:
[ba01b14]668                static BasicType::Kind floattype[2][12] = {
[e15853c]669                        { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, (BasicType::Kind)-1, (BasicType::Kind)-1, BasicType::uFloat16Complex, BasicType::uFloat32Complex, BasicType::uFloat32xComplex, BasicType::uFloat64Complex, BasicType::uFloat64xComplex, BasicType::uFloat128Complex, BasicType::uFloat128xComplex, },
670                        { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x, },
[5b639ee]671                };
672
[702e826]673        FloatingPoint: ;
[5b639ee]674                if ( td->signedness != DeclarationNode::NoSignedness ) {
[201aeb9]675                        genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
[5b639ee]676                } // if
677                if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
[201aeb9]678                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
[5b639ee]679                } // if
[4ee3b0c1]680                if ( td->basictype != DeclarationNode::Double && td->length == DeclarationNode::Long ) {
[201aeb9]681                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
[5b639ee]682                } // if
[ba01b14]683                if ( td->complextype == DeclarationNode::Imaginary ) {
684                        genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
685                } // if
[e15853c]686                if ( (td->basictype == DeclarationNode::uuFloat80 || td->basictype == DeclarationNode::uuFloat128) && td->complextype == DeclarationNode::Complex ) { // gcc unsupported
[ba01b14]687                        genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
688                } // if
[5b639ee]689                if ( td->length == DeclarationNode::Long ) {
690                        const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
691                } // if
692
693                ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
[ba01b14]694                //printf( "XXXX %d %d %d %d\n", td->complextype, td->basictype, DeclarationNode::Float, ret );
[5b639ee]695                break;
696
[702e826]697        case DeclarationNode::NoBasicType:
[5b639ee]698                // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
699                if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
700                        const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
701                        goto FloatingPoint;
702                } // if
703
704                const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
705                goto Integral;
[702e826]706        default:
707                assertf( false, "unknown basic type" );
[2ee5426]708                return nullptr;
[5b639ee]709        } // switch
710
711        BasicType * bt = new BasicType( buildQualifiers( td ), ret );
[413ad05]712        buildForall( td->forall, bt->get_forall() );
[b87a5ed]713        return bt;
[413ad05]714} // buildBasicType
[51b7345]715
[201aeb9]716
[413ad05]717PointerType * buildPointer( const TypeData * td ) {
718        PointerType * pt;
719        if ( td->base ) {
720                pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
[b87a5ed]721        } else {
[413ad05]722                pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
[68cd1ce]723        } // if
[413ad05]724        buildForall( td->forall, pt->get_forall() );
[b87a5ed]725        return pt;
[413ad05]726} // buildPointer
[51b7345]727
[201aeb9]728
[413ad05]729ArrayType * buildArray( const TypeData * td ) {
730        ArrayType * at;
731        if ( td->base ) {
[702e826]732                at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild( td->array.dimension ),
[8f6f47d7]733                                                        td->array.isVarLen, td->array.isStatic );
[b87a5ed]734        } else {
[413ad05]735                at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
[702e826]736                                                        maybeBuild( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
[68cd1ce]737        } // if
[413ad05]738        buildForall( td->forall, at->get_forall() );
[b87a5ed]739        return at;
[ce8c12f]740} // buildArray
741
[201aeb9]742
[ce8c12f]743ReferenceType * buildReference( const TypeData * td ) {
744        ReferenceType * rt;
745        if ( td->base ) {
746                rt = new ReferenceType( buildQualifiers( td ), typebuild( td->base ) );
747        } else {
748                rt = new ReferenceType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
749        } // if
750        buildForall( td->forall, rt->get_forall() );
751        return rt;
752} // buildReference
[51b7345]753
[201aeb9]754
[fa4805f]755AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
[413ad05]756        assert( td->kind == TypeData::Aggregate );
757        AggregateDecl * at;
[8f6f47d7]758        switch ( td->aggregate.kind ) {
[702e826]759        case AggregateDecl::Struct:
760        case AggregateDecl::Coroutine:
761        case AggregateDecl::Exception:
762        case AggregateDecl::Generator:
763        case AggregateDecl::Monitor:
764        case AggregateDecl::Thread:
[fa4805f]765                at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
[8f6f47d7]766                buildForall( td->aggregate.params, at->get_parameters() );
[b87a5ed]767                break;
[702e826]768        case AggregateDecl::Union:
[bd46af4]769                at = new UnionDecl( *td->aggregate.name, attributes, linkage );
[8f6f47d7]770                buildForall( td->aggregate.params, at->get_parameters() );
[b87a5ed]771                break;
[702e826]772        case AggregateDecl::Trait:
[bd46af4]773                at = new TraitDecl( *td->aggregate.name, attributes, linkage );
[8f6f47d7]774                buildList( td->aggregate.params, at->get_parameters() );
[b87a5ed]775                break;
[702e826]776        default:
[b87a5ed]777                assert( false );
[68cd1ce]778        } // switch
[4e06c1e]779
[8f6f47d7]780        buildList( td->aggregate.fields, at->get_members() );
781        at->set_body( td->aggregate.body );
[b87a5ed]782
783        return at;
[413ad05]784} // buildAggregate
[51b7345]785
[201aeb9]786
[fa4805f]787ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
[43c89a7]788        switch ( type->kind ) {
[702e826]789        case TypeData::Enum:
790                if ( type->enumeration.body ) {
791                        EnumDecl * typedecl = buildEnum( type, attributes, linkage );
792                        return new EnumInstType( buildQualifiers( type ), typedecl );
793                } else {
794                        return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
795                } // if
796        case TypeData::Aggregate:
797                if ( type->aggregate.body ) {
798                        AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
799                        switch ( type->aggregate.kind ) {
800                        case AggregateDecl::Struct:
801                        case AggregateDecl::Coroutine:
802                        case AggregateDecl::Monitor:
803                        case AggregateDecl::Thread:
804                                return new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
805                        case AggregateDecl::Union:
806                                return new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
807                        case AggregateDecl::Trait:
808                                assert( false );
809                                //return new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
810                                break;
811                        default:
812                                assert( false );
813                        } // switch
814                } else {
815                        switch ( type->aggregate.kind ) {
816                        case AggregateDecl::Struct:
817                        case AggregateDecl::Coroutine:
818                        case AggregateDecl::Monitor:
819                        case AggregateDecl::Thread:
820                                return new StructInstType( buildQualifiers( type ), *type->aggregate.name );
821                        case AggregateDecl::Union:
822                                return new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
823                        case AggregateDecl::Trait:
824                                return new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
825                        default:
826                                assert( false );
827                        } // switch
828                } // if
829                return nullptr;
830        default:
[43c89a7]831                assert( false );
832        } // switch
833} // buildAggInst
834
[201aeb9]835
[413ad05]836ReferenceToType * buildAggInst( const TypeData * td ) {
837        assert( td->kind == TypeData::AggregateInst );
[b87a5ed]838
[43c89a7]839        // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
840        ReferenceToType * ret = nullptr;
841        TypeData * type = td->aggInst.aggregate;
842        switch ( type->kind ) {
[702e826]843        case TypeData::Enum:
844                return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
845        case TypeData::Aggregate:
846                switch ( type->aggregate.kind ) {
847                case AggregateDecl::Struct:
848                case AggregateDecl::Coroutine:
849                case AggregateDecl::Monitor:
850                case AggregateDecl::Thread:
851                        ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
852                        break;
853                case AggregateDecl::Union:
854                        ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
855                        break;
856                case AggregateDecl::Trait:
857                        ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
858                        break;
859                default:
860                        assert( false );
861                } // switch
862                break;
863        default:
[43c89a7]864                assert( false );
865        } // switch
866
867        ret->set_hoistType( td->aggInst.hoistType );
[8f6f47d7]868        buildList( td->aggInst.params, ret->get_parameters() );
[413ad05]869        buildForall( td->forall, ret->get_forall() );
[b87a5ed]870        return ret;
[413ad05]871} // buildAggInst
872
[201aeb9]873
[1f370451]874NamedTypeDecl * buildSymbolic( const TypeData * td, std::list< Attribute * > attributes, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
[413ad05]875        assert( td->kind == TypeData::Symbolic );
876        NamedTypeDecl * ret;
877        assert( td->base );
[8f6f47d7]878        if ( td->symbolic.isTypedef ) {
[0b0f1dd]879                ret = new TypedefDecl( name, td->location, scs, typebuild( td->base ), linkage );
[b87a5ed]880        } else {
[f0ecf9b]881                ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Dtype, true );
[68cd1ce]882        } // if
[8f6f47d7]883        buildList( td->symbolic.assertions, ret->get_assertions() );
[1f370451]884        ret->base->attributes.splice( ret->base->attributes.end(), attributes );
[b87a5ed]885        return ret;
[413ad05]886} // buildSymbolic
[51b7345]887
[201aeb9]888
[bd46af4]889EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
[413ad05]890        assert( td->kind == TypeData::Enum );
[f238fcc2]891        Type * baseType = td->base ? typebuild(td->base) : nullptr;
[b0d9ff7]892        EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, td->enumeration.typed, linkage, baseType );
[f238fcc2]893        buildList( td->enumeration.constants, ret->get_members() );
[2298f728]894        list< Declaration * >::iterator members = ret->get_members().begin();
[e4d7c1c]895        ret->hide = td->enumeration.hiding == EnumHiding::Hide ? EnumDecl::EnumHiding::Hide : EnumDecl::EnumHiding::Visible;
[f135b50]896        for ( const DeclarationNode * cur = td->enumeration.constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
[1e30df7]897                if ( cur->enumInLine ) {
[e874605]898                        // Do Nothing
899                } else if ( ret->isTyped && !ret->base && cur->has_enumeratorValue() ) {
[b0d9ff7]900                        SemanticError( td->location, "Enumerator of enum(void) cannot have an explicit initializer value." );
901                } else if ( cur->has_enumeratorValue() ) {
[413ad05]902                        ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
[702e826]903                        member->set_init( new SingleInit( maybeMoveBuild( cur->consume_enumeratorValue() ) ) );
[9e7236f4]904                } else if ( !cur->initializer ) {
[8bb86ce]905                        if ( baseType && (!dynamic_cast<BasicType *>(baseType) || !dynamic_cast<BasicType *>(baseType)->isInteger())) {
[b0d9ff7]906                                SemanticError( td->location, "Enumerators of an non-integer typed enum must be explicitly initialized." );
[f238fcc2]907                        }
[9e7236f4]908                }
909                // else cur is a List Initializer and has been set as init in buildList()
910                // if
[90c3b1c]911        } // for
[9e7236f4]912        ret->set_body( td->enumeration.body );
[b87a5ed]913        return ret;
[413ad05]914} // buildEnum
[51b7345]915
[201aeb9]916
[413ad05]917TypeInstType * buildSymbolicInst( const TypeData * td ) {
918        assert( td->kind == TypeData::SymbolicInst );
[2298f728]919        TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
[8f6f47d7]920        buildList( td->symbolic.actuals, ret->get_parameters() );
[413ad05]921        buildForall( td->forall, ret->get_forall() );
[b87a5ed]922        return ret;
[413ad05]923} // buildSymbolicInst
[51b7345]924
[201aeb9]925
[413ad05]926TupleType * buildTuple( const TypeData * td ) {
927        assert( td->kind == TypeData::Tuple );
[62423350]928        std::list< Type * > types;
929        buildTypeList( td->tuple, types );
930        TupleType * ret = new TupleType( buildQualifiers( td ), types );
[413ad05]931        buildForall( td->forall, ret->get_forall() );
[b87a5ed]932        return ret;
[413ad05]933} // buildTuple
934
[201aeb9]935
[413ad05]936TypeofType * buildTypeof( const TypeData * td ) {
[f855545]937        assert( td->kind == TypeData::Typeof || td->kind == TypeData::Basetypeof );
[413ad05]938        assert( td->typeexpr );
[8f6f47d7]939        // assert( td->typeexpr->expr );
[9f77301]940        return new TypeofType{ buildQualifiers( td ), td->typeexpr->build(), td->kind == TypeData::Basetypeof };
[413ad05]941} // buildTypeof
942
[201aeb9]943
[93bbbc4]944VTableType * buildVtable( const TypeData * td ) {
945        assert( td->base );
946        return new VTableType{ buildQualifiers( td ), typebuild( td->base ) };
947} // buildVtable
948
949
[e612146c]950Declaration * 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 ) {
[413ad05]951        if ( td->kind == TypeData::Function ) {
[ca1a547]952                if ( td->function.idList ) {                                    // KR function ?
953                        buildKRFunction( td->function );                        // transform into C11 function
[3a5131ed]954                } // if
955
[413ad05]956                FunctionDecl * decl;
[702e826]957                Statement * stmt = maybeBuild( td->function.body );
[a7c90d4]958                CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
959                decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
[c453ac4]960                buildList( td->function.withExprs, decl->withExprs );
[58dd019]961                return decl->set_asmName( asmName );
[413ad05]962        } else if ( td->kind == TypeData::Aggregate ) {
[fa4805f]963                return buildAggregate( td, attributes, linkage );
[413ad05]964        } else if ( td->kind == TypeData::Enum ) {
[bd46af4]965                return buildEnum( td, attributes, linkage );
[413ad05]966        } else if ( td->kind == TypeData::Symbolic ) {
[1f370451]967                return buildSymbolic( td, attributes, name, scs, linkage );
[413ad05]968        } else {
[a7c90d4]969                return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
[413ad05]970        } // if
[2298f728]971        return nullptr;
[413ad05]972} // buildDecl
973
[201aeb9]974
[413ad05]975FunctionType * buildFunction( const TypeData * td ) {
976        assert( td->kind == TypeData::Function );
[2a8427c6]977        FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis );
[704d11e]978        buildList( td->function.params, ft->parameters );
979        buildForall( td->forall, ft->forall );
[413ad05]980        if ( td->base ) {
981                switch ( td->base->kind ) {
[702e826]982                case TypeData::Tuple:
[704d11e]983                        buildList( td->base->tuple, ft->returnVals );
[413ad05]984                        break;
[702e826]985                default:
[ddfd945]986                        ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
[413ad05]987                } // switch
988        } else {
[68fe077a]989                ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
[413ad05]990        } // if
991        return ft;
992} // buildFunction
[b87a5ed]993
[201aeb9]994
[a7c4921]995// Transform KR routine declarations into C99 routine declarations:
996//
997//    rtn( a, b, c ) int a, c; double b {}  =>  int rtn( int a, double c, int b ) {}
998//
999// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
1000// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
1001// extra names are disallowed.
1002//
1003// Note, there is no KR routine-prototype syntax:
1004//
1005//    rtn( a, b, c ) int a, c; double b; // invalid KR prototype
1006//    rtn(); // valid KR prototype
1007
[3a5131ed]1008void buildKRFunction( const TypeData::Function_t & function ) {
1009        assert( ! function.params );
[a7c4921]1010        // loop over declaration first as it is easier to spot errors
1011        for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
1012                // scan ALL parameter names for each declaration name to check for duplicates
[a7c90d4]1013                for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
[3a5131ed]1014                        if ( *decl->name == *param->name ) {
[a7c4921]1015                                // type set => parameter name already transformed by a declaration names so there is a duplicate
1016                                // declaration name attempting a second transformation
[a16764a6]1017                                if ( param->type ) SemanticError( param->location, string( "duplicate declaration name " ) + *param->name );
[a7c4921]1018                                // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
1019                                // parameter name attempting a second transformation
[a16764a6]1020                                if ( ! decl->type ) SemanticError( param->location, string( "duplicate parameter name " ) + *param->name );
[a7c4921]1021                                param->type = decl->type;                               // set copy declaration type to parameter type
1022                                decl->type = nullptr;                                   // reset declaration type
1023                                param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
[3a5131ed]1024                        } // if
1025                } // for
[a7c4921]1026                // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
[a16764a6]1027                if ( decl->type ) SemanticError( decl->location, string( "missing name in parameter list " ) + *decl->name );
[3a5131ed]1028        } // for
[a7c4921]1029
1030        // Parameter names without a declaration default to type int:
1031        //
1032        //    rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
1033
[a7c90d4]1034        for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
[ca1a547]1035                if ( ! param->type ) {                                                  // generate type int for empty parameter type
[3a5131ed]1036                        param->type = new TypeData( TypeData::Basic );
1037                        param->type->basictype = DeclarationNode::Int;
1038                } // if
1039        } // for
1040
[a7c4921]1041        function.params = function.idList;                                      // newly modified idList becomes parameters
1042        function.idList = nullptr;                                                      // idList now empty
1043        delete function.oldDeclList;                                            // deletes entire list
1044        function.oldDeclList = nullptr;                                         // reset
[3a5131ed]1045} // buildKRFunction
1046
[b87a5ed]1047// Local Variables: //
1048// tab-width: 4 //
1049// mode: c++ //
1050// compile-command: "make install" //
1051// End: //
Note: See TracBrowser for help on using the repository browser.