Skip to content

Commit 253fbdf

Browse files
authored
Refactor nextValueForMacro so it does not rely on tail call optimization (#387)
1 parent 5cb9094 commit 253fbdf

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

Sources/SWBMacro/MacroEvaluationScope.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -345,16 +345,20 @@ final class MacroEvaluationContext {
345345

346346
/// Returns the next value for `macro` (this works correctly if even `macro` is the same as the macro declaration associated with the context, i.e. the inherited-value case — in this case the result is exactly the same as invoking `nextInheritedValue()`). Returns nil if there is no next value.
347347
func nextValueForMacro(_ macro: MacroDeclaration) -> MacroValueAssignment? {
348-
// If it’s the same macro as ours, it’s really just an inheritance lookup.
349-
if macro === self.macro {
350-
return nextInheritedValue()
351-
}
352-
// Otherwise, if we have a parent context, we defer the question to it.
353-
if let parent = self.parent {
354-
return parent.nextValueForMacro(macro)
348+
var currentContext: MacroEvaluationContext = self
349+
while true {
350+
// If it’s the same macro as ours, it’s really just an inheritance lookup.
351+
if macro === currentContext.macro {
352+
return currentContext.nextInheritedValue()
353+
}
354+
// Otherwise, if we have a parent context, we defer the question to it.
355+
if let parent = currentContext.parent {
356+
currentContext = parent
357+
} else {
358+
// If we get this far, we need to look up in our scope’s table. We might or might not find it.
359+
return currentContext.scope.table.lookupMacro(macro, overrideLookup: currentContext.lookup)?.firstMatchingCondition(currentContext.scope.conditionParameterValues)
360+
}
355361
}
356-
// If we get this far, we need to look up in our scope’s table. We might or might not find it.
357-
return scope.table.lookupMacro(macro, overrideLookup: lookup)?.firstMatchingCondition(scope.conditionParameterValues)
358362
}
359363
}
360364

0 commit comments

Comments
 (0)