| 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
|
|---|
| 11 | // Last Modified By : Andrew Beach
|
|---|
| 12 | // Last Modified On : Thu Jul 23 14:16:00 2020
|
|---|
| 13 | // Update Count : 5
|
|---|
| 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 "ForallSubstitutor.hpp" // for substituteForall
|
|---|
| 24 | #include "Init.hpp"
|
|---|
| 25 | #include "Common/utility.h" // for copy, move
|
|---|
| 26 | #include "InitTweak/InitTweak.h" // for getPointerBase
|
|---|
| 27 | #include "Tuples/Tuples.h" // for isTtype
|
|---|
| 28 |
|
|---|
| 29 | namespace ast {
|
|---|
| 30 |
|
|---|
| 31 | const Type * Type::getComponent( unsigned i ) const {
|
|---|
| 32 | assertf( size() == 1 && i == 0, "Type::getComponent was called with size %d and index %d\n", size(), i );
|
|---|
| 33 | return this;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | const Type * Type::stripDeclarator() const {
|
|---|
| 37 | const Type * t;
|
|---|
| 38 | const Type * a;
|
|---|
| 39 | for ( t = this; (a = InitTweak::getPointerBase( t )); t = a );
|
|---|
| 40 | return t;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | const Type * Type::stripReferences() const {
|
|---|
| 44 | const Type * t;
|
|---|
| 45 | const ReferenceType * r;
|
|---|
| 46 | for ( t = this; (r = dynamic_cast<const ReferenceType *>(t) ); t = r->base );
|
|---|
| 47 | return t;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | // --- BasicType
|
|---|
| 51 |
|
|---|
| 52 | // GENERATED START, DO NOT EDIT
|
|---|
| 53 | // GENERATED BY BasicTypes-gen.cc
|
|---|
| 54 | const char * BasicType::typeNames[] = {
|
|---|
| 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 | };
|
|---|
| 92 | // GENERATED END
|
|---|
| 93 |
|
|---|
| 94 | // --- ParameterizedType
|
|---|
| 95 |
|
|---|
| 96 | void ParameterizedType::initWithSub(
|
|---|
| 97 | const ParameterizedType & o, Pass< ForallSubstitutor > & sub
|
|---|
| 98 | ) {
|
|---|
| 99 | forall = sub.core( o.forall );
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | // --- FunctionType
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 | FunctionType::FunctionType( const FunctionType & o )
|
|---|
| 106 | : ParameterizedType( o.qualifiers, copy( o.attributes ) ), returns(), params(),
|
|---|
| 107 | isVarArgs( o.isVarArgs ) {
|
|---|
| 108 | Pass< ForallSubstitutor > sub;
|
|---|
| 109 | initWithSub( o, sub ); // initialize substitution map
|
|---|
| 110 | returns = sub.core( o.returns ); // apply to return and parameter types
|
|---|
| 111 | params = sub.core( o.params );
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | namespace {
|
|---|
| 115 | bool containsTtype( const std::vector<ptr<Type>> & l ) {
|
|---|
| 116 | if ( ! l.empty() ) {
|
|---|
| 117 | return Tuples::isTtype( l.back() );
|
|---|
| 118 | }
|
|---|
| 119 | return false;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | bool FunctionType::isTtype() const {
|
|---|
| 124 | return containsTtype( returns ) || containsTtype( params );
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | // --- BaseInstType
|
|---|
| 128 |
|
|---|
| 129 | void BaseInstType::initWithSub( const BaseInstType & o, Pass< ForallSubstitutor > & sub ) {
|
|---|
| 130 | ParameterizedType::initWithSub( o, sub ); // initialize substitution
|
|---|
| 131 | params = sub.core( o.params ); // apply to parameters
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | BaseInstType::BaseInstType( const BaseInstType & o )
|
|---|
| 135 | : ParameterizedType( o.qualifiers, copy( o.attributes ) ), params(), name( o.name ),
|
|---|
| 136 | hoistType( o.hoistType ) {
|
|---|
| 137 | Pass< ForallSubstitutor > sub;
|
|---|
| 138 | initWithSub( o, sub );
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | std::vector<readonly<Decl>> BaseInstType::lookup( const std::string& name ) const {
|
|---|
| 142 | assertf( aggr(), "Must have aggregate to perform lookup" );
|
|---|
| 143 |
|
|---|
| 144 | std::vector<readonly<Decl>> found;
|
|---|
| 145 | for ( const Decl * decl : aggr()->members ) {
|
|---|
| 146 | if ( decl->name == name ) { found.emplace_back( decl ); }
|
|---|
| 147 | }
|
|---|
| 148 | return found;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | // --- SueInstType (StructInstType, UnionInstType, EnumInstType)
|
|---|
| 152 |
|
|---|
| 153 | template<typename decl_t>
|
|---|
| 154 | SueInstType<decl_t>::SueInstType(
|
|---|
| 155 | const decl_t * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
|
|---|
| 156 | : BaseInstType( b->name, q, move(as) ), base( b ) {}
|
|---|
| 157 |
|
|---|
| 158 | template<typename decl_t>
|
|---|
| 159 | SueInstType<decl_t>::SueInstType(
|
|---|
| 160 | const base_type * b, std::vector<ptr<Expr>> && params,
|
|---|
| 161 | CV::Qualifiers q, std::vector<ptr<Attribute>> && as )
|
|---|
| 162 | : BaseInstType( b->name, std::move(params), q, std::move(as) ), base( b ) {}
|
|---|
| 163 |
|
|---|
| 164 | template<typename decl_t>
|
|---|
| 165 | bool SueInstType<decl_t>::isComplete() const {
|
|---|
| 166 | return base ? base->body : false;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | template class SueInstType<StructDecl>;
|
|---|
| 170 | template class SueInstType<UnionDecl>;
|
|---|
| 171 | template class SueInstType<EnumDecl>;
|
|---|
| 172 |
|
|---|
| 173 | // --- TraitInstType
|
|---|
| 174 |
|
|---|
| 175 | TraitInstType::TraitInstType(
|
|---|
| 176 | const TraitDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
|
|---|
| 177 | : BaseInstType( b->name, q, move(as) ), base( b ) {}
|
|---|
| 178 |
|
|---|
| 179 | // --- TypeInstType
|
|---|
| 180 |
|
|---|
| 181 | TypeInstType::TypeInstType( const TypeInstType & o )
|
|---|
| 182 | : BaseInstType( o.name, o.qualifiers, copy( o.attributes ) ), base(), kind( o.kind ) {
|
|---|
| 183 | Pass< ForallSubstitutor > sub;
|
|---|
| 184 | initWithSub( o, sub ); // initialize substitution
|
|---|
| 185 | base = sub.core( o.base ); // apply to base type
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | void TypeInstType::set_base( const TypeDecl * b ) {
|
|---|
| 189 | base = b;
|
|---|
| 190 | kind = b->kind;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | bool TypeInstType::isComplete() const { return base->sized; }
|
|---|
| 194 |
|
|---|
| 195 | // --- TupleType
|
|---|
| 196 |
|
|---|
| 197 | TupleType::TupleType( std::vector<ptr<Type>> && ts, CV::Qualifiers q )
|
|---|
| 198 | : Type( q ), types( move(ts) ), members() {
|
|---|
| 199 | // This constructor is awkward. `TupleType` needs to contain objects so that members can be
|
|---|
| 200 | // named, but members without initializer nodes end up getting constructors, which breaks
|
|---|
| 201 | // things. This happens because the object decls have to be visited so that their types are
|
|---|
| 202 | // kept in sync with the types listed here. Ultimately, the types listed here should perhaps
|
|---|
| 203 | // be eliminated and replaced with a list-view over members. The temporary solution is to
|
|---|
| 204 | // make a `ListInit` with `maybeConstructed = false`, so when the object is visited it is not
|
|---|
| 205 | // constructed. Potential better solutions include:
|
|---|
| 206 | // a) Separate `TupleType` from its declarations, into `TupleDecl` and `Tuple{Inst?}Type`,
|
|---|
| 207 | // similar to the aggregate types.
|
|---|
| 208 | // b) Separate initializer nodes better, e.g. add a `MaybeConstructed` node that is replaced
|
|---|
| 209 | // by `genInit`, rather than the current boolean flag.
|
|---|
| 210 | members.reserve( types.size() );
|
|---|
| 211 | for ( const Type * ty : types ) {
|
|---|
| 212 | members.emplace_back( new ObjectDecl{
|
|---|
| 213 | CodeLocation{}, "", ty, new ListInit( CodeLocation{}, {}, {}, NoConstruct ),
|
|---|
| 214 | Storage::Classes{}, Linkage::Cforall } );
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | bool isUnboundType(const Type * type) {
|
|---|
| 219 | if (auto typeInst = dynamic_cast<const TypeInstType *>(type)) {
|
|---|
| 220 | // xxx - look for a type name produced by renameTyVars.
|
|---|
| 221 |
|
|---|
| 222 | // TODO: once TypeInstType representation is updated, it should properly check
|
|---|
| 223 | // if the context id is filled. this is a temporary hack for now
|
|---|
| 224 | return isUnboundType(typeInst->name);
|
|---|
| 225 | }
|
|---|
| 226 | return false;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | bool isUnboundType(const std::string & tname) {
|
|---|
| 230 | // xxx - look for a type name produced by renameTyVars.
|
|---|
| 231 |
|
|---|
| 232 | // TODO: once TypeInstType representation is updated, it should properly check
|
|---|
| 233 | // if the context id is filled. this is a temporary hack for now
|
|---|
| 234 | if (std::count(tname.begin(), tname.end(), '_') >= 3) {
|
|---|
| 235 | return true;
|
|---|
| 236 | }
|
|---|
| 237 | return false;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | // Local Variables: //
|
|---|
| 243 | // tab-width: 4 //
|
|---|
| 244 | // mode: c++ //
|
|---|
| 245 | // compile-command: "make install" //
|
|---|
| 246 | // End: //
|
|---|