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 : Fri Jul 1 09:12:33 2022 |
---|
13 | // Update Count : 32 |
---|
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 | 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 | |
---|
76 | void 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 | |
---|
88 | const char * StructDecl::typeString() const { return aggrString( kind ); } |
---|
89 | |
---|
90 | StructInstType * StructDecl::makeInst( std::list< Expression * > const & new_parameters ) { |
---|
91 | std::list< Expression * > copy_parameters; |
---|
92 | cloneAll( new_parameters, copy_parameters ); |
---|
93 | return makeInst( copy( copy_parameters ) ); |
---|
94 | } |
---|
95 | |
---|
96 | StructInstType * StructDecl::makeInst( std::list< Expression * > && new_parameters ) { |
---|
97 | assert( parameters.size() == new_parameters.size() ); |
---|
98 | StructInstType * type = new StructInstType( noQualifiers, this ); |
---|
99 | type->parameters = std::move( new_parameters ); |
---|
100 | return type; |
---|
101 | } |
---|
102 | |
---|
103 | const char * UnionDecl::typeString() const { return aggrString( Union ); } |
---|
104 | |
---|
105 | const char * EnumDecl::typeString() const { return aggrString( Enum ); } |
---|
106 | |
---|
107 | void EnumDecl::print( std::ostream & os, Indenter indent ) const { |
---|
108 | AggregateDecl::print(os, indent); |
---|
109 | os << " with base? " << (base? "True" : "False") << std::endl; |
---|
110 | if ( base ) { |
---|
111 | os << "Base Type of Enum:" << std::endl; |
---|
112 | base->print(os, indent); |
---|
113 | } |
---|
114 | os << std::endl << "End of EnumDecl::print" << std::endl; |
---|
115 | } |
---|
116 | |
---|
117 | const char * TraitDecl::typeString() const { return aggrString( Trait ); } |
---|
118 | |
---|
119 | bool EnumDecl::valueOf( Declaration * enumerator, long long int & value ) { |
---|
120 | if ( enumValues.empty() ) { |
---|
121 | long long int currentValue = 0; |
---|
122 | for ( Declaration * member : members ) { |
---|
123 | ObjectDecl * field = strict_dynamic_cast< ObjectDecl * >( member ); |
---|
124 | if ( field->init ) { |
---|
125 | SingleInit * init = strict_dynamic_cast< SingleInit * >( field->init ); |
---|
126 | auto result = eval( init->value ); |
---|
127 | if ( ! result.second ) SemanticError( init->location, toString( "Enumerator value for '", field, "' is not an integer constant" ) ); |
---|
128 | currentValue = result.first; |
---|
129 | } |
---|
130 | assertf( enumValues.count( field->name ) == 0, "Enum %s has multiple members with the name %s", name.c_str(), field->name.c_str() ); |
---|
131 | enumValues[ field->name ] = currentValue; |
---|
132 | ++currentValue; |
---|
133 | } |
---|
134 | } |
---|
135 | if ( enumValues.count( enumerator->name ) ) { |
---|
136 | value = enumValues[ enumerator->name ]; |
---|
137 | return true; |
---|
138 | } |
---|
139 | return false; |
---|
140 | } |
---|
141 | |
---|
142 | // Local Variables: // |
---|
143 | // tab-width: 4 // |
---|
144 | // mode: c++ // |
---|
145 | // compile-command: "make install" // |
---|
146 | // End: // |
---|