Ignore:
Timestamp:
Sep 12, 2016, 9:52:12 PM (8 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
101e0bd
Parents:
b6424d9
Message:

more refactoring of parser code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    rb6424d9 r5b639ee  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Sep 11 09:24:11 2016
    13 // Update Count     : 438
     12// Last Modified On : Mon Sep 12 21:03:18 2016
     13// Update Count     : 491
    1414//
    1515
     
    3232// These must remain in the same order as the corresponding DeclarationNode enumerations.
    3333const char *DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "NoStorageClass" };
    34 const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic" };
    35 const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary",  };
    36 const char *DeclarationNode::modifierName[]  = { "signed", "unsigned", "short", "long" };
     34const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoQualifier" };
     35const char *DeclarationNode::basicTypeName[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicType" };
     36const char *DeclarationNode::complexTypeName[] = { "_Complex", "_Imaginary", "NoComplexType" };
     37const char *DeclarationNode::signednessName[] = { "signed", "unsigned", "NoSignedness" };
     38const char *DeclarationNode::lengthName[] = { "short", "long", "long long", "NoLength" };
    3739const char *DeclarationNode::aggregateName[] = { "struct", "union", "context" };
    38 const char *DeclarationNode::typeClassName[] = { "type", "dtype", "ftype" };
     40const char *DeclarationNode::typeClassName[] = { "otype", "dtype", "ftype" };
    3941const char *DeclarationNode::builtinTypeName[] = { "__builtin_va_list" };
    4042
     
    5355                linkage( ::linkage ),
    5456                extension( false ) {
    55         variable.tyClass = DeclarationNode::Type;
     57        variable.tyClass = DeclarationNode::Otype;
    5658        variable.assertions = nullptr;
    5759
     
    188190        DeclarationNode *newnode = new DeclarationNode;
    189191        newnode->type = new TypeData( TypeData::Basic );
    190         newnode->type->basic.typeSpec.push_back( bt );
     192        newnode->type->basictype = bt;
    191193        return newnode;
    192194} // DeclarationNode::newBasicType
    193195
    194 DeclarationNode * DeclarationNode::newModifier( Modifier mod ) {
     196DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
    195197        DeclarationNode *newnode = new DeclarationNode;
    196198        newnode->type = new TypeData( TypeData::Basic );
    197         newnode->type->basic.modifiers.push_back( mod );
    198         return newnode;
    199 } // DeclarationNode::newModifier
     199        newnode->type->complextype = ct;
     200        return newnode;
     201} // DeclarationNode::newComplexType
     202
     203DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
     204        DeclarationNode *newnode = new DeclarationNode;
     205        newnode->type = new TypeData( TypeData::Basic );
     206        newnode->type->signedness = sn;
     207        return newnode;
     208} // DeclarationNode::newSignedNess
     209
     210DeclarationNode * DeclarationNode::newLength( Length lnth ) {
     211        DeclarationNode *newnode = new DeclarationNode;
     212        newnode->type = new TypeData( TypeData::Basic );
     213        newnode->type->length = lnth;
     214        return newnode;
     215} // DeclarationNode::newLength
    200216
    201217DeclarationNode * DeclarationNode::newFromTypedef( std::string *name ) {
     
    388404}
    389405
     406void appendError( string & dst, const string & src ) {
     407        if ( src.empty() ) return;
     408        if ( dst.empty() ) { dst = src; return; }
     409        dst += ", " + src;
     410} // appendError
     411
    390412void DeclarationNode::checkQualifiers( const TypeData *src, const TypeData *dst ) {
    391413        TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
    392414
    393415        if ( (qsrc & qdst).any() ) {                                            // common qualifier ?
    394                 for ( int i = 0; i < NoOfQualifier; i += 1 ) {  // find common qualifiers
    395                         if ( qsrc[i] & qdst[i] ) {
    396                                 if ( ! error.empty() ) error += ", ";   // separator
    397                                 error += string( "duplicate " ) + DeclarationNode::qualifierName[i];
     416                for ( int i = 0; i < NoQualifier; i += 1 ) {    // find common qualifiers
     417                        if ( qsrc[i] && qdst[i] ) {
     418                                appendError( error, string( "duplicate " ) + DeclarationNode::qualifierName[i] );
    398419                        } // if
    399420                } // for
     
    403424void DeclarationNode::checkStorageClasses( DeclarationNode *q ) {
    404425        if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) {
    405                 if ( ! error.empty() ) error += ", ";                   // separator
    406426                if ( storageClass == q->storageClass ) {                // duplicate qualifier
    407                         error += string( "duplicate " ) + storageName[ storageClass ];
     427                        appendError( error, string( "duplicate " ) + storageName[ storageClass ] );
    408428                } else {                                                                                // only one storage class
    409                         error += string( "conflicting " ) + storageName[ storageClass ] + " & " + storageName[ q->storageClass ];
    410                         q->storageClass = storageClass;                         // FIX ERROR
    411                 } // if
    412                 if ( ! q->error.empty() ) error += ", " + q->error;     // separator
    413         } else {
    414                 if ( ! error.empty() ) {
    415                         if ( ! q->error.empty() ) error += ", " + q->error; // separator
    416                 } else if ( ! q->error.empty() ) error += q->error;
    417         } // if
     429                        appendError( error, string( "conflicting " ) + storageName[ storageClass ] + " & " + storageName[ q->storageClass ] );
     430                        q->storageClass = storageClass;                         // FIX ERROR, prevent assertions from triggering
     431                } // if
     432        } // if
     433        appendError( error, q->error );
    418434} // DeclarationNode::copyStorageClasses
    419435
    420436DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
    421         isInline = isInline | q->isInline;
    422         isNoreturn = isNoreturn | q->isNoreturn;
     437        isInline = isInline || q->isInline;
     438        isNoreturn = isNoreturn || q->isNoreturn;
    423439        // do not overwrite an existing value with NoStorageClass
    424440        if ( q->storageClass != NoStorageClass ) {
     
    483499                                if ( src->kind != TypeData::Unknown ) {
    484500                                        assert( src->kind == TypeData::Basic );
    485                                         dst->basic.modifiers.splice( dst->basic.modifiers.end(), src->basic.modifiers );
    486                                         dst->basic.typeSpec.splice( dst->basic.typeSpec.end(), src->basic.typeSpec );
     501
     502                                        if ( dst->basictype == DeclarationNode::NoBasicType ) {
     503                                                dst->basictype = src->basictype;
     504                                        } else if ( src->basictype != DeclarationNode::NoBasicType )
     505                                                throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src );
     506
     507                                        if ( dst->complextype == DeclarationNode::NoComplexType ) {
     508                                                dst->complextype = src->complextype;
     509                                        } else if ( src->complextype != DeclarationNode::NoComplexType )
     510                                                throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src );
     511
     512                                        if ( dst->signedness == DeclarationNode::NoSignedness ) {
     513                                                dst->signedness = src->signedness;
     514                                        } else if ( src->signedness != DeclarationNode::NoSignedness )
     515                                                throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src );
     516
     517                                        if ( dst->length == DeclarationNode::NoLength ) {
     518                                                dst->length = src->length;
     519                                        } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
     520                                                dst->length = DeclarationNode::LongLong;
     521                                        } else if ( src->length != DeclarationNode::NoLength )
     522                                                throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src );
    487523                                } // if
    488524                                break;
     
    875911        while ( cur ) {
    876912                try {
    877 ///       if ( DeclarationNode *extr = cur->extractAggregate() ) {
    878 ///     // handle the case where a structure declaration is contained within an object or type
    879 ///     // declaration
    880 ///     Declaration *decl = extr->build();
    881 ///     if ( decl ) {
    882 ///          *out++ = decl;
    883 ///     }
    884 ///       }
    885913                        Declaration *decl = cur->build();
    886914                        if ( decl ) {
Note: See TracChangeset for help on using the changeset viewer.