Skip to content

Commit 66aba1b

Browse files
committed
[ASTGen] Add Encoder/Decoder implementation backed by llvm::json::Value
Preparation for a future use.
1 parent 9f92220 commit 66aba1b

File tree

6 files changed

+1224
-0
lines changed

6 files changed

+1224
-0
lines changed

include/swift/Basic/CBasicBridging.h

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
//===--- CBasicBridging.h - header for the swift SILBridging module -------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2023 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+
#ifndef SWIFT_C_BASIC_BASICBRIDGING_H
14+
#define SWIFT_C_BASIC_BASICBRIDGING_H
15+
16+
#include "swift/Basic/Compiler.h"
17+
18+
#include <inttypes.h>
19+
#include <stdbool.h>
20+
#include <stddef.h>
21+
#include <stdint.h>
22+
23+
#if __clang__
24+
// Provide macros to temporarily suppress warning about the use of
25+
// _Nullable and _Nonnull.
26+
#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS \
27+
_Pragma("clang diagnostic push") \
28+
_Pragma("clang diagnostic ignored \"-Wnullability-extension\"") \
29+
_Pragma("clang assume_nonnull begin")
30+
31+
#define SWIFT_END_NULLABILITY_ANNOTATIONS \
32+
_Pragma("clang diagnostic pop") _Pragma("clang assume_nonnull end")
33+
#else
34+
#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
35+
#define SWIFT_END_NULLABILITY_ANNOTATIONS
36+
#define _Nullable
37+
#define _Nonnull
38+
#endif
39+
40+
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
41+
42+
#ifdef __cplusplus
43+
extern "C" {
44+
#endif
45+
46+
typedef struct BridgedData {
47+
const char *_Nullable baseAddress;
48+
size_t size;
49+
} BridgedData;
50+
51+
void BridgedData_free(BridgedData data);
52+
53+
//===----------------------------------------------------------------------===//
54+
// Plugins
55+
//===----------------------------------------------------------------------===//
56+
57+
/// Create a new root 'null' JSON value. Clients must call \c JSON_value_delete
58+
/// after using it.
59+
void *JSON_newValue();
60+
61+
/// Parse \p data as a JSON data and return the top-level value. Clients must
62+
/// call \c JSON_value_delete after using it.
63+
void *JSON_deserializedValue(BridgedData data);
64+
65+
/// Serialize a value and populate \p result with the result data. Clients
66+
/// must call \c BridgedData_free after using the \p result.
67+
void JSON_value_serialize(void *valuePtr, BridgedData *result);
68+
69+
/// Destroy and release the memory for \p valuePtr that is a result from
70+
/// \c JSON_newValue() or \c JSON_deserializedValue() .
71+
void JSON_value_delete(void *valuePtr);
72+
73+
bool JSON_value_getAsNull(void *valuePtr);
74+
bool JSON_value_getAsBoolean(void *valuePtr, bool *result);
75+
bool JSON_value_getAsString(void *valuePtr, BridgedData *result);
76+
bool JSON_value_getAsDouble(void *valuePtr, double *result);
77+
bool JSON_value_getAsInteger(void *valuePtr, int64_t *result);
78+
bool JSON_value_getAsObject(void *valuePtr, void *_Nullable *_Nonnull result);
79+
bool JSON_value_getAsArray(void *valuePtr, void *_Nullable *_Nonnull result);
80+
81+
size_t JSON_object_getSize(void *objectPtr);
82+
BridgedData JSON_object_getKey(void *objectPtr, size_t i);
83+
bool JSON_object_hasKey(void *objectPtr, const char *key);
84+
void *JSON_object_getValue(void *objectPtr, const char *key);
85+
86+
int64_t JSON_array_getSize(void *arrayPtr);
87+
void *JSON_array_getValue(void *arrayPtr, int64_t index);
88+
89+
void JSON_value_emplaceNull(void *valuePtr);
90+
void JSON_value_emplaceBoolean(void *valuePtr, bool value);
91+
void JSON_value_emplaceString(void *valuePtr, const char *value);
92+
void JSON_value_emplaceDouble(void *valuePtr, double value);
93+
void JSON_value_emplaceInteger(void *valuePtr, int64_t value);
94+
void *JSON_value_emplaceNewObject(void *valuePtr);
95+
void *JSON_value_emplaceNewArray(void *valuePtr);
96+
97+
void JSON_object_setNull(void *objectPtr, const char *key);
98+
void JSON_object_setBoolean(void *objectPtr, const char *key, bool value);
99+
void JSON_object_setString(void *objectPtr, const char *key, const char *value);
100+
void JSON_object_setDouble(void *objectPtr, const char *key, double value);
101+
void JSON_object_setInteger(void *objectPtr, const char *key, int64_t value);
102+
void *JSON_object_setNewObject(void *objectPtr, const char *key);
103+
void *JSON_object_setNewArray(void *objectPtr, const char *key);
104+
void *JSON_object_setNewValue(void *objectPtr, const char *key);
105+
106+
void JSON_array_pushNull(void *arrayPtr);
107+
void JSON_array_pushBoolean(void *arrayPtr, bool value);
108+
void JSON_array_pushString(void *arrayPtr, const char *value);
109+
void JSON_array_pushDouble(void *arrayPtr, double value);
110+
void JSON_array_pushInteger(void *arrayPtr, int64_t value);
111+
void *JSON_array_pushNewObject(void *arrayPtr);
112+
void *JSON_array_pushNewArray(void *arrayPtr);
113+
void *JSON_array_pushNewValue(void *arrayPtr);
114+
115+
#ifdef __cplusplus
116+
}
117+
#endif
118+
119+
SWIFT_END_NULLABILITY_ANNOTATIONS
120+
121+
#undef SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
122+
#undef SWIFT_END_NULLABILITY_ANNOTATIONS
123+
124+
#endif // SWIFT_C_BASIC_BASICBRIDGING_H

include/swift/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module BasicBridging {
66
export *
77
}
88

9+
module CBasicBridging {
10+
header "Basic/CBasicBridging.h"
11+
}
12+
913
module ASTBridging {
1014
header "AST/AnyFunctionRef.h"
1115
header "AST/ASTBridging.h"

lib/ASTGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ if (SWIFT_SWIFT_PARSER)
2525
Sources/ASTGen/Exprs.swift
2626
Sources/ASTGen/Generics.swift
2727
Sources/ASTGen/Literals.swift
28+
Sources/ASTGen/LLVMJSON.swift
2829
Sources/ASTGen/Macros.swift
2930
Sources/ASTGen/Misc.swift
3031
Sources/ASTGen/SourceFile.swift

0 commit comments

Comments
 (0)