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