| 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 | // Box.cpp -- Implement polymorphic function calls and types. | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Andrew Beach | 
|---|
| 10 | // Created On       : Thr Oct  6 13:39:00 2022 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Thu Dec 14 17:42:17 2023 | 
|---|
| 13 | // Update Count     : 7 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "Box.hpp" | 
|---|
| 17 |  | 
|---|
| 18 | #include "AST/Decl.hpp"                // for Decl, FunctionDecl, ... | 
|---|
| 19 | #include "AST/Expr.hpp"                // for AlignofExpr, ConstantExpr, ... | 
|---|
| 20 | #include "AST/Init.hpp"                // for Init, SingleInit | 
|---|
| 21 | #include "AST/Inspect.hpp"             // for getFunctionName | 
|---|
| 22 | #include "AST/Pass.hpp"                // for Pass, WithDeclsToAdd, ... | 
|---|
| 23 | #include "AST/Stmt.hpp"                // for CompoundStmt, ExprStmt, ... | 
|---|
| 24 | #include "AST/Vector.hpp"              // for vector | 
|---|
| 25 | #include "AST/GenericSubstitution.hpp" // for genericSubstitution | 
|---|
| 26 | #include "CodeGen/OperatorTable.hpp"   // for isAssignment | 
|---|
| 27 | #include "Common/Iterate.hpp"          // for group_iterate | 
|---|
| 28 | #include "Common/ScopedMap.hpp"        // for ScopedMap | 
|---|
| 29 | #include "Common/ToString.hpp"         // for toCString | 
|---|
| 30 | #include "Common/UniqueName.hpp"       // for UniqueName | 
|---|
| 31 | #include "GenPoly/FindFunction.hpp"    // for findFunction | 
|---|
| 32 | #include "GenPoly/GenPoly.hpp"         // for getFunctionType, ... | 
|---|
| 33 | #include "GenPoly/Lvalue.hpp"          // for generalizedLvalue | 
|---|
| 34 | #include "GenPoly/ScopedSet.hpp"       // for ScopedSet | 
|---|
| 35 | #include "GenPoly/ScrubTypeVars.hpp"   // for scrubTypeVars, scrubAllTypeVars | 
|---|
| 36 | #include "ResolvExpr/Unify.hpp"        // for typesCompatible | 
|---|
| 37 | #include "SymTab/Mangler.hpp"          // for mangle, mangleType | 
|---|
| 38 |  | 
|---|
| 39 | namespace GenPoly { | 
|---|
| 40 |  | 
|---|
| 41 | namespace { | 
|---|
| 42 |  | 
|---|
| 43 | /// The layout type is used to represent sizes, alignments and offsets. | 
|---|
| 44 | ast::BasicType * makeLayoutType() { | 
|---|
| 45 | return new ast::BasicType( ast::BasicKind::LongUnsignedInt ); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | /// Fixed version of layout type (just adding a 'C' in C++ style). | 
|---|
| 49 | ast::BasicType * makeLayoutCType() { | 
|---|
| 50 | return new ast::BasicType( ast::BasicKind::LongUnsignedInt, | 
|---|
| 51 | ast::CV::Qualifiers( ast::CV::Const ) ); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | // -------------------------------------------------------------------------- | 
|---|
| 55 | /// Adds layout-generation functions to polymorphic types. | 
|---|
| 56 | struct LayoutFunctionBuilder final : | 
|---|
| 57 | public ast::WithDeclsToAdd<>, | 
|---|
| 58 | public ast::WithShortCircuiting, | 
|---|
| 59 | public ast::WithVisitorRef<LayoutFunctionBuilder> { | 
|---|
| 60 | void previsit( ast::StructDecl const * decl ); | 
|---|
| 61 | void previsit( ast::UnionDecl const * decl ); | 
|---|
| 62 | }; | 
|---|
| 63 |  | 
|---|
| 64 | /// Get all sized type declarations; those that affect a layout function. | 
|---|
| 65 | ast::vector<ast::TypeDecl> takeSizedParams( | 
|---|
| 66 | ast::vector<ast::TypeDecl> const & decls ) { | 
|---|
| 67 | ast::vector<ast::TypeDecl> sizedParams; | 
|---|
| 68 | for ( ast::ptr<ast::TypeDecl> const & decl : decls ) { | 
|---|
| 69 | if ( decl->isComplete() ) { | 
|---|
| 70 | sizedParams.emplace_back( decl ); | 
|---|
| 71 | } | 
|---|
| 72 | } | 
|---|
| 73 | return sizedParams; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | /// Adds parameters for otype size and alignment to a function type. | 
|---|
| 77 | void addSTypeParams( | 
|---|
| 78 | ast::vector<ast::DeclWithType> & params, | 
|---|
| 79 | ast::vector<ast::TypeDecl> const & sizedParams ) { | 
|---|
| 80 | for ( ast::ptr<ast::TypeDecl> const & sizedParam : sizedParams ) { | 
|---|
| 81 | ast::TypeInstType inst( sizedParam ); | 
|---|
| 82 | std::string paramName = Mangle::mangleType( &inst ); | 
|---|
| 83 | params.emplace_back( new ast::ObjectDecl( | 
|---|
| 84 | sizedParam->location, | 
|---|
| 85 | sizeofName( paramName ), | 
|---|
| 86 | makeLayoutCType() | 
|---|
| 87 | ) ); | 
|---|
| 88 | params.emplace_back( new ast::ObjectDecl( | 
|---|
| 89 | sizedParam->location, | 
|---|
| 90 | alignofName( paramName ), | 
|---|
| 91 | makeLayoutCType() | 
|---|
| 92 | ) ); | 
|---|
| 93 | } | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | ast::Type * makeLayoutOutType() { | 
|---|
| 97 | return new ast::PointerType( makeLayoutType() ); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | struct LayoutData { | 
|---|
| 101 | ast::FunctionDecl * function; | 
|---|
| 102 | ast::ObjectDecl * sizeofParam; | 
|---|
| 103 | ast::ObjectDecl * alignofParam; | 
|---|
| 104 | ast::ObjectDecl * offsetofParam; | 
|---|
| 105 | }; | 
|---|
| 106 |  | 
|---|
| 107 | LayoutData buildLayoutFunction( | 
|---|
| 108 | CodeLocation const & location, ast::AggregateDecl const * aggr, | 
|---|
| 109 | ast::vector<ast::TypeDecl> const & sizedParams, | 
|---|
| 110 | bool isInFunction, bool isStruct ) { | 
|---|
| 111 | ast::ObjectDecl * sizeParam = new ast::ObjectDecl( | 
|---|
| 112 | location, | 
|---|
| 113 | sizeofName( aggr->name ), | 
|---|
| 114 | makeLayoutOutType() | 
|---|
| 115 | ); | 
|---|
| 116 | ast::ObjectDecl * alignParam = new ast::ObjectDecl( | 
|---|
| 117 | location, | 
|---|
| 118 | alignofName( aggr->name ), | 
|---|
| 119 | makeLayoutOutType() | 
|---|
| 120 | ); | 
|---|
| 121 | ast::ObjectDecl * offsetParam = nullptr; | 
|---|
| 122 | ast::vector<ast::DeclWithType> params = { sizeParam, alignParam }; | 
|---|
| 123 | if ( isStruct ) { | 
|---|
| 124 | offsetParam = new ast::ObjectDecl( | 
|---|
| 125 | location, | 
|---|
| 126 | offsetofName( aggr->name ), | 
|---|
| 127 | makeLayoutOutType() | 
|---|
| 128 | ); | 
|---|
| 129 | params.push_back( offsetParam ); | 
|---|
| 130 | } | 
|---|
| 131 | addSTypeParams( params, sizedParams ); | 
|---|
| 132 |  | 
|---|
| 133 | // Routines at global scope marked "static" to prevent multiple | 
|---|
| 134 | // definitions is separate translation units because each unit generates | 
|---|
| 135 | // copies of the default routines for each aggregate. | 
|---|
| 136 | ast::FunctionDecl * layoutDecl = new ast::FunctionDecl( | 
|---|
| 137 | location, | 
|---|
| 138 | layoutofName( aggr ), | 
|---|
| 139 | {}, // forall | 
|---|
| 140 | {}, // assertions | 
|---|
| 141 | std::move( params ), | 
|---|
| 142 | {}, // returns | 
|---|
| 143 | new ast::CompoundStmt( location ), | 
|---|
| 144 | isInFunction ? ast::Storage::Classes() : ast::Storage::Static, | 
|---|
| 145 | ast::Linkage::AutoGen, | 
|---|
| 146 | {}, // attrs | 
|---|
| 147 | ast::Function::Inline, | 
|---|
| 148 | ast::FixedArgs | 
|---|
| 149 | ); | 
|---|
| 150 | layoutDecl->fixUniqueId(); | 
|---|
| 151 | return LayoutData{ layoutDecl, sizeParam, alignParam, offsetParam }; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | /// Makes a binary operation. | 
|---|
| 155 | ast::Expr * makeOp( CodeLocation const & location, std::string const & name, | 
|---|
| 156 | ast::Expr const * lhs, ast::Expr const * rhs ) { | 
|---|
| 157 | return new ast::UntypedExpr( location, | 
|---|
| 158 | new ast::NameExpr( location, name ), { lhs, rhs } ); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | /// Make a binary operation and wrap it in a statement. | 
|---|
| 162 | ast::Stmt * makeOpStmt( CodeLocation const & location, std::string const & name, | 
|---|
| 163 | ast::Expr const * lhs, ast::Expr const * rhs ) { | 
|---|
| 164 | return new ast::ExprStmt( location, makeOp( location, name, lhs, rhs ) ); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | /// Returns the dereference of a local pointer variable. | 
|---|
| 168 | ast::Expr * derefVar( | 
|---|
| 169 | CodeLocation const & location, ast::ObjectDecl const * var ) { | 
|---|
| 170 | return ast::UntypedExpr::createDeref( location, | 
|---|
| 171 | new ast::VariableExpr( location, var ) ); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | /// Makes an if-statement with a single-expression then and no else. | 
|---|
| 175 | ast::Stmt * makeCond( CodeLocation const & location, | 
|---|
| 176 | ast::Expr const * cond, ast::Expr const * thenPart ) { | 
|---|
| 177 | return new ast::IfStmt( location, | 
|---|
| 178 | cond, new ast::ExprStmt( location, thenPart ), nullptr ); | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | /// Makes a statement that aligns lhs to rhs (rhs should be an integer | 
|---|
| 182 | /// power of two). | 
|---|
| 183 | ast::Stmt * makeAlignTo( CodeLocation const & location, | 
|---|
| 184 | ast::Expr const * lhs, ast::Expr const * rhs ) { | 
|---|
| 185 | // Check that the lhs is zeroed out to the level of rhs. | 
|---|
| 186 | ast::Expr * ifCond = makeOp( location, "?&?", lhs, | 
|---|
| 187 | makeOp( location, "?-?", rhs, | 
|---|
| 188 | ast::ConstantExpr::from_ulong( location, 1 ) ) ); | 
|---|
| 189 | // If not aligned, increment to alignment. | 
|---|
| 190 | ast::Expr * ifExpr = makeOp( location, "?+=?", ast::deepCopy( lhs ), | 
|---|
| 191 | makeOp( location, "?-?", ast::deepCopy( rhs ), | 
|---|
| 192 | ast::deepCopy( ifCond ) ) ); | 
|---|
| 193 | return makeCond( location, ifCond, ifExpr ); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | /// Makes a statement that assigns rhs to lhs if lhs < rhs. | 
|---|
| 197 | ast::Stmt * makeAssignMax( CodeLocation const & location, | 
|---|
| 198 | ast::Expr const * lhs, ast::Expr const * rhs ) { | 
|---|
| 199 | return makeCond( location, | 
|---|
| 200 | makeOp( location, "?<?", ast::deepCopy( lhs ), ast::deepCopy( rhs ) ), | 
|---|
| 201 | makeOp( location, "?=?", lhs, rhs ) ); | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | void LayoutFunctionBuilder::previsit( ast::StructDecl const * decl ) { | 
|---|
| 205 | // Do not generate layout function for empty tag structures. | 
|---|
| 206 | visit_children = false; | 
|---|
| 207 | if ( decl->members.empty() ) return; | 
|---|
| 208 |  | 
|---|
| 209 | // Get parameters that can change layout, exiting early if none. | 
|---|
| 210 | ast::vector<ast::TypeDecl> sizedParams = | 
|---|
| 211 | takeSizedParams( decl->params ); | 
|---|
| 212 | if ( sizedParams.empty() ) return; | 
|---|
| 213 |  | 
|---|
| 214 | CodeLocation const & location = decl->location; | 
|---|
| 215 |  | 
|---|
| 216 | // Build layout function signature. | 
|---|
| 217 | LayoutData layout = buildLayoutFunction( | 
|---|
| 218 | location, decl, sizedParams, isInFunction(), true ); | 
|---|
| 219 | ast::FunctionDecl * layoutDecl = layout.function; | 
|---|
| 220 | // Also return these or extract them from the parameter list? | 
|---|
| 221 | ast::ObjectDecl const * sizeofParam = layout.sizeofParam; | 
|---|
| 222 | ast::ObjectDecl const * alignofParam = layout.alignofParam; | 
|---|
| 223 | ast::ObjectDecl const * offsetofParam = layout.offsetofParam; | 
|---|
| 224 | assert( nullptr != layout.offsetofParam ); | 
|---|
| 225 |  | 
|---|
| 226 | // Calculate structure layout in function body. | 
|---|
| 227 | // Initialize size and alignment to 0 and 1 | 
|---|
| 228 | // (Will have at least one member to update size). | 
|---|
| 229 | auto & kids = layoutDecl->stmts.get_and_mutate()->kids; | 
|---|
| 230 | kids.emplace_back( makeOpStmt( location, "?=?", | 
|---|
| 231 | derefVar( location, sizeofParam ), | 
|---|
| 232 | ast::ConstantExpr::from_ulong( location, 0 ) | 
|---|
| 233 | ) ); | 
|---|
| 234 | kids.emplace_back( makeOpStmt( location, "?=?", | 
|---|
| 235 | derefVar( location, alignofParam ), | 
|---|
| 236 | ast::ConstantExpr::from_ulong( location, 1 ) | 
|---|
| 237 | ) ); | 
|---|
| 238 | // TODO: Polymorphic types will be out of the struct declaration scope. | 
|---|
| 239 | // This breaks invariants until it is corrected later. | 
|---|
| 240 | for ( auto const & member : enumerate( decl->members ) ) { | 
|---|
| 241 | auto dwt = member.val.strict_as<ast::DeclWithType>(); | 
|---|
| 242 | ast::Type const * memberType = dwt->get_type(); | 
|---|
| 243 |  | 
|---|
| 244 | if ( 0 < member.idx ) { | 
|---|
| 245 | // Make sure all later members have padding to align them. | 
|---|
| 246 | kids.emplace_back( makeAlignTo( location, | 
|---|
| 247 | derefVar( location, sizeofParam ), | 
|---|
| 248 | new ast::AlignofExpr( location, ast::deepCopy( memberType ) ) | 
|---|
| 249 | ) ); | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | // Place current size in the current offset index. | 
|---|
| 253 | kids.emplace_back( makeOpStmt( location, "?=?", | 
|---|
| 254 | makeOp( location, "?[?]", | 
|---|
| 255 | new ast::VariableExpr( location, offsetofParam ), | 
|---|
| 256 | ast::ConstantExpr::from_ulong( location, member.idx ) ), | 
|---|
| 257 | derefVar( location, sizeofParam ) ) ); | 
|---|
| 258 |  | 
|---|
| 259 | // Add member size to current size. | 
|---|
| 260 | kids.emplace_back( makeOpStmt( location, "?+=?", | 
|---|
| 261 | derefVar( location, sizeofParam ), | 
|---|
| 262 | new ast::SizeofExpr( location, ast::deepCopy( memberType ) ) ) ); | 
|---|
| 263 |  | 
|---|
| 264 | // Take max of member alignment and global alignment. | 
|---|
| 265 | // (As align is always 2^n, this will always be a multiple of both.) | 
|---|
| 266 | kids.emplace_back( makeAssignMax( location, | 
|---|
| 267 | derefVar( location, alignofParam ), | 
|---|
| 268 | new ast::AlignofExpr( location, ast::deepCopy( memberType ) ) ) ); | 
|---|
| 269 | } | 
|---|
| 270 | // Make sure the type is end-padded to a multiple of its alignment. | 
|---|
| 271 | kids.emplace_back( makeAlignTo( location, | 
|---|
| 272 | derefVar( location, sizeofParam ), | 
|---|
| 273 | derefVar( location, alignofParam ) ) ); | 
|---|
| 274 |  | 
|---|
| 275 | declsToAddAfter.emplace_back( layoutDecl ); | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | void LayoutFunctionBuilder::previsit( ast::UnionDecl const * decl ) { | 
|---|
| 279 | visit_children = false; | 
|---|
| 280 | // Do not generate layout function for empty tag unions. | 
|---|
| 281 | if ( decl->members.empty() ) return; | 
|---|
| 282 |  | 
|---|
| 283 | // Get parameters that can change layout, exiting early if none. | 
|---|
| 284 | ast::vector<ast::TypeDecl> sizedParams = | 
|---|
| 285 | takeSizedParams( decl->params ); | 
|---|
| 286 | if ( sizedParams.empty() ) return; | 
|---|
| 287 |  | 
|---|
| 288 | CodeLocation const & location = decl->location; | 
|---|
| 289 |  | 
|---|
| 290 | // Build layout function signature. | 
|---|
| 291 | LayoutData layout = buildLayoutFunction( | 
|---|
| 292 | location, decl, sizedParams, isInFunction(), false ); | 
|---|
| 293 | ast::FunctionDecl * layoutDecl = layout.function; | 
|---|
| 294 | // Also return these or extract them from the parameter list? | 
|---|
| 295 | ast::ObjectDecl const * sizeofParam = layout.sizeofParam; | 
|---|
| 296 | ast::ObjectDecl const * alignofParam = layout.alignofParam; | 
|---|
| 297 | assert( nullptr == layout.offsetofParam ); | 
|---|
| 298 |  | 
|---|
| 299 | // Calculate union layout in function body. | 
|---|
| 300 | // Both are simply the maximum for union (actually align is always the | 
|---|
| 301 | // LCM, but with powers of two that is also the maximum). | 
|---|
| 302 | auto & kids = layoutDecl->stmts.get_and_mutate()->kids; | 
|---|
| 303 | kids.emplace_back( makeOpStmt( location, | 
|---|
| 304 | "?=?", derefVar( location, sizeofParam ), | 
|---|
| 305 | ast::ConstantExpr::from_ulong( location, 1 ) | 
|---|
| 306 | ) ); | 
|---|
| 307 | kids.emplace_back( makeOpStmt( location, | 
|---|
| 308 | "?=?", derefVar( location, alignofParam ), | 
|---|
| 309 | ast::ConstantExpr::from_ulong( location, 1 ) | 
|---|
| 310 | ) ); | 
|---|
| 311 | // TODO: Polymorphic types will be out of the union declaration scope. | 
|---|
| 312 | // This breaks invariants until it is corrected later. | 
|---|
| 313 | for ( auto const & member : decl->members ) { | 
|---|
| 314 | auto dwt = member.strict_as<ast::DeclWithType>(); | 
|---|
| 315 | ast::Type const * memberType = dwt->get_type(); | 
|---|
| 316 |  | 
|---|
| 317 | // Take max member size and global size. | 
|---|
| 318 | kids.emplace_back( makeAssignMax( location, | 
|---|
| 319 | derefVar( location, sizeofParam ), | 
|---|
| 320 | new ast::SizeofExpr( location, ast::deepCopy( memberType ) ) | 
|---|
| 321 | ) ); | 
|---|
| 322 |  | 
|---|
| 323 | // Take max of member alignment and global alignment. | 
|---|
| 324 | kids.emplace_back( makeAssignMax( location, | 
|---|
| 325 | derefVar( location, alignofParam ), | 
|---|
| 326 | new ast::AlignofExpr( location, ast::deepCopy( memberType ) ) | 
|---|
| 327 | ) ); | 
|---|
| 328 | } | 
|---|
| 329 | kids.emplace_back( makeAlignTo( location, | 
|---|
| 330 | derefVar( location, sizeofParam ), | 
|---|
| 331 | derefVar( location, alignofParam ) ) ); | 
|---|
| 332 |  | 
|---|
| 333 | declsToAddAfter.emplace_back( layoutDecl ); | 
|---|
| 334 | } | 
|---|
| 335 |  | 
|---|
| 336 | // -------------------------------------------------------------------------- | 
|---|
| 337 | /// Application expression transformer. | 
|---|
| 338 | /// * Replaces polymorphic return types with out-parameters. | 
|---|
| 339 | /// * Replaces call to polymorphic functions with adapter calls which handles | 
|---|
| 340 | ///   dynamic arguments and return values. | 
|---|
| 341 | /// * Adds appropriate type variables to the function calls. | 
|---|
| 342 | struct CallAdapter final : | 
|---|
| 343 | public ast::WithConstTypeSubstitution, | 
|---|
| 344 | public ast::WithGuards, | 
|---|
| 345 | public ast::WithShortCircuiting, | 
|---|
| 346 | public ast::WithStmtsToAdd<>, | 
|---|
| 347 | public ast::WithVisitorRef<CallAdapter> { | 
|---|
| 348 | CallAdapter(); | 
|---|
| 349 |  | 
|---|
| 350 | void previsit( ast::Decl const * decl ); | 
|---|
| 351 | ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl ); | 
|---|
| 352 | void previsit( ast::TypeDecl const * decl ); | 
|---|
| 353 | void previsit( ast::CommaExpr const * expr ); | 
|---|
| 354 | ast::Expr const * postvisit( ast::ApplicationExpr const * expr ); | 
|---|
| 355 | ast::Expr const * postvisit( ast::UntypedExpr const * expr ); | 
|---|
| 356 | void previsit( ast::AddressExpr const * expr ); | 
|---|
| 357 | ast::Expr const * postvisit( ast::AddressExpr const * expr ); | 
|---|
| 358 | ast::ReturnStmt const * previsit( ast::ReturnStmt const * stmt ); | 
|---|
| 359 |  | 
|---|
| 360 | void beginScope(); | 
|---|
| 361 | void endScope(); | 
|---|
| 362 | private: | 
|---|
| 363 | // Many helpers here use a mutable ApplicationExpr as an in/out parameter | 
|---|
| 364 | // instead of using the return value, to save on mutates and free up the | 
|---|
| 365 | // return value. | 
|---|
| 366 |  | 
|---|
| 367 | /// Passes extra layout arguments for sized polymorphic type parameters. | 
|---|
| 368 | void passTypeVars( | 
|---|
| 369 | ast::ApplicationExpr * expr, | 
|---|
| 370 | ast::vector<ast::Expr> & extraArgs, | 
|---|
| 371 | ast::FunctionType const * funcType ); | 
|---|
| 372 | /// Wraps a function application with a new temporary for the | 
|---|
| 373 | /// out-parameter return value. | 
|---|
| 374 | ast::Expr const * addRetParam( | 
|---|
| 375 | ast::ApplicationExpr * expr, ast::Type const * retType ); | 
|---|
| 376 | /// Wraps a function application returning a polymorphic type with a new | 
|---|
| 377 | /// temporary for the out-parameter return value. | 
|---|
| 378 | ast::Expr const * addDynRetParam( | 
|---|
| 379 | ast::ApplicationExpr * expr, ast::Type const * polyType ); | 
|---|
| 380 | /// Modify a call so it passes the function through the correct adapter. | 
|---|
| 381 | ast::Expr const * applyAdapter( | 
|---|
| 382 | ast::ApplicationExpr * expr, | 
|---|
| 383 | ast::FunctionType const * function ); | 
|---|
| 384 | /// Convert a single argument into its boxed form to pass the parameter. | 
|---|
| 385 | void boxParam( ast::ptr<ast::Expr> & arg, | 
|---|
| 386 | ast::Type const * formal, TypeVarMap const & exprTyVars ); | 
|---|
| 387 | /// Box every argument from arg forward, matching the functionType | 
|---|
| 388 | /// parameter list. arg should point into expr's argument list. | 
|---|
| 389 | void boxParams( | 
|---|
| 390 | ast::ApplicationExpr * expr, | 
|---|
| 391 | ast::Type const * polyRetType, | 
|---|
| 392 | ast::FunctionType const * function, | 
|---|
| 393 | const TypeVarMap & typeVars ); | 
|---|
| 394 | /// Adds the inferred parameters derived from the assertions of the | 
|---|
| 395 | /// expression to the call. | 
|---|
| 396 | void addInferredParams( | 
|---|
| 397 | ast::ApplicationExpr * expr, | 
|---|
| 398 | ast::vector<ast::Expr> & extraArgs, | 
|---|
| 399 | ast::FunctionType const * functionType, | 
|---|
| 400 | const TypeVarMap & typeVars ); | 
|---|
| 401 | /// Stores assignment operators from assertion list in | 
|---|
| 402 | /// local map of assignment operations. | 
|---|
| 403 | void passAdapters( | 
|---|
| 404 | ast::ApplicationExpr * expr, | 
|---|
| 405 | ast::FunctionType const * type, | 
|---|
| 406 | const TypeVarMap & typeVars ); | 
|---|
| 407 | /// Create an adapter function based on the type of the adaptee and the | 
|---|
| 408 | /// real type with the type substitutions applied. | 
|---|
| 409 | ast::FunctionDecl * makeAdapter( | 
|---|
| 410 | ast::FunctionType const * adaptee, | 
|---|
| 411 | ast::FunctionType const * realType, | 
|---|
| 412 | std::string const & mangleName, | 
|---|
| 413 | TypeVarMap const & typeVars, | 
|---|
| 414 | CodeLocation const & location ) const; | 
|---|
| 415 | /// Replaces intrinsic operator functions with their arithmetic desugaring. | 
|---|
| 416 | ast::Expr const * handleIntrinsics( ast::ApplicationExpr const * ); | 
|---|
| 417 | /// Inserts a new temporary variable into the current scope with an | 
|---|
| 418 | /// auto-generated name. | 
|---|
| 419 | ast::ObjectDecl * makeTemporary( | 
|---|
| 420 | CodeLocation const & location, ast::Type const * type ); | 
|---|
| 421 |  | 
|---|
| 422 | TypeVarMap scopeTypeVars; | 
|---|
| 423 | ScopedMap< std::string, ast::DeclWithType const * > adapters; | 
|---|
| 424 | std::map< ast::ApplicationExpr const *, ast::Expr const * > retVals; | 
|---|
| 425 | ast::DeclWithType const * retval; | 
|---|
| 426 | UniqueName tmpNamer; | 
|---|
| 427 | }; | 
|---|
| 428 |  | 
|---|
| 429 | /// Replaces a polymorphic type with its concrete equivalant under the | 
|---|
| 430 | /// current environment (returns itself if concrete). | 
|---|
| 431 | /// If `doClone` is set to false, will not clone interior types | 
|---|
| 432 | ast::Type const * replaceWithConcrete( | 
|---|
| 433 | ast::Type const * type, | 
|---|
| 434 | ast::TypeSubstitution const & typeSubs, | 
|---|
| 435 | bool doCopy = true ); | 
|---|
| 436 |  | 
|---|
| 437 | /// Replaces all the type parameters of a generic type with their | 
|---|
| 438 | /// concrete equivalents under the current environment. | 
|---|
| 439 | void replaceParametersWithConcrete( | 
|---|
| 440 | ast::vector<ast::Expr> & params, | 
|---|
| 441 | ast::TypeSubstitution const & typeSubs ) { | 
|---|
| 442 | for ( ast::ptr<ast::Expr> & paramExpr : params ) { | 
|---|
| 443 | ast::TypeExpr const * param = paramExpr.as<ast::TypeExpr>(); | 
|---|
| 444 | assertf( param, "Aggregate parameters should be type expressions." ); | 
|---|
| 445 | paramExpr = ast::mutate_field( param, &ast::TypeExpr::type, | 
|---|
| 446 | replaceWithConcrete( param->type.get(), typeSubs, false ) ); | 
|---|
| 447 | } | 
|---|
| 448 | } | 
|---|
| 449 |  | 
|---|
| 450 | ast::Type const * replaceWithConcrete( | 
|---|
| 451 | ast::Type const * type, | 
|---|
| 452 | ast::TypeSubstitution const & typeSubs, | 
|---|
| 453 | bool doCopy ) { | 
|---|
| 454 | if ( auto instType = dynamic_cast<ast::TypeInstType const *>( type ) ) { | 
|---|
| 455 | ast::Type const * concrete = typeSubs.lookup( instType ); | 
|---|
| 456 | return ( nullptr != concrete ) ? concrete : instType; | 
|---|
| 457 | } else if ( auto structType = | 
|---|
| 458 | dynamic_cast<ast::StructInstType const *>( type ) ) { | 
|---|
| 459 | ast::StructInstType * newType = | 
|---|
| 460 | doCopy ? ast::deepCopy( structType ) : ast::mutate( structType ); | 
|---|
| 461 | replaceParametersWithConcrete( newType->params, typeSubs ); | 
|---|
| 462 | return newType; | 
|---|
| 463 | } else if ( auto unionType = | 
|---|
| 464 | dynamic_cast<ast::UnionInstType const *>( type ) ) { | 
|---|
| 465 | ast::UnionInstType * newType = | 
|---|
| 466 | doCopy ? ast::deepCopy( unionType ) : ast::mutate( unionType ); | 
|---|
| 467 | replaceParametersWithConcrete( newType->params, typeSubs ); | 
|---|
| 468 | return newType; | 
|---|
| 469 | } else { | 
|---|
| 470 | return type; | 
|---|
| 471 | } | 
|---|
| 472 | } | 
|---|
| 473 |  | 
|---|
| 474 | std::string makePolyMonoSuffix( | 
|---|
| 475 | ast::FunctionType const * function, | 
|---|
| 476 | TypeVarMap const & typeVars ) { | 
|---|
| 477 | // If the return type or a parameter type involved polymorphic types, | 
|---|
| 478 | // then the adapter will need to take those polymorphic types as pointers. | 
|---|
| 479 | // Therefore, there can be two different functions with the same mangled | 
|---|
| 480 | // name, so we need to further mangle the names. | 
|---|
| 481 | std::stringstream name; | 
|---|
| 482 | for ( auto ret : function->returns ) { | 
|---|
| 483 | name << ( isPolyType( ret, typeVars ) ? 'P' : 'M' ); | 
|---|
| 484 | } | 
|---|
| 485 | name << '_'; | 
|---|
| 486 | for ( auto arg : function->params ) { | 
|---|
| 487 | name << ( isPolyType( arg, typeVars ) ? 'P' : 'M' ); | 
|---|
| 488 | } | 
|---|
| 489 | return name.str(); | 
|---|
| 490 | } | 
|---|
| 491 |  | 
|---|
| 492 | std::string mangleAdapterName( | 
|---|
| 493 | ast::FunctionType const * function, | 
|---|
| 494 | TypeVarMap const & typeVars ) { | 
|---|
| 495 | return Mangle::mangle( function, {} ) | 
|---|
| 496 | + makePolyMonoSuffix( function, typeVars ); | 
|---|
| 497 | } | 
|---|
| 498 |  | 
|---|
| 499 | std::string makeAdapterName( std::string const & mangleName ) { | 
|---|
| 500 | return "_adapter" + mangleName; | 
|---|
| 501 | } | 
|---|
| 502 |  | 
|---|
| 503 | void makeRetParam( ast::FunctionType * type ) { | 
|---|
| 504 | ast::ptr<ast::Type> & retParam = type->returns.front(); | 
|---|
| 505 |  | 
|---|
| 506 | // Make a new parameter that is a pointer to the type of the old return value. | 
|---|
| 507 | retParam = new ast::PointerType( retParam.get() ); | 
|---|
| 508 | type->params.emplace( type->params.begin(), retParam ); | 
|---|
| 509 |  | 
|---|
| 510 | // We don't need the return value any more. | 
|---|
| 511 | type->returns.clear(); | 
|---|
| 512 | } | 
|---|
| 513 |  | 
|---|
| 514 | ast::FunctionType * makeAdapterType( | 
|---|
| 515 | ast::FunctionType const * adaptee, | 
|---|
| 516 | TypeVarMap const & typeVars ) { | 
|---|
| 517 | ast::FunctionType * adapter = ast::deepCopy( adaptee ); | 
|---|
| 518 | if ( isDynRet( adapter, typeVars ) ) { | 
|---|
| 519 | makeRetParam( adapter ); | 
|---|
| 520 | } | 
|---|
| 521 | adapter->params.emplace( adapter->params.begin(), | 
|---|
| 522 | new ast::PointerType( new ast::FunctionType( ast::VariableArgs ) ) | 
|---|
| 523 | ); | 
|---|
| 524 | return adapter; | 
|---|
| 525 | } | 
|---|
| 526 |  | 
|---|
| 527 | CallAdapter::CallAdapter() : tmpNamer( "_temp" ) {} | 
|---|
| 528 |  | 
|---|
| 529 | void CallAdapter::previsit( ast::Decl const * ) { | 
|---|
| 530 | // Prevent type declaration information from leaking out. | 
|---|
| 531 | GuardScope( scopeTypeVars ); | 
|---|
| 532 | } | 
|---|
| 533 |  | 
|---|
| 534 | ast::FunctionDecl const * CallAdapter::previsit( ast::FunctionDecl const * decl ) { | 
|---|
| 535 | // Prevent type declaration information from leaking out. | 
|---|
| 536 | GuardScope( scopeTypeVars ); | 
|---|
| 537 |  | 
|---|
| 538 | if ( nullptr == decl->stmts ) { | 
|---|
| 539 | return decl; | 
|---|
| 540 | } | 
|---|
| 541 |  | 
|---|
| 542 | GuardValue( retval ); | 
|---|
| 543 |  | 
|---|
| 544 | // Process polymorphic return value. | 
|---|
| 545 | retval = nullptr; | 
|---|
| 546 | ast::FunctionType const * type = decl->type; | 
|---|
| 547 | if ( isDynRet( type ) && decl->linkage != ast::Linkage::C ) { | 
|---|
| 548 | retval = decl->returns.front(); | 
|---|
| 549 |  | 
|---|
| 550 | // Give names to unnamed return values. | 
|---|
| 551 | if ( "" == retval->name ) { | 
|---|
| 552 | auto mutRet = ast::mutate( retval ); | 
|---|
| 553 | mutRet->name = "_retparam"; | 
|---|
| 554 | mutRet->linkage = ast::Linkage::C; | 
|---|
| 555 | retval = mutRet; | 
|---|
| 556 | decl = ast::mutate_field_index( decl, | 
|---|
| 557 | &ast::FunctionDecl::returns, 0, mutRet ); | 
|---|
| 558 | } | 
|---|
| 559 | } | 
|---|
| 560 |  | 
|---|
| 561 | // The formal_usage/expr_id values may be off if we get them from the | 
|---|
| 562 | // type, trying the declaration instead. | 
|---|
| 563 | makeTypeVarMap( type, scopeTypeVars ); | 
|---|
| 564 |  | 
|---|
| 565 | // Get all needed adapters from the call. We will forward them. | 
|---|
| 566 | ast::vector<ast::FunctionType> functions; | 
|---|
| 567 | for ( ast::ptr<ast::VariableExpr> const & assertion : type->assertions ) { | 
|---|
| 568 | auto atype = assertion->result.get(); | 
|---|
| 569 | findFunction( atype, functions, scopeTypeVars, needsAdapter ); | 
|---|
| 570 | } | 
|---|
| 571 |  | 
|---|
| 572 | for ( ast::ptr<ast::Type> const & arg : type->params ) { | 
|---|
| 573 | findFunction( arg, functions, scopeTypeVars, needsAdapter ); | 
|---|
| 574 | } | 
|---|
| 575 |  | 
|---|
| 576 | for ( auto funcType : functions ) { | 
|---|
| 577 | std::string mangleName = mangleAdapterName( funcType, scopeTypeVars ); | 
|---|
| 578 | if ( adapters.contains( mangleName ) ) continue; | 
|---|
| 579 | std::string adapterName = makeAdapterName( mangleName ); | 
|---|
| 580 | // NODE: This creates floating nodes, breaking invariants. | 
|---|
| 581 | // This is corrected in the RewireAdapters sub-pass. | 
|---|
| 582 | adapters.insert( | 
|---|
| 583 | mangleName, | 
|---|
| 584 | new ast::ObjectDecl( | 
|---|
| 585 | decl->location, | 
|---|
| 586 | adapterName, | 
|---|
| 587 | new ast::PointerType( | 
|---|
| 588 | makeAdapterType( funcType, scopeTypeVars ) ), | 
|---|
| 589 | nullptr, // init | 
|---|
| 590 | ast::Storage::Classes(), | 
|---|
| 591 | ast::Linkage::C | 
|---|
| 592 | ) | 
|---|
| 593 | ); | 
|---|
| 594 | } | 
|---|
| 595 |  | 
|---|
| 596 | return decl; | 
|---|
| 597 | } | 
|---|
| 598 |  | 
|---|
| 599 | void CallAdapter::previsit( ast::TypeDecl const * decl ) { | 
|---|
| 600 | addToTypeVarMap( decl, scopeTypeVars ); | 
|---|
| 601 | } | 
|---|
| 602 |  | 
|---|
| 603 | void CallAdapter::previsit( ast::CommaExpr const * expr ) { | 
|---|
| 604 | // Attempting to find application expressions that were mutated by the | 
|---|
| 605 | // copy constructor passes to use an explicit return variable, so that | 
|---|
| 606 | // the variable can be reused as a parameter to the call rather than | 
|---|
| 607 | // creating a new temporary variable. Previously this step was an | 
|---|
| 608 | // optimization, but with the introduction of tuples and UniqueExprs, | 
|---|
| 609 | // it is necessary to ensure that they use the same variable. | 
|---|
| 610 | // Essentially, looking for pattern: | 
|---|
| 611 | // (x=f(...), x) | 
|---|
| 612 | // To compound the issue, the right side can be *x, etc. | 
|---|
| 613 | // because of lvalue-returning functions | 
|---|
| 614 | if ( auto assign = expr->arg1.as<ast::UntypedExpr>() ) { | 
|---|
| 615 | if ( CodeGen::isAssignment( ast::getFunctionName( assign ) ) ) { | 
|---|
| 616 | assert( 2 == assign->args.size() ); | 
|---|
| 617 | if ( auto app = assign->args.back().as<ast::ApplicationExpr>() ) { | 
|---|
| 618 | // First argument is assignable, so it must be an lvalue, | 
|---|
| 619 | // so it should be legal to takes its address. | 
|---|
| 620 | retVals.insert_or_assign( app, assign->args.front() ); | 
|---|
| 621 | } | 
|---|
| 622 | } | 
|---|
| 623 | } | 
|---|
| 624 | } | 
|---|
| 625 |  | 
|---|
| 626 | ast::Expr const * CallAdapter::postvisit( ast::ApplicationExpr const * expr ) { | 
|---|
| 627 | assert( expr->func->result ); | 
|---|
| 628 | ast::FunctionType const * function = getFunctionType( expr->func->result ); | 
|---|
| 629 | assertf( function, "ApplicationExpr has non-function type %s", | 
|---|
| 630 | toCString( expr->func->result ) ); | 
|---|
| 631 |  | 
|---|
| 632 | if ( auto newExpr = handleIntrinsics( expr ) ) { | 
|---|
| 633 | return newExpr; | 
|---|
| 634 | } | 
|---|
| 635 |  | 
|---|
| 636 | ast::ApplicationExpr * mutExpr = ast::mutate( expr ); | 
|---|
| 637 | ast::Expr const * ret = expr; | 
|---|
| 638 |  | 
|---|
| 639 | TypeVarMap exprTypeVars; | 
|---|
| 640 | makeTypeVarMap( function, exprTypeVars ); | 
|---|
| 641 | auto dynRetType = isDynRet( function, exprTypeVars ); | 
|---|
| 642 |  | 
|---|
| 643 | // NOTE: addDynRetParam needs to know the actual (generated) return type | 
|---|
| 644 | // so it can make a temporary variable, so pass the result type form the | 
|---|
| 645 | // `expr` `passTypeVars` needs to know the program-text return type ([ex] | 
|---|
| 646 | // the distinction between _conc_T30 and T3(int)) concRetType may not be | 
|---|
| 647 | // a good name in one or both of these places. | 
|---|
| 648 | if ( dynRetType ) { | 
|---|
| 649 | ast::Type const * result = mutExpr->result; | 
|---|
| 650 | ast::Type const * concRetType = result->isVoid() ? nullptr : result; | 
|---|
| 651 | // [Comment from before translation.] | 
|---|
| 652 | // Used to use dynRetType instead of concRetType. | 
|---|
| 653 | ret = addDynRetParam( mutExpr, concRetType ); | 
|---|
| 654 | } else if ( needsAdapter( function, scopeTypeVars ) | 
|---|
| 655 | && !needsAdapter( function, exprTypeVars ) ) { | 
|---|
| 656 | // Change the application so it calls the adapter rather than the | 
|---|
| 657 | // passed function. | 
|---|
| 658 | ret = applyAdapter( mutExpr, function ); | 
|---|
| 659 | } | 
|---|
| 660 |  | 
|---|
| 661 | ast::vector<ast::Expr> prependArgs; | 
|---|
| 662 | passTypeVars( mutExpr, prependArgs, function ); | 
|---|
| 663 | addInferredParams( mutExpr, prependArgs, function, exprTypeVars ); | 
|---|
| 664 |  | 
|---|
| 665 | boxParams( mutExpr, dynRetType, function, exprTypeVars ); | 
|---|
| 666 | spliceBegin( mutExpr->args, prependArgs ); | 
|---|
| 667 | passAdapters( mutExpr, function, exprTypeVars ); | 
|---|
| 668 |  | 
|---|
| 669 | return ret; | 
|---|
| 670 | } | 
|---|
| 671 |  | 
|---|
| 672 | bool isPolyDeref( ast::UntypedExpr const * expr, | 
|---|
| 673 | TypeVarMap const & typeVars, | 
|---|
| 674 | ast::TypeSubstitution const * typeSubs ) { | 
|---|
| 675 | if ( expr->result && isPolyType( expr->result, typeVars, typeSubs ) ) { | 
|---|
| 676 | if ( auto name = expr->func.as<ast::NameExpr>() ) { | 
|---|
| 677 | if ( "*?" == name->name ) { | 
|---|
| 678 | return true; | 
|---|
| 679 | } | 
|---|
| 680 | } | 
|---|
| 681 | } | 
|---|
| 682 | return false; | 
|---|
| 683 | } | 
|---|
| 684 |  | 
|---|
| 685 | ast::Expr const * CallAdapter::postvisit( ast::UntypedExpr const * expr ) { | 
|---|
| 686 | if ( isPolyDeref( expr, scopeTypeVars, typeSubs ) ) { | 
|---|
| 687 | return expr->args.front(); | 
|---|
| 688 | } | 
|---|
| 689 | return expr; | 
|---|
| 690 | } | 
|---|
| 691 |  | 
|---|
| 692 | void CallAdapter::previsit( ast::AddressExpr const * ) { | 
|---|
| 693 | visit_children = false; | 
|---|
| 694 | } | 
|---|
| 695 |  | 
|---|
| 696 | ast::Expr const * CallAdapter::postvisit( ast::AddressExpr const * expr ) { | 
|---|
| 697 | assert( expr->arg->result ); | 
|---|
| 698 | assert( !expr->arg->result->isVoid() ); | 
|---|
| 699 |  | 
|---|
| 700 | bool doesNeedAdapter = false; | 
|---|
| 701 | if ( auto un = expr->arg.as<ast::UntypedExpr>() ) { | 
|---|
| 702 | if ( isPolyDeref( un, scopeTypeVars, typeSubs ) ) { | 
|---|
| 703 | if ( auto app = un->args.front().as<ast::ApplicationExpr>() ) { | 
|---|
| 704 | assert( app->func->result ); | 
|---|
| 705 | auto function = getFunctionType( app->func->result ); | 
|---|
| 706 | assert( function ); | 
|---|
| 707 | doesNeedAdapter = needsAdapter( function, scopeTypeVars ); | 
|---|
| 708 | } | 
|---|
| 709 | } | 
|---|
| 710 | } | 
|---|
| 711 | // isPolyType check needs to happen before mutating expr arg, | 
|---|
| 712 | // so pull it forward out of the if condition. | 
|---|
| 713 | expr = ast::mutate_field( expr, &ast::AddressExpr::arg, | 
|---|
| 714 | expr->arg->accept( *visitor ) ); | 
|---|
| 715 | // But must happen after mutate, since argument might change | 
|---|
| 716 | // (ex. intrinsic *?, ?[?]) re-evaluate above comment. | 
|---|
| 717 | bool polyType = isPolyType( expr->arg->result, scopeTypeVars, typeSubs ); | 
|---|
| 718 | if ( polyType || doesNeedAdapter ) { | 
|---|
| 719 | ast::Expr * ret = ast::mutate( expr->arg.get() ); | 
|---|
| 720 | ret->result = ast::deepCopy( expr->result ); | 
|---|
| 721 | return ret; | 
|---|
| 722 | } else { | 
|---|
| 723 | return expr; | 
|---|
| 724 | } | 
|---|
| 725 | } | 
|---|
| 726 |  | 
|---|
| 727 | ast::ReturnStmt const * CallAdapter::previsit( ast::ReturnStmt const * stmt ) { | 
|---|
| 728 | // Since retval is set when the return type is dynamic, this function | 
|---|
| 729 | // should have been converted to void return & out parameter. | 
|---|
| 730 | if ( retval && stmt->expr ) { | 
|---|
| 731 | assert( stmt->expr->result ); | 
|---|
| 732 | assert( !stmt->expr->result->isVoid() ); | 
|---|
| 733 | return ast::mutate_field( stmt, &ast::ReturnStmt::expr, nullptr ); | 
|---|
| 734 | } | 
|---|
| 735 | return stmt; | 
|---|
| 736 | } | 
|---|
| 737 |  | 
|---|
| 738 | void CallAdapter::beginScope() { | 
|---|
| 739 | adapters.beginScope(); | 
|---|
| 740 | } | 
|---|
| 741 |  | 
|---|
| 742 | void CallAdapter::endScope() { | 
|---|
| 743 | adapters.endScope(); | 
|---|
| 744 | } | 
|---|
| 745 |  | 
|---|
| 746 | /// Find instances of polymorphic type parameters. | 
|---|
| 747 | struct PolyFinder { | 
|---|
| 748 | TypeVarMap const & typeVars; | 
|---|
| 749 | bool result = false; | 
|---|
| 750 | PolyFinder( TypeVarMap const & tvs ) : typeVars( tvs ) {} | 
|---|
| 751 |  | 
|---|
| 752 | void previsit( ast::TypeInstType const * type ) { | 
|---|
| 753 | if ( isPolyType( type, typeVars ) ) result = true; | 
|---|
| 754 | } | 
|---|
| 755 | }; | 
|---|
| 756 |  | 
|---|
| 757 | /// True if these is an instance of a polymorphic type parameter in the type. | 
|---|
| 758 | bool hasPolymorphism( ast::Type const * type, TypeVarMap const & typeVars ) { | 
|---|
| 759 | return ast::Pass<PolyFinder>::read( type, typeVars ); | 
|---|
| 760 | } | 
|---|
| 761 |  | 
|---|
| 762 | void CallAdapter::passTypeVars( | 
|---|
| 763 | ast::ApplicationExpr * expr, | 
|---|
| 764 | ast::vector<ast::Expr> & extraArgs, | 
|---|
| 765 | ast::FunctionType const * function ) { | 
|---|
| 766 | assert( typeSubs ); | 
|---|
| 767 | // Pass size/align for type variables. | 
|---|
| 768 | for ( ast::ptr<ast::TypeInstType> const & typeVar : function->forall ) { | 
|---|
| 769 | if ( !typeVar->base->isComplete() ) continue; | 
|---|
| 770 | ast::Type const * concrete = typeSubs->lookup( typeVar ); | 
|---|
| 771 | if ( !concrete ) { | 
|---|
| 772 | // Should this be an assertion? | 
|---|
| 773 | SemanticError( expr->location, "\nunbound type variable %s in application %s", | 
|---|
| 774 | toString( typeSubs ).c_str(), typeVar->typeString().c_str() ); | 
|---|
| 775 | } | 
|---|
| 776 | extraArgs.emplace_back( | 
|---|
| 777 | new ast::SizeofExpr( expr->location, ast::deepCopy( concrete ) ) ); | 
|---|
| 778 | extraArgs.emplace_back( | 
|---|
| 779 | new ast::AlignofExpr( expr->location, ast::deepCopy( concrete ) ) ); | 
|---|
| 780 | } | 
|---|
| 781 | } | 
|---|
| 782 |  | 
|---|
| 783 | ast::Expr const * CallAdapter::addRetParam( | 
|---|
| 784 | ast::ApplicationExpr * expr, ast::Type const * retType ) { | 
|---|
| 785 | // Create temporary to hold return value of polymorphic function and | 
|---|
| 786 | // produce that temporary as a result using a comma expression. | 
|---|
| 787 | assert( retType ); | 
|---|
| 788 |  | 
|---|
| 789 | ast::Expr * paramExpr = nullptr; | 
|---|
| 790 | // Try to use existing return value parameter if it exists, | 
|---|
| 791 | // otherwise create a new temporary. | 
|---|
| 792 | if ( retVals.count( expr ) ) { | 
|---|
| 793 | paramExpr = ast::deepCopy( retVals[ expr ] ); | 
|---|
| 794 | } else { | 
|---|
| 795 | auto newObj = makeTemporary( expr->location, ast::deepCopy( retType ) ); | 
|---|
| 796 | paramExpr = new ast::VariableExpr( expr->location, newObj ); | 
|---|
| 797 | } | 
|---|
| 798 | ast::Expr * retExpr = ast::deepCopy( paramExpr ); | 
|---|
| 799 |  | 
|---|
| 800 | // If the type of the temporary is not polpmorphic, box temporary by | 
|---|
| 801 | // taking its address; otherwise the temporary is already boxed and can | 
|---|
| 802 | // be used directly. | 
|---|
| 803 | if ( !isPolyType( paramExpr->result, scopeTypeVars, typeSubs ) ) { | 
|---|
| 804 | paramExpr = new ast::AddressExpr( paramExpr->location, paramExpr ); | 
|---|
| 805 | } | 
|---|
| 806 | // Add argument to function call. | 
|---|
| 807 | expr->args.insert( expr->args.begin(), paramExpr ); | 
|---|
| 808 | // Build a comma expression to call the function and return a value. | 
|---|
| 809 | ast::CommaExpr * comma = new ast::CommaExpr( | 
|---|
| 810 | expr->location, expr, retExpr ); | 
|---|
| 811 | comma->env = expr->env; | 
|---|
| 812 | expr->env = nullptr; | 
|---|
| 813 | return comma; | 
|---|
| 814 | } | 
|---|
| 815 |  | 
|---|
| 816 | ast::Expr const * CallAdapter::addDynRetParam( | 
|---|
| 817 | ast::ApplicationExpr * expr, ast::Type const * polyType ) { | 
|---|
| 818 | assert( typeSubs ); | 
|---|
| 819 | ast::Type const * concrete = replaceWithConcrete( polyType, *typeSubs ); | 
|---|
| 820 | // Add out-parameter for return value. | 
|---|
| 821 | return addRetParam( expr, concrete ); | 
|---|
| 822 | } | 
|---|
| 823 |  | 
|---|
| 824 | ast::Expr const * CallAdapter::applyAdapter( | 
|---|
| 825 | ast::ApplicationExpr * expr, | 
|---|
| 826 | ast::FunctionType const * function ) { | 
|---|
| 827 | ast::Expr const * ret = expr; | 
|---|
| 828 | if ( isDynRet( function, scopeTypeVars ) ) { | 
|---|
| 829 | ret = addRetParam( expr, function->returns.front() ); | 
|---|
| 830 | } | 
|---|
| 831 | std::string mangleName = mangleAdapterName( function, scopeTypeVars ); | 
|---|
| 832 | std::string adapterName = makeAdapterName( mangleName ); | 
|---|
| 833 |  | 
|---|
| 834 | // Cast adaptee to `void (*)()`, since it may have any type inside a | 
|---|
| 835 | // polymorphic function. | 
|---|
| 836 | ast::Type const * adapteeType = new ast::PointerType( | 
|---|
| 837 | new ast::FunctionType( ast::VariableArgs ) ); | 
|---|
| 838 | expr->args.insert( expr->args.begin(), | 
|---|
| 839 | new ast::CastExpr( expr->location, expr->func, adapteeType ) ); | 
|---|
| 840 | // The result field is never set on NameExpr. / Now it is. | 
|---|
| 841 | auto head = new ast::NameExpr( expr->location, adapterName ); | 
|---|
| 842 | head->result = ast::deepCopy( adapteeType ); | 
|---|
| 843 | expr->func = head; | 
|---|
| 844 |  | 
|---|
| 845 | return ret; | 
|---|
| 846 | } | 
|---|
| 847 |  | 
|---|
| 848 | /// Cast parameters to polymorphic functions so that types are replaced with | 
|---|
| 849 | /// `void *` if they are type parameters in the formal type. | 
|---|
| 850 | /// This gets rid of warnings from gcc. | 
|---|
| 851 | void addCast( | 
|---|
| 852 | ast::ptr<ast::Expr> & actual, | 
|---|
| 853 | ast::Type const * formal, | 
|---|
| 854 | TypeVarMap const & typeVars ) { | 
|---|
| 855 | // Type contains polymorphism, but isn't exactly a polytype, in which | 
|---|
| 856 | // case it has some real actual type (ex. unsigned int) and casting to | 
|---|
| 857 | // `void *` is wrong. | 
|---|
| 858 | if ( hasPolymorphism( formal, typeVars ) | 
|---|
| 859 | && !isPolyType( formal, typeVars ) ) { | 
|---|
| 860 | ast::Type const * newType = ast::deepCopy( formal ); | 
|---|
| 861 | newType = scrubTypeVars( newType, typeVars ); | 
|---|
| 862 | actual = new ast::CastExpr( actual->location, actual, newType ); | 
|---|
| 863 | } | 
|---|
| 864 | } | 
|---|
| 865 |  | 
|---|
| 866 | void CallAdapter::boxParam( ast::ptr<ast::Expr> & arg, | 
|---|
| 867 | ast::Type const * param, TypeVarMap const & exprTypeVars ) { | 
|---|
| 868 | assertf( arg->result, "arg does not have result: %s", toCString( arg ) ); | 
|---|
| 869 | addCast( arg, param, exprTypeVars ); | 
|---|
| 870 | if ( !needsBoxing( param, arg->result, exprTypeVars, typeSubs ) ) { | 
|---|
| 871 | return; | 
|---|
| 872 | } | 
|---|
| 873 | CodeLocation const & location = arg->location; | 
|---|
| 874 |  | 
|---|
| 875 | if ( arg->get_lvalue() ) { | 
|---|
| 876 | // The argument expression may be CFA lvalue, but not C lvalue, | 
|---|
| 877 | // so apply generalizedLvalue transformations. | 
|---|
| 878 | // if ( auto var = dynamic_cast<ast::VariableExpr const *>( arg ) ) { | 
|---|
| 879 | //  if ( dynamic_cast<ast::ArrayType const *>( varExpr->var->get_type() ) ){ | 
|---|
| 880 | //      // temporary hack - don't box arrays, because &arr is not the same as &arr[0] | 
|---|
| 881 | //      return; | 
|---|
| 882 | //  } | 
|---|
| 883 | // } | 
|---|
| 884 | arg = generalizedLvalue( new ast::AddressExpr( arg->location, arg ) ); | 
|---|
| 885 | if ( !ResolvExpr::typesCompatible( param, arg->result ) ) { | 
|---|
| 886 | // Silence warnings by casting boxed parameters when the actually | 
|---|
| 887 | // type does not match up with the formal type. | 
|---|
| 888 | arg = new ast::CastExpr( location, arg, ast::deepCopy( param ) ); | 
|---|
| 889 | } | 
|---|
| 890 | } else { | 
|---|
| 891 | // Use type computed in unification to declare boxed variables. | 
|---|
| 892 | ast::ptr<ast::Type> newType = ast::deepCopy( param ); | 
|---|
| 893 | if ( typeSubs ) typeSubs->apply( newType ); | 
|---|
| 894 | ast::ObjectDecl * newObj = makeTemporary( location, newType ); | 
|---|
| 895 | auto assign = ast::UntypedExpr::createCall( location, "?=?", { | 
|---|
| 896 | new ast::VariableExpr( location, newObj ), | 
|---|
| 897 | arg, | 
|---|
| 898 | } ); | 
|---|
| 899 | stmtsToAddBefore.push_back( new ast::ExprStmt( location, assign ) ); | 
|---|
| 900 | arg = new ast::AddressExpr( | 
|---|
| 901 | new ast::VariableExpr( location, newObj ) ); | 
|---|
| 902 | } | 
|---|
| 903 | } | 
|---|
| 904 |  | 
|---|
| 905 | void CallAdapter::boxParams( | 
|---|
| 906 | ast::ApplicationExpr * expr, | 
|---|
| 907 | ast::Type const * polyRetType, | 
|---|
| 908 | ast::FunctionType const * function, | 
|---|
| 909 | const TypeVarMap & typeVars ) { | 
|---|
| 910 | // Start at the beginning, but the return argument may have been added. | 
|---|
| 911 | auto arg = expr->args.begin(); | 
|---|
| 912 | if ( polyRetType ) ++arg; | 
|---|
| 913 |  | 
|---|
| 914 | for ( auto param : function->params ) { | 
|---|
| 915 | assertf( arg != expr->args.end(), | 
|---|
| 916 | "boxParams: missing argument for param %s to %s in %s", | 
|---|
| 917 | toCString( param ), toCString( function ), toCString( expr ) ); | 
|---|
| 918 | boxParam( *arg, param, typeVars ); | 
|---|
| 919 | ++arg; | 
|---|
| 920 | } | 
|---|
| 921 | } | 
|---|
| 922 |  | 
|---|
| 923 | void CallAdapter::addInferredParams( | 
|---|
| 924 | ast::ApplicationExpr * expr, | 
|---|
| 925 | ast::vector<ast::Expr> & extraArgs, | 
|---|
| 926 | ast::FunctionType const * functionType, | 
|---|
| 927 | TypeVarMap const & typeVars ) { | 
|---|
| 928 | for ( auto assertion : functionType->assertions ) { | 
|---|
| 929 | auto inferParam = expr->inferred.inferParams().find( | 
|---|
| 930 | assertion->var->uniqueId ); | 
|---|
| 931 | assertf( inferParam != expr->inferred.inferParams().end(), | 
|---|
| 932 | "addInferredParams missing inferred parameter: %s in: %s", | 
|---|
| 933 | toCString( assertion ), toCString( expr ) ); | 
|---|
| 934 | ast::ptr<ast::Expr> newExpr = ast::deepCopy( inferParam->second.expr ); | 
|---|
| 935 | boxParam( newExpr, assertion->result, typeVars ); | 
|---|
| 936 | extraArgs.emplace_back( newExpr.release() ); | 
|---|
| 937 | } | 
|---|
| 938 | } | 
|---|
| 939 |  | 
|---|
| 940 | /// Modifies the ApplicationExpr to accept adapter functions for its | 
|---|
| 941 | /// assertion and parameters, declares the required adapters. | 
|---|
| 942 | void CallAdapter::passAdapters( | 
|---|
| 943 | ast::ApplicationExpr * expr, | 
|---|
| 944 | ast::FunctionType const * type, | 
|---|
| 945 | const TypeVarMap & exprTypeVars ) { | 
|---|
| 946 | // Collect a list of function types passed as parameters or implicit | 
|---|
| 947 | // parameters (assertions). | 
|---|
| 948 | ast::vector<ast::Type> const & paramList = type->params; | 
|---|
| 949 | ast::vector<ast::FunctionType> functions; | 
|---|
| 950 |  | 
|---|
| 951 | for ( ast::ptr<ast::VariableExpr> const & assertion : type->assertions ) { | 
|---|
| 952 | findFunction( assertion->result, functions, exprTypeVars, needsAdapter ); | 
|---|
| 953 | } | 
|---|
| 954 | for ( ast::ptr<ast::Type> const & arg : paramList ) { | 
|---|
| 955 | findFunction( arg, functions, exprTypeVars, needsAdapter ); | 
|---|
| 956 | } | 
|---|
| 957 |  | 
|---|
| 958 | // Parameter function types for which an appropriate adapter has been | 
|---|
| 959 | // generated. We cannot use the types after applying substitutions, | 
|---|
| 960 | // since two different parameter types may be unified to the same type. | 
|---|
| 961 | std::set<std::string> adaptersDone; | 
|---|
| 962 |  | 
|---|
| 963 | CodeLocation const & location = expr->location; | 
|---|
| 964 |  | 
|---|
| 965 | for ( ast::ptr<ast::FunctionType> const & funcType : functions ) { | 
|---|
| 966 | std::string mangleName = Mangle::mangle( funcType ); | 
|---|
| 967 |  | 
|---|
| 968 | // Only attempt to create an adapter or pass one as a parameter if we | 
|---|
| 969 | // haven't already done so for this pre-substitution parameter | 
|---|
| 970 | // function type. | 
|---|
| 971 | // The second part of the result if is if the element was inserted. | 
|---|
| 972 | if ( !adaptersDone.insert( mangleName ).second ) continue; | 
|---|
| 973 |  | 
|---|
| 974 | // Apply substitution to type variables to figure out what the | 
|---|
| 975 | // adapter's type should look like. (Copy to make the release safe.) | 
|---|
| 976 | assert( typeSubs ); | 
|---|
| 977 | auto result = typeSubs->apply( ast::deepCopy( funcType ) ); | 
|---|
| 978 | ast::FunctionType * realType = ast::mutate( result.node.release() ); | 
|---|
| 979 | mangleName = Mangle::mangle( realType ); | 
|---|
| 980 | mangleName += makePolyMonoSuffix( funcType, exprTypeVars ); | 
|---|
| 981 |  | 
|---|
| 982 | // Check if the adapter has already been created, or has to be. | 
|---|
| 983 | using AdapterIter = decltype(adapters)::iterator; | 
|---|
| 984 | AdapterIter adapter = adapters.find( mangleName ); | 
|---|
| 985 | if ( adapter == adapters.end() ) { | 
|---|
| 986 | ast::FunctionDecl * newAdapter = makeAdapter( | 
|---|
| 987 | funcType, realType, mangleName, exprTypeVars, location ); | 
|---|
| 988 | std::pair<AdapterIter, bool> answer = | 
|---|
| 989 | adapters.insert( mangleName, newAdapter ); | 
|---|
| 990 | adapter = answer.first; | 
|---|
| 991 | stmtsToAddBefore.push_back( | 
|---|
| 992 | new ast::DeclStmt( location, newAdapter ) ); | 
|---|
| 993 | } | 
|---|
| 994 | assert( adapter != adapters.end() ); | 
|---|
| 995 |  | 
|---|
| 996 | // Add the approprate adapter as a parameter. | 
|---|
| 997 | expr->args.insert( expr->args.begin(), | 
|---|
| 998 | new ast::VariableExpr( location, adapter->second ) ); | 
|---|
| 999 | } | 
|---|
| 1000 | } | 
|---|
| 1001 |  | 
|---|
| 1002 | // Parameter and argument may be used wrong around here. | 
|---|
| 1003 | ast::Expr * makeAdapterArg( | 
|---|
| 1004 | ast::DeclWithType const * param, | 
|---|
| 1005 | ast::Type const * arg, | 
|---|
| 1006 | ast::Type const * realParam, | 
|---|
| 1007 | TypeVarMap const & typeVars, | 
|---|
| 1008 | CodeLocation const & location ) { | 
|---|
| 1009 | assert( param ); | 
|---|
| 1010 | assert( arg ); | 
|---|
| 1011 | assert( realParam ); | 
|---|
| 1012 | if ( isPolyType( realParam, typeVars ) && !isPolyType( arg ) ) { | 
|---|
| 1013 | ast::UntypedExpr * deref = ast::UntypedExpr::createDeref( | 
|---|
| 1014 | location, | 
|---|
| 1015 | new ast::CastExpr( location, | 
|---|
| 1016 | new ast::VariableExpr( location, param ), | 
|---|
| 1017 | new ast::PointerType( ast::deepCopy( arg ) ) | 
|---|
| 1018 | ) | 
|---|
| 1019 | ); | 
|---|
| 1020 | deref->result = ast::deepCopy( arg ); | 
|---|
| 1021 | return deref; | 
|---|
| 1022 | } | 
|---|
| 1023 | return new ast::VariableExpr( location, param ); | 
|---|
| 1024 | } | 
|---|
| 1025 |  | 
|---|
| 1026 | // This seems to be one of the problematic functions. | 
|---|
| 1027 | void addAdapterParams( | 
|---|
| 1028 | ast::ApplicationExpr * adaptee, | 
|---|
| 1029 | ast::vector<ast::Type>::const_iterator arg, | 
|---|
| 1030 | ast::vector<ast::DeclWithType>::iterator param, | 
|---|
| 1031 | ast::vector<ast::DeclWithType>::iterator paramEnd, | 
|---|
| 1032 | ast::vector<ast::Type>::const_iterator realParam, | 
|---|
| 1033 | TypeVarMap const & typeVars, | 
|---|
| 1034 | CodeLocation const & location ) { | 
|---|
| 1035 | UniqueName paramNamer( "_p" ); | 
|---|
| 1036 | for ( ; param != paramEnd ; ++param, ++arg, ++realParam ) { | 
|---|
| 1037 | if ( "" == (*param)->name ) { | 
|---|
| 1038 | auto mutParam = (*param).get_and_mutate(); | 
|---|
| 1039 | mutParam->name = paramNamer.newName(); | 
|---|
| 1040 | mutParam->linkage = ast::Linkage::C; | 
|---|
| 1041 | } | 
|---|
| 1042 | adaptee->args.push_back( | 
|---|
| 1043 | makeAdapterArg( *param, *arg, *realParam, typeVars, location ) ); | 
|---|
| 1044 | } | 
|---|
| 1045 | } | 
|---|
| 1046 |  | 
|---|
| 1047 | ast::FunctionDecl * CallAdapter::makeAdapter( | 
|---|
| 1048 | ast::FunctionType const * adaptee, | 
|---|
| 1049 | ast::FunctionType const * realType, | 
|---|
| 1050 | std::string const & mangleName, | 
|---|
| 1051 | TypeVarMap const & typeVars, | 
|---|
| 1052 | CodeLocation const & location ) const { | 
|---|
| 1053 | ast::FunctionType * adapterType = makeAdapterType( adaptee, typeVars ); | 
|---|
| 1054 | adapterType = ast::mutate( scrubTypeVars( adapterType, typeVars ) ); | 
|---|
| 1055 |  | 
|---|
| 1056 | // Some of these names will be overwritten, but it gives a default. | 
|---|
| 1057 | UniqueName pNamer( "_param" ); | 
|---|
| 1058 | UniqueName rNamer( "_ret" ); | 
|---|
| 1059 |  | 
|---|
| 1060 | bool first = true; | 
|---|
| 1061 |  | 
|---|
| 1062 | ast::FunctionDecl * adapterDecl = new ast::FunctionDecl( location, | 
|---|
| 1063 | makeAdapterName( mangleName ), | 
|---|
| 1064 | {}, // forall | 
|---|
| 1065 | {}, // assertions | 
|---|
| 1066 | map_range<ast::vector<ast::DeclWithType>>( adapterType->params, | 
|---|
| 1067 | [&pNamer, &location, &first]( ast::ptr<ast::Type> const & param ) { | 
|---|
| 1068 | // [Trying to make the generated code match exactly more often.] | 
|---|
| 1069 | if ( first ) { | 
|---|
| 1070 | first = false; | 
|---|
| 1071 | return new ast::ObjectDecl( location, "_adaptee", param ); | 
|---|
| 1072 | } | 
|---|
| 1073 | return new ast::ObjectDecl( location, pNamer.newName(), param ); | 
|---|
| 1074 | } ), | 
|---|
| 1075 | map_range<ast::vector<ast::DeclWithType>>( adapterType->returns, | 
|---|
| 1076 | [&rNamer, &location]( ast::ptr<ast::Type> const & retval ) { | 
|---|
| 1077 | return new ast::ObjectDecl( location, rNamer.newName(), retval ); | 
|---|
| 1078 | } ), | 
|---|
| 1079 | nullptr, // stmts | 
|---|
| 1080 | {}, // storage | 
|---|
| 1081 | ast::Linkage::C | 
|---|
| 1082 | ); | 
|---|
| 1083 |  | 
|---|
| 1084 | ast::DeclWithType * adapteeDecl = | 
|---|
| 1085 | adapterDecl->params.front().get_and_mutate(); | 
|---|
| 1086 | adapteeDecl->name = "_adaptee"; | 
|---|
| 1087 |  | 
|---|
| 1088 | // Do not carry over attributes to real type parameters/return values. | 
|---|
| 1089 | auto mutRealType = ast::mutate( realType ); | 
|---|
| 1090 | for ( ast::ptr<ast::Type> & decl : mutRealType->params ) { | 
|---|
| 1091 | if ( decl->attributes.empty() ) continue; | 
|---|
| 1092 | auto mut = ast::mutate( decl.get() ); | 
|---|
| 1093 | mut->attributes.clear(); | 
|---|
| 1094 | decl = mut; | 
|---|
| 1095 | } | 
|---|
| 1096 | for ( ast::ptr<ast::Type> & decl : mutRealType->returns ) { | 
|---|
| 1097 | if ( decl->attributes.empty() ) continue; | 
|---|
| 1098 | auto mut = ast::mutate( decl.get() ); | 
|---|
| 1099 | mut->attributes.clear(); | 
|---|
| 1100 | decl = mut; | 
|---|
| 1101 | } | 
|---|
| 1102 | realType = mutRealType; | 
|---|
| 1103 |  | 
|---|
| 1104 | ast::ApplicationExpr * adapteeApp = new ast::ApplicationExpr( location, | 
|---|
| 1105 | new ast::CastExpr( location, | 
|---|
| 1106 | new ast::VariableExpr( location, adapteeDecl ), | 
|---|
| 1107 | new ast::PointerType( realType ) | 
|---|
| 1108 | ) | 
|---|
| 1109 | ); | 
|---|
| 1110 |  | 
|---|
| 1111 | for ( auto const & [assertArg, assertParam, assertReal] : group_iterate( | 
|---|
| 1112 | realType->assertions, adapterType->assertions, adaptee->assertions ) ) { | 
|---|
| 1113 | adapteeApp->args.push_back( makeAdapterArg( | 
|---|
| 1114 | assertParam->var, assertArg->var->get_type(), | 
|---|
| 1115 | assertReal->var->get_type(), typeVars, location | 
|---|
| 1116 | ) ); | 
|---|
| 1117 | } | 
|---|
| 1118 |  | 
|---|
| 1119 | ast::vector<ast::Type>::const_iterator | 
|---|
| 1120 | arg = realType->params.begin(), | 
|---|
| 1121 | param = adapterType->params.begin(), | 
|---|
| 1122 | realParam = adaptee->params.begin(); | 
|---|
| 1123 | ast::vector<ast::DeclWithType>::iterator | 
|---|
| 1124 | paramDecl = adapterDecl->params.begin(); | 
|---|
| 1125 | // Skip adaptee parameter in the adapter type. | 
|---|
| 1126 | ++param; | 
|---|
| 1127 | ++paramDecl; | 
|---|
| 1128 |  | 
|---|
| 1129 | ast::Stmt * bodyStmt; | 
|---|
| 1130 | // Returns void/nothing. | 
|---|
| 1131 | if ( realType->returns.empty() ) { | 
|---|
| 1132 | addAdapterParams( adapteeApp, arg, paramDecl, adapterDecl->params.end(), | 
|---|
| 1133 | realParam, typeVars, location ); | 
|---|
| 1134 | bodyStmt = new ast::ExprStmt( location, adapteeApp ); | 
|---|
| 1135 | // Returns a polymorphic type. | 
|---|
| 1136 | } else if ( isDynType( adaptee->returns.front(), typeVars ) ) { | 
|---|
| 1137 | ast::UntypedExpr * assign = new ast::UntypedExpr( location, | 
|---|
| 1138 | new ast::NameExpr( location, "?=?" ) ); | 
|---|
| 1139 | ast::UntypedExpr * deref = ast::UntypedExpr::createDeref( location, | 
|---|
| 1140 | new ast::CastExpr( location, | 
|---|
| 1141 | new ast::VariableExpr( location, *paramDecl++ ), | 
|---|
| 1142 | new ast::PointerType( | 
|---|
| 1143 | ast::deepCopy( realType->returns.front() ) ) ) ); | 
|---|
| 1144 | assign->args.push_back( deref ); | 
|---|
| 1145 | addAdapterParams( adapteeApp, arg, paramDecl, adapterDecl->params.end(), | 
|---|
| 1146 | realParam, typeVars, location ); | 
|---|
| 1147 | assign->args.push_back( adapteeApp ); | 
|---|
| 1148 | bodyStmt = new ast::ExprStmt( location, assign ); | 
|---|
| 1149 | // Adapter for a function that returns a monomorphic value. | 
|---|
| 1150 | } else { | 
|---|
| 1151 | addAdapterParams( adapteeApp, arg, paramDecl, adapterDecl->params.end(), | 
|---|
| 1152 | realParam, typeVars, location ); | 
|---|
| 1153 | bodyStmt = new ast::ReturnStmt( location, adapteeApp ); | 
|---|
| 1154 | } | 
|---|
| 1155 |  | 
|---|
| 1156 | adapterDecl->stmts = new ast::CompoundStmt( location, { bodyStmt } ); | 
|---|
| 1157 | return adapterDecl; | 
|---|
| 1158 | } | 
|---|
| 1159 |  | 
|---|
| 1160 | ast::Expr const * makeIncrDecrExpr( | 
|---|
| 1161 | CodeLocation const & location, | 
|---|
| 1162 | ast::ApplicationExpr const * expr, | 
|---|
| 1163 | ast::Type const * polyType, | 
|---|
| 1164 | bool isIncr ) { | 
|---|
| 1165 | ast::NameExpr * opExpr = | 
|---|
| 1166 | new ast::NameExpr( location, isIncr ? "?+=?" : "?-=?" ); | 
|---|
| 1167 | ast::UntypedExpr * addAssign = new ast::UntypedExpr( location, opExpr ); | 
|---|
| 1168 | if ( auto address = expr->args.front().as<ast::AddressExpr>() ) { | 
|---|
| 1169 | addAssign->args.push_back( address->arg ); | 
|---|
| 1170 | } else { | 
|---|
| 1171 | addAssign->args.push_back( expr->args.front() ); | 
|---|
| 1172 | } | 
|---|
| 1173 | addAssign->args.push_back( new ast::NameExpr( location, | 
|---|
| 1174 | sizeofName( Mangle::mangleType( polyType ) ) ) ); | 
|---|
| 1175 | addAssign->result = ast::deepCopy( expr->result ); | 
|---|
| 1176 | addAssign->env = expr->env ? expr->env : addAssign->env; | 
|---|
| 1177 | return addAssign; | 
|---|
| 1178 | } | 
|---|
| 1179 |  | 
|---|
| 1180 | /// Handles intrinsic functions for postvisit ApplicationExpr. | 
|---|
| 1181 | ast::Expr const * CallAdapter::handleIntrinsics( | 
|---|
| 1182 | ast::ApplicationExpr const * expr ) { | 
|---|
| 1183 | auto varExpr = expr->func.as<ast::VariableExpr>(); | 
|---|
| 1184 | if ( !varExpr || varExpr->var->linkage != ast::Linkage::Intrinsic ) { | 
|---|
| 1185 | return nullptr; | 
|---|
| 1186 | } | 
|---|
| 1187 | std::string const & varName = varExpr->var->name; | 
|---|
| 1188 |  | 
|---|
| 1189 | // Index Intrinsic: | 
|---|
| 1190 | if ( "?[?]" == varName ) { | 
|---|
| 1191 | assert( expr->result ); | 
|---|
| 1192 | assert( 2 == expr->args.size() ); | 
|---|
| 1193 |  | 
|---|
| 1194 | ast::Type const * baseType1 = | 
|---|
| 1195 | isPolyPtr( expr->args.front()->result, scopeTypeVars, typeSubs ); | 
|---|
| 1196 | ast::Type const * baseType2 = | 
|---|
| 1197 | isPolyPtr( expr->args.back()->result, scopeTypeVars, typeSubs ); | 
|---|
| 1198 | // If neither argument is a polymorphic pointer, do nothing. | 
|---|
| 1199 | if ( !baseType1 && !baseType2 ) { | 
|---|
| 1200 | return expr; | 
|---|
| 1201 | } | 
|---|
| 1202 | // The arguments cannot both be polymorphic pointers. | 
|---|
| 1203 | assert( !baseType1 || !baseType2 ); | 
|---|
| 1204 | // (So exactly one of the arguments is a polymorphic pointer.) | 
|---|
| 1205 |  | 
|---|
| 1206 | CodeLocation const & location = expr->location; | 
|---|
| 1207 | CodeLocation const & location1 = expr->args.front()->location; | 
|---|
| 1208 | CodeLocation const & location2 = expr->args.back()->location; | 
|---|
| 1209 |  | 
|---|
| 1210 | ast::UntypedExpr * ret = new ast::UntypedExpr( location, | 
|---|
| 1211 | new ast::NameExpr( location, "?+?" ) ); | 
|---|
| 1212 | if ( baseType1 ) { | 
|---|
| 1213 | auto multiply = ast::UntypedExpr::createCall( location2, "?*?", { | 
|---|
| 1214 | expr->args.back(), | 
|---|
| 1215 | new ast::SizeofExpr( location1, deepCopy( baseType1 ) ), | 
|---|
| 1216 | } ); | 
|---|
| 1217 | ret->args.push_back( expr->args.front() ); | 
|---|
| 1218 | ret->args.push_back( multiply ); | 
|---|
| 1219 | } else { | 
|---|
| 1220 | assert( baseType2 ); | 
|---|
| 1221 | auto multiply = ast::UntypedExpr::createCall( location1, "?*?", { | 
|---|
| 1222 | expr->args.front(), | 
|---|
| 1223 | new ast::SizeofExpr( location2, deepCopy( baseType2 ) ), | 
|---|
| 1224 | } ); | 
|---|
| 1225 | ret->args.push_back( multiply ); | 
|---|
| 1226 | ret->args.push_back( expr->args.back() ); | 
|---|
| 1227 | } | 
|---|
| 1228 | ret->result = ast::deepCopy( expr->result ); | 
|---|
| 1229 | ret->env = expr->env ? expr->env : ret->env; | 
|---|
| 1230 | return ret; | 
|---|
| 1231 | // Dereference Intrinsic: | 
|---|
| 1232 | } else if ( "*?" == varName ) { | 
|---|
| 1233 | assert( expr->result ); | 
|---|
| 1234 | assert( 1 == expr->args.size() ); | 
|---|
| 1235 |  | 
|---|
| 1236 | // If this isn't for a poly type, then do nothing. | 
|---|
| 1237 | if ( !isPolyType( expr->result, scopeTypeVars, typeSubs ) ) { | 
|---|
| 1238 | return expr; | 
|---|
| 1239 | } | 
|---|
| 1240 |  | 
|---|
| 1241 | // Remove dereference from polymorphic types since they are boxed. | 
|---|
| 1242 | ast::Expr * ret = ast::deepCopy( expr->args.front() ); | 
|---|
| 1243 | // Fix expression type to remove pointer. | 
|---|
| 1244 | ret->result = expr->result; | 
|---|
| 1245 | ret->env = expr->env ? expr->env : ret->env; | 
|---|
| 1246 | return ret; | 
|---|
| 1247 | // Post-Increment/Decrement Intrinsics: | 
|---|
| 1248 | } else if ( "?++" == varName || "?--" == varName ) { | 
|---|
| 1249 | assert( expr->result ); | 
|---|
| 1250 | assert( 1 == expr->args.size() ); | 
|---|
| 1251 |  | 
|---|
| 1252 | ast::Type const * baseType = | 
|---|
| 1253 | isPolyType( expr->result, scopeTypeVars, typeSubs ); | 
|---|
| 1254 | if ( nullptr == baseType ) { | 
|---|
| 1255 | return expr; | 
|---|
| 1256 | } | 
|---|
| 1257 | ast::Type * tempType = ast::deepCopy( expr->result ); | 
|---|
| 1258 | if ( typeSubs ) { | 
|---|
| 1259 | auto result = typeSubs->apply( tempType ); | 
|---|
| 1260 | tempType = ast::mutate( result.node.release() ); | 
|---|
| 1261 | } | 
|---|
| 1262 | CodeLocation const & location = expr->location; | 
|---|
| 1263 | ast::ObjectDecl * newObj = makeTemporary( location, tempType ); | 
|---|
| 1264 | ast::VariableExpr * tempExpr = | 
|---|
| 1265 | new ast::VariableExpr( location, newObj ); | 
|---|
| 1266 | ast::UntypedExpr * assignExpr = new ast::UntypedExpr( location, | 
|---|
| 1267 | new ast::NameExpr( location, "?=?" ) ); | 
|---|
| 1268 | assignExpr->args.push_back( ast::deepCopy( tempExpr ) ); | 
|---|
| 1269 | if ( auto address = expr->args.front().as<ast::AddressExpr>() ) { | 
|---|
| 1270 | assignExpr->args.push_back( ast::deepCopy( address->arg ) ); | 
|---|
| 1271 | } else { | 
|---|
| 1272 | assignExpr->args.push_back( ast::deepCopy( expr->args.front() ) ); | 
|---|
| 1273 | } | 
|---|
| 1274 | return new ast::CommaExpr( location, | 
|---|
| 1275 | new ast::CommaExpr( location, | 
|---|
| 1276 | assignExpr, | 
|---|
| 1277 | makeIncrDecrExpr( location, expr, baseType, "?++" == varName ) | 
|---|
| 1278 | ), | 
|---|
| 1279 | tempExpr | 
|---|
| 1280 | ); | 
|---|
| 1281 | // Pre-Increment/Decrement Intrinsics: | 
|---|
| 1282 | } else if ( "++?" == varName || "--?" == varName ) { | 
|---|
| 1283 | assert( expr->result ); | 
|---|
| 1284 | assert( 1 == expr->args.size() ); | 
|---|
| 1285 |  | 
|---|
| 1286 | ast::Type const * baseType = | 
|---|
| 1287 | isPolyType( expr->result, scopeTypeVars, typeSubs ); | 
|---|
| 1288 | if ( nullptr == baseType ) { | 
|---|
| 1289 | return expr; | 
|---|
| 1290 | } | 
|---|
| 1291 | return makeIncrDecrExpr( | 
|---|
| 1292 | expr->location, expr, baseType, "++?" == varName ); | 
|---|
| 1293 | // Addition and Subtration Intrinsics: | 
|---|
| 1294 | } else if ( "?+?" == varName || "?-?" == varName ) { | 
|---|
| 1295 | assert( expr->result ); | 
|---|
| 1296 | assert( 2 == expr->args.size() ); | 
|---|
| 1297 |  | 
|---|
| 1298 | auto baseType1 = | 
|---|
| 1299 | isPolyPtr( expr->args.front()->result, scopeTypeVars, typeSubs ); | 
|---|
| 1300 | auto baseType2 = | 
|---|
| 1301 | isPolyPtr( expr->args.back()->result, scopeTypeVars, typeSubs ); | 
|---|
| 1302 |  | 
|---|
| 1303 | CodeLocation const & location = expr->location; | 
|---|
| 1304 | CodeLocation const & location1 = expr->args.front()->location; | 
|---|
| 1305 | CodeLocation const & location2 = expr->args.back()->location; | 
|---|
| 1306 | // LHS op RHS -> (LHS op RHS) / sizeof(LHS) | 
|---|
| 1307 | if ( baseType1 && baseType2 ) { | 
|---|
| 1308 | auto divide = ast::UntypedExpr::createCall( location, "?/?", { | 
|---|
| 1309 | expr, | 
|---|
| 1310 | new ast::SizeofExpr( location, deepCopy( baseType1 ) ), | 
|---|
| 1311 | } ); | 
|---|
| 1312 | if ( expr->env ) divide->env = expr->env; | 
|---|
| 1313 | return divide; | 
|---|
| 1314 | // LHS op RHS -> LHS op (RHS * sizeof(LHS)) | 
|---|
| 1315 | } else if ( baseType1 ) { | 
|---|
| 1316 | auto multiply = ast::UntypedExpr::createCall( location2, "?*?", { | 
|---|
| 1317 | expr->args.back(), | 
|---|
| 1318 | new ast::SizeofExpr( location1, deepCopy( baseType1 ) ), | 
|---|
| 1319 | } ); | 
|---|
| 1320 | return ast::mutate_field_index( | 
|---|
| 1321 | expr, &ast::ApplicationExpr::args, 1, multiply ); | 
|---|
| 1322 | // LHS op RHS -> (LHS * sizeof(RHS)) op RHS | 
|---|
| 1323 | } else if ( baseType2 ) { | 
|---|
| 1324 | auto multiply = ast::UntypedExpr::createCall( location1, "?*?", { | 
|---|
| 1325 | expr->args.front(), | 
|---|
| 1326 | new ast::SizeofExpr( location2, deepCopy( baseType2 ) ), | 
|---|
| 1327 | } ); | 
|---|
| 1328 | return ast::mutate_field_index( | 
|---|
| 1329 | expr, &ast::ApplicationExpr::args, 0, multiply ); | 
|---|
| 1330 | } | 
|---|
| 1331 | // Addition and Subtration Relative Assignment Intrinsics: | 
|---|
| 1332 | } else if ( "?+=?" == varName || "?-=?" == varName ) { | 
|---|
| 1333 | assert( expr->result ); | 
|---|
| 1334 | assert( 2 == expr->args.size() ); | 
|---|
| 1335 |  | 
|---|
| 1336 | CodeLocation const & location1 = expr->args.front()->location; | 
|---|
| 1337 | CodeLocation const & location2 = expr->args.back()->location; | 
|---|
| 1338 | auto baseType = isPolyPtr( expr->result, scopeTypeVars, typeSubs ); | 
|---|
| 1339 | // LHS op RHS -> LHS op (RHS * sizeof(LHS)) | 
|---|
| 1340 | if ( baseType ) { | 
|---|
| 1341 | auto multiply = ast::UntypedExpr::createCall( location2, "?*?", { | 
|---|
| 1342 | expr->args.back(), | 
|---|
| 1343 | new ast::SizeofExpr( location1, deepCopy( baseType ) ), | 
|---|
| 1344 | } ); | 
|---|
| 1345 | return ast::mutate_field_index( | 
|---|
| 1346 | expr, &ast::ApplicationExpr::args, 1, multiply ); | 
|---|
| 1347 | } | 
|---|
| 1348 | } | 
|---|
| 1349 | return expr; | 
|---|
| 1350 | } | 
|---|
| 1351 |  | 
|---|
| 1352 | ast::ObjectDecl * CallAdapter::makeTemporary( | 
|---|
| 1353 | CodeLocation const & location, ast::Type const * type ) { | 
|---|
| 1354 | auto newObj = new ast::ObjectDecl( location, tmpNamer.newName(), type ); | 
|---|
| 1355 | stmtsToAddBefore.push_back( new ast::DeclStmt( location, newObj ) ); | 
|---|
| 1356 | return newObj; | 
|---|
| 1357 | } | 
|---|
| 1358 |  | 
|---|
| 1359 | // -------------------------------------------------------------------------- | 
|---|
| 1360 | /// Modifies declarations to accept implicit parameters. | 
|---|
| 1361 | /// * Move polymorphic returns in function types to pointer-type parameters. | 
|---|
| 1362 | /// * Adds type size and assertion parameters to parameter lists. | 
|---|
| 1363 | struct DeclAdapter final { | 
|---|
| 1364 | ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl ); | 
|---|
| 1365 | ast::FunctionDecl const * postvisit( ast::FunctionDecl const * decl ); | 
|---|
| 1366 | private: | 
|---|
| 1367 | void addAdapters( ast::FunctionDecl * decl, TypeVarMap & localTypeVars ); | 
|---|
| 1368 | }; | 
|---|
| 1369 |  | 
|---|
| 1370 | ast::ObjectDecl * makeObj( | 
|---|
| 1371 | CodeLocation const & location, std::string const & name ) { | 
|---|
| 1372 | // The size/align parameters may be unused, so add the unused attribute. | 
|---|
| 1373 | return new ast::ObjectDecl( location, name, | 
|---|
| 1374 | makeLayoutCType(), | 
|---|
| 1375 | nullptr, ast::Storage::Classes(), ast::Linkage::C, nullptr, | 
|---|
| 1376 | { new ast::Attribute( "unused" ) } ); | 
|---|
| 1377 | } | 
|---|
| 1378 |  | 
|---|
| 1379 | /// A modified and specialized version of ast::add_qualifiers. | 
|---|
| 1380 | ast::Type const * addConst( ast::Type const * type ) { | 
|---|
| 1381 | ast::CV::Qualifiers cvq = { ast::CV::Const }; | 
|---|
| 1382 | if ( ( type->qualifiers & cvq ) != 0 ) return type; | 
|---|
| 1383 | auto mutType = ast::mutate( type ); | 
|---|
| 1384 | mutType->qualifiers |= cvq; | 
|---|
| 1385 | return mutType; | 
|---|
| 1386 | } | 
|---|
| 1387 |  | 
|---|
| 1388 | ast::FunctionDecl const * DeclAdapter::previsit( ast::FunctionDecl const * decl ) { | 
|---|
| 1389 | TypeVarMap localTypeVars; | 
|---|
| 1390 | makeTypeVarMap( decl, localTypeVars ); | 
|---|
| 1391 |  | 
|---|
| 1392 | auto mutDecl = mutate( decl ); | 
|---|
| 1393 |  | 
|---|
| 1394 | // Move polymorphic return type to parameter list. | 
|---|
| 1395 | if ( isDynRet( mutDecl->type ) ) { | 
|---|
| 1396 | auto ret = strict_dynamic_cast<ast::ObjectDecl *>( | 
|---|
| 1397 | mutDecl->returns.front().get_and_mutate() ); | 
|---|
| 1398 | ret->set_type( new ast::PointerType( ret->type ) ); | 
|---|
| 1399 | mutDecl->params.insert( mutDecl->params.begin(), ret ); | 
|---|
| 1400 | mutDecl->returns.erase( mutDecl->returns.begin() ); | 
|---|
| 1401 | ret->init = nullptr; | 
|---|
| 1402 | } | 
|---|
| 1403 |  | 
|---|
| 1404 | // Add size/align and assertions for type parameters to parameter list. | 
|---|
| 1405 | ast::vector<ast::DeclWithType> inferredParams; | 
|---|
| 1406 | ast::vector<ast::DeclWithType> layoutParams; | 
|---|
| 1407 | for ( ast::ptr<ast::TypeDecl> & typeParam : mutDecl->type_params ) { | 
|---|
| 1408 | auto mutParam = mutate( typeParam.get() ); | 
|---|
| 1409 | // Add all size and alignment parameters to parameter list. | 
|---|
| 1410 | if ( mutParam->isComplete() ) { | 
|---|
| 1411 | ast::TypeInstType paramType( mutParam ); | 
|---|
| 1412 | std::string paramName = Mangle::mangleType( ¶mType ); | 
|---|
| 1413 |  | 
|---|
| 1414 | auto sizeParam = makeObj( typeParam->location, sizeofName( paramName ) ); | 
|---|
| 1415 | layoutParams.emplace_back( sizeParam ); | 
|---|
| 1416 |  | 
|---|
| 1417 | auto alignParam = makeObj( typeParam->location, alignofName( paramName ) ); | 
|---|
| 1418 | layoutParams.emplace_back( alignParam ); | 
|---|
| 1419 | } | 
|---|
| 1420 | // Assertions should be stored in the main list. | 
|---|
| 1421 | assert( mutParam->assertions.empty() ); | 
|---|
| 1422 | typeParam = mutParam; | 
|---|
| 1423 | } | 
|---|
| 1424 | for ( ast::ptr<ast::DeclWithType> & assert : mutDecl->assertions ) { | 
|---|
| 1425 | ast::DeclWithType * mutAssert = ast::mutate( assert.get() ); | 
|---|
| 1426 | // Assertion parameters may not be used in body, | 
|---|
| 1427 | // pass along with unused attribute. | 
|---|
| 1428 | mutAssert->attributes.push_back( new ast::Attribute( "unused" ) ); | 
|---|
| 1429 | mutAssert->set_type( addConst( mutAssert->get_type() ) ); | 
|---|
| 1430 | inferredParams.emplace_back( mutAssert ); | 
|---|
| 1431 | } | 
|---|
| 1432 | mutDecl->assertions.clear(); | 
|---|
| 1433 |  | 
|---|
| 1434 | // Prepend each argument group. From last group to first. addAdapters | 
|---|
| 1435 | // does do the same, it just does it itself and see all other parameters. | 
|---|
| 1436 | spliceBegin( mutDecl->params, inferredParams ); | 
|---|
| 1437 | spliceBegin( mutDecl->params, layoutParams ); | 
|---|
| 1438 | addAdapters( mutDecl, localTypeVars ); | 
|---|
| 1439 |  | 
|---|
| 1440 | // Now have to update the type to match the declaration. | 
|---|
| 1441 | ast::FunctionType * type = new ast::FunctionType( | 
|---|
| 1442 | mutDecl->type->isVarArgs, mutDecl->type->qualifiers ); | 
|---|
| 1443 | // The forall clauses don't match until Eraser. The assertions are empty. | 
|---|
| 1444 | for ( auto param : mutDecl->params ) { | 
|---|
| 1445 | type->params.emplace_back( param->get_type() ); | 
|---|
| 1446 | } | 
|---|
| 1447 | for ( auto retval : mutDecl->returns ) { | 
|---|
| 1448 | type->returns.emplace_back( retval->get_type() ); | 
|---|
| 1449 | } | 
|---|
| 1450 | mutDecl->type = type; | 
|---|
| 1451 |  | 
|---|
| 1452 | return mutDecl; | 
|---|
| 1453 | } | 
|---|
| 1454 |  | 
|---|
| 1455 | ast::FunctionDecl const * DeclAdapter::postvisit( | 
|---|
| 1456 | ast::FunctionDecl const * decl ) { | 
|---|
| 1457 | ast::FunctionDecl * mutDecl = mutate( decl ); | 
|---|
| 1458 | if ( !mutDecl->returns.empty() && mutDecl->stmts | 
|---|
| 1459 | // Intrinsic functions won't be using the _retval so no need to | 
|---|
| 1460 | // generate it. | 
|---|
| 1461 | && mutDecl->linkage != ast::Linkage::Intrinsic | 
|---|
| 1462 | // Remove check for prefix once thunks properly use ctor/dtors. | 
|---|
| 1463 | && !isPrefix( mutDecl->name, "_thunk" ) | 
|---|
| 1464 | && !isPrefix( mutDecl->name, "_adapter" ) ) { | 
|---|
| 1465 | assert( 1 == mutDecl->returns.size() ); | 
|---|
| 1466 | ast::DeclWithType const * retval = mutDecl->returns.front(); | 
|---|
| 1467 | if ( "" == retval->name ) { | 
|---|
| 1468 | retval = ast::mutate_field( | 
|---|
| 1469 | retval, &ast::DeclWithType::name, "_retval" ); | 
|---|
| 1470 | mutDecl->returns.front() = retval; | 
|---|
| 1471 | } | 
|---|
| 1472 | auto stmts = mutDecl->stmts.get_and_mutate(); | 
|---|
| 1473 | stmts->kids.push_front( new ast::DeclStmt( retval->location, retval ) ); | 
|---|
| 1474 | ast::DeclWithType * newRet = ast::deepCopy( retval ); | 
|---|
| 1475 | mutDecl->returns.front() = newRet; | 
|---|
| 1476 | } | 
|---|
| 1477 | // Errors should have been caught by this point, remove initializers from | 
|---|
| 1478 | // parameters to allow correct codegen of default arguments. | 
|---|
| 1479 | for ( ast::ptr<ast::DeclWithType> & param : mutDecl->params ) { | 
|---|
| 1480 | if ( auto obj = param.as<ast::ObjectDecl>() ) { | 
|---|
| 1481 | param = ast::mutate_field( obj, &ast::ObjectDecl::init, nullptr ); | 
|---|
| 1482 | } | 
|---|
| 1483 | } | 
|---|
| 1484 | return mutDecl; | 
|---|
| 1485 | } | 
|---|
| 1486 |  | 
|---|
| 1487 | void DeclAdapter::addAdapters( | 
|---|
| 1488 | ast::FunctionDecl * mutDecl, TypeVarMap & localTypeVars ) { | 
|---|
| 1489 | ast::vector<ast::FunctionType> functions; | 
|---|
| 1490 | for ( ast::ptr<ast::DeclWithType> & arg : mutDecl->params ) { | 
|---|
| 1491 | ast::Type const * type = arg->get_type(); | 
|---|
| 1492 | type = findAndReplaceFunction( type, functions, localTypeVars, needsAdapter ); | 
|---|
| 1493 | arg.get_and_mutate()->set_type( type ); | 
|---|
| 1494 | } | 
|---|
| 1495 | std::set<std::string> adaptersDone; | 
|---|
| 1496 | for ( ast::ptr<ast::FunctionType> const & func : functions ) { | 
|---|
| 1497 | std::string mangleName = mangleAdapterName( func, localTypeVars ); | 
|---|
| 1498 | if ( adaptersDone.find( mangleName ) != adaptersDone.end() ) { | 
|---|
| 1499 | continue; | 
|---|
| 1500 | } | 
|---|
| 1501 | std::string adapterName = makeAdapterName( mangleName ); | 
|---|
| 1502 | // The adapter may not actually be used, so make sure it has unused. | 
|---|
| 1503 | mutDecl->params.insert( mutDecl->params.begin(), new ast::ObjectDecl( | 
|---|
| 1504 | mutDecl->location, adapterName, | 
|---|
| 1505 | new ast::PointerType( makeAdapterType( func, localTypeVars ) ), | 
|---|
| 1506 | nullptr, {}, {}, nullptr, | 
|---|
| 1507 | { new ast::Attribute( "unused" ) } ) ); | 
|---|
| 1508 | adaptersDone.insert( adaptersDone.begin(), mangleName ); | 
|---|
| 1509 | } | 
|---|
| 1510 | } | 
|---|
| 1511 |  | 
|---|
| 1512 | // -------------------------------------------------------------------------- | 
|---|
| 1513 | /// Corrects the floating nodes created in CallAdapter. | 
|---|
| 1514 | struct RewireAdapters final : public ast::WithGuards { | 
|---|
| 1515 | ScopedMap<std::string, ast::ObjectDecl const *> adapters; | 
|---|
| 1516 | void beginScope() { adapters.beginScope(); } | 
|---|
| 1517 | void endScope() { adapters.endScope(); } | 
|---|
| 1518 | void previsit( ast::FunctionDecl const * decl ); | 
|---|
| 1519 | ast::VariableExpr const * previsit( ast::VariableExpr const * expr ); | 
|---|
| 1520 | }; | 
|---|
| 1521 |  | 
|---|
| 1522 | void RewireAdapters::previsit( ast::FunctionDecl const * decl ) { | 
|---|
| 1523 | GuardScope( adapters ); | 
|---|
| 1524 | for ( ast::ptr<ast::DeclWithType> const & param : decl->params ) { | 
|---|
| 1525 | if ( auto objectParam = param.as<ast::ObjectDecl>() ) { | 
|---|
| 1526 | adapters.insert( objectParam->name, objectParam ); | 
|---|
| 1527 | } | 
|---|
| 1528 | } | 
|---|
| 1529 | } | 
|---|
| 1530 |  | 
|---|
| 1531 | ast::VariableExpr const * RewireAdapters::previsit( | 
|---|
| 1532 | ast::VariableExpr const * expr ) { | 
|---|
| 1533 | // If the node is not floating, we can skip. | 
|---|
| 1534 | if ( expr->var->isManaged() ) return expr; | 
|---|
| 1535 | auto it = adapters.find( expr->var->name ); | 
|---|
| 1536 | assertf( it != adapters.end(), "Could not correct floating node." ); | 
|---|
| 1537 | return ast::mutate_field( expr, &ast::VariableExpr::var, it->second ); | 
|---|
| 1538 | } | 
|---|
| 1539 |  | 
|---|
| 1540 | // -------------------------------------------------------------------------- | 
|---|
| 1541 | /// Inserts code to access polymorphic layout inforation. | 
|---|
| 1542 | /// * Replaces member and size/alignment/offsetof expressions on polymorphic | 
|---|
| 1543 | ///   generic types with calculated expressions. | 
|---|
| 1544 | /// * Replaces member expressions for polymorphic types with calculated | 
|---|
| 1545 | ///   add-field-offset-and-dereference. | 
|---|
| 1546 | /// * Calculates polymorphic offsetof expressions from offset array. | 
|---|
| 1547 | /// * Inserts dynamic calculation of polymorphic type layouts where needed. | 
|---|
| 1548 | struct PolyGenericCalculator final : | 
|---|
| 1549 | public ast::WithConstTypeSubstitution, | 
|---|
| 1550 | public ast::WithDeclsToAdd<>, | 
|---|
| 1551 | public ast::WithGuards, | 
|---|
| 1552 | public ast::WithStmtsToAdd<>, | 
|---|
| 1553 | public ast::WithVisitorRef<PolyGenericCalculator> { | 
|---|
| 1554 | PolyGenericCalculator(); | 
|---|
| 1555 |  | 
|---|
| 1556 | void previsit( ast::FunctionDecl const * decl ); | 
|---|
| 1557 | void previsit( ast::TypedefDecl const * decl ); | 
|---|
| 1558 | void previsit( ast::TypeDecl const * decl ); | 
|---|
| 1559 | ast::Decl const * postvisit( ast::TypeDecl const * decl ); | 
|---|
| 1560 | ast::StructDecl const * previsit( ast::StructDecl const * decl ); | 
|---|
| 1561 | ast::UnionDecl const * previsit( ast::UnionDecl const * decl ); | 
|---|
| 1562 | ast::DeclStmt const * previsit( ast::DeclStmt const * stmt ); | 
|---|
| 1563 | ast::Expr const * postvisit( ast::MemberExpr const * expr ); | 
|---|
| 1564 | void previsit( ast::AddressExpr const * expr ); | 
|---|
| 1565 | ast::Expr const * postvisit( ast::AddressExpr const * expr ); | 
|---|
| 1566 | ast::Expr const * postvisit( ast::SizeofExpr const * expr ); | 
|---|
| 1567 | ast::Expr const * postvisit( ast::AlignofExpr const * expr ); | 
|---|
| 1568 | ast::Expr const * postvisit( ast::OffsetofExpr const * expr ); | 
|---|
| 1569 | ast::Expr const * postvisit( ast::OffsetPackExpr const * expr ); | 
|---|
| 1570 |  | 
|---|
| 1571 | void beginScope(); | 
|---|
| 1572 | void endScope(); | 
|---|
| 1573 | private: | 
|---|
| 1574 | /// Makes a new variable in the current scope with the given name, | 
|---|
| 1575 | /// type and optional initializer. | 
|---|
| 1576 | ast::ObjectDecl * makeVar( | 
|---|
| 1577 | CodeLocation const & location, std::string const & name, | 
|---|
| 1578 | ast::Type const * type, ast::Init const * init = nullptr ); | 
|---|
| 1579 | /// Returns true if the type has a dynamic layout; | 
|---|
| 1580 | /// such a layout will be stored in appropriately-named local variables | 
|---|
| 1581 | /// when the function returns. | 
|---|
| 1582 | bool findGeneric( CodeLocation const & location, ast::Type const * ); | 
|---|
| 1583 | /// Adds type parameters to the layout call; will generate the | 
|---|
| 1584 | /// appropriate parameters if needed. | 
|---|
| 1585 | void addSTypeParamsToLayoutCall( | 
|---|
| 1586 | ast::UntypedExpr * layoutCall, | 
|---|
| 1587 | const ast::vector<ast::Type> & otypeParams ); | 
|---|
| 1588 | /// Change the type of generic aggregate members to char[]. | 
|---|
| 1589 | void mutateMembers( ast::AggregateDecl * aggr ); | 
|---|
| 1590 | /// Returns the calculated sizeof expression for type, or nullptr for use | 
|---|
| 1591 | /// C sizeof(). | 
|---|
| 1592 | ast::Expr const * genSizeof( CodeLocation const &, ast::Type const * ); | 
|---|
| 1593 | /// Enters a new scope for type-variables, | 
|---|
| 1594 | /// adding the type variables from the provided type. | 
|---|
| 1595 | void beginTypeScope( ast::Type const * ); | 
|---|
| 1596 |  | 
|---|
| 1597 | /// The type variables and polymorphic parameters currently in scope. | 
|---|
| 1598 | TypeVarMap scopeTypeVars; | 
|---|
| 1599 | /// Set of generic type layouts known in the current scope, | 
|---|
| 1600 | /// indexed by sizeofName. | 
|---|
| 1601 | ScopedSet<std::string> knownLayouts; | 
|---|
| 1602 | /// Set of non-generic types for which the offset array exists in the | 
|---|
| 1603 | /// current scope, indexed by offsetofName. | 
|---|
| 1604 | ScopedSet<std::string> knownOffsets; | 
|---|
| 1605 | /// Namer for VLA (variable length array) buffers. | 
|---|
| 1606 | UniqueName bufNamer; | 
|---|
| 1607 | /// If the argument of an AddressExpr is MemberExpr, it is stored here. | 
|---|
| 1608 | ast::MemberExpr const * addrMember = nullptr; | 
|---|
| 1609 | }; | 
|---|
| 1610 |  | 
|---|
| 1611 | PolyGenericCalculator::PolyGenericCalculator() : | 
|---|
| 1612 | knownLayouts(), knownOffsets(), bufNamer( "_buf" ) | 
|---|
| 1613 | {} | 
|---|
| 1614 |  | 
|---|
| 1615 | /// Converts polymorphic type into a suitable monomorphic representation. | 
|---|
| 1616 | /// Currently: __attribute__(( aligned(8) )) char[size_T]; | 
|---|
| 1617 | ast::Type * polyToMonoType( CodeLocation const & location, | 
|---|
| 1618 | ast::Type const * declType ) { | 
|---|
| 1619 | auto charType = new ast::BasicType( ast::BasicKind::Char ); | 
|---|
| 1620 | auto size = new ast::NameExpr( location, | 
|---|
| 1621 | sizeofName( Mangle::mangleType( declType ) ) ); | 
|---|
| 1622 | auto ret = new ast::ArrayType( charType, size, | 
|---|
| 1623 | ast::VariableLen, ast::DynamicDim, ast::CV::Qualifiers() ); | 
|---|
| 1624 | ret->attributes.emplace_back( new ast::Attribute( "aligned", | 
|---|
| 1625 | { ast::ConstantExpr::from_int( location, 8 ) } ) ); | 
|---|
| 1626 | return ret; | 
|---|
| 1627 | } | 
|---|
| 1628 |  | 
|---|
| 1629 | void PolyGenericCalculator::previsit( ast::FunctionDecl const * decl ) { | 
|---|
| 1630 | GuardScope( *this ); | 
|---|
| 1631 | beginTypeScope( decl->type ); | 
|---|
| 1632 | } | 
|---|
| 1633 |  | 
|---|
| 1634 | void PolyGenericCalculator::previsit( ast::TypedefDecl const * decl ) { | 
|---|
| 1635 | assertf( false, "All typedef declarations should be removed." ); | 
|---|
| 1636 | beginTypeScope( decl->base ); | 
|---|
| 1637 | } | 
|---|
| 1638 |  | 
|---|
| 1639 | void PolyGenericCalculator::previsit( ast::TypeDecl const * decl ) { | 
|---|
| 1640 | addToTypeVarMap( decl, scopeTypeVars ); | 
|---|
| 1641 | } | 
|---|
| 1642 |  | 
|---|
| 1643 | ast::Decl const * PolyGenericCalculator::postvisit( | 
|---|
| 1644 | ast::TypeDecl const * decl ) { | 
|---|
| 1645 | ast::Type const * base = decl->base; | 
|---|
| 1646 | if ( nullptr == base ) return decl; | 
|---|
| 1647 |  | 
|---|
| 1648 | // Add size/align variables for opaque type declarations. | 
|---|
| 1649 | ast::TypeInstType inst( decl->name, decl ); | 
|---|
| 1650 | std::string typeName = Mangle::mangleType( &inst ); | 
|---|
| 1651 |  | 
|---|
| 1652 | ast::ObjectDecl * sizeDecl = new ast::ObjectDecl( decl->location, | 
|---|
| 1653 | sizeofName( typeName ), makeLayoutCType(), | 
|---|
| 1654 | new ast::SingleInit( decl->location, | 
|---|
| 1655 | new ast::SizeofExpr( decl->location, deepCopy( base ) ) | 
|---|
| 1656 | ) | 
|---|
| 1657 | ); | 
|---|
| 1658 | ast::ObjectDecl * alignDecl = new ast::ObjectDecl( decl->location, | 
|---|
| 1659 | alignofName( typeName ), makeLayoutCType(), | 
|---|
| 1660 | new ast::SingleInit( decl->location, | 
|---|
| 1661 | new ast::AlignofExpr( decl->location, deepCopy( base ) ) | 
|---|
| 1662 | ) | 
|---|
| 1663 | ); | 
|---|
| 1664 |  | 
|---|
| 1665 | // Ensure that the initializing sizeof/alignof exprs are properly mutated. | 
|---|
| 1666 | sizeDecl->accept( *visitor ); | 
|---|
| 1667 | alignDecl->accept( *visitor ); | 
|---|
| 1668 |  | 
|---|
| 1669 | // A little trick to replace this with two declarations. | 
|---|
| 1670 | // Adding after makes sure that there is no conflict with adding stmts. | 
|---|
| 1671 | declsToAddAfter.push_back( alignDecl ); | 
|---|
| 1672 | return sizeDecl; | 
|---|
| 1673 | } | 
|---|
| 1674 |  | 
|---|
| 1675 | ast::StructDecl const * PolyGenericCalculator::previsit( | 
|---|
| 1676 | ast::StructDecl const * decl ) { | 
|---|
| 1677 | auto mutDecl = mutate( decl ); | 
|---|
| 1678 | mutateMembers( mutDecl ); | 
|---|
| 1679 | return mutDecl; | 
|---|
| 1680 | } | 
|---|
| 1681 |  | 
|---|
| 1682 | ast::UnionDecl const * PolyGenericCalculator::previsit( | 
|---|
| 1683 | ast::UnionDecl const * decl ) { | 
|---|
| 1684 | auto mutDecl = mutate( decl ); | 
|---|
| 1685 | mutateMembers( mutDecl ); | 
|---|
| 1686 | return mutDecl; | 
|---|
| 1687 | } | 
|---|
| 1688 |  | 
|---|
| 1689 | ast::DeclStmt const * PolyGenericCalculator::previsit( ast::DeclStmt const * stmt ) { | 
|---|
| 1690 | ast::ObjectDecl const * decl = stmt->decl.as<ast::ObjectDecl>(); | 
|---|
| 1691 | if ( !decl || !findGeneric( decl->location, decl->type ) ) { | 
|---|
| 1692 | return stmt; | 
|---|
| 1693 | } | 
|---|
| 1694 |  | 
|---|
| 1695 | // Change initialization of a polymorphic value object to allocate via a | 
|---|
| 1696 | // variable-length-array (alloca cannot be safely used in loops). | 
|---|
| 1697 | ast::ObjectDecl * newBuf = new ast::ObjectDecl( decl->location, | 
|---|
| 1698 | bufNamer.newName(), | 
|---|
| 1699 | polyToMonoType( decl->location, decl->type ), | 
|---|
| 1700 | nullptr, {}, ast::Linkage::C | 
|---|
| 1701 | ); | 
|---|
| 1702 | stmtsToAddBefore.push_back( new ast::DeclStmt( stmt->location, newBuf ) ); | 
|---|
| 1703 |  | 
|---|
| 1704 | // If the object has a cleanup attribute, the clean-up should be on the | 
|---|
| 1705 | // buffer, not the pointer. [Perhaps this should be lifted?] | 
|---|
| 1706 | auto matchAndMove = [newBuf]( ast::ptr<ast::Attribute> & attr ) { | 
|---|
| 1707 | if ( "cleanup" == attr->name ) { | 
|---|
| 1708 | newBuf->attributes.push_back( attr ); | 
|---|
| 1709 | return true; | 
|---|
| 1710 | } | 
|---|
| 1711 | return false; | 
|---|
| 1712 | }; | 
|---|
| 1713 |  | 
|---|
| 1714 | auto mutDecl = mutate( decl ); | 
|---|
| 1715 |  | 
|---|
| 1716 | // Forally, side effects are not safe in this function. But it works. | 
|---|
| 1717 | erase_if( mutDecl->attributes, matchAndMove ); | 
|---|
| 1718 |  | 
|---|
| 1719 | mutDecl->init = new ast::SingleInit( decl->location, | 
|---|
| 1720 | new ast::VariableExpr( decl->location, newBuf ) ); | 
|---|
| 1721 |  | 
|---|
| 1722 | return ast::mutate_field( stmt, &ast::DeclStmt::decl, mutDecl ); | 
|---|
| 1723 | } | 
|---|
| 1724 |  | 
|---|
| 1725 | /// Checks if memberDecl matches the decl from an aggregate. | 
|---|
| 1726 | bool isMember( ast::DeclWithType const * memberDecl, ast::Decl const * decl ) { | 
|---|
| 1727 | // No matter the field, if the name is different it is not the same. | 
|---|
| 1728 | if ( memberDecl->name != decl->name ) { | 
|---|
| 1729 | return false; | 
|---|
| 1730 | } | 
|---|
| 1731 |  | 
|---|
| 1732 | if ( memberDecl->name.empty() ) { | 
|---|
| 1733 | // Plan-9 Field: Match on unique_id. | 
|---|
| 1734 | return ( memberDecl->uniqueId == decl->uniqueId ); | 
|---|
| 1735 | } | 
|---|
| 1736 |  | 
|---|
| 1737 | ast::DeclWithType const * declWithType = | 
|---|
| 1738 | strict_dynamic_cast<ast::DeclWithType const *>( decl ); | 
|---|
| 1739 |  | 
|---|
| 1740 | if ( memberDecl->mangleName.empty() || declWithType->mangleName.empty() ) { | 
|---|
| 1741 | // Tuple-Element Field: Expect neither had mangled name; | 
|---|
| 1742 | // accept match on simple name (like field_2) only. | 
|---|
| 1743 | assert( memberDecl->mangleName.empty() ); | 
|---|
| 1744 | assert( declWithType->mangleName.empty() ); | 
|---|
| 1745 | return true; | 
|---|
| 1746 | } | 
|---|
| 1747 |  | 
|---|
| 1748 | // Ordinary Field: Use full name to accommodate overloading. | 
|---|
| 1749 | return ( memberDecl->mangleName == declWithType->mangleName ); | 
|---|
| 1750 | } | 
|---|
| 1751 |  | 
|---|
| 1752 | /// Finds the member in the base list that matches the given declaration; | 
|---|
| 1753 | /// returns its index, or -1 if not present. | 
|---|
| 1754 | long findMember( ast::DeclWithType const * memberDecl, | 
|---|
| 1755 | const ast::vector<ast::Decl> & baseDecls ) { | 
|---|
| 1756 | for ( auto const & [index, value] : enumerate( baseDecls ) ) { | 
|---|
| 1757 | if ( isMember( memberDecl, value.get() ) ) { | 
|---|
| 1758 | return index; | 
|---|
| 1759 | } | 
|---|
| 1760 | } | 
|---|
| 1761 | return -1; | 
|---|
| 1762 | } | 
|---|
| 1763 |  | 
|---|
| 1764 | /// Returns an index expression into the offset array for a type. | 
|---|
| 1765 | ast::Expr * makeOffsetIndex( CodeLocation const & location, | 
|---|
| 1766 | ast::Type const * objectType, long i ) { | 
|---|
| 1767 | std::string name = offsetofName( Mangle::mangleType( objectType ) ); | 
|---|
| 1768 | return ast::UntypedExpr::createCall( location, "?[?]", { | 
|---|
| 1769 | new ast::NameExpr( location, name ), | 
|---|
| 1770 | ast::ConstantExpr::from_ulong( location, i ), | 
|---|
| 1771 | } ); | 
|---|
| 1772 | } | 
|---|
| 1773 |  | 
|---|
| 1774 | ast::Expr const * PolyGenericCalculator::postvisit( | 
|---|
| 1775 | ast::MemberExpr const * expr ) { | 
|---|
| 1776 | // Only mutate member expressions for polymorphic types. | 
|---|
| 1777 | ast::Type const * objectType = hasPolyBase( | 
|---|
| 1778 | expr->aggregate->result, scopeTypeVars | 
|---|
| 1779 | ); | 
|---|
| 1780 | if ( !objectType ) return expr; | 
|---|
| 1781 | // Ensure layout for this type is available. | 
|---|
| 1782 | // The boolean result is ignored. | 
|---|
| 1783 | findGeneric( expr->location, objectType ); | 
|---|
| 1784 |  | 
|---|
| 1785 | // Replace member expression with dynamically-computed layout expression. | 
|---|
| 1786 | ast::Expr * newMemberExpr = nullptr; | 
|---|
| 1787 | if ( auto structType = dynamic_cast<ast::StructInstType const *>( objectType ) ) { | 
|---|
| 1788 | long offsetIndex = findMember( expr->member, structType->base->members ); | 
|---|
| 1789 | if ( -1 == offsetIndex ) return expr; | 
|---|
| 1790 |  | 
|---|
| 1791 | // Replace member expression with pointer to struct plus offset. | 
|---|
| 1792 | ast::UntypedExpr * fieldLoc = new ast::UntypedExpr( expr->location, | 
|---|
| 1793 | new ast::NameExpr( expr->location, "?+?" ) ); | 
|---|
| 1794 | ast::Expr * aggr = deepCopy( expr->aggregate ); | 
|---|
| 1795 | aggr->env = nullptr; | 
|---|
| 1796 | fieldLoc->args.push_back( aggr ); | 
|---|
| 1797 | fieldLoc->args.push_back( | 
|---|
| 1798 | makeOffsetIndex( expr->location, objectType, offsetIndex ) ); | 
|---|
| 1799 | fieldLoc->result = deepCopy( expr->result ); | 
|---|
| 1800 | newMemberExpr = fieldLoc; | 
|---|
| 1801 | // Union members are all at offset zero, so just use the aggregate expr. | 
|---|
| 1802 | } else if ( dynamic_cast<ast::UnionInstType const *>( objectType ) ) { | 
|---|
| 1803 | ast::Expr * aggr = deepCopy( expr->aggregate ); | 
|---|
| 1804 | aggr->env = nullptr; | 
|---|
| 1805 | aggr->result = deepCopy( expr->result ); | 
|---|
| 1806 | newMemberExpr = aggr; | 
|---|
| 1807 | } else { | 
|---|
| 1808 | return expr; | 
|---|
| 1809 | } | 
|---|
| 1810 | assert( newMemberExpr ); | 
|---|
| 1811 |  | 
|---|
| 1812 | // Must apply the generic substitution to the member type to handle cases | 
|---|
| 1813 | // where the member is a generic parameter subsituted by a known concrete | 
|---|
| 1814 | // type. [ex] | 
|---|
| 1815 | //      forall( T ) struct Box { T x; } | 
|---|
| 1816 | //      forall( T ) void f() { | 
|---|
| 1817 | //              Box( T * ) b; b.x; | 
|---|
| 1818 | //      } | 
|---|
| 1819 | // TODO: expr->result should be exactly expr->member->get_type() after | 
|---|
| 1820 | // substitution, so it doesn't seem like it should be necessary to apply | 
|---|
| 1821 | // the substitution manually. For some reason this is not currently the | 
|---|
| 1822 | // case. This requires more investigation. | 
|---|
| 1823 | ast::ptr<ast::Type> memberType = deepCopy( expr->member->get_type() ); | 
|---|
| 1824 | ast::TypeSubstitution sub = genericSubstitution( objectType ); | 
|---|
| 1825 | sub.apply( memberType ); | 
|---|
| 1826 |  | 
|---|
| 1827 | // Not all members of a polymorphic type are themselves of a polymorphic | 
|---|
| 1828 | // type; in this case the member expression should be wrapped and | 
|---|
| 1829 | // dereferenced to form an lvalue. | 
|---|
| 1830 | if ( !isPolyType( memberType, scopeTypeVars ) ) { | 
|---|
| 1831 | auto ptrCastExpr = new ast::CastExpr( expr->location, newMemberExpr, | 
|---|
| 1832 | new ast::PointerType( memberType ) ); | 
|---|
| 1833 | auto derefExpr = ast::UntypedExpr::createDeref( expr->location, | 
|---|
| 1834 | ptrCastExpr ); | 
|---|
| 1835 | newMemberExpr = derefExpr; | 
|---|
| 1836 | } | 
|---|
| 1837 |  | 
|---|
| 1838 | return newMemberExpr; | 
|---|
| 1839 | } | 
|---|
| 1840 |  | 
|---|
| 1841 | void PolyGenericCalculator::previsit( ast::AddressExpr const * expr ) { | 
|---|
| 1842 | GuardValue( addrMember ) = expr->arg.as<ast::MemberExpr>(); | 
|---|
| 1843 | } | 
|---|
| 1844 |  | 
|---|
| 1845 | ast::Expr const * PolyGenericCalculator::postvisit( | 
|---|
| 1846 | ast::AddressExpr const * expr ) { | 
|---|
| 1847 | // arg has to have been a MemberExpr and has been mutated. | 
|---|
| 1848 | if ( nullptr == addrMember || expr->arg == addrMember ) { | 
|---|
| 1849 | return expr; | 
|---|
| 1850 | } | 
|---|
| 1851 | ast::UntypedExpr const * untyped = expr->arg.as<ast::UntypedExpr>(); | 
|---|
| 1852 | if ( !untyped || getFunctionName( untyped ) != "?+?" ) { | 
|---|
| 1853 | return expr; | 
|---|
| 1854 | } | 
|---|
| 1855 | // MemberExpr was converted to pointer + offset; and it is not valid C to | 
|---|
| 1856 | // take the address of an addition, so strip away the address-of. | 
|---|
| 1857 | // It also preserves the env value. | 
|---|
| 1858 | return ast::mutate_field( expr->arg.get(), &ast::Expr::env, expr->env ); | 
|---|
| 1859 | } | 
|---|
| 1860 |  | 
|---|
| 1861 | ast::Expr const * PolyGenericCalculator::postvisit( | 
|---|
| 1862 | ast::SizeofExpr const * expr ) { | 
|---|
| 1863 | ast::Type const * type = expr->type ? expr->type : expr->expr->result; | 
|---|
| 1864 | ast::Expr const * gen = genSizeof( expr->location, type ); | 
|---|
| 1865 | return ( gen ) ? gen : expr; | 
|---|
| 1866 | } | 
|---|
| 1867 |  | 
|---|
| 1868 | ast::Expr const * PolyGenericCalculator::postvisit( | 
|---|
| 1869 | ast::AlignofExpr const * expr ) { | 
|---|
| 1870 | ast::Type const * type = expr->type ? expr->type : expr->expr->result; | 
|---|
| 1871 | if ( findGeneric( expr->location, type ) ) { | 
|---|
| 1872 | return new ast::NameExpr( expr->location, | 
|---|
| 1873 | alignofName( Mangle::mangleType( type ) ) ); | 
|---|
| 1874 | } else { | 
|---|
| 1875 | return expr; | 
|---|
| 1876 | } | 
|---|
| 1877 | } | 
|---|
| 1878 |  | 
|---|
| 1879 | ast::Expr const * PolyGenericCalculator::postvisit( | 
|---|
| 1880 | ast::OffsetofExpr const * expr ) { | 
|---|
| 1881 | ast::Type const * type = expr->type; | 
|---|
| 1882 | if ( !findGeneric( expr->location, type ) ) return expr; | 
|---|
| 1883 |  | 
|---|
| 1884 | // Structures replace offsetof expression with an index into offset array. | 
|---|
| 1885 | if ( auto structType = dynamic_cast<ast::StructInstType const *>( type ) ) { | 
|---|
| 1886 | long offsetIndex = findMember( expr->member, structType->base->members ); | 
|---|
| 1887 | if ( -1 == offsetIndex ) return expr; | 
|---|
| 1888 |  | 
|---|
| 1889 | return makeOffsetIndex( expr->location, type, offsetIndex ); | 
|---|
| 1890 | // All union members are at offset zero. | 
|---|
| 1891 | } else if ( dynamic_cast<ast::UnionInstType const *>( type ) ) { | 
|---|
| 1892 | return ast::ConstantExpr::from_ulong( expr->location, 0 ); | 
|---|
| 1893 | } else { | 
|---|
| 1894 | return expr; | 
|---|
| 1895 | } | 
|---|
| 1896 | } | 
|---|
| 1897 |  | 
|---|
| 1898 | ast::Expr const * PolyGenericCalculator::postvisit( | 
|---|
| 1899 | ast::OffsetPackExpr const * expr ) { | 
|---|
| 1900 | ast::StructInstType const * type = expr->type; | 
|---|
| 1901 |  | 
|---|
| 1902 | // Pull offset back from generated type information. | 
|---|
| 1903 | if ( findGeneric( expr->location, type ) ) { | 
|---|
| 1904 | return new ast::NameExpr( expr->location, | 
|---|
| 1905 | offsetofName( Mangle::mangleType( type ) ) ); | 
|---|
| 1906 | } | 
|---|
| 1907 |  | 
|---|
| 1908 | std::string offsetName = offsetofName( Mangle::mangleType( type ) ); | 
|---|
| 1909 | // Use the already generated offsets for this type. | 
|---|
| 1910 | if ( knownOffsets.contains( offsetName ) ) { | 
|---|
| 1911 | return new ast::NameExpr( expr->location, offsetName ); | 
|---|
| 1912 | } | 
|---|
| 1913 |  | 
|---|
| 1914 | knownOffsets.insert( offsetName ); | 
|---|
| 1915 |  | 
|---|
| 1916 | // Build initializer list for offset array. | 
|---|
| 1917 | ast::vector<ast::Init> inits; | 
|---|
| 1918 | for ( ast::ptr<ast::Decl> const & member : type->base->members ) { | 
|---|
| 1919 | auto memberDecl = member.as<ast::DeclWithType>(); | 
|---|
| 1920 | assertf( memberDecl, "Requesting offset of non-DWT member: %s", | 
|---|
| 1921 | toCString( member ) ); | 
|---|
| 1922 | inits.push_back( new ast::SingleInit( expr->location, | 
|---|
| 1923 | new ast::OffsetofExpr( expr->location, | 
|---|
| 1924 | deepCopy( type ), | 
|---|
| 1925 | memberDecl | 
|---|
| 1926 | ) | 
|---|
| 1927 | ) ); | 
|---|
| 1928 | } | 
|---|
| 1929 |  | 
|---|
| 1930 | auto offsetArray = makeVar( expr->location, offsetName, | 
|---|
| 1931 | new ast::ArrayType( | 
|---|
| 1932 | makeLayoutType(), | 
|---|
| 1933 | ast::ConstantExpr::from_ulong( expr->location, inits.size() ), | 
|---|
| 1934 | ast::FixedLen, | 
|---|
| 1935 | ast::DynamicDim | 
|---|
| 1936 | ), | 
|---|
| 1937 | new ast::ListInit( expr->location, std::move( inits ) ) | 
|---|
| 1938 | ); | 
|---|
| 1939 |  | 
|---|
| 1940 | return new ast::VariableExpr( expr->location, offsetArray ); | 
|---|
| 1941 | } | 
|---|
| 1942 |  | 
|---|
| 1943 | void PolyGenericCalculator::beginScope() { | 
|---|
| 1944 | knownLayouts.beginScope(); | 
|---|
| 1945 | knownOffsets.beginScope(); | 
|---|
| 1946 | } | 
|---|
| 1947 |  | 
|---|
| 1948 | void PolyGenericCalculator::endScope() { | 
|---|
| 1949 | knownOffsets.endScope(); | 
|---|
| 1950 | knownLayouts.endScope(); | 
|---|
| 1951 | } | 
|---|
| 1952 |  | 
|---|
| 1953 | ast::ObjectDecl * PolyGenericCalculator::makeVar( | 
|---|
| 1954 | CodeLocation const & location, std::string const & name, | 
|---|
| 1955 | ast::Type const * type, ast::Init const * init ) { | 
|---|
| 1956 | ast::ObjectDecl * ret = new ast::ObjectDecl( location, name, type, init ); | 
|---|
| 1957 | stmtsToAddBefore.push_back( new ast::DeclStmt( location, ret ) ); | 
|---|
| 1958 | return ret; | 
|---|
| 1959 | } | 
|---|
| 1960 |  | 
|---|
| 1961 | /// Returns true if any of the otype parameters have a dynamic layout; and | 
|---|
| 1962 | /// puts all otype parameters in the output list. | 
|---|
| 1963 | bool findGenericParams( | 
|---|
| 1964 | ast::vector<ast::Type> & out, | 
|---|
| 1965 | ast::vector<ast::TypeDecl> const & baseParams, | 
|---|
| 1966 | ast::vector<ast::Expr> const & typeParams ) { | 
|---|
| 1967 | bool hasDynamicLayout = false; | 
|---|
| 1968 |  | 
|---|
| 1969 | for ( auto const & [baseParam, typeParam] : group_iterate( | 
|---|
| 1970 | baseParams, typeParams ) ) { | 
|---|
| 1971 | if ( !baseParam->isComplete() ) continue; | 
|---|
| 1972 | ast::TypeExpr const * typeExpr = typeParam.as<ast::TypeExpr>(); | 
|---|
| 1973 | assertf( typeExpr, "All type parameters should be type expressions." ); | 
|---|
| 1974 |  | 
|---|
| 1975 | ast::Type const * type = typeExpr->type.get(); | 
|---|
| 1976 | out.push_back( type ); | 
|---|
| 1977 | if ( isPolyType( type ) ) hasDynamicLayout = true; | 
|---|
| 1978 | } | 
|---|
| 1979 |  | 
|---|
| 1980 | return hasDynamicLayout; | 
|---|
| 1981 | } | 
|---|
| 1982 |  | 
|---|
| 1983 | bool PolyGenericCalculator::findGeneric( | 
|---|
| 1984 | CodeLocation const & location, ast::Type const * type ) { | 
|---|
| 1985 | type = replaceTypeInst( type, typeSubs ); | 
|---|
| 1986 |  | 
|---|
| 1987 | if ( auto inst = dynamic_cast<ast::TypeInstType const *>( type ) ) { | 
|---|
| 1988 | // Assumes that getting put in the scopeTypeVars includes having the | 
|---|
| 1989 | // layout variables set. | 
|---|
| 1990 | if ( scopeTypeVars.contains( *inst ) ) { | 
|---|
| 1991 | return true; | 
|---|
| 1992 | } | 
|---|
| 1993 | } else if ( auto inst = dynamic_cast<ast::StructInstType const *>( type ) ) { | 
|---|
| 1994 | // Check if this type already has a layout generated for it. | 
|---|
| 1995 | std::string typeName = Mangle::mangleType( type ); | 
|---|
| 1996 | if ( knownLayouts.contains( typeName ) ) return true; | 
|---|
| 1997 |  | 
|---|
| 1998 | // Check if any type parameters have dynamic layout; | 
|---|
| 1999 | // If none do, this type is (or will be) monomorphized. | 
|---|
| 2000 | ast::vector<ast::Type> sizedParams; | 
|---|
| 2001 | if ( !findGenericParams( sizedParams, | 
|---|
| 2002 | inst->base->params, inst->params ) ) { | 
|---|
| 2003 | return false; | 
|---|
| 2004 | } | 
|---|
| 2005 |  | 
|---|
| 2006 | // Insert local variables for layout and generate call to layout | 
|---|
| 2007 | // function. | 
|---|
| 2008 | // Done early so as not to interfere with the later addition of | 
|---|
| 2009 | // parameters to the layout call. | 
|---|
| 2010 | knownLayouts.insert( typeName ); | 
|---|
| 2011 |  | 
|---|
| 2012 | int memberCount = inst->base->members.size(); | 
|---|
| 2013 | if ( 0 == memberCount ) { | 
|---|
| 2014 | // All empty structures have the same layout (size 1, align 1). | 
|---|
| 2015 | makeVar( location, | 
|---|
| 2016 | sizeofName( typeName ), makeLayoutType(), | 
|---|
| 2017 | new ast::SingleInit( location, | 
|---|
| 2018 | ast::ConstantExpr::from_ulong( location, 1 ) ) ); | 
|---|
| 2019 | makeVar( location, | 
|---|
| 2020 | alignofName( typeName ), makeLayoutType(), | 
|---|
| 2021 | new ast::SingleInit( location, | 
|---|
| 2022 | ast::ConstantExpr::from_ulong( location, 1 ) ) ); | 
|---|
| 2023 | // Since 0-length arrays are forbidden in C, skip the offset array. | 
|---|
| 2024 | } else { | 
|---|
| 2025 | ast::ObjectDecl const * sizeofVar = makeVar( location, | 
|---|
| 2026 | sizeofName( typeName ), makeLayoutType(), nullptr ); | 
|---|
| 2027 | ast::ObjectDecl const * alignofVar = makeVar( location, | 
|---|
| 2028 | alignofName( typeName ), makeLayoutType(), nullptr ); | 
|---|
| 2029 | ast::ObjectDecl const * offsetofVar = makeVar( location, | 
|---|
| 2030 | offsetofName( typeName ), | 
|---|
| 2031 | new ast::ArrayType( | 
|---|
| 2032 | makeLayoutType(), | 
|---|
| 2033 | ast::ConstantExpr::from_int( location, memberCount ), | 
|---|
| 2034 | ast::FixedLen, | 
|---|
| 2035 | ast::DynamicDim | 
|---|
| 2036 | ), | 
|---|
| 2037 | nullptr | 
|---|
| 2038 | ); | 
|---|
| 2039 |  | 
|---|
| 2040 | // Generate call to layout function. | 
|---|
| 2041 | ast::UntypedExpr * layoutCall = new ast::UntypedExpr( location, | 
|---|
| 2042 | new ast::NameExpr( location, layoutofName( inst->base ) ), | 
|---|
| 2043 | { | 
|---|
| 2044 | new ast::AddressExpr( | 
|---|
| 2045 | new ast::VariableExpr( location, sizeofVar ) ), | 
|---|
| 2046 | new ast::AddressExpr( | 
|---|
| 2047 | new ast::VariableExpr( location, alignofVar ) ), | 
|---|
| 2048 | new ast::VariableExpr( location, offsetofVar ), | 
|---|
| 2049 | } ); | 
|---|
| 2050 |  | 
|---|
| 2051 | addSTypeParamsToLayoutCall( layoutCall, sizedParams ); | 
|---|
| 2052 |  | 
|---|
| 2053 | stmtsToAddBefore.emplace_back( | 
|---|
| 2054 | new ast::ExprStmt( location, layoutCall ) ); | 
|---|
| 2055 | } | 
|---|
| 2056 |  | 
|---|
| 2057 | return true; | 
|---|
| 2058 | } else if ( auto inst = dynamic_cast<ast::UnionInstType const *>( type ) ) { | 
|---|
| 2059 | // Check if this type already has a layout generated for it. | 
|---|
| 2060 | std::string typeName = Mangle::mangleType( type ); | 
|---|
| 2061 | if ( knownLayouts.contains( typeName ) ) return true; | 
|---|
| 2062 |  | 
|---|
| 2063 | // Check if any type parameters have dynamic layout; | 
|---|
| 2064 | // If none do, this type is (or will be) monomorphized. | 
|---|
| 2065 | ast::vector<ast::Type> sizedParams; | 
|---|
| 2066 | if ( !findGenericParams( sizedParams, | 
|---|
| 2067 | inst->base->params, inst->params ) ) { | 
|---|
| 2068 | return false; | 
|---|
| 2069 | } | 
|---|
| 2070 |  | 
|---|
| 2071 | // Insert local variables for layout and generate call to layout | 
|---|
| 2072 | // function. | 
|---|
| 2073 | // Done early so as not to interfere with the later addition of | 
|---|
| 2074 | // parameters to the layout call. | 
|---|
| 2075 | knownLayouts.insert( typeName ); | 
|---|
| 2076 |  | 
|---|
| 2077 | ast::ObjectDecl * sizeofVar = makeVar( location, | 
|---|
| 2078 | sizeofName( typeName ), makeLayoutType() ); | 
|---|
| 2079 | ast::ObjectDecl * alignofVar = makeVar( location, | 
|---|
| 2080 | alignofName( typeName ), makeLayoutType() ); | 
|---|
| 2081 |  | 
|---|
| 2082 | ast::UntypedExpr * layoutCall = new ast::UntypedExpr( location, | 
|---|
| 2083 | new ast::NameExpr( location, layoutofName( inst->base ) ), | 
|---|
| 2084 | { | 
|---|
| 2085 | new ast::AddressExpr( | 
|---|
| 2086 | new ast::VariableExpr( location, sizeofVar ) ), | 
|---|
| 2087 | new ast::AddressExpr( | 
|---|
| 2088 | new ast::VariableExpr( location, alignofVar ) ), | 
|---|
| 2089 | } ); | 
|---|
| 2090 |  | 
|---|
| 2091 | addSTypeParamsToLayoutCall( layoutCall, sizedParams ); | 
|---|
| 2092 |  | 
|---|
| 2093 | stmtsToAddBefore.emplace_back( | 
|---|
| 2094 | new ast::ExprStmt( location, layoutCall ) ); | 
|---|
| 2095 |  | 
|---|
| 2096 | return true; | 
|---|
| 2097 | } | 
|---|
| 2098 | return false; | 
|---|
| 2099 | } | 
|---|
| 2100 |  | 
|---|
| 2101 | void PolyGenericCalculator::addSTypeParamsToLayoutCall( | 
|---|
| 2102 | ast::UntypedExpr * layoutCall, | 
|---|
| 2103 | const ast::vector<ast::Type> & otypeParams ) { | 
|---|
| 2104 | CodeLocation const & location = layoutCall->location; | 
|---|
| 2105 | ast::vector<ast::Expr> & args = layoutCall->args; | 
|---|
| 2106 | for ( ast::ptr<ast::Type> const & param : otypeParams ) { | 
|---|
| 2107 | if ( findGeneric( location, param ) ) { | 
|---|
| 2108 | // Push size/align vars for a generic parameter back. | 
|---|
| 2109 | std::string paramName = Mangle::mangleType( param ); | 
|---|
| 2110 | args.emplace_back( | 
|---|
| 2111 | new ast::NameExpr( location, sizeofName( paramName ) ) ); | 
|---|
| 2112 | args.emplace_back( | 
|---|
| 2113 | new ast::NameExpr( location, alignofName( paramName ) ) ); | 
|---|
| 2114 | } else { | 
|---|
| 2115 | args.emplace_back( | 
|---|
| 2116 | new ast::SizeofExpr( location, ast::deepCopy( param ) ) ); | 
|---|
| 2117 | args.emplace_back( | 
|---|
| 2118 | new ast::AlignofExpr( location, ast::deepCopy( param ) ) ); | 
|---|
| 2119 | } | 
|---|
| 2120 | } | 
|---|
| 2121 | } | 
|---|
| 2122 |  | 
|---|
| 2123 | void PolyGenericCalculator::mutateMembers( ast::AggregateDecl * aggr ) { | 
|---|
| 2124 | std::set<std::string> genericParams; | 
|---|
| 2125 | for ( ast::ptr<ast::TypeDecl> const & decl : aggr->params ) { | 
|---|
| 2126 | genericParams.insert( decl->name ); | 
|---|
| 2127 | } | 
|---|
| 2128 | for ( ast::ptr<ast::Decl> & decl : aggr->members ) { | 
|---|
| 2129 | auto field = decl.as<ast::ObjectDecl>(); | 
|---|
| 2130 | if ( nullptr == field ) continue; | 
|---|
| 2131 |  | 
|---|
| 2132 | ast::Type const * type = replaceTypeInst( field->type, typeSubs ); | 
|---|
| 2133 | auto typeInst = dynamic_cast<ast::TypeInstType const *>( type ); | 
|---|
| 2134 | if ( nullptr == typeInst ) continue; | 
|---|
| 2135 |  | 
|---|
| 2136 | // Do not try to monoporphize generic parameters. | 
|---|
| 2137 | if ( scopeTypeVars.contains( ast::TypeEnvKey( *typeInst ) ) && | 
|---|
| 2138 | !genericParams.count( typeInst->name ) ) { | 
|---|
| 2139 | // Polymorphic aggregate members should be converted into | 
|---|
| 2140 | // monomorphic members. Using char[size_T] here respects | 
|---|
| 2141 | // the expected sizing rules of an aggregate type. | 
|---|
| 2142 | decl = ast::mutate_field( field, &ast::ObjectDecl::type, | 
|---|
| 2143 | polyToMonoType( field->location, field->type ) ); | 
|---|
| 2144 | } | 
|---|
| 2145 | } | 
|---|
| 2146 | } | 
|---|
| 2147 |  | 
|---|
| 2148 | ast::Expr const * PolyGenericCalculator::genSizeof( | 
|---|
| 2149 | CodeLocation const & location, ast::Type const * type ) { | 
|---|
| 2150 | if ( auto * array = dynamic_cast<ast::ArrayType const *>( type ) ) { | 
|---|
| 2151 | // Generate calculated size for possibly generic array. | 
|---|
| 2152 | ast::Expr const * sizeofBase = genSizeof( location, array->base ); | 
|---|
| 2153 | if ( nullptr == sizeofBase ) return nullptr; | 
|---|
| 2154 | ast::Expr const * dim = array->dimension; | 
|---|
| 2155 | return makeOp( location, "?*?", sizeofBase, dim ); | 
|---|
| 2156 | } else if ( findGeneric( location, type ) ) { | 
|---|
| 2157 | // Generate calculated size for generic type. | 
|---|
| 2158 | return new ast::NameExpr( location, sizeofName( | 
|---|
| 2159 | Mangle::mangleType( type ) ) ); | 
|---|
| 2160 | } else { | 
|---|
| 2161 | return nullptr; | 
|---|
| 2162 | } | 
|---|
| 2163 | } | 
|---|
| 2164 |  | 
|---|
| 2165 | void PolyGenericCalculator::beginTypeScope( ast::Type const * type ) { | 
|---|
| 2166 | GuardScope( scopeTypeVars ); | 
|---|
| 2167 | makeTypeVarMap( type, scopeTypeVars ); | 
|---|
| 2168 | } | 
|---|
| 2169 |  | 
|---|
| 2170 | // -------------------------------------------------------------------------- | 
|---|
| 2171 | /// Removes unneeded or incorrect type information. | 
|---|
| 2172 | /// * Replaces initialization of polymorphic values with alloca. | 
|---|
| 2173 | /// * Replaces declaration of dtype/ftype with appropriate void expression. | 
|---|
| 2174 | /// * Replaces sizeof expressions of polymorphic types with a variable. | 
|---|
| 2175 | /// * Strips fields from generic structure declarations. | 
|---|
| 2176 | struct Eraser final : | 
|---|
| 2177 | public ast::WithGuards { | 
|---|
| 2178 | void guardTypeVarMap( ast::Type const * type ) { | 
|---|
| 2179 | GuardScope( scopeTypeVars ); | 
|---|
| 2180 | makeTypeVarMap( type, scopeTypeVars ); | 
|---|
| 2181 | } | 
|---|
| 2182 |  | 
|---|
| 2183 | ast::ObjectDecl const * previsit( ast::ObjectDecl const * decl ); | 
|---|
| 2184 | ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl ); | 
|---|
| 2185 | ast::FunctionDecl const * postvisit( ast::FunctionDecl const * decl ); | 
|---|
| 2186 | ast::TypedefDecl const * previsit( ast::TypedefDecl const * decl ); | 
|---|
| 2187 | ast::StructDecl const * previsit( ast::StructDecl const * decl ); | 
|---|
| 2188 | ast::UnionDecl const * previsit( ast::UnionDecl const * decl ); | 
|---|
| 2189 | void previsit( ast::TypeDecl const * decl ); | 
|---|
| 2190 | void previsit( ast::PointerType const * type ); | 
|---|
| 2191 | void previsit( ast::FunctionType const * type ); | 
|---|
| 2192 | public: | 
|---|
| 2193 | TypeVarMap scopeTypeVars; | 
|---|
| 2194 | }; | 
|---|
| 2195 |  | 
|---|
| 2196 | ast::ObjectDecl const * Eraser::previsit( ast::ObjectDecl const * decl ) { | 
|---|
| 2197 | guardTypeVarMap( decl->type ); | 
|---|
| 2198 | return scrubAllTypeVars( decl ); | 
|---|
| 2199 | } | 
|---|
| 2200 |  | 
|---|
| 2201 | ast::FunctionDecl const * Eraser::previsit( ast::FunctionDecl const * decl ) { | 
|---|
| 2202 | guardTypeVarMap( decl->type ); | 
|---|
| 2203 | return scrubAllTypeVars( decl ); | 
|---|
| 2204 | } | 
|---|
| 2205 |  | 
|---|
| 2206 | ast::FunctionDecl const * Eraser::postvisit( ast::FunctionDecl const * decl ) { | 
|---|
| 2207 | if ( decl->type_params.empty() ) return decl; | 
|---|
| 2208 | auto mutDecl = mutate( decl ); | 
|---|
| 2209 | mutDecl->type_params.clear(); | 
|---|
| 2210 | return mutDecl; | 
|---|
| 2211 | } | 
|---|
| 2212 |  | 
|---|
| 2213 | ast::TypedefDecl const * Eraser::previsit( ast::TypedefDecl const * decl ) { | 
|---|
| 2214 | guardTypeVarMap( decl->base ); | 
|---|
| 2215 | return scrubAllTypeVars( decl ); | 
|---|
| 2216 | } | 
|---|
| 2217 |  | 
|---|
| 2218 | /// Strips the members from a generic aggregate. | 
|---|
| 2219 | template<typename node_t> | 
|---|
| 2220 | node_t const * stripGenericMembers( node_t const * decl ) { | 
|---|
| 2221 | if ( decl->params.empty() ) return decl; | 
|---|
| 2222 | auto mutDecl = ast::mutate( decl ); | 
|---|
| 2223 | mutDecl->members.clear(); | 
|---|
| 2224 | return mutDecl; | 
|---|
| 2225 | } | 
|---|
| 2226 |  | 
|---|
| 2227 | ast::StructDecl const * Eraser::previsit( ast::StructDecl const * decl ) { | 
|---|
| 2228 | return stripGenericMembers( decl ); | 
|---|
| 2229 | } | 
|---|
| 2230 |  | 
|---|
| 2231 | ast::UnionDecl const * Eraser::previsit( ast::UnionDecl const * decl ) { | 
|---|
| 2232 | return stripGenericMembers( decl ); | 
|---|
| 2233 | } | 
|---|
| 2234 |  | 
|---|
| 2235 | void Eraser::previsit( ast::TypeDecl const * decl ) { | 
|---|
| 2236 | addToTypeVarMap( decl, scopeTypeVars ); | 
|---|
| 2237 | } | 
|---|
| 2238 |  | 
|---|
| 2239 | void Eraser::previsit( ast::PointerType const * type ) { | 
|---|
| 2240 | guardTypeVarMap( type ); | 
|---|
| 2241 | } | 
|---|
| 2242 |  | 
|---|
| 2243 | void Eraser::previsit( ast::FunctionType const * type ) { | 
|---|
| 2244 | guardTypeVarMap( type ); | 
|---|
| 2245 | } | 
|---|
| 2246 |  | 
|---|
| 2247 | } // namespace | 
|---|
| 2248 |  | 
|---|
| 2249 | // -------------------------------------------------------------------------- | 
|---|
| 2250 | void box( ast::TranslationUnit & translationUnit ) { | 
|---|
| 2251 | ast::Pass<LayoutFunctionBuilder>::run( translationUnit ); | 
|---|
| 2252 | ast::Pass<CallAdapter>::run( translationUnit ); | 
|---|
| 2253 | ast::Pass<DeclAdapter>::run( translationUnit ); | 
|---|
| 2254 | ast::Pass<RewireAdapters>::run( translationUnit ); | 
|---|
| 2255 | ast::Pass<PolyGenericCalculator>::run( translationUnit ); | 
|---|
| 2256 | ast::Pass<Eraser>::run( translationUnit ); | 
|---|
| 2257 | } | 
|---|
| 2258 |  | 
|---|
| 2259 | } // namespace GenPoly | 
|---|
| 2260 |  | 
|---|
| 2261 | // Local Variables: // | 
|---|
| 2262 | // tab-width: 4 // | 
|---|
| 2263 | // mode: c++ // | 
|---|
| 2264 | // compile-command: "make install" // | 
|---|
| 2265 | // End: // | 
|---|