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 index of a nested function name.
238 Gogo::nested_function_num(const std::string
& name
)
240 std::string
n(Gogo::unpack_hidden_name(name
));
241 go_assert(n
.compare(0, 7, "$nested") == 0);
242 return strtol(n
.substr(7).c_str(), NULL
, 0);
245 // Return the name to use for a sink function, a function whose name
246 // is simply underscore. We don't really need these functions but we
247 // do have to generate them for error checking.
250 Gogo::sink_function_name()
252 static int sink_count
;
254 snprintf(buf
, sizeof buf
, ".$sink%d", sink_count
);
259 // Return the name to use for a redefined function. These functions
260 // are erroneous but we still generate them for further error
264 Gogo::redefined_function_name()
266 static int redefinition_count
;
268 snprintf(buf
, sizeof buf
, ".$redefined%d", redefinition_count
);
269 ++redefinition_count
;
273 // Return the name to use for a recover thunk for the function NAME.
274 // If the function is a method, RTYPE is the receiver type.
277 Gogo::recover_thunk_name(const std::string
& name
, const Type
* rtype
)
279 std::string
ret(name
);
283 ret
.append(rtype
->mangled_name(this));
285 ret
.append("$recover");
289 // Return the name to use for a GC root variable. The GC root
290 // variable is a composite literal that is passed to
291 // runtime.registerGCRoots. There is at most one of these variables
300 // Return the name to use for a composite literal or string
301 // initializer. This is a local name never referenced outside of this
305 Gogo::initializer_name()
307 static unsigned int counter
;
309 snprintf(buf
, sizeof buf
, "C%u", counter
);
314 // Return the name of the variable used to represent the zero value of
315 // a map. This is a globally visible common symbol.
318 Gogo::map_zero_value_name()
320 return "go$zerovalue";
323 // Return the name to use for the import control function.
326 Gogo::get_init_fn_name()
328 if (this->init_fn_name_
.empty())
330 go_assert(this->package_
!= NULL
);
331 if (this->is_main_package())
333 // Use a name that the runtime knows.
334 this->init_fn_name_
= "__go_init_main";
338 std::string s
= this->pkgpath_symbol();
339 s
.append("..import");
340 this->init_fn_name_
= s
;
344 return this->init_fn_name_
;
347 // Return a mangled name for a type. These names appear in symbol
348 // names in the assembler file for things like type descriptors and
352 Type::mangled_name(Gogo
* gogo
) const
356 // The do_mangled_name virtual function should set RET to the
357 // mangled name. For a composite type it should append a code for
358 // the composition and then call do_mangled_name on the components.
359 this->do_mangled_name(gogo
, &ret
);
364 // The mangled name is implemented as a method on each instance of
368 Error_type::do_mangled_name(Gogo
*, std::string
* ret
) const
374 Void_type::do_mangled_name(Gogo
*, std::string
* ret
) const
380 Boolean_type::do_mangled_name(Gogo
*, std::string
* ret
) const
386 Integer_type::do_mangled_name(Gogo
*, std::string
* ret
) const
389 snprintf(buf
, sizeof buf
, "i%s%s%de",
390 this->is_abstract_
? "a" : "",
391 this->is_unsigned_
? "u" : "",
397 Float_type::do_mangled_name(Gogo
*, std::string
* ret
) const
400 snprintf(buf
, sizeof buf
, "f%s%de",
401 this->is_abstract_
? "a" : "",
407 Complex_type::do_mangled_name(Gogo
*, std::string
* ret
) const
410 snprintf(buf
, sizeof buf
, "c%s%de",
411 this->is_abstract_
? "a" : "",
417 String_type::do_mangled_name(Gogo
*, std::string
* ret
) const
423 Function_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
427 if (this->receiver_
!= NULL
)
430 this->append_mangled_name(this->receiver_
->type(), gogo
, ret
);
433 const Typed_identifier_list
* params
= this->parameters();
437 for (Typed_identifier_list::const_iterator p
= params
->begin();
440 this->append_mangled_name(p
->type(), gogo
, ret
);
441 if (this->is_varargs_
)
446 const Typed_identifier_list
* results
= this->results();
450 for (Typed_identifier_list::const_iterator p
= results
->begin();
453 this->append_mangled_name(p
->type(), gogo
, ret
);
461 Pointer_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
464 this->append_mangled_name(this->to_type_
, gogo
, ret
);
468 Nil_type::do_mangled_name(Gogo
*, std::string
* ret
) const
474 Struct_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
478 const Struct_field_list
* fields
= this->fields_
;
481 for (Struct_field_list::const_iterator p
= fields
->begin();
485 if (p
->is_anonymous())
490 std::string
n(Gogo::mangle_possibly_hidden_name(p
->field_name()));
492 snprintf(buf
, sizeof buf
, "%u_",
493 static_cast<unsigned int>(n
.length()));
498 // For an anonymous field with an alias type, the field name
499 // is the alias name.
500 if (p
->is_anonymous()
501 && p
->type()->named_type() != NULL
502 && p
->type()->named_type()->is_alias())
503 p
->type()->named_type()->append_mangled_type_name(gogo
, true, ret
);
505 this->append_mangled_name(p
->type(), gogo
, ret
);
508 const std::string
& tag(p
->tag());
510 for (std::string::const_iterator p
= tag
.begin();
514 if (ISALNUM(*p
) || *p
== '_')
519 snprintf(buf
, sizeof buf
, ".%x.",
520 static_cast<unsigned int>(*p
));
525 snprintf(buf
, sizeof buf
, "T%u_",
526 static_cast<unsigned int>(out
.length()));
533 if (this->is_struct_incomparable_
)
540 Array_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
543 this->append_mangled_name(this->element_type_
, gogo
, ret
);
544 if (this->length_
!= NULL
)
547 if (!this->length_
->numeric_constant_value(&nc
))
549 go_assert(saw_errors());
553 if (!nc
.to_int(&val
))
555 go_assert(saw_errors());
558 char *s
= mpz_get_str(NULL
, 10, val
);
562 if (this->is_array_incomparable_
)
569 Map_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
572 this->append_mangled_name(this->key_type_
, gogo
, ret
);
574 this->append_mangled_name(this->val_type_
, gogo
, ret
);
578 Channel_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
581 this->append_mangled_name(this->element_type_
, gogo
, ret
);
584 if (this->may_receive_
)
590 Interface_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
592 go_assert(this->methods_are_finalized_
);
596 const Typed_identifier_list
* methods
= this->all_methods_
;
597 if (methods
!= NULL
&& !this->seen_
)
600 for (Typed_identifier_list::const_iterator p
= methods
->begin();
604 if (!p
->name().empty())
606 std::string
n(Gogo::mangle_possibly_hidden_name(p
->name()));
608 snprintf(buf
, sizeof buf
, "%u_",
609 static_cast<unsigned int>(n
.length()));
613 this->append_mangled_name(p
->type(), gogo
, ret
);
622 Named_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
624 this->append_mangled_type_name(gogo
, false, ret
);
628 Forward_declaration_type::do_mangled_name(Gogo
* gogo
, std::string
* ret
) const
630 if (this->is_defined())
631 this->append_mangled_name(this->real_type(), gogo
, ret
);
634 const Named_object
* no
= this->named_object();
636 if (no
->package() == NULL
)
637 name
= gogo
->pkgpath_symbol();
639 name
= no
->package()->pkgpath_symbol();
641 name
+= Gogo::unpack_hidden_name(no
->name());
643 snprintf(buf
, sizeof buf
, "N%u_",
644 static_cast<unsigned int>(name
.length()));
650 // Append the mangled name for a named type to RET. For an alias we
651 // normally use the real name, but if USE_ALIAS is true we use the
652 // alias name itself.
655 Named_type::append_mangled_type_name(Gogo
* gogo
, bool use_alias
,
656 std::string
* ret
) const
660 if (this->is_alias_
&& !use_alias
)
662 if (this->seen_alias_
)
664 this->seen_alias_
= true;
665 this->append_mangled_name(this->type_
, gogo
, ret
);
666 this->seen_alias_
= false;
669 Named_object
* no
= this->named_object_
;
671 if (this->is_builtin())
672 go_assert(this->in_function_
== NULL
);
675 const std::string
& pkgpath(no
->package() == NULL
676 ? gogo
->pkgpath_symbol()
677 : no
->package()->pkgpath_symbol());
680 if (this->in_function_
!= NULL
)
682 const Typed_identifier
* rcvr
=
683 this->in_function_
->func_value()->type()->receiver();
686 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
687 name
.append(Gogo::unpack_hidden_name(rcvr_type
->name()));
690 name
.append(Gogo::unpack_hidden_name(this->in_function_
->name()));
692 if (this->in_function_index_
> 0)
695 snprintf(buf
, sizeof buf
, "%u", this->in_function_index_
);
701 name
.append(Gogo::unpack_hidden_name(no
->name()));
703 snprintf(buf
, sizeof buf
, "N%u_", static_cast<unsigned int>(name
.length()));
708 // Return the name for the type descriptor symbol for TYPE. This can
709 // be a global, common, or local symbol, depending. NT is not NULL if
710 // it is the name to use.
713 Gogo::type_descriptor_name(Type
* type
, Named_type
* nt
)
715 // The type descriptor symbol for the unsafe.Pointer type is defined
716 // in libgo/runtime/go-unsafe-pointer.c, so just use a reference to
718 if (type
->is_unsafe_pointer_type())
719 return "__go_tdn_unsafe.Pointer";
722 return "__go_td_" + type
->mangled_name(this);
724 Named_object
* no
= nt
->named_object();
726 const Named_object
* in_function
= nt
->in_function(&index
);
727 std::string ret
= "__go_tdn_";
728 if (nt
->is_builtin())
729 go_assert(in_function
== NULL
);
732 const std::string
& pkgpath(no
->package() == NULL
733 ? this->pkgpath_symbol()
734 : no
->package()->pkgpath_symbol());
737 if (in_function
!= NULL
)
739 const Typed_identifier
* rcvr
=
740 in_function
->func_value()->type()->receiver();
743 Named_type
* rcvr_type
= rcvr
->type()->deref()->named_type();
744 ret
.append(Gogo::unpack_hidden_name(rcvr_type
->name()));
747 ret
.append(Gogo::unpack_hidden_name(in_function
->name()));
752 snprintf(buf
, sizeof buf
, "%u", index
);
759 std::string
mname(Gogo::mangle_possibly_hidden_name(no
->name()));
765 // Return the name for the GC symbol for a type. This is used to
766 // initialize the gcdata field of a type descriptor. This is a local
767 // name never referenced outside of this assembly file. (Note that
768 // some type descriptors will initialize the gcdata field with a name
769 // generated by ptrmask_symbol_name rather than this method.)
772 Gogo::gc_symbol_name(Type
* type
)
774 return this->type_descriptor_name(type
, type
->named_type()) + "$gc";
777 // Return the name for a ptrmask variable. PTRMASK_SYM_NAME is a
778 // base64 string encoding the ptrmask (as returned by Ptrmask::symname
779 // in types.cc). This name is used to intialize the gcdata field of a
780 // type descriptor. These names are globally visible. (Note that
781 // some type descriptors will initialize the gcdata field with a name
782 // generated by gc_symbol_name rather than this method.)
785 Gogo::ptrmask_symbol_name(const std::string
& ptrmask_sym_name
)
787 return "runtime.gcbits." + ptrmask_sym_name
;
790 // Return the name to use for an interface method table used for the
791 // ordinary type TYPE converted to the interface type ITYPE.
792 // IS_POINTER is true if this is for the method set for a pointer
796 Gogo::interface_method_table_name(Interface_type
* itype
, Type
* type
,
799 return ((is_pointer
? "__go_pimt__" : "__go_imt_")
800 + itype
->mangled_name(this)
802 + type
->mangled_name(this));