[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 |
---|
[ecfd758] | 12 | // Last Modified On : Thr Apr 8 15:51:00 2021 |
---|
| 13 | // Update Count : 1 |
---|
[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 | |
---|
| 52 | bool isVTableInstanceName( std::string const & name ) { |
---|
| 53 | // There are some delicate length calculations here. |
---|
| 54 | return 17 < name.size() && '_' == name[0] && |
---|
| 55 | std::string("_vtable_instance") == name.substr(1, name.size() - 17); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | static ObjectDecl * makeVtableDeclaration( |
---|
| 59 | StructInstType * type, Initializer * init ) { |
---|
| 60 | std::string const & name = instanceName( type->name ); |
---|
| 61 | Type::StorageClasses storage = noStorageClasses; |
---|
| 62 | if ( nullptr == init ) { |
---|
| 63 | storage.is_extern = true; |
---|
| 64 | } |
---|
| 65 | return new ObjectDecl( |
---|
| 66 | name, |
---|
| 67 | storage, |
---|
| 68 | LinkageSpec::Cforall, |
---|
| 69 | nullptr, |
---|
| 70 | type, |
---|
| 71 | init |
---|
| 72 | ); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | ObjectDecl * makeVtableForward( StructInstType * type ) { |
---|
[69c5c00] | 76 | assert( type ); |
---|
[1c01c58] | 77 | return makeVtableDeclaration( type, nullptr ); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | ObjectDecl * makeVtableInstance( |
---|
[69c5c00] | 81 | StructInstType * vtableType, Type * objectType, Initializer * init ) { |
---|
| 82 | assert( vtableType ); |
---|
| 83 | assert( objectType ); |
---|
[1c01c58] | 84 | StructDecl * vtableStruct = vtableType->baseStruct; |
---|
| 85 | // Build the initialization |
---|
| 86 | if ( nullptr == init ) { |
---|
| 87 | std::list< Initializer * > inits; |
---|
| 88 | |
---|
| 89 | // This is going to have to be run before the resolver to connect expressions. |
---|
| 90 | for ( auto field : vtableStruct->members ) { |
---|
| 91 | if ( std::string( "parent" ) == field->name ) { |
---|
| 92 | // This will not work with polymorphic state. |
---|
| 93 | auto oField = strict_dynamic_cast< ObjectDecl * >( field ); |
---|
| 94 | auto fieldType = strict_dynamic_cast< PointerType * >( oField->type ); |
---|
| 95 | auto parentType = strict_dynamic_cast< StructInstType * >( fieldType->base ); |
---|
| 96 | std::string const & parentInstance = instanceName( parentType->name ); |
---|
| 97 | inits.push_back( |
---|
| 98 | new SingleInit( new AddressExpr( new NameExpr( parentInstance ) ) ) ); |
---|
[ecfd758] | 99 | } else if ( std::string( "__cfavir_typeid" ) == field->name ) { |
---|
| 100 | std::string const & baseType = baseTypeName( vtableType->name ); |
---|
| 101 | std::string const & typeId = typeIdName( baseType ); |
---|
| 102 | inits.push_back( new SingleInit( new AddressExpr( new NameExpr( typeId ) ) ) ); |
---|
[1c01c58] | 103 | } else if ( std::string( "size" ) == field->name ) { |
---|
[69c5c00] | 104 | inits.push_back( new SingleInit( new SizeofExpr( objectType->clone() ) ) ); |
---|
[1c01c58] | 105 | } else if ( std::string( "align" ) == field->name ) { |
---|
[69c5c00] | 106 | inits.push_back( new SingleInit( new AlignofExpr( objectType->clone() ) ) ); |
---|
[1c01c58] | 107 | } else { |
---|
| 108 | inits.push_back( new SingleInit( new NameExpr( field->name ) ) ); |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | init = new ListInit( inits ); |
---|
| 112 | // This should initialize everything except the parent pointer, the |
---|
| 113 | // size-of and align-of fields. These should be inserted. |
---|
| 114 | } else { |
---|
| 115 | assert(false); |
---|
| 116 | } |
---|
| 117 | return makeVtableDeclaration( vtableType, init ); |
---|
| 118 | } |
---|
| 119 | |
---|
[69c5c00] | 120 | namespace { |
---|
| 121 | std::string const functionName = "get_exception_vtable"; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | FunctionDecl * makeGetExceptionForward( |
---|
| 125 | Type * vtableType, Type * exceptType ) { |
---|
| 126 | assert( vtableType ); |
---|
| 127 | assert( exceptType ); |
---|
| 128 | FunctionType * type = new FunctionType( noQualifiers, false ); |
---|
| 129 | vtableType->tq.is_const = true; |
---|
| 130 | type->returnVals.push_back( new ObjectDecl( |
---|
| 131 | "_retvalue", |
---|
| 132 | noStorageClasses, |
---|
| 133 | LinkageSpec::Cforall, |
---|
| 134 | nullptr, |
---|
| 135 | new ReferenceType( noQualifiers, vtableType ), |
---|
| 136 | nullptr, |
---|
| 137 | { new Attribute("unused") } |
---|
| 138 | ) ); |
---|
| 139 | type->parameters.push_back( new ObjectDecl( |
---|
| 140 | "__unused", |
---|
| 141 | noStorageClasses, |
---|
| 142 | LinkageSpec::Cforall, |
---|
| 143 | nullptr, |
---|
| 144 | new PointerType( noQualifiers, exceptType ), |
---|
| 145 | nullptr, |
---|
| 146 | { new Attribute("unused") } |
---|
| 147 | ) ); |
---|
| 148 | return new FunctionDecl( |
---|
| 149 | functionName, |
---|
| 150 | noStorageClasses, |
---|
| 151 | LinkageSpec::Cforall, |
---|
| 152 | type, |
---|
| 153 | nullptr |
---|
| 154 | ); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | FunctionDecl * makeGetExceptionFunction( |
---|
| 158 | ObjectDecl * vtableInstance, Type * exceptType ) { |
---|
| 159 | assert( vtableInstance ); |
---|
| 160 | assert( exceptType ); |
---|
| 161 | FunctionDecl * func = makeGetExceptionForward( |
---|
| 162 | vtableInstance->type->clone(), exceptType ); |
---|
| 163 | func->statements = new CompoundStmt( { |
---|
| 164 | new ReturnStmt( new VariableExpr( vtableInstance ) ), |
---|
| 165 | } ); |
---|
| 166 | return func; |
---|
[1c01c58] | 167 | } |
---|
| 168 | |
---|
[ecfd758] | 169 | ObjectDecl * makeTypeIdForward() { |
---|
| 170 | return nullptr; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | Attribute * linkonce( const std::string & subsection ) { |
---|
[b91bfde] | 174 | const std::string section = ".gnu.linkonce." + subsection; |
---|
[ecfd758] | 175 | return new Attribute( "section", { |
---|
[b91bfde] | 176 | new ConstantExpr( Constant::from_string( section ) ), |
---|
[ecfd758] | 177 | } ); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | ObjectDecl * makeTypeIdInstance( StructInstType const * typeIdType ) { |
---|
| 181 | assert( typeIdType ); |
---|
| 182 | StructInstType * type = typeIdType->clone(); |
---|
| 183 | type->tq.is_const = true; |
---|
| 184 | std::string const & typeid_name = typeIdTypeToInstance( typeIdType->name ); |
---|
| 185 | return new ObjectDecl( |
---|
| 186 | typeid_name, |
---|
| 187 | noStorageClasses, |
---|
| 188 | LinkageSpec::Cforall, |
---|
| 189 | /* bitfieldWidth */ nullptr, |
---|
| 190 | type, |
---|
| 191 | new ListInit( { new SingleInit( |
---|
| 192 | new AddressExpr( new NameExpr( "__cfatid_exception_t" ) ) |
---|
| 193 | ) } ), |
---|
| 194 | { linkonce( typeid_name ) }, |
---|
| 195 | noFuncSpecifiers |
---|
| 196 | ); |
---|
| 197 | } |
---|
| 198 | |
---|
[1c01c58] | 199 | } |
---|