| [1c01c58] | 1 | //
 | 
|---|
 | 2 | // Cforall Version 1.0.0 Copyright (C) 2016 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 | // Tables.cc --
 | 
|---|
 | 8 | //
 | 
|---|
 | 9 | // Author           : Andrew Beach
 | 
|---|
 | 10 | // Created On       : Mon Aug 31 11:11:00 2020
 | 
|---|
 | 11 | // Last Modified By : Andrew Beach
 | 
|---|
| [b583113] | 12 | // Last Modified On : Wed Apr 21 15:36:00 2021
 | 
|---|
 | 13 | // Update Count     : 2
 | 
|---|
| [1c01c58] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
| [69c5c00] | 16 | #include <SynTree/Attribute.h>
 | 
|---|
| [1c01c58] | 17 | #include <SynTree/Declaration.h>
 | 
|---|
 | 18 | #include <SynTree/Expression.h>
 | 
|---|
| [69c5c00] | 19 | #include <SynTree/Statement.h>
 | 
|---|
| [1c01c58] | 20 | #include <SynTree/Type.h>
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | namespace Virtual {
 | 
|---|
 | 23 | 
 | 
|---|
| [ecfd758] | 24 | std::string typeIdType( std::string const & type_name ) {
 | 
|---|
 | 25 |         return "__cfatid_struct_" + type_name;
 | 
|---|
 | 26 | }
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | std::string typeIdName( std::string const & type_name ) {
 | 
|---|
 | 29 |         return "__cfatid_" + type_name;
 | 
|---|
 | 30 | }
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | static std::string typeIdTypeToInstance( std::string const & type_name ) {
 | 
|---|
 | 33 |         return typeIdName(type_name.substr(16));
 | 
|---|
 | 34 | }
 | 
|---|
 | 35 | 
 | 
|---|
| [1c01c58] | 36 | std::string vtableTypeName( std::string const & name ) {
 | 
|---|
 | 37 |         return name + "_vtable";
 | 
|---|
 | 38 | }
 | 
|---|
 | 39 | 
 | 
|---|
| [ecfd758] | 40 | std::string baseTypeName( std::string const & vtable_type_name ) {
 | 
|---|
 | 41 |         return vtable_type_name.substr(0, vtable_type_name.size() - 7);
 | 
|---|
 | 42 | }
 | 
|---|
 | 43 | 
 | 
|---|
| [1c01c58] | 44 | std::string instanceName( std::string const & name ) {
 | 
|---|
 | 45 |         return std::string("_") + name + "_instance";
 | 
|---|
 | 46 | }
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 | std::string vtableInstanceName( std::string const & name ) {
 | 
|---|
 | 49 |         return instanceName( vtableTypeName( name ) );
 | 
|---|
 | 50 | }
 | 
|---|
 | 51 | 
 | 
|---|
| [b583113] | 52 | std::string concurrentDefaultVTableName() {
 | 
|---|
 | 53 |         return "_default_vtable";
 | 
|---|
 | 54 | }
 | 
|---|
 | 55 | 
 | 
|---|
| [1c01c58] | 56 | bool isVTableInstanceName( std::string const & name ) {
 | 
|---|
 | 57 |         // There are some delicate length calculations here.
 | 
|---|
 | 58 |         return 17 < name.size() && '_' == name[0] &&
 | 
|---|
 | 59 |                 std::string("_vtable_instance") == name.substr(1, name.size() - 17);
 | 
|---|
 | 60 | }
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 | static ObjectDecl * makeVtableDeclaration(
 | 
|---|
| [b583113] | 63 |                 std::string const & name,
 | 
|---|
| [1c01c58] | 64 |                 StructInstType * type, Initializer * init ) {
 | 
|---|
 | 65 |         Type::StorageClasses storage = noStorageClasses;
 | 
|---|
 | 66 |         if ( nullptr == init ) {
 | 
|---|
 | 67 |                 storage.is_extern = true;
 | 
|---|
 | 68 |         }
 | 
|---|
 | 69 |         return new ObjectDecl(
 | 
|---|
 | 70 |                 name,
 | 
|---|
 | 71 |                 storage,
 | 
|---|
 | 72 |                 LinkageSpec::Cforall,
 | 
|---|
 | 73 |                 nullptr,
 | 
|---|
 | 74 |                 type,
 | 
|---|
 | 75 |                 init
 | 
|---|
 | 76 |         );
 | 
|---|
 | 77 | }
 | 
|---|
 | 78 | 
 | 
|---|
| [b583113] | 79 | ObjectDecl * makeVtableForward( std::string const & name, StructInstType * type ) {
 | 
|---|
| [69c5c00] | 80 |         assert( type );
 | 
|---|
| [b583113] | 81 |         return makeVtableDeclaration( name, type, nullptr );
 | 
|---|
| [1c01c58] | 82 | }
 | 
|---|
 | 83 | 
 | 
|---|
 | 84 | ObjectDecl * makeVtableInstance(
 | 
|---|
| [b583113] | 85 |                 std::string const & name, StructInstType * vtableType,
 | 
|---|
 | 86 |                 Type * objectType, Initializer * init ) {
 | 
|---|
| [69c5c00] | 87 |         assert( vtableType );
 | 
|---|
 | 88 |         assert( objectType );
 | 
|---|
| [1c01c58] | 89 |         StructDecl * vtableStruct = vtableType->baseStruct;
 | 
|---|
 | 90 |         // Build the initialization
 | 
|---|
 | 91 |         if ( nullptr == init ) {
 | 
|---|
 | 92 |                 std::list< Initializer * > inits;
 | 
|---|
 | 93 | 
 | 
|---|
 | 94 |                 // This is going to have to be run before the resolver to connect expressions.
 | 
|---|
 | 95 |                 for ( auto field : vtableStruct->members ) {
 | 
|---|
 | 96 |                         if ( std::string( "parent" ) == field->name ) {
 | 
|---|
 | 97 |                                 // This will not work with polymorphic state.
 | 
|---|
 | 98 |                                 auto oField = strict_dynamic_cast< ObjectDecl * >( field );
 | 
|---|
 | 99 |                                 auto fieldType = strict_dynamic_cast< PointerType * >( oField->type );
 | 
|---|
 | 100 |                                 auto parentType = strict_dynamic_cast< StructInstType * >( fieldType->base );
 | 
|---|
 | 101 |                                 std::string const & parentInstance = instanceName( parentType->name );
 | 
|---|
 | 102 |                                 inits.push_back(
 | 
|---|
 | 103 |                                                 new SingleInit( new AddressExpr( new NameExpr( parentInstance ) ) ) );
 | 
|---|
| [ecfd758] | 104 |                         } else if ( std::string( "__cfavir_typeid" ) == field->name ) {
 | 
|---|
 | 105 |                                 std::string const & baseType = baseTypeName( vtableType->name );
 | 
|---|
 | 106 |                                 std::string const & typeId = typeIdName( baseType );
 | 
|---|
 | 107 |                                 inits.push_back( new SingleInit( new AddressExpr( new NameExpr( typeId ) ) ) );
 | 
|---|
| [1c01c58] | 108 |                         } else if ( std::string( "size" ) == field->name ) {
 | 
|---|
| [69c5c00] | 109 |                                 inits.push_back( new SingleInit( new SizeofExpr( objectType->clone() ) ) );
 | 
|---|
| [1c01c58] | 110 |                         } else if ( std::string( "align" ) == field->name ) {
 | 
|---|
| [69c5c00] | 111 |                                 inits.push_back( new SingleInit( new AlignofExpr( objectType->clone() ) ) );
 | 
|---|
| [1c01c58] | 112 |                         } else {
 | 
|---|
 | 113 |                                 inits.push_back( new SingleInit( new NameExpr( field->name ) ) );
 | 
|---|
 | 114 |                         }
 | 
|---|
 | 115 |                 }
 | 
|---|
 | 116 |                 init = new ListInit( inits );
 | 
|---|
 | 117 |         // This should initialize everything except the parent pointer, the
 | 
|---|
 | 118 |         // size-of and align-of fields. These should be inserted.
 | 
|---|
 | 119 |         } else {
 | 
|---|
 | 120 |                 assert(false);
 | 
|---|
 | 121 |         }
 | 
|---|
| [b583113] | 122 |         return makeVtableDeclaration( name, vtableType, init );
 | 
|---|
| [1c01c58] | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
| [69c5c00] | 125 | namespace {
 | 
|---|
 | 126 |         std::string const functionName = "get_exception_vtable";
 | 
|---|
 | 127 | }
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 | FunctionDecl * makeGetExceptionForward(
 | 
|---|
 | 130 |                 Type * vtableType, Type * exceptType ) {
 | 
|---|
 | 131 |         assert( vtableType );
 | 
|---|
 | 132 |         assert( exceptType );
 | 
|---|
 | 133 |         FunctionType * type = new FunctionType( noQualifiers, false );
 | 
|---|
 | 134 |         vtableType->tq.is_const = true;
 | 
|---|
 | 135 |         type->returnVals.push_back( new ObjectDecl(
 | 
|---|
 | 136 |                 "_retvalue",
 | 
|---|
 | 137 |                 noStorageClasses,
 | 
|---|
 | 138 |                 LinkageSpec::Cforall,
 | 
|---|
 | 139 |                 nullptr,
 | 
|---|
 | 140 |                 new ReferenceType( noQualifiers, vtableType ),
 | 
|---|
 | 141 |                 nullptr,
 | 
|---|
 | 142 |         { new Attribute("unused") }
 | 
|---|
 | 143 |         ) );
 | 
|---|
 | 144 |         type->parameters.push_back( new ObjectDecl(
 | 
|---|
 | 145 |                 "__unused",
 | 
|---|
 | 146 |                 noStorageClasses,
 | 
|---|
 | 147 |                 LinkageSpec::Cforall,
 | 
|---|
 | 148 |                 nullptr,
 | 
|---|
 | 149 |                 new PointerType( noQualifiers, exceptType ),
 | 
|---|
 | 150 |                 nullptr,
 | 
|---|
 | 151 |                 { new Attribute("unused") }
 | 
|---|
 | 152 |         ) );
 | 
|---|
 | 153 |         return new FunctionDecl(
 | 
|---|
 | 154 |                 functionName,
 | 
|---|
 | 155 |                 noStorageClasses,
 | 
|---|
 | 156 |                 LinkageSpec::Cforall,
 | 
|---|
 | 157 |                 type,
 | 
|---|
 | 158 |                 nullptr
 | 
|---|
 | 159 |         );
 | 
|---|
 | 160 | }
 | 
|---|
 | 161 | 
 | 
|---|
 | 162 | FunctionDecl * makeGetExceptionFunction(
 | 
|---|
 | 163 |                 ObjectDecl * vtableInstance, Type * exceptType ) {
 | 
|---|
 | 164 |         assert( vtableInstance );
 | 
|---|
 | 165 |         assert( exceptType );
 | 
|---|
 | 166 |         FunctionDecl * func = makeGetExceptionForward(
 | 
|---|
 | 167 |                 vtableInstance->type->clone(), exceptType );
 | 
|---|
 | 168 |         func->statements = new CompoundStmt( {
 | 
|---|
 | 169 |                 new ReturnStmt( new VariableExpr( vtableInstance ) ),
 | 
|---|
 | 170 |         } );
 | 
|---|
 | 171 |         return func;
 | 
|---|
| [1c01c58] | 172 | }
 | 
|---|
 | 173 | 
 | 
|---|
| [ecfd758] | 174 | ObjectDecl * makeTypeIdInstance( StructInstType const * typeIdType ) {
 | 
|---|
 | 175 |         assert( typeIdType );
 | 
|---|
 | 176 |         StructInstType * type = typeIdType->clone();
 | 
|---|
 | 177 |         type->tq.is_const = true;
 | 
|---|
 | 178 |         std::string const & typeid_name = typeIdTypeToInstance( typeIdType->name );
 | 
|---|
 | 179 |         return new ObjectDecl(
 | 
|---|
 | 180 |                 typeid_name,
 | 
|---|
 | 181 |                 noStorageClasses,
 | 
|---|
 | 182 |                 LinkageSpec::Cforall,
 | 
|---|
 | 183 |                 /* bitfieldWidth */ nullptr,
 | 
|---|
 | 184 |                 type,
 | 
|---|
 | 185 |                 new ListInit( { new SingleInit(
 | 
|---|
 | 186 |                         new AddressExpr( new NameExpr( "__cfatid_exception_t" ) )
 | 
|---|
 | 187 |                         ) } ),
 | 
|---|
| [aff7e86] | 188 |                 { new Attribute( "cfa_linkonce", {} ) },
 | 
|---|
| [ecfd758] | 189 |                 noFuncSpecifiers
 | 
|---|
 | 190 |         );
 | 
|---|
 | 191 | }
 | 
|---|
 | 192 | 
 | 
|---|
| [1c01c58] | 193 | }
 | 
|---|