source: src/SynTree/AggregateDecl.cc @ 42cd451e

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 42cd451e 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
Line 
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// AggregateDecl.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 23:56:39 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Dec 16 15:07:20 2019
13// Update Count     : 31
14//
15
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
23#include "Initializer.h"
24#include "LinkageSpec.h"         // for Spec, linkageName, Cforall
25#include "Type.h"                // for Type, Type::StorageClasses
26
27
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
35AggregateDecl::AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, Type::StorageClasses(), linkage ), body( false ), attributes( attributes ) {
36}
37
38AggregateDecl::AggregateDecl( const AggregateDecl &other ) : Parent( other ) {
39        cloneAll( other.members, members );
40        cloneAll( other.parameters, parameters );
41        cloneAll( other.attributes, attributes );
42        body = other.body;
43}
44
45AggregateDecl::~AggregateDecl() {
46        deleteAll( attributes );
47        deleteAll( parameters );
48        deleteAll( members );
49}
50
51void AggregateDecl::print( std::ostream &os, Indenter indent ) const {
52        using std::string;
53        using std::endl;
54
55        os << typeString() << " " << name << ":";
56        if ( get_linkage() != LinkageSpec::Cforall ) {
57                os << " " << LinkageSpec::name( linkage );
58        } // if
59        os << " with body " << has_body();
60
61        if ( ! parameters.empty() ) {
62                os << endl << indent << "... with parameters" << endl;
63                printAll( parameters, os, indent+1 );
64        } // if
65        if ( ! members.empty() ) {
66                os << endl << indent << "... with members" << endl;
67                printAll( members, os, indent+1 );
68        } // if
69        if ( ! attributes.empty() ) {
70                os << endl << indent << "... with attributes" << endl;
71                printAll( attributes, os, indent+1 );
72        } // if
73        os << endl;
74}
75
76void AggregateDecl::printShort( std::ostream &os, Indenter indent ) const {
77        using std::string;
78        using std::endl;
79
80        os << typeString() << " " << name << " with body " << has_body() << endl;
81
82        if ( ! parameters.empty() ) {
83                os << indent << "... with parameters" << endl;
84                printAll( parameters, os, indent+1 );
85        } // if
86}
87
88const char * StructDecl::typeString() const { return aggrString( kind ); }
89
90const char * UnionDecl::typeString() const { return aggrString( Union ); }
91
92const char * EnumDecl::typeString() const { return aggrString( Enum ); }
93
94const char * TraitDecl::typeString() const { return aggrString( Trait ); }
95
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 );
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;
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
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.