1 // types.cc -- Go frontend types.
3 // Copyright 2009 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.
11 #ifndef ENABLE_BUILD_WITH_CXX
23 #ifndef ENABLE_BUILD_WITH_CXX
30 #include "expressions.h"
31 #include "statements.h"
39 Type::Type(Type_classification classification)
40 : classification_(classification), btype_(NULL), type_descriptor_var_(NULL)
48 // Get the base type for a type--skip names and forward declarations.
53 switch (this->classification_)
56 return this->named_type()->named_base();
58 return this->forward_declaration_type()->real_type()->base();
67 switch (this->classification_)
70 return this->named_type()->named_base();
72 return this->forward_declaration_type()->real_type()->base();
78 // Skip defined forward declarations.
84 Forward_declaration_type* ftype = t->forward_declaration_type();
85 while (ftype != NULL && ftype->is_defined())
87 t = ftype->real_type();
88 ftype = t->forward_declaration_type();
94 Type::forwarded() const
97 const Forward_declaration_type* ftype = t->forward_declaration_type();
98 while (ftype != NULL && ftype->is_defined())
100 t = ftype->real_type();
101 ftype = t->forward_declaration_type();
106 // If this is a named type, return it. Otherwise, return NULL.
111 return this->forwarded()->convert_no_base<Named_type, TYPE_NAMED>();
115 Type::named_type() const
117 return this->forwarded()->convert_no_base<const Named_type, TYPE_NAMED>();
120 // Return true if this type is not defined.
123 Type::is_undefined() const
125 return this->forwarded()->forward_declaration_type() != NULL;
128 // Return true if this is a basic type: a type which is not composed
129 // of other types, and is not void.
132 Type::is_basic_type() const
134 switch (this->classification_)
157 return this->base()->is_basic_type();
164 // Return true if this is an abstract type.
167 Type::is_abstract() const
169 switch (this->classification())
172 return this->integer_type()->is_abstract();
174 return this->float_type()->is_abstract();
176 return this->complex_type()->is_abstract();
178 return this->is_abstract_string_type();
180 return this->is_abstract_boolean_type();
186 // Return a non-abstract version of an abstract type.
189 Type::make_non_abstract_type()
191 go_assert(this->is_abstract());
192 switch (this->classification())
195 return Type::lookup_integer_type("int");
197 return Type::lookup_float_type("float64");
199 return Type::lookup_complex_type("complex128");
201 return Type::lookup_string_type();
203 return Type::lookup_bool_type();
209 // Return true if this is an error type. Don't give an error if we
210 // try to dereference an undefined forwarding type, as this is called
211 // in the parser when the type may legitimately be undefined.
214 Type::is_error_type() const
216 const Type* t = this->forwarded();
217 // Note that we return false for an undefined forward type.
218 switch (t->classification_)
223 return t->named_type()->is_named_error_type();
229 // If this is a pointer type, return the type to which it points.
230 // Otherwise, return NULL.
233 Type::points_to() const
235 const Pointer_type* ptype = this->convert<const Pointer_type,
237 return ptype == NULL ? NULL : ptype->points_to();
240 // Return whether this is an open array type.
243 Type::is_slice_type() const
245 return this->array_type() != NULL && this->array_type()->length() == NULL;
248 // Return whether this is the predeclared constant nil being used as a
252 Type::is_nil_constant_as_type() const
254 const Type* t = this->forwarded();
255 if (t->forward_declaration_type() != NULL)
257 const Named_object* no = t->forward_declaration_type()->named_object();
258 if (no->is_unknown())
259 no = no->unknown_value()->real_named_object();
262 && no->const_value()->expr()->is_nil_expression())
271 Type::traverse(Type* type, Traverse* traverse)
273 go_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
274 || (traverse->traverse_mask()
275 & Traverse::traverse_expressions) != 0);
276 if (traverse->remember_type(type))
278 // We have already traversed this type.
279 return TRAVERSE_CONTINUE;
281 if ((traverse->traverse_mask() & Traverse::traverse_types) != 0)
283 int t = traverse->type(type);
284 if (t == TRAVERSE_EXIT)
285 return TRAVERSE_EXIT;
286 else if (t == TRAVERSE_SKIP_COMPONENTS)
287 return TRAVERSE_CONTINUE;
289 // An array type has an expression which we need to traverse if
290 // traverse_expressions is set.
291 if (type->do_traverse(traverse) == TRAVERSE_EXIT)
292 return TRAVERSE_EXIT;
293 return TRAVERSE_CONTINUE;
296 // Default implementation for do_traverse for child class.
299 Type::do_traverse(Traverse*)
301 return TRAVERSE_CONTINUE;
304 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
305 // then return true for all erroneous types; this is used to avoid
306 // cascading errors. If REASON is not NULL, optionally set *REASON to
307 // the reason the types are not identical.
310 Type::are_identical(const Type* t1, const Type* t2, bool errors_are_identical,
313 if (t1 == NULL || t2 == NULL)
315 // Something is wrong.
316 return errors_are_identical ? true : t1 == t2;
319 // Skip defined forward declarations.
320 t1 = t1->forwarded();
321 t2 = t2->forwarded();
326 // An undefined forward declaration is an error.
327 if (t1->forward_declaration_type() != NULL
328 || t2->forward_declaration_type() != NULL)
329 return errors_are_identical;
331 // Avoid cascading errors with error types.
332 if (t1->is_error_type() || t2->is_error_type())
334 if (errors_are_identical)
336 return t1->is_error_type() && t2->is_error_type();
339 // Get a good reason for the sink type. Note that the sink type on
340 // the left hand side of an assignment is handled in are_assignable.
341 if (t1->is_sink_type() || t2->is_sink_type())
344 *reason = "invalid use of _";
348 // A named type is only identical to itself.
349 if (t1->named_type() != NULL || t2->named_type() != NULL)
352 // Check type shapes.
353 if (t1->classification() != t2->classification())
356 switch (t1->classification())
362 // These types are always identical.
366 return t1->integer_type()->is_identical(t2->integer_type());
369 return t1->float_type()->is_identical(t2->float_type());
372 return t1->complex_type()->is_identical(t2->complex_type());
375 return t1->function_type()->is_identical(t2->function_type(),
377 errors_are_identical,
381 return Type::are_identical(t1->points_to(), t2->points_to(),
382 errors_are_identical, reason);
385 return t1->struct_type()->is_identical(t2->struct_type(),
386 errors_are_identical);
389 return t1->array_type()->is_identical(t2->array_type(),
390 errors_are_identical);
393 return t1->map_type()->is_identical(t2->map_type(),
394 errors_are_identical);
397 return t1->channel_type()->is_identical(t2->channel_type(),
398 errors_are_identical);
401 return t1->interface_type()->is_identical(t2->interface_type(),
402 errors_are_identical);
404 case TYPE_CALL_MULTIPLE_RESULT:
406 *reason = "invalid use of multiple value function call";
414 // Return true if it's OK to have a binary operation with types LHS
415 // and RHS. This is not used for shifts or comparisons.
418 Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
420 if (Type::are_identical(lhs, rhs, true, NULL))
423 // A constant of abstract bool type may be mixed with any bool type.
424 if ((rhs->is_abstract_boolean_type() && lhs->is_boolean_type())
425 || (lhs->is_abstract_boolean_type() && rhs->is_boolean_type()))
428 // A constant of abstract string type may be mixed with any string
430 if ((rhs->is_abstract_string_type() && lhs->is_string_type())
431 || (lhs->is_abstract_string_type() && rhs->is_string_type()))
437 // A constant of abstract integer, float, or complex type may be
438 // mixed with an integer, float, or complex type.
439 if ((rhs->is_abstract()
440 && (rhs->integer_type() != NULL
441 || rhs->float_type() != NULL
442 || rhs->complex_type() != NULL)
443 && (lhs->integer_type() != NULL
444 || lhs->float_type() != NULL
445 || lhs->complex_type() != NULL))
446 || (lhs->is_abstract()
447 && (lhs->integer_type() != NULL
448 || lhs->float_type() != NULL
449 || lhs->complex_type() != NULL)
450 && (rhs->integer_type() != NULL
451 || rhs->float_type() != NULL
452 || rhs->complex_type() != NULL)))
455 // The nil type may be compared to a pointer, an interface type, a
456 // slice type, a channel type, a map type, or a function type.
457 if (lhs->is_nil_type()
458 && (rhs->points_to() != NULL
459 || rhs->interface_type() != NULL
460 || rhs->is_slice_type()
461 || rhs->map_type() != NULL
462 || rhs->channel_type() != NULL
463 || rhs->function_type() != NULL))
465 if (rhs->is_nil_type()
466 && (lhs->points_to() != NULL
467 || lhs->interface_type() != NULL
468 || lhs->is_slice_type()
469 || lhs->map_type() != NULL
470 || lhs->channel_type() != NULL
471 || lhs->function_type() != NULL))
477 // Return true if a value with type RHS may be assigned to a variable
478 // with type LHS. If CHECK_HIDDEN_FIELDS is true, check whether any
479 // hidden fields are modified. If REASON is not NULL, set *REASON to
480 // the reason the types are not assignable.
483 Type::are_assignable_check_hidden(const Type* lhs, const Type* rhs,
484 bool check_hidden_fields,
487 // Do some checks first. Make sure the types are defined.
489 && rhs->forwarded()->forward_declaration_type() == NULL
490 && rhs->is_void_type())
493 *reason = "non-value used as value";
497 if (lhs != NULL && lhs->forwarded()->forward_declaration_type() == NULL)
499 // Any value may be assigned to the blank identifier.
500 if (lhs->is_sink_type())
503 // All fields of a struct must be exported, or the assignment
504 // must be in the same package.
505 if (check_hidden_fields
507 && rhs->forwarded()->forward_declaration_type() == NULL)
509 if (lhs->has_hidden_fields(NULL, reason)
510 || rhs->has_hidden_fields(NULL, reason))
515 // Identical types are assignable.
516 if (Type::are_identical(lhs, rhs, true, reason))
519 // The types are assignable if they have identical underlying types
520 // and either LHS or RHS is not a named type.
521 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
522 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
523 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
526 // The types are assignable if LHS is an interface type and RHS
527 // implements the required methods.
528 const Interface_type* lhs_interface_type = lhs->interface_type();
529 if (lhs_interface_type != NULL)
531 if (lhs_interface_type->implements_interface(rhs, reason))
533 const Interface_type* rhs_interface_type = rhs->interface_type();
534 if (rhs_interface_type != NULL
535 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
540 // The type are assignable if RHS is a bidirectional channel type,
541 // LHS is a channel type, they have identical element types, and
542 // either LHS or RHS is not a named type.
543 if (lhs->channel_type() != NULL
544 && rhs->channel_type() != NULL
545 && rhs->channel_type()->may_send()
546 && rhs->channel_type()->may_receive()
547 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
548 && Type::are_identical(lhs->channel_type()->element_type(),
549 rhs->channel_type()->element_type(),
554 // The nil type may be assigned to a pointer, function, slice, map,
555 // channel, or interface type.
556 if (rhs->is_nil_type()
557 && (lhs->points_to() != NULL
558 || lhs->function_type() != NULL
559 || lhs->is_slice_type()
560 || lhs->map_type() != NULL
561 || lhs->channel_type() != NULL
562 || lhs->interface_type() != NULL))
565 // An untyped numeric constant may be assigned to a numeric type if
566 // it is representable in that type.
567 if ((rhs->is_abstract()
568 && (rhs->integer_type() != NULL
569 || rhs->float_type() != NULL
570 || rhs->complex_type() != NULL))
571 && (lhs->integer_type() != NULL
572 || lhs->float_type() != NULL
573 || lhs->complex_type() != NULL))
576 // Give some better error messages.
577 if (reason != NULL && reason->empty())
579 if (rhs->interface_type() != NULL)
580 reason->assign(_("need explicit conversion"));
581 else if (rhs->is_call_multiple_result_type())
582 reason->assign(_("multiple value function call in "
583 "single value context"));
584 else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
586 size_t len = (lhs->named_type()->name().length()
587 + rhs->named_type()->name().length()
589 char* buf = new char[len];
590 snprintf(buf, len, _("cannot use type %s as type %s"),
591 rhs->named_type()->message_name().c_str(),
592 lhs->named_type()->message_name().c_str());
601 // Return true if a value with type RHS may be assigned to a variable
602 // with type LHS. If REASON is not NULL, set *REASON to the reason
603 // the types are not assignable.
606 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
608 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
611 // Like are_assignable but don't check for hidden fields.
614 Type::are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
617 return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
620 // Return true if a value with type RHS may be converted to type LHS.
621 // If REASON is not NULL, set *REASON to the reason the types are not
625 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
627 // The types are convertible if they are assignable.
628 if (Type::are_assignable(lhs, rhs, reason))
631 // The types are convertible if they have identical underlying
633 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
634 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
637 // The types are convertible if they are both unnamed pointer types
638 // and their pointer base types have identical underlying types.
639 if (lhs->named_type() == NULL
640 && rhs->named_type() == NULL
641 && lhs->points_to() != NULL
642 && rhs->points_to() != NULL
643 && (lhs->points_to()->named_type() != NULL
644 || rhs->points_to()->named_type() != NULL)
645 && Type::are_identical(lhs->points_to()->base(),
646 rhs->points_to()->base(),
651 // Integer and floating point types are convertible to each other.
652 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
653 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
656 // Complex types are convertible to each other.
657 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
660 // An integer, or []byte, or []int, may be converted to a string.
661 if (lhs->is_string_type())
663 if (rhs->integer_type() != NULL)
665 if (rhs->is_slice_type())
667 const Type* e = rhs->array_type()->element_type()->forwarded();
668 if (e->integer_type() != NULL
669 && (e == Type::lookup_integer_type("uint8")
670 || e == Type::lookup_integer_type("int")))
675 // A string may be converted to []byte or []int.
676 if (rhs->is_string_type() && lhs->is_slice_type())
678 const Type* e = lhs->array_type()->element_type()->forwarded();
679 if (e->integer_type() != NULL
680 && (e == Type::lookup_integer_type("uint8")
681 || e == Type::lookup_integer_type("int")))
685 // An unsafe.Pointer type may be converted to any pointer type or to
686 // uintptr, and vice-versa.
687 if (lhs->is_unsafe_pointer_type()
688 && (rhs->points_to() != NULL
689 || (rhs->integer_type() != NULL
690 && rhs->forwarded() == Type::lookup_integer_type("uintptr"))))
692 if (rhs->is_unsafe_pointer_type()
693 && (lhs->points_to() != NULL
694 || (lhs->integer_type() != NULL
695 && lhs->forwarded() == Type::lookup_integer_type("uintptr"))))
698 // Give a better error message.
702 *reason = "invalid type conversion";
705 std::string s = "invalid type conversion (";
715 // Return whether this type has any hidden fields. This is only a
716 // possibility for a few types.
719 Type::has_hidden_fields(const Named_type* within, std::string* reason) const
721 switch (this->forwarded()->classification_)
724 return this->named_type()->named_type_has_hidden_fields(reason);
726 return this->struct_type()->struct_has_hidden_fields(within, reason);
728 return this->array_type()->array_has_hidden_fields(within, reason);
734 // Return a hash code for the type to be used for method lookup.
737 Type::hash_for_method(Gogo* gogo) const
739 unsigned int ret = 0;
740 if (this->classification_ != TYPE_FORWARD)
741 ret += this->classification_;
742 return ret + this->do_hash_for_method(gogo);
745 // Default implementation of do_hash_for_method. This is appropriate
746 // for types with no subfields.
749 Type::do_hash_for_method(Gogo*) const
754 // Return a hash code for a string, given a starting hash.
757 Type::hash_string(const std::string& s, unsigned int h)
759 const char* p = s.data();
760 size_t len = s.length();
761 for (; len > 0; --len)
769 // A hash table mapping unnamed types to the backend representation of
772 Type::Type_btypes Type::type_btypes;
774 // Return a tree representing this type.
777 Type::get_backend(Gogo* gogo)
779 if (this->btype_ != NULL)
782 if (this->forward_declaration_type() != NULL
783 || this->named_type() != NULL)
784 return this->get_btype_without_hash(gogo);
786 if (this->is_error_type())
787 return gogo->backend()->error_type();
789 // To avoid confusing the backend, translate all identical Go types
790 // to the same backend representation. We use a hash table to do
791 // that. There is no need to use the hash table for named types, as
792 // named types are only identical to themselves.
794 std::pair<Type*, Btype*> val(this, NULL);
795 std::pair<Type_btypes::iterator, bool> ins =
796 Type::type_btypes.insert(val);
797 if (!ins.second && ins.first->second != NULL)
799 if (gogo != NULL && gogo->named_types_are_converted())
800 this->btype_ = ins.first->second;
801 return ins.first->second;
804 Btype* bt = this->get_btype_without_hash(gogo);
806 if (ins.first->second == NULL)
807 ins.first->second = bt;
810 // We have already created a backend representation for this
811 // type. This can happen when an unnamed type is defined using
812 // a named type which in turns uses an identical unnamed type.
813 // Use the tree we created earlier and ignore the one we just
815 bt = ins.first->second;
816 if (gogo == NULL || !gogo->named_types_are_converted())
824 // Return the backend representation for a type without looking in the
825 // hash table for identical types. This is used for named types,
826 // since a named type is never identical to any other type.
829 Type::get_btype_without_hash(Gogo* gogo)
831 if (this->btype_ == NULL)
833 Btype* bt = this->do_get_backend(gogo);
835 // For a recursive function or pointer type, we will temporarily
836 // return a circular pointer type during the recursion. We
837 // don't want to record that for a forwarding type, as it may
839 if (this->forward_declaration_type() != NULL
840 && gogo->backend()->is_circular_pointer_type(bt))
843 if (gogo == NULL || !gogo->named_types_are_converted())
851 // Return a pointer to the type descriptor for this type.
854 Type::type_descriptor_pointer(Gogo* gogo, Location location)
856 Type* t = this->forwarded();
857 if (t->type_descriptor_var_ == NULL)
859 t->make_type_descriptor_var(gogo);
860 go_assert(t->type_descriptor_var_ != NULL);
862 tree var_tree = var_to_tree(t->type_descriptor_var_);
863 if (var_tree == error_mark_node)
864 return error_mark_node;
865 return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
868 // A mapping from unnamed types to type descriptor variables.
870 Type::Type_descriptor_vars Type::type_descriptor_vars;
872 // Build the type descriptor for this type.
875 Type::make_type_descriptor_var(Gogo* gogo)
877 go_assert(this->type_descriptor_var_ == NULL);
879 Named_type* nt = this->named_type();
881 // We can have multiple instances of unnamed types, but we only want
882 // to emit the type descriptor once. We use a hash table. This is
883 // not necessary for named types, as they are unique, and we store
884 // the type descriptor in the type itself.
885 Bvariable** phash = NULL;
888 Bvariable* bvnull = NULL;
889 std::pair<Type_descriptor_vars::iterator, bool> ins =
890 Type::type_descriptor_vars.insert(std::make_pair(this, bvnull));
893 // We've already build a type descriptor for this type.
894 this->type_descriptor_var_ = ins.first->second;
897 phash = &ins.first->second;
900 std::string var_name;
902 var_name = this->unnamed_type_descriptor_var_name(gogo);
904 var_name = this->type_descriptor_var_name(gogo);
906 // Build the contents of the type descriptor.
907 Expression* initializer = this->do_type_descriptor(gogo, NULL);
909 Btype* initializer_btype = initializer->type()->get_backend(gogo);
911 // See if this type descriptor is defined in a different package.
912 bool is_defined_elsewhere = false;
915 if (nt->named_object()->package() != NULL)
917 // This is a named type defined in a different package. The
918 // type descriptor should be defined in that package.
919 is_defined_elsewhere = true;
924 if (this->points_to() != NULL
925 && this->points_to()->named_type() != NULL
926 && this->points_to()->named_type()->named_object()->package() != NULL)
928 // This is an unnamed pointer to a named type defined in a
929 // different package. The descriptor should be defined in
931 is_defined_elsewhere = true;
935 Location loc = nt == NULL ? Linemap::predeclared_location() : nt->location();
937 if (is_defined_elsewhere)
939 this->type_descriptor_var_ =
940 gogo->backend()->immutable_struct_reference(var_name,
944 *phash = this->type_descriptor_var_;
948 // See if this type descriptor can appear in multiple packages.
949 bool is_common = false;
952 // We create the descriptor for a builtin type whenever we need
954 is_common = nt->is_builtin();
958 // This is an unnamed type. The descriptor could be defined in
959 // any package where it is needed, and the linker will pick one
960 // descriptor to keep.
964 // We are going to build the type descriptor in this package. We
965 // must create the variable before we convert the initializer to the
966 // backend representation, because the initializer may refer to the
967 // type descriptor of this type. By setting type_descriptor_var_ we
968 // ensure that type_descriptor_pointer will work if called while
969 // converting INITIALIZER.
971 this->type_descriptor_var_ =
972 gogo->backend()->immutable_struct(var_name, is_common, initializer_btype,
975 *phash = this->type_descriptor_var_;
977 Translate_context context(gogo, NULL, NULL, NULL);
978 context.set_is_const();
979 Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
981 gogo->backend()->immutable_struct_set_init(this->type_descriptor_var_,
983 initializer_btype, loc,
987 // Return the name of the type descriptor variable for an unnamed
991 Type::unnamed_type_descriptor_var_name(Gogo* gogo)
993 return "__go_td_" + this->mangled_name(gogo);
996 // Return the name of the type descriptor variable for a named type.
999 Type::type_descriptor_var_name(Gogo* gogo)
1001 Named_type* nt = this->named_type();
1002 Named_object* no = nt->named_object();
1003 const Named_object* in_function = nt->in_function();
1004 std::string ret = "__go_tdn_";
1005 if (nt->is_builtin())
1006 go_assert(in_function == NULL);
1009 const std::string& unique_prefix(no->package() == NULL
1010 ? gogo->unique_prefix()
1011 : no->package()->unique_prefix());
1012 const std::string& package_name(no->package() == NULL
1013 ? gogo->package_name()
1014 : no->package()->name());
1015 ret.append(unique_prefix);
1017 ret.append(package_name);
1019 if (in_function != NULL)
1021 ret.append(Gogo::unpack_hidden_name(in_function->name()));
1025 ret.append(no->name());
1029 // Return a composite literal for a type descriptor.
1032 Type::type_descriptor(Gogo* gogo, Type* type)
1034 return type->do_type_descriptor(gogo, NULL);
1037 // Return a composite literal for a type descriptor with a name.
1040 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
1042 go_assert(name != NULL && type->named_type() != name);
1043 return type->do_type_descriptor(gogo, name);
1046 // Make a builtin struct type from a list of fields. The fields are
1047 // pairs of a name and a type.
1050 Type::make_builtin_struct_type(int nfields, ...)
1053 va_start(ap, nfields);
1055 Location bloc = Linemap::predeclared_location();
1056 Struct_field_list* sfl = new Struct_field_list();
1057 for (int i = 0; i < nfields; i++)
1059 const char* field_name = va_arg(ap, const char *);
1060 Type* type = va_arg(ap, Type*);
1061 sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
1066 return Type::make_struct_type(sfl, bloc);
1069 // A list of builtin named types.
1071 std::vector<Named_type*> Type::named_builtin_types;
1073 // Make a builtin named type.
1076 Type::make_builtin_named_type(const char* name, Type* type)
1078 Location bloc = Linemap::predeclared_location();
1079 Named_object* no = Named_object::make_type(name, NULL, type, bloc);
1080 Named_type* ret = no->type_value();
1081 Type::named_builtin_types.push_back(ret);
1085 // Convert the named builtin types.
1088 Type::convert_builtin_named_types(Gogo* gogo)
1090 for (std::vector<Named_type*>::const_iterator p =
1091 Type::named_builtin_types.begin();
1092 p != Type::named_builtin_types.end();
1095 bool r = (*p)->verify();
1097 (*p)->convert(gogo);
1101 // Return the type of a type descriptor. We should really tie this to
1102 // runtime.Type rather than copying it. This must match commonType in
1103 // libgo/go/runtime/type.go.
1106 Type::make_type_descriptor_type()
1111 Location bloc = Linemap::predeclared_location();
1113 Type* uint8_type = Type::lookup_integer_type("uint8");
1114 Type* uint32_type = Type::lookup_integer_type("uint32");
1115 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1116 Type* string_type = Type::lookup_string_type();
1117 Type* pointer_string_type = Type::make_pointer_type(string_type);
1119 // This is an unnamed version of unsafe.Pointer. Perhaps we
1120 // should use the named version instead, although that would
1121 // require us to create the unsafe package if it has not been
1122 // imported. It probably doesn't matter.
1123 Type* void_type = Type::make_void_type();
1124 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1126 // Forward declaration for the type descriptor type.
1127 Named_object* named_type_descriptor_type =
1128 Named_object::make_type_declaration("commonType", NULL, bloc);
1129 Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
1130 Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
1132 // The type of a method on a concrete type.
1133 Struct_type* method_type =
1134 Type::make_builtin_struct_type(5,
1135 "name", pointer_string_type,
1136 "pkgPath", pointer_string_type,
1137 "mtyp", pointer_type_descriptor_type,
1138 "typ", pointer_type_descriptor_type,
1139 "tfn", unsafe_pointer_type);
1140 Named_type* named_method_type =
1141 Type::make_builtin_named_type("method", method_type);
1143 // Information for types with a name or methods.
1144 Type* slice_named_method_type =
1145 Type::make_array_type(named_method_type, NULL);
1146 Struct_type* uncommon_type =
1147 Type::make_builtin_struct_type(3,
1148 "name", pointer_string_type,
1149 "pkgPath", pointer_string_type,
1150 "methods", slice_named_method_type);
1151 Named_type* named_uncommon_type =
1152 Type::make_builtin_named_type("uncommonType", uncommon_type);
1154 Type* pointer_uncommon_type =
1155 Type::make_pointer_type(named_uncommon_type);
1157 // The type descriptor type.
1159 Typed_identifier_list* params = new Typed_identifier_list();
1160 params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
1161 params->push_back(Typed_identifier("", uintptr_type, bloc));
1163 Typed_identifier_list* results = new Typed_identifier_list();
1164 results->push_back(Typed_identifier("", uintptr_type, bloc));
1166 Type* hashfn_type = Type::make_function_type(NULL, params, results, bloc);
1168 params = new Typed_identifier_list();
1169 params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
1170 params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
1171 params->push_back(Typed_identifier("", uintptr_type, bloc));
1173 results = new Typed_identifier_list();
1174 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1176 Type* equalfn_type = Type::make_function_type(NULL, params, results,
1179 Struct_type* type_descriptor_type =
1180 Type::make_builtin_struct_type(10,
1182 "align", uint8_type,
1183 "fieldAlign", uint8_type,
1184 "size", uintptr_type,
1185 "hash", uint32_type,
1186 "hashfn", hashfn_type,
1187 "equalfn", equalfn_type,
1188 "string", pointer_string_type,
1189 "", pointer_uncommon_type,
1191 pointer_type_descriptor_type);
1193 Named_type* named = Type::make_builtin_named_type("commonType",
1194 type_descriptor_type);
1196 named_type_descriptor_type->set_type_value(named);
1204 // Make the type of a pointer to a type descriptor as represented in
1208 Type::make_type_descriptor_ptr_type()
1212 ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1216 // Return the names of runtime functions which compute a hash code for
1217 // this type and which compare whether two values of this type are
1221 Type::type_functions(const char** hash_fn, const char** equal_fn) const
1223 switch (this->base()->classification())
1225 case Type::TYPE_ERROR:
1226 case Type::TYPE_VOID:
1227 case Type::TYPE_NIL:
1228 // These types can not be hashed or compared.
1229 *hash_fn = "__go_type_hash_error";
1230 *equal_fn = "__go_type_equal_error";
1233 case Type::TYPE_BOOLEAN:
1234 case Type::TYPE_INTEGER:
1235 case Type::TYPE_FLOAT:
1236 case Type::TYPE_COMPLEX:
1237 case Type::TYPE_POINTER:
1238 case Type::TYPE_FUNCTION:
1239 case Type::TYPE_MAP:
1240 case Type::TYPE_CHANNEL:
1241 *hash_fn = "__go_type_hash_identity";
1242 *equal_fn = "__go_type_equal_identity";
1245 case Type::TYPE_STRING:
1246 *hash_fn = "__go_type_hash_string";
1247 *equal_fn = "__go_type_equal_string";
1250 case Type::TYPE_STRUCT:
1251 case Type::TYPE_ARRAY:
1252 // These types can not be hashed or compared.
1253 *hash_fn = "__go_type_hash_error";
1254 *equal_fn = "__go_type_equal_error";
1257 case Type::TYPE_INTERFACE:
1258 if (this->interface_type()->is_empty())
1260 *hash_fn = "__go_type_hash_empty_interface";
1261 *equal_fn = "__go_type_equal_empty_interface";
1265 *hash_fn = "__go_type_hash_interface";
1266 *equal_fn = "__go_type_equal_interface";
1270 case Type::TYPE_NAMED:
1271 case Type::TYPE_FORWARD:
1279 // Return a composite literal for the type descriptor for a plain type
1280 // of kind RUNTIME_TYPE_KIND named NAME.
1283 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1284 Named_type* name, const Methods* methods,
1285 bool only_value_methods)
1287 Location bloc = Linemap::predeclared_location();
1289 Type* td_type = Type::make_type_descriptor_type();
1290 const Struct_field_list* fields = td_type->struct_type()->fields();
1292 Expression_list* vals = new Expression_list();
1295 if (!this->has_pointer())
1296 runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
1297 Struct_field_list::const_iterator p = fields->begin();
1298 go_assert(p->is_field_name("Kind"));
1300 mpz_init_set_ui(iv, runtime_type_kind);
1301 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1304 go_assert(p->is_field_name("align"));
1305 Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1306 vals->push_back(Expression::make_type_info(this, type_info));
1309 go_assert(p->is_field_name("fieldAlign"));
1310 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1311 vals->push_back(Expression::make_type_info(this, type_info));
1314 go_assert(p->is_field_name("size"));
1315 type_info = Expression::TYPE_INFO_SIZE;
1316 vals->push_back(Expression::make_type_info(this, type_info));
1319 go_assert(p->is_field_name("hash"));
1320 mpz_set_ui(iv, this->hash_for_method(gogo));
1321 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1323 const char* hash_fn;
1324 const char* equal_fn;
1325 this->type_functions(&hash_fn, &equal_fn);
1328 go_assert(p->is_field_name("hashfn"));
1329 Function_type* fntype = p->type()->function_type();
1330 Named_object* no = Named_object::make_function_declaration(hash_fn, NULL,
1333 no->func_declaration_value()->set_asm_name(hash_fn);
1334 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1337 go_assert(p->is_field_name("equalfn"));
1338 fntype = p->type()->function_type();
1339 no = Named_object::make_function_declaration(equal_fn, NULL, fntype, bloc);
1340 no->func_declaration_value()->set_asm_name(equal_fn);
1341 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1344 go_assert(p->is_field_name("string"));
1345 Expression* s = Expression::make_string((name != NULL
1346 ? name->reflection(gogo)
1347 : this->reflection(gogo)),
1349 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1352 go_assert(p->is_field_name("uncommonType"));
1353 if (name == NULL && methods == NULL)
1354 vals->push_back(Expression::make_nil(bloc));
1357 if (methods == NULL)
1358 methods = name->methods();
1359 vals->push_back(this->uncommon_type_constructor(gogo,
1362 only_value_methods));
1366 go_assert(p->is_field_name("ptrToThis"));
1368 vals->push_back(Expression::make_nil(bloc));
1371 Type* pt = Type::make_pointer_type(name);
1372 vals->push_back(Expression::make_type_descriptor(pt, bloc));
1376 go_assert(p == fields->end());
1380 return Expression::make_struct_composite_literal(td_type, vals, bloc);
1383 // Return a composite literal for the uncommon type information for
1384 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1385 // struct. If name is not NULL, it is the name of the type. If
1386 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1387 // is true if only value methods should be included. At least one of
1388 // NAME and METHODS must not be NULL.
1391 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
1392 Named_type* name, const Methods* methods,
1393 bool only_value_methods) const
1395 Location bloc = Linemap::predeclared_location();
1397 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
1399 Expression_list* vals = new Expression_list();
1402 Struct_field_list::const_iterator p = fields->begin();
1403 go_assert(p->is_field_name("name"));
1406 go_assert(p->is_field_name("pkgPath"));
1410 vals->push_back(Expression::make_nil(bloc));
1411 vals->push_back(Expression::make_nil(bloc));
1415 Named_object* no = name->named_object();
1416 std::string n = Gogo::unpack_hidden_name(no->name());
1417 Expression* s = Expression::make_string(n, bloc);
1418 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1420 if (name->is_builtin())
1421 vals->push_back(Expression::make_nil(bloc));
1424 const Package* package = no->package();
1425 const std::string& unique_prefix(package == NULL
1426 ? gogo->unique_prefix()
1427 : package->unique_prefix());
1428 const std::string& package_name(package == NULL
1429 ? gogo->package_name()
1431 n.assign(unique_prefix);
1433 n.append(package_name);
1434 if (name->in_function() != NULL)
1437 n.append(Gogo::unpack_hidden_name(name->in_function()->name()));
1439 s = Expression::make_string(n, bloc);
1440 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1445 go_assert(p->is_field_name("methods"));
1446 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
1447 only_value_methods));
1450 go_assert(p == fields->end());
1452 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
1454 return Expression::make_unary(OPERATOR_AND, r, bloc);
1457 // Sort methods by name.
1463 operator()(const std::pair<std::string, const Method*>& m1,
1464 const std::pair<std::string, const Method*>& m2) const
1465 { return m1.first < m2.first; }
1468 // Return a composite literal for the type method table for this type.
1469 // METHODS_TYPE is the type of the table, and is a slice type.
1470 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1471 // then only value methods are used.
1474 Type::methods_constructor(Gogo* gogo, Type* methods_type,
1475 const Methods* methods,
1476 bool only_value_methods) const
1478 Location bloc = Linemap::predeclared_location();
1480 std::vector<std::pair<std::string, const Method*> > smethods;
1481 if (methods != NULL)
1483 smethods.reserve(methods->count());
1484 for (Methods::const_iterator p = methods->begin();
1485 p != methods->end();
1488 if (p->second->is_ambiguous())
1490 if (only_value_methods && !p->second->is_value_method())
1492 smethods.push_back(std::make_pair(p->first, p->second));
1496 if (smethods.empty())
1497 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
1499 std::sort(smethods.begin(), smethods.end(), Sort_methods());
1501 Type* method_type = methods_type->array_type()->element_type();
1503 Expression_list* vals = new Expression_list();
1504 vals->reserve(smethods.size());
1505 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
1507 p != smethods.end();
1509 vals->push_back(this->method_constructor(gogo, method_type, p->first,
1510 p->second, only_value_methods));
1512 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
1515 // Return a composite literal for a single method. METHOD_TYPE is the
1516 // type of the entry. METHOD_NAME is the name of the method and M is
1517 // the method information.
1520 Type::method_constructor(Gogo*, Type* method_type,
1521 const std::string& method_name,
1523 bool only_value_methods) const
1525 Location bloc = Linemap::predeclared_location();
1527 const Struct_field_list* fields = method_type->struct_type()->fields();
1529 Expression_list* vals = new Expression_list();
1532 Struct_field_list::const_iterator p = fields->begin();
1533 go_assert(p->is_field_name("name"));
1534 const std::string n = Gogo::unpack_hidden_name(method_name);
1535 Expression* s = Expression::make_string(n, bloc);
1536 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1539 go_assert(p->is_field_name("pkgPath"));
1540 if (!Gogo::is_hidden_name(method_name))
1541 vals->push_back(Expression::make_nil(bloc));
1544 s = Expression::make_string(Gogo::hidden_name_prefix(method_name), bloc);
1545 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1548 Named_object* no = (m->needs_stub_method()
1550 : m->named_object());
1552 Function_type* mtype;
1553 if (no->is_function())
1554 mtype = no->func_value()->type();
1556 mtype = no->func_declaration_value()->type();
1557 go_assert(mtype->is_method());
1558 Type* nonmethod_type = mtype->copy_without_receiver();
1561 go_assert(p->is_field_name("mtyp"));
1562 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
1565 go_assert(p->is_field_name("typ"));
1566 if (!only_value_methods && m->is_value_method())
1568 // This is a value method on a pointer type. Change the type of
1569 // the method to use a pointer receiver. The implementation
1570 // always uses a pointer receiver anyhow.
1571 Type* rtype = mtype->receiver()->type();
1572 Type* prtype = Type::make_pointer_type(rtype);
1573 Typed_identifier* receiver =
1574 new Typed_identifier(mtype->receiver()->name(), prtype,
1575 mtype->receiver()->location());
1576 mtype = Type::make_function_type(receiver,
1577 (mtype->parameters() == NULL
1579 : mtype->parameters()->copy()),
1580 (mtype->results() == NULL
1582 : mtype->results()->copy()),
1585 vals->push_back(Expression::make_type_descriptor(mtype, bloc));
1588 go_assert(p->is_field_name("tfn"));
1589 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1592 go_assert(p == fields->end());
1594 return Expression::make_struct_composite_literal(method_type, vals, bloc);
1597 // Return a composite literal for the type descriptor of a plain type.
1598 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1599 // NULL, it is the name to use as well as the list of methods.
1602 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
1605 return this->type_descriptor_constructor(gogo, runtime_type_kind,
1609 // Return the type reflection string for this type.
1612 Type::reflection(Gogo* gogo) const
1616 // The do_reflection virtual function should set RET to the
1617 // reflection string.
1618 this->do_reflection(gogo, &ret);
1623 // Return a mangled name for the type.
1626 Type::mangled_name(Gogo* gogo) const
1630 // The do_mangled_name virtual function should set RET to the
1631 // mangled name. For a composite type it should append a code for
1632 // the composition and then call do_mangled_name on the components.
1633 this->do_mangled_name(gogo, &ret);
1638 // Default function to export a type.
1641 Type::do_export(Export*) const
1649 Type::import_type(Import* imp)
1651 if (imp->match_c_string("("))
1652 return Function_type::do_import(imp);
1653 else if (imp->match_c_string("*"))
1654 return Pointer_type::do_import(imp);
1655 else if (imp->match_c_string("struct "))
1656 return Struct_type::do_import(imp);
1657 else if (imp->match_c_string("["))
1658 return Array_type::do_import(imp);
1659 else if (imp->match_c_string("map "))
1660 return Map_type::do_import(imp);
1661 else if (imp->match_c_string("chan "))
1662 return Channel_type::do_import(imp);
1663 else if (imp->match_c_string("interface"))
1664 return Interface_type::do_import(imp);
1667 error_at(imp->location(), "import error: expected type");
1668 return Type::make_error_type();
1672 // A type used to indicate a parsing error. This exists to simplify
1673 // later error detection.
1675 class Error_type : public Type
1684 do_get_backend(Gogo* gogo)
1685 { return gogo->backend()->error_type(); }
1688 do_type_descriptor(Gogo*, Named_type*)
1689 { return Expression::make_error(Linemap::predeclared_location()); }
1692 do_reflection(Gogo*, std::string*) const
1693 { go_assert(saw_errors()); }
1696 do_mangled_name(Gogo*, std::string* ret) const
1697 { ret->push_back('E'); }
1701 Type::make_error_type()
1703 static Error_type singleton_error_type;
1704 return &singleton_error_type;
1709 class Void_type : public Type
1718 do_get_backend(Gogo* gogo)
1719 { return gogo->backend()->void_type(); }
1722 do_type_descriptor(Gogo*, Named_type*)
1723 { go_unreachable(); }
1726 do_reflection(Gogo*, std::string*) const
1730 do_mangled_name(Gogo*, std::string* ret) const
1731 { ret->push_back('v'); }
1735 Type::make_void_type()
1737 static Void_type singleton_void_type;
1738 return &singleton_void_type;
1741 // The boolean type.
1743 class Boolean_type : public Type
1747 : Type(TYPE_BOOLEAN)
1752 do_get_backend(Gogo* gogo)
1753 { return gogo->backend()->bool_type(); }
1756 do_type_descriptor(Gogo*, Named_type* name);
1758 // We should not be asked for the reflection string of a basic type.
1760 do_reflection(Gogo*, std::string* ret) const
1761 { ret->append("bool"); }
1764 do_mangled_name(Gogo*, std::string* ret) const
1765 { ret->push_back('b'); }
1768 // Make the type descriptor.
1771 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
1774 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
1777 Named_object* no = gogo->lookup_global("bool");
1778 go_assert(no != NULL);
1779 return Type::type_descriptor(gogo, no->type_value());
1784 Type::make_boolean_type()
1786 static Boolean_type boolean_type;
1787 return &boolean_type;
1790 // The named type "bool".
1792 static Named_type* named_bool_type;
1794 // Get the named type "bool".
1797 Type::lookup_bool_type()
1799 return named_bool_type;
1802 // Make the named type "bool".
1805 Type::make_named_bool_type()
1807 Type* bool_type = Type::make_boolean_type();
1808 Named_object* named_object =
1809 Named_object::make_type("bool", NULL, bool_type,
1810 Linemap::predeclared_location());
1811 Named_type* named_type = named_object->type_value();
1812 named_bool_type = named_type;
1816 // Class Integer_type.
1818 Integer_type::Named_integer_types Integer_type::named_integer_types;
1820 // Create a new integer type. Non-abstract integer types always have
1824 Integer_type::create_integer_type(const char* name, bool is_unsigned,
1825 int bits, int runtime_type_kind)
1827 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
1829 std::string sname(name);
1830 Named_object* named_object =
1831 Named_object::make_type(sname, NULL, integer_type,
1832 Linemap::predeclared_location());
1833 Named_type* named_type = named_object->type_value();
1834 std::pair<Named_integer_types::iterator, bool> ins =
1835 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
1836 go_assert(ins.second);
1840 // Look up an existing integer type.
1843 Integer_type::lookup_integer_type(const char* name)
1845 Named_integer_types::const_iterator p =
1846 Integer_type::named_integer_types.find(name);
1847 go_assert(p != Integer_type::named_integer_types.end());
1851 // Create a new abstract integer type.
1854 Integer_type::create_abstract_integer_type()
1856 static Integer_type* abstract_type;
1857 if (abstract_type == NULL)
1858 abstract_type = new Integer_type(true, false, INT_TYPE_SIZE,
1859 RUNTIME_TYPE_KIND_INT);
1860 return abstract_type;
1863 // Integer type compatibility.
1866 Integer_type::is_identical(const Integer_type* t) const
1868 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
1870 return this->is_abstract_ == t->is_abstract_;
1876 Integer_type::do_hash_for_method(Gogo*) const
1878 return ((this->bits_ << 4)
1879 + ((this->is_unsigned_ ? 1 : 0) << 8)
1880 + ((this->is_abstract_ ? 1 : 0) << 9));
1883 // Convert an Integer_type to the backend representation.
1886 Integer_type::do_get_backend(Gogo* gogo)
1888 if (this->is_abstract_)
1890 go_assert(saw_errors());
1891 return gogo->backend()->error_type();
1893 return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
1896 // The type descriptor for an integer type. Integer types are always
1900 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
1902 go_assert(name != NULL);
1903 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
1906 // We should not be asked for the reflection string of a basic type.
1909 Integer_type::do_reflection(Gogo*, std::string*) const
1911 go_assert(saw_errors());
1917 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
1920 snprintf(buf, sizeof buf, "i%s%s%de",
1921 this->is_abstract_ ? "a" : "",
1922 this->is_unsigned_ ? "u" : "",
1927 // Make an integer type.
1930 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
1931 int runtime_type_kind)
1933 return Integer_type::create_integer_type(name, is_unsigned, bits,
1937 // Make an abstract integer type.
1940 Type::make_abstract_integer_type()
1942 return Integer_type::create_abstract_integer_type();
1945 // Look up an integer type.
1948 Type::lookup_integer_type(const char* name)
1950 return Integer_type::lookup_integer_type(name);
1953 // Class Float_type.
1955 Float_type::Named_float_types Float_type::named_float_types;
1957 // Create a new float type. Non-abstract float types always have
1961 Float_type::create_float_type(const char* name, int bits,
1962 int runtime_type_kind)
1964 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
1965 std::string sname(name);
1966 Named_object* named_object =
1967 Named_object::make_type(sname, NULL, float_type,
1968 Linemap::predeclared_location());
1969 Named_type* named_type = named_object->type_value();
1970 std::pair<Named_float_types::iterator, bool> ins =
1971 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
1972 go_assert(ins.second);
1976 // Look up an existing float type.
1979 Float_type::lookup_float_type(const char* name)
1981 Named_float_types::const_iterator p =
1982 Float_type::named_float_types.find(name);
1983 go_assert(p != Float_type::named_float_types.end());
1987 // Create a new abstract float type.
1990 Float_type::create_abstract_float_type()
1992 static Float_type* abstract_type;
1993 if (abstract_type == NULL)
1994 abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
1995 return abstract_type;
1998 // Whether this type is identical with T.
2001 Float_type::is_identical(const Float_type* t) const
2003 if (this->bits_ != t->bits_)
2005 return this->is_abstract_ == t->is_abstract_;
2011 Float_type::do_hash_for_method(Gogo*) const
2013 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2016 // Convert to the backend representation.
2019 Float_type::do_get_backend(Gogo* gogo)
2021 return gogo->backend()->float_type(this->bits_);
2024 // The type descriptor for a float type. Float types are always named.
2027 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2029 go_assert(name != NULL);
2030 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2033 // We should not be asked for the reflection string of a basic type.
2036 Float_type::do_reflection(Gogo*, std::string*) const
2038 go_assert(saw_errors());
2044 Float_type::do_mangled_name(Gogo*, std::string* ret) const
2047 snprintf(buf, sizeof buf, "f%s%de",
2048 this->is_abstract_ ? "a" : "",
2053 // Make a floating point type.
2056 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
2058 return Float_type::create_float_type(name, bits, runtime_type_kind);
2061 // Make an abstract float type.
2064 Type::make_abstract_float_type()
2066 return Float_type::create_abstract_float_type();
2069 // Look up a float type.
2072 Type::lookup_float_type(const char* name)
2074 return Float_type::lookup_float_type(name);
2077 // Class Complex_type.
2079 Complex_type::Named_complex_types Complex_type::named_complex_types;
2081 // Create a new complex type. Non-abstract complex types always have
2085 Complex_type::create_complex_type(const char* name, int bits,
2086 int runtime_type_kind)
2088 Complex_type* complex_type = new Complex_type(false, bits,
2090 std::string sname(name);
2091 Named_object* named_object =
2092 Named_object::make_type(sname, NULL, complex_type,
2093 Linemap::predeclared_location());
2094 Named_type* named_type = named_object->type_value();
2095 std::pair<Named_complex_types::iterator, bool> ins =
2096 Complex_type::named_complex_types.insert(std::make_pair(sname,
2098 go_assert(ins.second);
2102 // Look up an existing complex type.
2105 Complex_type::lookup_complex_type(const char* name)
2107 Named_complex_types::const_iterator p =
2108 Complex_type::named_complex_types.find(name);
2109 go_assert(p != Complex_type::named_complex_types.end());
2113 // Create a new abstract complex type.
2116 Complex_type::create_abstract_complex_type()
2118 static Complex_type* abstract_type;
2119 if (abstract_type == NULL)
2120 abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
2121 return abstract_type;
2124 // Whether this type is identical with T.
2127 Complex_type::is_identical(const Complex_type *t) const
2129 if (this->bits_ != t->bits_)
2131 return this->is_abstract_ == t->is_abstract_;
2137 Complex_type::do_hash_for_method(Gogo*) const
2139 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2142 // Convert to the backend representation.
2145 Complex_type::do_get_backend(Gogo* gogo)
2147 return gogo->backend()->complex_type(this->bits_);
2150 // The type descriptor for a complex type. Complex types are always
2154 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2156 go_assert(name != NULL);
2157 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2160 // We should not be asked for the reflection string of a basic type.
2163 Complex_type::do_reflection(Gogo*, std::string*) const
2165 go_assert(saw_errors());
2171 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2174 snprintf(buf, sizeof buf, "c%s%de",
2175 this->is_abstract_ ? "a" : "",
2180 // Make a complex type.
2183 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2185 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2188 // Make an abstract complex type.
2191 Type::make_abstract_complex_type()
2193 return Complex_type::create_abstract_complex_type();
2196 // Look up a complex type.
2199 Type::lookup_complex_type(const char* name)
2201 return Complex_type::lookup_complex_type(name);
2204 // Class String_type.
2206 // Convert String_type to the backend representation. A string is a
2207 // struct with two fields: a pointer to the characters and a length.
2210 String_type::do_get_backend(Gogo* gogo)
2212 static Btype* backend_string_type;
2213 if (backend_string_type == NULL)
2215 std::vector<Backend::Btyped_identifier> fields(2);
2217 Type* b = gogo->lookup_global("byte")->type_value();
2218 Type* pb = Type::make_pointer_type(b);
2219 fields[0].name = "__data";
2220 fields[0].btype = pb->get_backend(gogo);
2221 fields[0].location = Linemap::predeclared_location();
2223 Type* int_type = Type::lookup_integer_type("int");
2224 fields[1].name = "__length";
2225 fields[1].btype = int_type->get_backend(gogo);
2226 fields[1].location = fields[0].location;
2228 backend_string_type = gogo->backend()->struct_type(fields);
2230 return backend_string_type;
2233 // Return a tree for the length of STRING.
2236 String_type::length_tree(Gogo*, tree string)
2238 tree string_type = TREE_TYPE(string);
2239 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2240 tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2241 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2243 return fold_build3(COMPONENT_REF, integer_type_node, string,
2244 length_field, NULL_TREE);
2247 // Return a tree for a pointer to the bytes of STRING.
2250 String_type::bytes_tree(Gogo*, tree string)
2252 tree string_type = TREE_TYPE(string);
2253 go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2254 tree bytes_field = TYPE_FIELDS(string_type);
2255 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
2257 return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
2258 bytes_field, NULL_TREE);
2261 // The type descriptor for the string type.
2264 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2267 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
2270 Named_object* no = gogo->lookup_global("string");
2271 go_assert(no != NULL);
2272 return Type::type_descriptor(gogo, no->type_value());
2276 // We should not be asked for the reflection string of a basic type.
2279 String_type::do_reflection(Gogo*, std::string* ret) const
2281 ret->append("string");
2284 // Mangled name of a string type.
2287 String_type::do_mangled_name(Gogo*, std::string* ret) const
2289 ret->push_back('z');
2292 // Make a string type.
2295 Type::make_string_type()
2297 static String_type string_type;
2298 return &string_type;
2301 // The named type "string".
2303 static Named_type* named_string_type;
2305 // Get the named type "string".
2308 Type::lookup_string_type()
2310 return named_string_type;
2313 // Make the named type string.
2316 Type::make_named_string_type()
2318 Type* string_type = Type::make_string_type();
2319 Named_object* named_object =
2320 Named_object::make_type("string", NULL, string_type,
2321 Linemap::predeclared_location());
2322 Named_type* named_type = named_object->type_value();
2323 named_string_type = named_type;
2327 // The sink type. This is the type of the blank identifier _. Any
2328 // type may be assigned to it.
2330 class Sink_type : public Type
2339 do_get_backend(Gogo*)
2340 { go_unreachable(); }
2343 do_type_descriptor(Gogo*, Named_type*)
2344 { go_unreachable(); }
2347 do_reflection(Gogo*, std::string*) const
2348 { go_unreachable(); }
2351 do_mangled_name(Gogo*, std::string*) const
2352 { go_unreachable(); }
2355 // Make the sink type.
2358 Type::make_sink_type()
2360 static Sink_type sink_type;
2364 // Class Function_type.
2369 Function_type::do_traverse(Traverse* traverse)
2371 if (this->receiver_ != NULL
2372 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
2373 return TRAVERSE_EXIT;
2374 if (this->parameters_ != NULL
2375 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
2376 return TRAVERSE_EXIT;
2377 if (this->results_ != NULL
2378 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
2379 return TRAVERSE_EXIT;
2380 return TRAVERSE_CONTINUE;
2383 // Returns whether T is a valid redeclaration of this type. If this
2384 // returns false, and REASON is not NULL, *REASON may be set to a
2385 // brief explanation of why it returned false.
2388 Function_type::is_valid_redeclaration(const Function_type* t,
2389 std::string* reason) const
2391 if (!this->is_identical(t, false, true, reason))
2394 // A redeclaration of a function is required to use the same names
2395 // for the receiver and parameters.
2396 if (this->receiver() != NULL
2397 && this->receiver()->name() != t->receiver()->name()
2398 && this->receiver()->name() != Import::import_marker
2399 && t->receiver()->name() != Import::import_marker)
2402 *reason = "receiver name changed";
2406 const Typed_identifier_list* parms1 = this->parameters();
2407 const Typed_identifier_list* parms2 = t->parameters();
2410 Typed_identifier_list::const_iterator p1 = parms1->begin();
2411 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2412 p2 != parms2->end();
2415 if (p1->name() != p2->name()
2416 && p1->name() != Import::import_marker
2417 && p2->name() != Import::import_marker)
2420 *reason = "parameter name changed";
2424 // This is called at parse time, so we may have unknown
2426 Type* t1 = p1->type()->forwarded();
2427 Type* t2 = p2->type()->forwarded();
2429 && t1->forward_declaration_type() != NULL
2430 && (t2->forward_declaration_type() == NULL
2431 || (t1->forward_declaration_type()->named_object()
2432 != t2->forward_declaration_type()->named_object())))
2437 const Typed_identifier_list* results1 = this->results();
2438 const Typed_identifier_list* results2 = t->results();
2439 if (results1 != NULL)
2441 Typed_identifier_list::const_iterator res1 = results1->begin();
2442 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2443 res2 != results2->end();
2446 if (res1->name() != res2->name()
2447 && res1->name() != Import::import_marker
2448 && res2->name() != Import::import_marker)
2451 *reason = "result name changed";
2455 // This is called at parse time, so we may have unknown
2457 Type* t1 = res1->type()->forwarded();
2458 Type* t2 = res2->type()->forwarded();
2460 && t1->forward_declaration_type() != NULL
2461 && (t2->forward_declaration_type() == NULL
2462 || (t1->forward_declaration_type()->named_object()
2463 != t2->forward_declaration_type()->named_object())))
2471 // Check whether T is the same as this type.
2474 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
2475 bool errors_are_identical,
2476 std::string* reason) const
2478 if (!ignore_receiver)
2480 const Typed_identifier* r1 = this->receiver();
2481 const Typed_identifier* r2 = t->receiver();
2482 if ((r1 != NULL) != (r2 != NULL))
2485 *reason = _("different receiver types");
2490 if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
2493 if (reason != NULL && !reason->empty())
2494 *reason = "receiver: " + *reason;
2500 const Typed_identifier_list* parms1 = this->parameters();
2501 const Typed_identifier_list* parms2 = t->parameters();
2502 if ((parms1 != NULL) != (parms2 != NULL))
2505 *reason = _("different number of parameters");
2510 Typed_identifier_list::const_iterator p1 = parms1->begin();
2511 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2512 p2 != parms2->end();
2515 if (p1 == parms1->end())
2518 *reason = _("different number of parameters");
2522 if (!Type::are_identical(p1->type(), p2->type(),
2523 errors_are_identical, NULL))
2526 *reason = _("different parameter types");
2530 if (p1 != parms1->end())
2533 *reason = _("different number of parameters");
2538 if (this->is_varargs() != t->is_varargs())
2541 *reason = _("different varargs");
2545 const Typed_identifier_list* results1 = this->results();
2546 const Typed_identifier_list* results2 = t->results();
2547 if ((results1 != NULL) != (results2 != NULL))
2550 *reason = _("different number of results");
2553 if (results1 != NULL)
2555 Typed_identifier_list::const_iterator res1 = results1->begin();
2556 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2557 res2 != results2->end();
2560 if (res1 == results1->end())
2563 *reason = _("different number of results");
2567 if (!Type::are_identical(res1->type(), res2->type(),
2568 errors_are_identical, NULL))
2571 *reason = _("different result types");
2575 if (res1 != results1->end())
2578 *reason = _("different number of results");
2589 Function_type::do_hash_for_method(Gogo* gogo) const
2591 unsigned int ret = 0;
2592 // We ignore the receiver type for hash codes, because we need to
2593 // get the same hash code for a method in an interface and a method
2594 // declared for a type. The former will not have a receiver.
2595 if (this->parameters_ != NULL)
2598 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
2599 p != this->parameters_->end();
2601 ret += p->type()->hash_for_method(gogo) << shift;
2603 if (this->results_ != NULL)
2606 for (Typed_identifier_list::const_iterator p = this->results_->begin();
2607 p != this->results_->end();
2609 ret += p->type()->hash_for_method(gogo) << shift;
2611 if (this->is_varargs_)
2617 // Get the backend representation for a function type.
2620 Function_type::get_function_backend(Gogo* gogo)
2622 Backend::Btyped_identifier breceiver;
2623 if (this->receiver_ != NULL)
2625 breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
2627 // We always pass the address of the receiver parameter, in
2628 // order to make interface calls work with unknown types.
2629 Type* rtype = this->receiver_->type();
2630 if (rtype->points_to() == NULL)
2631 rtype = Type::make_pointer_type(rtype);
2632 breceiver.btype = rtype->get_backend(gogo);
2633 breceiver.location = this->receiver_->location();
2636 std::vector<Backend::Btyped_identifier> bparameters;
2637 if (this->parameters_ != NULL)
2639 bparameters.resize(this->parameters_->size());
2641 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
2642 p != this->parameters_->end();
2645 bparameters[i].name = Gogo::unpack_hidden_name(p->name());
2646 bparameters[i].btype = p->type()->get_backend(gogo);
2647 bparameters[i].location = p->location();
2649 go_assert(i == bparameters.size());
2652 std::vector<Backend::Btyped_identifier> bresults;
2653 if (this->results_ != NULL)
2655 bresults.resize(this->results_->size());
2657 for (Typed_identifier_list::const_iterator p = this->results_->begin();
2658 p != this->results_->end();
2661 bresults[i].name = Gogo::unpack_hidden_name(p->name());
2662 bresults[i].btype = p->type()->get_backend(gogo);
2663 bresults[i].location = p->location();
2665 go_assert(i == bresults.size());
2668 return gogo->backend()->function_type(breceiver, bparameters, bresults,
2672 // A hash table mapping function types to their backend placeholders.
2674 Function_type::Placeholders Function_type::placeholders;
2676 // Get the backend representation for a function type. If we are
2677 // still converting types, and this types has multiple results, return
2678 // a placeholder instead. We do this because for multiple results we
2679 // build a struct, and we need to make sure that all the types in the
2680 // struct are valid before we create the struct.
2683 Function_type::do_get_backend(Gogo* gogo)
2685 if (!gogo->named_types_are_converted()
2686 && this->results_ != NULL
2687 && this->results_->size() > 1)
2689 Btype* placeholder =
2690 gogo->backend()->placeholder_pointer_type("", this->location(), true);
2691 Function_type::placeholders.push_back(std::make_pair(this, placeholder));
2694 return this->get_function_backend(gogo);
2697 // Convert function types after all named types are converted.
2700 Function_type::convert_types(Gogo* gogo)
2702 for (Placeholders::const_iterator p = Function_type::placeholders.begin();
2703 p != Function_type::placeholders.end();
2706 Btype* bt = p->first->get_function_backend(gogo);
2707 if (!gogo->backend()->set_placeholder_function_type(p->second, bt))
2708 go_assert(saw_errors());
2712 // The type of a function type descriptor.
2715 Function_type::make_function_type_descriptor_type()
2720 Type* tdt = Type::make_type_descriptor_type();
2721 Type* ptdt = Type::make_type_descriptor_ptr_type();
2723 Type* bool_type = Type::lookup_bool_type();
2725 Type* slice_type = Type::make_array_type(ptdt, NULL);
2727 Struct_type* s = Type::make_builtin_struct_type(4,
2729 "dotdotdot", bool_type,
2733 ret = Type::make_builtin_named_type("FuncType", s);
2739 // The type descriptor for a function type.
2742 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2744 Location bloc = Linemap::predeclared_location();
2746 Type* ftdt = Function_type::make_function_type_descriptor_type();
2748 const Struct_field_list* fields = ftdt->struct_type()->fields();
2750 Expression_list* vals = new Expression_list();
2753 Struct_field_list::const_iterator p = fields->begin();
2754 go_assert(p->is_field_name("commonType"));
2755 vals->push_back(this->type_descriptor_constructor(gogo,
2756 RUNTIME_TYPE_KIND_FUNC,
2760 go_assert(p->is_field_name("dotdotdot"));
2761 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
2764 go_assert(p->is_field_name("in"));
2765 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
2766 this->parameters()));
2769 go_assert(p->is_field_name("out"));
2770 vals->push_back(this->type_descriptor_params(p->type(), NULL,
2774 go_assert(p == fields->end());
2776 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
2779 // Return a composite literal for the parameters or results of a type
2783 Function_type::type_descriptor_params(Type* params_type,
2784 const Typed_identifier* receiver,
2785 const Typed_identifier_list* params)
2787 Location bloc = Linemap::predeclared_location();
2789 if (receiver == NULL && params == NULL)
2790 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
2792 Expression_list* vals = new Expression_list();
2793 vals->reserve((params == NULL ? 0 : params->size())
2794 + (receiver != NULL ? 1 : 0));
2796 if (receiver != NULL)
2797 vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
2801 for (Typed_identifier_list::const_iterator p = params->begin();
2804 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
2807 return Expression::make_slice_composite_literal(params_type, vals, bloc);
2810 // The reflection string.
2813 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
2815 // FIXME: Turn this off until we straighten out the type of the
2816 // struct field used in a go statement which calls a method.
2817 // go_assert(this->receiver_ == NULL);
2819 ret->append("func");
2821 if (this->receiver_ != NULL)
2823 ret->push_back('(');
2824 this->append_reflection(this->receiver_->type(), gogo, ret);
2825 ret->push_back(')');
2828 ret->push_back('(');
2829 const Typed_identifier_list* params = this->parameters();
2832 bool is_varargs = this->is_varargs_;
2833 for (Typed_identifier_list::const_iterator p = params->begin();
2837 if (p != params->begin())
2839 if (!is_varargs || p + 1 != params->end())
2840 this->append_reflection(p->type(), gogo, ret);
2844 this->append_reflection(p->type()->array_type()->element_type(),
2849 ret->push_back(')');
2851 const Typed_identifier_list* results = this->results();
2852 if (results != NULL && !results->empty())
2854 if (results->size() == 1)
2855 ret->push_back(' ');
2858 for (Typed_identifier_list::const_iterator p = results->begin();
2859 p != results->end();
2862 if (p != results->begin())
2864 this->append_reflection(p->type(), gogo, ret);
2866 if (results->size() > 1)
2867 ret->push_back(')');
2874 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
2876 ret->push_back('F');
2878 if (this->receiver_ != NULL)
2880 ret->push_back('m');
2881 this->append_mangled_name(this->receiver_->type(), gogo, ret);
2884 const Typed_identifier_list* params = this->parameters();
2887 ret->push_back('p');
2888 for (Typed_identifier_list::const_iterator p = params->begin();
2891 this->append_mangled_name(p->type(), gogo, ret);
2892 if (this->is_varargs_)
2893 ret->push_back('V');
2894 ret->push_back('e');
2897 const Typed_identifier_list* results = this->results();
2898 if (results != NULL)
2900 ret->push_back('r');
2901 for (Typed_identifier_list::const_iterator p = results->begin();
2902 p != results->end();
2904 this->append_mangled_name(p->type(), gogo, ret);
2905 ret->push_back('e');
2908 ret->push_back('e');
2911 // Export a function type.
2914 Function_type::do_export(Export* exp) const
2916 // We don't write out the receiver. The only function types which
2917 // should have a receiver are the ones associated with explicitly
2918 // defined methods. For those the receiver type is written out by
2919 // Function::export_func.
2921 exp->write_c_string("(");
2923 if (this->parameters_ != NULL)
2925 bool is_varargs = this->is_varargs_;
2926 for (Typed_identifier_list::const_iterator p =
2927 this->parameters_->begin();
2928 p != this->parameters_->end();
2934 exp->write_c_string(", ");
2935 if (!is_varargs || p + 1 != this->parameters_->end())
2936 exp->write_type(p->type());
2939 exp->write_c_string("...");
2940 exp->write_type(p->type()->array_type()->element_type());
2944 exp->write_c_string(")");
2946 const Typed_identifier_list* results = this->results_;
2947 if (results != NULL)
2949 exp->write_c_string(" ");
2950 if (results->size() == 1)
2951 exp->write_type(results->begin()->type());
2955 exp->write_c_string("(");
2956 for (Typed_identifier_list::const_iterator p = results->begin();
2957 p != results->end();
2963 exp->write_c_string(", ");
2964 exp->write_type(p->type());
2966 exp->write_c_string(")");
2971 // Import a function type.
2974 Function_type::do_import(Import* imp)
2976 imp->require_c_string("(");
2977 Typed_identifier_list* parameters;
2978 bool is_varargs = false;
2979 if (imp->peek_char() == ')')
2983 parameters = new Typed_identifier_list();
2986 if (imp->match_c_string("..."))
2992 Type* ptype = imp->read_type();
2994 ptype = Type::make_array_type(ptype, NULL);
2995 parameters->push_back(Typed_identifier(Import::import_marker,
2996 ptype, imp->location()));
2997 if (imp->peek_char() != ',')
2999 go_assert(!is_varargs);
3000 imp->require_c_string(", ");
3003 imp->require_c_string(")");
3005 Typed_identifier_list* results;
3006 if (imp->peek_char() != ' ')
3011 results = new Typed_identifier_list;
3012 if (imp->peek_char() != '(')
3014 Type* rtype = imp->read_type();
3015 results->push_back(Typed_identifier(Import::import_marker, rtype,
3023 Type* rtype = imp->read_type();
3024 results->push_back(Typed_identifier(Import::import_marker,
3025 rtype, imp->location()));
3026 if (imp->peek_char() != ',')
3028 imp->require_c_string(", ");
3030 imp->require_c_string(")");
3034 Function_type* ret = Type::make_function_type(NULL, parameters, results,
3037 ret->set_is_varargs();
3041 // Make a copy of a function type without a receiver.
3044 Function_type::copy_without_receiver() const
3046 go_assert(this->is_method());
3047 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
3050 if (this->is_varargs())
3051 ret->set_is_varargs();
3052 if (this->is_builtin())
3053 ret->set_is_builtin();
3057 // Make a copy of a function type with a receiver.
3060 Function_type::copy_with_receiver(Type* receiver_type) const
3062 go_assert(!this->is_method());
3063 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3065 return Type::make_function_type(receiver, this->parameters_,
3066 this->results_, this->location_);
3069 // Make a function type.
3072 Type::make_function_type(Typed_identifier* receiver,
3073 Typed_identifier_list* parameters,
3074 Typed_identifier_list* results,
3077 return new Function_type(receiver, parameters, results, location);
3080 // Class Pointer_type.
3085 Pointer_type::do_traverse(Traverse* traverse)
3087 return Type::traverse(this->to_type_, traverse);
3093 Pointer_type::do_hash_for_method(Gogo* gogo) const
3095 return this->to_type_->hash_for_method(gogo) << 4;
3098 // The tree for a pointer type.
3101 Pointer_type::do_get_backend(Gogo* gogo)
3103 Btype* to_btype = this->to_type_->get_backend(gogo);
3104 return gogo->backend()->pointer_type(to_btype);
3107 // The type of a pointer type descriptor.
3110 Pointer_type::make_pointer_type_descriptor_type()
3115 Type* tdt = Type::make_type_descriptor_type();
3116 Type* ptdt = Type::make_type_descriptor_ptr_type();
3118 Struct_type* s = Type::make_builtin_struct_type(2,
3122 ret = Type::make_builtin_named_type("PtrType", s);
3128 // The type descriptor for a pointer type.
3131 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3133 if (this->is_unsafe_pointer_type())
3135 go_assert(name != NULL);
3136 return this->plain_type_descriptor(gogo,
3137 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
3142 Location bloc = Linemap::predeclared_location();
3144 const Methods* methods;
3145 Type* deref = this->points_to();
3146 if (deref->named_type() != NULL)
3147 methods = deref->named_type()->methods();
3148 else if (deref->struct_type() != NULL)
3149 methods = deref->struct_type()->methods();
3153 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
3155 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
3157 Expression_list* vals = new Expression_list();
3160 Struct_field_list::const_iterator p = fields->begin();
3161 go_assert(p->is_field_name("commonType"));
3162 vals->push_back(this->type_descriptor_constructor(gogo,
3163 RUNTIME_TYPE_KIND_PTR,
3164 name, methods, false));
3167 go_assert(p->is_field_name("elem"));
3168 vals->push_back(Expression::make_type_descriptor(deref, bloc));
3170 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
3174 // Reflection string.
3177 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
3179 ret->push_back('*');
3180 this->append_reflection(this->to_type_, gogo, ret);
3186 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3188 ret->push_back('p');
3189 this->append_mangled_name(this->to_type_, gogo, ret);
3195 Pointer_type::do_export(Export* exp) const
3197 exp->write_c_string("*");
3198 if (this->is_unsafe_pointer_type())
3199 exp->write_c_string("any");
3201 exp->write_type(this->to_type_);
3207 Pointer_type::do_import(Import* imp)
3209 imp->require_c_string("*");
3210 if (imp->match_c_string("any"))
3213 return Type::make_pointer_type(Type::make_void_type());
3215 Type* to = imp->read_type();
3216 return Type::make_pointer_type(to);
3219 // Make a pointer type.
3222 Type::make_pointer_type(Type* to_type)
3224 typedef Unordered_map(Type*, Pointer_type*) Hashtable;
3225 static Hashtable pointer_types;
3226 Hashtable::const_iterator p = pointer_types.find(to_type);
3227 if (p != pointer_types.end())
3229 Pointer_type* ret = new Pointer_type(to_type);
3230 pointer_types[to_type] = ret;
3234 // The nil type. We use a special type for nil because it is not the
3235 // same as any other type. In C term nil has type void*, but there is
3236 // no such type in Go.
3238 class Nil_type : public Type
3247 do_get_backend(Gogo* gogo)
3248 { return gogo->backend()->pointer_type(gogo->backend()->void_type()); }
3251 do_type_descriptor(Gogo*, Named_type*)
3252 { go_unreachable(); }
3255 do_reflection(Gogo*, std::string*) const
3256 { go_unreachable(); }
3259 do_mangled_name(Gogo*, std::string* ret) const
3260 { ret->push_back('n'); }
3263 // Make the nil type.
3266 Type::make_nil_type()
3268 static Nil_type singleton_nil_type;
3269 return &singleton_nil_type;
3272 // The type of a function call which returns multiple values. This is
3273 // really a struct, but we don't want to confuse a function call which
3274 // returns a struct with a function call which returns multiple
3277 class Call_multiple_result_type : public Type
3280 Call_multiple_result_type(Call_expression* call)
3281 : Type(TYPE_CALL_MULTIPLE_RESULT),
3287 do_has_pointer() const
3289 go_assert(saw_errors());
3294 do_get_backend(Gogo* gogo)
3296 go_assert(saw_errors());
3297 return gogo->backend()->error_type();
3301 do_type_descriptor(Gogo*, Named_type*)
3303 go_assert(saw_errors());
3304 return Expression::make_error(Linemap::unknown_location());
3308 do_reflection(Gogo*, std::string*) const
3309 { go_assert(saw_errors()); }
3312 do_mangled_name(Gogo*, std::string*) const
3313 { go_assert(saw_errors()); }
3316 // The expression being called.
3317 Call_expression* call_;
3320 // Make a call result type.
3323 Type::make_call_multiple_result_type(Call_expression* call)
3325 return new Call_multiple_result_type(call);
3328 // Class Struct_field.
3330 // Get the name of a field.
3333 Struct_field::field_name() const
3335 const std::string& name(this->typed_identifier_.name());
3340 // This is called during parsing, before anything is lowered, so
3341 // we have to be pretty careful to avoid dereferencing an
3342 // unknown type name.
3343 Type* t = this->typed_identifier_.type();
3345 if (t->classification() == Type::TYPE_POINTER)
3348 Pointer_type* ptype = static_cast<Pointer_type*>(t);
3349 dt = ptype->points_to();
3351 if (dt->forward_declaration_type() != NULL)
3352 return dt->forward_declaration_type()->name();
3353 else if (dt->named_type() != NULL)
3354 return dt->named_type()->name();
3355 else if (t->is_error_type() || dt->is_error_type())
3357 static const std::string error_string = "*error*";
3358 return error_string;
3362 // Avoid crashing in the erroneous case where T is named but
3365 if (t->forward_declaration_type() != NULL)
3366 return t->forward_declaration_type()->name();
3367 else if (t->named_type() != NULL)
3368 return t->named_type()->name();
3375 // Return whether this field is named NAME.
3378 Struct_field::is_field_name(const std::string& name) const
3380 const std::string& me(this->typed_identifier_.name());
3385 Type* t = this->typed_identifier_.type();
3386 if (t->points_to() != NULL)
3388 Named_type* nt = t->named_type();
3389 if (nt != NULL && nt->name() == name)
3392 // This is a horrible hack caused by the fact that we don't pack
3393 // the names of builtin types. FIXME.
3396 && nt->name() == Gogo::unpack_hidden_name(name))
3403 // Class Struct_type.
3408 Struct_type::do_traverse(Traverse* traverse)
3410 Struct_field_list* fields = this->fields_;
3413 for (Struct_field_list::iterator p = fields->begin();
3417 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
3418 return TRAVERSE_EXIT;
3421 return TRAVERSE_CONTINUE;
3424 // Verify that the struct type is complete and valid.
3427 Struct_type::do_verify()
3429 Struct_field_list* fields = this->fields_;
3433 for (Struct_field_list::iterator p = fields->begin();
3437 Type* t = p->type();
3438 if (t->is_undefined())
3440 error_at(p->location(), "struct field type is incomplete");
3441 p->set_type(Type::make_error_type());
3444 else if (p->is_anonymous())
3446 if (t->named_type() != NULL && t->points_to() != NULL)
3448 error_at(p->location(), "embedded type may not be a pointer");
3449 p->set_type(Type::make_error_type());
3452 if (t->points_to() != NULL
3453 && t->points_to()->interface_type() != NULL)
3455 error_at(p->location(),
3456 "embedded type may not be pointer to interface");
3457 p->set_type(Type::make_error_type());
3465 // Whether this contains a pointer.
3468 Struct_type::do_has_pointer() const
3470 const Struct_field_list* fields = this->fields();
3473 for (Struct_field_list::const_iterator p = fields->begin();
3477 if (p->type()->has_pointer())
3483 // Whether this type is identical to T.
3486 Struct_type::is_identical(const Struct_type* t,
3487 bool errors_are_identical) const
3489 const Struct_field_list* fields1 = this->fields();
3490 const Struct_field_list* fields2 = t->fields();
3491 if (fields1 == NULL || fields2 == NULL)
3492 return fields1 == fields2;
3493 Struct_field_list::const_iterator pf2 = fields2->begin();
3494 for (Struct_field_list::const_iterator pf1 = fields1->begin();
3495 pf1 != fields1->end();
3498 if (pf2 == fields2->end())
3500 if (pf1->field_name() != pf2->field_name())
3502 if (pf1->is_anonymous() != pf2->is_anonymous()
3503 || !Type::are_identical(pf1->type(), pf2->type(),
3504 errors_are_identical, NULL))
3506 if (!pf1->has_tag())
3513 if (!pf2->has_tag())
3515 if (pf1->tag() != pf2->tag())
3519 if (pf2 != fields2->end())
3524 // Whether this struct type has any hidden fields.
3527 Struct_type::struct_has_hidden_fields(const Named_type* within,
3528 std::string* reason) const
3530 const Struct_field_list* fields = this->fields();
3533 const Package* within_package = (within == NULL
3535 : within->named_object()->package());
3536 for (Struct_field_list::const_iterator pf = fields->begin();
3537 pf != fields->end();
3540 if (within_package != NULL
3541 && !pf->is_anonymous()
3542 && Gogo::is_hidden_name(pf->field_name()))
3546 std::string within_name = within->named_object()->message_name();
3547 std::string name = Gogo::message_name(pf->field_name());
3548 size_t bufsize = 200 + within_name.length() + name.length();
3549 char* buf = new char[bufsize];
3550 snprintf(buf, bufsize,
3551 _("implicit assignment of %s%s%s hidden field %s%s%s"),
3552 open_quote, within_name.c_str(), close_quote,
3553 open_quote, name.c_str(), close_quote);
3554 reason->assign(buf);
3560 if (pf->type()->has_hidden_fields(within, reason))
3570 Struct_type::do_hash_for_method(Gogo* gogo) const
3572 unsigned int ret = 0;
3573 if (this->fields() != NULL)
3575 for (Struct_field_list::const_iterator pf = this->fields()->begin();
3576 pf != this->fields()->end();
3578 ret = (ret << 1) + pf->type()->hash_for_method(gogo);
3583 // Find the local field NAME.
3586 Struct_type::find_local_field(const std::string& name,
3587 unsigned int *pindex) const
3589 const Struct_field_list* fields = this->fields_;
3593 for (Struct_field_list::const_iterator pf = fields->begin();
3594 pf != fields->end();
3597 if (pf->is_field_name(name))
3607 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
3609 Field_reference_expression*
3610 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
3611 Location location) const
3614 return this->field_reference_depth(struct_expr, name, location, NULL,
3618 // Return an expression for a field, along with the depth at which it
3621 Field_reference_expression*
3622 Struct_type::field_reference_depth(Expression* struct_expr,
3623 const std::string& name,
3625 Saw_named_type* saw,
3626 unsigned int* depth) const
3628 const Struct_field_list* fields = this->fields_;
3632 // Look for a field with this name.
3634 for (Struct_field_list::const_iterator pf = fields->begin();
3635 pf != fields->end();
3638 if (pf->is_field_name(name))
3641 return Expression::make_field_reference(struct_expr, i, location);
3645 // Look for an anonymous field which contains a field with this
3647 unsigned int found_depth = 0;
3648 Field_reference_expression* ret = NULL;
3650 for (Struct_field_list::const_iterator pf = fields->begin();
3651 pf != fields->end();
3654 if (!pf->is_anonymous())
3657 Struct_type* st = pf->type()->deref()->struct_type();
3661 Saw_named_type* hold_saw = saw;
3662 Saw_named_type saw_here;
3663 Named_type* nt = pf->type()->named_type();
3665 nt = pf->type()->deref()->named_type();
3669 for (q = saw; q != NULL; q = q->next)
3673 // If this is an error, it will be reported
3680 saw_here.next = saw;
3685 // Look for a reference using a NULL struct expression. If we
3686 // find one, fill in the struct expression with a reference to
3688 unsigned int subdepth;
3689 Field_reference_expression* sub = st->field_reference_depth(NULL, name,
3699 if (ret == NULL || subdepth < found_depth)
3704 found_depth = subdepth;
3705 Expression* here = Expression::make_field_reference(struct_expr, i,
3707 if (pf->type()->points_to() != NULL)
3708 here = Expression::make_unary(OPERATOR_MULT, here, location);
3709 while (sub->expr() != NULL)
3711 sub = sub->expr()->deref()->field_reference_expression();
3712 go_assert(sub != NULL);
3714 sub->set_struct_expression(here);
3716 else if (subdepth > found_depth)
3720 // We do not handle ambiguity here--it should be handled by
3721 // Type::bind_field_or_method.
3729 *depth = found_depth + 1;
3734 // Return the total number of fields, including embedded fields.
3737 Struct_type::total_field_count() const
3739 if (this->fields_ == NULL)
3741 unsigned int ret = 0;
3742 for (Struct_field_list::const_iterator pf = this->fields_->begin();
3743 pf != this->fields_->end();
3746 if (!pf->is_anonymous() || pf->type()->deref()->struct_type() == NULL)
3749 ret += pf->type()->struct_type()->total_field_count();
3754 // Return whether NAME is an unexported field, for better error reporting.
3757 Struct_type::is_unexported_local_field(Gogo* gogo,
3758 const std::string& name) const
3760 const Struct_field_list* fields = this->fields_;
3763 for (Struct_field_list::const_iterator pf = fields->begin();
3764 pf != fields->end();
3767 const std::string& field_name(pf->field_name());
3768 if (Gogo::is_hidden_name(field_name)
3769 && name == Gogo::unpack_hidden_name(field_name)
3770 && gogo->pack_hidden_name(name, false) != field_name)
3777 // Finalize the methods of an unnamed struct.
3780 Struct_type::finalize_methods(Gogo* gogo)
3782 if (this->all_methods_ != NULL)
3784 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
3787 // Return the method NAME, or NULL if there isn't one or if it is
3788 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
3792 Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
3794 return Type::method_function(this->all_methods_, name, is_ambiguous);
3797 // Convert struct fields to the backend representation. This is not
3798 // declared in types.h so that types.h doesn't have to #include
3802 get_backend_struct_fields(Gogo* gogo, const Struct_field_list* fields,
3803 std::vector<Backend::Btyped_identifier>* bfields)
3805 bfields->resize(fields->size());
3807 for (Struct_field_list::const_iterator p = fields->begin();
3811 (*bfields)[i].name = Gogo::unpack_hidden_name(p->field_name());
3812 (*bfields)[i].btype = p->type()->get_backend(gogo);
3813 (*bfields)[i].location = p->location();
3815 go_assert(i == fields->size());
3818 // Get the tree for a struct type.
3821 Struct_type::do_get_backend(Gogo* gogo)
3823 std::vector<Backend::Btyped_identifier> bfields;
3824 get_backend_struct_fields(gogo, this->fields_, &bfields);
3825 return gogo->backend()->struct_type(bfields);
3828 // The type of a struct type descriptor.
3831 Struct_type::make_struct_type_descriptor_type()
3836 Type* tdt = Type::make_type_descriptor_type();
3837 Type* ptdt = Type::make_type_descriptor_ptr_type();
3839 Type* uintptr_type = Type::lookup_integer_type("uintptr");
3840 Type* string_type = Type::lookup_string_type();
3841 Type* pointer_string_type = Type::make_pointer_type(string_type);
3844 Type::make_builtin_struct_type(5,
3845 "name", pointer_string_type,
3846 "pkgPath", pointer_string_type,
3848 "tag", pointer_string_type,
3849 "offset", uintptr_type);
3850 Type* nsf = Type::make_builtin_named_type("structField", sf);
3852 Type* slice_type = Type::make_array_type(nsf, NULL);
3854 Struct_type* s = Type::make_builtin_struct_type(2,
3856 "fields", slice_type);
3858 ret = Type::make_builtin_named_type("StructType", s);
3864 // Build a type descriptor for a struct type.
3867 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3869 Location bloc = Linemap::predeclared_location();
3871 Type* stdt = Struct_type::make_struct_type_descriptor_type();
3873 const Struct_field_list* fields = stdt->struct_type()->fields();
3875 Expression_list* vals = new Expression_list();
3878 const Methods* methods = this->methods();
3879 // A named struct should not have methods--the methods should attach
3880 // to the named type.
3881 go_assert(methods == NULL || name == NULL);
3883 Struct_field_list::const_iterator ps = fields->begin();
3884 go_assert(ps->is_field_name("commonType"));
3885 vals->push_back(this->type_descriptor_constructor(gogo,
3886 RUNTIME_TYPE_KIND_STRUCT,
3887 name, methods, true));
3890 go_assert(ps->is_field_name("fields"));
3892 Expression_list* elements = new Expression_list();
3893 elements->reserve(this->fields_->size());
3894 Type* element_type = ps->type()->array_type()->element_type();
3895 for (Struct_field_list::const_iterator pf = this->fields_->begin();
3896 pf != this->fields_->end();
3899 const Struct_field_list* f = element_type->struct_type()->fields();
3901 Expression_list* fvals = new Expression_list();
3904 Struct_field_list::const_iterator q = f->begin();
3905 go_assert(q->is_field_name("name"));
3906 if (pf->is_anonymous())
3907 fvals->push_back(Expression::make_nil(bloc));
3910 std::string n = Gogo::unpack_hidden_name(pf->field_name());
3911 Expression* s = Expression::make_string(n, bloc);
3912 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3916 go_assert(q->is_field_name("pkgPath"));
3917 if (!Gogo::is_hidden_name(pf->field_name()))
3918 fvals->push_back(Expression::make_nil(bloc));
3921 std::string n = Gogo::hidden_name_prefix(pf->field_name());
3922 Expression* s = Expression::make_string(n, bloc);
3923 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3927 go_assert(q->is_field_name("typ"));
3928 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
3931 go_assert(q->is_field_name("tag"));
3933 fvals->push_back(Expression::make_nil(bloc));
3936 Expression* s = Expression::make_string(pf->tag(), bloc);
3937 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3941 go_assert(q->is_field_name("offset"));
3942 fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
3944 Expression* v = Expression::make_struct_composite_literal(element_type,
3946 elements->push_back(v);
3949 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
3952 return Expression::make_struct_composite_literal(stdt, vals, bloc);
3955 // Reflection string.
3958 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
3960 ret->append("struct { ");
3962 for (Struct_field_list::const_iterator p = this->fields_->begin();
3963 p != this->fields_->end();
3966 if (p != this->fields_->begin())
3968 if (p->is_anonymous())
3969 ret->push_back('?');
3971 ret->append(Gogo::unpack_hidden_name(p->field_name()));
3972 ret->push_back(' ');
3973 this->append_reflection(p->type(), gogo, ret);
3977 const std::string& tag(p->tag());
3979 for (std::string::const_iterator p = tag.begin();
3984 ret->append("\\x00");
3985 else if (*p == '\n')
3987 else if (*p == '\t')
3990 ret->append("\\\"");
3991 else if (*p == '\\')
3992 ret->append("\\\\");
3996 ret->push_back('"');
4006 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4008 ret->push_back('S');
4010 const Struct_field_list* fields = this->fields_;
4013 for (Struct_field_list::const_iterator p = fields->begin();
4017 if (p->is_anonymous())
4021 std::string n = Gogo::unpack_hidden_name(p->field_name());
4023 snprintf(buf, sizeof buf, "%u_",
4024 static_cast<unsigned int>(n.length()));
4028 this->append_mangled_name(p->type(), gogo, ret);
4031 const std::string& tag(p->tag());
4033 for (std::string::const_iterator p = tag.begin();
4037 if (ISALNUM(*p) || *p == '_')
4042 snprintf(buf, sizeof buf, ".%x.",
4043 static_cast<unsigned int>(*p));
4048 snprintf(buf, sizeof buf, "T%u_",
4049 static_cast<unsigned int>(out.length()));
4056 ret->push_back('e');
4062 Struct_type::do_export(Export* exp) const
4064 exp->write_c_string("struct { ");
4065 const Struct_field_list* fields = this->fields_;
4066 go_assert(fields != NULL);
4067 for (Struct_field_list::const_iterator p = fields->begin();
4071 if (p->is_anonymous())
4072 exp->write_string("? ");
4075 exp->write_string(p->field_name());
4076 exp->write_c_string(" ");
4078 exp->write_type(p->type());
4082 exp->write_c_string(" ");
4084 Expression::make_string(p->tag(), Linemap::predeclared_location());
4085 expr->export_expression(exp);
4089 exp->write_c_string("; ");
4091 exp->write_c_string("}");
4097 Struct_type::do_import(Import* imp)
4099 imp->require_c_string("struct { ");
4100 Struct_field_list* fields = new Struct_field_list;
4101 if (imp->peek_char() != '}')
4106 if (imp->match_c_string("? "))
4110 name = imp->read_identifier();
4111 imp->require_c_string(" ");
4113 Type* ftype = imp->read_type();
4115 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
4117 if (imp->peek_char() == ' ')
4120 Expression* expr = Expression::import_expression(imp);
4121 String_expression* sexpr = expr->string_expression();
4122 go_assert(sexpr != NULL);
4123 sf.set_tag(sexpr->val());
4127 imp->require_c_string("; ");
4128 fields->push_back(sf);
4129 if (imp->peek_char() == '}')
4133 imp->require_c_string("}");
4135 return Type::make_struct_type(fields, imp->location());
4138 // Make a struct type.
4141 Type::make_struct_type(Struct_field_list* fields,
4144 return new Struct_type(fields, location);
4147 // Class Array_type.
4149 // Whether two array types are identical.
4152 Array_type::is_identical(const Array_type* t, bool errors_are_identical) const
4154 if (!Type::are_identical(this->element_type(), t->element_type(),
4155 errors_are_identical, NULL))
4158 Expression* l1 = this->length();
4159 Expression* l2 = t->length();
4161 // Slices of the same element type are identical.
4162 if (l1 == NULL && l2 == NULL)
4165 // Arrays of the same element type are identical if they have the
4167 if (l1 != NULL && l2 != NULL)