[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 | |
---|
[7ff3e522] | 96 | void ParameterizedType::initWithSub( |
---|
| 97 | const ParameterizedType & o, Pass< ForallSubstitutor > & sub |
---|
[e0e9a0b] | 98 | ) { |
---|
[7ff3e522] | 99 | forall = sub.core( o.forall ); |
---|
[e0e9a0b] | 100 | } |
---|
| 101 | |
---|
[69bafd2] | 102 | // --- FunctionType |
---|
| 103 | |
---|
[e0e9a0b] | 104 | FunctionType::FunctionType( const FunctionType & o ) |
---|
[7ff3e522] | 105 | : ParameterizedType( o.qualifiers, copy( o.attributes ) ), returns(), params(), |
---|
[e0e9a0b] | 106 | isVarArgs( o.isVarArgs ) { |
---|
| 107 | Pass< ForallSubstitutor > sub; |
---|
| 108 | initWithSub( o, sub ); // initialize substitution map |
---|
[7ff3e522] | 109 | returns = sub.core( o.returns ); // apply to return and parameter types |
---|
| 110 | params = sub.core( o.params ); |
---|
[e0e9a0b] | 111 | } |
---|
| 112 | |
---|
[69bafd2] | 113 | namespace { |
---|
| 114 | bool containsTtype( const std::vector<ptr<DeclWithType>> & l ) { |
---|
| 115 | if ( ! l.empty() ) { |
---|
| 116 | return Tuples::isTtype( l.back()->get_type() ); |
---|
| 117 | } |
---|
| 118 | return false; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | bool FunctionType::isTtype() const { |
---|
[acd80b4] | 123 | return containsTtype( returns ) || containsTtype( params ); |
---|
[69bafd2] | 124 | } |
---|
| 125 | |
---|
| 126 | // --- ReferenceToType |
---|
[e0e9a0b] | 127 | |
---|
| 128 | void ReferenceToType::initWithSub( const ReferenceToType & o, Pass< ForallSubstitutor > & sub ) { |
---|
| 129 | ParameterizedType::initWithSub( o, sub ); // initialize substitution |
---|
[7ff3e522] | 130 | params = sub.core( o.params ); // apply to parameters |
---|
[e0e9a0b] | 131 | } |
---|
| 132 | |
---|
| 133 | ReferenceToType::ReferenceToType( const ReferenceToType & o ) |
---|
| 134 | : ParameterizedType( o.qualifiers, copy( o.attributes ) ), params(), name( o.name ), |
---|
| 135 | hoistType( o.hoistType ) { |
---|
| 136 | Pass< ForallSubstitutor > sub; |
---|
| 137 | initWithSub( o, sub ); |
---|
| 138 | } |
---|
| 139 | |
---|
[69bafd2] | 140 | std::vector<readonly<Decl>> ReferenceToType::lookup( const std::string& name ) const { |
---|
| 141 | assertf( aggr(), "Must have aggregate to perform lookup" ); |
---|
[acd80b4] | 142 | |
---|
[69bafd2] | 143 | std::vector<readonly<Decl>> found; |
---|
| 144 | for ( const Decl * decl : aggr()->members ) { |
---|
| 145 | if ( decl->name == name ) { found.emplace_back( decl ); } |
---|
| 146 | } |
---|
| 147 | return found; |
---|
| 148 | } |
---|
| 149 | |
---|
[923d25a] | 150 | // --- SueInstType (StructInstType, UnionInstType, EnumInstType) |
---|
[69bafd2] | 151 | |
---|
[923d25a] | 152 | template<typename decl_t> |
---|
| 153 | SueInstType<decl_t>::SueInstType( |
---|
| 154 | const decl_t * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as ) |
---|
[e0e9a0b] | 155 | : ReferenceToType( b->name, q, move(as) ), base( b ) {} |
---|
[69bafd2] | 156 | |
---|
[923d25a] | 157 | template<typename decl_t> |
---|
| 158 | bool SueInstType<decl_t>::isComplete() const { |
---|
| 159 | return base ? base->body : false; |
---|
| 160 | } |
---|
[69bafd2] | 161 | |
---|
[923d25a] | 162 | template class SueInstType<StructDecl>; |
---|
| 163 | template class SueInstType<UnionDecl>; |
---|
| 164 | template class SueInstType<EnumDecl>; |
---|
[69bafd2] | 165 | |
---|
[514a791] | 166 | // --- TraitInstType |
---|
| 167 | |
---|
[7ff3e522] | 168 | TraitInstType::TraitInstType( |
---|
[e0e9a0b] | 169 | const TraitDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as ) |
---|
| 170 | : ReferenceToType( b->name, q, move(as) ), base( b ) {} |
---|
[514a791] | 171 | |
---|
[69bafd2] | 172 | // --- TypeInstType |
---|
| 173 | |
---|
[7ff3e522] | 174 | TypeInstType::TypeInstType( const TypeInstType & o ) |
---|
[e0e9a0b] | 175 | : ReferenceToType( o.name, o.qualifiers, copy( o.attributes ) ), base(), kind( o.kind ) { |
---|
| 176 | Pass< ForallSubstitutor > sub; |
---|
| 177 | initWithSub( o, sub ); // initialize substitution |
---|
[7ff3e522] | 178 | base = sub.core( o.base ); // apply to base type |
---|
[e0e9a0b] | 179 | } |
---|
| 180 | |
---|
[69bafd2] | 181 | void TypeInstType::set_base( const TypeDecl * b ) { |
---|
| 182 | base = b; |
---|
| 183 | kind = b->kind; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | bool TypeInstType::isComplete() const { return base->sized; } |
---|
| 187 | |
---|
| 188 | // --- TupleType |
---|
| 189 | |
---|
[acd80b4] | 190 | TupleType::TupleType( std::vector<ptr<Type>> && ts, CV::Qualifiers q ) |
---|
[e0e9a0b] | 191 | : Type( q ), types( move(ts) ), members() { |
---|
[acd80b4] | 192 | // This constructor is awkward. `TupleType` needs to contain objects so that members can be |
---|
| 193 | // named, but members without initializer nodes end up getting constructors, which breaks |
---|
| 194 | // things. This happens because the object decls have to be visited so that their types are |
---|
| 195 | // kept in sync with the types listed here. Ultimately, the types listed here should perhaps |
---|
| 196 | // be eliminated and replaced with a list-view over members. The temporary solution is to |
---|
| 197 | // make a `ListInit` with `maybeConstructed = false`, so when the object is visited it is not |
---|
[69bafd2] | 198 | // constructed. Potential better solutions include: |
---|
[acd80b4] | 199 | // a) Separate `TupleType` from its declarations, into `TupleDecl` and `Tuple{Inst?}Type`, |
---|
[69bafd2] | 200 | // similar to the aggregate types. |
---|
[acd80b4] | 201 | // b) Separate initializer nodes better, e.g. add a `MaybeConstructed` node that is replaced |
---|
[69bafd2] | 202 | // by `genInit`, rather than the current boolean flag. |
---|
| 203 | members.reserve( types.size() ); |
---|
| 204 | for ( const Type * ty : types ) { |
---|
| 205 | members.emplace_back( new ObjectDecl{ |
---|
[acd80b4] | 206 | CodeLocation{}, "", ty, new ListInit( CodeLocation{}, {}, {}, MaybeConstruct ), |
---|
[69bafd2] | 207 | Storage::Classes{}, Linkage::Cforall } ); |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | // Local Variables: // |
---|
| 214 | // tab-width: 4 // |
---|
| 215 | // mode: c++ // |
---|
| 216 | // compile-command: "make install" // |
---|
| 217 | // End: // |
---|