1 // names.cc -- Names used by gofrontend generated code.
3 // Copyright 2017 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
10 #include "go-encode-id.h"
12 #include "expressions.h"
14 // This file contains functions that generate names that appear in the
15 // assembly code. This is not used for names that appear only in the
18 // Our external names may contain only ASCII alphanumeric characters,
19 // underscore, and dot. (According to the GCC sources, dot is not
20 // permitted in assembler symbols on VxWorks and MMIX. We will not
21 // support those systems.) Go identifiers cannot contain dot, but Go
22 // package paths can. Both Go identifiers and package paths can, of
23 // course, contain all sorts of Unicode characters.
25 // The gc compiler uses names like "pkg.F", and it seems convenient to
26 // emulate that. Therefore, we will use dot to separate different
27 // components of names.
29 // Since package paths can contain dot, to avoid ambiguity we must
30 // encode package paths such that they do not contain any dot. The
31 // natural way to do this is to encode forbidden characters, including
32 // dot, using a notation based on underscore. We will, of course,
33 // have to encode underscore itself.
35 // Since we will be using an underscore encoding for the package path,
36 // it seems reasonable to use the same encoding for Go identifiers.
37 // This has the disadvantage that encoded Go identifiers will appear
38 // to be valid Go identifiers with funny spellings, but it seems like
39 // the best available approach.
41 // Therefore, in the following discussion we may assume that none of
42 // the names under discussion contain a dot. All of the names we
43 // generate for Go identifiers (that don't use //export or
44 // //go:linkname) will contain at least one dot, as discussed below.
45 // We assume that none of the non-Go symbols in the final link will
46 // contain a dot, so we don't worry about conflicts.
48 // We first describe the basic symbol names, used to represent Go
49 // functions and variables.
51 // The external name for a normal Go symbol NAME, a function or
52 // variable, is simply "PKGPATH.NAME". Note that NAME is not the
53 // packed form used for the "hidden" name internally in the compiler;
54 // it is the name that appears in the source code. Both PKGPATH and
55 // NAME will be encoded as described below. The encoding process
56 // ensures that neither encoded string can contain a dot, and neither
57 // will start with a digit (NAME is a Go identifier that can't contain
58 // a dot or start with a digit anyhow). The encoding process means
59 // that these external names contain exactly one dot and do not start
62 // The external name for a method NAME for a named type TYPE is
63 // "PKGPATH.TYPE.NAME". Both NAME and TYPE are simple Go identifiers.
64 // Unlike the gc compiler, the external name does not indicate whether
65 // this is a pointer method or a value method; a named type can not
66 // have both a pointer and value method with the same name, so there
67 // is no ambiguity. PKGPATH is the package path of the package in
68 // which TYPE is defined. PKGPATH, TYPE, and NAME are encoded, and
69 // cannot be empty or contain a dot or start with a digit. These
70 // external names contain exactly two dots, not consecutive, and they
71 // do not start with a dot.
73 // It's uncommon, but the use of type literals with embedded fields
74 // can cause us to have methods on unnamed types. The external names
75 // for these are also PKGPATH.TYPELIT.NAME, where TYPELIT is an
76 // approximately readable version of the type literal, described
77 // below. A TYPELIT will always contain characters that cannot appear
78 // in a Go identifier, so TYPELIT can never be confused with a TYPE
79 // name. There is no ambiguity as long as encoded type literals are
82 // Also uncommon is an external name that must refer to a named type
83 // defined within a function. While such a type can not have methods
84 // itself, it can pick up embedded methods, and those methods need
85 // names. These are treated as a kind of type literal written as,
86 // before type literal encoding, FNNAME.TYPENAME(INDEX) or, for a
87 // method, TYPE.MNAME.TYPENAME(INDEX). INDEX is the index of that
88 // named type within the function, as a single function can have
89 // multiple types with the same name. This is unambiguous as
90 // parentheses can not appear in a type literal in this form (they can
91 // only appear in interface method declarations).
93 // That is the end of the list of basic names. The remaining names
94 // exist for special purposes, and are differentiated from the basic
95 // names by containing two consecutive dots.
97 // The hash function for a type is treated as a method whose name is
98 // ".hash". That is, the method name begins with a dot. The effect
99 // is that there will be two consecutive dots in the name; the name
100 // will always end with "..hash".
102 // Similarly the equality function for a type is treated as a method
103 // whose name is ".eq".
105 // The function descriptor for a function is the same as the name of
106 // the function with an added suffix "..f".
108 // A thunk for a go or defer statement is treated as a function whose
109 // name is ".thunkNN", unencoded, where NN is a sequence of digits
110 // (these functions are never globally visible). Thus the final name
111 // of a thunk will be PKGPATH..thunkNN (PKGPATH is encoded).
113 // An init function is treated as a function whose name is ".initNN",
114 // unencoded, where NN is a sequence of digits (these functions are
115 // never globally visible). Thus the final name of an init function
116 // will be PKGPATH..initNN (PKGPATH is encoded).
118 // A nested function is given the name of outermost enclosing function
119 // or method with an added suffix "..funcNN", unencoded, where NN is a
120 // sequence of digits. Note that the function descriptor of a nested
121 // function, if needed, will end with "..funcNN..f".
123 // A recover thunk is the same as the name of the function with an
124 // added suffix "..r".
126 // The name of a type descriptor for a named type is
127 // PKGPATH.TYPENAME..d (PKGPATH and TYPENAME are encoded).
129 // The name of a type descriptor for a pointer to a named type is
130 // PKGPATH.TYPENAME..p (PKGPATH and TYPENAME are encoded).
132 // The name of a type descriptor for an unnamed type is type..TYPELIT.
133 // That is, the string "type.." followed by the encoded type literal.
134 // These names are common symbols, in the linker's sense of the word
135 // common: in the final executable there is only one instance of the
136 // type descriptor for a given unnamed type.
138 // The name of the GC symbol for a named type is PKGPATH.TYPE..g
139 // (PKGPATH and TYPE are encoded).
141 // The name of the GC symbol for an unnamed type is type..TYPELIT..g.
142 // These are common symbols.
144 // The name of a ptrmask symbol is gcbits..B32 where B32 is an
145 // encoding of the ptrmask bits using only ASCII letters. These are
148 // An interface method table for assigning the non-interface type TYPE
149 // to the interface type ITYPE is named imt..ITYPE..TYPE. If ITYPE or
150 // TYPE is a named type, they are written as PKGPATH.TYPE (where both
151 // PKGPATH and TYPE are encoded). Otherwise they are written as a
152 // type literal. An interface method table for a pointer method set
153 // uses pimt instead of imt.
155 // The names of composite literal initializers, including the GC root
156 // variable, are not referenced. They must not conflict with any C
157 // language names, but the names are otherwise unimportant. They are
158 // named "go..CNN" where NN is a sequence of digits. The names do not
159 // include the PKGPATH.
161 // The map zero value, a common symbol that represents the zero value
162 // of a map, is named simply "go..zerovalue". The name does not
163 // include the PKGPATH.
165 // The import function for the main package is referenced by C code,
166 // and is named __go_init_main. For other packages it is
167 // PKGPATH..import. If a package doesn't need an init function, it
168 // will have a dummy one, named ~PKGPATH.
170 // In each package there is a list of all the type descriptors defined
171 // in this package. The name of the list is PKGPATH..types.
173 // In the main package it gathers all the type descriptor lists in a
174 // single list, named go..typelists.
176 // The type literal encoding is essentially a single line version of
177 // the type literal, such as "struct { pkgpath.i int; J int }". In
178 // this representation unexported names use their pkgpath, exported
181 // The type literal encoding is not quite valid Go, as some aspects of
182 // compiler generated types can not be represented. For example,
183 // incomparable struct types have an extra field "{x}". Struct tags
184 // can contain any character, which will be underscore encoded as
185 // usual. In the unusual case of a curly brace or a backslash in a
186 // struct tag, the brace or backslash will be backslash quoted, before
187 // underscore encoding.
189 // Many of these names will be visible in the debugger. The debugger
190 // will be given these names before applying any underscore encoding.
191 // These user names do not have to be unique--they are only used by
192 // the debugger, not the linker--so this is OK. However, there is an
193 // exception: if the name would otherwise include characters that
194 // can't normally appear in an identifier, then the user name will
195 // also be underscore encoded. This avoids problems with
196 // communicating the debug info to the assembler and with handling the
197 // debug info in the debugger. A Go-aware debugger will need to know
198 // whether to apply underscore decoding to a name before showing it to
199 // the user. We indicate this by adding a prefix of "g.", and
200 // assuming that cases of a package path of "g" are unusual. This
201 // prefix will only appear in the user name, not the assembler name.
203 // The underscore encoding is, naturally, an underscore followed by
204 // other characters. As there are various characters that commonly
205 // appear in type literals and in package paths, we have a set of
206 // short encodings. Then we have general encodings for other
224 // Other non-alphanumeric ASCII characters are encoded as _xNN, where
225 // NN is the hex value for the character. If an encoded name would
226 // otherwise start with a digit, this encoding is also used for the
229 // Non-ASCII Unicode characters are encoded as _u and four hex digits
230 // or _U and eight digits, just as in the language only using _u and
231 // _U instead of \u and \U.
233 // Demangling these names is straightforward:
234 // - replace _xXX with an ASCII character
235 // - replace _uXXXX with a unicode character
236 // - replace _UXXXXXXXX with a unicode character
237 // - replace _C per the table above
238 // That will get you as close as possible to a readable name.
240 // Set BNAME to the name to use for an exported function, a method, or
241 // a function/method declaration. GO_NAME is the name that appears in
242 // the Go code. PACKAGE is the package where the function is defined,
243 // and is NULL for the package being compiled. For a method, RTYPE is
244 // the method's receiver type; for a function, RTYPE is NULL.
247 Gogo::function_backend_name(const std::string
& go_name
,
248 const Package
* package
, const Type
* rtype
,
252 rtype
->deref()->backend_name(this, bname
);
253 else if (package
== NULL
)
254 bname
->add(this->pkgpath());
256 bname
->add(package
->pkgpath());
258 size_t pos
= Gogo::special_name_pos(go_name
);
259 if (pos
== std::string::npos
)
260 bname
->add(Gogo::unpack_hidden_name(go_name
));
264 bname
->add(go_name
.substr(0, pos
));
265 bname
->set_suffix(go_name
.substr(pos
));
269 // Set BNAME to the name to use for a function descriptor. These
270 // symbols are globally visible.
273 Gogo::function_descriptor_backend_name(Named_object
* no
,
276 if (no
->is_function())
277 no
->func_value()->backend_name(this, no
, bname
);
278 else if (no
->is_function_declaration())
279 no
->func_declaration_value()->backend_name(this, no
, bname
);
282 bname
->append_suffix("..f");
285 // Return the name to use for a generated stub method. A stub method
286 // is used as the method table entry for a promoted method of an
287 // embedded type. MNAME is the method name. PACKAGE is the package
288 // where the type that needs this stub method is defined. These
289 // functions are globally visible.
291 // This returns a name that acts like a Go identifier, as though the
292 // stub method were written in Go as an explicitly defined method that
293 // simply calls the promoted method. The name we return here will
294 // eventually be passed to function_backend_name, which will return a
295 // name that includes the receiver type.
297 // We construct a unique method name and append "..stub".
298 // function_backend_name will look for the "..stub" and turn that into
299 // an unencoded suffix. The rest of the name will be encoded as
303 Gogo::stub_method_name(const Package
* package
, const std::string
& mname
)
305 if (!Gogo::is_hidden_name(mname
))
306 return mname
+ "..stub";
308 const std::string
& ppkgpath(package
== NULL
310 : package
->pkgpath());
311 std::string mpkgpath
= Gogo::hidden_name_pkgpath(mname
);
312 if (mpkgpath
== ppkgpath
)
313 return Gogo::unpack_hidden_name(mname
) + "..stub";
315 // We are creating a stub method for an unexported method of an
316 // imported embedded type. A single type can have multiple promoted
317 // methods with the same unexported name, if it embeds types from
318 // different packages. We need to disambiguate the method name.
319 // This produces an unambiguous name because even though MPKGPATH
320 // can be anything, we know that MNAME does not contain a dot. The
321 // dot we return here, between MPKGPATH and MNAME, will wind up
322 // being underscore encoded.
323 std::string
ret(mpkgpath
);
325 ret
.append(Gogo::unpack_hidden_name(mname
));
326 ret
.append("..stub");
330 // Set BNAME to the name of the hash function for TYPE.
333 Gogo::hash_function_name(const Type
* type
, Backend_name
* bname
)
335 if (type
->named_type() != NULL
)
336 type
->backend_name(this, bname
);
339 bname
->add(this->pkgpath());
340 type
->backend_name(this, bname
);
342 bname
->set_suffix("..hash");
345 // Set BNAME to the name of the equal function for TYPE. If NAME is
346 // not NULL it is the name of the type.
349 Gogo::equal_function_name(const Type
* type
, const Named_type
* name
,
353 name
->backend_name(this, bname
);
356 bname
->add(this->pkgpath());
357 type
->backend_name(this, bname
);
359 bname
->set_suffix("..eq");
362 // Set BNAME to the name to use for a global variable. GO_NAME is the
363 // name that appears in the Go code. PACKAGE is the package where the
364 // variable is defined, and is NULL for the package being compiled.
367 Gogo::global_var_backend_name(const std::string
& go_name
,
368 const Package
* package
,
372 bname
->add(this->pkgpath());
374 bname
->add(package
->pkgpath());
375 bname
->add(Gogo::unpack_hidden_name(go_name
));
378 // Return an erroneous name that indicates that an error has already
379 // been reported. This name will act like a Go identifier.
382 Gogo::erroneous_name()
384 go_assert(saw_errors());
385 static int erroneous_count
;
387 snprintf(name
, sizeof name
, ".erroneous%d", erroneous_count
);
392 // Return whether a name is an erroneous name.
395 Gogo::is_erroneous_name(const std::string
& name
)
397 return name
.compare(0, 10, ".erroneous") == 0;
400 // Return a name for a thunk object. This name will act like a Go
401 // identifier. The name returned here will eventually be passed to
402 // function_backend_name, which will pull off the ..thunk as an
408 static int thunk_count
;
410 snprintf(thunk_name
, sizeof thunk_name
, "..thunk%d", thunk_count
);
412 // We don't want to return a name that starts with a dot, as that
413 // will confuse Gogo::is_hidden_name. And we don't want to change
414 // ..thunk, which fits our general theme and is used by code like
415 // runtime.Callers. But the prefix doesn't matter, as the actual
416 // name will include the package path.
417 std::string ret
= "go";
418 return ret
+ thunk_name
;
421 // Return whether a function is a thunk.
424 Gogo::is_thunk(const Named_object
* no
)
426 const std::string
& name(no
->name());
427 size_t i
= name
.rfind("..thunk");
428 if (i
== std::string::npos
)
430 return Gogo::is_digits(name
.substr(i
+ 7));
433 // Return the name to use for an init function. There can be multiple
434 // functions named "init" so each one needs a different name.
437 Gogo::init_function_name()
439 static int init_count
;
441 snprintf(buf
, sizeof buf
, "..init%d", init_count
);
443 return this->pkgpath() + buf
;
446 // Return the name to use for a nested function. This name acts like
447 // a Go identifier. This name will be rewritten by
448 // Function::backend_name.
451 Gogo::nested_function_name(Named_object
* enclosing
)
455 if (enclosing
== NULL
)
457 // A function literal at top level, as in
459 static unsigned int toplevel_index
;
461 index
= toplevel_index
;
468 Named_object
* parent
= enclosing
->func_value()->enclosing();
473 const Typed_identifier
* rcvr
=
474 enclosing
->func_value()->type()->receiver();
478 rcvr
->type()->backend_name(this, &bname
);
479 prefix
= bname
.name();
480 prefix
.push_back('.');
482 prefix
.append(Gogo::unpack_hidden_name(enclosing
->name()));
483 index
= enclosing
->func_value()->next_nested_function_index();
486 snprintf(buf
, sizeof buf
, "..func%u", index
);
490 // Return the name to use for a sink function, a function whose name
491 // is simply underscore. We don't really need these functions but we
492 // do have to generate them for error checking.
495 Gogo::sink_function_name()
497 static int sink_count
;
499 snprintf(buf
, sizeof buf
, ".sink%d", sink_count
);
504 // Return the name to use for a redefined function. These functions
505 // are erroneous but we still generate them for further error
509 Gogo::redefined_function_name()
511 static int redefinition_count
;
513 snprintf(buf
, sizeof buf
, ".redefined%d", redefinition_count
);
514 ++redefinition_count
;
518 // Return the name to use for a recover thunk for the function NAME.
519 // If the function is a method, RTYPE is the receiver type. This is a
520 // name that acts like a Go identifier.
523 Gogo::recover_thunk_name(const std::string
& name
, const Type
* rtype
)
529 rtype
->deref()->backend_name(this, &bname
);
533 if (Gogo::special_name_pos(name
) != std::string::npos
)
536 ret
.append(Gogo::unpack_hidden_name(name
));
541 // Return the name to use for a GC root variable. The GC root
542 // variable is a composite literal that is passed to
543 // runtime.registerGCRoots. There is at most one of these variables
552 // Return the name to use for a composite literal or string
553 // initializer. This is a local name never referenced outside of this
557 Gogo::initializer_name()
559 static unsigned int counter
;
562 snprintf(buf
, sizeof buf
, "go..C%u", counter
);
566 // Return the assembler name of the variable used to represent the
567 // zero value of a map. This is a globally visible common symbol.
570 Gogo::map_zero_value_name()
572 return "go..zerovalue";
575 // Return the name to use for the import control function. This name
576 // is handled specially by Function::backend_name. It is not encoded
580 Gogo::get_init_fn_name()
582 if (this->init_fn_name_
.empty())
584 go_assert(this->package_
!= NULL
);
585 if (this->is_main_package())
587 // Use a name that the runtime knows.
588 this->init_fn_name_
= "__go_init_main";
592 std::string s
= this->pkgpath_symbol();
593 s
.append("..import");
594 this->init_fn_name_
= s
;
598 return this->init_fn_name_
;
601 // Return the name for a dummy init function, which is not a real
602 // function but only for tracking transitive import.
605 Gogo::dummy_init_fn_name()
607 return "~" + this->pkgpath_symbol();
610 // Return the package path symbol from an init function name, which
611 // can be a real init function or a dummy one.
614 Gogo::pkgpath_symbol_from_init_fn_name(std::string name
)
616 go_assert(!name
.empty());
618 return name
.substr(1);
619 size_t pos
= name
.find("..import");
620 if (pos
!= std::string::npos
)
621 return name
.substr(0, pos
);
625 // Set BNAME to a name for a type to use in a symbol. Return a name
626 // for a type to use in a symbol. These names appear in symbol names
627 // in the assembler file for things like type descriptors and methods.
630 Type::backend_name(Gogo
* gogo
, Backend_name
* bname
) const
632 // Special case top level named types to get nicer name encodings
633 // for this common case.
634 const Named_type
* nt
= this->unalias()->named_type();
635 if (nt
!= NULL
&& !nt
->is_builtin())
638 if (nt
->in_function(&index
) == NULL
)
640 const Named_object
* no
= nt
->named_object();
641 if (no
->package() == NULL
)
642 bname
->add(gogo
->pkgpath());
644 bname
->add(no
->package()->pkgpath());
645 bname
->add(Gogo::unpack_hidden_name(no
->name()));
651 bool is_non_identifier
= false;
653 // The do_symbol_name virtual function will set RET to the mangled
654 // name before encoding.
655 this->do_mangled_name(gogo
, &name
, &is_non_identifier
);
658 if (is_non_identifier
)
659 bname
->set_is_non_identifier();
662 // The mangled name is implemented as a method on each instance of
666 Error_type::do_mangled_name(Gogo
*, std::string
* ret
,
667 bool* is_non_identifier
) const
669 ret
->append("{error}");
670 *is_non_identifier
= true;
674 Void_type::do_mangled_name(Gogo
*, std::string
* ret
,
675 bool* is_non_identifier
) const
677 ret
->append("{void}");
678 *is_non_identifier
= true;
682 Boolean_type::do_mangled_name(Gogo
*, std::string
* ret
, bool*) const
688 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
,
689 bool* is_non_identifier
) const
692 snprintf(buf
, sizeof buf
, "%s%si%d",
693 this->is_abstract_
? "{abstract}" : "",
694 this->is_unsigned_
? "u" : "",
697 if (this->is_abstract_
)
698 *is_non_identifier
= true;
702 Float_type::do_mangled_name(Gogo
*, std::string
* ret
,
703 bool* is_non_identifier
) const
706 snprintf(buf
, sizeof buf
, "%sfloat%d",
707 this->is_abstract_
? "{abstract}" : "",
710 if (this->is_abstract_
)
711 *is_non_identifier
= true;
715 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
,
716 bool* is_non_identifier
) const
719 snprintf(buf
, sizeof buf
, "%sc%d",
720 this->is_abstract_
? "{abstract}" : "",
723 if (this->is_abstract_
)
724 *is_non_identifier
= true;
728 String_type::do_mangled_name(Gogo
*, std::string
* ret
, bool*) const
730 ret
->append("string");
734 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
,
735 bool* is_non_identifier
) const
739 if (this->receiver_
!= NULL
)
742 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
,
748 const Typed_identifier_list
* params
= this->parameters();
752 for (Typed_identifier_list::const_iterator p
= params
->begin();
760 if (this->is_varargs_
&& p
+ 1 == params
->end())
762 this->append_mangled_name(p
->type(), gogo
, ret
,
769 const Typed_identifier_list
* results
= this->results();
773 for (Typed_identifier_list::const_iterator p
= results
->begin();
781 this->append_mangled_name(p
->type(), gogo
, ret
, is_non_identifier
);
786 *is_non_identifier
= true;
790 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
,
791 bool* is_non_identifier
) const
794 this->append_mangled_name(this->to_type_
, gogo
, ret
, is_non_identifier
);
795 *is_non_identifier
= true;
799 Nil_type::do_mangled_name(Gogo
*, std::string
* ret
,
800 bool* is_non_identifier
) const
802 ret
->append("{nil}");
803 *is_non_identifier
= true;
807 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
,
808 bool* is_non_identifier
) const
810 ret
->append("struct{");
812 if (this->is_struct_incomparable_
)
815 const Struct_field_list
* fields
= this->fields_
;
819 for (Struct_field_list::const_iterator p
= fields
->begin();
828 if (!p
->is_anonymous())
830 Gogo::append_possibly_hidden_name(ret
, p
->field_name());
834 const Type
* ft
= p
->type();
835 const Named_type
* nt
= ft
->named_type();
837 if (p
->is_anonymous() && nt
!= NULL
&& nt
->is_builtin())
839 // For an embedded field with a builtin type, we must
840 // include a package path. Otherwise embedding builtin
841 // types in different packages will produce identical
842 // types, which shouldn't happen because the builtin
843 // types are not exported.
844 ret
->append(gogo
->pkgpath());
846 nt
->append_symbol_type_name(gogo
, true, ret
, is_non_identifier
);
848 else if (p
->is_anonymous() && nt
!= NULL
&& nt
->is_alias())
850 // For an anonymous field with an alias type, the field name
851 // is the alias name.
852 nt
->append_symbol_type_name(gogo
, true, ret
, is_non_identifier
);
855 this->append_mangled_name(ft
, gogo
, ret
, is_non_identifier
);
859 // Use curly braces around a struct tag, since they are
860 // unambiguous here and struct tags rarely contain curly
863 ret
->append(go_mangle_struct_tag(p
->tag()));
871 *is_non_identifier
= true;
875 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
,
876 bool* is_non_identifier
) const
879 if (this->length_
!= NULL
)
882 if (!this->length_
->numeric_constant_value(&nc
))
884 go_assert(saw_errors());
888 if (!nc
.to_int(&val
))
890 go_assert(saw_errors());
893 char *s
= mpz_get_str(NULL
, 10, val
);
897 if (this->is_array_incomparable_
)
901 this->append_mangled_name(this->element_type_
, gogo
, ret
, is_non_identifier
);
902 *is_non_identifier
= true;
906 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
,
907 bool* is_non_identifier
) const
910 this->append_mangled_name(this->key_type_
, gogo
, ret
, is_non_identifier
);
912 this->append_mangled_name(this->val_type_
, gogo
, ret
, is_non_identifier
);
913 *is_non_identifier
= true;
917 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
,
918 bool* is_non_identifier
) const
920 if (!this->may_send_
)
923 if (!this->may_receive_
)
926 this->append_mangled_name(this->element_type_
, gogo
, ret
, is_non_identifier
);
927 *is_non_identifier
= true;
931 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
,
932 bool* is_non_identifier
) const
934 go_assert(this->methods_are_finalized_
);
936 ret
->append("interface{");
938 const Typed_identifier_list
* methods
= this->all_methods_
;
939 if (methods
!= NULL
&& !this->seen_
)
943 for (Typed_identifier_list::const_iterator p
= methods
->begin();
952 if (!p
->name().empty())
954 Gogo::append_possibly_hidden_name(ret
, p
->name());
958 this->append_mangled_name(p
->type(), gogo
, ret
, is_non_identifier
);
965 *is_non_identifier
= true;
969 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
,
970 bool* is_non_identifier
) const
972 this->append_symbol_type_name(gogo
, false, ret
, is_non_identifier
);
976 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
,
977 bool *is_non_identifier
) const
979 if (this->is_defined())
980 this->append_mangled_name(this->real_type(), gogo
, ret
, is_non_identifier
);
983 const Named_object
* no
= this->named_object();
984 if (no
->package() == NULL
)
985 ret
->append(gogo
->pkgpath());
987 ret
->append(no
->package()->pkgpath());
989 ret
->append(Gogo::unpack_hidden_name(no
->name()));
993 // Append the symbol name for a named type to RET. For an alias we
994 // normally use the real name, but if USE_ALIAS is true we use the
995 // alias name itself.
998 Named_type::append_symbol_type_name(Gogo
* gogo
, bool use_alias
,
1000 bool* is_non_identifier
) const
1002 if (this->is_error_
)
1004 if (this->is_alias_
&& !use_alias
)
1006 if (this->seen_alias_
)
1008 this->seen_alias_
= true;
1009 this->append_mangled_name(this->type_
, gogo
, ret
, is_non_identifier
);
1010 this->seen_alias_
= false;
1013 Named_object
* no
= this->named_object_
;
1015 if (this->is_builtin())
1016 go_assert(this->in_function_
== NULL
);
1019 if (this->in_function_
!= NULL
)
1021 const Typed_identifier
* rcvr
=
1022 this->in_function_
->func_value()->type()->receiver();
1026 rcvr
->type()->deref()->backend_name(gogo
, &bname
);
1027 ret
->append(bname
.name());
1028 if (bname
.is_non_identifier())
1029 *is_non_identifier
= true;
1031 else if (this->in_function_
->package() == NULL
)
1032 ret
->append(gogo
->pkgpath());
1034 ret
->append(this->in_function_
->package()->pkgpath());
1035 ret
->push_back('.');
1036 ret
->append(Gogo::unpack_hidden_name(this->in_function_
->name()));
1040 if (no
->package() == NULL
)
1041 ret
->append(gogo
->pkgpath());
1043 ret
->append(no
->package()->pkgpath());
1045 ret
->push_back('.');
1048 ret
->append(Gogo::unpack_hidden_name(no
->name()));
1050 if (this->in_function_
!= NULL
&& this->in_function_index_
> 0)
1053 snprintf(buf
, sizeof buf
, ".i%u", this->in_function_index_
);
1058 // Given a name which may or may not have been hidden, append the
1059 // appropriate version of the name to the result string.
1062 Gogo::append_possibly_hidden_name(std::string
*result
, const std::string
& name
)
1064 if (!Gogo::is_hidden_name(name
))
1067 *result
+= name
.substr(1);
1070 // Set BNAME to the name for the type descriptor symbol for TYPE.
1071 // This can be a global, common, or local symbol, depending. NT is
1072 // not NULL if it is the name to use.
1075 Gogo::type_descriptor_backend_name(const Type
* type
, Named_type
* nt
,
1076 Backend_name
* bname
)
1078 // The type descriptor symbol for the unsafe.Pointer type is defined
1079 // in libgo/runtime/go-unsafe-pointer.c, so just use a reference to
1080 // that symbol for all unsafe pointer types.
1081 if (type
->is_unsafe_pointer_type())
1083 bname
->set_asm_name("unsafe.Pointer..d");
1087 bool is_pointer
= false;
1088 if (nt
== NULL
&& type
->points_to() != NULL
)
1090 nt
= type
->points_to()->unalias()->named_type();
1096 // Sanity check: we should never generate a type descriptor for
1097 // an unnamed primitive type. For those we should always be
1098 // using a named type, like "int".
1099 go_assert(!type
->is_basic_type());
1101 type
->backend_name(this, bname
);
1102 bname
->set_prefix("type..");
1106 nt
->backend_name(this, bname
);
1107 bname
->set_suffix(is_pointer
? "..p" : "..d");
1111 // Return the name of the type descriptor list symbol of a package.
1112 // This is passed directly to the backend without further encoding.
1115 Gogo::type_descriptor_list_symbol(const std::string
& pkgpath_symbol
)
1117 return pkgpath_symbol
+ "..types";
1120 // Return the name of the list of all type descriptor lists. This is
1121 // only used in the main package. This is passed directly to the
1122 // backend without further encoding.
1125 Gogo::typelists_symbol()
1127 return "go..typelists";
1130 // Return the assembler name for the GC symbol for a type. This is
1131 // used to initialize the gcdata field of a type descriptor. This is
1132 // a local name never referenced outside of this assembly file. (Note
1133 // that some type descriptors will initialize the gcdata field with a
1134 // name generated by ptrmask_symbol_name rather than this method.)
1135 // This is passed directly to the backend without further encoding.
1138 Gogo::gc_symbol_name(Type
* type
)
1141 this->type_descriptor_backend_name(type
, type
->named_type(), &bname
);
1142 bname
.append_suffix("..g");
1143 return bname
.asm_name();
1146 // Return the assembler name for a ptrmask variable. PTRMASK_SYM_NAME
1147 // is a base32 string encoding the ptrmask (as returned by
1148 // Ptrmask::symname in types.cc). This name is used to intialize the
1149 // gcdata field of a type descriptor. These names are globally
1150 // visible. (Note that some type descriptors will initialize the
1151 // gcdata field with a name generated by gc_symbol_name rather than
1152 // this method.) This is passed directly to the backend without
1153 // further encoding.
1156 Gogo::ptrmask_symbol_name(const std::string
& ptrmask_sym_name
)
1158 return "gcbits.." + ptrmask_sym_name
;
1161 // Return the assembler name to use for an interface method table used
1162 // for the ordinary type TYPE converted to the interface type ITYPE.
1163 // IS_POINTER is true if this is for the method set for a pointer
1164 // receiver. This is passed directly to the backend without further
1168 Gogo::interface_method_table_name(Interface_type
* itype
, Type
* type
,
1172 itype
->backend_name(this, &iname
);
1174 type
->backend_name(this, &tname
);
1175 return ((is_pointer
? "pimt.." : "imt..")
1178 + tname
.asm_name());
1181 // If NAME is a special name with a ".." suffix, return the position
1182 // of that suffix. This is needed because various special names use
1183 // "..SUFFIX", but unpack_hidden_name just looks for '.', and because
1184 // we don't want to encode the suffix.
1187 Gogo::special_name_pos(const std::string
& name
)
1189 size_t pos
= name
.rfind("..");
1190 if (pos
== std::string::npos
)
1192 std::string
suffix(name
.substr(pos
));
1193 if (suffix
== "..hash"
1195 || suffix
== "..stub"
1199 || suffix
== "..import")
1201 if ((suffix
.compare(2, 4, "func") == 0
1202 || suffix
.compare(2, 4, "init") == 0)
1203 && Gogo::is_digits(suffix
.substr(6)))
1205 if (suffix
.compare(2, 5, "thunk") == 0
1206 && Gogo::is_digits(suffix
.substr(7)))
1208 return std::string::npos
;
1211 // Return whether the string is non-empty and contains only digits.
1214 Gogo::is_digits(const std::string
& s
)
1218 for (size_t i
= 0; i
< s
.size(); ++i
)
1219 if (s
[i
] < '0' || s
[i
] > '9')
1224 // Class Backend_name.
1226 // Get the user visible name.
1229 Backend_name::name() const
1231 if (this->is_asm_name_
)
1232 return this->components_
[0];
1234 // If there is some character in the name that can't appear in an
1235 // identifier, use the assembler name as the user name. This avoids
1236 // possible problems in the assembler or debugger. The usual
1237 // demangling scheme will still work. We use a prefix of "g." to
1238 // tell the debugger about this.
1239 if (this->is_non_identifier_
)
1240 return "g." + this->asm_name();
1243 if (this->prefix_
!= NULL
)
1244 ret
.append(this->prefix_
);
1245 for (int i
= 0; i
< this->count_
; i
++)
1249 ret
.append(this->components_
[i
]);
1251 if (!this->suffix_
.empty())
1252 ret
.append(this->suffix_
);
1256 // Get the assembler name.
1259 Backend_name::asm_name() const
1261 if (this->is_asm_name_
)
1262 return this->components_
[0];
1264 if (this->prefix_
!= NULL
)
1265 ret
.append(this->prefix_
);
1266 for (int i
= 0; i
< this->count_
; i
++)
1270 ret
.append(go_encode_id(this->components_
[i
]));
1272 if (!this->suffix_
.empty())
1273 ret
.append(this->suffix_
);
1277 // Get the assembler name, or the empty string if it is the same as
1278 // the user visible name.
1281 Backend_name::optional_asm_name() const
1283 if (this->is_asm_name_
)
1285 if (this->is_non_identifier_
)
1286 return this->asm_name();
1287 for (int i
= 0; i
< this->count_
; i
++)
1288 if (go_id_needs_encoding(this->components_
[i
]))
1289 return this->asm_name();