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