Changeset 692c1cc


Ignore:
Timestamp:
Feb 16, 2023, 4:44:53 PM (14 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, master
Children:
14f6a3cb
Parents:
f8729be
Message:

update printing attributes, clean up anon flag setting, move attribute transparent_union from typedef to its union alias

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    rf8729be r692c1cc  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Aug  8 17:07:00 2022
    13 // Update Count     : 1185
     12// Last Modified On : Thu Feb 16 14:12:03 2023
     13// Update Count     : 1388
    1414//
    1515
     
    154154        } // if
    155155
    156         for ( Attribute * attr: reverseIterate( attributes ) ) {
    157                 os << string( indent + 2, ' ' ) << "attr " << attr->name.c_str();
    158         } // for
     156        if ( ! attributes.empty() ) {
     157                os << string( indent + 2, ' ' ) << "with attributes " << endl;
     158                for ( Attribute * attr: reverseIterate( attributes ) ) {
     159                        os << string( indent + 4, ' ' ) << attr->name.c_str() << endl;
     160                } // for
     161        } // if
    159162
    160163        os << endl;
     
    244247        newnode->type = new TypeData( TypeData::Aggregate );
    245248        newnode->type->aggregate.kind = kind;
    246         newnode->type->aggregate.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;
     249        newnode->type->aggregate.anon = name == nullptr;
     250        newnode->type->aggregate.name = newnode->type->aggregate.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
    247251        newnode->type->aggregate.actuals = actuals;
    248252        newnode->type->aggregate.fields = fields;
     
    250254        newnode->type->aggregate.tagged = false;
    251255        newnode->type->aggregate.parent = nullptr;
    252         newnode->type->aggregate.anon = name == nullptr;
    253256        return newnode;
    254257} // DeclarationNode::newAggregate
     
    257260        DeclarationNode * newnode = new DeclarationNode;
    258261        newnode->type = new TypeData( TypeData::Enum );
    259         newnode->type->enumeration.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;
     262        newnode->type->enumeration.anon = name == nullptr;
     263        newnode->type->enumeration.name = newnode->type->enumeration.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
    260264        newnode->type->enumeration.constants = constants;
    261265        newnode->type->enumeration.body = body;
    262         newnode->type->enumeration.anon = name == nullptr;
    263266        newnode->type->enumeration.typed = typed;
    264267        newnode->type->enumeration.hiding = hiding;
     
    989992        for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
    990993                try {
    991                         bool extracted = false;
    992                         bool anon = false;
     994                        bool extracted = false, anon = false;
     995                        AggregateDecl * unionDecl = nullptr;
     996
    993997                        if ( DeclarationNode * extr = cur->extractAggregate() ) {
    994998                                // handle the case where a structure declaration is contained within an object or type declaration
     999
    9951000                                Declaration * decl = extr->build();
    9961001                                if ( decl ) {
    997                                         // hoist the structure declaration
     1002                                        // Remember the declaration if it is a union aggregate ?
     1003                                        unionDecl = dynamic_cast<UnionDecl *>( decl );
     1004
    9981005                                        decl->location = cur->location;
    999                                         * out++ = decl;
     1006                                        *out++ = decl;
    10001007
    10011008                                        // need to remember the cases where a declaration contains an anonymous aggregate definition
     
    10031010                                        assert( extr->type );
    10041011                                        if ( extr->type->kind == TypeData::Aggregate ) {
     1012                                                // typedef struct { int A } B is the only case?
    10051013                                                anon = extr->type->aggregate.anon;
    10061014                                        } else if ( extr->type->kind == TypeData::Enum ) {
    1007                                                 // xxx - is it useful to have an implicit anonymous enum member?
     1015                                                // typedef enum { A } B is the only case?
    10081016                                                anon = extr->type->enumeration.anon;
    10091017                                        }
     
    10141022                        Declaration * decl = cur->build();
    10151023                        if ( decl ) {
     1024                                if ( TypedefDecl * typedefDecl = dynamic_cast<TypedefDecl *>( decl ) ) {
     1025                                        if ( unionDecl ) {                                      // is the typedef alias a union aggregate ?
     1026                                                // This code handles a special issue with the attribute transparent_union.
     1027                                                //
     1028                                                //    typedef union U { int i; } typedef_name __attribute__(( aligned(16) )) __attribute__(( transparent_union ))
     1029                                                //
     1030                                                // Here the attribute aligned goes with the typedef_name, so variables declared of this type are
     1031                                                // aligned.  However, the attribute transparent_union must be moved from the typedef_name to
     1032                                                // alias union U.  Currently, this is the only know attribute that must be moved from typedef to
     1033                                                // alias.
     1034
     1035                                                // If typedef is an alias for a union, then its alias type was hoisted above and remembered.
     1036                                                if ( UnionInstType * unionInstType = dynamic_cast<UnionInstType *>( typedefDecl->base ) ) {
     1037                                                        // Remove all transparent_union attributes from typedef and move to alias union.
     1038                                                        list<Attribute *>::iterator attr;
     1039                                                        for ( attr = unionInstType->attributes.begin(); attr != unionInstType->attributes.end(); ) { // forward order
     1040                                                                if ( (*attr)->name == "transparent_union" || (*attr)->name == "__transparent_union__" ) {
     1041                                                                        list<Attribute *>::iterator cur = attr; // remember current node
     1042                                                                        attr++;                         // advance iterator
     1043                                                                        unionDecl->attributes.emplace_back( *cur ); // move current
     1044                                                                        unionInstType->attributes.erase( cur ); // remove current
     1045                                                                } else {
     1046                                                                        attr++;                         // advance iterator
     1047                                                                } // if
     1048                                                        } // for
     1049                                                } // if
     1050                                        } // if
     1051                                } // if
     1052
    10161053                                // don't include anonymous declaration for named aggregates, but do include them for anonymous aggregates, e.g.:
    10171054                                // struct S {
Note: See TracChangeset for help on using the changeset viewer.