2323#include " absl/container/flat_hash_set.h"
2424#include " absl/log/absl_check.h"
2525#include " absl/status/status.h"
26+ #include " absl/status/statusor.h"
2627#include " absl/strings/str_cat.h"
2728#include " absl/strings/string_view.h"
29+ #include " common/internal/signature.h"
2830#include " common/type.h"
2931#include " common/type_kind.h"
3032
@@ -104,181 +106,6 @@ bool SignaturesOverlap(const OverloadDecl& lhs, const OverloadDecl& rhs) {
104106 return args_overlap;
105107}
106108
107- void AppendEscaped (std::string* result, absl::string_view str,
108- bool escape_dot) {
109- for (char c : str) {
110- switch (c) {
111- case ' \\ ' :
112- case ' (' :
113- case ' )' :
114- case ' <' :
115- case ' >' :
116- case ' "' :
117- case ' ,' :
118- result->push_back (' \\ ' );
119- result->push_back (c);
120- break ;
121- case ' .' :
122- if (escape_dot) {
123- result->push_back (' \\ ' );
124- }
125- result->push_back (c);
126- break ;
127- default :
128- result->push_back (c);
129- break ;
130- }
131- }
132- }
133-
134- void AppendTypeParameters (std::string* result, const Type& type);
135-
136- // Recursively appends a string representation of the given `type` to `result`.
137- // Type parameters are enclosed in angle brackets and separated by commas.
138- void AppendTypeToOverloadId (std::string* result, const Type& type) {
139- switch (type.kind ()) {
140- case TypeKind::kNull :
141- absl::StrAppend (result, " null" );
142- return ;
143- case TypeKind::kBool :
144- absl::StrAppend (result, " bool" );
145- return ;
146- case TypeKind::kInt :
147- absl::StrAppend (result, " int" );
148- return ;
149- case TypeKind::kUint :
150- absl::StrAppend (result, " uint" );
151- return ;
152- case TypeKind::kDouble :
153- absl::StrAppend (result, " double" );
154- return ;
155- case TypeKind::kString :
156- absl::StrAppend (result, " string" );
157- return ;
158- case TypeKind::kBytes :
159- absl::StrAppend (result, " bytes" );
160- return ;
161- case TypeKind::kDuration :
162- absl::StrAppend (result, " duration" );
163- return ;
164- case TypeKind::kTimestamp :
165- absl::StrAppend (result, " timestamp" );
166- return ;
167- case TypeKind::kUnknown :
168- absl::StrAppend (result, " unknown" );
169- return ;
170- case TypeKind::kError :
171- absl::StrAppend (result, " error" );
172- return ;
173- case TypeKind::kAny :
174- absl::StrAppend (result, " any" );
175- return ;
176- case TypeKind::kDyn :
177- absl::StrAppend (result, " dyn" );
178- return ;
179- case TypeKind::kBoolWrapper :
180- absl::StrAppend (result, " bool_wrapper" );
181- return ;
182- case TypeKind::kIntWrapper :
183- absl::StrAppend (result, " int_wrapper" );
184- return ;
185- case TypeKind::kUintWrapper :
186- absl::StrAppend (result, " uint_wrapper" );
187- return ;
188- case TypeKind::kDoubleWrapper :
189- absl::StrAppend (result, " double_wrapper" );
190- return ;
191- case TypeKind::kStringWrapper :
192- absl::StrAppend (result, " string_wrapper" );
193- return ;
194- case TypeKind::kBytesWrapper :
195- absl::StrAppend (result, " bytes_wrapper" );
196- return ;
197- case TypeKind::kList :
198- absl::StrAppend (result, " list" );
199- AppendTypeParameters (result, type);
200- return ;
201- case TypeKind::kMap :
202- absl::StrAppend (result, " map" );
203- AppendTypeParameters (result, type);
204- return ;
205- case TypeKind::kFunction :
206- absl::StrAppend (result, " function" );
207- AppendTypeParameters (result, type);
208- return ;
209- case TypeKind::kEnum :
210- absl::StrAppend (result, " enum" );
211- AppendTypeParameters (result, type);
212- return ;
213- case TypeKind::kType :
214- absl::StrAppend (result, " type" );
215- AppendTypeParameters (result, type);
216- return ;
217- case TypeKind::kOpaque :
218- result->push_back (' "' );
219- AppendEscaped (result, type.name (), /* escape_dot=*/ false );
220- result->push_back (' "' );
221- AppendTypeParameters (result, type);
222- return ;
223- default : // This includes TypeKind::kStruct aka TypeKind::kTypeMessage
224- AppendEscaped (result, type.name (), /* escape_dot=*/ false );
225- return ;
226- }
227- }
228-
229- void AppendTypeParameters (std::string* result, const Type& type) {
230- const auto & parameters = type.GetParameters ();
231- if (!parameters.empty ()) {
232- result->push_back (' <' );
233- for (size_t i = 0 ; i < parameters.size (); ++i) {
234- AppendTypeToOverloadId (result, parameters[i]);
235- if (i < parameters.size () - 1 ) {
236- result->push_back (' ,' );
237- }
238- }
239- result->push_back (' >' );
240- }
241- }
242-
243- // Generates an identifier for the overload based on the function name and
244- // the types of the arguments. If `member` is true, the first argument type
245- // is used as the receiver and is prepended to the function name, followed by
246- // a dot.
247- //
248- // Examples:
249- //
250- // - `foo()`
251- // - `foo(int)`
252- // - `bar.foo(int)`
253- // - `foo(int,string)`
254- // - `foo(list<int>,list<string>)`
255- // - `bar.foo(list<int>,list<"my_type"<A>>)`
256- //
257- std::string GenerateOverloadId (std::string_view function_name,
258- const std::vector<Type>& args, bool member) {
259- std::string result;
260- if (member) {
261- if (!args.empty ()) {
262- AppendTypeToOverloadId (&result, args[0 ]);
263- } else {
264- // This should never happen: a member function with no receiver.
265- absl::StrAppend (&result, " error" );
266- }
267- result.push_back (' .' );
268- }
269- AppendEscaped (&result, function_name, /* escape_dot=*/ true );
270- result.push_back (' (' );
271- for (size_t i = member ? 1 : 0 ; i < args.size (); ++i) {
272- AppendTypeToOverloadId (&result, args[i]);
273- if (i < args.size () - 1 ) {
274- result.push_back (' ,' );
275- }
276- }
277- result.push_back (' )' );
278-
279- return result;
280- }
281-
282109template <typename Overload>
283110void AddOverloadInternal (std::string_view function_name,
284111 std::vector<OverloadDecl>& insertion_order,
@@ -290,8 +117,14 @@ void AddOverloadInternal(std::string_view function_name,
290117
291118 if (overload.id ().empty ()) {
292119 OverloadDecl overload_decl = overload;
293- overload_decl.set_id (GenerateOverloadId (function_name, overload_decl.args (),
294- overload_decl.member ()));
120+ absl::StatusOr<std::string> overload_id =
121+ common_internal::MakeOverloadSignature (
122+ function_name, overload_decl.args (), overload_decl.member ());
123+ if (!overload_id.ok ()) {
124+ status = overload_id.status ();
125+ return ;
126+ }
127+ overload_decl.set_id (*overload_id);
295128 AddOverloadInternal (function_name, insertion_order, overloads,
296129 std::move (overload_decl), status);
297130 return ;
0 commit comments