source: src/Parser/TypeData.cc @ 86f852b

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

first attempt at new basic-type int128, and length suffix with explicit size

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