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