[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 |
---|
[cfaa2873] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Sun Dec 15 16:56:28 2019 |
---|
| 13 | // Update Count : 4 |
---|
[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" |
---|
| 24 | #include "InitTweak/InitTweak.h" // for getPointerBase |
---|
| 25 | #include "Tuples/Tuples.h" // for isTtype |
---|
| 26 | |
---|
| 27 | namespace ast { |
---|
| 28 | |
---|
[d76c588] | 29 | const Type * Type::getComponent( unsigned i ) const { |
---|
[69bafd2] | 30 | assertf( size() == 1 && i == 0, "Type::getComponent was called with size %d and index %d\n", size(), i ); |
---|
| 31 | return this; |
---|
| 32 | } |
---|
| 33 | |
---|
[d76c588] | 34 | const Type * Type::stripDeclarator() const { |
---|
[69bafd2] | 35 | const Type * t; |
---|
| 36 | const Type * a; |
---|
| 37 | for ( t = this; (a = InitTweak::getPointerBase( t )); t = a ); |
---|
| 38 | return t; |
---|
| 39 | } |
---|
| 40 | |
---|
[d76c588] | 41 | const Type * Type::stripReferences() const { |
---|
[69bafd2] | 42 | const Type * t; |
---|
| 43 | const ReferenceType * r; |
---|
[acd80b4] | 44 | for ( t = this; (r = dynamic_cast<const ReferenceType *>(t) ); t = r->base ); |
---|
[69bafd2] | 45 | return t; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | // --- BasicType |
---|
| 49 | |
---|
[cfaa2873] | 50 | // GENERATED START, DO NOT EDIT |
---|
| 51 | // GENERATED BY BasicTypes-gen.cc |
---|
| 52 | const char * BasicType::typeNames[] = { |
---|
[69bafd2] | 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 | }; |
---|
[cfaa2873] | 90 | // GENERATED END |
---|
[69bafd2] | 91 | |
---|
| 92 | // --- FunctionType |
---|
| 93 | |
---|
| 94 | namespace { |
---|
| 95 | bool containsTtype( const std::vector<ptr<DeclWithType>> & l ) { |
---|
| 96 | if ( ! l.empty() ) { |
---|
| 97 | return Tuples::isTtype( l.back()->get_type() ); |
---|
| 98 | } |
---|
| 99 | return false; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | bool FunctionType::isTtype() const { |
---|
[acd80b4] | 104 | return containsTtype( returns ) || containsTtype( params ); |
---|
[69bafd2] | 105 | } |
---|
| 106 | |
---|
| 107 | // --- ReferenceToType |
---|
| 108 | std::vector<readonly<Decl>> ReferenceToType::lookup( const std::string& name ) const { |
---|
| 109 | assertf( aggr(), "Must have aggregate to perform lookup" ); |
---|
[acd80b4] | 110 | |
---|
[69bafd2] | 111 | std::vector<readonly<Decl>> found; |
---|
| 112 | for ( const Decl * decl : aggr()->members ) { |
---|
| 113 | if ( decl->name == name ) { found.emplace_back( decl ); } |
---|
| 114 | } |
---|
| 115 | return found; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | // --- StructInstType |
---|
| 119 | |
---|
[acd80b4] | 120 | StructInstType::StructInstType( const StructDecl * b, CV::Qualifiers q, |
---|
| 121 | std::vector<ptr<Attribute>>&& as ) |
---|
[69bafd2] | 122 | : ReferenceToType( b->name, q, std::move(as) ), base( b ) {} |
---|
| 123 | |
---|
| 124 | bool StructInstType::isComplete() const { return base ? base->body : false; } |
---|
| 125 | |
---|
| 126 | // --- UnionInstType |
---|
| 127 | |
---|
[acd80b4] | 128 | UnionInstType::UnionInstType( const UnionDecl * b, CV::Qualifiers q, |
---|
| 129 | std::vector<ptr<Attribute>>&& as ) |
---|
[69bafd2] | 130 | : ReferenceToType( b->name, q, std::move(as) ), base( b ) {} |
---|
| 131 | |
---|
| 132 | bool UnionInstType::isComplete() const { return base ? base->body : false; } |
---|
| 133 | |
---|
| 134 | // --- EnumInstType |
---|
| 135 | |
---|
[acd80b4] | 136 | EnumInstType::EnumInstType( const EnumDecl * b, CV::Qualifiers q, |
---|
| 137 | std::vector<ptr<Attribute>>&& as ) |
---|
[69bafd2] | 138 | : ReferenceToType( b->name, q, std::move(as) ), base( b ) {} |
---|
| 139 | |
---|
| 140 | bool EnumInstType::isComplete() const { return base ? base->body : false; } |
---|
| 141 | |
---|
[514a791] | 142 | // --- TraitInstType |
---|
| 143 | |
---|
| 144 | TraitInstType::TraitInstType( const TraitDecl * b, CV::Qualifiers q, |
---|
| 145 | std::vector<ptr<Attribute>>&& as ) |
---|
| 146 | : ReferenceToType( b->name, q, std::move(as) ), base( b ) {} |
---|
| 147 | |
---|
[69bafd2] | 148 | // --- TypeInstType |
---|
| 149 | |
---|
| 150 | void TypeInstType::set_base( const TypeDecl * b ) { |
---|
| 151 | base = b; |
---|
| 152 | kind = b->kind; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | bool TypeInstType::isComplete() const { return base->sized; } |
---|
| 156 | |
---|
| 157 | // --- TupleType |
---|
| 158 | |
---|
[acd80b4] | 159 | TupleType::TupleType( std::vector<ptr<Type>> && ts, CV::Qualifiers q ) |
---|
[69bafd2] | 160 | : Type( q ), types( std::move(ts) ), members() { |
---|
[acd80b4] | 161 | // This constructor is awkward. `TupleType` needs to contain objects so that members can be |
---|
| 162 | // named, but members without initializer nodes end up getting constructors, which breaks |
---|
| 163 | // things. This happens because the object decls have to be visited so that their types are |
---|
| 164 | // kept in sync with the types listed here. Ultimately, the types listed here should perhaps |
---|
| 165 | // be eliminated and replaced with a list-view over members. The temporary solution is to |
---|
| 166 | // make a `ListInit` with `maybeConstructed = false`, so when the object is visited it is not |
---|
[69bafd2] | 167 | // constructed. Potential better solutions include: |
---|
[acd80b4] | 168 | // a) Separate `TupleType` from its declarations, into `TupleDecl` and `Tuple{Inst?}Type`, |
---|
[69bafd2] | 169 | // similar to the aggregate types. |
---|
[acd80b4] | 170 | // b) Separate initializer nodes better, e.g. add a `MaybeConstructed` node that is replaced |
---|
[69bafd2] | 171 | // by `genInit`, rather than the current boolean flag. |
---|
| 172 | members.reserve( types.size() ); |
---|
| 173 | for ( const Type * ty : types ) { |
---|
| 174 | members.emplace_back( new ObjectDecl{ |
---|
[acd80b4] | 175 | CodeLocation{}, "", ty, new ListInit( CodeLocation{}, {}, {}, MaybeConstruct ), |
---|
[69bafd2] | 176 | Storage::Classes{}, Linkage::Cforall } ); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | // Local Variables: // |
---|
| 183 | // tab-width: 4 // |
---|
| 184 | // mode: c++ // |
---|
| 185 | // compile-command: "make install" // |
---|
| 186 | // End: // |
---|