| 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 : Aaron B. Moss
|
|---|
| 12 | // Last Modified On : Mon May 13 15:00:00 2019
|
|---|
| 13 | // Update Count : 1
|
|---|
| 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 | const char *BasicType::typeNames[] = {
|
|---|
| 53 | "_Bool",
|
|---|
| 54 | "char",
|
|---|
| 55 | "signed char",
|
|---|
| 56 | "unsigned char",
|
|---|
| 57 | "signed short int",
|
|---|
| 58 | "unsigned short int",
|
|---|
| 59 | "signed int",
|
|---|
| 60 | "unsigned int",
|
|---|
| 61 | "signed long int",
|
|---|
| 62 | "unsigned long int",
|
|---|
| 63 | "signed long long int",
|
|---|
| 64 | "unsigned long long int",
|
|---|
| 65 | "__int128",
|
|---|
| 66 | "unsigned __int128",
|
|---|
| 67 | "_Float16",
|
|---|
| 68 | "_Float16 _Complex",
|
|---|
| 69 | "_Float32",
|
|---|
| 70 | "_Float32 _Complex",
|
|---|
| 71 | "float",
|
|---|
| 72 | "float _Complex",
|
|---|
| 73 | "_Float32x",
|
|---|
| 74 | "_Float32x _Complex",
|
|---|
| 75 | "_Float64",
|
|---|
| 76 | "_Float64 _Complex",
|
|---|
| 77 | "double",
|
|---|
| 78 | "double _Complex",
|
|---|
| 79 | "_Float64x",
|
|---|
| 80 | "_Float64x _Complex",
|
|---|
| 81 | "__float80",
|
|---|
| 82 | "_Float128",
|
|---|
| 83 | "_Float128 _Complex",
|
|---|
| 84 | "__float128",
|
|---|
| 85 | "long double",
|
|---|
| 86 | "long double _Complex",
|
|---|
| 87 | "_Float128x",
|
|---|
| 88 | "_Float128x _Complex",
|
|---|
| 89 | };
|
|---|
| 90 | static_assert(
|
|---|
| 91 | sizeof(BasicType::typeNames)/sizeof(BasicType::typeNames[0]) == BasicType::NUMBER_OF_BASIC_TYPES,
|
|---|
| 92 | "Each basic type name should have a corresponding kind enum value"
|
|---|
| 93 | );
|
|---|
| 94 |
|
|---|
| 95 | // --- ParameterizedType
|
|---|
| 96 |
|
|---|
| 97 | void ParameterizedType::initWithSub(
|
|---|
| 98 | const ParameterizedType & o, Pass< ForallSubstitutor > & sub
|
|---|
| 99 | ) {
|
|---|
| 100 | forall = sub.pass( o.forall );
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | // --- FunctionType
|
|---|
| 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.pass( o.returns ); // apply to return and parameter types
|
|---|
| 111 | params = sub.pass( o.params );
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | namespace {
|
|---|
| 115 | bool containsTtype( const std::vector<ptr<DeclWithType>> & l ) {
|
|---|
| 116 | if ( ! l.empty() ) {
|
|---|
| 117 | return Tuples::isTtype( l.back()->get_type() );
|
|---|
| 118 | }
|
|---|
| 119 | return false;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | bool FunctionType::isTtype() const {
|
|---|
| 124 | return containsTtype( returns ) || containsTtype( params );
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | // --- ReferenceToType
|
|---|
| 128 |
|
|---|
| 129 | void ReferenceToType::initWithSub( const ReferenceToType & o, Pass< ForallSubstitutor > & sub ) {
|
|---|
| 130 | ParameterizedType::initWithSub( o, sub ); // initialize substitution
|
|---|
| 131 | params = sub.pass( o.params ); // apply to parameters
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | ReferenceToType::ReferenceToType( const ReferenceToType & 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>> ReferenceToType::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 | // --- StructInstType
|
|---|
| 152 |
|
|---|
| 153 | StructInstType::StructInstType(
|
|---|
| 154 | const StructDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
|
|---|
| 155 | : ReferenceToType( b->name, q, move(as) ), base( b ) {}
|
|---|
| 156 |
|
|---|
| 157 | bool StructInstType::isComplete() const { return base ? base->body : false; }
|
|---|
| 158 |
|
|---|
| 159 | // --- UnionInstType
|
|---|
| 160 |
|
|---|
| 161 | UnionInstType::UnionInstType(
|
|---|
| 162 | const UnionDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
|
|---|
| 163 | : ReferenceToType( b->name, q, move(as) ), base( b ) {}
|
|---|
| 164 |
|
|---|
| 165 | bool UnionInstType::isComplete() const { return base ? base->body : false; }
|
|---|
| 166 |
|
|---|
| 167 | // --- EnumInstType
|
|---|
| 168 |
|
|---|
| 169 | EnumInstType::EnumInstType(
|
|---|
| 170 | const EnumDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
|
|---|
| 171 | : ReferenceToType( b->name, q, move(as) ), base( b ) {}
|
|---|
| 172 |
|
|---|
| 173 | bool EnumInstType::isComplete() const { return base ? base->body : false; }
|
|---|
| 174 |
|
|---|
| 175 | // --- TraitInstType
|
|---|
| 176 |
|
|---|
| 177 | TraitInstType::TraitInstType(
|
|---|
| 178 | const TraitDecl * b, CV::Qualifiers q, std::vector<ptr<Attribute>>&& as )
|
|---|
| 179 | : ReferenceToType( b->name, q, move(as) ), base( b ) {}
|
|---|
| 180 |
|
|---|
| 181 | // --- TypeInstType
|
|---|
| 182 |
|
|---|
| 183 | TypeInstType::TypeInstType( const TypeInstType & o )
|
|---|
| 184 | : ReferenceToType( o.name, o.qualifiers, copy( o.attributes ) ), base(), kind( o.kind ) {
|
|---|
| 185 | Pass< ForallSubstitutor > sub;
|
|---|
| 186 | initWithSub( o, sub ); // initialize substitution
|
|---|
| 187 | base = sub.pass( o.base ); // apply to base type
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | void TypeInstType::set_base( const TypeDecl * b ) {
|
|---|
| 191 | base = b;
|
|---|
| 192 | kind = b->kind;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | bool TypeInstType::isComplete() const { return base->sized; }
|
|---|
| 196 |
|
|---|
| 197 | // --- TupleType
|
|---|
| 198 |
|
|---|
| 199 | TupleType::TupleType( std::vector<ptr<Type>> && ts, CV::Qualifiers q )
|
|---|
| 200 | : Type( q ), types( move(ts) ), members() {
|
|---|
| 201 | // This constructor is awkward. `TupleType` needs to contain objects so that members can be
|
|---|
| 202 | // named, but members without initializer nodes end up getting constructors, which breaks
|
|---|
| 203 | // things. This happens because the object decls have to be visited so that their types are
|
|---|
| 204 | // kept in sync with the types listed here. Ultimately, the types listed here should perhaps
|
|---|
| 205 | // be eliminated and replaced with a list-view over members. The temporary solution is to
|
|---|
| 206 | // make a `ListInit` with `maybeConstructed = false`, so when the object is visited it is not
|
|---|
| 207 | // constructed. Potential better solutions include:
|
|---|
| 208 | // a) Separate `TupleType` from its declarations, into `TupleDecl` and `Tuple{Inst?}Type`,
|
|---|
| 209 | // similar to the aggregate types.
|
|---|
| 210 | // b) Separate initializer nodes better, e.g. add a `MaybeConstructed` node that is replaced
|
|---|
| 211 | // by `genInit`, rather than the current boolean flag.
|
|---|
| 212 | members.reserve( types.size() );
|
|---|
| 213 | for ( const Type * ty : types ) {
|
|---|
| 214 | members.emplace_back( new ObjectDecl{
|
|---|
| 215 | CodeLocation{}, "", ty, new ListInit( CodeLocation{}, {}, {}, MaybeConstruct ),
|
|---|
| 216 | Storage::Classes{}, Linkage::Cforall } );
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | // Local Variables: //
|
|---|
| 223 | // tab-width: 4 //
|
|---|
| 224 | // mode: c++ //
|
|---|
| 225 | // compile-command: "make install" //
|
|---|
| 226 | // End: //
|
|---|