|
| 1 | +//===--- ASTGen+CompilerBuildConfiguration.swift --------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024-2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftDiagnostics |
| 14 | +import SwiftIfConfig |
| 15 | +import SwiftSyntax |
| 16 | + |
| 17 | +/// Enumeration that separates an #if config decl from the underlying element. |
| 18 | +enum IfConfigOrUnderlying<Element> { |
| 19 | + case ifConfigDecl(IfConfigDeclSyntax) |
| 20 | + case underlying(Element) |
| 21 | +} |
| 22 | + |
| 23 | +extension ASTGenVisitor { |
| 24 | + /// Determine the active clause for the given #if, emitting any diagnostics |
| 25 | + /// produced due to the evaluation. |
| 26 | + func activeClause(in node: IfConfigDeclSyntax) -> IfConfigClauseSyntax? { |
| 27 | + // Determine the active clause. |
| 28 | + var buildConfiguration = self.buildConfiguration |
| 29 | + buildConfiguration.conditionLoc = generateSourceLoc(node) |
| 30 | + let (activeClause, diagnostics) = node.activeClause(in: buildConfiguration) |
| 31 | + diagnoseAll(diagnostics) |
| 32 | + |
| 33 | + return activeClause |
| 34 | + } |
| 35 | + |
| 36 | + /// Visit a collection of elements that may contain #if clauses within it |
| 37 | + /// within it, recursing into the active clauses and to ensure that every |
| 38 | + /// active element is visited. |
| 39 | + /// - Parameters: |
| 40 | + /// - elements: A syntax collection that can config `IfConfigDeclSyntax` |
| 41 | + /// nodes in it. |
| 42 | + /// - flatElementType: The element type within the syntax collection for the |
| 43 | + /// non-#if case. |
| 44 | + /// - split: Splits an element in elements into the two cases: #if config |
| 45 | + /// or a flat element. |
| 46 | + /// - body: The body that handles each syntax node of type FlatElement. |
| 47 | + func visitIfConfigElements<Elements, FlatElement>( |
| 48 | + _ elements: Elements, |
| 49 | + of flatElementType: FlatElement.Type, |
| 50 | + split: (Elements.Element) -> IfConfigOrUnderlying<FlatElement>, |
| 51 | + body: (FlatElement) -> Void |
| 52 | + ) where Elements: SyntaxCollection, FlatElement: SyntaxProtocol { |
| 53 | + for element in elements { |
| 54 | + switch split(element) { |
| 55 | + case .ifConfigDecl(let ifConfigDecl): |
| 56 | + if let activeClause = self.activeClause(in: ifConfigDecl), |
| 57 | + let activeElements = activeClause.elements { |
| 58 | + guard let activeElements = activeElements._syntaxNode.as(Elements.self) else { |
| 59 | + fatalError("Parser produced invalid nesting of #if decls") |
| 60 | + } |
| 61 | + |
| 62 | + visitIfConfigElements( |
| 63 | + activeElements, |
| 64 | + of: FlatElement.self, |
| 65 | + split: split, |
| 66 | + body: body |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + case .underlying(let element): |
| 71 | + body(element) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | + |
0 commit comments