|
| 1 | +//===---------------------------- CNodes.swift ----------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 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 | +@_implementationOnly import _InternalSwiftSyntaxParser |
| 14 | + |
| 15 | +typealias CSyntaxNode = swiftparse_syntax_node_t |
| 16 | +typealias CTriviaPiece = swiftparse_trivia_piece_t |
| 17 | +typealias CSyntaxNodePtr = UnsafePointer<CSyntaxNode> |
| 18 | +typealias CTriviaPiecePtr = UnsafePointer<CTriviaPiece> |
| 19 | +typealias CSyntaxKind = swiftparse_syntax_kind_t |
| 20 | +typealias CTokenKind = swiftparse_token_kind_t |
| 21 | +typealias CTriviaKind = swiftparse_trivia_kind_t |
| 22 | +typealias CTokenData = swiftparse_token_data_t |
| 23 | +typealias CLayoutData = swiftparse_layout_data_t |
| 24 | +typealias CParseLookupResult = swiftparse_lookup_result_t |
| 25 | +typealias CClientNode = swiftparse_client_node_t |
| 26 | +typealias CDiagnostic = swiftparser_diagnostic_t |
| 27 | +typealias CFixit = swiftparse_diagnostic_fixit_t |
| 28 | +typealias CRange = swiftparse_range_t |
| 29 | + |
| 30 | +/// Computes a hash value that describes the layout of all C nodes which are |
| 31 | +/// passed as opaque values between `SwiftSyntaxParser` and `SwiftSyntax`. |
| 32 | +/// This should match the value returned by the `cNodeLayoutHash` function in |
| 33 | +/// the `SwiftSyntax` module. |
| 34 | +public func cNodeLayoutHash() -> Int { |
| 35 | + var hasher = Hasher() |
| 36 | + |
| 37 | + // These two types are not defined in terms of the C types in SwiftSyntax. |
| 38 | + // Let's include them specifically in the hash as well. |
| 39 | + hasher.combine(MemoryLayout<CSyntaxKind>.size) |
| 40 | + hasher.combine(MemoryLayout<CClientNode>.size) |
| 41 | + |
| 42 | + hasher.combine(MemoryLayout<swiftparse_range_t>.size) |
| 43 | + hasher.combine(MemoryLayout<swiftparse_range_t>.offset(of: \.offset)) |
| 44 | + hasher.combine(MemoryLayout<swiftparse_range_t>.offset(of: \.length)) |
| 45 | + |
| 46 | + hasher.combine(MemoryLayout<swiftparse_trivia_kind_t>.size) |
| 47 | + hasher.combine(MemoryLayout<swiftparse_token_kind_t>.size) |
| 48 | + hasher.combine(MemoryLayout<swiftparse_syntax_kind_t>.size) |
| 49 | + |
| 50 | + hasher.combine(MemoryLayout<swiftparse_client_node_t>.size) |
| 51 | + |
| 52 | + hasher.combine(MemoryLayout<swiftparse_trivia_piece_t>.size) |
| 53 | + hasher.combine(MemoryLayout<swiftparse_trivia_piece_t>.offset(of: \.length)) |
| 54 | + hasher.combine(MemoryLayout<swiftparse_trivia_piece_t>.offset(of: \.kind)) |
| 55 | + |
| 56 | + hasher.combine(MemoryLayout<swiftparse_token_data_t>.size) |
| 57 | + hasher.combine(MemoryLayout<swiftparse_token_data_t>.offset(of: \.leading_trivia)) |
| 58 | + hasher.combine(MemoryLayout<swiftparse_token_data_t>.offset(of: \.trailing_trivia)) |
| 59 | + hasher.combine(MemoryLayout<swiftparse_token_data_t>.offset(of: \.leading_trivia_count)) |
| 60 | + hasher.combine(MemoryLayout<swiftparse_token_data_t>.offset(of: \.trailing_trivia_count)) |
| 61 | + hasher.combine(MemoryLayout<swiftparse_token_data_t>.offset(of: \.kind)) |
| 62 | + hasher.combine(MemoryLayout<swiftparse_token_data_t>.offset(of: \.range)) |
| 63 | + |
| 64 | + hasher.combine(MemoryLayout<swiftparse_layout_data_t>.size) |
| 65 | + hasher.combine(MemoryLayout<swiftparse_layout_data_t>.offset(of: \.nodes)) |
| 66 | + hasher.combine(MemoryLayout<swiftparse_layout_data_t>.offset(of: \.nodes_count)) |
| 67 | + |
| 68 | + hasher.combine(MemoryLayout<swiftparse_syntax_node_t>.size) |
| 69 | + hasher.combine(MemoryLayout<swiftparse_syntax_node_t>.offset(of: \.kind)) |
| 70 | + hasher.combine(MemoryLayout<swiftparse_syntax_node_t>.offset(of: \.present)) |
| 71 | + |
| 72 | + return hasher.finalize() |
| 73 | +} |
0 commit comments