Ignore:
Timestamp:
May 16, 2015, 3:36:19 PM (9 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
a32b204
Parents:
b8508a2
Message:

licencing: first groups of files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • translator/Parser/DeclarationNode.cc

    rb8508a2 rb87a5ed  
     1//
     2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
     3//
     4// The contents of this file are covered under the licence agreement in the
     5// file "LICENCE" distributed with Cforall.
     6//
     7// DeclarationNode.cc --
     8//
     9// Author           : Rodolfo G. Esteves
     10// Created On       : Sat May 16 12:34:05 2015
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Sat May 16 12:38:20 2015
     13// Update Count     : 4
     14//
     15
    116#include <string>
    217#include <list>
     
    2338
    2439DeclarationNode *DeclarationNode::clone() const {
    25     DeclarationNode *newnode = new DeclarationNode;
    26     newnode->type = maybeClone( type );
    27     newnode->name = name;
    28     newnode->storageClasses = storageClasses;
    29     newnode->bitfieldWidth = maybeClone( bitfieldWidth );
    30     newnode->hasEllipsis = hasEllipsis;
    31     newnode->initializer = initializer;
    32     newnode->next = maybeClone( next );
    33     newnode->linkage = linkage;
    34     return newnode;
     40        DeclarationNode *newnode = new DeclarationNode;
     41        newnode->type = maybeClone( type );
     42        newnode->name = name;
     43        newnode->storageClasses = storageClasses;
     44        newnode->bitfieldWidth = maybeClone( bitfieldWidth );
     45        newnode->hasEllipsis = hasEllipsis;
     46        newnode->initializer = initializer;
     47        newnode->next = maybeClone( next );
     48        newnode->linkage = linkage;
     49        return newnode;
    3550}
    3651
     
    3954
    4055DeclarationNode::~DeclarationNode() {
    41     delete type;
    42     delete bitfieldWidth;
    43     delete initializer;
     56        delete type;
     57        delete bitfieldWidth;
     58        delete initializer;
    4459}
    4560
    4661bool DeclarationNode::get_hasEllipsis() const {
    47     return hasEllipsis;
     62        return hasEllipsis;
    4863}
    4964
    5065const char *storageClassName[] = {
    51     // order must correspond with DeclarationNode::StorageClass
    52     "extern",
    53     "static",
    54     "auto",
    55     "register",
    56     "inline",
    57     "fortran",
     66        // order must correspond with DeclarationNode::StorageClass
     67        "extern",
     68        "static",
     69        "auto",
     70        "register",
     71        "inline",
     72        "fortran",
    5873};
    5974
    6075void DeclarationNode::print( std::ostream &os, int indent ) const {
    61     os << string(indent, ' ' );
    62     if ( name == "" ) {
    63         os << "unnamed: ";
    64     } else {
    65         os << name << ": ";
    66     }
    67 
    68     if ( linkage != LinkageSpec::Cforall ) {
    69         os << LinkageSpec::toString( linkage ) << " ";
    70     }
    71 
    72     printEnums( storageClasses.begin(), storageClasses.end(), storageClassName, os );
    73     if ( type ) {
    74         type->print( os, indent );
    75     } else {
    76         os << "untyped entity ";
    77     }
    78 
    79     if ( bitfieldWidth ) {
    80         os << endl << string(indent+2,  ' ') << "with bitfield width ";
    81         bitfieldWidth->printOneLine( os );
    82     }
    83 
    84     if ( initializer != 0 ) {
    85         os << endl << string(indent+2,  ' ') << "with initializer ";
    86         initializer->printOneLine( os );
    87     }
    88 
    89     os << endl;
     76        os << string(indent, ' ' );
     77        if ( name == "" ) {
     78                os << "unnamed: ";
     79        } else {
     80                os << name << ": ";
     81        }
     82
     83        if ( linkage != LinkageSpec::Cforall ) {
     84                os << LinkageSpec::toString( linkage ) << " ";
     85        }
     86
     87        printEnums( storageClasses.begin(), storageClasses.end(), storageClassName, os );
     88        if ( type ) {
     89                type->print( os, indent );
     90        } else {
     91                os << "untyped entity ";
     92        }
     93
     94        if ( bitfieldWidth ) {
     95                os << endl << string(indent+2,  ' ') << "with bitfield width ";
     96                bitfieldWidth->printOneLine( os );
     97        }
     98
     99        if ( initializer != 0 ) {
     100                os << endl << string(indent+2,  ' ') << "with initializer ";
     101                initializer->printOneLine( os );
     102        }
     103
     104        os << endl;
    90105}
    91106
    92107void DeclarationNode::printList( std::ostream &os, int indent ) const {
    93     ParseNode::printList( os, indent );
    94     if ( hasEllipsis ) {
    95         os << string( indent, ' ' )  << "and a variable number of other arguments" << endl;
    96     }
     108        ParseNode::printList( os, indent );
     109        if ( hasEllipsis ) {
     110                os << string( indent, ' ' )  << "and a variable number of other arguments" << endl;
     111        }
    97112}
    98113
    99114DeclarationNode *DeclarationNode::newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle ) {
    100     DeclarationNode *newnode = new DeclarationNode;
    101     newnode->name = assign_strptr( name );
    102 
    103     newnode->type = new TypeData( TypeData::Function );
    104     newnode->type->function->params = param;
    105     newnode->type->function->newStyle = newStyle;
    106     newnode->type->function->body = body;
    107 
    108     if ( body ) {
    109         newnode->type->function->hasBody = true;
    110     }
    111 
    112     if ( ret ) {
    113         newnode->type->base = ret->type;
    114         ret->type = 0;
    115         delete ret;
    116     }
    117 
    118     return newnode;
     115        DeclarationNode *newnode = new DeclarationNode;
     116        newnode->name = assign_strptr( name );
     117
     118        newnode->type = new TypeData( TypeData::Function );
     119        newnode->type->function->params = param;
     120        newnode->type->function->newStyle = newStyle;
     121        newnode->type->function->body = body;
     122
     123        if ( body ) {
     124                newnode->type->function->hasBody = true;
     125        }
     126
     127        if ( ret ) {
     128                newnode->type->base = ret->type;
     129                ret->type = 0;
     130                delete ret;
     131        }
     132
     133        return newnode;
    119134}
    120135
    121136DeclarationNode *DeclarationNode::newQualifier( Qualifier q ) {
    122     DeclarationNode *newnode = new DeclarationNode;
    123     newnode->type = new TypeData();
    124     newnode->type->qualifiers.push_back( q );
    125     return newnode;
     137        DeclarationNode *newnode = new DeclarationNode;
     138        newnode->type = new TypeData();
     139        newnode->type->qualifiers.push_back( q );
     140        return newnode;
    126141}
    127142
    128143DeclarationNode *DeclarationNode::newStorageClass( StorageClass sc ) {
    129     DeclarationNode *newnode = new DeclarationNode;
    130     newnode->storageClasses.push_back( sc );
    131     return newnode;
     144        DeclarationNode *newnode = new DeclarationNode;
     145        newnode->storageClasses.push_back( sc );
     146        return newnode;
    132147}
    133148
    134149DeclarationNode *DeclarationNode::newBasicType( BasicType bt ) {
    135     DeclarationNode *newnode = new DeclarationNode;
    136     newnode->type = new TypeData( TypeData::Basic );
    137     newnode->type->basic->typeSpec.push_back( bt );
    138     return newnode;
     150        DeclarationNode *newnode = new DeclarationNode;
     151        newnode->type = new TypeData( TypeData::Basic );
     152        newnode->type->basic->typeSpec.push_back( bt );
     153        return newnode;
    139154}
    140155
    141156DeclarationNode *DeclarationNode::newModifier( Modifier mod ) {
    142     DeclarationNode *newnode = new DeclarationNode;
    143     newnode->type = new TypeData( TypeData::Basic );
    144     newnode->type->basic->modifiers.push_back( mod );
    145     return newnode;
     157        DeclarationNode *newnode = new DeclarationNode;
     158        newnode->type = new TypeData( TypeData::Basic );
     159        newnode->type->basic->modifiers.push_back( mod );
     160        return newnode;
    146161}
    147162
    148163DeclarationNode *DeclarationNode::newForall( DeclarationNode* forall ) {
    149     DeclarationNode *newnode = new DeclarationNode;
    150     newnode->type = new TypeData( TypeData::Unknown );
    151     newnode->type->forall = forall;
    152     return newnode;
     164        DeclarationNode *newnode = new DeclarationNode;
     165        newnode->type = new TypeData( TypeData::Unknown );
     166        newnode->type->forall = forall;
     167        return newnode;
    153168}
    154169
    155170DeclarationNode *DeclarationNode::newFromTypedef( std::string* name ) {
    156     DeclarationNode *newnode = new DeclarationNode;
    157     newnode->type = new TypeData( TypeData::SymbolicInst );
    158     newnode->type->symbolic->name = assign_strptr( name );
    159     newnode->type->symbolic->isTypedef = true;
    160     newnode->type->symbolic->params = 0;
    161     return newnode;
     171        DeclarationNode *newnode = new DeclarationNode;
     172        newnode->type = new TypeData( TypeData::SymbolicInst );
     173        newnode->type->symbolic->name = assign_strptr( name );
     174        newnode->type->symbolic->isTypedef = true;
     175        newnode->type->symbolic->params = 0;
     176        return newnode;
    162177}
    163178
    164179DeclarationNode *DeclarationNode::newAggregate( TyCon kind, std::string* name, DeclarationNode *formals, ExpressionNode *actuals, DeclarationNode *fields ) {
    165     DeclarationNode *newnode = new DeclarationNode;
    166     newnode->type = new TypeData( TypeData::Aggregate );
    167     newnode->type->aggregate->kind = kind;
    168     newnode->type->aggregate->name = assign_strptr( name );
    169     if ( newnode->type->aggregate->name == "" ) {
    170         newnode->type->aggregate->name = DeclarationNode::anonymous.newName();
    171     }
    172     newnode->type->aggregate->params = formals;
    173     newnode->type->aggregate->actuals = actuals;
    174     newnode->type->aggregate->members = fields;
    175     return newnode;
     180        DeclarationNode *newnode = new DeclarationNode;
     181        newnode->type = new TypeData( TypeData::Aggregate );
     182        newnode->type->aggregate->kind = kind;
     183        newnode->type->aggregate->name = assign_strptr( name );
     184        if ( newnode->type->aggregate->name == "" ) {
     185                newnode->type->aggregate->name = DeclarationNode::anonymous.newName();
     186        }
     187        newnode->type->aggregate->params = formals;
     188        newnode->type->aggregate->actuals = actuals;
     189        newnode->type->aggregate->members = fields;
     190        return newnode;
    176191}
    177192
    178193DeclarationNode *DeclarationNode::newEnum( std::string *name, DeclarationNode *constants ) {
    179     DeclarationNode *newnode = new DeclarationNode;
    180     newnode->name = assign_strptr( name );
    181     newnode->type = new TypeData( TypeData::Enum );
    182     newnode->type->enumeration->name = newnode->name;
    183     if ( newnode->type->enumeration->name == "" ) {
    184         newnode->type->enumeration->name = DeclarationNode::anonymous.newName();
    185     }
    186     newnode->type->enumeration->constants = constants;
    187     return newnode;
     194        DeclarationNode *newnode = new DeclarationNode;
     195        newnode->name = assign_strptr( name );
     196        newnode->type = new TypeData( TypeData::Enum );
     197        newnode->type->enumeration->name = newnode->name;
     198        if ( newnode->type->enumeration->name == "" ) {
     199                newnode->type->enumeration->name = DeclarationNode::anonymous.newName();
     200        }
     201        newnode->type->enumeration->constants = constants;
     202        return newnode;
    188203}
    189204
    190205DeclarationNode *DeclarationNode::newEnumConstant( std::string* name, ExpressionNode *constant ) {
    191     DeclarationNode *newnode = new DeclarationNode;
    192     newnode->name = assign_strptr( name );
    193     // do something with the constant
    194     return newnode;
     206        DeclarationNode *newnode = new DeclarationNode;
     207        newnode->name = assign_strptr( name );
     208        // do something with the constant
     209        return newnode;
    195210}
    196211
    197212DeclarationNode *DeclarationNode::newName( std::string* name ) {
    198     DeclarationNode *newnode = new DeclarationNode;
    199     newnode->name = assign_strptr( name );
    200     return newnode;
     213        DeclarationNode *newnode = new DeclarationNode;
     214        newnode->name = assign_strptr( name );
     215        return newnode;
    201216}
    202217
    203218DeclarationNode *DeclarationNode::newFromTypeGen( std::string* name, ExpressionNode *params ) {
    204     DeclarationNode *newnode = new DeclarationNode;
    205     newnode->type = new TypeData( TypeData::SymbolicInst );
    206     newnode->type->symbolic->name = assign_strptr( name );
    207     newnode->type->symbolic->isTypedef = false;
    208     newnode->type->symbolic->actuals = params;
    209     return newnode;
     219        DeclarationNode *newnode = new DeclarationNode;
     220        newnode->type = new TypeData( TypeData::SymbolicInst );
     221        newnode->type->symbolic->name = assign_strptr( name );
     222        newnode->type->symbolic->isTypedef = false;
     223        newnode->type->symbolic->actuals = params;
     224        return newnode;
    210225}
    211226
    212227DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string* name ) {
    213     DeclarationNode *newnode = new DeclarationNode;
    214     newnode->name = assign_strptr( name );
    215     newnode->type = new TypeData( TypeData::Variable );
    216     newnode->type->variable->tyClass = tc;
    217     newnode->type->variable->name = newnode->name;
    218     return newnode;
     228        DeclarationNode *newnode = new DeclarationNode;
     229        newnode->name = assign_strptr( name );
     230        newnode->type = new TypeData( TypeData::Variable );
     231        newnode->type->variable->tyClass = tc;
     232        newnode->type->variable->name = newnode->name;
     233        return newnode;
    219234}
    220235
    221236DeclarationNode *DeclarationNode::newContext( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) {
    222     DeclarationNode *newnode = new DeclarationNode;
    223     newnode->type = new TypeData( TypeData::Aggregate );
    224     newnode->type->aggregate->kind = Context;
    225     newnode->type->aggregate->params = params;
    226     newnode->type->aggregate->members = asserts;
    227     newnode->type->aggregate->name = assign_strptr( name );
    228     return newnode;
     237        DeclarationNode *newnode = new DeclarationNode;
     238        newnode->type = new TypeData( TypeData::Aggregate );
     239        newnode->type->aggregate->kind = Context;
     240        newnode->type->aggregate->params = params;
     241        newnode->type->aggregate->members = asserts;
     242        newnode->type->aggregate->name = assign_strptr( name );
     243        return newnode;
    229244}
    230245
    231246DeclarationNode *DeclarationNode::newContextUse( std::string *name, ExpressionNode *params ) {
    232     DeclarationNode *newnode = new DeclarationNode;
    233     newnode->type = new TypeData( TypeData::AggregateInst );
    234     newnode->type->aggInst->aggregate = new TypeData( TypeData::Aggregate );
    235     newnode->type->aggInst->aggregate->aggregate->kind = Context;
    236     newnode->type->aggInst->aggregate->aggregate->name = assign_strptr( name );
    237     newnode->type->aggInst->params = params;
    238     return newnode;
     247        DeclarationNode *newnode = new DeclarationNode;
     248        newnode->type = new TypeData( TypeData::AggregateInst );
     249        newnode->type->aggInst->aggregate = new TypeData( TypeData::Aggregate );
     250        newnode->type->aggInst->aggregate->aggregate->kind = Context;
     251        newnode->type->aggInst->aggregate->aggregate->name = assign_strptr( name );
     252        newnode->type->aggInst->params = params;
     253        return newnode;
    239254}
    240255
    241256DeclarationNode *DeclarationNode::newTypeDecl( std::string *name, DeclarationNode *typeParams ) {
    242     DeclarationNode *newnode = new DeclarationNode;
    243     newnode->name = assign_strptr( name );
    244     newnode->type = new TypeData( TypeData::Symbolic );
    245     newnode->type->symbolic->isTypedef = false;
    246     newnode->type->symbolic->params = typeParams;
    247     newnode->type->symbolic->name = newnode->name;
    248     return newnode;
     257        DeclarationNode *newnode = new DeclarationNode;
     258        newnode->name = assign_strptr( name );
     259        newnode->type = new TypeData( TypeData::Symbolic );
     260        newnode->type->symbolic->isTypedef = false;
     261        newnode->type->symbolic->params = typeParams;
     262        newnode->type->symbolic->name = newnode->name;
     263        return newnode;
    249264}
    250265
    251266DeclarationNode *DeclarationNode::newPointer( DeclarationNode *qualifiers ) {
    252     DeclarationNode *newnode = new DeclarationNode;
    253     newnode->type = new TypeData( TypeData::Pointer );
    254     return newnode->addQualifiers( qualifiers );
     267        DeclarationNode *newnode = new DeclarationNode;
     268        newnode->type = new TypeData( TypeData::Pointer );
     269        return newnode->addQualifiers( qualifiers );
    255270}
    256271
    257272DeclarationNode *DeclarationNode::newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ) {
    258     DeclarationNode *newnode = new DeclarationNode;
    259     newnode->type = new TypeData( TypeData::Array );
    260     newnode->type->array->dimension = size;
    261     newnode->type->array->isStatic = isStatic;
    262     newnode->type->array->isVarLen = false;
    263     return newnode->addQualifiers( qualifiers );
     273        DeclarationNode *newnode = new DeclarationNode;
     274        newnode->type = new TypeData( TypeData::Array );
     275        newnode->type->array->dimension = size;
     276        newnode->type->array->isStatic = isStatic;
     277        newnode->type->array->isVarLen = false;
     278        return newnode->addQualifiers( qualifiers );
    264279}
    265280
    266281DeclarationNode *DeclarationNode::newVarArray( DeclarationNode *qualifiers ) {
    267     DeclarationNode *newnode = new DeclarationNode;
    268     newnode->type = new TypeData( TypeData::Array );
    269     newnode->type->array->dimension = 0;
    270     newnode->type->array->isStatic = false;
    271     newnode->type->array->isVarLen = true;
    272     return newnode->addQualifiers( qualifiers );
     282        DeclarationNode *newnode = new DeclarationNode;
     283        newnode->type = new TypeData( TypeData::Array );
     284        newnode->type->array->dimension = 0;
     285        newnode->type->array->isStatic = false;
     286        newnode->type->array->isVarLen = true;
     287        return newnode->addQualifiers( qualifiers );
    273288}
    274289
    275290DeclarationNode *DeclarationNode::newBitfield( ExpressionNode *size ) {
    276     DeclarationNode *newnode = new DeclarationNode;
    277     newnode->bitfieldWidth = size;
    278     return newnode;
     291        DeclarationNode *newnode = new DeclarationNode;
     292        newnode->bitfieldWidth = size;
     293        return newnode;
    279294}
    280295
    281296DeclarationNode *DeclarationNode::newTuple( DeclarationNode *members ) {
    282     DeclarationNode *newnode = new DeclarationNode;
    283     newnode->type = new TypeData( TypeData::Tuple );
    284     newnode->type->tuple->members = members;
    285     return newnode;
     297        DeclarationNode *newnode = new DeclarationNode;
     298        newnode->type = new TypeData( TypeData::Tuple );
     299        newnode->type->tuple->members = members;
     300        return newnode;
    286301}
    287302
    288303DeclarationNode *DeclarationNode::newTypeof( ExpressionNode *expr ) {
    289     DeclarationNode *newnode = new DeclarationNode;
    290     newnode->type = new TypeData( TypeData::Typeof );
    291     newnode->type->typeexpr->expr = expr;
    292     return newnode;
     304        DeclarationNode *newnode = new DeclarationNode;
     305        newnode->type = new TypeData( TypeData::Typeof );
     306        newnode->type->typeexpr->expr = expr;
     307        return newnode;
    293308}
    294309
    295310DeclarationNode *DeclarationNode::newAttr( std::string *name, ExpressionNode *expr ) {
    296     DeclarationNode *newnode = new DeclarationNode;
    297     newnode->type = new TypeData( TypeData::Attr );
    298     newnode->type->attr->name = assign_strptr( name );
    299     newnode->type->attr->expr = expr;
    300     return newnode;
     311        DeclarationNode *newnode = new DeclarationNode;
     312        newnode->type = new TypeData( TypeData::Attr );
     313        newnode->type->attr->name = assign_strptr( name );
     314        newnode->type->attr->expr = expr;
     315        return newnode;
    301316}
    302317
    303318DeclarationNode *DeclarationNode::newAttr( std::string *name, DeclarationNode *type ) {
    304     DeclarationNode *newnode = new DeclarationNode;
    305     newnode->type = new TypeData( TypeData::Attr );
    306     newnode->type->attr->name = assign_strptr( name );
    307     newnode->type->attr->type = type;
    308     return newnode;
     319        DeclarationNode *newnode = new DeclarationNode;
     320        newnode->type = new TypeData( TypeData::Attr );
     321        newnode->type->attr->name = assign_strptr( name );
     322        newnode->type->attr->type = type;
     323        return newnode;
    309324}
    310325
    311326static void addQualifiersToType( TypeData *&src, TypeData *dst ) {
    312     if ( src && dst ) {
    313         if ( src->forall && dst->kind == TypeData::Function ) {
    314             if ( dst->forall ) {
    315                 dst->forall->appendList( src->forall );
    316             } else {
    317                 dst->forall = src->forall;
    318             }
    319             src->forall = 0;
    320         }
    321         if ( dst->base ) {
    322             addQualifiersToType( src, dst->base );
    323         } else if ( dst->kind == TypeData::Function ) {
    324             dst->base = src;
    325             src = 0;
    326         } else {
    327             dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
    328         }
    329     }
    330 }
    331      
     327        if ( src && dst ) {
     328                if ( src->forall && dst->kind == TypeData::Function ) {
     329                        if ( dst->forall ) {
     330                                dst->forall->appendList( src->forall );
     331                        } else {
     332                                dst->forall = src->forall;
     333                        }
     334                        src->forall = 0;
     335                }
     336                if ( dst->base ) {
     337                        addQualifiersToType( src, dst->base );
     338                } else if ( dst->kind == TypeData::Function ) {
     339                        dst->base = src;
     340                        src = 0;
     341                } else {
     342                        dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
     343                }
     344        }
     345}
     346         
    332347DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) {
    333     if ( q ) {
    334         storageClasses.splice( storageClasses.end(), q->storageClasses );
    335         if ( q->type ) {
    336             if ( ! type ) {
    337                 type = new TypeData;
    338             }
    339             addQualifiersToType( q->type, type );
    340             if ( q->type && q->type->forall ) {
    341                 if ( type->forall ) {
    342                     type->forall->appendList( q->type->forall );
     348        if ( q ) {
     349                storageClasses.splice( storageClasses.end(), q->storageClasses );
     350                if ( q->type ) {
     351                        if ( ! type ) {
     352                                type = new TypeData;
     353                        }
     354                        addQualifiersToType( q->type, type );
     355                        if ( q->type && q->type->forall ) {
     356                                if ( type->forall ) {
     357                                        type->forall->appendList( q->type->forall );
     358                                } else {
     359                                        type->forall = q->type->forall;
     360                                }
     361                                q->type->forall = 0;
     362                        }
     363                }
     364        }
     365        delete q;
     366        return this;
     367}
     368
     369DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
     370        storageClasses = q->storageClasses;
     371        return this;
     372}
     373
     374static void addTypeToType( TypeData *&src, TypeData *&dst ) {
     375        if ( src && dst ) {
     376                if ( src->forall && dst->kind == TypeData::Function ) {
     377                        if ( dst->forall ) {
     378                                dst->forall->appendList( src->forall );
     379                        } else {
     380                                dst->forall = src->forall;
     381                        }
     382                        src->forall = 0;
     383                }
     384                if ( dst->base ) {
     385                        addTypeToType( src, dst->base );
    343386                } else {
    344                     type->forall = q->type->forall;
    345                 }
    346                 q->type->forall = 0;
    347             }
    348         }
    349     }
    350     delete q;
    351     return this;
    352 }
    353 
    354 DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
    355     storageClasses = q->storageClasses;
    356     return this;
    357 }
    358 
    359 static void addTypeToType( TypeData *&src, TypeData *&dst ) {
    360     if ( src && dst ) {
    361         if ( src->forall && dst->kind == TypeData::Function ) {
    362             if ( dst->forall ) {
    363                 dst->forall->appendList( src->forall );
    364             } else {
    365                 dst->forall = src->forall;
    366             }
    367             src->forall = 0;
    368         }
    369         if ( dst->base ) {
    370             addTypeToType( src, dst->base );
    371         } else {
    372             switch ( dst->kind ) {
    373               case TypeData::Unknown:
    374                 src->qualifiers.splice( src->qualifiers.end(), dst->qualifiers );
    375                 dst = src;
    376                 src = 0;
     387                        switch ( dst->kind ) {
     388                          case TypeData::Unknown:
     389                                src->qualifiers.splice( src->qualifiers.end(), dst->qualifiers );
     390                                dst = src;
     391                                src = 0;
     392                                break;
     393
     394                          case TypeData::Basic:
     395                                dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
     396                                if ( src->kind != TypeData::Unknown ) {
     397                                        assert( src->kind == TypeData::Basic );
     398                                        dst->basic->modifiers.splice( dst->basic->modifiers.end(), src->basic->modifiers );
     399                                        dst->basic->typeSpec.splice( dst->basic->typeSpec.end(), src->basic->typeSpec );
     400                                }
     401                                break;
     402
     403                          default:
     404                                switch ( src->kind ) {
     405                                  case TypeData::Aggregate:
     406                                  case TypeData::Enum:
     407                                        dst->base = new TypeData( TypeData::AggregateInst );
     408                                        dst->base->aggInst->aggregate = src;
     409                                        if ( src->kind == TypeData::Aggregate ) {
     410                                                dst->base->aggInst->params = maybeClone( src->aggregate->actuals );
     411                                        }
     412                                        dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers );
     413                                        src = 0;
     414                                        break;
     415
     416                                  default:
     417                                        if ( dst->forall ) {
     418                                                dst->forall->appendList( src->forall );
     419                                        } else {
     420                                                dst->forall = src->forall;
     421                                        }
     422                                        src->forall = 0;
     423                                        dst->base = src;
     424                                        src = 0;
     425                                }
     426                        }
     427                }
     428        }
     429}
     430
     431DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
     432        if ( o ) {
     433                storageClasses.splice( storageClasses.end(), o->storageClasses );
     434                if ( o->type ) {
     435                        if ( ! type ) {
     436                                if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
     437                                        type = new TypeData( TypeData::AggregateInst );
     438                                        type->aggInst->aggregate = o->type;
     439                                        if ( o->type->kind == TypeData::Aggregate ) {
     440                                                type->aggInst->params = maybeClone( o->type->aggregate->actuals );
     441                                        }
     442                                        type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers );
     443                                } else {
     444                                        type = o->type;
     445                                }
     446                                o->type = 0;
     447                        } else {
     448                                addTypeToType( o->type, type );
     449                        }
     450                }
     451                if ( o->bitfieldWidth ) {
     452                        bitfieldWidth = o->bitfieldWidth;
     453                }
     454        }
     455        delete o;
     456        return this;
     457}
     458
     459DeclarationNode *DeclarationNode::addTypedef() {
     460        TypeData *newtype = new TypeData( TypeData::Symbolic );
     461        newtype->symbolic->params = 0;
     462        newtype->symbolic->isTypedef = true;
     463        newtype->symbolic->name = name;
     464        newtype->base = type;
     465        type = newtype;
     466        return this;
     467}
     468
     469DeclarationNode *DeclarationNode::addAssertions( DeclarationNode* assertions ) {
     470        assert( type );
     471        switch ( type->kind ) {
     472          case TypeData::Symbolic:
     473                if ( type->symbolic->assertions ) {
     474                        type->symbolic->assertions->appendList( assertions );
     475                } else {
     476                        type->symbolic->assertions = assertions;
     477                }
    377478                break;
    378 
    379               case TypeData::Basic:
    380                 dst->qualifiers.splice( dst->qualifiers.end(), src->qualifiers );
    381                 if ( src->kind != TypeData::Unknown ) {
    382                     assert( src->kind == TypeData::Basic );
    383                     dst->basic->modifiers.splice( dst->basic->modifiers.end(), src->basic->modifiers );
    384                     dst->basic->typeSpec.splice( dst->basic->typeSpec.end(), src->basic->typeSpec );
     479       
     480          case TypeData::Variable:
     481                if ( type->variable->assertions ) {
     482                        type->variable->assertions->appendList( assertions );
     483                } else {
     484                        type->variable->assertions = assertions;
    385485                }
    386486                break;
    387 
    388               default:
    389                 switch ( src->kind ) {
    390                   case TypeData::Aggregate:
    391                   case TypeData::Enum:
    392                     dst->base = new TypeData( TypeData::AggregateInst );
    393                     dst->base->aggInst->aggregate = src;
    394                     if ( src->kind == TypeData::Aggregate ) {
    395                         dst->base->aggInst->params = maybeClone( src->aggregate->actuals );
    396                     }
    397                     dst->base->qualifiers.splice( dst->base->qualifiers.end(), src->qualifiers );
    398                     src = 0;
    399                     break;
    400          
    401                   default:
    402                     if ( dst->forall ) {
    403                         dst->forall->appendList( src->forall );
    404                     } else {
    405                         dst->forall = src->forall;
    406                     }
    407                     src->forall = 0;
    408                     dst->base = src;
    409                     src = 0;
    410                 }
    411             }
    412         }
    413     }
    414 }
    415 
    416 DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
    417     if ( o ) {
    418         storageClasses.splice( storageClasses.end(), o->storageClasses );
    419         if ( o->type ) {
    420             if ( ! type ) {
    421                 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
    422                     type = new TypeData( TypeData::AggregateInst );
    423                     type->aggInst->aggregate = o->type;
    424                     if ( o->type->kind == TypeData::Aggregate ) {
    425                         type->aggInst->params = maybeClone( o->type->aggregate->actuals );
    426                     }
    427                     type->qualifiers.splice( type->qualifiers.end(), o->type->qualifiers );
    428                 } else {
    429                     type = o->type;
    430                 }
    431                 o->type = 0;
    432             } else {
    433                 addTypeToType( o->type, type );
    434             }
    435         }
    436         if ( o->bitfieldWidth ) {
    437             bitfieldWidth = o->bitfieldWidth;
    438         }
    439     }
    440     delete o;
    441     return this;
    442 }
    443 
    444 DeclarationNode *DeclarationNode::addTypedef() {
    445     TypeData *newtype = new TypeData( TypeData::Symbolic );
    446     newtype->symbolic->params = 0;
    447     newtype->symbolic->isTypedef = true;
    448     newtype->symbolic->name = name;
    449     newtype->base = type;
    450     type = newtype;
    451     return this;
    452 }
    453 
    454 DeclarationNode *DeclarationNode::addAssertions( DeclarationNode* assertions ) {
    455     assert( type );
    456     switch ( type->kind ) {
    457       case TypeData::Symbolic:
    458         if ( type->symbolic->assertions ) {
    459             type->symbolic->assertions->appendList( assertions );
    460         } else {
    461             type->symbolic->assertions = assertions;
    462         }
    463         break;
    464    
    465       case TypeData::Variable:
    466         if ( type->variable->assertions ) {
    467             type->variable->assertions->appendList( assertions );
    468         } else {
    469             type->variable->assertions = assertions;
    470         }
    471         break;
    472    
    473       default:
    474         assert( false );
    475     }
    476    
    477     return this;
     487       
     488          default:
     489                assert( false );
     490        }
     491       
     492        return this;
    478493}
    479494
    480495DeclarationNode *DeclarationNode::addName( std::string* newname ) {
    481     name = assign_strptr( newname );
    482     return this;
     496        name = assign_strptr( newname );
     497        return this;
    483498}
    484499
    485500DeclarationNode *DeclarationNode::addBitfield( ExpressionNode *size ) {
    486     bitfieldWidth = size;
    487     return this;
     501        bitfieldWidth = size;
     502        return this;
    488503}
    489504
    490505DeclarationNode *DeclarationNode::addVarArgs() {
    491     assert( type );
    492     hasEllipsis = true;
    493     return this;
     506        assert( type );
     507        hasEllipsis = true;
     508        return this;
    494509}
    495510
    496511DeclarationNode *DeclarationNode::addFunctionBody( StatementNode *body ) {
    497     assert( type );
    498     assert( type->kind == TypeData::Function );
    499     assert( type->function->body == 0 );
    500     type->function->body = body;
    501     type->function->hasBody = true;
    502     return this;
     512        assert( type );
     513        assert( type->kind == TypeData::Function );
     514        assert( type->function->body == 0 );
     515        type->function->body = body;
     516        type->function->hasBody = true;
     517        return this;
    503518}
    504519
    505520DeclarationNode *DeclarationNode::addOldDeclList( DeclarationNode *list ) {
    506     assert( type );
    507     assert( type->kind == TypeData::Function );
    508     assert( type->function->oldDeclList == 0 );
    509     type->function->oldDeclList = list;
    510     return this;
     521        assert( type );
     522        assert( type->kind == TypeData::Function );
     523        assert( type->function->oldDeclList == 0 );
     524        type->function->oldDeclList = list;
     525        return this;
    511526}
    512527
    513528static void
    514529setBase( TypeData *&type, TypeData *newType ) {
    515     if ( type ) {
    516         TypeData *prevBase = type;
    517         TypeData *curBase = type->base;
    518         while( curBase != 0 ) {
    519             prevBase = curBase;
    520             curBase = curBase->base;
    521         }
    522         prevBase->base = newType;
    523     } else {
    524         type = newType;
    525     }
     530        if ( type ) {
     531                TypeData *prevBase = type;
     532                TypeData *curBase = type->base;
     533                while( curBase != 0 ) {
     534                        prevBase = curBase;
     535                        curBase = curBase->base;
     536                }
     537                prevBase->base = newType;
     538        } else {
     539                type = newType;
     540        }
    526541}
    527542
    528543DeclarationNode *DeclarationNode::addPointer( DeclarationNode *p ) {
    529     if ( p ) {
    530         assert( p->type->kind == TypeData::Pointer );
    531         setBase( type, p->type );
    532         p->type = 0;
    533         delete p;
    534     }
    535     return this;
     544        if ( p ) {
     545                assert( p->type->kind == TypeData::Pointer );
     546                setBase( type, p->type );
     547                p->type = 0;
     548                delete p;
     549        }
     550        return this;
    536551}
    537552
    538553DeclarationNode *DeclarationNode::addArray( DeclarationNode *a ) {
    539     if ( a ) {
    540         assert( a->type->kind == TypeData::Array );
    541         setBase( type, a->type );
    542         a->type = 0;
    543         delete a;
    544     }
    545     return this;
     554        if ( a ) {
     555                assert( a->type->kind == TypeData::Array );
     556                setBase( type, a->type );
     557                a->type = 0;
     558                delete a;
     559        }
     560        return this;
    546561}
    547562
    548563DeclarationNode *DeclarationNode::addNewPointer( DeclarationNode *p ) {
    549     if ( p ) {
    550         assert( p->type->kind == TypeData::Pointer );
     564        if ( p ) {
     565                assert( p->type->kind == TypeData::Pointer );
     566                if ( type ) {
     567                        switch ( type->kind ) {
     568                          case TypeData::Aggregate:
     569                          case TypeData::Enum:
     570                                p->type->base = new TypeData( TypeData::AggregateInst );
     571                                p->type->base->aggInst->aggregate = type;
     572                                if ( type->kind == TypeData::Aggregate ) {
     573                                        p->type->base->aggInst->params = maybeClone( type->aggregate->actuals );
     574                                }
     575                                p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers );
     576                                break;
     577
     578                          default:
     579                                p->type->base = type;
     580                        }
     581                        type = 0;
     582                }
     583                delete this;
     584                return p;
     585        } else {
     586                return this;
     587        }
     588}
     589
     590static TypeData *findLast( TypeData *a ) {
     591        assert( a );
     592        TypeData *cur = a;
     593        while( cur->base ) {
     594                cur = cur->base;
     595        }
     596        return cur;
     597}
     598
     599DeclarationNode *DeclarationNode::addNewArray( DeclarationNode *a ) {
     600        if ( a ) {
     601                assert( a->type->kind == TypeData::Array );
     602                TypeData *lastArray = findLast( a->type );
     603                if ( type ) { 
     604                        switch ( type->kind ) {
     605                          case TypeData::Aggregate:
     606                          case TypeData::Enum:
     607                                lastArray->base = new TypeData( TypeData::AggregateInst );
     608                                lastArray->base->aggInst->aggregate = type;
     609                                if ( type->kind == TypeData::Aggregate ) {
     610                                        lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals );
     611                                }
     612                                lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers );
     613                                break;
     614                          default:
     615                                lastArray->base = type;
     616                        }
     617                        type = 0;
     618                }
     619                delete this;
     620                return a;
     621        } else {
     622                return this;
     623        }
     624}
     625
     626DeclarationNode *DeclarationNode::addParamList( DeclarationNode *params ) {
     627        TypeData *ftype = new TypeData( TypeData::Function );
     628        ftype->function->params = params;
     629        setBase( type, ftype );
     630        return this;
     631}
     632
     633static TypeData *addIdListToType( TypeData *type, DeclarationNode *ids ) {
    551634        if ( type ) {
    552             switch ( type->kind ) {
    553               case TypeData::Aggregate:
    554               case TypeData::Enum:
    555                 p->type->base = new TypeData( TypeData::AggregateInst );
    556                 p->type->base->aggInst->aggregate = type;
    557                 if ( type->kind == TypeData::Aggregate ) {
    558                     p->type->base->aggInst->params = maybeClone( type->aggregate->actuals );
    559                 }
    560                 p->type->base->qualifiers.splice( p->type->base->qualifiers.end(), type->qualifiers );
    561                 break;
    562        
    563               default:
    564                 p->type->base = type;
    565             }
    566             type = 0;
    567         }
    568         delete this;
    569         return p;
    570     } else {
    571         return this;
    572     }
    573 }
    574 
    575 static TypeData *findLast( TypeData *a ) {
    576     assert( a );
    577     TypeData *cur = a;
    578     while( cur->base ) {
    579         cur = cur->base;
    580     }
    581     return cur;
    582 }
    583 
    584 DeclarationNode *DeclarationNode::addNewArray( DeclarationNode *a ) {
    585     if ( a ) {
    586         assert( a->type->kind == TypeData::Array );
    587         TypeData *lastArray = findLast( a->type );
    588         if ( type ) { 
    589             switch ( type->kind ) {
    590               case TypeData::Aggregate:
    591               case TypeData::Enum:
    592                 lastArray->base = new TypeData( TypeData::AggregateInst );
    593                 lastArray->base->aggInst->aggregate = type;
    594                 if ( type->kind == TypeData::Aggregate ) {
    595                     lastArray->base->aggInst->params = maybeClone( type->aggregate->actuals );
    596                 }
    597                 lastArray->base->qualifiers.splice( lastArray->base->qualifiers.end(), type->qualifiers );
    598                 break;
    599        
    600               default:
    601                 lastArray->base = type;
    602             }
    603             type = 0;
    604         }
    605         delete this;
    606         return a;
    607     } else {
    608         return this;
    609     }
    610 }
    611 
    612 DeclarationNode *DeclarationNode::addParamList( DeclarationNode *params ) {
    613     TypeData *ftype = new TypeData( TypeData::Function );
    614     ftype->function->params = params;
    615     setBase( type, ftype );
    616     return this;
    617 }
    618 
    619 static TypeData *addIdListToType( TypeData *type, DeclarationNode *ids ) {
    620     if ( type ) {
    621         if ( type->kind != TypeData::Function ) {
    622             type->base = addIdListToType( type->base, ids );
     635                if ( type->kind != TypeData::Function ) {
     636                        type->base = addIdListToType( type->base, ids );
     637                } else {
     638                        type->function->idList = ids;
     639                }
     640                return type;
    623641        } else {
    624             type->function->idList = ids;
    625         }
    626         return type;
    627     } else {
    628         TypeData *newtype = new TypeData( TypeData::Function );
    629         newtype->function->idList = ids;
    630         return newtype;
    631     }
    632 }
    633    
     642                TypeData *newtype = new TypeData( TypeData::Function );
     643                newtype->function->idList = ids;
     644                return newtype;
     645        }
     646}
     647       
    634648DeclarationNode *DeclarationNode::addIdList( DeclarationNode *ids ) {
    635     type = addIdListToType( type, ids );
    636     return this;
     649        type = addIdListToType( type, ids );
     650        return this;
    637651}
    638652
    639653DeclarationNode *DeclarationNode::addInitializer( InitializerNode *init ) {
    640     //assert
    641     initializer = init;
    642     return this;
     654        //assert
     655        initializer = init;
     656        return this;
    643657}
    644658
    645659DeclarationNode *DeclarationNode::cloneBaseType( string *newName ) {
    646     DeclarationNode *newnode = new DeclarationNode;
    647     TypeData *srcType = type;
    648     while( srcType->base ) {
    649         srcType = srcType->base;
    650     }
    651     newnode->type = maybeClone( srcType );
    652     if ( newnode->type->kind == TypeData::AggregateInst ) {
    653         // don't duplicate members
    654         if ( newnode->type->aggInst->aggregate->kind == TypeData::Enum ) {
    655             delete newnode->type->aggInst->aggregate->enumeration->constants;
    656             newnode->type->aggInst->aggregate->enumeration->constants = 0;
     660        DeclarationNode *newnode = new DeclarationNode;
     661        TypeData *srcType = type;
     662        while( srcType->base ) {
     663                srcType = srcType->base;
     664        }
     665        newnode->type = maybeClone( srcType );
     666        if ( newnode->type->kind == TypeData::AggregateInst ) {
     667                // don't duplicate members
     668                if ( newnode->type->aggInst->aggregate->kind == TypeData::Enum ) {
     669                        delete newnode->type->aggInst->aggregate->enumeration->constants;
     670                        newnode->type->aggInst->aggregate->enumeration->constants = 0;
     671                } else {
     672                        assert( newnode->type->aggInst->aggregate->kind == TypeData::Aggregate );
     673                        delete newnode->type->aggInst->aggregate->aggregate->members;
     674                        newnode->type->aggInst->aggregate->aggregate->members = 0;
     675                }
     676        }
     677        newnode->type->forall = maybeClone( type->forall );
     678        newnode->storageClasses = storageClasses;
     679        newnode->name = assign_strptr( newName );
     680        return newnode;
     681}
     682
     683DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) {
     684        if ( o ) {
     685                o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
     686                if ( type ) {
     687                        TypeData *srcType = type;
     688                        while( srcType->base ) {
     689                                srcType = srcType->base;
     690                        }
     691                        TypeData *newType = srcType->clone();
     692                        if ( newType->kind == TypeData::AggregateInst ) {
     693                                // don't duplicate members
     694                                if ( newType->aggInst->aggregate->kind == TypeData::Enum ) {
     695                                        delete newType->aggInst->aggregate->enumeration->constants;
     696                                        newType->aggInst->aggregate->enumeration->constants = 0;
     697                                } else {
     698                                        assert( newType->aggInst->aggregate->kind == TypeData::Aggregate );
     699                                        delete newType->aggInst->aggregate->aggregate->members;
     700                                        newType->aggInst->aggregate->aggregate->members = 0;
     701                                }
     702                        }
     703                        newType->forall = maybeClone( type->forall );
     704                        if ( ! o->type ) {
     705                                o->type = newType;
     706                        } else {
     707                                addTypeToType( newType, o->type );
     708                                delete newType;
     709                        }
     710                }
     711        }
     712        return o;
     713}
     714
     715DeclarationNode *DeclarationNode::cloneType( string *newName ) {
     716        DeclarationNode *newnode = new DeclarationNode;
     717        newnode->type = maybeClone( type );
     718        newnode->storageClasses = storageClasses;
     719        newnode->name = assign_strptr( newName );
     720        return newnode;
     721}
     722
     723DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) {
     724        if ( o ) {
     725                o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
     726                if ( type ) {
     727                        TypeData *newType = type->clone();
     728                        if ( ! o->type ) {
     729                                o->type = newType;
     730                        } else {
     731                                addTypeToType( newType, o->type );
     732                                delete newType;
     733                        }
     734                }
     735        }
     736        return o;
     737}
     738
     739DeclarationNode *DeclarationNode::appendList( DeclarationNode *node ) {
     740        if ( node != 0 ) {
     741                set_link( node );
     742        }
     743        return this;
     744}
     745
     746DeclarationNode *DeclarationNode::extractAggregate() const {
     747        if ( type ) {
     748                TypeData *ret = type->extractAggregate();
     749                if ( ret ) {
     750                        DeclarationNode *newnode = new DeclarationNode;
     751                        newnode->type = ret;
     752                        return newnode;
     753                } else {
     754                        return 0;
     755                }
    657756        } else {
    658             assert( newnode->type->aggInst->aggregate->kind == TypeData::Aggregate );
    659             delete newnode->type->aggInst->aggregate->aggregate->members;
    660             newnode->type->aggInst->aggregate->aggregate->members = 0;
    661         }
    662     }
    663     newnode->type->forall = maybeClone( type->forall );
    664     newnode->storageClasses = storageClasses;
    665     newnode->name = assign_strptr( newName );
    666     return newnode;
    667 }
    668 
    669 DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) {
    670     if ( o ) {
    671         o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
    672         if ( type ) {
    673             TypeData *srcType = type;
    674             while( srcType->base ) {
    675                 srcType = srcType->base;
    676             }
    677             TypeData *newType = srcType->clone();
    678             if ( newType->kind == TypeData::AggregateInst ) {
    679                 // don't duplicate members
    680                 if ( newType->aggInst->aggregate->kind == TypeData::Enum ) {
    681                     delete newType->aggInst->aggregate->enumeration->constants;
    682                     newType->aggInst->aggregate->enumeration->constants = 0;
    683                 } else {
    684                     assert( newType->aggInst->aggregate->kind == TypeData::Aggregate );
    685                     delete newType->aggInst->aggregate->aggregate->members;
    686                     newType->aggInst->aggregate->aggregate->members = 0;
    687                 }
    688             }
    689             newType->forall = maybeClone( type->forall );
    690             if ( ! o->type ) {
    691                 o->type = newType;
    692             } else {
    693                 addTypeToType( newType, o->type );
    694                 delete newType;
    695             }
    696         }
    697     }
    698     return o;
    699 }
    700 
    701 DeclarationNode *DeclarationNode::cloneType( string *newName ) {
    702     DeclarationNode *newnode = new DeclarationNode;
    703     newnode->type = maybeClone( type );
    704     newnode->storageClasses = storageClasses;
    705     newnode->name = assign_strptr( newName );
    706     return newnode;
    707 }
    708 
    709 DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) {
    710     if ( o ) {
    711         o->storageClasses.insert( o->storageClasses.end(), storageClasses.begin(), storageClasses.end() );
    712         if ( type ) {
    713             TypeData *newType = type->clone();
    714             if ( ! o->type ) {
    715                 o->type = newType;
    716             } else {
    717                 addTypeToType( newType, o->type );
    718                 delete newType;
    719             }
    720         }
    721     }
    722     return o;
    723 }
    724 
    725 DeclarationNode *DeclarationNode::appendList( DeclarationNode *node ) {
    726     if ( node != 0 ) {
    727         set_link( node );
    728     }
    729     return this;
    730 }
    731 
    732 DeclarationNode *DeclarationNode::extractAggregate() const {
    733     if ( type ) {
    734         TypeData *ret = type->extractAggregate();
    735         if ( ret ) {
    736             DeclarationNode *newnode = new DeclarationNode;
    737             newnode->type = ret;
    738             return newnode;
    739         } else {
    740             return 0;
    741         }
    742     } else {
    743         return 0;
    744     }
     757                return 0;
     758        }
    745759}
    746760
    747761void buildList( const DeclarationNode *firstNode, std::list< Declaration* > &outputList ) {
    748     SemanticError errors;
    749     std::back_insert_iterator< std::list< Declaration* > > out( outputList );
    750     const DeclarationNode *cur = firstNode;
    751     while( cur ) {
    752         try {
    753             if ( DeclarationNode *extr = cur->extractAggregate() ) {
    754                 // handle the case where a structure declaration is contained within an object or type
    755                 // declaration
    756                 Declaration *decl = extr->build();
    757                 if ( decl ) {
    758                    *out++ = decl;
    759                 }
    760             }
    761             Declaration *decl = cur->build();
    762             if ( decl ) {
    763                 *out++ = decl;
    764             }
    765         } catch( SemanticError &e ) {
    766             errors.append( e );
    767         }
    768         cur = dynamic_cast< DeclarationNode* >( cur->get_link() );
    769     }
    770     if ( ! errors.isEmpty() ) {
    771         throw errors;
    772     }
     762        SemanticError errors;
     763        std::back_insert_iterator< std::list< Declaration* > > out( outputList );
     764        const DeclarationNode *cur = firstNode;
     765        while( cur ) {
     766                try {
     767                        if ( DeclarationNode *extr = cur->extractAggregate() ) {
     768                                // handle the case where a structure declaration is contained within an object or type declaration
     769                                Declaration *decl = extr->build();
     770                                if ( decl ) {
     771                                        *out++ = decl;
     772                                }
     773                        }
     774                        Declaration *decl = cur->build();
     775                        if ( decl ) {
     776                                *out++ = decl;
     777                        }
     778                } catch( SemanticError &e ) {
     779                        errors.append( e );
     780                }
     781                cur = dynamic_cast< DeclarationNode* >( cur->get_link() );
     782        }
     783        if ( ! errors.isEmpty() ) {
     784                throw errors;
     785        }
    773786}
    774787
    775788void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType* > &outputList ) {
    776     SemanticError errors;
    777     std::back_insert_iterator< std::list< DeclarationWithType* > > out( outputList );
    778     const DeclarationNode *cur = firstNode;
    779     while( cur ) {
    780         try {
     789        SemanticError errors;
     790        std::back_insert_iterator< std::list< DeclarationWithType* > > out( outputList );
     791        const DeclarationNode *cur = firstNode;
     792        while( cur ) {
     793                try {
    781794///       if ( DeclarationNode *extr = cur->extractAggregate() ) {
    782795///     // handle the case where a structure declaration is contained within an object or type
     
    787800///     }
    788801///       }
    789             Declaration *decl = cur->build();
    790             if ( decl ) {
    791                 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( decl ) ) {
    792                    *out++ = dwt;
    793                 } else if ( StructDecl *agg = dynamic_cast< StructDecl* >( decl ) ) {
    794                     StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    795                     *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
    796                     delete agg;
    797                 } else if ( UnionDecl *agg = dynamic_cast< UnionDecl* >( decl ) ) {
    798                     UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    799                     *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
    800                 }
    801             }
    802         } catch( SemanticError &e ) {
    803             errors.append( e );
    804         }
    805         cur = dynamic_cast< DeclarationNode* >( cur->get_link() );
    806     }
    807     if ( ! errors.isEmpty() ) {
    808         throw errors;
    809     }
     802                        Declaration *decl = cur->build();
     803                        if ( decl ) {
     804                                if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( decl ) ) {
     805                                        *out++ = dwt;
     806                                } else if ( StructDecl *agg = dynamic_cast< StructDecl* >( decl ) ) {
     807                                        StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
     808                                        *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
     809                                        delete agg;
     810                                } else if ( UnionDecl *agg = dynamic_cast< UnionDecl* >( decl ) ) {
     811                                        UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
     812                                        *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
     813                                }
     814                        }
     815                } catch( SemanticError &e ) {
     816                        errors.append( e );
     817                }
     818                cur = dynamic_cast< DeclarationNode* >( cur->get_link() );
     819        }
     820        if ( ! errors.isEmpty() ) {
     821                throw errors;
     822        }
    810823}
    811824
    812825void buildTypeList( const DeclarationNode *firstNode, std::list< Type* > &outputList ) {
    813     SemanticError errors;
    814     std::back_insert_iterator< std::list< Type* > > out( outputList );
    815     const DeclarationNode *cur = firstNode;
    816     while( cur ) {
    817         try {
    818            *out++ = cur->buildType();
    819         } catch( SemanticError &e ) {
    820             errors.append( e );
    821         }
    822         cur = dynamic_cast< DeclarationNode* >( cur->get_link() );
    823     }
    824     if ( ! errors.isEmpty() ) {
    825         throw errors;
    826     }
     826        SemanticError errors;
     827        std::back_insert_iterator< std::list< Type* > > out( outputList );
     828        const DeclarationNode *cur = firstNode;
     829        while( cur ) {
     830                try {
     831                        *out++ = cur->buildType();
     832                } catch( SemanticError &e ) {
     833                        errors.append( e );
     834                }
     835                cur = dynamic_cast< DeclarationNode* >( cur->get_link() );
     836        }
     837        if ( ! errors.isEmpty() ) {
     838                throw errors;
     839        }
    827840}
    828841
    829842Declaration *DeclarationNode::build() const {
    830843
    831     if ( ! type ) {
    832         if ( buildInline() ) {
    833             throw SemanticError( "invalid inline specification in declaration of ", this );
     844        if ( ! type ) {
     845                if ( buildInline() ) {
     846                        throw SemanticError( "invalid inline specification in declaration of ", this );
     847                } else {
     848                        return new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) );
     849                }
    834850        } else {
    835             return new ObjectDecl( name, buildStorageClass(), linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) );
    836         }
    837     } else {
    838         Declaration *newDecl = type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildInline(), linkage, maybeBuild< Initializer >(initializer) );
    839         return newDecl;
    840     }
    841     // we should never get here
    842     assert( false );
    843     return 0;
     851                Declaration *newDecl = type->buildDecl( name, buildStorageClass(), maybeBuild< Expression >( bitfieldWidth ), buildInline(), linkage, maybeBuild< Initializer >(initializer) );
     852                return newDecl;
     853        }
     854        // we should never get here
     855        assert( false );
     856        return 0;
    844857}
    845858
    846859Type *DeclarationNode::buildType() const {
    847     assert( type );
     860        assert( type );
    848861 
    849     switch ( type->kind ) {
    850       case TypeData::Enum:
    851         return new EnumInstType( type->buildQualifiers(), type->enumeration->name );
    852       case TypeData::Aggregate: {
    853           ReferenceToType *ret;
    854           switch ( type->aggregate->kind ) {
    855             case DeclarationNode::Struct:
    856               ret = new StructInstType( type->buildQualifiers(), type->aggregate->name );
    857               break;
    858             case DeclarationNode::Union:
    859               ret = new UnionInstType( type->buildQualifiers(), type->aggregate->name );
    860               break;
    861             case DeclarationNode::Context:
    862               ret = new ContextInstType( type->buildQualifiers(), type->aggregate->name );
    863               break;
    864             default:
    865               assert( false );
    866           } // switch
    867           buildList( type->aggregate->actuals, ret->get_parameters() );
    868           return ret;
    869       }
    870       case TypeData::Symbolic: {
    871           TypeInstType *ret = new TypeInstType( type->buildQualifiers(), type->symbolic->name, false );
    872           buildList( type->symbolic->actuals, ret->get_parameters() );
    873           return ret;
    874       }
    875       default:
    876         return type->build();
    877     } // switch
     862        switch ( type->kind ) {
     863          case TypeData::Enum:
     864                return new EnumInstType( type->buildQualifiers(), type->enumeration->name );
     865          case TypeData::Aggregate: {
     866                  ReferenceToType *ret;
     867                  switch ( type->aggregate->kind ) {
     868                        case DeclarationNode::Struct:
     869                          ret = new StructInstType( type->buildQualifiers(), type->aggregate->name );
     870                          break;
     871                        case DeclarationNode::Union:
     872                          ret = new UnionInstType( type->buildQualifiers(), type->aggregate->name );
     873                          break;
     874                        case DeclarationNode::Context:
     875                          ret = new ContextInstType( type->buildQualifiers(), type->aggregate->name );
     876                          break;
     877                        default:
     878                          assert( false );
     879                  } // switch
     880                  buildList( type->aggregate->actuals, ret->get_parameters() );
     881                  return ret;
     882          }
     883          case TypeData::Symbolic: {
     884                  TypeInstType *ret = new TypeInstType( type->buildQualifiers(), type->symbolic->name, false );
     885                  buildList( type->symbolic->actuals, ret->get_parameters() );
     886                  return ret;
     887          }
     888          default:
     889                return type->build();
     890        } // switch
    878891}
    879892
    880893Declaration::StorageClass DeclarationNode::buildStorageClass() const {
    881     static const Declaration::StorageClass scMap[] = { 
    882         Declaration::Extern,
    883         Declaration::Static,
    884         Declaration::Auto,
    885         Declaration::Register,
    886         Declaration::Inline,
    887         Declaration::Fortran
    888     }; 
     894        static const Declaration::StorageClass scMap[] = { 
     895                Declaration::Extern,
     896                Declaration::Static,
     897                Declaration::Auto,
     898                Declaration::Register,
     899                Declaration::Inline,
     900                Declaration::Fortran
     901        }; 
    889902 
    890     Declaration::StorageClass ret = Declaration::NoStorageClass;
    891     for ( std::list< StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
    892         assert( unsigned( *i ) < sizeof( scMap ) / sizeof( scMap[0] ) );
    893         if ( *i == Inline ) continue;
    894         if ( ret == Declaration::NoStorageClass ) {
    895             ret = scMap[ *i ];
     903        Declaration::StorageClass ret = Declaration::NoStorageClass;
     904        for ( std::list< StorageClass >::const_iterator i = storageClasses.begin(); i != storageClasses.end(); ++i ) {
     905                assert( unsigned( *i ) < sizeof( scMap ) / sizeof( scMap[0] ) );
     906                if ( *i == Inline ) continue;
     907                if ( ret == Declaration::NoStorageClass ) {
     908                        ret = scMap[ *i ];
     909                } else {
     910                        throw SemanticError( "invalid combination of storage classes in declaration of ", this );
     911                }
     912        }
     913        return ret;
     914}
     915
     916bool DeclarationNode::buildInline() const {
     917        std::list< StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), Inline );
     918        if ( first == storageClasses.end() ) {
     919                return false;
    896920        } else {
    897             throw SemanticError( "invalid combination of storage classes in declaration of ", this );
    898         }
    899     }
    900     return ret;
    901 }
    902 
    903 bool DeclarationNode::buildInline() const {
    904     std::list< StorageClass >::const_iterator first = std::find( storageClasses.begin(), storageClasses.end(), Inline );
    905     if ( first == storageClasses.end() ) {
     921                std::list< StorageClass >::const_iterator next = std::find( ++first, storageClasses.end(), Inline );
     922                if ( next == storageClasses.end() ) {
     923                        return true;
     924                } else {
     925                        throw SemanticError( "duplicate inline specification in declaration of ", this );
     926                }
     927        }
     928        // we should never get here
    906929        return false;
    907     } else {
    908         std::list< StorageClass >::const_iterator next = std::find( ++first, storageClasses.end(), Inline );
    909         if ( next == storageClasses.end() ) {
    910             return true;
    911         } else {
    912             throw SemanticError( "duplicate inline specification in declaration of ", this );
    913         }
    914     }
    915     // we should never get here
    916     return false;
    917 }
     930}
     931
     932// Local Variables: //
     933// tab-width: 4 //
     934// mode: c++ //
     935// compile-command: "make install" //
     936// End: //
Note: See TracChangeset for help on using the changeset viewer.