[69bafd2] | 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 | // Type.cpp -- |
---|
| 8 | // |
---|
| 9 | // Author : Aaron B. Moss |
---|
| 10 | // Created On : Mon May 13 15:00:00 2019 |
---|
[923d25a] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Thu Jul 23 14:16:00 2020 |
---|
| 13 | // Update Count : 5 |
---|
[69bafd2] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "Type.hpp" |
---|
| 17 | |
---|
| 18 | #include <cassert> |
---|
| 19 | #include <utility> // for move |
---|
| 20 | #include <vector> |
---|
| 21 | |
---|
| 22 | #include "Decl.hpp" |
---|
[e0e9a0b] | 23 | #include "ForallSubstitutor.hpp" // for substituteForall |
---|
[69bafd2] | 24 | #include "Init.hpp" |
---|
[e0e9a0b] | 25 | #include "Common/utility.h" // for copy, move |
---|
[69bafd2] | 26 | #include "InitTweak/InitTweak.h" // for getPointerBase |
---|
| 27 | #include "Tuples/Tuples.h" // for isTtype |
---|
| 28 | |
---|
| 29 | namespace ast { |
---|
| 30 | |
---|
[d76c588] | 31 | const Type * Type::getComponent( unsigned i ) const { |
---|
[69bafd2] | 32 | assertf( size() == 1 && i == 0, "Type::getComponent was called with size %d and index %d\n", size(), i ); |
---|
| 33 | return this; |
---|
| 34 | } |
---|
| 35 | |
---|
[d76c588] | 36 | const Type * Type::stripDeclarator() const { |
---|
[69bafd2] | 37 | const Type * t; |
---|
| 38 | const Type * a; |
---|
| 39 | for ( t = this; (a = InitTweak::getPointerBase( t )); t = a ); |
---|
| 40 | return t; |
---|
| 41 | } |
---|
| 42 | |
---|
[d76c588] | 43 | const Type * Type::stripReferences() const { |
---|
[69bafd2] | 44 | const Type * t; |
---|
| 45 | const ReferenceType * r; |
---|
[acd80b4] | 46 | for ( t = this; (r = dynamic_cast<const ReferenceType *>(t) ); t = r->base ); |
---|
[69bafd2] | 47 | return t; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | // --- BasicType |
---|
| 51 | |
---|
[cfaa2873] | 52 | // GENERATED START, DO NOT EDIT |
---|
| 53 | // GENERATED BY BasicTypes-gen.cc |
---|
| 54 | const char * BasicType::typeNames[] = { |
---|
[69bafd2] | 55 | "_Bool", |
---|
| 56 | "char", |
---|
| 57 | "signed char", |
---|
| 58 | "unsigned char", |
---|
| 59 | "signed short int", |
---|
| 60 | "unsigned short int", |
---|
| 61 | "signed int", |
---|
| 62 | "unsigned int", |
---|
| 63 | "signed long int", |
---|
| 64 | "unsigned long int", |
---|
| 65 | "signed long long int", |
---|
| 66 | "unsigned long long int", |
---|
| 67 | "__int128", |
---|
| 68 | "unsigned __int128", |
---|
| 69 | "_Float16", |
---|
| 70 | "_Float16 _Complex", |
---|
| 71 | "_Float32", |
---|
| 72 | "_Float32 _Complex", |
---|
| 73 | "float", |
---|
| 74 | "float _Complex", |
---|
| 75 | "_Float32x", |
---|
| 76 | "_Float32x _Complex", |
---|
| 77 | "_Float64", |
---|
| 78 | "_Float64 _Complex", |
---|
| 79 | "double", |
---|
| 80 | "double _Complex", |
---|
| 81 | "_Float64x", |
---|
| 82 | "_Float64x _Complex", |
---|
| 83 | "__float80", |
---|
| 84 | "_Float128", |
---|
| 85 | "_Float128 _Complex", |
---|
| 86 | "__float128", |
---|
| 87 | "long double", |
---|
| 88 | "long double _Complex", |
---|
| 89 | "_Float128x", |
---|
| 90 | "_Float128x _Complex", |
---|
| 91 | }; |
---|
[cfaa2873] | 92 | // GENERATED END |
---|
[69bafd2] | 93 | |
---|
[e0e9a0b] | 94 | // --- ParameterizedType |
---|
| 95 | |
---|
[361bf01] | 96 | void FunctionType::initWithSub( |
---|
| 97 | const FunctionType & o, Pass< ForallSubstitutor > & sub |
---|
[e0e9a0b] | 98 | ) { |
---|
[7ff3e522] | 99 | forall = sub.core( o.forall ); |
---|
[e0e9a0b] | 100 | } |
---|
| 101 | |
---|
[69bafd2] | 102 | // --- FunctionType |
---|
| 103 | |
---|
[954c954] | 104 | |
---|
[e0e9a0b] | 105 | FunctionType::FunctionType( const FunctionType & o ) |
---|
[361bf01] | 106 | : Type( o.qualifiers, copy( o.attributes ) ), returns(), params(), |
---|
[e0e9a0b] | 107 | isVarArgs( o.isVarArgs ) { |
---|
| 108 | Pass< ForallSubstitutor > sub; |
---|
| 109 | initWithSub( o, sub ); // initialize substitution map |
---|
[7ff3e522] | 110 | returns = sub.core( o.returns ); // apply to return and parameter types |
---|
| 111 | params = sub.core( o.params ); |
---|
[e0e9a0b] | 112 | } |
---|
| 113 | |
---|
[69bafd2] | 114 | namespace { |
---|
[954c954] | 115 | bool containsTtype( const std::vector<ptr<Type>> & l ) { |
---|
[69bafd2] | 116 | if ( ! l.empty() ) { |
---|
[954c954] | 117 | return Tuples::isTtype( l.back() ); |
---|
[69bafd2] | 118 | } |
---|
| 119 | return false; |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | bool FunctionType::isTtype() const { |
---|
[acd80b4] | 124 | return containsTtype( returns ) || containsTtype( params ); |
---|
[69bafd2] | 125 | } |
---|
| 126 | |
---|
[98e8b3b] | 127 | std::vector<readonly<Decl>> BaseInstType::lookup( const std::string& name ) const { |
---|
[69bafd2] | 128 | assertf( aggr(), "Must have aggregate to perform lookup" ); |
---|
[acd80b4] | 129 | |
---|
[69bafd2] | 130 | std::vector<readonly<Decl>> found; |
---|
| 131 | for ( const Decl * decl : aggr()->members ) { |
---|
| 132 | if ( decl->name == name ) { found.emplace_back( decl ); } |
---|
| 133 | } |
---|
| 134 | return found; |
---|
| 135 | } |
---|
| 136 | |
---|
[923d25a] | 137 | // --- SueInstType (StructInstType, UnionInstType, EnumInstType) |
---|
[69bafd2] | 138 | |
---|
[923d25a] | 139 | template<typename decl_t> |
---|
| 140 | SueInstType<decl_t>::SueInstType( |
---|
| 141 | const decl_t * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as ) |
---|
[98e8b3b] | 142 | : BaseInstType( b->name, q, move(as) ), base( b ) {} |
---|
[69bafd2] | 143 | |
---|
[3aec25f] | 144 | template<typename decl_t> |
---|
| 145 | SueInstType<decl_t>::SueInstType( |
---|
| 146 | const base_type * b, std::vector<ptr<Expr>> && params, |
---|
| 147 | CV::Qualifiers q, std::vector<ptr<Attribute>> && as ) |
---|
| 148 | : BaseInstType( b->name, std::move(params), q, std::move(as) ), base( b ) {} |
---|
| 149 | |
---|
[923d25a] | 150 | template<typename decl_t> |
---|
| 151 | bool SueInstType<decl_t>::isComplete() const { |
---|
| 152 | return base ? base->body : false; |
---|
| 153 | } |
---|
[69bafd2] | 154 | |
---|
[923d25a] | 155 | template class SueInstType<StructDecl>; |
---|
| 156 | template class SueInstType<UnionDecl>; |
---|
| 157 | template class SueInstType<EnumDecl>; |
---|
[69bafd2] | 158 | |
---|
[514a791] | 159 | // --- TraitInstType |
---|
| 160 | |
---|
[7ff3e522] | 161 | TraitInstType::TraitInstType( |
---|
[e0e9a0b] | 162 | const TraitDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as ) |
---|
[98e8b3b] | 163 | : BaseInstType( b->name, q, move(as) ), base( b ) {} |
---|
[514a791] | 164 | |
---|
[69bafd2] | 165 | void TypeInstType::set_base( const TypeDecl * b ) { |
---|
| 166 | base = b; |
---|
| 167 | kind = b->kind; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | bool TypeInstType::isComplete() const { return base->sized; } |
---|
| 171 | |
---|
| 172 | // --- TupleType |
---|
| 173 | |
---|
[acd80b4] | 174 | TupleType::TupleType( std::vector<ptr<Type>> && ts, CV::Qualifiers q ) |
---|
[e0e9a0b] | 175 | : Type( q ), types( move(ts) ), members() { |
---|
[acd80b4] | 176 | // This constructor is awkward. `TupleType` needs to contain objects so that members can be |
---|
| 177 | // named, but members without initializer nodes end up getting constructors, which breaks |
---|
| 178 | // things. This happens because the object decls have to be visited so that their types are |
---|
| 179 | // kept in sync with the types listed here. Ultimately, the types listed here should perhaps |
---|
| 180 | // be eliminated and replaced with a list-view over members. The temporary solution is to |
---|
| 181 | // make a `ListInit` with `maybeConstructed = false`, so when the object is visited it is not |
---|
[69bafd2] | 182 | // constructed. Potential better solutions include: |
---|
[acd80b4] | 183 | // a) Separate `TupleType` from its declarations, into `TupleDecl` and `Tuple{Inst?}Type`, |
---|
[69bafd2] | 184 | // similar to the aggregate types. |
---|
[acd80b4] | 185 | // b) Separate initializer nodes better, e.g. add a `MaybeConstructed` node that is replaced |
---|
[69bafd2] | 186 | // by `genInit`, rather than the current boolean flag. |
---|
| 187 | members.reserve( types.size() ); |
---|
| 188 | for ( const Type * ty : types ) { |
---|
| 189 | members.emplace_back( new ObjectDecl{ |
---|
[16ba4a6] | 190 | CodeLocation{}, "", ty, new ListInit( CodeLocation{}, {}, {}, NoConstruct ), |
---|
[69bafd2] | 191 | Storage::Classes{}, Linkage::Cforall } ); |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | |
---|
[e5c3811] | 195 | bool isUnboundType(const Type * type) { |
---|
| 196 | if (auto typeInst = dynamic_cast<const TypeInstType *>(type)) { |
---|
| 197 | // xxx - look for a type name produced by renameTyVars. |
---|
| 198 | |
---|
| 199 | // TODO: once TypeInstType representation is updated, it should properly check |
---|
| 200 | // if the context id is filled. this is a temporary hack for now |
---|
[cd6a6ff] | 201 | return isUnboundType(typeInst->name); |
---|
| 202 | } |
---|
| 203 | return false; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | bool isUnboundType(const std::string & tname) { |
---|
| 207 | // xxx - look for a type name produced by renameTyVars. |
---|
| 208 | |
---|
| 209 | // TODO: once TypeInstType representation is updated, it should properly check |
---|
| 210 | // if the context id is filled. this is a temporary hack for now |
---|
| 211 | if (std::count(tname.begin(), tname.end(), '_') >= 3) { |
---|
| 212 | return true; |
---|
[e5c3811] | 213 | } |
---|
| 214 | return false; |
---|
| 215 | } |
---|
| 216 | |
---|
[69bafd2] | 217 | } |
---|
| 218 | |
---|
| 219 | // Local Variables: // |
---|
| 220 | // tab-width: 4 // |
---|
| 221 | // mode: c++ // |
---|
| 222 | // compile-command: "make install" // |
---|
| 223 | // End: // |
---|