Skip to content

Commit 9841d1c

Browse files
authored
Merge pull request #39441 from ahoppen/pr/split-swiftsyntax-c-data-types
[SyntaxParser] Split definition of C data types into separate file
2 parents ce334af + b147dfa commit 9841d1c

File tree

2 files changed

+77
-46
lines changed

2 files changed

+77
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===------------------ SwiftSyntaxCDataTypes.h -------------------*- C -*-===//
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+
// There are two copies of this file in the Swift compiler repository and in //
13+
// the SwiftSyntax repository. They need to always share the same contents. //
14+
// //
15+
// The reason behind this is that the SwiftSyntax module should be able to //
16+
// build with no dependencies to the compiler repo (in particular not //
17+
// _InternalSwiftSyntaxParser) so that it can be used for code generation //
18+
// without a matching toolchain. But to make SwiftSyntax parsing efficient, //
19+
// we need to create RawSyntax nodes from the C nodes without any conversion //
20+
// and thus SwiftSyntax needs to have knowledge about the layout of these C //
21+
// types. Thus the two copies of the file. The equality of these files is //
22+
// checked in CI (verify the source matches) and at runtime by //
23+
// SwiftSyntaxParser (verify that a hash generated from the layouts matches) //
24+
//===----------------------------------------------------------------------===//
25+
26+
#ifndef SWIFT_C_SYNTAX_C_DATA_TYPES_H
27+
#define SWIFT_C_SYNTAX_C_DATA_TYPES_H
28+
29+
#include <stdbool.h>
30+
31+
/// Offset+length in UTF8 bytes.
32+
typedef struct {
33+
uint32_t offset;
34+
uint32_t length;
35+
} swiftparse_range_t;
36+
37+
typedef uint8_t swiftparse_trivia_kind_t;
38+
typedef uint8_t swiftparse_token_kind_t;
39+
typedef uint16_t swiftparse_syntax_kind_t;
40+
41+
/// This is for the client to provide an opaque pointer that the parser will
42+
/// associate with a syntax node.
43+
typedef void *swiftparse_client_node_t;
44+
45+
typedef struct {
46+
/// The length in source this trivia piece occupies, in UTF8 bytes.
47+
uint32_t length;
48+
swiftparse_trivia_kind_t kind;
49+
} swiftparse_trivia_piece_t;
50+
51+
typedef struct {
52+
const swiftparse_trivia_piece_t *leading_trivia;
53+
const swiftparse_trivia_piece_t *trailing_trivia;
54+
uint16_t leading_trivia_count;
55+
uint16_t trailing_trivia_count;
56+
swiftparse_token_kind_t kind;
57+
/// Represents the range for the node, including trivia.
58+
swiftparse_range_t range;
59+
} swiftparse_token_data_t;
60+
61+
typedef struct {
62+
const swiftparse_client_node_t *nodes;
63+
uint32_t nodes_count;
64+
} swiftparse_layout_data_t;
65+
66+
typedef struct {
67+
union {
68+
swiftparse_token_data_t token_data;
69+
swiftparse_layout_data_t layout_data;
70+
};
71+
/// The syntax kind. A value of '0' means this is a token node.
72+
swiftparse_syntax_kind_t kind;
73+
bool present;
74+
} swiftparse_syntax_node_t;
75+
76+
#endif

include/swift-c/SyntaxParser/SwiftSyntaxParser.h

+1-46
Original file line numberDiff line numberDiff line change
@@ -75,52 +75,7 @@
7575

7676
SWIFTPARSE_BEGIN_DECLS
7777

78-
//=== Syntax Data Types ---------------------------------------------------===//
79-
80-
/// Offset+length in UTF8 bytes.
81-
typedef struct {
82-
uint32_t offset;
83-
uint32_t length;
84-
} swiftparse_range_t;
85-
86-
typedef uint8_t swiftparse_trivia_kind_t;
87-
typedef uint8_t swiftparse_token_kind_t;
88-
typedef uint16_t swiftparse_syntax_kind_t;
89-
90-
/// This is for the client to provide an opaque pointer that the parser will
91-
/// associate with a syntax node.
92-
typedef void *swiftparse_client_node_t;
93-
94-
typedef struct {
95-
/// The length in source this trivia piece occupies, in UTF8 bytes.
96-
uint32_t length;
97-
swiftparse_trivia_kind_t kind;
98-
} swiftparse_trivia_piece_t;
99-
100-
typedef struct {
101-
const swiftparse_trivia_piece_t *leading_trivia;
102-
const swiftparse_trivia_piece_t *trailing_trivia;
103-
uint16_t leading_trivia_count;
104-
uint16_t trailing_trivia_count;
105-
swiftparse_token_kind_t kind;
106-
/// Represents the range for the node, including trivia.
107-
swiftparse_range_t range;
108-
} swiftparse_token_data_t;
109-
110-
typedef struct {
111-
const swiftparse_client_node_t *nodes;
112-
uint32_t nodes_count;
113-
} swiftparse_layout_data_t;
114-
115-
typedef struct {
116-
union {
117-
swiftparse_token_data_t token_data;
118-
swiftparse_layout_data_t layout_data;
119-
};
120-
/// The syntax kind. A value of '0' means this is a token node.
121-
swiftparse_syntax_kind_t kind;
122-
bool present;
123-
} swiftparse_syntax_node_t;
78+
#include "SwiftSyntaxCDataTypes.h"
12479

12580
//=== Parser Functions ----------------------------------------------------===//
12681

0 commit comments

Comments
 (0)