compiler: drop special handling of unexported func/var assembler names
[official-gcc.git] / gcc / go / gofrontend / names.cc
blob20f7c57ee499cd3465650e0afdcf68e3b7b5e55b
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.
7 #include "go-system.h"
9 #include "gogo.h"
10 #include "go-encode-id.h"
11 #include "types.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
16 // debug info.
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.
26 std::string
27 Gogo::function_asm_name(const std::string& go_name, const Package* package,
28 const Type* rtype)
30 std::string ret = (package == NULL
31 ? this->pkgpath_symbol()
32 : package->pkgpath_symbol());
34 if (rtype != NULL
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
40 // package.
41 std::string p = Gogo::hidden_name_pkgpath(go_name);
42 ret = this->pkgpath_symbol_for_package(p);
45 ret.append(1, '.');
46 ret.append(Gogo::unpack_hidden_name(go_name));
48 if (rtype != NULL)
50 ret.append(1, '.');
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.
60 std::string
61 Gogo::function_descriptor_name(Named_object* no)
63 std::string var_name;
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";
70 else
71 var_name = no->func_declaration_value()->asm_name() + "$descriptor";
73 else
75 if (no->package() == NULL)
76 var_name = this->pkgpath_symbol();
77 else
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");
83 return var_name;
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
92 // type.
94 std::string
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
102 // *EQUAL_NAME.
104 void
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;
110 if (name == NULL)
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);
117 size_t i;
118 while ((i = base_name.find('.')) != std::string::npos)
119 base_name[i] = '$';
120 base_name = this->pack_hidden_name(base_name, false);
122 else
124 // This name is already hidden or not as appropriate.
125 base_name = name->name();
126 unsigned int index;
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();
133 if (rcvr != NULL)
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()));
140 if (index > 0)
142 char buf[30];
143 snprintf(buf, sizeof buf, "%u", index);
144 base_name += '$';
145 base_name += buf;
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
156 // compiled.
158 std::string
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());
164 ret.push_back('.');
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
170 // been reported.
172 std::string
173 Gogo::erroneous_name()
175 static int erroneous_count;
176 char name[50];
177 snprintf(name, sizeof name, "$erroneous%d", erroneous_count);
178 ++erroneous_count;
179 return name;
182 // Return whether a name is an erroneous name.
184 bool
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.
192 std::string
193 Gogo::thunk_name()
195 static int thunk_count;
196 char thunk_name[50];
197 snprintf(thunk_name, sizeof thunk_name, "$thunk%d", thunk_count);
198 ++thunk_count;
199 return thunk_name;
202 // Return whether a function is a thunk.
204 bool
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.
213 std::string
214 Gogo::init_function_name()
216 static int init_count;
217 char buf[30];
218 snprintf(buf, sizeof buf, ".$init%d", init_count);
219 ++init_count;
220 return buf;
223 // Return the name to use for a nested function.
225 std::string
226 Gogo::nested_function_name()
228 static int nested_count;
229 char buf[30];
230 snprintf(buf, sizeof buf, ".$nested%d", nested_count);
231 ++nested_count;
232 return buf;
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, 8, ".$nested") == 0);
242 return strtol(n.substr(8).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.
249 std::string
250 Gogo::sink_function_name()
252 static int sink_count;
253 char buf[30];
254 snprintf(buf, sizeof buf, ".$sink%d", sink_count);
255 ++sink_count;
256 return buf;
259 // Return the name to use for a redefined function. These functions
260 // are erroneous but we still generate them for further error
261 // checking.
263 std::string
264 Gogo::redefined_function_name()
266 static int redefinition_count;
267 char buf[30];
268 snprintf(buf, sizeof buf, ".$redefined%d", redefinition_count);
269 ++redefinition_count;
270 return buf;
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.
276 std::string
277 Gogo::recover_thunk_name(const std::string& name, const Type* rtype)
279 std::string ret(name);
280 if (rtype != NULL)
282 ret.push_back('$');
283 ret.append(rtype->mangled_name(this));
285 ret.append("$recover");
286 return ret;
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
292 // per compilation.
294 std::string
295 Gogo::gc_root_name()
297 return "gc0";
300 // Return the name to use for a composite literal or string
301 // initializer. This is a local name never referenced outside of this
302 // file.
304 std::string
305 Gogo::initializer_name()
307 static unsigned int counter;
308 char buf[30];
309 snprintf(buf, sizeof buf, "C%u", counter);
310 ++counter;
311 return buf;
314 // Return the name of the variable used to represent the zero value of
315 // a map. This is a globally visible common symbol.
317 std::string
318 Gogo::map_zero_value_name()
320 return "go$zerovalue";
323 // Return the name to use for the import control function.
325 const std::string&
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";
336 else
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
349 // methods.
351 std::string
352 Type::mangled_name(Gogo* gogo) const
354 std::string ret;
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);
361 return ret;
364 // The mangled name is implemented as a method on each instance of
365 // Type.
367 void
368 Error_type::do_mangled_name(Gogo*, std::string* ret) const
370 ret->push_back('E');
373 void
374 Void_type::do_mangled_name(Gogo*, std::string* ret) const
376 ret->push_back('v');
379 void
380 Boolean_type::do_mangled_name(Gogo*, std::string* ret) const
382 ret->push_back('b');
385 void
386 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
388 char buf[100];
389 snprintf(buf, sizeof buf, "i%s%s%de",
390 this->is_abstract_ ? "a" : "",
391 this->is_unsigned_ ? "u" : "",
392 this->bits_);
393 ret->append(buf);
396 void
397 Float_type::do_mangled_name(Gogo*, std::string* ret) const
399 char buf[100];
400 snprintf(buf, sizeof buf, "f%s%de",
401 this->is_abstract_ ? "a" : "",
402 this->bits_);
403 ret->append(buf);
406 void
407 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
409 char buf[100];
410 snprintf(buf, sizeof buf, "c%s%de",
411 this->is_abstract_ ? "a" : "",
412 this->bits_);
413 ret->append(buf);
416 void
417 String_type::do_mangled_name(Gogo*, std::string* ret) const
419 ret->push_back('z');
422 void
423 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
425 ret->push_back('F');
427 if (this->receiver_ != NULL)
429 ret->push_back('m');
430 this->append_mangled_name(this->receiver_->type(), gogo, ret);
433 const Typed_identifier_list* params = this->parameters();
434 if (params != NULL)
436 ret->push_back('p');
437 for (Typed_identifier_list::const_iterator p = params->begin();
438 p != params->end();
439 ++p)
440 this->append_mangled_name(p->type(), gogo, ret);
441 if (this->is_varargs_)
442 ret->push_back('V');
443 ret->push_back('e');
446 const Typed_identifier_list* results = this->results();
447 if (results != NULL)
449 ret->push_back('r');
450 for (Typed_identifier_list::const_iterator p = results->begin();
451 p != results->end();
452 ++p)
453 this->append_mangled_name(p->type(), gogo, ret);
454 ret->push_back('e');
457 ret->push_back('e');
460 void
461 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
463 ret->push_back('p');
464 this->append_mangled_name(this->to_type_, gogo, ret);
467 void
468 Nil_type::do_mangled_name(Gogo*, std::string* ret) const
470 ret->push_back('n');
473 void
474 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
476 ret->push_back('S');
478 const Struct_field_list* fields = this->fields_;
479 if (fields != NULL)
481 for (Struct_field_list::const_iterator p = fields->begin();
482 p != fields->end();
483 ++p)
485 if (p->is_anonymous())
486 ret->append("0_");
487 else
490 std::string n(Gogo::mangle_possibly_hidden_name(p->field_name()));
491 char buf[20];
492 snprintf(buf, sizeof buf, "%u_",
493 static_cast<unsigned int>(n.length()));
494 ret->append(buf);
495 ret->append(n);
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);
504 else
505 this->append_mangled_name(p->type(), gogo, ret);
506 if (p->has_tag())
508 const std::string& tag(p->tag());
509 std::string out;
510 for (std::string::const_iterator p = tag.begin();
511 p != tag.end();
512 ++p)
514 if (ISALNUM(*p) || *p == '_')
515 out.push_back(*p);
516 else
518 char buf[20];
519 snprintf(buf, sizeof buf, ".%x.",
520 static_cast<unsigned int>(*p));
521 out.append(buf);
524 char buf[20];
525 snprintf(buf, sizeof buf, "T%u_",
526 static_cast<unsigned int>(out.length()));
527 ret->append(buf);
528 ret->append(out);
533 if (this->is_struct_incomparable_)
534 ret->push_back('x');
536 ret->push_back('e');
539 void
540 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
542 ret->push_back('A');
543 this->append_mangled_name(this->element_type_, gogo, ret);
544 if (this->length_ != NULL)
546 Numeric_constant nc;
547 if (!this->length_->numeric_constant_value(&nc))
549 go_assert(saw_errors());
550 return;
552 mpz_t val;
553 if (!nc.to_int(&val))
555 go_assert(saw_errors());
556 return;
558 char *s = mpz_get_str(NULL, 10, val);
559 ret->append(s);
560 free(s);
561 mpz_clear(val);
562 if (this->is_array_incomparable_)
563 ret->push_back('x');
565 ret->push_back('e');
568 void
569 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
571 ret->push_back('M');
572 this->append_mangled_name(this->key_type_, gogo, ret);
573 ret->append("__");
574 this->append_mangled_name(this->val_type_, gogo, ret);
577 void
578 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
580 ret->push_back('C');
581 this->append_mangled_name(this->element_type_, gogo, ret);
582 if (this->may_send_)
583 ret->push_back('s');
584 if (this->may_receive_)
585 ret->push_back('r');
586 ret->push_back('e');
589 void
590 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
592 go_assert(this->methods_are_finalized_);
594 ret->push_back('I');
596 const Typed_identifier_list* methods = this->all_methods_;
597 if (methods != NULL && !this->seen_)
599 this->seen_ = true;
600 for (Typed_identifier_list::const_iterator p = methods->begin();
601 p != methods->end();
602 ++p)
604 if (!p->name().empty())
606 std::string n(Gogo::mangle_possibly_hidden_name(p->name()));
607 char buf[20];
608 snprintf(buf, sizeof buf, "%u_",
609 static_cast<unsigned int>(n.length()));
610 ret->append(buf);
611 ret->append(n);
613 this->append_mangled_name(p->type(), gogo, ret);
615 this->seen_ = false;
618 ret->push_back('e');
621 void
622 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
624 this->append_mangled_type_name(gogo, false, ret);
627 void
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);
632 else
634 const Named_object* no = this->named_object();
635 std::string name;
636 if (no->package() == NULL)
637 name = gogo->pkgpath_symbol();
638 else
639 name = no->package()->pkgpath_symbol();
640 name += '.';
641 name += Gogo::unpack_hidden_name(no->name());
642 char buf[20];
643 snprintf(buf, sizeof buf, "N%u_",
644 static_cast<unsigned int>(name.length()));
645 ret->append(buf);
646 ret->append(name);
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.
654 void
655 Named_type::append_mangled_type_name(Gogo* gogo, bool use_alias,
656 std::string* ret) const
658 if (this->is_error_)
659 return;
660 if (this->is_alias_ && !use_alias)
662 if (this->seen_alias_)
663 return;
664 this->seen_alias_ = true;
665 this->append_mangled_name(this->type_, gogo, ret);
666 this->seen_alias_ = false;
667 return;
669 Named_object* no = this->named_object_;
670 std::string name;
671 if (this->is_builtin())
672 go_assert(this->in_function_ == NULL);
673 else
675 const std::string& pkgpath(no->package() == NULL
676 ? gogo->pkgpath_symbol()
677 : no->package()->pkgpath_symbol());
678 name = pkgpath;
679 name.append(1, '.');
680 if (this->in_function_ != NULL)
682 const Typed_identifier* rcvr =
683 this->in_function_->func_value()->type()->receiver();
684 if (rcvr != NULL)
686 Named_type* rcvr_type = rcvr->type()->deref()->named_type();
687 name.append(Gogo::unpack_hidden_name(rcvr_type->name()));
688 name.append(1, '.');
690 name.append(Gogo::unpack_hidden_name(this->in_function_->name()));
691 name.append(1, '$');
692 if (this->in_function_index_ > 0)
694 char buf[30];
695 snprintf(buf, sizeof buf, "%u", this->in_function_index_);
696 name.append(buf);
697 name.append(1, '$');
701 name.append(Gogo::unpack_hidden_name(no->name()));
702 char buf[20];
703 snprintf(buf, sizeof buf, "N%u_", static_cast<unsigned int>(name.length()));
704 ret->append(buf);
705 ret->append(name);
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.
712 std::string
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
717 // that symbol.
718 if (type->is_unsafe_pointer_type())
719 return "__go_tdn_unsafe.Pointer";
721 if (nt == NULL)
722 return "__go_td_" + type->mangled_name(this);
724 Named_object* no = nt->named_object();
725 unsigned int index;
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);
730 else
732 const std::string& pkgpath(no->package() == NULL
733 ? this->pkgpath_symbol()
734 : no->package()->pkgpath_symbol());
735 ret.append(pkgpath);
736 ret.append(1, '.');
737 if (in_function != NULL)
739 const Typed_identifier* rcvr =
740 in_function->func_value()->type()->receiver();
741 if (rcvr != NULL)
743 Named_type* rcvr_type = rcvr->type()->deref()->named_type();
744 ret.append(Gogo::unpack_hidden_name(rcvr_type->name()));
745 ret.append(1, '.');
747 ret.append(Gogo::unpack_hidden_name(in_function->name()));
748 ret.append(1, '.');
749 if (index > 0)
751 char buf[30];
752 snprintf(buf, sizeof buf, "%u", index);
753 ret.append(buf);
754 ret.append(1, '.');
759 std::string mname(Gogo::mangle_possibly_hidden_name(no->name()));
760 ret.append(mname);
762 return ret;
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.)
771 std::string
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.)
784 std::string
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
793 // receiver.
795 std::string
796 Gogo::interface_method_table_name(Interface_type* itype, Type* type,
797 bool is_pointer)
799 return ((is_pointer ? "__go_pimt__" : "__go_imt_")
800 + itype->mangled_name(this)
801 + "__"
802 + type->mangled_name(this));