source: src/GenPoly/Box.cpp@ 4a89b52

Last change on this file since 4a89b52 was 83fd57d, checked in by Andrew Beach <ajbeach@…>, 23 months ago

Removed 'New' suffixes, they are no longer needed for disambiguation.

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