source: src/SynTree/AggregateDecl.cc @ e3bc51c

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since e3bc51c was d912bed, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

harmonize SynTree/LinkageSpec?.* with AST/LinkageSpec.*

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[0dd3a2f]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//
[79970ed]7// AggregateDecl.cc --
[0dd3a2f]8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 23:56:39 2015
[312029a]11// Last Modified By : Peter A. Buhr
[d912bed]12// Last Modified On : Mon Dec 16 15:07:20 2019
13// Update Count     : 31
[0dd3a2f]14//
[51b7345]15
[ea6332d]16#include <list>                  // for list
17#include <ostream>               // for operator<<, basic_ostream, ostream
18#include <string>                // for operator<<, string, char_traits
19
20#include "Attribute.h"           // for Attribute
21#include "Common/utility.h"      // for printAll, cloneAll, deleteAll
22#include "Declaration.h"         // for AggregateDecl, TypeDecl, Declaration
[312029a]23#include "Initializer.h"
[07de76b]24#include "LinkageSpec.h"         // for Spec, linkageName, Cforall
[ea6332d]25#include "Type.h"                // for Type, Type::StorageClasses
[51b7345]26
27
[312029a]28// These must harmonize with the corresponding AggregateDecl::Aggregate enumerations.
29static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };
30
31const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) {
32        return aggregateNames[aggr];
33}
34
[fa4805f]35AggregateDecl::AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, Type::StorageClasses(), linkage ), body( false ), attributes( attributes ) {
[51b7345]36}
37
[0dd3a2f]38AggregateDecl::AggregateDecl( const AggregateDecl &other ) : Parent( other ) {
[a08ba92]39        cloneAll( other.members, members );
40        cloneAll( other.parameters, parameters );
[c0aa336]41        cloneAll( other.attributes, attributes );
[5d125e4]42        body = other.body;
[51b7345]43}
44
[0dd3a2f]45AggregateDecl::~AggregateDecl() {
[c0aa336]46        deleteAll( attributes );
[a08ba92]47        deleteAll( parameters );
[c0aa336]48        deleteAll( members );
[51b7345]49}
50
[50377a4]51void AggregateDecl::print( std::ostream &os, Indenter indent ) const {
[a08ba92]52        using std::string;
53        using std::endl;
[51b7345]54
[50377a4]55        os << typeString() << " " << name << ":";
[cbce272]56        if ( get_linkage() != LinkageSpec::Cforall ) {
[d912bed]57                os << " " << LinkageSpec::name( linkage );
[cbce272]58        } // if
[50377a4]59        os << " with body " << has_body();
[5d125e4]60
[a08ba92]61        if ( ! parameters.empty() ) {
[50377a4]62                os << endl << indent << "... with parameters" << endl;
63                printAll( parameters, os, indent+1 );
[a08ba92]64        } // if
65        if ( ! members.empty() ) {
[50377a4]66                os << endl << indent << "... with members" << endl;
67                printAll( members, os, indent+1 );
[a08ba92]68        } // if
[c0aa336]69        if ( ! attributes.empty() ) {
[50377a4]70                os << endl << indent << "... with attributes" << endl;
71                printAll( attributes, os, indent+1 );
[c0aa336]72        } // if
[50377a4]73        os << endl;
[51b7345]74}
75
[50377a4]76void AggregateDecl::printShort( std::ostream &os, Indenter indent ) const {
[a08ba92]77        using std::string;
78        using std::endl;
[51b7345]79
[50377a4]80        os << typeString() << " " << name << " with body " << has_body() << endl;
[5d125e4]81
[a08ba92]82        if ( ! parameters.empty() ) {
[50377a4]83                os << indent << "... with parameters" << endl;
84                printAll( parameters, os, indent+1 );
[a08ba92]85        } // if
[51b7345]86}
87
[312029a]88const char * StructDecl::typeString() const { return aggrString( kind ); }
[51b7345]89
[312029a]90const char * UnionDecl::typeString() const { return aggrString( Union ); }
[51b7345]91
[312029a]92const char * EnumDecl::typeString() const { return aggrString( Enum ); }
[51b7345]93
[312029a]94const char * TraitDecl::typeString() const { return aggrString( Trait ); }
[51b7345]95
[fc9153d]96bool EnumDecl::valueOf( Declaration * enumerator, long long int & value ) {
97        if ( enumValues.empty() ) {
98                long long int currentValue = 0;
99                for ( Declaration * member : members ) {
100                        ObjectDecl * field = strict_dynamic_cast< ObjectDecl * >( member );
101                        if ( field->init ) {
102                                SingleInit * init = strict_dynamic_cast< SingleInit * >( field->init );
[ac3362c]103                                auto result = eval( init->value );
104                                if ( ! result.second ) SemanticError( init->location, toString( "Non-constexpr in initialization of enumerator: ", field ) );
105                                currentValue = result.first;
[fc9153d]106                        }
107                        assertf( enumValues.count( field->name ) == 0, "Enum %s has multiple members with the name %s", name.c_str(), field->name.c_str() );
108                        enumValues[ field->name ] = currentValue;
109                        ++currentValue;
110                }
111        }
112        if ( enumValues.count( enumerator->name ) ) {
113                value = enumValues[ enumerator->name ];
114                return true;
115        }
116        return false;
117}
118
[0dd3a2f]119// Local Variables: //
120// tab-width: 4 //
121// mode: c++ //
122// compile-command: "make install" //
123// End: //
Note: See TracBrowser for help on using the repository browser.