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 // Return the assembler name to use for an exported function, a
19 // method, or a function/method declaration. This is not called if
20 // the function has been given an explicit name via a magic //extern
21 // or //go:linkname comment. GO_NAME is the name that appears in the
22 // Go code. PACKAGE is the package where the function is defined, and
23 // is NULL for the package being compiled. For a method, RTYPE is
24 // the method's receiver type; for a function, RTYPE is NULL.
27 Gogo::function_asm_name(const std::string
& go_name
, const Package
* package
,
30 std::string ret
= (package
== NULL
31 ? this->pkgpath_symbol()
32 : package
->pkgpath_symbol());
35 && Gogo::is_hidden_name(go_name
)
36 && Gogo::hidden_name_pkgpath(go_name
) != this->pkgpath())
38 // This is a method created for an unexported method of an
39 // imported embedded type. Use the pkgpath of the imported
41 std::string p
= Gogo::hidden_name_pkgpath(go_name
);
42 ret
= this->pkgpath_symbol_for_package(p
);
46 ret
.append(Gogo::unpack_hidden_name(go_name
));
51 ret
.append(rtype
->mangled_name(this));
54 return go_encode_id(ret
);
57 // Return the name to use for a function descriptor. These symbols
58 // are globally visible.
61 Gogo::function_descriptor_name(Named_object
* no
)
64 if (no
->is_function_declaration()
65 && !no
->func_declaration_value()->asm_name().empty()
66 && Linemap::is_predeclared_location(no
->location()))
68 if (no
->func_declaration_value()->asm_name().substr(0, 8) != "runtime.")
69 var_name
= no
->func_declaration_value()->asm_name() + "_descriptor";
71 var_name
= no
->func_declaration_value()->asm_name() + "$descriptor";
75 if (no
->package() == NULL
)
76 var_name
= this->pkgpath_symbol();
78 var_name
= no
->package()->pkgpath_symbol();
79 var_name
.push_back('.');
80 var_name
.append(Gogo::unpack_hidden_name(no
->name()));
81 var_name
.append("$descriptor");
86 // Return the name to use for a generated stub method. MNAME is the
87 // method name. These functions are globally visible. Note that this
88 // is the function name that corresponds to the name used for the
89 // method in Go source code, if this stub method were written in Go.
90 // The assembler name will be generated by Gogo::function_asm_name,
91 // and because this is a method that name will include the receiver
95 Gogo::stub_method_name(const std::string
& mname
)
97 return mname
+ "$stub";
100 // Return the names of the hash and equality functions for TYPE. If
101 // NAME is not NULL it is the name of the type. Set *HASH_NAME and
105 Gogo::specific_type_function_names(const Type
* type
, const Named_type
* name
,
106 std::string
*hash_name
,
107 std::string
*equal_name
)
109 std::string base_name
;
112 // Mangled names can have '.' if they happen to refer to named
113 // types in some way. That's fine if this is simply a named
114 // type, but otherwise it will confuse the code that builds
115 // function identifiers. Remove '.' when necessary.
116 base_name
= type
->mangled_name(this);
118 while ((i
= base_name
.find('.')) != std::string::npos
)
120 base_name
= this->pack_hidden_name(base_name
, false);
124 // This name is already hidden or not as appropriate.
125 base_name
= name
->name();
127 const Named_object
* in_function
= name
->in_function(&index
);
128 if (in_function
!= NULL
)
130 base_name
.append(1, '$');
131 const Typed_identifier
* rcvr
=
132 in_function
->func_value()->type()->receiver();
135 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
136 base_name
.append(Gogo::unpack_hidden_name(rcvr_type
->name()));
137 base_name
.append(1, '$');
139 base_name
.append(Gogo::unpack_hidden_name(in_function
->name()));
143 snprintf(buf
, sizeof buf
, "%u", index
);
149 *hash_name
= base_name
+ "$hash";
150 *equal_name
= base_name
+ "$equal";
153 // Return the assembler name to use for a global variable. GO_NAME is
154 // the name that appears in the Go code. PACKAGE is the package where
155 // the variable is defined, and is NULL for the package being
159 Gogo::global_var_asm_name(const std::string
& go_name
, const Package
* package
)
161 std::string ret
= (package
!= NULL
162 ? package
->pkgpath_symbol()
163 : this->pkgpath_symbol());
165 ret
.append(Gogo::unpack_hidden_name(go_name
));
166 return go_encode_id(ret
);
169 // Return an erroneous name that indicates that an error has already
173 Gogo::erroneous_name()
175 static int erroneous_count
;
177 snprintf(name
, sizeof name
, "$erroneous%d", erroneous_count
);
182 // Return whether a name is an erroneous name.
185 Gogo::is_erroneous_name(const std::string
& name
)
187 return name
.compare(0, 10, "$erroneous") == 0;
190 // Return a name for a thunk object.
195 static int thunk_count
;
197 snprintf(thunk_name
, sizeof thunk_name
, "$thunk%d", thunk_count
);
202 // Return whether a function is a thunk.
205 Gogo::is_thunk(const Named_object
* no
)
207 return no
->name().compare(0, 6, "$thunk") == 0;
210 // Return the name to use for an init function. There can be multiple
211 // functions named "init" so each one needs a different name.
214 Gogo::init_function_name()
216 static int init_count
;
218 snprintf(buf
, sizeof buf
, ".$init%d", init_count
);
223 // Return the name to use for a nested function.
226 Gogo::nested_function_name()
228 static int nested_count
;
230 snprintf(buf
, sizeof buf
, ".$nested%d", nested_count
);
235 // Return the name to use for a sink function, a function whose name
236 // is simply underscore. We don't really need these functions but we
237 // do have to generate them for error checking.
240 Gogo::sink_function_name()
242 static int sink_count
;
244 snprintf(buf
, sizeof buf
, ".$sink%d", sink_count
);
249 // Return the name to use for a redefined function. These functions
250 // are erroneous but we still generate them for further error
254 Gogo::redefined_function_name()
256 static int redefinition_count
;
258 snprintf(buf
, sizeof buf
, ".$redefined%d", redefinition_count
);
259 ++redefinition_count
;
263 // Return the name to use for a recover thunk for the function NAME.
264 // If the function is a method, RTYPE is the receiver type.
267 Gogo::recover_thunk_name(const std::string
& name
, const Type
* rtype
)
269 std::string
ret(name
);
273 ret
.append(rtype
->mangled_name(this));
275 ret
.append("$recover");
279 // Return the name to use for a GC root variable. The GC root
280 // variable is a composite literal that is passed to
281 // runtime.registerGCRoots. There is at most one of these variables
290 // Return the name to use for a composite literal or string
291 // initializer. This is a local name never referenced outside of this
295 Gogo::initializer_name()
297 static unsigned int counter
;
299 snprintf(buf
, sizeof buf
, "C%u", counter
);
304 // Return the name of the variable used to represent the zero value of
305 // a map. This is a globally visible common symbol.
308 Gogo::map_zero_value_name()
310 return "go$zerovalue";
313 // Return the name to use for the import control function.
316 Gogo::get_init_fn_name()
318 if (this->init_fn_name_
.empty())
320 go_assert(this->package_
!= NULL
);
321 if (this->is_main_package())
323 // Use a name that the runtime knows.
324 this->init_fn_name_
= "__go_init_main";
328 std::string s
= this->pkgpath_symbol();
329 s
.append("..import");
330 this->init_fn_name_
= s
;
334 return this->init_fn_name_
;
337 // Return a mangled name for a type. These names appear in symbol
338 // names in the assembler file for things like type descriptors and
342 Type::mangled_name(Gogo
* gogo
) const
346 // The do_mangled_name virtual function should set RET to the
347 // mangled name. For a composite type it should append a code for
348 // the composition and then call do_mangled_name on the components.
349 this->do_mangled_name(gogo
, &ret
);
354 // The mangled name is implemented as a method on each instance of
358 Error_type::do_mangled_name(Gogo
*, std::string
* ret
) const
364 Void_type::do_mangled_name(Gogo
*, std::string
* ret
) const
370 Boolean_type::do_mangled_name(Gogo
*, std::string
* ret
) const
376 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
) const
379 snprintf(buf
, sizeof buf
, "i%s%s%de",
380 this->is_abstract_
? "a" : "",
381 this->is_unsigned_
? "u" : "",
387 Float_type::do_mangled_name(Gogo
*, std::string
* ret
) const
390 snprintf(buf
, sizeof buf
, "f%s%de",
391 this->is_abstract_
? "a" : "",
397 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
) const
400 snprintf(buf
, sizeof buf
, "c%s%de",
401 this->is_abstract_
? "a" : "",
407 String_type::do_mangled_name(Gogo
*, std::string
* ret
) const
413 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
417 if (this->receiver_
!= NULL
)
420 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
);
423 const Typed_identifier_list
* params
= this->parameters();
427 for (Typed_identifier_list::const_iterator p
= params
->begin();
430 this->append_mangled_name(p
->type(), gogo
, ret
);
431 if (this->is_varargs_
)
436 const Typed_identifier_list
* results
= this->results();
440 for (Typed_identifier_list::const_iterator p
= results
->begin();
443 this->append_mangled_name(p
->type(), gogo
, ret
);
451 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
454 this->append_mangled_name(this->to_type_
, gogo
, ret
);
458 Nil_type::do_mangled_name(Gogo
*, std::string
* ret
) const
464 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
468 const Struct_field_list
* fields
= this->fields_
;
471 for (Struct_field_list::const_iterator p
= fields
->begin();
475 if (p
->is_anonymous())
480 std::string
n(Gogo::mangle_possibly_hidden_name(p
->field_name()));
482 snprintf(buf
, sizeof buf
, "%u_",
483 static_cast<unsigned int>(n
.length()));
488 // For an anonymous field with an alias type, the field name
489 // is the alias name.
490 if (p
->is_anonymous()
491 && p
->type()->named_type() != NULL
492 && p
->type()->named_type()->is_alias())
493 p
->type()->named_type()->append_mangled_type_name(gogo
, true, ret
);
495 this->append_mangled_name(p
->type(), gogo
, ret
);
498 const std::string
& tag(p
->tag());
500 for (std::string::const_iterator p
= tag
.begin();
504 if (ISALNUM(*p
) || *p
== '_')
509 snprintf(buf
, sizeof buf
, ".%x.",
510 static_cast<unsigned int>(*p
));
515 snprintf(buf
, sizeof buf
, "T%u_",
516 static_cast<unsigned int>(out
.length()));
523 if (this->is_struct_incomparable_
)
530 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
533 this->append_mangled_name(this->element_type_
, gogo
, ret
);
534 if (this->length_
!= NULL
)
537 if (!this->length_
->numeric_constant_value(&nc
))
539 go_assert(saw_errors());
543 if (!nc
.to_int(&val
))
545 go_assert(saw_errors());
548 char *s
= mpz_get_str(NULL
, 10, val
);
552 if (this->is_array_incomparable_
)
559 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
562 this->append_mangled_name(this->key_type_
, gogo
, ret
);
564 this->append_mangled_name(this->val_type_
, gogo
, ret
);
568 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
571 this->append_mangled_name(this->element_type_
, gogo
, ret
);
574 if (this->may_receive_
)
580 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
582 go_assert(this->methods_are_finalized_
);
586 const Typed_identifier_list
* methods
= this->all_methods_
;
587 if (methods
!= NULL
&& !this->seen_
)
590 for (Typed_identifier_list::const_iterator p
= methods
->begin();
594 if (!p
->name().empty())
596 std::string
n(Gogo::mangle_possibly_hidden_name(p
->name()));
598 snprintf(buf
, sizeof buf
, "%u_",
599 static_cast<unsigned int>(n
.length()));
603 this->append_mangled_name(p
->type(), gogo
, ret
);
612 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
614 this->append_mangled_type_name(gogo
, false, ret
);
618 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
620 if (this->is_defined())
621 this->append_mangled_name(this->real_type(), gogo
, ret
);
624 const Named_object
* no
= this->named_object();
626 if (no
->package() == NULL
)
627 name
= gogo
->pkgpath_symbol();
629 name
= no
->package()->pkgpath_symbol();
631 name
+= Gogo::unpack_hidden_name(no
->name());
633 snprintf(buf
, sizeof buf
, "N%u_",
634 static_cast<unsigned int>(name
.length()));
640 // Append the mangled name for a named type to RET. For an alias we
641 // normally use the real name, but if USE_ALIAS is true we use the
642 // alias name itself.
645 Named_type::append_mangled_type_name(Gogo
* gogo
, bool use_alias
,
646 std::string
* ret
) const
650 if (this->is_alias_
&& !use_alias
)
652 if (this->seen_alias_
)
654 this->seen_alias_
= true;
655 this->append_mangled_name(this->type_
, gogo
, ret
);
656 this->seen_alias_
= false;
659 Named_object
* no
= this->named_object_
;
661 if (this->is_builtin())
662 go_assert(this->in_function_
== NULL
);
665 const std::string
& pkgpath(no
->package() == NULL
666 ? gogo
->pkgpath_symbol()
667 : no
->package()->pkgpath_symbol());
670 if (this->in_function_
!= NULL
)
672 const Typed_identifier
* rcvr
=
673 this->in_function_
->func_value()->type()->receiver();
676 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
677 name
.append(Gogo::unpack_hidden_name(rcvr_type
->name()));
680 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
682 if (this->in_function_index_
> 0)
685 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
691 name
.append(Gogo::unpack_hidden_name(no
->name()));
693 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
698 // Return the name for the type descriptor symbol for TYPE. This can
699 // be a global, common, or local symbol, depending. NT is not NULL if
700 // it is the name to use.
703 Gogo::type_descriptor_name(Type
* type
, Named_type
* nt
)
705 // The type descriptor symbol for the unsafe.Pointer type is defined
706 // in libgo/runtime/go-unsafe-pointer.c, so just use a reference to
708 if (type
->is_unsafe_pointer_type())
709 return "__go_tdn_unsafe.Pointer";
712 return "__go_td_" + type
->mangled_name(this);
714 Named_object
* no
= nt
->named_object();
716 const Named_object
* in_function
= nt
->in_function(&index
);
717 std::string ret
= "__go_tdn_";
718 if (nt
->is_builtin())
719 go_assert(in_function
== NULL
);
722 const std::string
& pkgpath(no
->package() == NULL
723 ? this->pkgpath_symbol()
724 : no
->package()->pkgpath_symbol());
727 if (in_function
!= NULL
)
729 const Typed_identifier
* rcvr
=
730 in_function
->func_value()->type()->receiver();
733 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
734 ret
.append(Gogo::unpack_hidden_name(rcvr_type
->name()));
737 ret
.append(Gogo::unpack_hidden_name(in_function
->name()));
742 snprintf(buf
, sizeof buf
, "%u", index
);
749 std::string
mname(Gogo::mangle_possibly_hidden_name(no
->name()));
755 // Return the name for the GC symbol for a type. This is used to
756 // initialize the gcdata field of a type descriptor. This is a local
757 // name never referenced outside of this assembly file. (Note that
758 // some type descriptors will initialize the gcdata field with a name
759 // generated by ptrmask_symbol_name rather than this method.)
762 Gogo::gc_symbol_name(Type
* type
)
764 return this->type_descriptor_name(type
, type
->named_type()) + "$gc";
767 // Return the name for a ptrmask variable. PTRMASK_SYM_NAME is a
768 // base64 string encoding the ptrmask (as returned by Ptrmask::symname
769 // in types.cc). This name is used to intialize the gcdata field of a
770 // type descriptor. These names are globally visible. (Note that
771 // some type descriptors will initialize the gcdata field with a name
772 // generated by gc_symbol_name rather than this method.)
775 Gogo::ptrmask_symbol_name(const std::string
& ptrmask_sym_name
)
777 return "runtime.gcbits." + ptrmask_sym_name
;
780 // Return the name to use for an interface method table used for the
781 // ordinary type TYPE converted to the interface type ITYPE.
782 // IS_POINTER is true if this is for the method set for a pointer
786 Gogo::interface_method_table_name(Interface_type
* itype
, Type
* type
,
789 return ((is_pointer
? "__go_pimt__" : "__go_imt_")
790 + itype
->mangled_name(this)
792 + type
->mangled_name(this));