[a32b204] | 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 | // |
---|
[c92bdcc] | 7 | // Resolver.cpp -- |
---|
[a32b204] | 8 | // |
---|
[d76c588] | 9 | // Author : Aaron B. Moss |
---|
[a32b204] | 10 | // Created On : Sun May 17 12:17:01 2015 |
---|
[4c2fe47] | 11 | // Last Modified By : Peter A. Buhr |
---|
[ca9d65e] | 12 | // Last Modified On : Thu Dec 14 18:44:43 2023 |
---|
| 13 | // Update Count : 251 |
---|
[a32b204] | 14 | // |
---|
| 15 | |
---|
[e3e16bc] | 16 | #include <cassert> // for strict_dynamic_cast, assert |
---|
[ea6332d] | 17 | #include <memory> // for allocator, allocator_traits<... |
---|
| 18 | #include <tuple> // for get |
---|
[6d6e829] | 19 | #include <vector> // for vector |
---|
[ea6332d] | 20 | |
---|
[99d4584] | 21 | #include "Candidate.hpp" |
---|
| 22 | #include "CandidateFinder.hpp" |
---|
[c92bdcc] | 23 | #include "CurrentObject.hpp" // for CurrentObject |
---|
| 24 | #include "RenameVars.hpp" // for RenameVars, global_renamer |
---|
| 25 | #include "Resolver.hpp" |
---|
| 26 | #include "ResolveTypeof.hpp" |
---|
[4a89b52] | 27 | #include "ResolveMode.hpp" // for ResolveMode |
---|
[c92bdcc] | 28 | #include "Typeops.hpp" // for extractResultType |
---|
| 29 | #include "Unify.hpp" // for unify |
---|
[b2ea0cd] | 30 | #include "CompilationState.hpp" |
---|
[2a8f0c1] | 31 | #include "AST/Decl.hpp" |
---|
| 32 | #include "AST/Init.hpp" |
---|
[d76c588] | 33 | #include "AST/Pass.hpp" |
---|
[99d4584] | 34 | #include "AST/Print.hpp" |
---|
[d76c588] | 35 | #include "AST/SymbolTable.hpp" |
---|
[2773ab8] | 36 | #include "AST/Type.hpp" |
---|
[c92bdcc] | 37 | #include "Common/Eval.hpp" // for eval |
---|
[9feb34b] | 38 | #include "Common/Iterate.hpp" // for group_iterate |
---|
[c92bdcc] | 39 | #include "Common/SemanticError.hpp" // for SemanticError |
---|
| 40 | #include "Common/Stats/ResolveTime.hpp" // for ResolveTime::start(), ResolveTime::stop() |
---|
[9feb34b] | 41 | #include "Common/ToString.hpp" // for toCString |
---|
[c92bdcc] | 42 | #include "Common/UniqueName.hpp" // for UniqueName |
---|
| 43 | #include "InitTweak/GenInit.hpp" |
---|
| 44 | #include "InitTweak/InitTweak.hpp" // for isIntrinsicSingleArgCallStmt |
---|
| 45 | #include "SymTab/Mangler.hpp" // for Mangler |
---|
| 46 | #include "Tuples/Tuples.hpp" |
---|
| 47 | #include "Validate/FindSpecialDecls.hpp" // for SizeType |
---|
[51b7345] | 48 | |
---|
[d9a0e76] | 49 | using namespace std; |
---|
[51b7345] | 50 | |
---|
[d9a0e76] | 51 | namespace ResolvExpr { |
---|
[99d4584] | 52 | |
---|
[14755e5] | 53 | namespace { |
---|
| 54 | /// Finds deleted expressions in an expression tree |
---|
| 55 | struct DeleteFinder final : public ast::WithShortCircuiting, public ast::WithVisitorRef<DeleteFinder> { |
---|
| 56 | const ast::DeletedExpr * result = nullptr; |
---|
| 57 | |
---|
| 58 | void previsit( const ast::DeletedExpr * expr ) { |
---|
| 59 | if ( result ) { visit_children = false; } |
---|
| 60 | else { result = expr; } |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void previsit( const ast::Expr * expr ) { |
---|
| 64 | if ( result ) { visit_children = false; } |
---|
| 65 | if (expr->inferred.hasParams()) { |
---|
| 66 | for (auto & imp : expr->inferred.inferParams() ) { |
---|
| 67 | imp.second.expr->accept(*visitor); |
---|
[361bf01] | 68 | } |
---|
[99d4584] | 69 | } |
---|
[14755e5] | 70 | } |
---|
| 71 | }; |
---|
[4894239] | 72 | |
---|
[14755e5] | 73 | struct ResolveDesignators final : public ast::WithShortCircuiting { |
---|
| 74 | ResolveContext& context; |
---|
| 75 | bool result = false; |
---|
[4894239] | 76 | |
---|
[14755e5] | 77 | ResolveDesignators( ResolveContext& _context ): context(_context) {}; |
---|
[4894239] | 78 | |
---|
[14755e5] | 79 | void previsit( const ast::Node * ) { |
---|
| 80 | // short circuit if we already know there are designations |
---|
| 81 | if ( result ) visit_children = false; |
---|
| 82 | } |
---|
[4894239] | 83 | |
---|
[14755e5] | 84 | void previsit( const ast::Designation * des ) { |
---|
| 85 | if ( result ) visit_children = false; |
---|
| 86 | else if ( ! des->designators.empty() ) { |
---|
| 87 | if ( (des->designators.size() == 1) ) { |
---|
| 88 | const ast::Expr * designator = des->designators.at(0); |
---|
| 89 | if ( const ast::NameExpr * designatorName = dynamic_cast<const ast::NameExpr *>(designator) ) { |
---|
| 90 | auto candidates = context.symtab.lookupId(designatorName->name); |
---|
| 91 | for ( auto candidate : candidates ) { |
---|
| 92 | if ( dynamic_cast<const ast::EnumInstType *>(candidate.id->get_type()) ) { |
---|
| 93 | result = true; |
---|
| 94 | break; |
---|
[4894239] | 95 | } |
---|
[2345ab3] | 96 | } |
---|
| 97 | } |
---|
[4894239] | 98 | } |
---|
[14755e5] | 99 | visit_children = false; |
---|
[4894239] | 100 | } |
---|
[14755e5] | 101 | } |
---|
| 102 | }; |
---|
| 103 | } // anonymous namespace |
---|
[99d4584] | 104 | |
---|
[14755e5] | 105 | /// Check if this expression is or includes a deleted expression |
---|
| 106 | const ast::DeletedExpr * findDeletedExpr( const ast::Expr * expr ) { |
---|
| 107 | return ast::Pass<DeleteFinder>::read( expr ); |
---|
| 108 | } |
---|
[b7d92b96] | 109 | |
---|
[14755e5] | 110 | namespace { |
---|
| 111 | /// always-accept candidate filter |
---|
| 112 | bool anyCandidate( const Candidate & ) { return true; } |
---|
[99d4584] | 113 | |
---|
[14755e5] | 114 | /// Calls the CandidateFinder and finds the single best candidate |
---|
| 115 | CandidateRef findUnfinishedKindExpression( |
---|
| 116 | const ast::Expr * untyped, const ResolveContext & context, const std::string & kind, |
---|
| 117 | std::function<bool(const Candidate &)> pred = anyCandidate, ResolveMode mode = {} |
---|
| 118 | ) { |
---|
| 119 | if ( ! untyped ) return nullptr; |
---|
| 120 | |
---|
| 121 | // xxx - this isn't thread-safe, but should work until we parallelize the resolver |
---|
| 122 | static unsigned recursion_level = 0; |
---|
| 123 | |
---|
| 124 | ++recursion_level; |
---|
| 125 | ast::TypeEnvironment env; |
---|
| 126 | CandidateFinder finder( context, env ); |
---|
| 127 | finder.allowVoid = true; |
---|
| 128 | finder.find( untyped, recursion_level == 1 ? mode.atTopLevel() : mode ); |
---|
| 129 | --recursion_level; |
---|
| 130 | |
---|
| 131 | // produce a filtered list of candidates |
---|
| 132 | CandidateList candidates; |
---|
| 133 | for ( auto & cand : finder.candidates ) { |
---|
| 134 | if ( pred( *cand ) ) { candidates.emplace_back( cand ); } |
---|
| 135 | } |
---|
[99d4584] | 136 | |
---|
[14755e5] | 137 | // produce invalid error if no candidates |
---|
| 138 | if ( candidates.empty() ) { |
---|
| 139 | SemanticError( untyped, |
---|
| 140 | toString( "No reasonable alternatives for ", kind, (kind != "" ? " " : ""), |
---|
| 141 | "expression: ") ); |
---|
| 142 | } |
---|
[99d4584] | 143 | |
---|
[14755e5] | 144 | // search for cheapest candidate |
---|
| 145 | CandidateList winners; |
---|
| 146 | bool seen_undeleted = false; |
---|
| 147 | for ( CandidateRef & cand : candidates ) { |
---|
| 148 | int c = winners.empty() ? -1 : cand->cost.compare( winners.front()->cost ); |
---|
| 149 | |
---|
| 150 | if ( c > 0 ) continue; // skip more expensive than winner |
---|
| 151 | |
---|
| 152 | if ( c < 0 ) { |
---|
| 153 | // reset on new cheapest |
---|
| 154 | seen_undeleted = ! findDeletedExpr( cand->expr ); |
---|
| 155 | winners.clear(); |
---|
| 156 | } else /* if ( c == 0 ) */ { |
---|
| 157 | if ( findDeletedExpr( cand->expr ) ) { |
---|
| 158 | // skip deleted expression if already seen one equivalent-cost not |
---|
| 159 | if ( seen_undeleted ) continue; |
---|
| 160 | } else if ( ! seen_undeleted ) { |
---|
| 161 | // replace list of equivalent-cost deleted expressions with one non-deleted |
---|
[99d4584] | 162 | winners.clear(); |
---|
[14755e5] | 163 | seen_undeleted = true; |
---|
[99d4584] | 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
[14755e5] | 167 | winners.emplace_back( std::move( cand ) ); |
---|
| 168 | } |
---|
[99d4584] | 169 | |
---|
[14755e5] | 170 | // promote candidate.cvtCost to .cost |
---|
| 171 | // promoteCvtCost( winners ); |
---|
| 172 | |
---|
| 173 | // produce ambiguous errors, if applicable |
---|
| 174 | if ( winners.size() != 1 ) { |
---|
| 175 | std::ostringstream stream; |
---|
| 176 | stream << "Cannot choose between " << winners.size() << " alternatives for " |
---|
| 177 | << kind << (kind != "" ? " " : "") << "expression\n"; |
---|
| 178 | ast::print( stream, untyped ); |
---|
| 179 | stream << " Alternatives are:\n"; |
---|
| 180 | print( stream, winners, 1 ); |
---|
| 181 | SemanticError( untyped->location, stream.str() ); |
---|
| 182 | } |
---|
[99d4584] | 183 | |
---|
[14755e5] | 184 | // single selected choice |
---|
| 185 | CandidateRef & choice = winners.front(); |
---|
[99d4584] | 186 | |
---|
[14755e5] | 187 | // fail on only expression deleted |
---|
| 188 | if ( ! seen_undeleted ) { |
---|
| 189 | SemanticError( untyped->location, choice->expr.get(), "Unique best alternative " |
---|
| 190 | "includes deleted identifier in " ); |
---|
[99d4584] | 191 | } |
---|
| 192 | |
---|
[14755e5] | 193 | return std::move( choice ); |
---|
| 194 | } |
---|
[99d4584] | 195 | |
---|
[14755e5] | 196 | /// Strips extraneous casts out of an expression |
---|
| 197 | struct StripCasts final { |
---|
| 198 | const ast::Expr * postvisit( const ast::CastExpr * castExpr ) { |
---|
| 199 | if ( |
---|
| 200 | castExpr->isGenerated == ast::GeneratedCast |
---|
| 201 | && typesCompatible( castExpr->arg->result, castExpr->result ) |
---|
| 202 | ) { |
---|
| 203 | // generated cast is the same type as its argument, remove it after keeping env |
---|
| 204 | return ast::mutate_field( |
---|
| 205 | castExpr->arg.get(), &ast::Expr::env, castExpr->env ); |
---|
[99d4584] | 206 | } |
---|
[14755e5] | 207 | return castExpr; |
---|
| 208 | } |
---|
[99d4584] | 209 | |
---|
[14755e5] | 210 | static void strip( ast::ptr< ast::Expr > & expr ) { |
---|
| 211 | ast::Pass< StripCasts > stripper; |
---|
| 212 | expr = expr->accept( stripper ); |
---|
[60aaa51d] | 213 | } |
---|
[14755e5] | 214 | }; |
---|
[60aaa51d] | 215 | |
---|
[14755e5] | 216 | /// Swaps argument into expression pointer, saving original environment |
---|
| 217 | void swap_and_save_env( ast::ptr< ast::Expr > & expr, const ast::Expr * newExpr ) { |
---|
| 218 | ast::ptr< ast::TypeSubstitution > env = expr->env; |
---|
| 219 | expr.set_and_mutate( newExpr )->env = env; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | /// Removes cast to type of argument (unlike StripCasts, also handles non-generated casts) |
---|
| 223 | void removeExtraneousCast( ast::ptr<ast::Expr> & expr ) { |
---|
| 224 | if ( const ast::CastExpr * castExpr = expr.as< ast::CastExpr >() ) { |
---|
| 225 | if ( typesCompatible( castExpr->arg->result, castExpr->result ) ) { |
---|
| 226 | // cast is to the same type as its argument, remove it |
---|
| 227 | swap_and_save_env( expr, castExpr->arg ); |
---|
[b7d92b96] | 228 | } |
---|
| 229 | } |
---|
[14755e5] | 230 | } |
---|
[b7d92b96] | 231 | |
---|
[14755e5] | 232 | } // anonymous namespace |
---|
[6668a3e] | 233 | |
---|
[490fb92e] | 234 | /// Establish post-resolver invariants for expressions |
---|
[14755e5] | 235 | void finishExpr( |
---|
| 236 | ast::ptr< ast::Expr > & expr, const ast::TypeEnvironment & env, |
---|
| 237 | const ast::TypeSubstitution * oldenv = nullptr |
---|
| 238 | ) { |
---|
| 239 | // set up new type substitution for expression |
---|
| 240 | ast::ptr< ast::TypeSubstitution > newenv = |
---|
| 241 | oldenv ? oldenv : new ast::TypeSubstitution{}; |
---|
| 242 | env.writeToSubstitution( *newenv.get_and_mutate() ); |
---|
| 243 | expr.get_and_mutate()->env = std::move( newenv ); |
---|
| 244 | // remove unncecessary casts |
---|
| 245 | StripCasts::strip( expr ); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | ast::ptr< ast::Expr > resolveInVoidContext( |
---|
| 249 | const ast::Expr * expr, const ResolveContext & context, |
---|
| 250 | ast::TypeEnvironment & env |
---|
| 251 | ) { |
---|
| 252 | assertf( expr, "expected a non-null expression" ); |
---|
| 253 | |
---|
| 254 | // set up and resolve expression cast to void |
---|
| 255 | ast::ptr< ast::CastExpr > untyped = new ast::CastExpr{ expr }; |
---|
| 256 | CandidateRef choice = findUnfinishedKindExpression( |
---|
| 257 | untyped, context, "", anyCandidate, ResolveMode::withAdjustment() ); |
---|
| 258 | |
---|
| 259 | // a cast expression has either 0 or 1 interpretations (by language rules); |
---|
| 260 | // if 0, an exception has already been thrown, and this code will not run |
---|
| 261 | const ast::CastExpr * castExpr = choice->expr.strict_as< ast::CastExpr >(); |
---|
| 262 | env = std::move( choice->env ); |
---|
| 263 | |
---|
| 264 | return castExpr->arg; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | /// Resolve `untyped` to the expression whose candidate is the best match for a `void` |
---|
| 268 | /// context. |
---|
| 269 | ast::ptr< ast::Expr > findVoidExpression( |
---|
| 270 | const ast::Expr * untyped, const ResolveContext & context |
---|
| 271 | ) { |
---|
| 272 | ast::TypeEnvironment env; |
---|
| 273 | ast::ptr< ast::Expr > newExpr = resolveInVoidContext( untyped, context, env ); |
---|
| 274 | finishExpr( newExpr, env, untyped->env ); |
---|
| 275 | return newExpr; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | namespace { |
---|
| 279 | /// resolve `untyped` to the expression whose candidate satisfies `pred` with the |
---|
| 280 | /// lowest cost, returning the resolved version |
---|
| 281 | ast::ptr< ast::Expr > findKindExpression( |
---|
| 282 | const ast::Expr * untyped, const ResolveContext & context, |
---|
| 283 | std::function<bool(const Candidate &)> pred = anyCandidate, |
---|
| 284 | const std::string & kind = "", ResolveMode mode = {} |
---|
[4b7cce6] | 285 | ) { |
---|
[14755e5] | 286 | if ( ! untyped ) return {}; |
---|
| 287 | CandidateRef choice = |
---|
| 288 | findUnfinishedKindExpression( untyped, context, kind, pred, mode ); |
---|
| 289 | ResolvExpr::finishExpr( choice->expr, choice->env, untyped->env ); |
---|
| 290 | return std::move( choice->expr ); |
---|
[4b7cce6] | 291 | } |
---|
[b7d92b96] | 292 | |
---|
[14755e5] | 293 | /// Resolve `untyped` to the single expression whose candidate is the best match |
---|
[16ba4a6] | 294 | ast::ptr< ast::Expr > findSingleExpression( |
---|
[14755e5] | 295 | const ast::Expr * untyped, const ResolveContext & context |
---|
[16ba4a6] | 296 | ) { |
---|
[14755e5] | 297 | Stats::ResolveTime::start( untyped ); |
---|
| 298 | auto res = findKindExpression( untyped, context ); |
---|
| 299 | Stats::ResolveTime::stop(); |
---|
| 300 | return res; |
---|
[16ba4a6] | 301 | } |
---|
[14755e5] | 302 | } // anonymous namespace |
---|
| 303 | |
---|
| 304 | ast::ptr< ast::Expr > findSingleExpression( |
---|
| 305 | const ast::Expr * untyped, const ast::Type * type, |
---|
| 306 | const ResolveContext & context |
---|
| 307 | ) { |
---|
| 308 | assert( untyped && type ); |
---|
| 309 | ast::ptr< ast::Expr > castExpr = new ast::CastExpr{ untyped, type }; |
---|
| 310 | ast::ptr< ast::Expr > newExpr = findSingleExpression( castExpr, context ); |
---|
| 311 | removeExtraneousCast( newExpr ); |
---|
| 312 | return newExpr; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | namespace { |
---|
| 316 | bool structOrUnion( const Candidate & i ) { |
---|
| 317 | const ast::Type * t = i.expr->result->stripReferences(); |
---|
| 318 | return dynamic_cast< const ast::StructInstType * >( t ) || dynamic_cast< const ast::UnionInstType * >( t ); |
---|
[99d4584] | 319 | } |
---|
[14755e5] | 320 | /// Predicate for "Candidate has integral type" |
---|
| 321 | bool hasIntegralType( const Candidate & i ) { |
---|
| 322 | const ast::Type * type = i.expr->result; |
---|
| 323 | |
---|
| 324 | if ( auto bt = dynamic_cast< const ast::BasicType * >( type ) ) { |
---|
| 325 | return bt->isInteger(); |
---|
| 326 | } else if ( |
---|
| 327 | dynamic_cast< const ast::EnumInstType * >( type ) |
---|
| 328 | || dynamic_cast< const ast::ZeroType * >( type ) |
---|
| 329 | || dynamic_cast< const ast::OneType * >( type ) |
---|
| 330 | ) { |
---|
| 331 | return true; |
---|
| 332 | } else return false; |
---|
[d76c588] | 333 | } |
---|
| 334 | |
---|
[14755e5] | 335 | /// Resolve `untyped` as an integral expression, returning the resolved version |
---|
| 336 | ast::ptr< ast::Expr > findIntegralExpression( |
---|
| 337 | const ast::Expr * untyped, const ResolveContext & context |
---|
[234b1cb] | 338 | ) { |
---|
[14755e5] | 339 | return findKindExpression( untyped, context, hasIntegralType, "condition" ); |
---|
[234b1cb] | 340 | } |
---|
| 341 | |
---|
[ab780e6] | 342 | ast::ptr< ast::Expr > findCondExpression( |
---|
| 343 | const ast::Expr * untyped, const ResolveContext & context |
---|
| 344 | ) { |
---|
| 345 | if ( nullptr == untyped ) return untyped; |
---|
| 346 | ast::ptr<ast::Expr> condExpr = createCondExpr( untyped ); |
---|
| 347 | return findIntegralExpression( condExpr, context ); |
---|
| 348 | } |
---|
| 349 | |
---|
[14755e5] | 350 | /// check if a type is a character type |
---|
| 351 | bool isCharType( const ast::Type * t ) { |
---|
| 352 | if ( auto bt = dynamic_cast< const ast::BasicType * >( t ) ) { |
---|
[7a780ad] | 353 | return bt->kind == ast::BasicKind::Char |
---|
| 354 | || bt->kind == ast::BasicKind::SignedChar |
---|
| 355 | || bt->kind == ast::BasicKind::UnsignedChar; |
---|
[14755e5] | 356 | } |
---|
| 357 | return false; |
---|
[17a0ede2] | 358 | } |
---|
| 359 | |
---|
[14755e5] | 360 | /// Advance a type itertor to the next mutex parameter |
---|
| 361 | template<typename Iter> |
---|
| 362 | inline bool nextMutex( Iter & it, const Iter & end ) { |
---|
| 363 | while ( it != end && ! (*it)->is_mutex() ) { ++it; } |
---|
| 364 | return it != end; |
---|
| 365 | } |
---|
[ab780e6] | 366 | } // anonymous namespace |
---|
[14755e5] | 367 | |
---|
| 368 | class Resolver final |
---|
| 369 | : public ast::WithSymbolTable, public ast::WithGuards, |
---|
| 370 | public ast::WithVisitorRef<Resolver>, public ast::WithShortCircuiting, |
---|
| 371 | public ast::WithStmtsToAdd<> { |
---|
| 372 | |
---|
| 373 | ast::ptr< ast::Type > functionReturn = nullptr; |
---|
| 374 | ast::CurrentObject currentObject; |
---|
| 375 | // for work previously in GenInit |
---|
| 376 | static InitTweak::ManagedTypes managedTypes; |
---|
| 377 | ResolveContext context; |
---|
| 378 | |
---|
| 379 | bool inEnumDecl = false; |
---|
| 380 | |
---|
| 381 | public: |
---|
| 382 | static size_t traceId; |
---|
| 383 | Resolver( const ast::TranslationGlobal & global ) : |
---|
| 384 | ast::WithSymbolTable(ast::SymbolTable::ErrorDetection::ValidateOnAdd), |
---|
| 385 | context{ symtab, global } {} |
---|
| 386 | Resolver( const ResolveContext & context ) : |
---|
| 387 | ast::WithSymbolTable{ context.symtab }, |
---|
| 388 | context{ symtab, context.global } {} |
---|
| 389 | |
---|
| 390 | const ast::FunctionDecl * previsit( const ast::FunctionDecl * ); |
---|
| 391 | const ast::FunctionDecl * postvisit( const ast::FunctionDecl * ); |
---|
| 392 | const ast::ObjectDecl * previsit( const ast::ObjectDecl * ); |
---|
| 393 | void previsit( const ast::AggregateDecl * ); |
---|
| 394 | void previsit( const ast::StructDecl * ); |
---|
| 395 | void previsit( const ast::EnumDecl * ); |
---|
| 396 | const ast::StaticAssertDecl * previsit( const ast::StaticAssertDecl * ); |
---|
| 397 | |
---|
| 398 | const ast::ArrayType * previsit( const ast::ArrayType * ); |
---|
| 399 | const ast::PointerType * previsit( const ast::PointerType * ); |
---|
| 400 | |
---|
| 401 | const ast::ExprStmt * previsit( const ast::ExprStmt * ); |
---|
| 402 | const ast::AsmExpr * previsit( const ast::AsmExpr * ); |
---|
| 403 | const ast::AsmStmt * previsit( const ast::AsmStmt * ); |
---|
| 404 | const ast::IfStmt * previsit( const ast::IfStmt * ); |
---|
| 405 | const ast::WhileDoStmt * previsit( const ast::WhileDoStmt * ); |
---|
| 406 | const ast::ForStmt * previsit( const ast::ForStmt * ); |
---|
| 407 | const ast::SwitchStmt * previsit( const ast::SwitchStmt * ); |
---|
| 408 | const ast::CaseClause * previsit( const ast::CaseClause * ); |
---|
| 409 | const ast::BranchStmt * previsit( const ast::BranchStmt * ); |
---|
| 410 | const ast::ReturnStmt * previsit( const ast::ReturnStmt * ); |
---|
| 411 | const ast::ThrowStmt * previsit( const ast::ThrowStmt * ); |
---|
| 412 | const ast::CatchClause * previsit( const ast::CatchClause * ); |
---|
| 413 | const ast::CatchClause * postvisit( const ast::CatchClause * ); |
---|
| 414 | const ast::WaitForStmt * previsit( const ast::WaitForStmt * ); |
---|
| 415 | const ast::WithStmt * previsit( const ast::WithStmt * ); |
---|
| 416 | |
---|
| 417 | const ast::SingleInit * previsit( const ast::SingleInit * ); |
---|
| 418 | const ast::ListInit * previsit( const ast::ListInit * ); |
---|
| 419 | const ast::ConstructorInit * previsit( const ast::ConstructorInit * ); |
---|
| 420 | |
---|
| 421 | void resolveWithExprs(std::vector<ast::ptr<ast::Expr>> & exprs, std::list<ast::ptr<ast::Stmt>> & stmtsToAdd); |
---|
[bc61563] | 422 | bool shouldGenCtorInit( const ast::ObjectDecl * ) const; |
---|
[14755e5] | 423 | |
---|
| 424 | void beginScope() { managedTypes.beginScope(); } |
---|
| 425 | void endScope() { managedTypes.endScope(); } |
---|
| 426 | bool on_error(ast::ptr<ast::Decl> & decl); |
---|
| 427 | }; |
---|
| 428 | // size_t Resolver::traceId = Stats::Heap::new_stacktrace_id("Resolver"); |
---|
| 429 | |
---|
| 430 | InitTweak::ManagedTypes Resolver::managedTypes; |
---|
| 431 | |
---|
| 432 | void resolve( ast::TranslationUnit& translationUnit ) { |
---|
| 433 | ast::Pass< Resolver >::run( translationUnit, translationUnit.global ); |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | ast::ptr< ast::Init > resolveCtorInit( |
---|
| 437 | const ast::ConstructorInit * ctorInit, const ResolveContext & context |
---|
| 438 | ) { |
---|
| 439 | assert( ctorInit ); |
---|
| 440 | ast::Pass< Resolver > resolver( context ); |
---|
| 441 | return ctorInit->accept( resolver ); |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | const ast::Expr * resolveStmtExpr( |
---|
| 445 | const ast::StmtExpr * stmtExpr, const ResolveContext & context |
---|
| 446 | ) { |
---|
| 447 | assert( stmtExpr ); |
---|
| 448 | ast::Pass< Resolver > resolver( context ); |
---|
| 449 | auto ret = mutate(stmtExpr->accept(resolver)); |
---|
| 450 | strict_dynamic_cast< ast::StmtExpr * >( ret )->computeResult(); |
---|
| 451 | return ret; |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | namespace { |
---|
| 455 | const ast::Attribute * handleAttribute(const CodeLocation & loc, const ast::Attribute * attr, const ResolveContext & context) { |
---|
| 456 | std::string name = attr->normalizedName(); |
---|
| 457 | if (name == "constructor" || name == "destructor") { |
---|
| 458 | if (attr->params.size() == 1) { |
---|
| 459 | auto arg = attr->params.front(); |
---|
[7a780ad] | 460 | auto resolved = ResolvExpr::findSingleExpression( arg, new ast::BasicType( ast::BasicKind::LongLongSignedInt ), context ); |
---|
[14755e5] | 461 | auto result = eval(arg); |
---|
| 462 | |
---|
| 463 | auto mutAttr = mutate(attr); |
---|
| 464 | mutAttr->params.front() = resolved; |
---|
| 465 | if (! result.hasKnownValue) { |
---|
| 466 | SemanticWarning(loc, Warning::GccAttributes, |
---|
| 467 | toCString( name, " priorities must be integers from 0 to 65535 inclusive: ", arg ) ); |
---|
| 468 | } |
---|
| 469 | else { |
---|
| 470 | auto priority = result.knownValue; |
---|
| 471 | if (priority < 101) { |
---|
[16ba4a6] | 472 | SemanticWarning(loc, Warning::GccAttributes, |
---|
[14755e5] | 473 | toCString( name, " priorities from 0 to 100 are reserved for the implementation" ) ); |
---|
| 474 | } else if (priority < 201 && ! buildingLibrary()) { |
---|
| 475 | SemanticWarning(loc, Warning::GccAttributes, |
---|
| 476 | toCString( name, " priorities from 101 to 200 are reserved for the implementation" ) ); |
---|
[16ba4a6] | 477 | } |
---|
| 478 | } |
---|
[14755e5] | 479 | return mutAttr; |
---|
| 480 | } else if (attr->params.size() > 1) { |
---|
| 481 | SemanticWarning(loc, Warning::GccAttributes, toCString( "too many arguments to ", name, " attribute" ) ); |
---|
| 482 | } else { |
---|
| 483 | SemanticWarning(loc, Warning::GccAttributes, toCString( "too few arguments to ", name, " attribute" ) ); |
---|
[16ba4a6] | 484 | } |
---|
| 485 | } |
---|
[14755e5] | 486 | return attr; |
---|
[16ba4a6] | 487 | } |
---|
[14755e5] | 488 | } |
---|
[16ba4a6] | 489 | |
---|
[14755e5] | 490 | const ast::FunctionDecl * Resolver::previsit( const ast::FunctionDecl * functionDecl ) { |
---|
| 491 | GuardValue( functionReturn ); |
---|
[16ba4a6] | 492 | |
---|
[14755e5] | 493 | assert (functionDecl->unique()); |
---|
| 494 | if (!functionDecl->has_body() && !functionDecl->withExprs.empty()) { |
---|
| 495 | SemanticError(functionDecl->location, functionDecl, "Function without body has with declarations"); |
---|
| 496 | } |
---|
[16ba4a6] | 497 | |
---|
[14755e5] | 498 | if (!functionDecl->isTypeFixed) { |
---|
| 499 | auto mutDecl = mutate(functionDecl); |
---|
| 500 | auto mutType = mutDecl->type.get_and_mutate(); |
---|
[16ba4a6] | 501 | |
---|
[14755e5] | 502 | for (auto & attr: mutDecl->attributes) { |
---|
| 503 | attr = handleAttribute(mutDecl->location, attr, context ); |
---|
| 504 | } |
---|
[16ba4a6] | 505 | |
---|
[14755e5] | 506 | // handle assertions |
---|
[16ba4a6] | 507 | |
---|
[14755e5] | 508 | symtab.enterScope(); |
---|
| 509 | mutType->forall.clear(); |
---|
| 510 | mutType->assertions.clear(); |
---|
| 511 | for (auto & typeParam : mutDecl->type_params) { |
---|
| 512 | symtab.addType(typeParam); |
---|
| 513 | mutType->forall.emplace_back(new ast::TypeInstType(typeParam)); |
---|
| 514 | } |
---|
| 515 | for (auto & asst : mutDecl->assertions) { |
---|
| 516 | asst = fixObjectType(asst.strict_as<ast::ObjectDecl>(), context); |
---|
| 517 | symtab.addId(asst); |
---|
| 518 | mutType->assertions.emplace_back(new ast::VariableExpr(functionDecl->location, asst)); |
---|
| 519 | } |
---|
[16ba4a6] | 520 | |
---|
[14755e5] | 521 | // temporarily adds params to symbol table. |
---|
| 522 | // actual scoping rules for params and withexprs differ - see Pass::visit(FunctionDecl) |
---|
[16ba4a6] | 523 | |
---|
[14755e5] | 524 | std::vector<ast::ptr<ast::Type>> paramTypes; |
---|
| 525 | std::vector<ast::ptr<ast::Type>> returnTypes; |
---|
[3e5dd913] | 526 | |
---|
[14755e5] | 527 | for (auto & param : mutDecl->params) { |
---|
| 528 | param = fixObjectType(param.strict_as<ast::ObjectDecl>(), context); |
---|
| 529 | symtab.addId(param); |
---|
| 530 | paramTypes.emplace_back(param->get_type()); |
---|
| 531 | } |
---|
| 532 | for (auto & ret : mutDecl->returns) { |
---|
| 533 | ret = fixObjectType(ret.strict_as<ast::ObjectDecl>(), context); |
---|
| 534 | returnTypes.emplace_back(ret->get_type()); |
---|
| 535 | } |
---|
| 536 | // since function type in decl is just a view of param types, need to update that as well |
---|
| 537 | mutType->params = std::move(paramTypes); |
---|
| 538 | mutType->returns = std::move(returnTypes); |
---|
[16ba4a6] | 539 | |
---|
[14755e5] | 540 | auto renamedType = strict_dynamic_cast<const ast::FunctionType *>(renameTyVars(mutType, RenameMode::GEN_EXPR_ID)); |
---|
[16ba4a6] | 541 | |
---|
[14755e5] | 542 | std::list<ast::ptr<ast::Stmt>> newStmts; |
---|
| 543 | resolveWithExprs (mutDecl->withExprs, newStmts); |
---|
[16ba4a6] | 544 | |
---|
[14755e5] | 545 | if (mutDecl->stmts) { |
---|
| 546 | auto mutStmt = mutDecl->stmts.get_and_mutate(); |
---|
| 547 | mutStmt->kids.splice(mutStmt->kids.begin(), std::move(newStmts)); |
---|
| 548 | mutDecl->stmts = mutStmt; |
---|
[16ba4a6] | 549 | } |
---|
| 550 | |
---|
[14755e5] | 551 | symtab.leaveScope(); |
---|
[d76c588] | 552 | |
---|
[14755e5] | 553 | mutDecl->type = renamedType; |
---|
| 554 | mutDecl->mangleName = Mangle::mangle(mutDecl); |
---|
| 555 | mutDecl->isTypeFixed = true; |
---|
| 556 | functionDecl = mutDecl; |
---|
| 557 | } |
---|
| 558 | managedTypes.handleDWT(functionDecl); |
---|
| 559 | |
---|
| 560 | functionReturn = extractResultType( functionDecl->type ); |
---|
| 561 | return functionDecl; |
---|
| 562 | } |
---|
| 563 | |
---|
| 564 | const ast::FunctionDecl * Resolver::postvisit( const ast::FunctionDecl * functionDecl ) { |
---|
| 565 | // default value expressions have an environment which shouldn't be there and trips up |
---|
| 566 | // later passes. |
---|
| 567 | assert( functionDecl->unique() ); |
---|
| 568 | ast::FunctionType * mutType = mutate( functionDecl->type.get() ); |
---|
| 569 | |
---|
| 570 | for ( unsigned i = 0 ; i < mutType->params.size() ; ++i ) { |
---|
| 571 | if ( const ast::ObjectDecl * obj = mutType->params[i].as< ast::ObjectDecl >() ) { |
---|
| 572 | if ( const ast::SingleInit * init = obj->init.as< ast::SingleInit >() ) { |
---|
| 573 | if ( init->value->env == nullptr ) continue; |
---|
| 574 | // clone initializer minus the initializer environment |
---|
| 575 | auto mutParam = mutate( mutType->params[i].strict_as< ast::ObjectDecl >() ); |
---|
| 576 | auto mutInit = mutate( mutParam->init.strict_as< ast::SingleInit >() ); |
---|
| 577 | auto mutValue = mutate( mutInit->value.get() ); |
---|
| 578 | |
---|
| 579 | mutValue->env = nullptr; |
---|
| 580 | mutInit->value = mutValue; |
---|
| 581 | mutParam->init = mutInit; |
---|
| 582 | mutType->params[i] = mutParam; |
---|
| 583 | |
---|
| 584 | assert( ! mutType->params[i].strict_as< ast::ObjectDecl >()->init.strict_as< ast::SingleInit >()->value->env); |
---|
[e068c8a] | 585 | } |
---|
[2a8f0c1] | 586 | } |
---|
[d76c588] | 587 | } |
---|
[14755e5] | 588 | mutate_field(functionDecl, &ast::FunctionDecl::type, mutType); |
---|
| 589 | return functionDecl; |
---|
| 590 | } |
---|
| 591 | |
---|
[bc61563] | 592 | bool Resolver::shouldGenCtorInit( ast::ObjectDecl const * decl ) const { |
---|
| 593 | // If we shouldn't try to construct it, then don't. |
---|
| 594 | if ( !InitTweak::tryConstruct( decl ) ) return false; |
---|
| 595 | // Otherwise, if it is a managed type, we may construct it. |
---|
| 596 | if ( managedTypes.isManaged( decl ) ) return true; |
---|
| 597 | // Skip construction if it is trivial at compile-time. |
---|
| 598 | if ( InitTweak::isConstExpr( decl->init ) ) return false; |
---|
| 599 | // Skip construction for local declarations. |
---|
| 600 | return ( !isInFunction() || decl->storage.is_static ); |
---|
| 601 | } |
---|
| 602 | |
---|
[14755e5] | 603 | const ast::ObjectDecl * Resolver::previsit( const ast::ObjectDecl * objectDecl ) { |
---|
| 604 | // To handle initialization of routine pointers [e.g. int (*fp)(int) = foo()], |
---|
| 605 | // class-variable `initContext` is changed multiple times because the LHS is analyzed |
---|
| 606 | // twice. The second analysis changes `initContext` because a function type can contain |
---|
| 607 | // object declarations in the return and parameter types. Therefore each value of |
---|
| 608 | // `initContext` is retained so the type on the first analysis is preserved and used for |
---|
| 609 | // selecting the RHS. |
---|
| 610 | GuardValue( currentObject ); |
---|
| 611 | |
---|
| 612 | if ( inEnumDecl && dynamic_cast< const ast::EnumInstType * >( objectDecl->get_type() ) ) { |
---|
| 613 | // enumerator initializers should not use the enum type to initialize, since the |
---|
| 614 | // enum type is still incomplete at this point. Use `int` instead. |
---|
| 615 | |
---|
| 616 | if ( auto enumBase = dynamic_cast< const ast::EnumInstType * > |
---|
| 617 | ( objectDecl->get_type() )->base->base ) { |
---|
| 618 | objectDecl = fixObjectType( objectDecl, context ); |
---|
| 619 | currentObject = ast::CurrentObject{ |
---|
| 620 | objectDecl->location, |
---|
| 621 | enumBase |
---|
| 622 | }; |
---|
| 623 | } else { |
---|
| 624 | objectDecl = fixObjectType( objectDecl, context ); |
---|
| 625 | currentObject = ast::CurrentObject{ |
---|
[7a780ad] | 626 | objectDecl->location, new ast::BasicType{ ast::BasicKind::SignedInt } }; |
---|
[b7d92b96] | 627 | } |
---|
[14755e5] | 628 | } else { |
---|
| 629 | if ( !objectDecl->isTypeFixed ) { |
---|
| 630 | auto newDecl = fixObjectType(objectDecl, context); |
---|
| 631 | auto mutDecl = mutate(newDecl); |
---|
| 632 | |
---|
| 633 | // generate CtorInit wrapper when necessary. |
---|
| 634 | // in certain cases, fixObjectType is called before reaching |
---|
| 635 | // this object in visitor pass, thus disabling CtorInit codegen. |
---|
| 636 | // this happens on aggregate members and function parameters. |
---|
[bc61563] | 637 | if ( shouldGenCtorInit( mutDecl ) ) { |
---|
[14755e5] | 638 | // constructed objects cannot be designated |
---|
| 639 | if ( InitTweak::isDesignated( mutDecl->init ) ) { |
---|
| 640 | ast::Pass<ResolveDesignators> res( context ); |
---|
| 641 | maybe_accept( mutDecl->init.get(), res ); |
---|
| 642 | if ( !res.core.result ) { |
---|
| 643 | SemanticError( mutDecl, "Cannot include designations in the initializer for a managed Object.\n" |
---|
| 644 | "If this is really what you want, initialize with @=." ); |
---|
[92355883] | 645 | } |
---|
[16ba4a6] | 646 | } |
---|
[14755e5] | 647 | // constructed objects should not have initializers nested too deeply |
---|
| 648 | if ( ! InitTweak::checkInitDepth( mutDecl ) ) SemanticError( mutDecl, "Managed object's initializer is too deep " ); |
---|
[16ba4a6] | 649 | |
---|
[14755e5] | 650 | mutDecl->init = InitTweak::genCtorInit( mutDecl->location, mutDecl ); |
---|
[16ba4a6] | 651 | } |
---|
| 652 | |
---|
[14755e5] | 653 | objectDecl = mutDecl; |
---|
[16ba4a6] | 654 | } |
---|
[14755e5] | 655 | currentObject = ast::CurrentObject{ objectDecl->location, objectDecl->get_type() }; |
---|
[16ba4a6] | 656 | } |
---|
| 657 | |
---|
[14755e5] | 658 | return objectDecl; |
---|
| 659 | } |
---|
[d76c588] | 660 | |
---|
[14755e5] | 661 | void Resolver::previsit( const ast::AggregateDecl * _aggDecl ) { |
---|
| 662 | auto aggDecl = mutate(_aggDecl); |
---|
| 663 | assertf(aggDecl == _aggDecl, "type declarations must be unique"); |
---|
[b7d92b96] | 664 | |
---|
[14755e5] | 665 | for (auto & member: aggDecl->members) { |
---|
| 666 | // nested type decls are hoisted already. no need to do anything |
---|
| 667 | if (auto obj = member.as<ast::ObjectDecl>()) { |
---|
| 668 | member = fixObjectType(obj, context); |
---|
[0f6a7752] | 669 | } |
---|
[d76c588] | 670 | } |
---|
[14755e5] | 671 | } |
---|
| 672 | |
---|
| 673 | void Resolver::previsit( const ast::StructDecl * structDecl ) { |
---|
| 674 | previsit(static_cast<const ast::AggregateDecl *>(structDecl)); |
---|
| 675 | managedTypes.handleStruct(structDecl); |
---|
| 676 | } |
---|
| 677 | |
---|
| 678 | void Resolver::previsit( const ast::EnumDecl * ) { |
---|
| 679 | // in case we decide to allow nested enums |
---|
| 680 | GuardValue( inEnumDecl ); |
---|
| 681 | inEnumDecl = true; |
---|
| 682 | // don't need to fix types for enum fields |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | const ast::StaticAssertDecl * Resolver::previsit( |
---|
| 686 | const ast::StaticAssertDecl * assertDecl |
---|
| 687 | ) { |
---|
| 688 | return ast::mutate_field( |
---|
| 689 | assertDecl, &ast::StaticAssertDecl::cond, |
---|
| 690 | findIntegralExpression( assertDecl->cond, context ) ); |
---|
| 691 | } |
---|
| 692 | |
---|
| 693 | template< typename PtrType > |
---|
| 694 | const PtrType * handlePtrType( const PtrType * type, const ResolveContext & context ) { |
---|
| 695 | if ( type->dimension ) { |
---|
| 696 | const ast::Type * sizeType = context.global.sizeType.get(); |
---|
| 697 | ast::ptr< ast::Expr > dimension = findSingleExpression( type->dimension, sizeType, context ); |
---|
| 698 | assertf(dimension->env->empty(), "array dimension expr has nonempty env"); |
---|
| 699 | dimension.get_and_mutate()->env = nullptr; |
---|
| 700 | ast::mutate_field( type, &PtrType::dimension, dimension ); |
---|
[d76c588] | 701 | } |
---|
[14755e5] | 702 | return type; |
---|
| 703 | } |
---|
| 704 | |
---|
| 705 | const ast::ArrayType * Resolver::previsit( const ast::ArrayType * at ) { |
---|
| 706 | return handlePtrType( at, context ); |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | const ast::PointerType * Resolver::previsit( const ast::PointerType * pt ) { |
---|
| 710 | return handlePtrType( pt, context ); |
---|
| 711 | } |
---|
| 712 | |
---|
| 713 | const ast::ExprStmt * Resolver::previsit( const ast::ExprStmt * exprStmt ) { |
---|
| 714 | visit_children = false; |
---|
| 715 | assertf( exprStmt->expr, "ExprStmt has null expression in resolver" ); |
---|
| 716 | |
---|
| 717 | return ast::mutate_field( |
---|
| 718 | exprStmt, &ast::ExprStmt::expr, findVoidExpression( exprStmt->expr, context ) ); |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | const ast::AsmExpr * Resolver::previsit( const ast::AsmExpr * asmExpr ) { |
---|
| 722 | visit_children = false; |
---|
| 723 | |
---|
| 724 | asmExpr = ast::mutate_field( |
---|
| 725 | asmExpr, &ast::AsmExpr::operand, findVoidExpression( asmExpr->operand, context ) ); |
---|
| 726 | |
---|
| 727 | return asmExpr; |
---|
| 728 | } |
---|
| 729 | |
---|
| 730 | const ast::AsmStmt * Resolver::previsit( const ast::AsmStmt * asmStmt ) { |
---|
| 731 | visitor->maybe_accept( asmStmt, &ast::AsmStmt::input ); |
---|
| 732 | visitor->maybe_accept( asmStmt, &ast::AsmStmt::output ); |
---|
| 733 | visit_children = false; |
---|
| 734 | return asmStmt; |
---|
| 735 | } |
---|
| 736 | |
---|
| 737 | const ast::IfStmt * Resolver::previsit( const ast::IfStmt * ifStmt ) { |
---|
| 738 | return ast::mutate_field( |
---|
[ab780e6] | 739 | ifStmt, &ast::IfStmt::cond, findCondExpression( ifStmt->cond, context ) ); |
---|
[14755e5] | 740 | } |
---|
| 741 | |
---|
| 742 | const ast::WhileDoStmt * Resolver::previsit( const ast::WhileDoStmt * whileDoStmt ) { |
---|
| 743 | return ast::mutate_field( |
---|
[ab780e6] | 744 | whileDoStmt, &ast::WhileDoStmt::cond, findCondExpression( whileDoStmt->cond, context ) ); |
---|
[14755e5] | 745 | } |
---|
| 746 | |
---|
| 747 | const ast::ForStmt * Resolver::previsit( const ast::ForStmt * forStmt ) { |
---|
| 748 | if ( forStmt->cond ) { |
---|
| 749 | forStmt = ast::mutate_field( |
---|
[ab780e6] | 750 | forStmt, &ast::ForStmt::cond, findCondExpression( forStmt->cond, context ) ); |
---|
[d76c588] | 751 | } |
---|
| 752 | |
---|
[14755e5] | 753 | if ( forStmt->inc ) { |
---|
| 754 | forStmt = ast::mutate_field( |
---|
| 755 | forStmt, &ast::ForStmt::inc, findVoidExpression( forStmt->inc, context ) ); |
---|
[d76c588] | 756 | } |
---|
| 757 | |
---|
[14755e5] | 758 | return forStmt; |
---|
| 759 | } |
---|
| 760 | |
---|
| 761 | const ast::SwitchStmt * Resolver::previsit( const ast::SwitchStmt * switchStmt ) { |
---|
| 762 | GuardValue( currentObject ); |
---|
| 763 | switchStmt = ast::mutate_field( |
---|
| 764 | switchStmt, &ast::SwitchStmt::cond, |
---|
| 765 | findIntegralExpression( switchStmt->cond, context ) ); |
---|
| 766 | currentObject = ast::CurrentObject{ switchStmt->location, switchStmt->cond->result }; |
---|
| 767 | return switchStmt; |
---|
| 768 | } |
---|
| 769 | |
---|
| 770 | const ast::CaseClause * Resolver::previsit( const ast::CaseClause * caseStmt ) { |
---|
| 771 | if ( caseStmt->cond ) { |
---|
| 772 | std::deque< ast::InitAlternative > initAlts = currentObject.getOptions(); |
---|
| 773 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral " |
---|
| 774 | "expression." ); |
---|
| 775 | |
---|
| 776 | ast::ptr< ast::Expr > untyped = |
---|
| 777 | new ast::CastExpr{ caseStmt->location, caseStmt->cond, initAlts.front().type }; |
---|
| 778 | ast::ptr< ast::Expr > newExpr = findSingleExpression( untyped, context ); |
---|
| 779 | |
---|
| 780 | // case condition cannot have a cast in C, so it must be removed here, regardless of |
---|
| 781 | // whether it would perform a conversion. |
---|
| 782 | if ( const ast::CastExpr * castExpr = newExpr.as< ast::CastExpr >() ) { |
---|
| 783 | swap_and_save_env( newExpr, castExpr->arg ); |
---|
| 784 | } |
---|
[ef5b828] | 785 | |
---|
[14755e5] | 786 | caseStmt = ast::mutate_field( caseStmt, &ast::CaseClause::cond, newExpr ); |
---|
[d76c588] | 787 | } |
---|
[14755e5] | 788 | return caseStmt; |
---|
| 789 | } |
---|
| 790 | |
---|
| 791 | const ast::BranchStmt * Resolver::previsit( const ast::BranchStmt * branchStmt ) { |
---|
| 792 | visit_children = false; |
---|
| 793 | // must resolve the argument of a computed goto |
---|
| 794 | if ( branchStmt->kind == ast::BranchStmt::Goto && branchStmt->computedTarget ) { |
---|
| 795 | // computed goto argument is void* |
---|
| 796 | ast::ptr< ast::Type > target = new ast::PointerType{ new ast::VoidType{} }; |
---|
| 797 | branchStmt = ast::mutate_field( |
---|
| 798 | branchStmt, &ast::BranchStmt::computedTarget, |
---|
| 799 | findSingleExpression( branchStmt->computedTarget, target, context ) ); |
---|
[d76c588] | 800 | } |
---|
[14755e5] | 801 | return branchStmt; |
---|
| 802 | } |
---|
| 803 | |
---|
| 804 | const ast::ReturnStmt * Resolver::previsit( const ast::ReturnStmt * returnStmt ) { |
---|
| 805 | visit_children = false; |
---|
| 806 | if ( returnStmt->expr ) { |
---|
| 807 | returnStmt = ast::mutate_field( |
---|
| 808 | returnStmt, &ast::ReturnStmt::expr, |
---|
| 809 | findSingleExpression( returnStmt->expr, functionReturn, context ) ); |
---|
[d76c588] | 810 | } |
---|
[14755e5] | 811 | return returnStmt; |
---|
| 812 | } |
---|
| 813 | |
---|
| 814 | const ast::ThrowStmt * Resolver::previsit( const ast::ThrowStmt * throwStmt ) { |
---|
| 815 | visit_children = false; |
---|
| 816 | if ( throwStmt->expr ) { |
---|
| 817 | const ast::StructDecl * exceptionDecl = |
---|
| 818 | symtab.lookupStruct( "__cfaehm_base_exception_t" ); |
---|
| 819 | assert( exceptionDecl ); |
---|
| 820 | ast::ptr< ast::Type > exceptType = |
---|
| 821 | new ast::PointerType{ new ast::StructInstType{ exceptionDecl } }; |
---|
| 822 | throwStmt = ast::mutate_field( |
---|
| 823 | throwStmt, &ast::ThrowStmt::expr, |
---|
| 824 | findSingleExpression( throwStmt->expr, exceptType, context ) ); |
---|
[d76c588] | 825 | } |
---|
[14755e5] | 826 | return throwStmt; |
---|
| 827 | } |
---|
| 828 | |
---|
| 829 | const ast::CatchClause * Resolver::previsit( const ast::CatchClause * catchClause ) { |
---|
| 830 | // Until we are very sure this invarent (ifs that move between passes have then) |
---|
| 831 | // holds, check it. This allows a check for when to decode the mangling. |
---|
| 832 | if ( auto ifStmt = catchClause->body.as<ast::IfStmt>() ) { |
---|
| 833 | assert( ifStmt->then ); |
---|
[d76c588] | 834 | } |
---|
[14755e5] | 835 | // Encode the catchStmt so the condition can see the declaration. |
---|
| 836 | if ( catchClause->cond ) { |
---|
| 837 | ast::CatchClause * clause = mutate( catchClause ); |
---|
| 838 | clause->body = new ast::IfStmt( clause->location, clause->cond, nullptr, clause->body ); |
---|
| 839 | clause->cond = nullptr; |
---|
| 840 | return clause; |
---|
[d76c588] | 841 | } |
---|
[14755e5] | 842 | return catchClause; |
---|
| 843 | } |
---|
| 844 | |
---|
| 845 | const ast::CatchClause * Resolver::postvisit( const ast::CatchClause * catchClause ) { |
---|
| 846 | // Decode the catchStmt so everything is stored properly. |
---|
| 847 | const ast::IfStmt * ifStmt = catchClause->body.as<ast::IfStmt>(); |
---|
| 848 | if ( nullptr != ifStmt && nullptr == ifStmt->then ) { |
---|
| 849 | assert( ifStmt->cond ); |
---|
| 850 | assert( ifStmt->else_ ); |
---|
| 851 | ast::CatchClause * clause = ast::mutate( catchClause ); |
---|
| 852 | clause->cond = ifStmt->cond; |
---|
| 853 | clause->body = ifStmt->else_; |
---|
| 854 | // ifStmt should be implicately deleted here. |
---|
| 855 | return clause; |
---|
[d76c588] | 856 | } |
---|
[14755e5] | 857 | return catchClause; |
---|
| 858 | } |
---|
[d76c588] | 859 | |
---|
[14755e5] | 860 | const ast::WaitForStmt * Resolver::previsit( const ast::WaitForStmt * stmt ) { |
---|
| 861 | visit_children = false; |
---|
[d76c588] | 862 | |
---|
[14755e5] | 863 | // Resolve all clauses first |
---|
| 864 | for ( unsigned i = 0; i < stmt->clauses.size(); ++i ) { |
---|
| 865 | const ast::WaitForClause & clause = *stmt->clauses[i]; |
---|
[d76c588] | 866 | |
---|
[14755e5] | 867 | ast::TypeEnvironment env; |
---|
| 868 | CandidateFinder funcFinder( context, env ); |
---|
[d76c588] | 869 | |
---|
[14755e5] | 870 | // Find all candidates for a function in canonical form |
---|
| 871 | funcFinder.find( clause.target, ResolveMode::withAdjustment() ); |
---|
[b9fa85b] | 872 | |
---|
[14755e5] | 873 | if ( funcFinder.candidates.empty() ) { |
---|
| 874 | stringstream ss; |
---|
| 875 | ss << "Use of undeclared indentifier '"; |
---|
| 876 | ss << clause.target.strict_as< ast::NameExpr >()->name; |
---|
| 877 | ss << "' in call to waitfor"; |
---|
| 878 | SemanticError( stmt->location, ss.str() ); |
---|
[2b59f55] | 879 | } |
---|
[d76c588] | 880 | |
---|
[14755e5] | 881 | if ( clause.target_args.empty() ) { |
---|
| 882 | SemanticError( stmt->location, |
---|
| 883 | "Waitfor clause must have at least one mutex parameter"); |
---|
| 884 | } |
---|
[2773ab8] | 885 | |
---|
[14755e5] | 886 | // Find all alternatives for all arguments in canonical form |
---|
| 887 | std::vector< CandidateFinder > argFinders = |
---|
| 888 | funcFinder.findSubExprs( clause.target_args ); |
---|
| 889 | |
---|
| 890 | // List all combinations of arguments |
---|
| 891 | std::vector< CandidateList > possibilities; |
---|
| 892 | combos( argFinders.begin(), argFinders.end(), back_inserter( possibilities ) ); |
---|
| 893 | |
---|
| 894 | // For every possible function: |
---|
| 895 | // * try matching the arguments to the parameters, not the other way around because |
---|
| 896 | // more arguments than parameters |
---|
| 897 | CandidateList funcCandidates; |
---|
| 898 | std::vector< CandidateList > argsCandidates; |
---|
| 899 | SemanticErrorException errors; |
---|
| 900 | for ( CandidateRef & func : funcFinder.candidates ) { |
---|
| 901 | try { |
---|
| 902 | auto pointerType = dynamic_cast< const ast::PointerType * >( |
---|
| 903 | func->expr->result->stripReferences() ); |
---|
| 904 | if ( ! pointerType ) { |
---|
| 905 | SemanticError( stmt->location, func->expr->result.get(), |
---|
| 906 | "candidate not viable: not a pointer type\n" ); |
---|
| 907 | } |
---|
[2773ab8] | 908 | |
---|
[14755e5] | 909 | auto funcType = pointerType->base.as< ast::FunctionType >(); |
---|
| 910 | if ( ! funcType ) { |
---|
| 911 | SemanticError( stmt->location, func->expr->result.get(), |
---|
| 912 | "candidate not viable: not a function type\n" ); |
---|
| 913 | } |
---|
[2773ab8] | 914 | |
---|
[14755e5] | 915 | { |
---|
| 916 | auto param = funcType->params.begin(); |
---|
| 917 | auto paramEnd = funcType->params.end(); |
---|
[2773ab8] | 918 | |
---|
[14755e5] | 919 | if( ! nextMutex( param, paramEnd ) ) { |
---|
| 920 | SemanticError( stmt->location, funcType, |
---|
| 921 | "candidate function not viable: no mutex parameters\n"); |
---|
| 922 | } |
---|
| 923 | } |
---|
[2773ab8] | 924 | |
---|
[14755e5] | 925 | CandidateRef func2{ new Candidate{ *func } }; |
---|
| 926 | // strip reference from function |
---|
| 927 | func2->expr = referenceToRvalueConversion( func->expr, func2->cost ); |
---|
| 928 | |
---|
| 929 | // Each argument must be matched with a parameter of the current candidate |
---|
| 930 | for ( auto & argsList : possibilities ) { |
---|
| 931 | try { |
---|
| 932 | // Declare data structures needed for resolution |
---|
| 933 | ast::OpenVarSet open; |
---|
| 934 | ast::AssertionSet need, have; |
---|
| 935 | ast::TypeEnvironment resultEnv{ func->env }; |
---|
| 936 | // Add all type variables as open so that those not used in the |
---|
| 937 | // parameter list are still considered open |
---|
| 938 | resultEnv.add( funcType->forall ); |
---|
| 939 | |
---|
| 940 | // load type variables from arguments into one shared space |
---|
| 941 | for ( auto & arg : argsList ) { |
---|
| 942 | resultEnv.simpleCombine( arg->env ); |
---|
| 943 | } |
---|
[2773ab8] | 944 | |
---|
[14755e5] | 945 | // Make sure we don't widen any existing bindings |
---|
| 946 | resultEnv.forbidWidening(); |
---|
[2773ab8] | 947 | |
---|
[14755e5] | 948 | // Find any unbound type variables |
---|
| 949 | resultEnv.extractOpenVars( open ); |
---|
[2773ab8] | 950 | |
---|
[14755e5] | 951 | auto param = funcType->params.begin(); |
---|
[2773ab8] | 952 | auto paramEnd = funcType->params.end(); |
---|
| 953 | |
---|
[14755e5] | 954 | unsigned n_mutex_param = 0; |
---|
[2773ab8] | 955 | |
---|
[14755e5] | 956 | // For every argument of its set, check if it matches one of the |
---|
| 957 | // parameters. The order is important |
---|
| 958 | for ( auto & arg : argsList ) { |
---|
| 959 | // Ignore non-mutex arguments |
---|
| 960 | if ( ! nextMutex( param, paramEnd ) ) { |
---|
| 961 | // We ran out of parameters but still have arguments. |
---|
| 962 | // This function doesn't match |
---|
| 963 | SemanticError( stmt->location, funcType, |
---|
| 964 | toString("candidate function not viable: too many mutex " |
---|
| 965 | "arguments, expected ", n_mutex_param, "\n" ) ); |
---|
[2773ab8] | 966 | } |
---|
| 967 | |
---|
[14755e5] | 968 | ++n_mutex_param; |
---|
| 969 | |
---|
| 970 | // Check if the argument matches the parameter type in the current scope. |
---|
| 971 | // ast::ptr< ast::Type > paramType = (*param)->get_type(); |
---|
| 972 | |
---|
| 973 | if ( |
---|
| 974 | ! unify( |
---|
| 975 | arg->expr->result, *param, resultEnv, need, have, open ) |
---|
| 976 | ) { |
---|
| 977 | // Type doesn't match |
---|
| 978 | stringstream ss; |
---|
| 979 | ss << "candidate function not viable: no known conversion " |
---|
| 980 | "from '"; |
---|
| 981 | ast::print( ss, *param ); |
---|
| 982 | ss << "' to '"; |
---|
| 983 | ast::print( ss, arg->expr->result ); |
---|
| 984 | ss << "' with env '"; |
---|
| 985 | ast::print( ss, resultEnv ); |
---|
| 986 | ss << "'\n"; |
---|
| 987 | SemanticError( stmt->location, funcType, ss.str() ); |
---|
| 988 | } |
---|
[2773ab8] | 989 | |
---|
[14755e5] | 990 | ++param; |
---|
| 991 | } |
---|
[2773ab8] | 992 | |
---|
[14755e5] | 993 | // All arguments match! |
---|
[2773ab8] | 994 | |
---|
[14755e5] | 995 | // Check if parameters are missing |
---|
| 996 | if ( nextMutex( param, paramEnd ) ) { |
---|
| 997 | do { |
---|
[2773ab8] | 998 | ++n_mutex_param; |
---|
| 999 | ++param; |
---|
[14755e5] | 1000 | } while ( nextMutex( param, paramEnd ) ); |
---|
[2773ab8] | 1001 | |
---|
[14755e5] | 1002 | // We ran out of arguments but still have parameters left; this |
---|
| 1003 | // function doesn't match |
---|
| 1004 | SemanticError( stmt->location, funcType, |
---|
| 1005 | toString( "candidate function not viable: too few mutex " |
---|
| 1006 | "arguments, expected ", n_mutex_param, "\n" ) ); |
---|
| 1007 | } |
---|
[2773ab8] | 1008 | |
---|
[14755e5] | 1009 | // All parameters match! |
---|
[2773ab8] | 1010 | |
---|
[14755e5] | 1011 | // Finish the expressions to tie in proper environments |
---|
| 1012 | finishExpr( func2->expr, resultEnv ); |
---|
| 1013 | for ( CandidateRef & arg : argsList ) { |
---|
| 1014 | finishExpr( arg->expr, resultEnv ); |
---|
| 1015 | } |
---|
[2773ab8] | 1016 | |
---|
[14755e5] | 1017 | // This is a match, store it and save it for later |
---|
| 1018 | funcCandidates.emplace_back( std::move( func2 ) ); |
---|
| 1019 | argsCandidates.emplace_back( std::move( argsList ) ); |
---|
[2773ab8] | 1020 | |
---|
[14755e5] | 1021 | } catch ( SemanticErrorException & e ) { |
---|
| 1022 | errors.append( e ); |
---|
[2773ab8] | 1023 | } |
---|
| 1024 | } |
---|
[14755e5] | 1025 | } catch ( SemanticErrorException & e ) { |
---|
| 1026 | errors.append( e ); |
---|
[2773ab8] | 1027 | } |
---|
[14755e5] | 1028 | } |
---|
[2773ab8] | 1029 | |
---|
[14755e5] | 1030 | // Make sure correct number of arguments |
---|
| 1031 | if( funcCandidates.empty() ) { |
---|
| 1032 | SemanticErrorException top( stmt->location, |
---|
| 1033 | "No alternatives for function in call to waitfor" ); |
---|
| 1034 | top.append( errors ); |
---|
| 1035 | throw top; |
---|
| 1036 | } |
---|
[2773ab8] | 1037 | |
---|
[14755e5] | 1038 | if( argsCandidates.empty() ) { |
---|
| 1039 | SemanticErrorException top( stmt->location, |
---|
| 1040 | "No alternatives for arguments in call to waitfor" ); |
---|
| 1041 | top.append( errors ); |
---|
| 1042 | throw top; |
---|
| 1043 | } |
---|
[2773ab8] | 1044 | |
---|
[14755e5] | 1045 | if( funcCandidates.size() > 1 ) { |
---|
| 1046 | SemanticErrorException top( stmt->location, |
---|
| 1047 | "Ambiguous function in call to waitfor" ); |
---|
| 1048 | top.append( errors ); |
---|
| 1049 | throw top; |
---|
| 1050 | } |
---|
| 1051 | if( argsCandidates.size() > 1 ) { |
---|
| 1052 | SemanticErrorException top( stmt->location, |
---|
| 1053 | "Ambiguous arguments in call to waitfor" ); |
---|
| 1054 | top.append( errors ); |
---|
| 1055 | throw top; |
---|
[2773ab8] | 1056 | } |
---|
[14755e5] | 1057 | // TODO: need to use findDeletedExpr to ensure no deleted identifiers are used. |
---|
| 1058 | |
---|
| 1059 | // build new clause |
---|
| 1060 | auto clause2 = new ast::WaitForClause( clause.location ); |
---|
| 1061 | |
---|
| 1062 | clause2->target = funcCandidates.front()->expr; |
---|
| 1063 | |
---|
| 1064 | clause2->target_args.reserve( clause.target_args.size() ); |
---|
| 1065 | const ast::StructDecl * decl_monitor = symtab.lookupStruct( "monitor$" ); |
---|
| 1066 | for ( auto arg : argsCandidates.front() ) { |
---|
| 1067 | const auto & loc = stmt->location; |
---|
| 1068 | |
---|
| 1069 | ast::Expr * init = new ast::CastExpr( loc, |
---|
| 1070 | new ast::UntypedExpr( loc, |
---|
| 1071 | new ast::NameExpr( loc, "get_monitor" ), |
---|
| 1072 | { arg->expr } |
---|
| 1073 | ), |
---|
| 1074 | new ast::PointerType( |
---|
| 1075 | new ast::StructInstType( |
---|
| 1076 | decl_monitor |
---|
| 1077 | ) |
---|
| 1078 | ) |
---|
| 1079 | ); |
---|
[2773ab8] | 1080 | |
---|
[14755e5] | 1081 | clause2->target_args.emplace_back( findSingleExpression( init, context ) ); |
---|
[2773ab8] | 1082 | } |
---|
| 1083 | |
---|
[14755e5] | 1084 | // Resolve the conditions as if it were an IfStmt, statements normally |
---|
[ab780e6] | 1085 | clause2->when_cond = findCondExpression( clause.when_cond, context ); |
---|
[14755e5] | 1086 | clause2->stmt = clause.stmt->accept( *visitor ); |
---|
[2773ab8] | 1087 | |
---|
[14755e5] | 1088 | // set results into stmt |
---|
| 1089 | auto n = mutate( stmt ); |
---|
| 1090 | n->clauses[i] = clause2; |
---|
| 1091 | stmt = n; |
---|
| 1092 | } |
---|
[2773ab8] | 1093 | |
---|
[14755e5] | 1094 | if ( stmt->timeout_stmt ) { |
---|
| 1095 | // resolve the timeout as a size_t, the conditions like IfStmt, and stmts normally |
---|
| 1096 | ast::ptr< ast::Type > target = |
---|
[7a780ad] | 1097 | new ast::BasicType{ ast::BasicKind::LongLongUnsignedInt }; |
---|
[14755e5] | 1098 | auto timeout_time = findSingleExpression( stmt->timeout_time, target, context ); |
---|
[ab780e6] | 1099 | auto timeout_cond = findCondExpression( stmt->timeout_cond, context ); |
---|
[14755e5] | 1100 | auto timeout_stmt = stmt->timeout_stmt->accept( *visitor ); |
---|
| 1101 | |
---|
| 1102 | // set results into stmt |
---|
| 1103 | auto n = mutate( stmt ); |
---|
| 1104 | n->timeout_time = std::move( timeout_time ); |
---|
| 1105 | n->timeout_cond = std::move( timeout_cond ); |
---|
| 1106 | n->timeout_stmt = std::move( timeout_stmt ); |
---|
| 1107 | stmt = n; |
---|
[d76c588] | 1108 | } |
---|
| 1109 | |
---|
[14755e5] | 1110 | if ( stmt->else_stmt ) { |
---|
| 1111 | // resolve the condition like IfStmt, stmts normally |
---|
[ab780e6] | 1112 | auto else_cond = findCondExpression( stmt->else_cond, context ); |
---|
[14755e5] | 1113 | auto else_stmt = stmt->else_stmt->accept( *visitor ); |
---|
| 1114 | |
---|
| 1115 | // set results into stmt |
---|
| 1116 | auto n = mutate( stmt ); |
---|
| 1117 | n->else_cond = std::move( else_cond ); |
---|
| 1118 | n->else_stmt = std::move( else_stmt ); |
---|
| 1119 | stmt = n; |
---|
[16ba4a6] | 1120 | } |
---|
| 1121 | |
---|
[14755e5] | 1122 | return stmt; |
---|
| 1123 | } |
---|
| 1124 | |
---|
| 1125 | const ast::WithStmt * Resolver::previsit( const ast::WithStmt * withStmt ) { |
---|
| 1126 | auto mutStmt = mutate(withStmt); |
---|
| 1127 | resolveWithExprs(mutStmt->exprs, stmtsToAddBefore); |
---|
| 1128 | return mutStmt; |
---|
| 1129 | } |
---|
| 1130 | |
---|
| 1131 | void Resolver::resolveWithExprs(std::vector<ast::ptr<ast::Expr>> & exprs, std::list<ast::ptr<ast::Stmt>> & stmtsToAdd) { |
---|
| 1132 | for (auto & expr : exprs) { |
---|
| 1133 | // only struct- and union-typed expressions are viable candidates |
---|
| 1134 | expr = findKindExpression( expr, context, structOrUnion, "with expression" ); |
---|
| 1135 | |
---|
| 1136 | // if with expression might be impure, create a temporary so that it is evaluated once |
---|
| 1137 | if ( Tuples::maybeImpure( expr ) ) { |
---|
| 1138 | static UniqueName tmpNamer( "_with_tmp_" ); |
---|
| 1139 | const CodeLocation loc = expr->location; |
---|
| 1140 | auto tmp = new ast::ObjectDecl(loc, tmpNamer.newName(), expr->result, new ast::SingleInit(loc, expr ) ); |
---|
| 1141 | expr = new ast::VariableExpr( loc, tmp ); |
---|
| 1142 | stmtsToAdd.push_back( new ast::DeclStmt(loc, tmp ) ); |
---|
| 1143 | if ( InitTweak::isConstructable( tmp->type ) ) { |
---|
| 1144 | // generate ctor/dtor and resolve them |
---|
| 1145 | tmp->init = InitTweak::genCtorInit( loc, tmp ); |
---|
[9e23b446] | 1146 | } |
---|
[14755e5] | 1147 | // since tmp is freshly created, this should modify tmp in-place |
---|
| 1148 | tmp->accept( *visitor ); |
---|
| 1149 | } else if (expr->env && expr->env->empty()) { |
---|
| 1150 | expr = ast::mutate_field(expr.get(), &ast::Expr::env, nullptr); |
---|
[16ba4a6] | 1151 | } |
---|
| 1152 | } |
---|
[14755e5] | 1153 | } |
---|
| 1154 | |
---|
| 1155 | const ast::SingleInit * Resolver::previsit( const ast::SingleInit * singleInit ) { |
---|
| 1156 | visit_children = false; |
---|
| 1157 | // resolve initialization using the possibilities as determined by the `currentObject` |
---|
| 1158 | // cursor. |
---|
| 1159 | ast::ptr< ast::Expr > untyped = new ast::UntypedInitExpr{ |
---|
| 1160 | singleInit->location, singleInit->value, currentObject.getOptions() }; |
---|
| 1161 | ast::ptr<ast::Expr> newExpr = findSingleExpression( untyped, context ); |
---|
| 1162 | const ast::InitExpr * initExpr = newExpr.strict_as< ast::InitExpr >(); |
---|
| 1163 | |
---|
| 1164 | // move cursor to the object that is actually initialized |
---|
| 1165 | currentObject.setNext( initExpr->designation ); |
---|
| 1166 | |
---|
| 1167 | // discard InitExpr wrapper and retain relevant pieces. |
---|
| 1168 | // `initExpr` may have inferred params in the case where the expression specialized a |
---|
| 1169 | // function pointer, and newExpr may already have inferParams of its own, so a simple |
---|
| 1170 | // swap is not sufficient |
---|
| 1171 | ast::Expr::InferUnion inferred = initExpr->inferred; |
---|
| 1172 | swap_and_save_env( newExpr, initExpr->expr ); |
---|
| 1173 | newExpr.get_and_mutate()->inferred.splice( std::move(inferred) ); |
---|
| 1174 | |
---|
| 1175 | // get the actual object's type (may not exactly match what comes back from the resolver |
---|
| 1176 | // due to conversions) |
---|
| 1177 | const ast::Type * initContext = currentObject.getCurrentType(); |
---|
| 1178 | |
---|
| 1179 | removeExtraneousCast( newExpr ); |
---|
| 1180 | |
---|
| 1181 | // check if actual object's type is char[] |
---|
| 1182 | if ( auto at = dynamic_cast< const ast::ArrayType * >( initContext ) ) { |
---|
| 1183 | if ( isCharType( at->base ) ) { |
---|
| 1184 | // check if the resolved type is char* |
---|
| 1185 | if ( auto pt = newExpr->result.as< ast::PointerType >() ) { |
---|
| 1186 | if ( isCharType( pt->base ) ) { |
---|
| 1187 | // strip cast if we're initializing a char[] with a char* |
---|
| 1188 | // e.g. char x[] = "hello" |
---|
| 1189 | if ( auto ce = newExpr.as< ast::CastExpr >() ) { |
---|
| 1190 | swap_and_save_env( newExpr, ce->arg ); |
---|
[60aaa51d] | 1191 | } |
---|
| 1192 | } |
---|
| 1193 | } |
---|
| 1194 | } |
---|
[d76c588] | 1195 | } |
---|
| 1196 | |
---|
[14755e5] | 1197 | // move cursor to next object in preparation for next initializer |
---|
| 1198 | currentObject.increment(); |
---|
| 1199 | |
---|
| 1200 | // set initializer expression to resolved expression |
---|
| 1201 | return ast::mutate_field( singleInit, &ast::SingleInit::value, std::move(newExpr) ); |
---|
| 1202 | } |
---|
| 1203 | |
---|
| 1204 | const ast::ListInit * Resolver::previsit( const ast::ListInit * listInit ) { |
---|
| 1205 | // move cursor into brace-enclosed initializer-list |
---|
| 1206 | currentObject.enterListInit( listInit->location ); |
---|
| 1207 | |
---|
| 1208 | assert( listInit->designations.size() == listInit->initializers.size() ); |
---|
| 1209 | for ( unsigned i = 0; i < listInit->designations.size(); ++i ) { |
---|
| 1210 | // iterate designations and initializers in pairs, moving the cursor to the current |
---|
| 1211 | // designated object and resolving the initializer against that object |
---|
| 1212 | listInit = ast::mutate_field_index( |
---|
| 1213 | listInit, &ast::ListInit::designations, i, |
---|
| 1214 | currentObject.findNext( listInit->designations[i] ) ); |
---|
| 1215 | listInit = ast::mutate_field_index( |
---|
| 1216 | listInit, &ast::ListInit::initializers, i, |
---|
| 1217 | listInit->initializers[i]->accept( *visitor ) ); |
---|
[d76c588] | 1218 | } |
---|
| 1219 | |
---|
[14755e5] | 1220 | // move cursor out of brace-enclosed initializer-list |
---|
| 1221 | currentObject.exitListInit(); |
---|
[2d11663] | 1222 | |
---|
[14755e5] | 1223 | visit_children = false; |
---|
| 1224 | return listInit; |
---|
| 1225 | } |
---|
[2d11663] | 1226 | |
---|
[14755e5] | 1227 | const ast::ConstructorInit * Resolver::previsit( const ast::ConstructorInit * ctorInit ) { |
---|
| 1228 | visitor->maybe_accept( ctorInit, &ast::ConstructorInit::ctor ); |
---|
| 1229 | visitor->maybe_accept( ctorInit, &ast::ConstructorInit::dtor ); |
---|
[2d11663] | 1230 | |
---|
[14755e5] | 1231 | // found a constructor - can get rid of C-style initializer |
---|
| 1232 | // xxx - Rob suggests this field is dead code |
---|
| 1233 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::init, nullptr ); |
---|
| 1234 | |
---|
| 1235 | // intrinsic single-parameter constructors and destructors do nothing. Since this was |
---|
| 1236 | // implicitly generated, there's no way for it to have side effects, so get rid of it to |
---|
| 1237 | // clean up generated code |
---|
| 1238 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) { |
---|
| 1239 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::ctor, nullptr ); |
---|
| 1240 | } |
---|
| 1241 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) { |
---|
| 1242 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::dtor, nullptr ); |
---|
[d76c588] | 1243 | } |
---|
| 1244 | |
---|
[14755e5] | 1245 | return ctorInit; |
---|
| 1246 | } |
---|
| 1247 | |
---|
| 1248 | // suppress error on autogen functions and mark invalid autogen as deleted. |
---|
| 1249 | bool Resolver::on_error(ast::ptr<ast::Decl> & decl) { |
---|
| 1250 | if (auto functionDecl = decl.as<ast::FunctionDecl>()) { |
---|
| 1251 | // xxx - can intrinsic gen ever fail? |
---|
| 1252 | if (functionDecl->linkage == ast::Linkage::AutoGen) { |
---|
| 1253 | auto mutDecl = mutate(functionDecl); |
---|
| 1254 | mutDecl->isDeleted = true; |
---|
| 1255 | mutDecl->stmts = nullptr; |
---|
| 1256 | decl = mutDecl; |
---|
| 1257 | return false; |
---|
[0dd9a5e] | 1258 | } |
---|
| 1259 | } |
---|
[14755e5] | 1260 | return true; |
---|
| 1261 | } |
---|
[0dd9a5e] | 1262 | |
---|
[51b7345] | 1263 | } // namespace ResolvExpr |
---|
[a32b204] | 1264 | |
---|
| 1265 | // Local Variables: // |
---|
| 1266 | // tab-width: 4 // |
---|
| 1267 | // mode: c++ // |
---|
| 1268 | // compile-command: "make install" // |
---|
| 1269 | // End: // |
---|