1*9e3b08aeSAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2*9e3b08aeSAndroid Build Coastguard Worker // -*- mode: C++ -*-
3*9e3b08aeSAndroid Build Coastguard Worker //
4*9e3b08aeSAndroid Build Coastguard Worker // Copyright 2022-2024 Google LLC
5*9e3b08aeSAndroid Build Coastguard Worker //
6*9e3b08aeSAndroid Build Coastguard Worker // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7*9e3b08aeSAndroid Build Coastguard Worker // "License"); you may not use this file except in compliance with the
8*9e3b08aeSAndroid Build Coastguard Worker // License. You may obtain a copy of the License at
9*9e3b08aeSAndroid Build Coastguard Worker //
10*9e3b08aeSAndroid Build Coastguard Worker // https://llvm.org/LICENSE.txt
11*9e3b08aeSAndroid Build Coastguard Worker //
12*9e3b08aeSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
13*9e3b08aeSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
14*9e3b08aeSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15*9e3b08aeSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
16*9e3b08aeSAndroid Build Coastguard Worker // limitations under the License.
17*9e3b08aeSAndroid Build Coastguard Worker //
18*9e3b08aeSAndroid Build Coastguard Worker // Author: Siddharth Nayyar
19*9e3b08aeSAndroid Build Coastguard Worker
20*9e3b08aeSAndroid Build Coastguard Worker #include "proto_reader.h"
21*9e3b08aeSAndroid Build Coastguard Worker
22*9e3b08aeSAndroid Build Coastguard Worker #include <algorithm>
23*9e3b08aeSAndroid Build Coastguard Worker #include <array>
24*9e3b08aeSAndroid Build Coastguard Worker #include <cerrno>
25*9e3b08aeSAndroid Build Coastguard Worker #include <cstdint>
26*9e3b08aeSAndroid Build Coastguard Worker #include <fstream>
27*9e3b08aeSAndroid Build Coastguard Worker #include <limits>
28*9e3b08aeSAndroid Build Coastguard Worker #include <map>
29*9e3b08aeSAndroid Build Coastguard Worker #include <optional>
30*9e3b08aeSAndroid Build Coastguard Worker #include <string>
31*9e3b08aeSAndroid Build Coastguard Worker #include <string_view>
32*9e3b08aeSAndroid Build Coastguard Worker #include <vector>
33*9e3b08aeSAndroid Build Coastguard Worker
34*9e3b08aeSAndroid Build Coastguard Worker #include <google/protobuf/io/tokenizer.h>
35*9e3b08aeSAndroid Build Coastguard Worker #include <google/protobuf/io/zero_copy_stream.h>
36*9e3b08aeSAndroid Build Coastguard Worker #include <google/protobuf/io/zero_copy_stream_impl.h>
37*9e3b08aeSAndroid Build Coastguard Worker #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
38*9e3b08aeSAndroid Build Coastguard Worker #include <google/protobuf/repeated_field.h>
39*9e3b08aeSAndroid Build Coastguard Worker #include <google/protobuf/repeated_ptr_field.h>
40*9e3b08aeSAndroid Build Coastguard Worker #include <google/protobuf/text_format.h>
41*9e3b08aeSAndroid Build Coastguard Worker #include "error.h"
42*9e3b08aeSAndroid Build Coastguard Worker #include "graph.h"
43*9e3b08aeSAndroid Build Coastguard Worker #include "hex.h"
44*9e3b08aeSAndroid Build Coastguard Worker #include "runtime.h"
45*9e3b08aeSAndroid Build Coastguard Worker #include "stg.pb.h"
46*9e3b08aeSAndroid Build Coastguard Worker
47*9e3b08aeSAndroid Build Coastguard Worker namespace stg {
48*9e3b08aeSAndroid Build Coastguard Worker namespace proto {
49*9e3b08aeSAndroid Build Coastguard Worker
50*9e3b08aeSAndroid Build Coastguard Worker namespace {
51*9e3b08aeSAndroid Build Coastguard Worker
52*9e3b08aeSAndroid Build Coastguard Worker struct Transformer {
Transformerstg::proto::__anonc9b4b7de0111::Transformer53*9e3b08aeSAndroid Build Coastguard Worker explicit Transformer(Graph& graph) : graph(graph), maker(graph) {}
54*9e3b08aeSAndroid Build Coastguard Worker
55*9e3b08aeSAndroid Build Coastguard Worker Id Transform(const proto::STG&);
56*9e3b08aeSAndroid Build Coastguard Worker
57*9e3b08aeSAndroid Build Coastguard Worker Id GetId(uint32_t);
58*9e3b08aeSAndroid Build Coastguard Worker
59*9e3b08aeSAndroid Build Coastguard Worker template <typename ProtoType>
60*9e3b08aeSAndroid Build Coastguard Worker void AddNodes(const google::protobuf::RepeatedPtrField<ProtoType>&);
61*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Void&);
62*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Variadic&);
63*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Special&);
64*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const PointerReference&);
65*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const PointerToMember&);
66*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Typedef&);
67*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Qualified&);
68*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Primitive&);
69*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Array&);
70*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const BaseClass&);
71*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Method&);
72*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Member&);
73*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Variant&);
74*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const StructUnion&);
75*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Enumeration&);
76*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const VariantMember&);
77*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Function&);
78*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const ElfSymbol&);
79*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Symbols&);
80*9e3b08aeSAndroid Build Coastguard Worker void AddNode(const Interface&);
81*9e3b08aeSAndroid Build Coastguard Worker template <typename STGType, typename... Args>
82*9e3b08aeSAndroid Build Coastguard Worker void AddNode(uint32_t, Args&&...);
83*9e3b08aeSAndroid Build Coastguard Worker
84*9e3b08aeSAndroid Build Coastguard Worker std::vector<Id> Transform(const google::protobuf::RepeatedField<uint32_t>&);
85*9e3b08aeSAndroid Build Coastguard Worker template <typename GetKey>
86*9e3b08aeSAndroid Build Coastguard Worker std::map<std::string, Id> Transform(GetKey,
87*9e3b08aeSAndroid Build Coastguard Worker const google::protobuf::RepeatedField<uint32_t>&);
88*9e3b08aeSAndroid Build Coastguard Worker stg::Special::Kind Transform(Special::Kind);
89*9e3b08aeSAndroid Build Coastguard Worker stg::PointerReference::Kind Transform(PointerReference::Kind);
90*9e3b08aeSAndroid Build Coastguard Worker stg::Qualifier Transform(Qualified::Qualifier);
91*9e3b08aeSAndroid Build Coastguard Worker stg::Primitive::Encoding Transform(Primitive::Encoding);
92*9e3b08aeSAndroid Build Coastguard Worker stg::BaseClass::Inheritance Transform(BaseClass::Inheritance);
93*9e3b08aeSAndroid Build Coastguard Worker stg::StructUnion::Kind Transform(StructUnion::Kind);
94*9e3b08aeSAndroid Build Coastguard Worker stg::ElfSymbol::SymbolType Transform(ElfSymbol::SymbolType);
95*9e3b08aeSAndroid Build Coastguard Worker stg::ElfSymbol::Binding Transform(ElfSymbol::Binding);
96*9e3b08aeSAndroid Build Coastguard Worker stg::ElfSymbol::Visibility Transform(ElfSymbol::Visibility);
97*9e3b08aeSAndroid Build Coastguard Worker stg::Enumeration::Enumerators Transform(
98*9e3b08aeSAndroid Build Coastguard Worker const google::protobuf::RepeatedPtrField<Enumeration::Enumerator>&);
99*9e3b08aeSAndroid Build Coastguard Worker template <typename STGType, typename ProtoType>
100*9e3b08aeSAndroid Build Coastguard Worker std::optional<STGType> Transform(bool, const ProtoType&);
101*9e3b08aeSAndroid Build Coastguard Worker template <typename Type>
102*9e3b08aeSAndroid Build Coastguard Worker Type Transform(const Type&);
103*9e3b08aeSAndroid Build Coastguard Worker
104*9e3b08aeSAndroid Build Coastguard Worker Graph& graph;
105*9e3b08aeSAndroid Build Coastguard Worker Maker<Hex<uint32_t>> maker;
106*9e3b08aeSAndroid Build Coastguard Worker };
107*9e3b08aeSAndroid Build Coastguard Worker
Transform(const proto::STG & x)108*9e3b08aeSAndroid Build Coastguard Worker Id Transformer::Transform(const proto::STG& x) {
109*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.void_()); // deprecated
110*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.variadic()); // deprecated
111*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.special());
112*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.pointer_reference());
113*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.pointer_to_member());
114*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.typedef_());
115*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.qualified());
116*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.primitive());
117*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.array());
118*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.base_class());
119*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.method());
120*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.member());
121*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.variant_member());
122*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.struct_union());
123*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.enumeration());
124*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.variant());
125*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.function());
126*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.elf_symbol());
127*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.symbols());
128*9e3b08aeSAndroid Build Coastguard Worker AddNodes(x.interface());
129*9e3b08aeSAndroid Build Coastguard Worker return GetId(x.root_id());
130*9e3b08aeSAndroid Build Coastguard Worker }
131*9e3b08aeSAndroid Build Coastguard Worker
GetId(uint32_t id)132*9e3b08aeSAndroid Build Coastguard Worker Id Transformer::GetId(uint32_t id) {
133*9e3b08aeSAndroid Build Coastguard Worker return maker.Get(Hex(id));
134*9e3b08aeSAndroid Build Coastguard Worker }
135*9e3b08aeSAndroid Build Coastguard Worker
136*9e3b08aeSAndroid Build Coastguard Worker template <typename ProtoType>
AddNodes(const google::protobuf::RepeatedPtrField<ProtoType> & x)137*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNodes(const google::protobuf::RepeatedPtrField<ProtoType>& x) {
138*9e3b08aeSAndroid Build Coastguard Worker for (const ProtoType& proto : x) {
139*9e3b08aeSAndroid Build Coastguard Worker AddNode(proto);
140*9e3b08aeSAndroid Build Coastguard Worker }
141*9e3b08aeSAndroid Build Coastguard Worker }
142*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Void & x)143*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Void& x) {
144*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Special>(x.id(), stg::Special::Kind::VOID);
145*9e3b08aeSAndroid Build Coastguard Worker }
146*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Variadic & x)147*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Variadic& x) {
148*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Special>(x.id(), stg::Special::Kind::VARIADIC);
149*9e3b08aeSAndroid Build Coastguard Worker }
150*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Special & x)151*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Special& x) {
152*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Special>(x.id(), x.kind());
153*9e3b08aeSAndroid Build Coastguard Worker }
154*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const PointerReference & x)155*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const PointerReference& x) {
156*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::PointerReference>(x.id(), x.kind(), GetId(x.pointee_type_id()));
157*9e3b08aeSAndroid Build Coastguard Worker }
158*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const PointerToMember & x)159*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const PointerToMember& x) {
160*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::PointerToMember>(x.id(), GetId(x.containing_type_id()),
161*9e3b08aeSAndroid Build Coastguard Worker GetId(x.pointee_type_id()));
162*9e3b08aeSAndroid Build Coastguard Worker }
163*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Typedef & x)164*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Typedef& x) {
165*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Typedef>(x.id(), x.name(), GetId(x.referred_type_id()));
166*9e3b08aeSAndroid Build Coastguard Worker }
167*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Qualified & x)168*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Qualified& x) {
169*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Qualified>(x.id(), x.qualifier(), GetId(x.qualified_type_id()));
170*9e3b08aeSAndroid Build Coastguard Worker }
171*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Primitive & x)172*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Primitive& x) {
173*9e3b08aeSAndroid Build Coastguard Worker const auto& encoding =
174*9e3b08aeSAndroid Build Coastguard Worker Transform<stg::Primitive::Encoding>(x.has_encoding(), x.encoding());
175*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Primitive>(x.id(), x.name(), encoding, x.bytesize());
176*9e3b08aeSAndroid Build Coastguard Worker }
177*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Array & x)178*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Array& x) {
179*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Array>(x.id(), x.number_of_elements(),
180*9e3b08aeSAndroid Build Coastguard Worker GetId(x.element_type_id()));
181*9e3b08aeSAndroid Build Coastguard Worker }
182*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const BaseClass & x)183*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const BaseClass& x) {
184*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::BaseClass>(x.id(), GetId(x.type_id()), x.offset(),
185*9e3b08aeSAndroid Build Coastguard Worker x.inheritance());
186*9e3b08aeSAndroid Build Coastguard Worker }
187*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Method & x)188*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Method& x) {
189*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Method>(x.id(), x.mangled_name(), x.name(), x.vtable_offset(),
190*9e3b08aeSAndroid Build Coastguard Worker GetId(x.type_id()));
191*9e3b08aeSAndroid Build Coastguard Worker }
192*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Member & x)193*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Member& x) {
194*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Member>(x.id(), x.name(), GetId(x.type_id()), x.offset(),
195*9e3b08aeSAndroid Build Coastguard Worker x.bitsize());
196*9e3b08aeSAndroid Build Coastguard Worker }
197*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const VariantMember & x)198*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const VariantMember& x) {
199*9e3b08aeSAndroid Build Coastguard Worker const auto& discr_value = x.has_discriminant_value()
200*9e3b08aeSAndroid Build Coastguard Worker ? std::make_optional(x.discriminant_value())
201*9e3b08aeSAndroid Build Coastguard Worker : std::nullopt;
202*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::VariantMember>(x.id(), x.name(), discr_value,
203*9e3b08aeSAndroid Build Coastguard Worker GetId(x.type_id()));
204*9e3b08aeSAndroid Build Coastguard Worker }
205*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const StructUnion & x)206*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const StructUnion& x) {
207*9e3b08aeSAndroid Build Coastguard Worker if (x.has_definition()) {
208*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::StructUnion>(
209*9e3b08aeSAndroid Build Coastguard Worker x.id(), x.kind(), x.name(), x.definition().bytesize(),
210*9e3b08aeSAndroid Build Coastguard Worker x.definition().base_class_id(), x.definition().method_id(),
211*9e3b08aeSAndroid Build Coastguard Worker x.definition().member_id());
212*9e3b08aeSAndroid Build Coastguard Worker } else {
213*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::StructUnion>(x.id(), x.kind(), x.name());
214*9e3b08aeSAndroid Build Coastguard Worker }
215*9e3b08aeSAndroid Build Coastguard Worker }
216*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Enumeration & x)217*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Enumeration& x) {
218*9e3b08aeSAndroid Build Coastguard Worker if (x.has_definition()) {
219*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Enumeration>(x.id(), x.name(),
220*9e3b08aeSAndroid Build Coastguard Worker GetId(x.definition().underlying_type_id()),
221*9e3b08aeSAndroid Build Coastguard Worker x.definition().enumerator());
222*9e3b08aeSAndroid Build Coastguard Worker return;
223*9e3b08aeSAndroid Build Coastguard Worker } else {
224*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Enumeration>(x.id(), x.name());
225*9e3b08aeSAndroid Build Coastguard Worker }
226*9e3b08aeSAndroid Build Coastguard Worker }
227*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Variant & x)228*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Variant& x) {
229*9e3b08aeSAndroid Build Coastguard Worker const auto& discriminant = x.has_discriminant()
230*9e3b08aeSAndroid Build Coastguard Worker ? std::make_optional(GetId(x.discriminant()))
231*9e3b08aeSAndroid Build Coastguard Worker : std::nullopt;
232*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Variant>(x.id(), x.name(), x.bytesize(), discriminant,
233*9e3b08aeSAndroid Build Coastguard Worker x.member_id());
234*9e3b08aeSAndroid Build Coastguard Worker }
235*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Function & x)236*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Function& x) {
237*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Function>(x.id(), GetId(x.return_type_id()), x.parameter_id());
238*9e3b08aeSAndroid Build Coastguard Worker }
239*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const ElfSymbol & x)240*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const ElfSymbol& x) {
241*9e3b08aeSAndroid Build Coastguard Worker auto make_version_info = [](const ElfSymbol::VersionInfo& x) {
242*9e3b08aeSAndroid Build Coastguard Worker return std::make_optional(
243*9e3b08aeSAndroid Build Coastguard Worker stg::ElfSymbol::VersionInfo{x.is_default(), x.name()});
244*9e3b08aeSAndroid Build Coastguard Worker };
245*9e3b08aeSAndroid Build Coastguard Worker const std::optional<stg::ElfSymbol::VersionInfo> version_info =
246*9e3b08aeSAndroid Build Coastguard Worker x.has_version_info() ? make_version_info(x.version_info()) : std::nullopt;
247*9e3b08aeSAndroid Build Coastguard Worker const auto& crc = x.has_crc()
248*9e3b08aeSAndroid Build Coastguard Worker ? std::make_optional<stg::ElfSymbol::CRC>(x.crc())
249*9e3b08aeSAndroid Build Coastguard Worker : std::nullopt;
250*9e3b08aeSAndroid Build Coastguard Worker const auto& ns = Transform<std::string>(x.has_namespace_(), x.namespace_());
251*9e3b08aeSAndroid Build Coastguard Worker const auto& type_id =
252*9e3b08aeSAndroid Build Coastguard Worker x.has_type_id() ? std::make_optional(GetId(x.type_id())) : std::nullopt;
253*9e3b08aeSAndroid Build Coastguard Worker const auto& full_name =
254*9e3b08aeSAndroid Build Coastguard Worker Transform<std::string>(x.has_full_name(), x.full_name());
255*9e3b08aeSAndroid Build Coastguard Worker
256*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::ElfSymbol>(x.id(), x.name(), version_info, x.is_defined(),
257*9e3b08aeSAndroid Build Coastguard Worker x.symbol_type(), x.binding(), x.visibility(), crc, ns,
258*9e3b08aeSAndroid Build Coastguard Worker type_id, full_name);
259*9e3b08aeSAndroid Build Coastguard Worker }
260*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Symbols & x)261*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Symbols& x) {
262*9e3b08aeSAndroid Build Coastguard Worker std::map<std::string, Id> symbols;
263*9e3b08aeSAndroid Build Coastguard Worker for (const auto& [symbol, id] : x.symbol()) {
264*9e3b08aeSAndroid Build Coastguard Worker symbols.emplace(symbol, GetId(id));
265*9e3b08aeSAndroid Build Coastguard Worker }
266*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Interface>(x.id(), symbols);
267*9e3b08aeSAndroid Build Coastguard Worker }
268*9e3b08aeSAndroid Build Coastguard Worker
AddNode(const Interface & x)269*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(const Interface& x) {
270*9e3b08aeSAndroid Build Coastguard Worker const InterfaceKey get_key(graph);
271*9e3b08aeSAndroid Build Coastguard Worker AddNode<stg::Interface>(x.id(), Transform(get_key, x.symbol_id()),
272*9e3b08aeSAndroid Build Coastguard Worker Transform(get_key, x.type_id()));
273*9e3b08aeSAndroid Build Coastguard Worker }
274*9e3b08aeSAndroid Build Coastguard Worker
275*9e3b08aeSAndroid Build Coastguard Worker template <typename STGType, typename... Args>
AddNode(uint32_t id,Args &&...args)276*9e3b08aeSAndroid Build Coastguard Worker void Transformer::AddNode(uint32_t id, Args&&... args) {
277*9e3b08aeSAndroid Build Coastguard Worker maker.Set<STGType>(Hex(id), Transform(args)...);
278*9e3b08aeSAndroid Build Coastguard Worker }
279*9e3b08aeSAndroid Build Coastguard Worker
Transform(const google::protobuf::RepeatedField<uint32_t> & ids)280*9e3b08aeSAndroid Build Coastguard Worker std::vector<Id> Transformer::Transform(
281*9e3b08aeSAndroid Build Coastguard Worker const google::protobuf::RepeatedField<uint32_t>& ids) {
282*9e3b08aeSAndroid Build Coastguard Worker std::vector<Id> result;
283*9e3b08aeSAndroid Build Coastguard Worker result.reserve(ids.size());
284*9e3b08aeSAndroid Build Coastguard Worker for (const uint32_t id : ids) {
285*9e3b08aeSAndroid Build Coastguard Worker result.push_back(GetId(id));
286*9e3b08aeSAndroid Build Coastguard Worker }
287*9e3b08aeSAndroid Build Coastguard Worker return result;
288*9e3b08aeSAndroid Build Coastguard Worker }
289*9e3b08aeSAndroid Build Coastguard Worker
290*9e3b08aeSAndroid Build Coastguard Worker template <typename GetKey>
Transform(GetKey get_key,const google::protobuf::RepeatedField<uint32_t> & ids)291*9e3b08aeSAndroid Build Coastguard Worker std::map<std::string, Id> Transformer::Transform(
292*9e3b08aeSAndroid Build Coastguard Worker GetKey get_key, const google::protobuf::RepeatedField<uint32_t>& ids) {
293*9e3b08aeSAndroid Build Coastguard Worker std::map<std::string, Id> result;
294*9e3b08aeSAndroid Build Coastguard Worker for (auto id : ids) {
295*9e3b08aeSAndroid Build Coastguard Worker const Id stg_id = GetId(id);
296*9e3b08aeSAndroid Build Coastguard Worker const auto [it, inserted] = result.emplace(get_key(stg_id), stg_id);
297*9e3b08aeSAndroid Build Coastguard Worker if (!inserted) {
298*9e3b08aeSAndroid Build Coastguard Worker Die() << "conflicting interface nodes: " << it->first;
299*9e3b08aeSAndroid Build Coastguard Worker }
300*9e3b08aeSAndroid Build Coastguard Worker }
301*9e3b08aeSAndroid Build Coastguard Worker return result;
302*9e3b08aeSAndroid Build Coastguard Worker }
303*9e3b08aeSAndroid Build Coastguard Worker
Transform(Special::Kind x)304*9e3b08aeSAndroid Build Coastguard Worker stg::Special::Kind Transformer::Transform(Special::Kind x) {
305*9e3b08aeSAndroid Build Coastguard Worker switch (x) {
306*9e3b08aeSAndroid Build Coastguard Worker case Special::VOID:
307*9e3b08aeSAndroid Build Coastguard Worker return stg::Special::Kind::VOID;
308*9e3b08aeSAndroid Build Coastguard Worker case Special::VARIADIC:
309*9e3b08aeSAndroid Build Coastguard Worker return stg::Special::Kind::VARIADIC;
310*9e3b08aeSAndroid Build Coastguard Worker case Special::NULLPTR:
311*9e3b08aeSAndroid Build Coastguard Worker return stg::Special::Kind::NULLPTR;
312*9e3b08aeSAndroid Build Coastguard Worker default:
313*9e3b08aeSAndroid Build Coastguard Worker Die() << "unknown Special::Kind " << x;
314*9e3b08aeSAndroid Build Coastguard Worker }
315*9e3b08aeSAndroid Build Coastguard Worker }
316*9e3b08aeSAndroid Build Coastguard Worker
Transform(PointerReference::Kind x)317*9e3b08aeSAndroid Build Coastguard Worker stg::PointerReference::Kind Transformer::Transform(PointerReference::Kind x) {
318*9e3b08aeSAndroid Build Coastguard Worker switch (x) {
319*9e3b08aeSAndroid Build Coastguard Worker case PointerReference::POINTER:
320*9e3b08aeSAndroid Build Coastguard Worker return stg::PointerReference::Kind::POINTER;
321*9e3b08aeSAndroid Build Coastguard Worker case PointerReference::LVALUE_REFERENCE:
322*9e3b08aeSAndroid Build Coastguard Worker return stg::PointerReference::Kind::LVALUE_REFERENCE;
323*9e3b08aeSAndroid Build Coastguard Worker case PointerReference::RVALUE_REFERENCE:
324*9e3b08aeSAndroid Build Coastguard Worker return stg::PointerReference::Kind::RVALUE_REFERENCE;
325*9e3b08aeSAndroid Build Coastguard Worker default:
326*9e3b08aeSAndroid Build Coastguard Worker Die() << "unknown PointerReference::Kind " << x;
327*9e3b08aeSAndroid Build Coastguard Worker }
328*9e3b08aeSAndroid Build Coastguard Worker }
329*9e3b08aeSAndroid Build Coastguard Worker
Transform(Qualified::Qualifier x)330*9e3b08aeSAndroid Build Coastguard Worker stg::Qualifier Transformer::Transform(Qualified::Qualifier x) {
331*9e3b08aeSAndroid Build Coastguard Worker switch (x) {
332*9e3b08aeSAndroid Build Coastguard Worker case Qualified::CONST:
333*9e3b08aeSAndroid Build Coastguard Worker return stg::Qualifier::CONST;
334*9e3b08aeSAndroid Build Coastguard Worker case Qualified::VOLATILE:
335*9e3b08aeSAndroid Build Coastguard Worker return stg::Qualifier::VOLATILE;
336*9e3b08aeSAndroid Build Coastguard Worker case Qualified::RESTRICT:
337*9e3b08aeSAndroid Build Coastguard Worker return stg::Qualifier::RESTRICT;
338*9e3b08aeSAndroid Build Coastguard Worker case Qualified::ATOMIC:
339*9e3b08aeSAndroid Build Coastguard Worker return stg::Qualifier::ATOMIC;
340*9e3b08aeSAndroid Build Coastguard Worker default:
341*9e3b08aeSAndroid Build Coastguard Worker Die() << "unknown Qualified::Qualifier " << x;
342*9e3b08aeSAndroid Build Coastguard Worker }
343*9e3b08aeSAndroid Build Coastguard Worker }
344*9e3b08aeSAndroid Build Coastguard Worker
Transform(Primitive::Encoding x)345*9e3b08aeSAndroid Build Coastguard Worker stg::Primitive::Encoding Transformer::Transform(Primitive::Encoding x) {
346*9e3b08aeSAndroid Build Coastguard Worker switch (x) {
347*9e3b08aeSAndroid Build Coastguard Worker case Primitive::BOOLEAN:
348*9e3b08aeSAndroid Build Coastguard Worker return stg::Primitive::Encoding::BOOLEAN;
349*9e3b08aeSAndroid Build Coastguard Worker case Primitive::SIGNED_INTEGER:
350*9e3b08aeSAndroid Build Coastguard Worker return stg::Primitive::Encoding::SIGNED_INTEGER;
351*9e3b08aeSAndroid Build Coastguard Worker case Primitive::UNSIGNED_INTEGER:
352*9e3b08aeSAndroid Build Coastguard Worker return stg::Primitive::Encoding::UNSIGNED_INTEGER;
353*9e3b08aeSAndroid Build Coastguard Worker case Primitive::SIGNED_CHARACTER:
354*9e3b08aeSAndroid Build Coastguard Worker return stg::Primitive::Encoding::SIGNED_CHARACTER;
355*9e3b08aeSAndroid Build Coastguard Worker case Primitive::UNSIGNED_CHARACTER:
356*9e3b08aeSAndroid Build Coastguard Worker return stg::Primitive::Encoding::UNSIGNED_CHARACTER;
357*9e3b08aeSAndroid Build Coastguard Worker case Primitive::REAL_NUMBER:
358*9e3b08aeSAndroid Build Coastguard Worker return stg::Primitive::Encoding::REAL_NUMBER;
359*9e3b08aeSAndroid Build Coastguard Worker case Primitive::COMPLEX_NUMBER:
360*9e3b08aeSAndroid Build Coastguard Worker return stg::Primitive::Encoding::COMPLEX_NUMBER;
361*9e3b08aeSAndroid Build Coastguard Worker case Primitive::UTF:
362*9e3b08aeSAndroid Build Coastguard Worker return stg::Primitive::Encoding::UTF;
363*9e3b08aeSAndroid Build Coastguard Worker default:
364*9e3b08aeSAndroid Build Coastguard Worker Die() << "unknown Primitive::Encoding " << x;
365*9e3b08aeSAndroid Build Coastguard Worker }
366*9e3b08aeSAndroid Build Coastguard Worker }
367*9e3b08aeSAndroid Build Coastguard Worker
Transform(BaseClass::Inheritance x)368*9e3b08aeSAndroid Build Coastguard Worker stg::BaseClass::Inheritance Transformer::Transform(BaseClass::Inheritance x) {
369*9e3b08aeSAndroid Build Coastguard Worker switch (x) {
370*9e3b08aeSAndroid Build Coastguard Worker case BaseClass::NON_VIRTUAL:
371*9e3b08aeSAndroid Build Coastguard Worker return stg::BaseClass::Inheritance::NON_VIRTUAL;
372*9e3b08aeSAndroid Build Coastguard Worker case BaseClass::VIRTUAL:
373*9e3b08aeSAndroid Build Coastguard Worker return stg::BaseClass::Inheritance::VIRTUAL;
374*9e3b08aeSAndroid Build Coastguard Worker default:
375*9e3b08aeSAndroid Build Coastguard Worker Die() << "unknown BaseClass::Inheritance " << x;
376*9e3b08aeSAndroid Build Coastguard Worker }
377*9e3b08aeSAndroid Build Coastguard Worker }
378*9e3b08aeSAndroid Build Coastguard Worker
Transform(StructUnion::Kind x)379*9e3b08aeSAndroid Build Coastguard Worker stg::StructUnion::Kind Transformer::Transform(StructUnion::Kind x) {
380*9e3b08aeSAndroid Build Coastguard Worker switch (x) {
381*9e3b08aeSAndroid Build Coastguard Worker case StructUnion::STRUCT:
382*9e3b08aeSAndroid Build Coastguard Worker return stg::StructUnion::Kind::STRUCT;
383*9e3b08aeSAndroid Build Coastguard Worker case StructUnion::UNION:
384*9e3b08aeSAndroid Build Coastguard Worker return stg::StructUnion::Kind::UNION;
385*9e3b08aeSAndroid Build Coastguard Worker default:
386*9e3b08aeSAndroid Build Coastguard Worker Die() << "unknown StructUnion::Kind " << x;
387*9e3b08aeSAndroid Build Coastguard Worker }
388*9e3b08aeSAndroid Build Coastguard Worker }
389*9e3b08aeSAndroid Build Coastguard Worker
Transform(ElfSymbol::SymbolType x)390*9e3b08aeSAndroid Build Coastguard Worker stg::ElfSymbol::SymbolType Transformer::Transform(ElfSymbol::SymbolType x) {
391*9e3b08aeSAndroid Build Coastguard Worker switch (x) {
392*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::NOTYPE:
393*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::SymbolType::NOTYPE;
394*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::OBJECT:
395*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::SymbolType::OBJECT;
396*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::FUNCTION:
397*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::SymbolType::FUNCTION;
398*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::COMMON:
399*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::SymbolType::COMMON;
400*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::TLS:
401*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::SymbolType::TLS;
402*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::GNU_IFUNC:
403*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::SymbolType::GNU_IFUNC;
404*9e3b08aeSAndroid Build Coastguard Worker default:
405*9e3b08aeSAndroid Build Coastguard Worker Die() << "unknown ElfSymbol::SymbolType " << x;
406*9e3b08aeSAndroid Build Coastguard Worker }
407*9e3b08aeSAndroid Build Coastguard Worker }
408*9e3b08aeSAndroid Build Coastguard Worker
Transform(ElfSymbol::Binding x)409*9e3b08aeSAndroid Build Coastguard Worker stg::ElfSymbol::Binding Transformer::Transform(ElfSymbol::Binding x) {
410*9e3b08aeSAndroid Build Coastguard Worker switch (x) {
411*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::GLOBAL:
412*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::Binding::GLOBAL;
413*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::LOCAL:
414*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::Binding::LOCAL;
415*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::WEAK:
416*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::Binding::WEAK;
417*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::GNU_UNIQUE:
418*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::Binding::GNU_UNIQUE;
419*9e3b08aeSAndroid Build Coastguard Worker default:
420*9e3b08aeSAndroid Build Coastguard Worker Die() << "unknown ElfSymbol::Binding " << x;
421*9e3b08aeSAndroid Build Coastguard Worker }
422*9e3b08aeSAndroid Build Coastguard Worker }
423*9e3b08aeSAndroid Build Coastguard Worker
Transform(ElfSymbol::Visibility x)424*9e3b08aeSAndroid Build Coastguard Worker stg::ElfSymbol::Visibility Transformer::Transform(ElfSymbol::Visibility x) {
425*9e3b08aeSAndroid Build Coastguard Worker switch (x) {
426*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::DEFAULT:
427*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::Visibility::DEFAULT;
428*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::PROTECTED:
429*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::Visibility::PROTECTED;
430*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::HIDDEN:
431*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::Visibility::HIDDEN;
432*9e3b08aeSAndroid Build Coastguard Worker case ElfSymbol::INTERNAL:
433*9e3b08aeSAndroid Build Coastguard Worker return stg::ElfSymbol::Visibility::INTERNAL;
434*9e3b08aeSAndroid Build Coastguard Worker default:
435*9e3b08aeSAndroid Build Coastguard Worker Die() << "unknown ElfSymbol::Visibility " << x;
436*9e3b08aeSAndroid Build Coastguard Worker }
437*9e3b08aeSAndroid Build Coastguard Worker }
438*9e3b08aeSAndroid Build Coastguard Worker
Transform(const google::protobuf::RepeatedPtrField<Enumeration::Enumerator> & x)439*9e3b08aeSAndroid Build Coastguard Worker stg::Enumeration::Enumerators Transformer::Transform(
440*9e3b08aeSAndroid Build Coastguard Worker const google::protobuf::RepeatedPtrField<Enumeration::Enumerator>& x) {
441*9e3b08aeSAndroid Build Coastguard Worker stg::Enumeration::Enumerators enumerators;
442*9e3b08aeSAndroid Build Coastguard Worker enumerators.reserve(x.size());
443*9e3b08aeSAndroid Build Coastguard Worker for (const auto& enumerator : x) {
444*9e3b08aeSAndroid Build Coastguard Worker enumerators.emplace_back(enumerator.name(), enumerator.value());
445*9e3b08aeSAndroid Build Coastguard Worker }
446*9e3b08aeSAndroid Build Coastguard Worker return enumerators;
447*9e3b08aeSAndroid Build Coastguard Worker }
448*9e3b08aeSAndroid Build Coastguard Worker
449*9e3b08aeSAndroid Build Coastguard Worker template <typename STGType, typename ProtoType>
Transform(bool has_field,const ProtoType & field)450*9e3b08aeSAndroid Build Coastguard Worker std::optional<STGType> Transformer::Transform(bool has_field,
451*9e3b08aeSAndroid Build Coastguard Worker const ProtoType& field) {
452*9e3b08aeSAndroid Build Coastguard Worker return has_field ? std::make_optional<STGType>(Transform(field))
453*9e3b08aeSAndroid Build Coastguard Worker : std::nullopt;
454*9e3b08aeSAndroid Build Coastguard Worker }
455*9e3b08aeSAndroid Build Coastguard Worker
456*9e3b08aeSAndroid Build Coastguard Worker template <typename Type>
Transform(const Type & x)457*9e3b08aeSAndroid Build Coastguard Worker Type Transformer::Transform(const Type& x) {
458*9e3b08aeSAndroid Build Coastguard Worker return x;
459*9e3b08aeSAndroid Build Coastguard Worker }
460*9e3b08aeSAndroid Build Coastguard Worker
461*9e3b08aeSAndroid Build Coastguard Worker const std::array<uint32_t, 3> kSupportedFormatVersions = {0, 1, 2};
462*9e3b08aeSAndroid Build Coastguard Worker
CheckFormatVersion(uint32_t version)463*9e3b08aeSAndroid Build Coastguard Worker void CheckFormatVersion(uint32_t version) {
464*9e3b08aeSAndroid Build Coastguard Worker Check(std::binary_search(kSupportedFormatVersions.begin(),
465*9e3b08aeSAndroid Build Coastguard Worker kSupportedFormatVersions.end(), version))
466*9e3b08aeSAndroid Build Coastguard Worker << "STG format version " << version
467*9e3b08aeSAndroid Build Coastguard Worker << " is not supported, minimum supported version: "
468*9e3b08aeSAndroid Build Coastguard Worker << kSupportedFormatVersions.front();
469*9e3b08aeSAndroid Build Coastguard Worker if (version != kSupportedFormatVersions.back()) {
470*9e3b08aeSAndroid Build Coastguard Worker Warn() << "STG format version " << version
471*9e3b08aeSAndroid Build Coastguard Worker << " is deprecated, consider upgrading to the latest version ("
472*9e3b08aeSAndroid Build Coastguard Worker << kSupportedFormatVersions.back() << ")";
473*9e3b08aeSAndroid Build Coastguard Worker }
474*9e3b08aeSAndroid Build Coastguard Worker }
475*9e3b08aeSAndroid Build Coastguard Worker
476*9e3b08aeSAndroid Build Coastguard Worker class ErrorSink : public google::protobuf::io::ErrorCollector {
477*9e3b08aeSAndroid Build Coastguard Worker public:
AddError(int line,google::protobuf::io::ColumnNumber column,const std::string & message)478*9e3b08aeSAndroid Build Coastguard Worker void AddError(int line, google::protobuf::io::ColumnNumber column,
479*9e3b08aeSAndroid Build Coastguard Worker const std::string& message) final {
480*9e3b08aeSAndroid Build Coastguard Worker Moan("error", line, column, message);
481*9e3b08aeSAndroid Build Coastguard Worker }
AddWarning(int line,google::protobuf::io::ColumnNumber column,const std::string & message)482*9e3b08aeSAndroid Build Coastguard Worker void AddWarning(int line, google::protobuf::io::ColumnNumber column,
483*9e3b08aeSAndroid Build Coastguard Worker const std::string& message) final {
484*9e3b08aeSAndroid Build Coastguard Worker Moan("warning", line, column, message);
485*9e3b08aeSAndroid Build Coastguard Worker }
486*9e3b08aeSAndroid Build Coastguard Worker
487*9e3b08aeSAndroid Build Coastguard Worker private:
Moan(std::string_view which,int line,google::protobuf::io::ColumnNumber column,const std::string & message)488*9e3b08aeSAndroid Build Coastguard Worker static void Moan(std::string_view which, int line,
489*9e3b08aeSAndroid Build Coastguard Worker google::protobuf::io::ColumnNumber column,
490*9e3b08aeSAndroid Build Coastguard Worker const std::string& message) {
491*9e3b08aeSAndroid Build Coastguard Worker Warn() << "google::protobuf::TextFormat " << which << " at line " << (line + 1)
492*9e3b08aeSAndroid Build Coastguard Worker << " column " << (column + 1) << ": " << message;
493*9e3b08aeSAndroid Build Coastguard Worker }
494*9e3b08aeSAndroid Build Coastguard Worker };
495*9e3b08aeSAndroid Build Coastguard Worker
ReadHelper(Runtime & runtime,Graph & graph,google::protobuf::io::ZeroCopyInputStream & is)496*9e3b08aeSAndroid Build Coastguard Worker Id ReadHelper(Runtime& runtime, Graph& graph,
497*9e3b08aeSAndroid Build Coastguard Worker google::protobuf::io::ZeroCopyInputStream& is) {
498*9e3b08aeSAndroid Build Coastguard Worker proto::STG stg;
499*9e3b08aeSAndroid Build Coastguard Worker {
500*9e3b08aeSAndroid Build Coastguard Worker const Time t(runtime, "proto.Parse");
501*9e3b08aeSAndroid Build Coastguard Worker ErrorSink error_sink;
502*9e3b08aeSAndroid Build Coastguard Worker google::protobuf::TextFormat::Parser parser;
503*9e3b08aeSAndroid Build Coastguard Worker parser.RecordErrorsTo(&error_sink);
504*9e3b08aeSAndroid Build Coastguard Worker Check(parser.Parse(&is, &stg)) << "failed to parse input as STG";
505*9e3b08aeSAndroid Build Coastguard Worker }
506*9e3b08aeSAndroid Build Coastguard Worker {
507*9e3b08aeSAndroid Build Coastguard Worker const Time t(runtime, "proto.Transform");
508*9e3b08aeSAndroid Build Coastguard Worker CheckFormatVersion(stg.version());
509*9e3b08aeSAndroid Build Coastguard Worker return Transformer(graph).Transform(stg);
510*9e3b08aeSAndroid Build Coastguard Worker }
511*9e3b08aeSAndroid Build Coastguard Worker }
512*9e3b08aeSAndroid Build Coastguard Worker
513*9e3b08aeSAndroid Build Coastguard Worker } // namespace
514*9e3b08aeSAndroid Build Coastguard Worker
Read(Runtime & runtime,Graph & graph,const std::string & path)515*9e3b08aeSAndroid Build Coastguard Worker Id Read(Runtime& runtime, Graph& graph, const std::string& path) {
516*9e3b08aeSAndroid Build Coastguard Worker std::ifstream ifs(path);
517*9e3b08aeSAndroid Build Coastguard Worker Check(ifs.good()) << "error opening file '" << path << "' for reading: "
518*9e3b08aeSAndroid Build Coastguard Worker << Error(errno);
519*9e3b08aeSAndroid Build Coastguard Worker google::protobuf::io::IstreamInputStream is(&ifs);
520*9e3b08aeSAndroid Build Coastguard Worker return ReadHelper(runtime, graph, is);
521*9e3b08aeSAndroid Build Coastguard Worker }
522*9e3b08aeSAndroid Build Coastguard Worker
ReadFromString(Runtime & runtime,Graph & graph,std::string_view input)523*9e3b08aeSAndroid Build Coastguard Worker Id ReadFromString(Runtime& runtime, Graph& graph, std::string_view input) {
524*9e3b08aeSAndroid Build Coastguard Worker Check(input.size() <= std::numeric_limits<int>::max()) << "input too big";
525*9e3b08aeSAndroid Build Coastguard Worker google::protobuf::io::ArrayInputStream is(input.data(), static_cast<int>(input.size()));
526*9e3b08aeSAndroid Build Coastguard Worker return ReadHelper(runtime, graph, is);
527*9e3b08aeSAndroid Build Coastguard Worker }
528*9e3b08aeSAndroid Build Coastguard Worker
529*9e3b08aeSAndroid Build Coastguard Worker } // namespace proto
530*9e3b08aeSAndroid Build Coastguard Worker } // namespace stg
531