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 "Expression.h" |
---|
24 | #include "Initializer.h" |
---|
25 | #include "LinkageSpec.h" // for Spec, linkageName, Cforall |
---|
26 | #include "Type.h" // for Type, Type::StorageClasses |
---|
27 | |
---|
28 | |
---|
29 | // These must harmonize with the corresponding AggregateDecl::Aggregate enumerations. |
---|
30 | static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" }; |
---|
31 | |
---|
32 | const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) { |
---|
33 | return aggregateNames[aggr]; |
---|
34 | } |
---|
35 | |
---|
36 | AggregateDecl::AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, Type::StorageClasses(), linkage ), body( false ), attributes( attributes ) { |
---|
37 | } |
---|
38 | |
---|
39 | AggregateDecl::AggregateDecl( const AggregateDecl &other ) : Parent( other ) { |
---|
40 | cloneAll( other.members, members ); |
---|
41 | cloneAll( other.parameters, parameters ); |
---|
42 | cloneAll( other.attributes, attributes ); |
---|
43 | body = other.body; |
---|
44 | } |
---|
45 | |
---|
46 | AggregateDecl::~AggregateDecl() { |
---|
47 | deleteAll( attributes ); |
---|
48 | deleteAll( parameters ); |
---|
49 | deleteAll( members ); |
---|
50 | } |
---|
51 | |
---|
52 | void AggregateDecl::print( std::ostream &os, Indenter indent ) const { |
---|
53 | using std::string; |
---|
54 | using std::endl; |
---|
55 | |
---|
56 | os << typeString() << " " << name << ":"; |
---|
57 | if ( get_linkage() != LinkageSpec::Cforall ) { |
---|
58 | os << " " << LinkageSpec::name( linkage ); |
---|
59 | } // if |
---|
60 | os << " with body " << has_body(); |
---|
61 | |
---|
62 | if ( ! parameters.empty() ) { |
---|
63 | os << endl << indent << "... with parameters" << endl; |
---|
64 | printAll( parameters, os, indent+1 ); |
---|
65 | } // if |
---|
66 | if ( ! members.empty() ) { |
---|
67 | os << endl << indent << "... with members" << endl; |
---|
68 | printAll( members, os, indent+1 ); |
---|
69 | } // if |
---|
70 | if ( ! attributes.empty() ) { |
---|
71 | os << endl << indent << "... with attributes" << endl; |
---|
72 | printAll( attributes, os, indent+1 ); |
---|
73 | } // if |
---|
74 | os << endl; |
---|
75 | } |
---|
76 | |
---|
77 | void AggregateDecl::printShort( std::ostream &os, Indenter indent ) const { |
---|
78 | using std::string; |
---|
79 | using std::endl; |
---|
80 | |
---|
81 | os << typeString() << " " << name << " with body " << has_body() << endl; |
---|
82 | |
---|
83 | if ( ! parameters.empty() ) { |
---|
84 | os << indent << "... with parameters" << endl; |
---|
85 | printAll( parameters, os, indent+1 ); |
---|
86 | } // if |
---|
87 | } |
---|
88 | |
---|
89 | const char * StructDecl::typeString() const { return aggrString( kind ); } |
---|
90 | |
---|
91 | StructInstType * StructDecl::makeInst( std::list< Expression * > const & new_parameters ) { |
---|
92 | std::list< Expression * > copy_parameters; |
---|
93 | cloneAll( new_parameters, copy_parameters ); |
---|
94 | return makeInst( move( copy( copy_parameters ) ) ); |
---|
95 | } |
---|
96 | |
---|
97 | StructInstType * StructDecl::makeInst( std::list< Expression * > && new_parameters ) { |
---|
98 | assert( parameters.size() == new_parameters.size() ); |
---|
99 | StructInstType * type = new StructInstType( noQualifiers, this ); |
---|
100 | type->parameters = std::move( new_parameters ); |
---|
101 | return type; |
---|
102 | } |
---|
103 | |
---|
104 | const char * UnionDecl::typeString() const { return aggrString( Union ); } |
---|
105 | |
---|
106 | const char * EnumDecl::typeString() const { return aggrString( Enum ); } |
---|
107 | |
---|
108 | const char * TraitDecl::typeString() const { return aggrString( Trait ); } |
---|
109 | |
---|
110 | bool EnumDecl::valueOf( Declaration * enumerator, long long int & value ) { |
---|
111 | if ( enumValues.empty() ) { |
---|
112 | long long int currentValue = 0; |
---|
113 | for ( Declaration * member : members ) { |
---|
114 | ObjectDecl * field = strict_dynamic_cast< ObjectDecl * >( member ); |
---|
115 | if ( field->init ) { |
---|
116 | SingleInit * init = strict_dynamic_cast< SingleInit * >( field->init ); |
---|
117 | auto result = eval( init->value ); |
---|
118 | if ( ! result.second ) SemanticError( init->location, toString( "Non-constexpr in initialization of enumerator: ", field ) ); |
---|
119 | currentValue = result.first; |
---|
120 | } |
---|
121 | assertf( enumValues.count( field->name ) == 0, "Enum %s has multiple members with the name %s", name.c_str(), field->name.c_str() ); |
---|
122 | enumValues[ field->name ] = currentValue; |
---|
123 | ++currentValue; |
---|
124 | } |
---|
125 | } |
---|
126 | if ( enumValues.count( enumerator->name ) ) { |
---|
127 | value = enumValues[ enumerator->name ]; |
---|
128 | return true; |
---|
129 | } |
---|
130 | return false; |
---|
131 | } |
---|
132 | |
---|
133 | // Local Variables: // |
---|
134 | // tab-width: 4 // |
---|
135 | // mode: c++ // |
---|
136 | // compile-command: "make install" // |
---|
137 | // End: // |
---|