//===--- Location.swift - Source location ---------------------------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// import SILBridging import AST /// Represents a location in source code. /// `Location` is used in SIL and by the Optimizer. /// /// When compiling a Swift file, `Location` is basically a `SourceLoc` + information about the /// containing debug scope. In this case the `SourceLoc` is directly retrieved from the AST nodes. /// /// However, for debug info which is de-serialized from a swiftmodule file, the location consists of /// a filename + line and column indices. From such a location, a `SourceLoc` can only be created by /// loading the file with `DiagnosticEngine.getLocationFromExternalSource`. /// /// In case of parsing textual SIL (e.g. with `sil-opt`), which does _not_ contain debug line /// information, the location is also a `SourceLoc` which points to the textual SIL. public struct Location: ProvidingSourceLocation, Equatable, CustomStringConvertible { let bridged: BridgedLocation public var description: String { return String(taking: bridged.getDebugDescription()) } public var sourceLoc: SourceLoc? { if hasValidLineNumber { return SourceLoc(bridged: bridged.getSourceLocation()) } return nil } public var fileNameAndPosition: (path: StringRef, line: Int, column: Int)? { if bridged.isFilenameAndLocation() { let loc = bridged.getFilenameAndLocation() return (StringRef(bridged: loc.path), loc.line, loc.column) } return nil } public func getSourceLocation(diagnosticEngine: DiagnosticEngine) -> SourceLoc? { if let sourceLoc = sourceLoc { return sourceLoc } if let (path, line, column) = fileNameAndPosition { return diagnosticEngine.getLocationFromExternalSource(path: path, line: line, column: column) } return nil } /// Keeps the debug scope but marks it as auto-generated. public var asAutoGenerated: Location { Location(bridged: bridged.getAutogeneratedLocation()) } public var asCleanup: Location { Location(bridged: bridged.getCleanupLocation()) } public func withScope(of other: Location) -> Location { Location(bridged: bridged.withScopeOf(other.bridged)) } public var hasValidLineNumber: Bool { bridged.hasValidLineNumber() } public var isAutoGenerated: Bool { bridged.isAutoGenerated() } public var isInlined: Bool { bridged.isInlined() } public var isDebugSteppable: Bool { hasValidLineNumber && !isAutoGenerated } /// The `Decl` if the location refers to a declaration. public var decl: Decl? { bridged.getDecl().decl } public static func ==(lhs: Location, rhs: Location) -> Bool { lhs.bridged.isEqualTo(rhs.bridged) } public func hasSameSourceLocation(as other: Location) -> Bool { bridged.hasSameSourceLocation(other.bridged) } public static var artificialUnreachableLocation: Location { Location(bridged: BridgedLocation.getArtificialUnreachableLocation()) } }