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 | // ReferenceToType.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Feb 23 16:38:54 2017 |
---|
13 | // Update Count : 24 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> // for assert |
---|
17 | #include <list> // for list, _List_const_iterator, list<>::cons... |
---|
18 | #include <ostream> // for operator<<, basic_ostream, ostream, endl |
---|
19 | #include <string> // for string, operator<<, char_traits, operator== |
---|
20 | |
---|
21 | #include "Common/utility.h" // for printAll, cloneAll, deleteAll |
---|
22 | #include "Declaration.h" // for StructDecl, UnionDecl, EnumDecl, Declara... |
---|
23 | #include "Expression.h" // for Expression |
---|
24 | #include "Type.h" // for TypeInstType, StructInstType, UnionInstType |
---|
25 | #include "TypeSubstitution.h" // for TypeSubstitution |
---|
26 | |
---|
27 | class Attribute; |
---|
28 | |
---|
29 | ReferenceToType::ReferenceToType( const Type::Qualifiers &tq, const std::string &name, const std::list< Attribute * > & attributes ) : Type( tq, attributes ), name( name ), hoistType( false ) { |
---|
30 | } |
---|
31 | |
---|
32 | ReferenceToType::ReferenceToType( const ReferenceToType &other ) : Type( other ), name( other.name ), hoistType( other.hoistType ) { |
---|
33 | cloneAll( other.parameters, parameters ); |
---|
34 | } |
---|
35 | |
---|
36 | ReferenceToType::~ReferenceToType() { |
---|
37 | deleteAll( parameters ); |
---|
38 | } |
---|
39 | |
---|
40 | void ReferenceToType::print( std::ostream &os, Indenter indent ) const { |
---|
41 | using std::endl; |
---|
42 | |
---|
43 | Type::print( os, indent ); |
---|
44 | os << "instance of " << typeString() << " " << name << " "; |
---|
45 | if ( ! parameters.empty() ) { |
---|
46 | os << endl << indent << "... with parameters" << endl; |
---|
47 | printAll( parameters, os, indent+1 ); |
---|
48 | } // if |
---|
49 | } |
---|
50 | |
---|
51 | namespace { |
---|
52 | void doLookup( const std::list< Declaration* > &members, const std::string &name, std::list< Declaration* > &foundDecls ) { |
---|
53 | for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) { |
---|
54 | if ( (*i)->get_name() == name ) { |
---|
55 | foundDecls.push_back( *i ); |
---|
56 | } // if |
---|
57 | } // for |
---|
58 | } |
---|
59 | } // namespace |
---|
60 | |
---|
61 | StructInstType::StructInstType( const Type::Qualifiers & tq, StructDecl * baseStruct, const std::list< Attribute * > & attributes ) : |
---|
62 | Parent( tq, baseStruct->get_name(), attributes ), baseStruct( baseStruct ) {} |
---|
63 | |
---|
64 | std::string StructInstType::typeString() const { return "struct"; } |
---|
65 | |
---|
66 | const std::list<TypeDecl*>* StructInstType::get_baseParameters() const { |
---|
67 | if ( ! baseStruct ) return nullptr; |
---|
68 | return &baseStruct->get_parameters(); |
---|
69 | } |
---|
70 | |
---|
71 | std::list<TypeDecl*>* StructInstType::get_baseParameters() { |
---|
72 | if ( ! baseStruct ) return nullptr; |
---|
73 | return &baseStruct->get_parameters(); |
---|
74 | } |
---|
75 | |
---|
76 | bool StructInstType::isComplete() const { return baseStruct ? baseStruct->has_body() : false; } |
---|
77 | |
---|
78 | AggregateDecl * StructInstType::getAggr() { return baseStruct; } |
---|
79 | |
---|
80 | TypeSubstitution StructInstType::genericSubstitution() const { |
---|
81 | return TypeSubstitution( get_baseParameters()->begin(), get_baseParameters()->end(), parameters.begin() ); |
---|
82 | } |
---|
83 | |
---|
84 | void StructInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const { |
---|
85 | assert( baseStruct ); |
---|
86 | doLookup( baseStruct->get_members(), name, foundDecls ); |
---|
87 | } |
---|
88 | |
---|
89 | void StructInstType::print( std::ostream &os, Indenter indent ) const { |
---|
90 | using std::endl; |
---|
91 | |
---|
92 | if ( baseStruct == nullptr ) ReferenceToType::print( os, indent ); |
---|
93 | else { |
---|
94 | Type::print( os, indent ); |
---|
95 | os << "instance of " << typeString() << " " << name << " with body " << baseStruct->has_body() << " "; |
---|
96 | if ( ! parameters.empty() ) { |
---|
97 | os << endl << indent << "... with parameters" << endl; |
---|
98 | printAll( parameters, os, indent+1 ); |
---|
99 | } // if |
---|
100 | } // if |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | UnionInstType::UnionInstType( const Type::Qualifiers & tq, UnionDecl * baseUnion, const std::list< Attribute * > & attributes ) : |
---|
105 | Parent( tq, baseUnion->get_name(), attributes ), baseUnion( baseUnion ) {} |
---|
106 | |
---|
107 | std::string UnionInstType::typeString() const { return "union"; } |
---|
108 | |
---|
109 | std::list< TypeDecl * > * UnionInstType::get_baseParameters() { |
---|
110 | if ( ! baseUnion ) return nullptr; |
---|
111 | return &baseUnion->get_parameters(); |
---|
112 | } |
---|
113 | |
---|
114 | const std::list< TypeDecl * > * UnionInstType::get_baseParameters() const { |
---|
115 | if ( ! baseUnion ) return nullptr; |
---|
116 | return &baseUnion->get_parameters(); |
---|
117 | } |
---|
118 | |
---|
119 | bool UnionInstType::isComplete() const { return baseUnion ? baseUnion->has_body() : false; } |
---|
120 | |
---|
121 | AggregateDecl * UnionInstType::getAggr() { return baseUnion; } |
---|
122 | |
---|
123 | TypeSubstitution UnionInstType::genericSubstitution() const { |
---|
124 | return TypeSubstitution( get_baseParameters()->begin(), get_baseParameters()->end(), parameters.begin() ); |
---|
125 | } |
---|
126 | |
---|
127 | void UnionInstType::lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const { |
---|
128 | assert( baseUnion ); |
---|
129 | doLookup( baseUnion->get_members(), name, foundDecls ); |
---|
130 | } |
---|
131 | |
---|
132 | void UnionInstType::print( std::ostream &os, Indenter indent ) const { |
---|
133 | using std::endl; |
---|
134 | |
---|
135 | if ( baseUnion == nullptr ) ReferenceToType::print( os, indent ); |
---|
136 | else { |
---|
137 | Type::print( os, indent ); |
---|
138 | os << "instance of " << typeString() << " " << name << " with body " << baseUnion->has_body() << " "; |
---|
139 | if ( ! parameters.empty() ) { |
---|
140 | os << endl << indent << "... with parameters" << endl; |
---|
141 | printAll( parameters, os, indent+1 ); |
---|
142 | } // if |
---|
143 | } // if |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | EnumInstType::EnumInstType( const Type::Qualifiers & tq, EnumDecl * baseEnum, const std::list< Attribute * > & attributes ) : |
---|
148 | Parent( tq, baseEnum->get_name(), attributes ), baseEnum( baseEnum ) {} |
---|
149 | |
---|
150 | std::string EnumInstType::typeString() const { return "enum"; } |
---|
151 | |
---|
152 | bool EnumInstType::isComplete() const { return baseEnum ? baseEnum->has_body() : false; } |
---|
153 | |
---|
154 | void EnumInstType::print( std::ostream &os, Indenter indent ) const { |
---|
155 | using std::endl; |
---|
156 | |
---|
157 | if ( baseEnum == nullptr ) ReferenceToType::print( os, indent ); |
---|
158 | else { |
---|
159 | Type::print( os, indent ); |
---|
160 | os << "instance of " << typeString() << " " << name << " with body " << baseEnum->has_body() << " "; |
---|
161 | } // if |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | std::string TraitInstType::typeString() const { return "trait"; } |
---|
166 | |
---|
167 | TraitInstType::TraitInstType( const Type::Qualifiers & tq, TraitDecl * baseTrait, const std::list< Attribute * > & attributes ) : Parent( tq, baseTrait->name, attributes ), baseTrait( baseTrait ) {} |
---|
168 | |
---|
169 | TraitInstType::TraitInstType( const TraitInstType &other ) : Parent( other ), baseTrait( other.baseTrait ) { |
---|
170 | } |
---|
171 | |
---|
172 | TraitInstType::~TraitInstType() { |
---|
173 | } |
---|
174 | |
---|
175 | bool TraitInstType::isComplete() const { assert( false ); } |
---|
176 | |
---|
177 | TypeInstType::TypeInstType( const Type::Qualifiers &tq, const std::string &name, TypeDecl *baseType, const std::list< Attribute * > & attributes ) : Parent( tq, name, attributes ) { |
---|
178 | set_baseType( baseType ); |
---|
179 | } |
---|
180 | |
---|
181 | TypeInstType::TypeInstType( const Type::Qualifiers &tq, const std::string &name, bool isFtype, const std::list< Attribute * > & attributes ) : Parent( tq, name, attributes ), baseType( 0 ), isFtype( isFtype ) { |
---|
182 | } |
---|
183 | |
---|
184 | TypeInstType::TypeInstType( const TypeInstType &other ) : Parent( other ), baseType( other.baseType ), isFtype( other.isFtype ) { |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | TypeInstType::~TypeInstType() { |
---|
189 | // delete baseType; //This is shared and should not be deleted |
---|
190 | } |
---|
191 | |
---|
192 | void TypeInstType::set_baseType( TypeDecl *newValue ) { |
---|
193 | baseType = newValue; |
---|
194 | isFtype = newValue->get_kind() == TypeDecl::Ftype; |
---|
195 | } |
---|
196 | |
---|
197 | std::string TypeInstType::typeString() const { return "type"; } |
---|
198 | |
---|
199 | bool TypeInstType::isComplete() const { return baseType->isComplete(); } |
---|
200 | |
---|
201 | void TypeInstType::print( std::ostream &os, Indenter indent ) const { |
---|
202 | using std::endl; |
---|
203 | |
---|
204 | Type::print( os, indent ); |
---|
205 | os << "instance of " << typeString() << " " << get_name() << " (" << ( isFtype ? "" : "not" ) << " function type) "; |
---|
206 | if ( ! parameters.empty() ) { |
---|
207 | os << endl << indent << "... with parameters" << endl; |
---|
208 | printAll( parameters, os, indent+1 ); |
---|
209 | } // if |
---|
210 | } |
---|
211 | |
---|
212 | // Local Variables: // |
---|
213 | // tab-width: 4 // |
---|
214 | // mode: c++ // |
---|
215 | // compile-command: "make install" // |
---|
216 | // End: // |
---|