Eric Salo | 0076500 | 2022-09-14 20:27:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009-2021, Google LLC |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of Google LLC nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT, |
| 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Eric Salo | 44916d7 | 2022-10-04 17:22:07 -0700 | [diff] [blame] | 28 | #ifndef UPB_REFLECTION_DEF_BUILDER_INTERNAL_H_ |
| 29 | #define UPB_REFLECTION_DEF_BUILDER_INTERNAL_H_ |
Eric Salo | 0076500 | 2022-09-14 20:27:24 -0700 | [diff] [blame] | 30 | |
| 31 | #include "upb/reflection/common.h" |
Eric Salo | 44916d7 | 2022-10-04 17:22:07 -0700 | [diff] [blame] | 32 | #include "upb/reflection/def_pool_internal.h" |
Eric Salo | 0076500 | 2022-09-14 20:27:24 -0700 | [diff] [blame] | 33 | #include "upb/reflection/def_type.h" |
| 34 | |
| 35 | // Must be last. |
Eric Salo | f630787 | 2022-11-05 16:16:27 -0700 | [diff] [blame] | 36 | #include "upb/port/def.inc" |
Eric Salo | 0076500 | 2022-09-14 20:27:24 -0700 | [diff] [blame] | 37 | |
| 38 | // We want to copy the options verbatim into the destination options proto. |
| 39 | // We use serialize+parse as our deep copy. |
Joshua Haberman | 204b9ee | 2023-01-06 17:39:28 -0800 | [diff] [blame] | 40 | #define UPB_DEF_SET_OPTIONS(target, desc_type, options_type, proto) \ |
| 41 | if (UPB_DESC(desc_type##_has_options)(proto)) { \ |
| 42 | size_t size; \ |
| 43 | char* pb = UPB_DESC(options_type##_serialize)( \ |
| 44 | UPB_DESC(desc_type##_options)(proto), ctx->tmp_arena, &size); \ |
| 45 | if (!pb) _upb_DefBuilder_OomErr(ctx); \ |
| 46 | target = \ |
| 47 | UPB_DESC(options_type##_parse)(pb, size, _upb_DefBuilder_Arena(ctx)); \ |
| 48 | if (!target) _upb_DefBuilder_OomErr(ctx); \ |
| 49 | } else { \ |
| 50 | target = (const UPB_DESC(options_type)*)kUpbDefOptDefault; \ |
Eric Salo | 0076500 | 2022-09-14 20:27:24 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | #ifdef __cplusplus |
| 54 | extern "C" { |
| 55 | #endif |
| 56 | |
| 57 | struct upb_DefBuilder { |
| 58 | upb_DefPool* symtab; |
| 59 | upb_FileDef* file; // File we are building. |
| 60 | upb_Arena* arena; // Allocate defs here. |
| 61 | upb_Arena* tmp_arena; // For temporary allocations. |
| 62 | upb_Status* status; // Record errors here. |
Eric Salo | 75907f7 | 2022-11-07 11:10:10 -0800 | [diff] [blame] | 63 | const upb_MiniTableFile* layout; // NULL if we should build layouts. |
Joshua Haberman | e41a2d7 | 2023-01-11 21:25:34 -0800 | [diff] [blame] | 64 | upb_MiniTablePlatform platform; // Platform we are targeting. |
Eric Salo | 0076500 | 2022-09-14 20:27:24 -0700 | [diff] [blame] | 65 | int enum_count; // Count of enums built so far. |
| 66 | int msg_count; // Count of messages built so far. |
| 67 | int ext_count; // Count of extensions built so far. |
| 68 | jmp_buf err; // longjmp() on error. |
| 69 | }; |
| 70 | |
| 71 | extern const char* kUpbDefOptDefault; |
| 72 | |
Eric Salo | efd06e4 | 2022-09-27 19:20:43 -0700 | [diff] [blame] | 73 | // ctx->status has already been set elsewhere so just fail/longjmp() |
| 74 | UPB_NORETURN void _upb_DefBuilder_FailJmp(upb_DefBuilder* ctx); |
| 75 | |
Eric Salo | 0076500 | 2022-09-14 20:27:24 -0700 | [diff] [blame] | 76 | UPB_NORETURN void _upb_DefBuilder_Errf(upb_DefBuilder* ctx, const char* fmt, |
| 77 | ...) UPB_PRINTF(2, 3); |
| 78 | UPB_NORETURN void _upb_DefBuilder_OomErr(upb_DefBuilder* ctx); |
| 79 | |
| 80 | const char* _upb_DefBuilder_MakeFullName(upb_DefBuilder* ctx, |
| 81 | const char* prefix, |
| 82 | upb_StringView name); |
| 83 | |
| 84 | // Given a symbol and the base symbol inside which it is defined, |
| 85 | // find the symbol's definition. |
| 86 | const void* _upb_DefBuilder_ResolveAny(upb_DefBuilder* ctx, |
| 87 | const char* from_name_dbg, |
| 88 | const char* base, upb_StringView sym, |
| 89 | upb_deftype_t* type); |
| 90 | |
| 91 | const void* _upb_DefBuilder_Resolve(upb_DefBuilder* ctx, |
| 92 | const char* from_name_dbg, const char* base, |
| 93 | upb_StringView sym, upb_deftype_t type); |
| 94 | |
| 95 | char _upb_DefBuilder_ParseEscape(upb_DefBuilder* ctx, const upb_FieldDef* f, |
| 96 | const char** src, const char* end); |
| 97 | |
| 98 | const char* _upb_DefBuilder_FullToShort(const char* fullname); |
| 99 | |
| 100 | UPB_INLINE void* _upb_DefBuilder_Alloc(upb_DefBuilder* ctx, size_t bytes) { |
| 101 | if (bytes == 0) return NULL; |
| 102 | void* ret = upb_Arena_Malloc(ctx->arena, bytes); |
| 103 | if (!ret) _upb_DefBuilder_OomErr(ctx); |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | // Adds a symbol |v| to the symtab, which must be a def pointer previously |
| 108 | // packed with pack_def(). The def's pointer to upb_FileDef* must be set before |
| 109 | // adding, so we know which entries to remove if building this file fails. |
| 110 | UPB_INLINE void _upb_DefBuilder_Add(upb_DefBuilder* ctx, const char* name, |
| 111 | upb_value v) { |
Eric Salo | efd06e4 | 2022-09-27 19:20:43 -0700 | [diff] [blame] | 112 | upb_StringView sym = {.data = name, .size = strlen(name)}; |
| 113 | bool ok = _upb_DefPool_InsertSym(ctx->symtab, sym, v, ctx->status); |
| 114 | if (!ok) _upb_DefBuilder_FailJmp(ctx); |
Eric Salo | 0076500 | 2022-09-14 20:27:24 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | UPB_INLINE upb_Arena* _upb_DefBuilder_Arena(const upb_DefBuilder* ctx) { |
| 118 | return ctx->arena; |
| 119 | } |
| 120 | |
| 121 | UPB_INLINE upb_FileDef* _upb_DefBuilder_File(const upb_DefBuilder* ctx) { |
| 122 | return ctx->file; |
| 123 | } |
| 124 | |
| 125 | // This version of CheckIdent() is only called by other, faster versions after |
| 126 | // they detect a parsing error. |
| 127 | void _upb_DefBuilder_CheckIdentSlow(upb_DefBuilder* ctx, upb_StringView name, |
| 128 | bool full); |
| 129 | |
Eric Salo | 0076500 | 2022-09-14 20:27:24 -0700 | [diff] [blame] | 130 | // Verify a full identifier string. This is slightly more complicated than |
| 131 | // verifying a relative identifier string because we must track '.' chars. |
| 132 | UPB_INLINE void _upb_DefBuilder_CheckIdentFull(upb_DefBuilder* ctx, |
| 133 | upb_StringView name) { |
| 134 | bool good = name.size > 0; |
| 135 | bool start = true; |
| 136 | |
| 137 | for (size_t i = 0; i < name.size; i++) { |
| 138 | const char c = name.data[i]; |
| 139 | const char d = c | 0x20; // force lowercase |
| 140 | const bool is_alpha = (('a' <= d) & (d <= 'z')) | (c == '_'); |
| 141 | const bool is_numer = ('0' <= c) & (c <= '9') & !start; |
| 142 | const bool is_dot = (c == '.') & !start; |
| 143 | |
| 144 | good &= is_alpha | is_numer | is_dot; |
| 145 | start = is_dot; |
| 146 | } |
| 147 | |
| 148 | if (!good) _upb_DefBuilder_CheckIdentSlow(ctx, name, true); |
| 149 | } |
| 150 | |
| 151 | #ifdef __cplusplus |
| 152 | } /* extern "C" */ |
| 153 | #endif |
| 154 | |
Eric Salo | f630787 | 2022-11-05 16:16:27 -0700 | [diff] [blame] | 155 | #include "upb/port/undef.inc" |
Eric Salo | 0076500 | 2022-09-14 20:27:24 -0700 | [diff] [blame] | 156 | |
Eric Salo | 44916d7 | 2022-10-04 17:22:07 -0700 | [diff] [blame] | 157 | #endif /* UPB_REFLECTION_DEF_BUILDER_INTERNAL_H_ */ |