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"
38 Type::Type(Type_classification classification)
39 : classification_(classification), tree_(NULL_TREE),
40 type_descriptor_decl_(NULL_TREE)
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 gcc_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_open_array_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 gcc_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_open_array_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_open_array_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 REASON is not NULL, set *REASON to the reason
479 // the types are not assignable.
482 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
484 // Do some checks first. Make sure the types are defined.
486 && rhs->forwarded()->forward_declaration_type() == NULL
487 && rhs->is_void_type())
490 *reason = "non-value used as value";
494 if (lhs != NULL && lhs->forwarded()->forward_declaration_type() == NULL)
496 // Any value may be assigned to the blank identifier.
497 if (lhs->is_sink_type())
500 // All fields of a struct must be exported, or the assignment
501 // must be in the same package.
502 if (rhs != NULL && rhs->forwarded()->forward_declaration_type() == NULL)
504 if (lhs->has_hidden_fields(NULL, reason)
505 || rhs->has_hidden_fields(NULL, reason))
510 // Identical types are assignable.
511 if (Type::are_identical(lhs, rhs, true, reason))
514 // The types are assignable if they have identical underlying types
515 // and either LHS or RHS is not a named type.
516 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
517 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
518 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
521 // The types are assignable if LHS is an interface type and RHS
522 // implements the required methods.
523 const Interface_type* lhs_interface_type = lhs->interface_type();
524 if (lhs_interface_type != NULL)
526 if (lhs_interface_type->implements_interface(rhs, reason))
528 const Interface_type* rhs_interface_type = rhs->interface_type();
529 if (rhs_interface_type != NULL
530 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
535 // The type are assignable if RHS is a bidirectional channel type,
536 // LHS is a channel type, they have identical element types, and
537 // either LHS or RHS is not a named type.
538 if (lhs->channel_type() != NULL
539 && rhs->channel_type() != NULL
540 && rhs->channel_type()->may_send()
541 && rhs->channel_type()->may_receive()
542 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
543 && Type::are_identical(lhs->channel_type()->element_type(),
544 rhs->channel_type()->element_type(),
549 // The nil type may be assigned to a pointer, function, slice, map,
550 // channel, or interface type.
551 if (rhs->is_nil_type()
552 && (lhs->points_to() != NULL
553 || lhs->function_type() != NULL
554 || lhs->is_open_array_type()
555 || lhs->map_type() != NULL
556 || lhs->channel_type() != NULL
557 || lhs->interface_type() != NULL))
560 // An untyped numeric constant may be assigned to a numeric type if
561 // it is representable in that type.
562 if ((rhs->is_abstract()
563 && (rhs->integer_type() != NULL
564 || rhs->float_type() != NULL
565 || rhs->complex_type() != NULL))
566 && (lhs->integer_type() != NULL
567 || lhs->float_type() != NULL
568 || lhs->complex_type() != NULL))
571 // Give some better error messages.
572 if (reason != NULL && reason->empty())
574 if (rhs->interface_type() != NULL)
575 reason->assign(_("need explicit conversion"));
576 else if (rhs->is_call_multiple_result_type())
577 reason->assign(_("multiple value function call in "
578 "single value context"));
579 else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
581 size_t len = (lhs->named_type()->name().length()
582 + rhs->named_type()->name().length()
584 char* buf = new char[len];
585 snprintf(buf, len, _("cannot use type %s as type %s"),
586 rhs->named_type()->message_name().c_str(),
587 lhs->named_type()->message_name().c_str());
596 // Return true if a value with type RHS may be converted to type LHS.
597 // If REASON is not NULL, set *REASON to the reason the types are not
601 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
603 // The types are convertible if they are assignable.
604 if (Type::are_assignable(lhs, rhs, reason))
607 // The types are convertible if they have identical underlying
609 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
610 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
613 // The types are convertible if they are both unnamed pointer types
614 // and their pointer base types have identical underlying types.
615 if (lhs->named_type() == NULL
616 && rhs->named_type() == NULL
617 && lhs->points_to() != NULL
618 && rhs->points_to() != NULL
619 && (lhs->points_to()->named_type() != NULL
620 || rhs->points_to()->named_type() != NULL)
621 && Type::are_identical(lhs->points_to()->base(),
622 rhs->points_to()->base(),
627 // Integer and floating point types are convertible to each other.
628 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
629 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
632 // Complex types are convertible to each other.
633 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
636 // An integer, or []byte, or []int, may be converted to a string.
637 if (lhs->is_string_type())
639 if (rhs->integer_type() != NULL)
641 if (rhs->is_open_array_type() && rhs->named_type() == NULL)
643 const Type* e = rhs->array_type()->element_type()->forwarded();
644 if (e->integer_type() != NULL
645 && (e == Type::lookup_integer_type("uint8")
646 || e == Type::lookup_integer_type("int")))
651 // A string may be converted to []byte or []int.
652 if (rhs->is_string_type()
653 && lhs->is_open_array_type()
654 && lhs->named_type() == NULL)
656 const Type* e = lhs->array_type()->element_type()->forwarded();
657 if (e->integer_type() != NULL
658 && (e == Type::lookup_integer_type("uint8")
659 || e == Type::lookup_integer_type("int")))
663 // An unsafe.Pointer type may be converted to any pointer type or to
664 // uintptr, and vice-versa.
665 if (lhs->is_unsafe_pointer_type()
666 && (rhs->points_to() != NULL
667 || (rhs->integer_type() != NULL
668 && rhs->forwarded() == Type::lookup_integer_type("uintptr"))))
670 if (rhs->is_unsafe_pointer_type()
671 && (lhs->points_to() != NULL
672 || (lhs->integer_type() != NULL
673 && lhs->forwarded() == Type::lookup_integer_type("uintptr"))))
676 // Give a better error message.
680 *reason = "invalid type conversion";
683 std::string s = "invalid type conversion (";
693 // Return whether this type has any hidden fields. This is only a
694 // possibility for a few types.
697 Type::has_hidden_fields(const Named_type* within, std::string* reason) const
699 switch (this->forwarded()->classification_)
702 return this->named_type()->named_type_has_hidden_fields(reason);
704 return this->struct_type()->struct_has_hidden_fields(within, reason);
706 return this->array_type()->array_has_hidden_fields(within, reason);
712 // Return a hash code for the type to be used for method lookup.
715 Type::hash_for_method(Gogo* gogo) const
717 unsigned int ret = 0;
718 if (this->classification_ != TYPE_FORWARD)
719 ret += this->classification_;
720 return ret + this->do_hash_for_method(gogo);
723 // Default implementation of do_hash_for_method. This is appropriate
724 // for types with no subfields.
727 Type::do_hash_for_method(Gogo*) const
732 // Return a hash code for a string, given a starting hash.
735 Type::hash_string(const std::string& s, unsigned int h)
737 const char* p = s.data();
738 size_t len = s.length();
739 for (; len > 0; --len)
747 // Default check for the expression passed to make. Any type which
748 // may be used with make implements its own version of this.
751 Type::do_check_make_expression(Expression_list*, source_location)
756 // Return whether an expression has an integer value. Report an error
757 // if not. This is used when handling calls to the predeclared make
761 Type::check_int_value(Expression* e, const char* errmsg,
762 source_location location)
764 if (e->type()->integer_type() != NULL)
767 // Check for a floating point constant with integer value.
772 if (e->float_constant_value(fval, &dummy) && mpfr_integer_p(fval))
779 mpfr_clear_overflow();
780 mpfr_clear_erangeflag();
781 mpfr_get_z(ival, fval, GMP_RNDN);
782 if (!mpfr_overflow_p()
783 && !mpfr_erangeflag_p()
784 && mpz_sgn(ival) >= 0)
786 Named_type* ntype = Type::lookup_integer_type("int");
787 Integer_type* inttype = ntype->integer_type();
789 mpz_init_set_ui(max, 1);
790 mpz_mul_2exp(max, max, inttype->bits() - 1);
791 ok = mpz_cmp(ival, max) < 0;
805 error_at(location, "%s", errmsg);
809 // A hash table mapping unnamed types to trees.
811 Type::Type_trees Type::type_trees;
813 // Return a tree representing this type.
816 Type::get_tree(Gogo* gogo)
818 if (this->tree_ != NULL)
821 if (this->forward_declaration_type() != NULL
822 || this->named_type() != NULL)
823 return this->get_tree_without_hash(gogo);
825 if (this->is_error_type())
826 return error_mark_node;
828 // To avoid confusing GIMPLE, we need to translate all identical Go
829 // types to the same GIMPLE type. We use a hash table to do that.
830 // There is no need to use the hash table for named types, as named
831 // types are only identical to themselves.
833 std::pair<Type*, tree> val(this, NULL);
834 std::pair<Type_trees::iterator, bool> ins =
835 Type::type_trees.insert(val);
836 if (!ins.second && ins.first->second != NULL_TREE)
838 if (gogo != NULL && gogo->named_types_are_converted())
839 this->tree_ = ins.first->second;
840 return ins.first->second;
843 tree t = this->get_tree_without_hash(gogo);
845 if (ins.first->second == NULL_TREE)
846 ins.first->second = t;
849 // We have already created a tree for this type. This can
850 // happen when an unnamed type is defined using a named type
851 // which in turns uses an identical unnamed type. Use the tree
852 // we created earlier and ignore the one we just built.
853 t = ins.first->second;
854 if (gogo == NULL || !gogo->named_types_are_converted())
862 // Return a tree for a type without looking in the hash table for
863 // identical types. This is used for named types, since there is no
864 // point to looking in the hash table for them.
867 Type::get_tree_without_hash(Gogo* gogo)
869 if (this->tree_ == NULL_TREE)
871 tree t = this->do_get_tree(gogo);
873 // For a recursive function or pointer type, we will temporarily
874 // return ptr_type_node during the recursion. We don't want to
875 // record that for a forwarding type, as it may confuse us
877 if (t == ptr_type_node && this->forward_declaration_type() != NULL)
880 if (gogo == NULL || !gogo->named_types_are_converted())
884 go_preserve_from_gc(t);
890 // Return a tree representing a zero initialization for this type.
893 Type::get_init_tree(Gogo* gogo, bool is_clear)
895 tree type_tree = this->get_tree(gogo);
896 if (type_tree == error_mark_node)
897 return error_mark_node;
898 return this->do_get_init_tree(gogo, type_tree, is_clear);
901 // Any type which supports the builtin make function must implement
905 Type::do_make_expression_tree(Translate_context*, Expression_list*,
911 // Return a pointer to the type descriptor for this type.
914 Type::type_descriptor_pointer(Gogo* gogo)
916 Type* t = this->forwarded();
917 if (t->type_descriptor_decl_ == NULL_TREE)
919 Expression* e = t->do_type_descriptor(gogo, NULL);
920 gogo->build_type_descriptor_decl(t, e, &t->type_descriptor_decl_);
921 gcc_assert(t->type_descriptor_decl_ != NULL_TREE
922 && (t->type_descriptor_decl_ == error_mark_node
923 || DECL_P(t->type_descriptor_decl_)));
925 if (t->type_descriptor_decl_ == error_mark_node)
926 return error_mark_node;
927 return build_fold_addr_expr(t->type_descriptor_decl_);
930 // Return a composite literal for a type descriptor.
933 Type::type_descriptor(Gogo* gogo, Type* type)
935 return type->do_type_descriptor(gogo, NULL);
938 // Return a composite literal for a type descriptor with a name.
941 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
943 gcc_assert(name != NULL && type->named_type() != name);
944 return type->do_type_descriptor(gogo, name);
947 // Make a builtin struct type from a list of fields. The fields are
948 // pairs of a name and a type.
951 Type::make_builtin_struct_type(int nfields, ...)
954 va_start(ap, nfields);
956 source_location bloc = BUILTINS_LOCATION;
957 Struct_field_list* sfl = new Struct_field_list();
958 for (int i = 0; i < nfields; i++)
960 const char* field_name = va_arg(ap, const char *);
961 Type* type = va_arg(ap, Type*);
962 sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
967 return Type::make_struct_type(sfl, bloc);
970 // A list of builtin named types.
972 std::vector<Named_type*> Type::named_builtin_types;
974 // Make a builtin named type.
977 Type::make_builtin_named_type(const char* name, Type* type)
979 source_location bloc = BUILTINS_LOCATION;
980 Named_object* no = Named_object::make_type(name, NULL, type, bloc);
981 Named_type* ret = no->type_value();
982 Type::named_builtin_types.push_back(ret);
986 // Convert the named builtin types.
989 Type::convert_builtin_named_types(Gogo* gogo)
991 for (std::vector<Named_type*>::const_iterator p =
992 Type::named_builtin_types.begin();
993 p != Type::named_builtin_types.end();
996 bool r = (*p)->verify();
1002 // Return the type of a type descriptor. We should really tie this to
1003 // runtime.Type rather than copying it. This must match commonType in
1004 // libgo/go/runtime/type.go.
1007 Type::make_type_descriptor_type()
1012 source_location bloc = BUILTINS_LOCATION;
1014 Type* uint8_type = Type::lookup_integer_type("uint8");
1015 Type* uint32_type = Type::lookup_integer_type("uint32");
1016 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1017 Type* string_type = Type::lookup_string_type();
1018 Type* pointer_string_type = Type::make_pointer_type(string_type);
1020 // This is an unnamed version of unsafe.Pointer. Perhaps we
1021 // should use the named version instead, although that would
1022 // require us to create the unsafe package if it has not been
1023 // imported. It probably doesn't matter.
1024 Type* void_type = Type::make_void_type();
1025 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1027 // Forward declaration for the type descriptor type.
1028 Named_object* named_type_descriptor_type =
1029 Named_object::make_type_declaration("commonType", NULL, bloc);
1030 Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
1031 Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
1033 // The type of a method on a concrete type.
1034 Struct_type* method_type =
1035 Type::make_builtin_struct_type(5,
1036 "name", pointer_string_type,
1037 "pkgPath", pointer_string_type,
1038 "mtyp", pointer_type_descriptor_type,
1039 "typ", pointer_type_descriptor_type,
1040 "tfn", unsafe_pointer_type);
1041 Named_type* named_method_type =
1042 Type::make_builtin_named_type("method", method_type);
1044 // Information for types with a name or methods.
1045 Type* slice_named_method_type =
1046 Type::make_array_type(named_method_type, NULL);
1047 Struct_type* uncommon_type =
1048 Type::make_builtin_struct_type(3,
1049 "name", pointer_string_type,
1050 "pkgPath", pointer_string_type,
1051 "methods", slice_named_method_type);
1052 Named_type* named_uncommon_type =
1053 Type::make_builtin_named_type("uncommonType", uncommon_type);
1055 Type* pointer_uncommon_type =
1056 Type::make_pointer_type(named_uncommon_type);
1058 // The type descriptor type.
1060 Typed_identifier_list* params = new Typed_identifier_list();
1061 params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
1062 params->push_back(Typed_identifier("", uintptr_type, bloc));
1064 Typed_identifier_list* results = new Typed_identifier_list();
1065 results->push_back(Typed_identifier("", uintptr_type, bloc));
1067 Type* hashfn_type = Type::make_function_type(NULL, params, results, bloc);
1069 params = new Typed_identifier_list();
1070 params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
1071 params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
1072 params->push_back(Typed_identifier("", uintptr_type, bloc));
1074 results = new Typed_identifier_list();
1075 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1077 Type* equalfn_type = Type::make_function_type(NULL, params, results,
1080 Struct_type* type_descriptor_type =
1081 Type::make_builtin_struct_type(10,
1083 "align", uint8_type,
1084 "fieldAlign", uint8_type,
1085 "size", uintptr_type,
1086 "hash", uint32_type,
1087 "hashfn", hashfn_type,
1088 "equalfn", equalfn_type,
1089 "string", pointer_string_type,
1090 "", pointer_uncommon_type,
1092 pointer_type_descriptor_type);
1094 Named_type* named = Type::make_builtin_named_type("commonType",
1095 type_descriptor_type);
1097 named_type_descriptor_type->set_type_value(named);
1105 // Make the type of a pointer to a type descriptor as represented in
1109 Type::make_type_descriptor_ptr_type()
1113 ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1117 // Return the names of runtime functions which compute a hash code for
1118 // this type and which compare whether two values of this type are
1122 Type::type_functions(const char** hash_fn, const char** equal_fn) const
1124 switch (this->base()->classification())
1126 case Type::TYPE_ERROR:
1127 case Type::TYPE_VOID:
1128 case Type::TYPE_NIL:
1129 // These types can not be hashed or compared.
1130 *hash_fn = "__go_type_hash_error";
1131 *equal_fn = "__go_type_equal_error";
1134 case Type::TYPE_BOOLEAN:
1135 case Type::TYPE_INTEGER:
1136 case Type::TYPE_FLOAT:
1137 case Type::TYPE_COMPLEX:
1138 case Type::TYPE_POINTER:
1139 case Type::TYPE_FUNCTION:
1140 case Type::TYPE_MAP:
1141 case Type::TYPE_CHANNEL:
1142 *hash_fn = "__go_type_hash_identity";
1143 *equal_fn = "__go_type_equal_identity";
1146 case Type::TYPE_STRING:
1147 *hash_fn = "__go_type_hash_string";
1148 *equal_fn = "__go_type_equal_string";
1151 case Type::TYPE_STRUCT:
1152 case Type::TYPE_ARRAY:
1153 // These types can not be hashed or compared.
1154 *hash_fn = "__go_type_hash_error";
1155 *equal_fn = "__go_type_equal_error";
1158 case Type::TYPE_INTERFACE:
1159 if (this->interface_type()->is_empty())
1161 *hash_fn = "__go_type_hash_empty_interface";
1162 *equal_fn = "__go_type_equal_empty_interface";
1166 *hash_fn = "__go_type_hash_interface";
1167 *equal_fn = "__go_type_equal_interface";
1171 case Type::TYPE_NAMED:
1172 case Type::TYPE_FORWARD:
1180 // Return a composite literal for the type descriptor for a plain type
1181 // of kind RUNTIME_TYPE_KIND named NAME.
1184 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1185 Named_type* name, const Methods* methods,
1186 bool only_value_methods)
1188 source_location bloc = BUILTINS_LOCATION;
1190 Type* td_type = Type::make_type_descriptor_type();
1191 const Struct_field_list* fields = td_type->struct_type()->fields();
1193 Expression_list* vals = new Expression_list();
1196 Struct_field_list::const_iterator p = fields->begin();
1197 gcc_assert(p->field_name() == "Kind");
1199 mpz_init_set_ui(iv, runtime_type_kind);
1200 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1203 gcc_assert(p->field_name() == "align");
1204 Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1205 vals->push_back(Expression::make_type_info(this, type_info));
1208 gcc_assert(p->field_name() == "fieldAlign");
1209 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1210 vals->push_back(Expression::make_type_info(this, type_info));
1213 gcc_assert(p->field_name() == "size");
1214 type_info = Expression::TYPE_INFO_SIZE;
1215 vals->push_back(Expression::make_type_info(this, type_info));
1218 gcc_assert(p->field_name() == "hash");
1219 mpz_set_ui(iv, this->hash_for_method(gogo));
1220 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1222 const char* hash_fn;
1223 const char* equal_fn;
1224 this->type_functions(&hash_fn, &equal_fn);
1227 gcc_assert(p->field_name() == "hashfn");
1228 Function_type* fntype = p->type()->function_type();
1229 Named_object* no = Named_object::make_function_declaration(hash_fn, NULL,
1232 no->func_declaration_value()->set_asm_name(hash_fn);
1233 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1236 gcc_assert(p->field_name() == "equalfn");
1237 fntype = p->type()->function_type();
1238 no = Named_object::make_function_declaration(equal_fn, NULL, fntype, bloc);
1239 no->func_declaration_value()->set_asm_name(equal_fn);
1240 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1243 gcc_assert(p->field_name() == "string");
1244 Expression* s = Expression::make_string((name != NULL
1245 ? name->reflection(gogo)
1246 : this->reflection(gogo)),
1248 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1251 gcc_assert(p->field_name() == "uncommonType");
1252 if (name == NULL && methods == NULL)
1253 vals->push_back(Expression::make_nil(bloc));
1256 if (methods == NULL)
1257 methods = name->methods();
1258 vals->push_back(this->uncommon_type_constructor(gogo,
1261 only_value_methods));
1265 gcc_assert(p->field_name() == "ptrToThis");
1267 vals->push_back(Expression::make_nil(bloc));
1270 Type* pt = Type::make_pointer_type(name);
1271 vals->push_back(Expression::make_type_descriptor(pt, bloc));
1275 gcc_assert(p == fields->end());
1279 return Expression::make_struct_composite_literal(td_type, vals, bloc);
1282 // Return a composite literal for the uncommon type information for
1283 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1284 // struct. If name is not NULL, it is the name of the type. If
1285 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1286 // is true if only value methods should be included. At least one of
1287 // NAME and METHODS must not be NULL.
1290 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
1291 Named_type* name, const Methods* methods,
1292 bool only_value_methods) const
1294 source_location bloc = BUILTINS_LOCATION;
1296 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
1298 Expression_list* vals = new Expression_list();
1301 Struct_field_list::const_iterator p = fields->begin();
1302 gcc_assert(p->field_name() == "name");
1305 gcc_assert(p->field_name() == "pkgPath");
1309 vals->push_back(Expression::make_nil(bloc));
1310 vals->push_back(Expression::make_nil(bloc));
1314 Named_object* no = name->named_object();
1315 std::string n = Gogo::unpack_hidden_name(no->name());
1316 Expression* s = Expression::make_string(n, bloc);
1317 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1319 if (name->is_builtin())
1320 vals->push_back(Expression::make_nil(bloc));
1323 const Package* package = no->package();
1324 const std::string& unique_prefix(package == NULL
1325 ? gogo->unique_prefix()
1326 : package->unique_prefix());
1327 const std::string& package_name(package == NULL
1328 ? gogo->package_name()
1330 n.assign(unique_prefix);
1332 n.append(package_name);
1333 if (name->in_function() != NULL)
1336 n.append(Gogo::unpack_hidden_name(name->in_function()->name()));
1338 s = Expression::make_string(n, bloc);
1339 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1344 gcc_assert(p->field_name() == "methods");
1345 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
1346 only_value_methods));
1349 gcc_assert(p == fields->end());
1351 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
1353 return Expression::make_unary(OPERATOR_AND, r, bloc);
1356 // Sort methods by name.
1362 operator()(const std::pair<std::string, const Method*>& m1,
1363 const std::pair<std::string, const Method*>& m2) const
1364 { return m1.first < m2.first; }
1367 // Return a composite literal for the type method table for this type.
1368 // METHODS_TYPE is the type of the table, and is a slice type.
1369 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1370 // then only value methods are used.
1373 Type::methods_constructor(Gogo* gogo, Type* methods_type,
1374 const Methods* methods,
1375 bool only_value_methods) const
1377 source_location bloc = BUILTINS_LOCATION;
1379 std::vector<std::pair<std::string, const Method*> > smethods;
1380 if (methods != NULL)
1382 smethods.reserve(methods->count());
1383 for (Methods::const_iterator p = methods->begin();
1384 p != methods->end();
1387 if (p->second->is_ambiguous())
1389 if (only_value_methods && !p->second->is_value_method())
1391 smethods.push_back(std::make_pair(p->first, p->second));
1395 if (smethods.empty())
1396 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
1398 std::sort(smethods.begin(), smethods.end(), Sort_methods());
1400 Type* method_type = methods_type->array_type()->element_type();
1402 Expression_list* vals = new Expression_list();
1403 vals->reserve(smethods.size());
1404 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
1406 p != smethods.end();
1408 vals->push_back(this->method_constructor(gogo, method_type, p->first,
1411 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
1414 // Return a composite literal for a single method. METHOD_TYPE is the
1415 // type of the entry. METHOD_NAME is the name of the method and M is
1416 // the method information.
1419 Type::method_constructor(Gogo*, Type* method_type,
1420 const std::string& method_name,
1421 const Method* m) const
1423 source_location bloc = BUILTINS_LOCATION;
1425 const Struct_field_list* fields = method_type->struct_type()->fields();
1427 Expression_list* vals = new Expression_list();
1430 Struct_field_list::const_iterator p = fields->begin();
1431 gcc_assert(p->field_name() == "name");
1432 const std::string n = Gogo::unpack_hidden_name(method_name);
1433 Expression* s = Expression::make_string(n, bloc);
1434 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1437 gcc_assert(p->field_name() == "pkgPath");
1438 if (!Gogo::is_hidden_name(method_name))
1439 vals->push_back(Expression::make_nil(bloc));
1442 s = Expression::make_string(Gogo::hidden_name_prefix(method_name), bloc);
1443 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1446 Named_object* no = (m->needs_stub_method()
1448 : m->named_object());
1450 Function_type* mtype;
1451 if (no->is_function())
1452 mtype = no->func_value()->type();
1454 mtype = no->func_declaration_value()->type();
1455 gcc_assert(mtype->is_method());
1456 Type* nonmethod_type = mtype->copy_without_receiver();
1459 gcc_assert(p->field_name() == "mtyp");
1460 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
1463 gcc_assert(p->field_name() == "typ");
1464 vals->push_back(Expression::make_type_descriptor(mtype, bloc));
1467 gcc_assert(p->field_name() == "tfn");
1468 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1471 gcc_assert(p == fields->end());
1473 return Expression::make_struct_composite_literal(method_type, vals, bloc);
1476 // Return a composite literal for the type descriptor of a plain type.
1477 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1478 // NULL, it is the name to use as well as the list of methods.
1481 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
1484 return this->type_descriptor_constructor(gogo, runtime_type_kind,
1488 // Return the type reflection string for this type.
1491 Type::reflection(Gogo* gogo) const
1495 // The do_reflection virtual function should set RET to the
1496 // reflection string.
1497 this->do_reflection(gogo, &ret);
1502 // Return a mangled name for the type.
1505 Type::mangled_name(Gogo* gogo) const
1509 // The do_mangled_name virtual function should set RET to the
1510 // mangled name. For a composite type it should append a code for
1511 // the composition and then call do_mangled_name on the components.
1512 this->do_mangled_name(gogo, &ret);
1517 // Default function to export a type.
1520 Type::do_export(Export*) const
1528 Type::import_type(Import* imp)
1530 if (imp->match_c_string("("))
1531 return Function_type::do_import(imp);
1532 else if (imp->match_c_string("*"))
1533 return Pointer_type::do_import(imp);
1534 else if (imp->match_c_string("struct "))
1535 return Struct_type::do_import(imp);
1536 else if (imp->match_c_string("["))
1537 return Array_type::do_import(imp);
1538 else if (imp->match_c_string("map "))
1539 return Map_type::do_import(imp);
1540 else if (imp->match_c_string("chan "))
1541 return Channel_type::do_import(imp);
1542 else if (imp->match_c_string("interface"))
1543 return Interface_type::do_import(imp);
1546 error_at(imp->location(), "import error: expected type");
1547 return Type::make_error_type();
1551 // A type used to indicate a parsing error. This exists to simplify
1552 // later error detection.
1554 class Error_type : public Type
1564 { return error_mark_node; }
1567 do_get_init_tree(Gogo*, tree, bool)
1568 { return error_mark_node; }
1571 do_type_descriptor(Gogo*, Named_type*)
1572 { return Expression::make_error(BUILTINS_LOCATION); }
1575 do_reflection(Gogo*, std::string*) const
1576 { gcc_assert(saw_errors()); }
1579 do_mangled_name(Gogo*, std::string* ret) const
1580 { ret->push_back('E'); }
1584 Type::make_error_type()
1586 static Error_type singleton_error_type;
1587 return &singleton_error_type;
1592 class Void_type : public Type
1602 { return void_type_node; }
1605 do_get_init_tree(Gogo*, tree, bool)
1606 { gcc_unreachable(); }
1609 do_type_descriptor(Gogo*, Named_type*)
1610 { gcc_unreachable(); }
1613 do_reflection(Gogo*, std::string*) const
1617 do_mangled_name(Gogo*, std::string* ret) const
1618 { ret->push_back('v'); }
1622 Type::make_void_type()
1624 static Void_type singleton_void_type;
1625 return &singleton_void_type;
1628 // The boolean type.
1630 class Boolean_type : public Type
1634 : Type(TYPE_BOOLEAN)
1640 { return boolean_type_node; }
1643 do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
1644 { return is_clear ? NULL : fold_convert(type_tree, boolean_false_node); }
1647 do_type_descriptor(Gogo*, Named_type* name);
1649 // We should not be asked for the reflection string of a basic type.
1651 do_reflection(Gogo*, std::string* ret) const
1652 { ret->append("bool"); }
1655 do_mangled_name(Gogo*, std::string* ret) const
1656 { ret->push_back('b'); }
1659 // Make the type descriptor.
1662 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
1665 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
1668 Named_object* no = gogo->lookup_global("bool");
1669 gcc_assert(no != NULL);
1670 return Type::type_descriptor(gogo, no->type_value());
1675 Type::make_boolean_type()
1677 static Boolean_type boolean_type;
1678 return &boolean_type;
1681 // The named type "bool".
1683 static Named_type* named_bool_type;
1685 // Get the named type "bool".
1688 Type::lookup_bool_type()
1690 return named_bool_type;
1693 // Make the named type "bool".
1696 Type::make_named_bool_type()
1698 Type* bool_type = Type::make_boolean_type();
1699 Named_object* named_object = Named_object::make_type("bool", NULL,
1702 Named_type* named_type = named_object->type_value();
1703 named_bool_type = named_type;
1707 // Class Integer_type.
1709 Integer_type::Named_integer_types Integer_type::named_integer_types;
1711 // Create a new integer type. Non-abstract integer types always have
1715 Integer_type::create_integer_type(const char* name, bool is_unsigned,
1716 int bits, int runtime_type_kind)
1718 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
1720 std::string sname(name);
1721 Named_object* named_object = Named_object::make_type(sname, NULL,
1724 Named_type* named_type = named_object->type_value();
1725 std::pair<Named_integer_types::iterator, bool> ins =
1726 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
1727 gcc_assert(ins.second);
1731 // Look up an existing integer type.
1734 Integer_type::lookup_integer_type(const char* name)
1736 Named_integer_types::const_iterator p =
1737 Integer_type::named_integer_types.find(name);
1738 gcc_assert(p != Integer_type::named_integer_types.end());
1742 // Create a new abstract integer type.
1745 Integer_type::create_abstract_integer_type()
1747 static Integer_type* abstract_type;
1748 if (abstract_type == NULL)
1749 abstract_type = new Integer_type(true, false, INT_TYPE_SIZE,
1750 RUNTIME_TYPE_KIND_INT);
1751 return abstract_type;
1754 // Integer type compatibility.
1757 Integer_type::is_identical(const Integer_type* t) const
1759 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
1761 return this->is_abstract_ == t->is_abstract_;
1767 Integer_type::do_hash_for_method(Gogo*) const
1769 return ((this->bits_ << 4)
1770 + ((this->is_unsigned_ ? 1 : 0) << 8)
1771 + ((this->is_abstract_ ? 1 : 0) << 9));
1774 // Get the tree for an Integer_type.
1777 Integer_type::do_get_tree(Gogo*)
1779 if (this->is_abstract_)
1781 gcc_assert(saw_errors());
1782 return error_mark_node;
1785 if (this->is_unsigned_)
1787 if (this->bits_ == INT_TYPE_SIZE)
1788 return unsigned_type_node;
1789 else if (this->bits_ == CHAR_TYPE_SIZE)
1790 return unsigned_char_type_node;
1791 else if (this->bits_ == SHORT_TYPE_SIZE)
1792 return short_unsigned_type_node;
1793 else if (this->bits_ == LONG_TYPE_SIZE)
1794 return long_unsigned_type_node;
1795 else if (this->bits_ == LONG_LONG_TYPE_SIZE)
1796 return long_long_unsigned_type_node;
1798 return make_unsigned_type(this->bits_);
1802 if (this->bits_ == INT_TYPE_SIZE)
1803 return integer_type_node;
1804 else if (this->bits_ == CHAR_TYPE_SIZE)
1805 return signed_char_type_node;
1806 else if (this->bits_ == SHORT_TYPE_SIZE)
1807 return short_integer_type_node;
1808 else if (this->bits_ == LONG_TYPE_SIZE)
1809 return long_integer_type_node;
1810 else if (this->bits_ == LONG_LONG_TYPE_SIZE)
1811 return long_long_integer_type_node;
1813 return make_signed_type(this->bits_);
1818 Integer_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
1820 return is_clear ? NULL : build_int_cst(type_tree, 0);
1823 // The type descriptor for an integer type. Integer types are always
1827 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
1829 gcc_assert(name != NULL);
1830 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
1833 // We should not be asked for the reflection string of a basic type.
1836 Integer_type::do_reflection(Gogo*, std::string*) const
1838 gcc_assert(saw_errors());
1844 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
1847 snprintf(buf, sizeof buf, "i%s%s%de",
1848 this->is_abstract_ ? "a" : "",
1849 this->is_unsigned_ ? "u" : "",
1854 // Make an integer type.
1857 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
1858 int runtime_type_kind)
1860 return Integer_type::create_integer_type(name, is_unsigned, bits,
1864 // Make an abstract integer type.
1867 Type::make_abstract_integer_type()
1869 return Integer_type::create_abstract_integer_type();
1872 // Look up an integer type.
1875 Type::lookup_integer_type(const char* name)
1877 return Integer_type::lookup_integer_type(name);
1880 // Class Float_type.
1882 Float_type::Named_float_types Float_type::named_float_types;
1884 // Create a new float type. Non-abstract float types always have
1888 Float_type::create_float_type(const char* name, int bits,
1889 int runtime_type_kind)
1891 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
1892 std::string sname(name);
1893 Named_object* named_object = Named_object::make_type(sname, NULL, float_type,
1895 Named_type* named_type = named_object->type_value();
1896 std::pair<Named_float_types::iterator, bool> ins =
1897 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
1898 gcc_assert(ins.second);
1902 // Look up an existing float type.
1905 Float_type::lookup_float_type(const char* name)
1907 Named_float_types::const_iterator p =
1908 Float_type::named_float_types.find(name);
1909 gcc_assert(p != Float_type::named_float_types.end());
1913 // Create a new abstract float type.
1916 Float_type::create_abstract_float_type()
1918 static Float_type* abstract_type;
1919 if (abstract_type == NULL)
1920 abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
1921 return abstract_type;
1924 // Whether this type is identical with T.
1927 Float_type::is_identical(const Float_type* t) const
1929 if (this->bits_ != t->bits_)
1931 return this->is_abstract_ == t->is_abstract_;
1937 Float_type::do_hash_for_method(Gogo*) const
1939 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
1942 // Get a tree without using a Gogo*.
1945 Float_type::type_tree() const
1947 if (this->bits_ == FLOAT_TYPE_SIZE)
1948 return float_type_node;
1949 else if (this->bits_ == DOUBLE_TYPE_SIZE)
1950 return double_type_node;
1951 else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE)
1952 return long_double_type_node;
1955 tree ret = make_node(REAL_TYPE);
1956 TYPE_PRECISION(ret) = this->bits_;
1965 Float_type::do_get_tree(Gogo*)
1967 return this->type_tree();
1971 Float_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
1976 real_from_integer(&r, TYPE_MODE(type_tree), 0, 0, 0);
1977 return build_real(type_tree, r);
1980 // The type descriptor for a float type. Float types are always named.
1983 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
1985 gcc_assert(name != NULL);
1986 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
1989 // We should not be asked for the reflection string of a basic type.
1992 Float_type::do_reflection(Gogo*, std::string*) const
1994 gcc_assert(saw_errors());
2000 Float_type::do_mangled_name(Gogo*, std::string* ret) const
2003 snprintf(buf, sizeof buf, "f%s%de",
2004 this->is_abstract_ ? "a" : "",
2009 // Make a floating point type.
2012 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
2014 return Float_type::create_float_type(name, bits, runtime_type_kind);
2017 // Make an abstract float type.
2020 Type::make_abstract_float_type()
2022 return Float_type::create_abstract_float_type();
2025 // Look up a float type.
2028 Type::lookup_float_type(const char* name)
2030 return Float_type::lookup_float_type(name);
2033 // Class Complex_type.
2035 Complex_type::Named_complex_types Complex_type::named_complex_types;
2037 // Create a new complex type. Non-abstract complex types always have
2041 Complex_type::create_complex_type(const char* name, int bits,
2042 int runtime_type_kind)
2044 Complex_type* complex_type = new Complex_type(false, bits,
2046 std::string sname(name);
2047 Named_object* named_object = Named_object::make_type(sname, NULL,
2050 Named_type* named_type = named_object->type_value();
2051 std::pair<Named_complex_types::iterator, bool> ins =
2052 Complex_type::named_complex_types.insert(std::make_pair(sname,
2054 gcc_assert(ins.second);
2058 // Look up an existing complex type.
2061 Complex_type::lookup_complex_type(const char* name)
2063 Named_complex_types::const_iterator p =
2064 Complex_type::named_complex_types.find(name);
2065 gcc_assert(p != Complex_type::named_complex_types.end());
2069 // Create a new abstract complex type.
2072 Complex_type::create_abstract_complex_type()
2074 static Complex_type* abstract_type;
2075 if (abstract_type == NULL)
2076 abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
2077 return abstract_type;
2080 // Whether this type is identical with T.
2083 Complex_type::is_identical(const Complex_type *t) const
2085 if (this->bits_ != t->bits_)
2087 return this->is_abstract_ == t->is_abstract_;
2093 Complex_type::do_hash_for_method(Gogo*) const
2095 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2098 // Get a tree without using a Gogo*.
2101 Complex_type::type_tree() const
2103 if (this->bits_ == FLOAT_TYPE_SIZE * 2)
2104 return complex_float_type_node;
2105 else if (this->bits_ == DOUBLE_TYPE_SIZE * 2)
2106 return complex_double_type_node;
2107 else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE * 2)
2108 return complex_long_double_type_node;
2111 tree ret = make_node(REAL_TYPE);
2112 TYPE_PRECISION(ret) = this->bits_ / 2;
2114 return build_complex_type(ret);
2121 Complex_type::do_get_tree(Gogo*)
2123 return this->type_tree();
2126 // Zero initializer.
2129 Complex_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
2134 real_from_integer(&r, TYPE_MODE(TREE_TYPE(type_tree)), 0, 0, 0);
2135 return build_complex(type_tree, build_real(TREE_TYPE(type_tree), r),
2136 build_real(TREE_TYPE(type_tree), r));
2139 // The type descriptor for a complex type. Complex types are always
2143 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2145 gcc_assert(name != NULL);
2146 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2149 // We should not be asked for the reflection string of a basic type.
2152 Complex_type::do_reflection(Gogo*, std::string*) const
2154 gcc_assert(saw_errors());
2160 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2163 snprintf(buf, sizeof buf, "c%s%de",
2164 this->is_abstract_ ? "a" : "",
2169 // Make a complex type.
2172 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2174 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2177 // Make an abstract complex type.
2180 Type::make_abstract_complex_type()
2182 return Complex_type::create_abstract_complex_type();
2185 // Look up a complex type.
2188 Type::lookup_complex_type(const char* name)
2190 return Complex_type::lookup_complex_type(name);
2193 // Class String_type.
2195 // Return the tree for String_type. A string is a struct with two
2196 // fields: a pointer to the characters and a length.
2199 String_type::do_get_tree(Gogo*)
2201 static tree struct_type;
2202 return Gogo::builtin_struct(&struct_type, "__go_string", NULL_TREE, 2,
2204 build_pointer_type(unsigned_char_type_node),
2209 // Return a tree for the length of STRING.
2212 String_type::length_tree(Gogo*, tree string)
2214 tree string_type = TREE_TYPE(string);
2215 gcc_assert(TREE_CODE(string_type) == RECORD_TYPE);
2216 tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2217 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2219 return fold_build3(COMPONENT_REF, integer_type_node, string,
2220 length_field, NULL_TREE);
2223 // Return a tree for a pointer to the bytes of STRING.
2226 String_type::bytes_tree(Gogo*, tree string)
2228 tree string_type = TREE_TYPE(string);
2229 gcc_assert(TREE_CODE(string_type) == RECORD_TYPE);
2230 tree bytes_field = TYPE_FIELDS(string_type);
2231 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
2233 return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
2234 bytes_field, NULL_TREE);
2237 // We initialize a string to { NULL, 0 }.
2240 String_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
2245 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2247 VEC(constructor_elt, gc)* init = VEC_alloc(constructor_elt, gc, 2);
2249 for (tree field = TYPE_FIELDS(type_tree);
2251 field = DECL_CHAIN(field))
2253 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
2255 elt->value = fold_convert(TREE_TYPE(field), size_zero_node);
2258 tree ret = build_constructor(type_tree, init);
2259 TREE_CONSTANT(ret) = 1;
2263 // The type descriptor for the string type.
2266 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2269 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
2272 Named_object* no = gogo->lookup_global("string");
2273 gcc_assert(no != NULL);
2274 return Type::type_descriptor(gogo, no->type_value());
2278 // We should not be asked for the reflection string of a basic type.
2281 String_type::do_reflection(Gogo*, std::string* ret) const
2283 ret->append("string");
2286 // Mangled name of a string type.
2289 String_type::do_mangled_name(Gogo*, std::string* ret) const
2291 ret->push_back('z');
2294 // Make a string type.
2297 Type::make_string_type()
2299 static String_type string_type;
2300 return &string_type;
2303 // The named type "string".
2305 static Named_type* named_string_type;
2307 // Get the named type "string".
2310 Type::lookup_string_type()
2312 return named_string_type;
2315 // Make the named type string.
2318 Type::make_named_string_type()
2320 Type* string_type = Type::make_string_type();
2321 Named_object* named_object = Named_object::make_type("string", NULL,
2324 Named_type* named_type = named_object->type_value();
2325 named_string_type = named_type;
2329 // The sink type. This is the type of the blank identifier _. Any
2330 // type may be assigned to it.
2332 class Sink_type : public Type
2342 { gcc_unreachable(); }
2345 do_get_init_tree(Gogo*, tree, bool)
2346 { gcc_unreachable(); }
2349 do_type_descriptor(Gogo*, Named_type*)
2350 { gcc_unreachable(); }
2353 do_reflection(Gogo*, std::string*) const
2354 { gcc_unreachable(); }
2357 do_mangled_name(Gogo*, std::string*) const
2358 { gcc_unreachable(); }
2361 // Make the sink type.
2364 Type::make_sink_type()
2366 static Sink_type sink_type;
2370 // Class Function_type.
2375 Function_type::do_traverse(Traverse* traverse)
2377 if (this->receiver_ != NULL
2378 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
2379 return TRAVERSE_EXIT;
2380 if (this->parameters_ != NULL
2381 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
2382 return TRAVERSE_EXIT;
2383 if (this->results_ != NULL
2384 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
2385 return TRAVERSE_EXIT;
2386 return TRAVERSE_CONTINUE;
2389 // Returns whether T is a valid redeclaration of this type. If this
2390 // returns false, and REASON is not NULL, *REASON may be set to a
2391 // brief explanation of why it returned false.
2394 Function_type::is_valid_redeclaration(const Function_type* t,
2395 std::string* reason) const
2397 if (!this->is_identical(t, false, true, reason))
2400 // A redeclaration of a function is required to use the same names
2401 // for the receiver and parameters.
2402 if (this->receiver() != NULL
2403 && this->receiver()->name() != t->receiver()->name()
2404 && this->receiver()->name() != Import::import_marker
2405 && t->receiver()->name() != Import::import_marker)
2408 *reason = "receiver name changed";
2412 const Typed_identifier_list* parms1 = this->parameters();
2413 const Typed_identifier_list* parms2 = t->parameters();
2416 Typed_identifier_list::const_iterator p1 = parms1->begin();
2417 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2418 p2 != parms2->end();
2421 if (p1->name() != p2->name()
2422 && p1->name() != Import::import_marker
2423 && p2->name() != Import::import_marker)
2426 *reason = "parameter name changed";
2430 // This is called at parse time, so we may have unknown
2432 Type* t1 = p1->type()->forwarded();
2433 Type* t2 = p2->type()->forwarded();
2435 && t1->forward_declaration_type() != NULL
2436 && (t2->forward_declaration_type() == NULL
2437 || (t1->forward_declaration_type()->named_object()
2438 != t2->forward_declaration_type()->named_object())))
2443 const Typed_identifier_list* results1 = this->results();
2444 const Typed_identifier_list* results2 = t->results();
2445 if (results1 != NULL)
2447 Typed_identifier_list::const_iterator res1 = results1->begin();
2448 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2449 res2 != results2->end();
2452 if (res1->name() != res2->name()
2453 && res1->name() != Import::import_marker
2454 && res2->name() != Import::import_marker)
2457 *reason = "result name changed";
2461 // This is called at parse time, so we may have unknown
2463 Type* t1 = res1->type()->forwarded();
2464 Type* t2 = res2->type()->forwarded();
2466 && t1->forward_declaration_type() != NULL
2467 && (t2->forward_declaration_type() == NULL
2468 || (t1->forward_declaration_type()->named_object()
2469 != t2->forward_declaration_type()->named_object())))
2477 // Check whether T is the same as this type.
2480 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
2481 bool errors_are_identical,
2482 std::string* reason) const
2484 if (!ignore_receiver)
2486 const Typed_identifier* r1 = this->receiver();
2487 const Typed_identifier* r2 = t->receiver();
2488 if ((r1 != NULL) != (r2 != NULL))
2491 *reason = _("different receiver types");
2496 if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
2499 if (reason != NULL && !reason->empty())
2500 *reason = "receiver: " + *reason;
2506 const Typed_identifier_list* parms1 = this->parameters();
2507 const Typed_identifier_list* parms2 = t->parameters();
2508 if ((parms1 != NULL) != (parms2 != NULL))
2511 *reason = _("different number of parameters");
2516 Typed_identifier_list::const_iterator p1 = parms1->begin();
2517 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2518 p2 != parms2->end();
2521 if (p1 == parms1->end())
2524 *reason = _("different number of parameters");
2528 if (!Type::are_identical(p1->type(), p2->type(),
2529 errors_are_identical, NULL))
2532 *reason = _("different parameter types");
2536 if (p1 != parms1->end())
2539 *reason = _("different number of parameters");
2544 if (this->is_varargs() != t->is_varargs())
2547 *reason = _("different varargs");
2551 const Typed_identifier_list* results1 = this->results();
2552 const Typed_identifier_list* results2 = t->results();
2553 if ((results1 != NULL) != (results2 != NULL))
2556 *reason = _("different number of results");
2559 if (results1 != NULL)
2561 Typed_identifier_list::const_iterator res1 = results1->begin();
2562 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2563 res2 != results2->end();
2566 if (res1 == results1->end())
2569 *reason = _("different number of results");
2573 if (!Type::are_identical(res1->type(), res2->type(),
2574 errors_are_identical, NULL))
2577 *reason = _("different result types");
2581 if (res1 != results1->end())
2584 *reason = _("different number of results");
2595 Function_type::do_hash_for_method(Gogo* gogo) const
2597 unsigned int ret = 0;
2598 // We ignore the receiver type for hash codes, because we need to
2599 // get the same hash code for a method in an interface and a method
2600 // declared for a type. The former will not have a receiver.
2601 if (this->parameters_ != NULL)
2604 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
2605 p != this->parameters_->end();
2607 ret += p->type()->hash_for_method(gogo) << shift;
2609 if (this->results_ != NULL)
2612 for (Typed_identifier_list::const_iterator p = this->results_->begin();
2613 p != this->results_->end();
2615 ret += p->type()->hash_for_method(gogo) << shift;
2617 if (this->is_varargs_)
2623 // Get the tree for a function type.
2626 Function_type::do_get_tree(Gogo* gogo)
2628 tree args = NULL_TREE;
2631 if (this->receiver_ != NULL)
2633 Type* rtype = this->receiver_->type();
2634 tree ptype = rtype->get_tree(gogo);
2635 if (ptype == error_mark_node)
2636 return error_mark_node;
2638 // We always pass the address of the receiver parameter, in
2639 // order to make interface calls work with unknown types.
2640 if (rtype->points_to() == NULL)
2641 ptype = build_pointer_type(ptype);
2643 *pp = tree_cons (NULL_TREE, ptype, NULL_TREE);
2644 pp = &TREE_CHAIN (*pp);
2647 if (this->parameters_ != NULL)
2649 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
2650 p != this->parameters_->end();
2653 tree ptype = p->type()->get_tree(gogo);
2654 if (ptype == error_mark_node)
2655 return error_mark_node;
2656 *pp = tree_cons (NULL_TREE, ptype, NULL_TREE);
2657 pp = &TREE_CHAIN (*pp);
2661 // Varargs is handled entirely at the Go level. At the tree level,
2662 // functions are not varargs.
2663 *pp = void_list_node;
2666 if (this->results_ == NULL)
2667 result = void_type_node;
2668 else if (this->results_->size() == 1)
2669 result = this->results_->begin()->type()->get_tree(gogo);
2672 result = make_node(RECORD_TYPE);
2673 tree field_trees = NULL_TREE;
2674 tree* pp = &field_trees;
2675 for (Typed_identifier_list::const_iterator p = this->results_->begin();
2676 p != this->results_->end();
2679 const std::string name = (p->name().empty()
2681 : Gogo::unpack_hidden_name(p->name()));
2682 tree name_tree = get_identifier_with_length(name.data(),
2684 tree field_type_tree = p->type()->get_tree(gogo);
2685 if (field_type_tree == error_mark_node)
2686 return error_mark_node;
2687 tree field = build_decl(this->location_, FIELD_DECL, name_tree,
2689 DECL_CONTEXT(field) = result;
2691 pp = &DECL_CHAIN(field);
2693 TYPE_FIELDS(result) = field_trees;
2694 layout_type(result);
2697 if (result == error_mark_node)
2698 return error_mark_node;
2700 tree fntype = build_function_type(result, args);
2701 if (fntype == error_mark_node)
2704 return build_pointer_type(fntype);
2707 // Functions are initialized to NULL.
2710 Function_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
2714 return fold_convert(type_tree, null_pointer_node);
2717 // The type of a function type descriptor.
2720 Function_type::make_function_type_descriptor_type()
2725 Type* tdt = Type::make_type_descriptor_type();
2726 Type* ptdt = Type::make_type_descriptor_ptr_type();
2728 Type* bool_type = Type::lookup_bool_type();
2730 Type* slice_type = Type::make_array_type(ptdt, NULL);
2732 Struct_type* s = Type::make_builtin_struct_type(4,
2734 "dotdotdot", bool_type,
2738 ret = Type::make_builtin_named_type("FuncType", s);
2744 // The type descriptor for a function type.
2747 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2749 source_location bloc = BUILTINS_LOCATION;
2751 Type* ftdt = Function_type::make_function_type_descriptor_type();
2753 const Struct_field_list* fields = ftdt->struct_type()->fields();
2755 Expression_list* vals = new Expression_list();
2758 Struct_field_list::const_iterator p = fields->begin();
2759 gcc_assert(p->field_name() == "commonType");
2760 vals->push_back(this->type_descriptor_constructor(gogo,
2761 RUNTIME_TYPE_KIND_FUNC,
2765 gcc_assert(p->field_name() == "dotdotdot");
2766 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
2769 gcc_assert(p->field_name() == "in");
2770 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
2771 this->parameters()));
2774 gcc_assert(p->field_name() == "out");
2775 vals->push_back(this->type_descriptor_params(p->type(), NULL,
2779 gcc_assert(p == fields->end());
2781 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
2784 // Return a composite literal for the parameters or results of a type
2788 Function_type::type_descriptor_params(Type* params_type,
2789 const Typed_identifier* receiver,
2790 const Typed_identifier_list* params)
2792 source_location bloc = BUILTINS_LOCATION;
2794 if (receiver == NULL && params == NULL)
2795 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
2797 Expression_list* vals = new Expression_list();
2798 vals->reserve((params == NULL ? 0 : params->size())
2799 + (receiver != NULL ? 1 : 0));
2801 if (receiver != NULL)
2803 Type* rtype = receiver->type();
2804 // The receiver is always passed as a pointer. FIXME: Is this
2805 // right? Should that fact affect the type descriptor?
2806 if (rtype->points_to() == NULL)
2807 rtype = Type::make_pointer_type(rtype);
2808 vals->push_back(Expression::make_type_descriptor(rtype, bloc));
2813 for (Typed_identifier_list::const_iterator p = params->begin();
2816 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
2819 return Expression::make_slice_composite_literal(params_type, vals, bloc);
2822 // The reflection string.
2825 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
2827 // FIXME: Turn this off until we straighten out the type of the
2828 // struct field used in a go statement which calls a method.
2829 // gcc_assert(this->receiver_ == NULL);
2831 ret->append("func");
2833 if (this->receiver_ != NULL)
2835 ret->push_back('(');
2836 this->append_reflection(this->receiver_->type(), gogo, ret);
2837 ret->push_back(')');
2840 ret->push_back('(');
2841 const Typed_identifier_list* params = this->parameters();
2844 bool is_varargs = this->is_varargs_;
2845 for (Typed_identifier_list::const_iterator p = params->begin();
2849 if (p != params->begin())
2851 if (!is_varargs || p + 1 != params->end())
2852 this->append_reflection(p->type(), gogo, ret);
2856 this->append_reflection(p->type()->array_type()->element_type(),
2861 ret->push_back(')');
2863 const Typed_identifier_list* results = this->results();
2864 if (results != NULL && !results->empty())
2866 if (results->size() == 1)
2867 ret->push_back(' ');
2870 for (Typed_identifier_list::const_iterator p = results->begin();
2871 p != results->end();
2874 if (p != results->begin())
2876 this->append_reflection(p->type(), gogo, ret);
2878 if (results->size() > 1)
2879 ret->push_back(')');
2886 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
2888 ret->push_back('F');
2890 if (this->receiver_ != NULL)
2892 ret->push_back('m');
2893 this->append_mangled_name(this->receiver_->type(), gogo, ret);
2896 const Typed_identifier_list* params = this->parameters();
2899 ret->push_back('p');
2900 for (Typed_identifier_list::const_iterator p = params->begin();
2903 this->append_mangled_name(p->type(), gogo, ret);
2904 if (this->is_varargs_)
2905 ret->push_back('V');
2906 ret->push_back('e');
2909 const Typed_identifier_list* results = this->results();
2910 if (results != NULL)
2912 ret->push_back('r');
2913 for (Typed_identifier_list::const_iterator p = results->begin();
2914 p != results->end();
2916 this->append_mangled_name(p->type(), gogo, ret);
2917 ret->push_back('e');
2920 ret->push_back('e');
2923 // Export a function type.
2926 Function_type::do_export(Export* exp) const
2928 // We don't write out the receiver. The only function types which
2929 // should have a receiver are the ones associated with explicitly
2930 // defined methods. For those the receiver type is written out by
2931 // Function::export_func.
2933 exp->write_c_string("(");
2935 if (this->parameters_ != NULL)
2937 bool is_varargs = this->is_varargs_;
2938 for (Typed_identifier_list::const_iterator p =
2939 this->parameters_->begin();
2940 p != this->parameters_->end();
2946 exp->write_c_string(", ");
2947 if (!is_varargs || p + 1 != this->parameters_->end())
2948 exp->write_type(p->type());
2951 exp->write_c_string("...");
2952 exp->write_type(p->type()->array_type()->element_type());
2956 exp->write_c_string(")");
2958 const Typed_identifier_list* results = this->results_;
2959 if (results != NULL)
2961 exp->write_c_string(" ");
2962 if (results->size() == 1)
2963 exp->write_type(results->begin()->type());
2967 exp->write_c_string("(");
2968 for (Typed_identifier_list::const_iterator p = results->begin();
2969 p != results->end();
2975 exp->write_c_string(", ");
2976 exp->write_type(p->type());
2978 exp->write_c_string(")");
2983 // Import a function type.
2986 Function_type::do_import(Import* imp)
2988 imp->require_c_string("(");
2989 Typed_identifier_list* parameters;
2990 bool is_varargs = false;
2991 if (imp->peek_char() == ')')
2995 parameters = new Typed_identifier_list();
2998 if (imp->match_c_string("..."))
3004 Type* ptype = imp->read_type();
3006 ptype = Type::make_array_type(ptype, NULL);
3007 parameters->push_back(Typed_identifier(Import::import_marker,
3008 ptype, imp->location()));
3009 if (imp->peek_char() != ',')
3011 gcc_assert(!is_varargs);
3012 imp->require_c_string(", ");
3015 imp->require_c_string(")");
3017 Typed_identifier_list* results;
3018 if (imp->peek_char() != ' ')
3023 results = new Typed_identifier_list;
3024 if (imp->peek_char() != '(')
3026 Type* rtype = imp->read_type();
3027 results->push_back(Typed_identifier(Import::import_marker, rtype,
3035 Type* rtype = imp->read_type();
3036 results->push_back(Typed_identifier(Import::import_marker,
3037 rtype, imp->location()));
3038 if (imp->peek_char() != ',')
3040 imp->require_c_string(", ");
3042 imp->require_c_string(")");
3046 Function_type* ret = Type::make_function_type(NULL, parameters, results,
3049 ret->set_is_varargs();
3053 // Make a copy of a function type without a receiver.
3056 Function_type::copy_without_receiver() const
3058 gcc_assert(this->is_method());
3059 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
3062 if (this->is_varargs())
3063 ret->set_is_varargs();
3064 if (this->is_builtin())
3065 ret->set_is_builtin();
3069 // Make a copy of a function type with a receiver.
3072 Function_type::copy_with_receiver(Type* receiver_type) const
3074 gcc_assert(!this->is_method());
3075 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3077 return Type::make_function_type(receiver, this->parameters_,
3078 this->results_, this->location_);
3081 // Make a function type.
3084 Type::make_function_type(Typed_identifier* receiver,
3085 Typed_identifier_list* parameters,
3086 Typed_identifier_list* results,
3087 source_location location)
3089 return new Function_type(receiver, parameters, results, location);
3092 // Class Pointer_type.
3097 Pointer_type::do_traverse(Traverse* traverse)
3099 return Type::traverse(this->to_type_, traverse);
3105 Pointer_type::do_hash_for_method(Gogo* gogo) const
3107 return this->to_type_->hash_for_method(gogo) << 4;
3110 // The tree for a pointer type.
3113 Pointer_type::do_get_tree(Gogo* gogo)
3115 return build_pointer_type(this->to_type_->get_tree(gogo));
3118 // Initialize a pointer type.
3121 Pointer_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
3125 return fold_convert(type_tree, null_pointer_node);
3128 // The type of a pointer type descriptor.
3131 Pointer_type::make_pointer_type_descriptor_type()
3136 Type* tdt = Type::make_type_descriptor_type();
3137 Type* ptdt = Type::make_type_descriptor_ptr_type();
3139 Struct_type* s = Type::make_builtin_struct_type(2,
3143 ret = Type::make_builtin_named_type("PtrType", s);
3149 // The type descriptor for a pointer type.
3152 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3154 if (this->is_unsafe_pointer_type())
3156 gcc_assert(name != NULL);
3157 return this->plain_type_descriptor(gogo,
3158 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
3163 source_location bloc = BUILTINS_LOCATION;
3165 const Methods* methods;
3166 Type* deref = this->points_to();
3167 if (deref->named_type() != NULL)
3168 methods = deref->named_type()->methods();
3169 else if (deref->struct_type() != NULL)
3170 methods = deref->struct_type()->methods();
3174 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
3176 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
3178 Expression_list* vals = new Expression_list();
3181 Struct_field_list::const_iterator p = fields->begin();
3182 gcc_assert(p->field_name() == "commonType");
3183 vals->push_back(this->type_descriptor_constructor(gogo,
3184 RUNTIME_TYPE_KIND_PTR,
3185 name, methods, false));
3188 gcc_assert(p->field_name() == "elem");
3189 vals->push_back(Expression::make_type_descriptor(deref, bloc));
3191 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
3195 // Reflection string.
3198 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
3200 ret->push_back('*');
3201 this->append_reflection(this->to_type_, gogo, ret);
3207 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3209 ret->push_back('p');
3210 this->append_mangled_name(this->to_type_, gogo, ret);
3216 Pointer_type::do_export(Export* exp) const
3218 exp->write_c_string("*");
3219 if (this->is_unsafe_pointer_type())
3220 exp->write_c_string("any");
3222 exp->write_type(this->to_type_);
3228 Pointer_type::do_import(Import* imp)
3230 imp->require_c_string("*");
3231 if (imp->match_c_string("any"))
3234 return Type::make_pointer_type(Type::make_void_type());
3236 Type* to = imp->read_type();
3237 return Type::make_pointer_type(to);
3240 // Make a pointer type.
3243 Type::make_pointer_type(Type* to_type)
3245 typedef Unordered_map(Type*, Pointer_type*) Hashtable;
3246 static Hashtable pointer_types;
3247 Hashtable::const_iterator p = pointer_types.find(to_type);
3248 if (p != pointer_types.end())
3250 Pointer_type* ret = new Pointer_type(to_type);
3251 pointer_types[to_type] = ret;
3255 // The nil type. We use a special type for nil because it is not the
3256 // same as any other type. In C term nil has type void*, but there is
3257 // no such type in Go.
3259 class Nil_type : public Type
3269 { return ptr_type_node; }
3272 do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
3273 { return is_clear ? NULL : fold_convert(type_tree, null_pointer_node); }
3276 do_type_descriptor(Gogo*, Named_type*)
3277 { gcc_unreachable(); }
3280 do_reflection(Gogo*, std::string*) const
3281 { gcc_unreachable(); }
3284 do_mangled_name(Gogo*, std::string* ret) const
3285 { ret->push_back('n'); }
3288 // Make the nil type.
3291 Type::make_nil_type()
3293 static Nil_type singleton_nil_type;
3294 return &singleton_nil_type;
3297 // The type of a function call which returns multiple values. This is
3298 // really a struct, but we don't want to confuse a function call which
3299 // returns a struct with a function call which returns multiple
3302 class Call_multiple_result_type : public Type
3305 Call_multiple_result_type(Call_expression* call)
3306 : Type(TYPE_CALL_MULTIPLE_RESULT),
3312 do_has_pointer() const
3314 gcc_assert(saw_errors());
3322 do_get_init_tree(Gogo*, tree, bool)
3324 gcc_assert(saw_errors());
3325 return error_mark_node;
3329 do_type_descriptor(Gogo*, Named_type*)
3331 gcc_assert(saw_errors());
3332 return Expression::make_error(UNKNOWN_LOCATION);
3336 do_reflection(Gogo*, std::string*) const
3337 { gcc_assert(saw_errors()); }
3340 do_mangled_name(Gogo*, std::string*) const
3341 { gcc_assert(saw_errors()); }
3344 // The expression being called.
3345 Call_expression* call_;
3348 // Return the tree for a call result.
3351 Call_multiple_result_type::do_get_tree(Gogo* gogo)
3353 Function_type* fntype = this->call_->get_function_type();
3354 gcc_assert(fntype != NULL);
3355 const Typed_identifier_list* results = fntype->results();
3356 gcc_assert(results != NULL && results->size() > 1);
3357 tree fntype_tree = fntype->get_tree(gogo);
3358 if (fntype_tree == error_mark_node)
3359 return error_mark_node;
3360 return TREE_TYPE(fntype_tree);
3363 // Make a call result type.
3366 Type::make_call_multiple_result_type(Call_expression* call)
3368 return new Call_multiple_result_type(call);
3371 // Class Struct_field.
3373 // Get the name of a field.
3376 Struct_field::field_name() const
3378 const std::string& name(this->typed_identifier_.name());
3383 // This is called during parsing, before anything is lowered, so
3384 // we have to be pretty careful to avoid dereferencing an
3385 // unknown type name.
3386 Type* t = this->typed_identifier_.type();
3388 if (t->classification() == Type::TYPE_POINTER)
3391 Pointer_type* ptype = static_cast<Pointer_type*>(t);
3392 dt = ptype->points_to();
3394 if (dt->forward_declaration_type() != NULL)
3395 return dt->forward_declaration_type()->name();
3396 else if (dt->named_type() != NULL)
3397 return dt->named_type()->name();
3398 else if (t->is_error_type() || dt->is_error_type())
3400 static const std::string error_string = "*error*";
3401 return error_string;
3405 // Avoid crashing in the erroneous case where T is named but
3407 gcc_assert(t != dt);
3408 if (t->forward_declaration_type() != NULL)
3409 return t->forward_declaration_type()->name();
3410 else if (t->named_type() != NULL)
3411 return t->named_type()->name();
3418 // Class Struct_type.
3423 Struct_type::do_traverse(Traverse* traverse)
3425 Struct_field_list* fields = this->fields_;
3428 for (Struct_field_list::iterator p = fields->begin();
3432 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
3433 return TRAVERSE_EXIT;
3436 return TRAVERSE_CONTINUE;
3439 // Verify that the struct type is complete and valid.
3442 Struct_type::do_verify()
3444 Struct_field_list* fields = this->fields_;
3448 for (Struct_field_list::iterator p = fields->begin();
3452 Type* t = p->type();
3453 if (t->is_undefined())
3455 error_at(p->location(), "struct field type is incomplete");
3456 p->set_type(Type::make_error_type());
3459 else if (p->is_anonymous())
3461 if (t->named_type() != NULL && t->points_to() != NULL)
3463 error_at(p->location(), "embedded type may not be a pointer");
3464 p->set_type(Type::make_error_type());
3472 // Whether this contains a pointer.
3475 Struct_type::do_has_pointer() const
3477 const Struct_field_list* fields = this->fields();
3480 for (Struct_field_list::const_iterator p = fields->begin();
3484 if (p->type()->has_pointer())
3490 // Whether this type is identical to T.
3493 Struct_type::is_identical(const Struct_type* t,
3494 bool errors_are_identical) const
3496 const Struct_field_list* fields1 = this->fields();
3497 const Struct_field_list* fields2 = t->fields();
3498 if (fields1 == NULL || fields2 == NULL)
3499 return fields1 == fields2;
3500 Struct_field_list::const_iterator pf2 = fields2->begin();
3501 for (Struct_field_list::const_iterator pf1 = fields1->begin();
3502 pf1 != fields1->end();
3505 if (pf2 == fields2->end())
3507 if (pf1->field_name() != pf2->field_name())
3509 if (pf1->is_anonymous() != pf2->is_anonymous()
3510 || !Type::are_identical(pf1->type(), pf2->type(),
3511 errors_are_identical, NULL))
3513 if (!pf1->has_tag())
3520 if (!pf2->has_tag())
3522 if (pf1->tag() != pf2->tag())
3526 if (pf2 != fields2->end())
3531 // Whether this struct type has any hidden fields.
3534 Struct_type::struct_has_hidden_fields(const Named_type* within,
3535 std::string* reason) const
3537 const Struct_field_list* fields = this->fields();
3540 const Package* within_package = (within == NULL
3542 : within->named_object()->package());
3543 for (Struct_field_list::const_iterator pf = fields->begin();
3544 pf != fields->end();
3547 if (within_package != NULL
3548 && !pf->is_anonymous()
3549 && Gogo::is_hidden_name(pf->field_name()))
3553 std::string within_name = within->named_object()->message_name();
3554 std::string name = Gogo::message_name(pf->field_name());
3555 size_t bufsize = 200 + within_name.length() + name.length();
3556 char* buf = new char[bufsize];
3557 snprintf(buf, bufsize,
3558 _("implicit assignment of %s%s%s hidden field %s%s%s"),
3559 open_quote, within_name.c_str(), close_quote,
3560 open_quote, name.c_str(), close_quote);
3561 reason->assign(buf);
3567 if (pf->type()->has_hidden_fields(within, reason))
3577 Struct_type::do_hash_for_method(Gogo* gogo) const
3579 unsigned int ret = 0;
3580 if (this->fields() != NULL)
3582 for (Struct_field_list::const_iterator pf = this->fields()->begin();
3583 pf != this->fields()->end();
3585 ret = (ret << 1) + pf->type()->hash_for_method(gogo);
3590 // Find the local field NAME.
3593 Struct_type::find_local_field(const std::string& name,
3594 unsigned int *pindex) const
3596 const Struct_field_list* fields = this->fields_;
3600 for (Struct_field_list::const_iterator pf = fields->begin();
3601 pf != fields->end();
3604 if (pf->field_name() == name)
3614 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
3616 Field_reference_expression*
3617 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
3618 source_location location) const
3621 return this->field_reference_depth(struct_expr, name, location, NULL,
3625 // Return an expression for a field, along with the depth at which it
3628 Field_reference_expression*
3629 Struct_type::field_reference_depth(Expression* struct_expr,
3630 const std::string& name,
3631 source_location location,
3632 Saw_named_type* saw,
3633 unsigned int* depth) const
3635 const Struct_field_list* fields = this->fields_;
3639 // Look for a field with this name.
3641 for (Struct_field_list::const_iterator pf = fields->begin();
3642 pf != fields->end();
3645 if (pf->field_name() == name)
3648 return Expression::make_field_reference(struct_expr, i, location);
3652 // Look for an anonymous field which contains a field with this
3654 unsigned int found_depth = 0;
3655 Field_reference_expression* ret = NULL;
3657 for (Struct_field_list::const_iterator pf = fields->begin();
3658 pf != fields->end();
3661 if (!pf->is_anonymous())
3664 Struct_type* st = pf->type()->deref()->struct_type();
3668 Saw_named_type* hold_saw = saw;
3669 Saw_named_type saw_here;
3670 Named_type* nt = pf->type()->named_type();
3672 nt = pf->type()->deref()->named_type();
3676 for (q = saw; q != NULL; q = q->next)
3680 // If this is an error, it will be reported
3687 saw_here.next = saw;
3692 // Look for a reference using a NULL struct expression. If we
3693 // find one, fill in the struct expression with a reference to
3695 unsigned int subdepth;
3696 Field_reference_expression* sub = st->field_reference_depth(NULL, name,
3706 if (ret == NULL || subdepth < found_depth)
3711 found_depth = subdepth;
3712 Expression* here = Expression::make_field_reference(struct_expr, i,
3714 if (pf->type()->points_to() != NULL)
3715 here = Expression::make_unary(OPERATOR_MULT, here, location);
3716 while (sub->expr() != NULL)
3718 sub = sub->expr()->deref()->field_reference_expression();
3719 gcc_assert(sub != NULL);
3721 sub->set_struct_expression(here);
3723 else if (subdepth > found_depth)
3727 // We do not handle ambiguity here--it should be handled by
3728 // Type::bind_field_or_method.
3736 *depth = found_depth + 1;
3741 // Return the total number of fields, including embedded fields.
3744 Struct_type::total_field_count() const
3746 if (this->fields_ == NULL)
3748 unsigned int ret = 0;
3749 for (Struct_field_list::const_iterator pf = this->fields_->begin();
3750 pf != this->fields_->end();
3753 if (!pf->is_anonymous() || pf->type()->deref()->struct_type() == NULL)
3756 ret += pf->type()->struct_type()->total_field_count();
3761 // Return whether NAME is an unexported field, for better error reporting.
3764 Struct_type::is_unexported_local_field(Gogo* gogo,
3765 const std::string& name) const
3767 const Struct_field_list* fields = this->fields_;
3770 for (Struct_field_list::const_iterator pf = fields->begin();
3771 pf != fields->end();
3774 const std::string& field_name(pf->field_name());
3775 if (Gogo::is_hidden_name(field_name)
3776 && name == Gogo::unpack_hidden_name(field_name)
3777 && gogo->pack_hidden_name(name, false) != field_name)
3784 // Finalize the methods of an unnamed struct.
3787 Struct_type::finalize_methods(Gogo* gogo)
3789 if (this->all_methods_ != NULL)
3791 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
3794 // Return the method NAME, or NULL if there isn't one or if it is
3795 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
3799 Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
3801 return Type::method_function(this->all_methods_, name, is_ambiguous);
3804 // Get the tree for a struct type.
3807 Struct_type::do_get_tree(Gogo* gogo)
3809 tree type = make_node(RECORD_TYPE);
3810 return this->fill_in_tree(gogo, type);
3813 // Fill in the fields for a struct type.
3816 Struct_type::fill_in_tree(Gogo* gogo, tree type)
3818 tree field_trees = NULL_TREE;
3819 tree* pp = &field_trees;
3820 for (Struct_field_list::const_iterator p = this->fields_->begin();
3821 p != this->fields_->end();
3824 std::string name = Gogo::unpack_hidden_name(p->field_name());
3825 tree name_tree = get_identifier_with_length(name.data(), name.length());
3827 tree field_type_tree = p->type()->get_tree(gogo);
3828 if (field_type_tree == error_mark_node)
3829 return error_mark_node;
3830 gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
3832 tree field = build_decl(p->location(), FIELD_DECL, name_tree,
3834 DECL_CONTEXT(field) = type;
3836 pp = &DECL_CHAIN(field);
3839 TYPE_FIELDS(type) = field_trees;
3846 // Initialize struct fields.
3849 Struct_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
3851 if (this->fields_ == NULL || this->fields_->empty())
3857 tree ret = build_constructor(type_tree,
3858 VEC_alloc(constructor_elt, gc, 0));
3859 TREE_CONSTANT(ret) = 1;
3864 bool is_constant = true;
3865 bool any_fields_set = false;
3866 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc,
3867 this->fields_->size());
3869 tree field = TYPE_FIELDS(type_tree);
3870 for (Struct_field_list::const_iterator p = this->fields_->begin();
3871 p != this->fields_->end();
3872 ++p, field = DECL_CHAIN(field))
3874 tree value = p->type()->get_init_tree(gogo, is_clear);
3875 if (value == error_mark_node)
3876 return error_mark_node;
3877 gcc_assert(field != NULL_TREE);
3880 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
3883 any_fields_set = true;
3884 if (!TREE_CONSTANT(value))
3885 is_constant = false;
3888 gcc_assert(field == NULL_TREE);
3890 if (!any_fields_set)
3892 gcc_assert(is_clear);
3893 VEC_free(constructor_elt, gc, init);
3897 tree ret = build_constructor(type_tree, init);
3899 TREE_CONSTANT(ret) = 1;
3903 // The type of a struct type descriptor.
3906 Struct_type::make_struct_type_descriptor_type()
3911 Type* tdt = Type::make_type_descriptor_type();
3912 Type* ptdt = Type::make_type_descriptor_ptr_type();
3914 Type* uintptr_type = Type::lookup_integer_type("uintptr");
3915 Type* string_type = Type::lookup_string_type();
3916 Type* pointer_string_type = Type::make_pointer_type(string_type);
3919 Type::make_builtin_struct_type(5,
3920 "name", pointer_string_type,
3921 "pkgPath", pointer_string_type,
3923 "tag", pointer_string_type,
3924 "offset", uintptr_type);
3925 Type* nsf = Type::make_builtin_named_type("structField", sf);
3927 Type* slice_type = Type::make_array_type(nsf, NULL);
3929 Struct_type* s = Type::make_builtin_struct_type(2,
3931 "fields", slice_type);
3933 ret = Type::make_builtin_named_type("StructType", s);
3939 // Build a type descriptor for a struct type.
3942 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3944 source_location bloc = BUILTINS_LOCATION;
3946 Type* stdt = Struct_type::make_struct_type_descriptor_type();
3948 const Struct_field_list* fields = stdt->struct_type()->fields();
3950 Expression_list* vals = new Expression_list();
3953 const Methods* methods = this->methods();
3954 // A named struct should not have methods--the methods should attach
3955 // to the named type.
3956 gcc_assert(methods == NULL || name == NULL);
3958 Struct_field_list::const_iterator ps = fields->begin();
3959 gcc_assert(ps->field_name() == "commonType");
3960 vals->push_back(this->type_descriptor_constructor(gogo,
3961 RUNTIME_TYPE_KIND_STRUCT,
3962 name, methods, true));
3965 gcc_assert(ps->field_name() == "fields");
3967 Expression_list* elements = new Expression_list();
3968 elements->reserve(this->fields_->size());
3969 Type* element_type = ps->type()->array_type()->element_type();
3970 for (Struct_field_list::const_iterator pf = this->fields_->begin();
3971 pf != this->fields_->end();
3974 const Struct_field_list* f = element_type->struct_type()->fields();
3976 Expression_list* fvals = new Expression_list();
3979 Struct_field_list::const_iterator q = f->begin();
3980 gcc_assert(q->field_name() == "name");
3981 if (pf->is_anonymous())
3982 fvals->push_back(Expression::make_nil(bloc));
3985 std::string n = Gogo::unpack_hidden_name(pf->field_name());
3986 Expression* s = Expression::make_string(n, bloc);
3987 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3991 gcc_assert(q->field_name() == "pkgPath");
3992 if (!Gogo::is_hidden_name(pf->field_name()))
3993 fvals->push_back(Expression::make_nil(bloc));
3996 std::string n = Gogo::hidden_name_prefix(pf->field_name());
3997 Expression* s = Expression::make_string(n, bloc);
3998 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4002 gcc_assert(q->field_name() == "typ");
4003 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
4006 gcc_assert(q->field_name() == "tag");
4008 fvals->push_back(Expression::make_nil(bloc));
4011 Expression* s = Expression::make_string(pf->tag(), bloc);
4012 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
4016 gcc_assert(q->field_name() == "offset");
4017 fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
4019 Expression* v = Expression::make_struct_composite_literal(element_type,
4021 elements->push_back(v);
4024 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
4027 return Expression::make_struct_composite_literal(stdt, vals, bloc);
4030 // Reflection string.
4033 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
4035 ret->append("struct { ");
4037 for (Struct_field_list::const_iterator p = this->fields_->begin();
4038 p != this->fields_->end();
4041 if (p != this->fields_->begin())
4043 if (p->is_anonymous())
4044 ret->push_back('?');
4046 ret->append(Gogo::unpack_hidden_name(p->field_name()));
4047 ret->push_back(' ');
4048 this->append_reflection(p->type(), gogo, ret);
4052 const std::string& tag(p->tag());
4054 for (std::string::const_iterator p = tag.begin();
4059 ret->append("\\x00");
4060 else if (*p == '\n')
4062 else if (*p == '\t')
4065 ret->append("\\\"");
4066 else if (*p == '\\')
4067 ret->append("\\\\");
4071 ret->push_back('"');
4081 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4083 ret->push_back('S');
4085 const Struct_field_list* fields = this->fields_;
4088 for (Struct_field_list::const_iterator p = fields->begin();
4092 if (p->is_anonymous())
4096 std::string n = Gogo::unpack_hidden_name(p->field_name());
4098 snprintf(buf, sizeof buf, "%u_",
4099 static_cast<unsigned int>(n.length()));
4103 this->append_mangled_name(p->type(), gogo, ret);
4106 const std::string& tag(p->tag());
4108 for (std::string::const_iterator p = tag.begin();
4112 if (ISALNUM(*p) || *p == '_')
4117 snprintf(buf, sizeof buf, ".%x.",
4118 static_cast<unsigned int>(*p));
4123 snprintf(buf, sizeof buf, "T%u_",
4124 static_cast<unsigned int>(out.length()));
4131 ret->push_back('e');
4137 Struct_type::do_export(Export* exp) const
4139 exp->write_c_string("struct { ");
4140 const Struct_field_list* fields = this->fields_;
4141 gcc_assert(fields != NULL);
4142 for (Struct_field_list::const_iterator p = fields->begin();
4146 if (p->is_anonymous())
4147 exp->write_string("? ");
4150 exp->write_string(p->field_name());
4151 exp->write_c_string(" ");
4153 exp->write_type(p->type());
4157 exp->write_c_string(" ");
4158 Expression* expr = Expression::make_string(p->tag(),
4160 expr->export_expression(exp);
4164 exp->write_c_string("; ");
4166 exp->write_c_string("}");
4172 Struct_type::do_import(Import* imp)
4174 imp->require_c_string("struct { ");
4175 Struct_field_list* fields = new Struct_field_list;
4176 if (imp->peek_char() != '}')
4181 if (imp->match_c_string("? "))
4185 name = imp->read_identifier();
4186 imp->require_c_string(" ");
4188 Type* ftype = imp->read_type();
4190 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
4192 if (imp->peek_char() == ' ')
4195 Expression* expr = Expression::import_expression(imp);
4196 String_expression* sexpr = expr->string_expression();
4197 gcc_assert(sexpr != NULL);
4198 sf.set_tag(sexpr->val());
4202 imp->require_c_string("; ");
4203 fields->push_back(sf);
4204 if (imp->peek_char() == '}')
4208 imp->require_c_string("}");
4210 return Type::make_struct_type(fields, imp->location());
4213 // Make a struct type.
4216 Type::make_struct_type(Struct_field_list* fields,
4217 source_location location)
4219 return new Struct_type(fields, location);
4222 // Class Array_type.
4224 // Whether two array types are identical.
4227 Array_type::is_identical(const Array_type* t, bool errors_are_identical) const
4229 if (!Type::are_identical(this->element_type(), t->element_type(),
4230 errors_are_identical, NULL))
4233 Expression* l1 = this->length();
4234 Expression* l2 = t->length();
4236 // Slices of the same element type are identical.
4237 if (l1 == NULL && l2 == NULL)
4240 // Arrays of the same element type are identical if they have the
4242 if (l1 != NULL && l2 != NULL)
4247 // Try to determine the lengths. If we can't, assume the arrays
4248 // are not identical.
4256 if (l1->integer_constant_value(true, v1, &type1)
4257 && l2->integer_constant_value(true, v2, &type2))
4258 ret = mpz_cmp(v1, v2) == 0;
4264 // Otherwise the arrays are not identical.
4271 Array_type::do_traverse(Traverse* traverse)
4273 if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
4274 return TRAVERSE_EXIT;
4275 if (this->length_ != NULL
4276 && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
4277 return TRAVERSE_EXIT;
4278 return TRAVERSE_CONTINUE;
4281 // Check that the length is valid.
4284 Array_type::verify_length()
4286 if (this->length_ == NULL)
4289 Type_context context(Type::lookup_integer_type("int"), false);
4290 this->length_->determine_type(&context);
4292 if (!this->length_->is_constant())
4294 error_at(this->length_->location(), "array bound is not constant");
4301 if (!this->length_->integer_constant_value(true, val, &vt))
4305 if (!this->length_->float_constant_value(fval, &vt))
4307 if (this->length_->type()->integer_type() != NULL
4308 || this->length_->type()->float_type() != NULL)
4309 error_at(this->length_->location(),
4310 "array bound is not constant");
4312 error_at(this->length_->location(),
4313 "array bound is not numeric");
4318 if (!mpfr_integer_p(fval))
4320 error_at(this->length_->location(),
4321 "array bound truncated to integer");
4327 mpfr_get_z(val, fval, GMP_RNDN);
4331 if (mpz_sgn(val) < 0)
4333 error_at(this->length_->location(), "negative array bound");
4338 Type* int_type = Type::lookup_integer_type("int");
4339 int tbits = int_type->integer_type()->bits();
4340 int vbits = mpz_sizeinbase(val, 2);
4341 if (vbits + 1 > tbits)
4343 error_at(this->length_->location(), "array bound overflows");
4356 Array_type::do_verify()
4358 if (!this->verify_length())
4360 this->length_ = Expression::make_error(this->length_->location());
4366 // Array type hash code.
4369 Array_type::do_hash_for_method(Gogo* gogo) const
4371 // There is no very convenient way to get a hash code for the
4373 return this->element_type_->hash_for_method(gogo) + 1;
4376 // See if the expression passed to make is suitable. The first
4377 // argument is required, and gives the length. An optional second
4378 // argument is permitted for the capacity.
4381 Array_type::do_check_make_expression(Expression_list* args,
4382 source_location location)
4384 gcc_assert(this->length_ == NULL);
4385 if (args == NULL || args->empty())
4387 error_at(location, "length required when allocating a slice");
4390 else if (args->size() > 2)
4392 error_at(location, "too many expressions passed to make");
4397 if (!Type::check_int_value(args->front(),
4398 _("bad length when making slice"), location))
4401 if (args->size() > 1)
4403 if (!Type::check_int_value(args->back(),
4404 _("bad capacity when making slice"),
4413 // Get a tree for the length of a fixed array. The length may be
4414 // computed using a function call, so we must only evaluate it once.
4417 Array_type::get_length_tree(Gogo* gogo)
4419 gcc_assert(this->length_ != NULL);
4420 if (this->length_tree_ == NULL_TREE)
4425 if (this->length_->integer_constant_value(true, val, &t))
4428 t = Type::lookup_integer_type("int");
4429 else if (t->is_abstract())
4430 t = t->make_non_abstract_type();
4431 tree tt = t->get_tree(gogo);
4432 this->length_tree_ = Expression::integer_constant_tree(val, tt);
4439 // Make up a translation context for the array length
4440 // expression. FIXME: This won't work in general.
4441 Translate_context context(gogo, NULL, NULL, NULL_TREE);
4442 tree len = this->length_->get_tree(&context);
4443 if (len != error_mark_node)
4445 len = convert_to_integer(integer_type_node, len);
4446 len = save_expr(len);
4448 this->length_tree_ = len;
4451 return this->length_tree_;
4454 // Get a tree for the type of this array. A fixed array is simply
4455 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
4456 // just like an array in C. An open array is a struct with three
4457 // fields: a data pointer, the length, and the capacity.
4460 Array_type::do_get_tree(Gogo* gogo)
4462 if (this->length_ == NULL)
4464 tree struct_type = gogo->slice_type_tree(void_type_node);
4465 return this->fill_in_slice_tree(gogo, struct_type);
4469 tree array_type = make_node(ARRAY_TYPE);
4470 return this->fill_in_array_tree(gogo, array_type);
4474 // Fill in the fields for an array type. This is used for named array
4478 Array_type::fill_in_array_tree(Gogo* gogo, tree array_type)
4480 gcc_assert(this->length_ != NULL);
4482 tree element_type_tree = this->element_type_->get_tree(gogo);
4483 tree length_tree = this->get_length_tree(gogo);
4484 if (element_type_tree == error_mark_node
4485 || length_tree == error_mark_node)
4486 return error_mark_node;
4488 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
4490 length_tree = fold_convert(sizetype, length_tree);
4492 // build_index_type takes the maximum index, which is one less than
4494 tree index_type = build_index_type(fold_build2(MINUS_EXPR, sizetype,
4498 TREE_TYPE(array_type) = element_type_tree;
4499 TYPE_DOMAIN(array_type) = index_type;
4500 TYPE_ADDR_SPACE(array_type) = TYPE_ADDR_SPACE(element_type_tree);
4501 layout_type(array_type);
4503 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree)
4504 || TYPE_STRUCTURAL_EQUALITY_P(index_type))
4505 SET_TYPE_STRUCTURAL_EQUALITY(array_type);
4506 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
4507 || TYPE_CANONICAL(index_type) != index_type)
4508 TYPE_CANONICAL(array_type) =
4509 build_array_type(TYPE_CANONICAL(element_type_tree),
4510 TYPE_CANONICAL(index_type));
4515 // Fill in the fields for a slice type. This is used for named slice
4519 Array_type::fill_in_slice_tree(Gogo* gogo, tree struct_type)
4521 gcc_assert(this->length_ == NULL);
4523 tree element_type_tree = this->element_type_->get_tree(gogo);
4524 if (element_type_tree == error_mark_node)
4525 return error_mark_node;
4526 tree field = TYPE_FIELDS(struct_type);
4527 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
4528 gcc_assert(POINTER_TYPE_P(TREE_TYPE(field))
4529 && TREE_TYPE(TREE_TYPE(field)) == void_type_node);
4530 TREE_TYPE(field) = build_pointer_type(element_type_tree);
4535 // Return an initializer for an array type.
4538 Array_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
4540 if (this->length_ == NULL)
4547 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
4549 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
4551 for (tree field = TYPE_FIELDS(type_tree);
4553 field = DECL_CHAIN(field))
4555 constructor_elt* elt = VEC_quick_push(constructor_elt, init,
4558 elt->value = fold_convert(TREE_TYPE(field), size_zero_node);
4561 tree ret = build_constructor(type_tree, init);
4562 TREE_CONSTANT(ret) = 1;
4569 tree value = this->element_type_->get_init_tree(gogo, is_clear);
4572 if (value == error_mark_node)
4573 return error_mark_node;
4575 tree length_tree = this->get_length_tree(gogo);
4576 if (length_tree == error_mark_node)
4577 return error_mark_node;
4579 length_tree = fold_convert(sizetype, length_tree);
4580 tree range = build2(RANGE_EXPR, sizetype, size_zero_node,
4581 fold_build2(MINUS_EXPR, sizetype,
4582 length_tree, size_one_node));
4583 tree ret = build_constructor_single(type_tree, range, value);
4584 if (TREE_CONSTANT(value))
4585 TREE_CONSTANT(ret) = 1;
4590 // Handle the builtin make function for a slice.
4593 Array_type::do_make_expression_tree(Translate_context* context,
4594 Expression_list* args,
4595 source_location location)
4597 gcc_assert(this->length_ == NULL);
4599 Gogo* gogo = context->gogo();
4600 tree type_tree = this->get_tree(gogo);
4601 if (type_tree == error_mark_node)
4602 return error_mark_node;
4604 tree values_field = TYPE_FIELDS(type_tree);
4605 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(values_field)),
4608 tree count_field = DECL_CHAIN(values_field);
4609 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(count_field)),
4612 tree element_type_tree = this->element_type_->get_tree(gogo);
4613 if (element_type_tree == error_mark_node)
4614 return error_mark_node;
4615 tree element_size_tree = TYPE_SIZE_UNIT(element_type_tree);
4617 tree value = this->element_type_->get_init_tree(gogo, true);
4618 if (value == error_mark_node)
4619 return error_mark_node;
4621 // The first argument is the number of elements, the optional second
4622 // argument is the capacity.
4623 gcc_assert(args != NULL && args->size() >= 1 && args->size() <= 2);
4625 tree length_tree = args->front()->get_tree(context);
4626 if (length_tree == error_mark_node)
4627 return error_mark_node;
4628 if (!DECL_P(length_tree))
4629 length_tree = save_expr(length_tree);
4630 if (!INTEGRAL_TYPE_P(TREE_TYPE(length_tree)))
4631 length_tree = convert_to_integer(TREE_TYPE(count_field), length_tree);
4633 tree bad_index = Expression::check_bounds(length_tree,
4634 TREE_TYPE(count_field),
4635 NULL_TREE, location);
4637 length_tree = fold_convert_loc(location, TREE_TYPE(count_field), length_tree);
4639 if (args->size() == 1)
4640 capacity_tree = length_tree;
4643 capacity_tree = args->back()->get_tree(context);
4644 if (capacity_tree == error_mark_node)
4645 return error_mark_node;
4646 if (!DECL_P(capacity_tree))
4647 capacity_tree = save_expr(capacity_tree);
4648 if (!INTEGRAL_TYPE_P(TREE_TYPE(capacity_tree)))
4649 capacity_tree = convert_to_integer(TREE_TYPE(count_field),
4652 bad_index = Expression::check_bounds(capacity_tree,
4653 TREE_TYPE(count_field),
4654 bad_index, location);
4656 tree chktype = (((TYPE_SIZE(TREE_TYPE(capacity_tree))
4657 > TYPE_SIZE(TREE_TYPE(length_tree)))
4658 || ((TYPE_SIZE(TREE_TYPE(capacity_tree))
4659 == TYPE_SIZE(TREE_TYPE(length_tree)))
4660 && TYPE_UNSIGNED(TREE_TYPE(capacity_tree))))
4661 ? TREE_TYPE(capacity_tree)
4662 : TREE_TYPE(length_tree));
4663 tree chk = fold_build2_loc(location, LT_EXPR, boolean_type_node,
4664 fold_convert_loc(location, chktype,
4666 fold_convert_loc(location, chktype,
4668 if (bad_index == NULL_TREE)
4671 bad_index = fold_build2_loc(location, TRUTH_OR_EXPR, boolean_type_node,
4674 capacity_tree = fold_convert_loc(location, TREE_TYPE(count_field),
4678 tree size_tree = fold_build2_loc(location, MULT_EXPR, sizetype,
4680 fold_convert_loc(location, sizetype,
4683 tree chk = fold_build2_loc(location, TRUTH_AND_EXPR, boolean_type_node,
4684 fold_build2_loc(location, GT_EXPR,
4686 fold_convert_loc(location,
4690 fold_build2_loc(location, LT_EXPR,
4692 size_tree, element_size_tree));
4693 if (bad_index == NULL_TREE)
4696 bad_index = fold_build2_loc(location, TRUTH_OR_EXPR, boolean_type_node,
4699 tree space = context->gogo()->allocate_memory(this->element_type_,
4700 size_tree, location);
4702 if (value != NULL_TREE)
4703 space = save_expr(space);
4705 space = fold_convert(TREE_TYPE(values_field), space);
4707 if (bad_index != NULL_TREE && bad_index != boolean_false_node)
4709 tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS,
4711 space = build2(COMPOUND_EXPR, TREE_TYPE(space),
4712 build3(COND_EXPR, void_type_node,
4713 bad_index, crash, NULL_TREE),
4717 tree constructor = gogo->slice_constructor(type_tree, space, length_tree,
4720 if (value == NULL_TREE)
4722 // The array contents are zero initialized.
4726 // The elements must be initialized.
4728 tree max = fold_build2_loc(location, MINUS_EXPR, TREE_TYPE(count_field),
4730 fold_convert_loc(location, TREE_TYPE(count_field),
4733 tree array_type = build_array_type(element_type_tree,
4734 build_index_type(max));
4736 tree value_pointer = fold_convert_loc(location,
4737 build_pointer_type(array_type),
4740 tree range = build2(RANGE_EXPR, sizetype, size_zero_node, max);
4741 tree space_init = build_constructor_single(array_type, range, value);
4743 return build2(COMPOUND_EXPR, TREE_TYPE(constructor),
4744 build2(MODIFY_EXPR, void_type_node,
4745 build_fold_indirect_ref(value_pointer),
4750 // Return a tree for a pointer to the values in ARRAY.
4753 Array_type::value_pointer_tree(Gogo*, tree array) const
4756 if (this->length() != NULL)
4759 ret = fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array))),
4760 build_fold_addr_expr(array));
4765 tree field = TYPE_FIELDS(TREE_TYPE(array));
4766 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
4768 ret = fold_build3(COMPONENT_REF, TREE_TYPE(field), array, field,
4771 if (TREE_CONSTANT(array))
4772 TREE_CONSTANT(ret) = 1;
4776 // Return a tree for the length of the array ARRAY which has this
4780 Array_type::length_tree(Gogo* gogo, tree array)
4782 if (this->length_ != NULL)
4784 if (TREE_CODE(array) == SAVE_EXPR)
4785 return fold_convert(integer_type_node, this->get_length_tree(gogo));
4787 return omit_one_operand(integer_type_node,
4788 this->get_length_tree(gogo), array);
4791 // This is an open array. We need to read the length field.
4793 tree type = TREE_TYPE(array);
4794 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
4796 tree field = DECL_CHAIN(TYPE_FIELDS(type));
4797 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
4799 tree ret = build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
4800 if (TREE_CONSTANT(array))
4801 TREE_CONSTANT(ret) = 1;
4805 // Return a tree for the capacity of the array ARRAY which has this
4809 Array_type::capacity_tree(Gogo* gogo, tree array)
4811 if (this->length_ != NULL)
4812 return omit_one_operand(sizetype, this->get_length_tree(gogo), array);
4814 // This is an open array. We need to read the capacity field.
4816 tree type = TREE_TYPE(array);
4817 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
4819 tree field = DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type)));
4820 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
4822 return build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
4828 Array_type::do_export(Export* exp) const
4830 exp->write_c_string("[");
4831 if (this->length_ != NULL)
4832 this->length_->export_expression(exp);
4833 exp->write_c_string("] ");
4834 exp->write_type(this->element_type_);
4840 Array_type::do_import(Import* imp)
4842 imp->require_c_string("[");
4844 if (imp->peek_char() == ']')
4847 length = Expression::import_expression(imp);
4848 imp->require_c_string("] ");
4849 Type* element_type = imp->read_type();
4850 return Type::make_array_type(element_type, length);
4853 // The type of an array type descriptor.
4856 Array_type::make_array_type_descriptor_type()
4861 Type* tdt = Type::make_type_descriptor_type();
4862 Type* ptdt = Type::make_type_descriptor_ptr_type();
4864 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4867 Type::make_builtin_struct_type(3,
4870 "len", uintptr_type);
4872 ret = Type::make_builtin_named_type("ArrayType", sf);
4878 // The type of an slice type descriptor.
4881 Array_type::make_slice_type_descriptor_type()
4886 Type* tdt = Type::make_type_descriptor_type();
4887 Type* ptdt = Type::make_type_descriptor_ptr_type();
4890 Type::make_builtin_struct_type(2,
4894 ret = Type::make_builtin_named_type("SliceType", sf);
4900 // Build a type descriptor for an array/slice type.
4903 Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4905 if (this->length_ != NULL)
4906 return this->array_type_descriptor(gogo, name);
4908 return this->slice_type_descriptor(gogo, name);
4911 // Build a type descriptor for an array type.
4914 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
4916 source_location bloc = BUILTINS_LOCATION;
4918 Type* atdt = Array_type::make_array_type_descriptor_type();
4920 const Struct_field_list* fields = atdt->struct_type()->fields();
4922 Expression_list* vals = new Expression_list();
4925 Struct_field_list::const_iterator p = fields->begin();
4926 gcc_assert(p->field_name() == "commonType");
4927 vals->push_back(this->type_descriptor_constructor(gogo,
4928 RUNTIME_TYPE_KIND_ARRAY,
4932 gcc_assert(p->field_name() == "elem");
4933 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
4936 gcc_assert(p->field_name() == "len");
4937 vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
4940 gcc_assert(p == fields->end());
4942 return Expression::make_struct_composite_literal(atdt, vals, bloc);
4945 // Build a type descriptor for a slice type.
4948 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
4950 source_location bloc = BUILTINS_LOCATION;
4952 Type* stdt = Array_type::make_slice_type_descriptor_type();
4954 const Struct_field_list* fields = stdt->struct_type()->fields();
4956 Expression_list* vals = new Expression_list();
4959 Struct_field_list::const_iterator p = fields->begin();
4960 gcc_assert(p->field_name() == "commonType");
4961 vals->push_back(this->type_descriptor_constructor(gogo,
4962 RUNTIME_TYPE_KIND_SLICE,
4966 gcc_assert(p->field_name() == "elem");
4967 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
4970 gcc_assert(p == fields->end());
4972 return Expression::make_struct_composite_literal(stdt, vals, bloc);
4975 // Reflection string.
4978 Array_type::do_reflection(Gogo* gogo, std::string* ret) const
4980 ret->push_back('[');
4981 if (this->length_ != NULL)
4986 if (!this->length_->integer_constant_value(true, val, &type))
4987 error_at(this->length_->location(),
4988 "array length must be integer constant expression");
4989 else if (mpz_cmp_si(val, 0) < 0)
4990 error_at(this->length_->location(), "array length is negative");
4991 else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0)
4992 error_at(this->length_->location(), "array length is too large");
4996 snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val));
5001 ret->push_back(']');
5003 this->append_reflection(this->element_type_, gogo, ret);
5009 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5011 ret->push_back('A');
5012 this->append_mangled_name(this->element_type_, gogo, ret);
5013 if (this->length_ != NULL)
5018 if (!this->length_->integer_constant_value(true, val, &type))
5019 error_at(this->length_->location(),
5020 "array length must be integer constant expression");
5021 else if (mpz_cmp_si(val, 0) < 0)
5022 error_at(this->length_->location(), "array length is negative");
5023 else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0)
5024 error_at(this->length_->location(), "array size is too large");
5028 snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val));
5033 ret->push_back('e');
5036 // Make an array type.
5039 Type::make_array_type(Type* element_type, Expression* length)
5041 return new Array_type(element_type, length);
5049 Map_type::do_traverse(Traverse* traverse)
5051 if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
5052 || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
5053 return TRAVERSE_EXIT;
5054 return TRAVERSE_CONTINUE;
5057 // Check that the map type is OK.
5060 Map_type::do_verify()
5062 if (this->key_type_->struct_type() != NULL
5063 || this->key_type_->array_type() != NULL)
5065 error_at(this->location_, "invalid map key type");
5071 // Whether two map types are identical.
5074 Map_type::is_identical(const Map_type* t, bool errors_are_identical) const
5076 return (Type::are_identical(this->key_type(), t->key_type(),
5077 errors_are_identical, NULL)
5078 && Type::are_identical(this->val_type(), t->val_type(),
5079 errors_are_identical, NULL));
5085 Map_type::do_hash_for_method(Gogo* gogo) const
5087 return (this->key_type_->hash_for_method(gogo)
5088 + this->val_type_->hash_for_method(gogo)
5092 // Check that a call to the builtin make function is valid. For a map
5093 // the optional argument is the number of spaces to preallocate for
5097 Map_type::do_check_make_expression(Expression_list* args,
5098 source_location location)
5100 if (args != NULL && !args->empty())
5102 if (!Type::check_int_value(args->front(), _("bad size when making map"),
5105 else if (args->size() > 1)
5107 error_at(location, "too many arguments when making map");
5114 // Get a tree for a map type. A map type is represented as a pointer
5115 // to a struct. The struct is __go_map in libgo/map.h.
5118 Map_type::do_get_tree(Gogo* gogo)
5120 static tree type_tree;
5121 if (type_tree == NULL_TREE)
5123 tree struct_type = make_node(RECORD_TYPE);
5125 tree map_descriptor_type = gogo->map_descriptor_type();
5126 tree const_map_descriptor_type =
5127 build_qualified_type(map_descriptor_type, TYPE_QUAL_CONST);
5128 tree name = get_identifier("__descriptor");
5129 tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
5130 build_pointer_type(const_map_descriptor_type));
5131 DECL_CONTEXT(field) = struct_type;
5132 TYPE_FIELDS(struct_type) = field;
5133 tree last_field = field;
5135 name = get_identifier("__element_count");
5136 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, sizetype);
5137 DECL_CONTEXT(field) = struct_type;
5138 DECL_CHAIN(last_field) = field;
5141 name = get_identifier("__bucket_count");
5142 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, sizetype);
5143 DECL_CONTEXT(field) = struct_type;
5144 DECL_CHAIN(last_field) = field;
5147 name = get_identifier("__buckets");
5148 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
5149 build_pointer_type(ptr_type_node));
5150 DECL_CONTEXT(field) = struct_type;
5151 DECL_CHAIN(last_field) = field;
5153 layout_type(struct_type);
5155 // Give the struct a name for better debugging info.
5156 name = get_identifier("__go_map");
5157 tree type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
5159 DECL_ARTIFICIAL(type_decl) = 1;
5160 TYPE_NAME(struct_type) = type_decl;
5161 go_preserve_from_gc(type_decl);
5162 rest_of_decl_compilation(type_decl, 1, 0);
5164 type_tree = build_pointer_type(struct_type);
5165 go_preserve_from_gc(type_tree);
5171 // Initialize a map.
5174 Map_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
5178 return fold_convert(type_tree, null_pointer_node);
5181 // Return an expression for a newly allocated map.
5184 Map_type::do_make_expression_tree(Translate_context* context,
5185 Expression_list* args,
5186 source_location location)
5188 tree bad_index = NULL_TREE;
5191 if (args == NULL || args->empty())
5192 expr_tree = size_zero_node;
5195 expr_tree = args->front()->get_tree(context);
5196 if (expr_tree == error_mark_node)
5197 return error_mark_node;
5198 if (!DECL_P(expr_tree))
5199 expr_tree = save_expr(expr_tree);
5200 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
5201 expr_tree = convert_to_integer(sizetype, expr_tree);
5202 bad_index = Expression::check_bounds(expr_tree, sizetype, bad_index,
5206 tree map_type = this->get_tree(context->gogo());
5208 static tree new_map_fndecl;
5209 tree ret = Gogo::call_builtin(&new_map_fndecl,
5214 TREE_TYPE(TYPE_FIELDS(TREE_TYPE(map_type))),
5215 context->gogo()->map_descriptor(this),
5218 if (ret == error_mark_node)
5219 return error_mark_node;
5220 // This can panic if the capacity is out of range.
5221 TREE_NOTHROW(new_map_fndecl) = 0;
5223 if (bad_index == NULL_TREE)
5227 tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS,
5229 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
5230 build3(COND_EXPR, void_type_node,
5231 bad_index, crash, NULL_TREE),
5236 // The type of a map type descriptor.
5239 Map_type::make_map_type_descriptor_type()
5244 Type* tdt = Type::make_type_descriptor_type();
5245 Type* ptdt = Type::make_type_descriptor_ptr_type();
5248 Type::make_builtin_struct_type(3,
5253 ret = Type::make_builtin_named_type("MapType", sf);
5259 // Build a type descriptor for a map type.
5262 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5264 source_location bloc = BUILTINS_LOCATION;
5266 Type* mtdt = Map_type::make_map_type_descriptor_type();
5268 const Struct_field_list* fields = mtdt->struct_type()->fields();
5270 Expression_list* vals = new Expression_list();
5273 Struct_field_list::const_iterator p = fields->begin();
5274 gcc_assert(p->field_name() == "commonType");
5275 vals->push_back(this->type_descriptor_constructor(gogo,
5276 RUNTIME_TYPE_KIND_MAP,
5280 gcc_assert(p->field_name() == "key");
5281 vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
5284 gcc_assert(p->field_name() == "elem");
5285 vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
5288 gcc_assert(p == fields->end());
5290 return Expression::make_struct_composite_literal(mtdt, vals, bloc);
5293 // Reflection string for a map.
5296 Map_type::do_reflection(Gogo* gogo, std::string* ret) const
5298 ret->append("map[");
5299 this->append_reflection(this->key_type_, gogo, ret);
5301 this->append_reflection(this->val_type_, gogo, ret);
5304 // Mangled name for a map.
5307 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5309 ret->push_back('M');
5310 this->append_mangled_name(this->key_type_, gogo, ret);
5312 this->append_mangled_name(this->val_type_, gogo, ret);
5315 // Export a map type.
5318 Map_type::do_export(Export* exp) const
5320 exp->write_c_string("map [");
5321 exp->write_type(this->key_type_);
5322 exp->write_c_string("] ");
5323 exp->write_type(this->val_type_);
5326 // Import a map type.
5329 Map_type::do_import(Import* imp)
5331 imp->require_c_string("map [");
5332 Type* key_type = imp->read_type();
5333 imp->require_c_string("] ");
5334 Type* val_type = imp->read_type();
5335 return Type::make_map_type(key_type, val_type, imp->location());
5341 Type::make_map_type(Type* key_type, Type* val_type, source_location location)
5343 return new Map_type(key_type, val_type, location);
5346 // Class Channel_type.
5351 Channel_type::do_hash_for_method(Gogo* gogo) const
5353 unsigned int ret = 0;
5354 if (this->may_send_)
5356 if (this->may_receive_)
5358 if (this->element_type_ != NULL)
5359 ret += this->element_type_->hash_for_method(gogo) << 2;
5363 // Whether this type is the same as T.
5366 Channel_type::is_identical(const Channel_type* t,
5367 bool errors_are_identical) const
5369 if (!Type::are_identical(this->element_type(), t->element_type(),
5370 errors_are_identical, NULL))
5372 return (this->may_send_ == t->may_send_
5373 && this->may_receive_ == t->may_receive_);
5376 // Check whether the parameters for a call to the builtin function
5377 // make are OK for a channel. A channel can take an optional single
5378 // parameter which is the buffer size.
5381 Channel_type::do_check_make_expression(Expression_list* args,
5382 source_location location)
5384 if (args != NULL && !args->empty())
5386 if (!Type::check_int_value(args->front(),
5387 _("bad buffer size when making channel"),
5390 else if (args->size() > 1)
5392 error_at(location, "too many arguments when making channel");
5399 // Return the tree for a channel type. A channel is a pointer to a
5400 // __go_channel struct. The __go_channel struct is defined in
5401 // libgo/runtime/channel.h.
5404 Channel_type::do_get_tree(Gogo*)
5406 static tree type_tree;
5407 if (type_tree == NULL_TREE)
5409 tree ret = make_node(RECORD_TYPE);
5410 TYPE_NAME(ret) = get_identifier("__go_channel");
5411 TYPE_STUB_DECL(ret) = build_decl(BUILTINS_LOCATION, TYPE_DECL, NULL_TREE,
5413 type_tree = build_pointer_type(ret);
5414 go_preserve_from_gc(type_tree);
5419 // Initialize a channel variable.
5422 Channel_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
5426 return fold_convert(type_tree, null_pointer_node);
5429 // Handle the builtin function make for a channel.
5432 Channel_type::do_make_expression_tree(Translate_context* context,
5433 Expression_list* args,
5434 source_location location)
5436 Gogo* gogo = context->gogo();
5437 tree channel_type = this->get_tree(gogo);
5439 tree element_tree = this->element_type_->get_tree(gogo);
5440 tree element_size_tree = size_in_bytes(element_tree);
5442 tree bad_index = NULL_TREE;
5445 if (args == NULL || args->empty())
5446 expr_tree = size_zero_node;
5449 expr_tree = args->front()->get_tree(context);
5450 if (expr_tree == error_mark_node)
5451 return error_mark_node;
5452 if (!DECL_P(expr_tree))
5453 expr_tree = save_expr(expr_tree);
5454 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
5455 expr_tree = convert_to_integer(sizetype, expr_tree);
5456 bad_index = Expression::check_bounds(expr_tree, sizetype, bad_index,
5460 static tree new_channel_fndecl;
5461 tree ret = Gogo::call_builtin(&new_channel_fndecl,
5470 if (ret == error_mark_node)
5471 return error_mark_node;
5472 // This can panic if the capacity is out of range.
5473 TREE_NOTHROW(new_channel_fndecl) = 0;
5475 if (bad_index == NULL_TREE)
5479 tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS,
5481 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
5482 build3(COND_EXPR, void_type_node,
5483 bad_index, crash, NULL_TREE),
5488 // Build a type descriptor for a channel type.
5491 Channel_type::make_chan_type_descriptor_type()
5496 Type* tdt = Type::make_type_descriptor_type();
5497 Type* ptdt = Type::make_type_descriptor_ptr_type();
5499 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5502 Type::make_builtin_struct_type(3,
5505 "dir", uintptr_type);
5507 ret = Type::make_builtin_named_type("ChanType", sf);
5513 // Build a type descriptor for a map type.
5516 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5518 source_location bloc = BUILTINS_LOCATION;
5520 Type* ctdt = Channel_type::make_chan_type_descriptor_type();
5522 const Struct_field_list* fields = ctdt->struct_type()->fields();
5524 Expression_list* vals = new Expression_list();
5527 Struct_field_list::const_iterator p = fields->begin();
5528 gcc_assert(p->field_name() == "commonType");
5529 vals->push_back(this->type_descriptor_constructor(gogo,
5530 RUNTIME_TYPE_KIND_CHAN,
5534 gcc_assert(p->field_name() == "elem");
5535 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5538 gcc_assert(p->field_name() == "dir");
5539 // These bits must match the ones in libgo/runtime/go-type.h.
5541 if (this->may_receive_)
5543 if (this->may_send_)
5546 mpz_init_set_ui(iv, val);
5547 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
5551 gcc_assert(p == fields->end());
5553 return Expression::make_struct_composite_literal(ctdt, vals, bloc);
5556 // Reflection string.
5559 Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
5561 if (!this->may_send_)
5563 ret->append("chan");
5564 if (!this->may_receive_)
5566 ret->push_back(' ');
5567 this->append_reflection(this->element_type_, gogo, ret);
5573 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5575 ret->push_back('C');
5576 this->append_mangled_name(this->element_type_, gogo, ret);
5577 if (this->may_send_)
5578 ret->push_back('s');
5579 if (this->may_receive_)
5580 ret->push_back('r');
5581 ret->push_back('e');
5587 Channel_type::do_export(Export* exp) const
5589 exp->write_c_string("chan ");
5590 if (this->may_send_ && !this->may_receive_)
5591 exp->write_c_string("-< ");
5592 else if (this->may_receive_ && !this->may_send_)
5593 exp->write_c_string("<- ");
5594 exp->write_type(this->element_type_);
5600 Channel_type::do_import(Import* imp)
5602 imp->require_c_string("chan ");
5606 if (imp->match_c_string("-< "))
5610 may_receive = false;
5612 else if (imp->match_c_string("<- "))
5624 Type* element_type = imp->read_type();
5626 return Type::make_channel_type(may_send, may_receive, element_type);
5629 // Make a new channel type.
5632 Type::make_channel_type(bool send, bool receive, Type* element_type)
5634 return new Channel_type(send, receive, element_type);
5637 // Class Interface_type.
5642 Interface_type::do_traverse(Traverse* traverse)
5644 if (this->methods_ == NULL)
5645 return TRAVERSE_CONTINUE;
5646 return this->methods_->traverse(traverse);
5649 // Finalize the methods. This handles interface inheritance.
5652 Interface_type::finalize_methods()
5654 if (this->methods_ == NULL)
5656 std::vector<Named_type*> seen;
5657 bool is_recursive = false;
5660 while (from < this->methods_->size())
5662 const Typed_identifier* p = &this->methods_->at(from);
5663 if (!p->name().empty())
5666 for (i = 0; i < to; ++i)
5668 if (this->methods_->at(i).name() == p->name())
5670 error_at(p->location(), "duplicate method %qs",
5671 Gogo::message_name(p->name()).c_str());
5678 this->methods_->set(to, *p);
5685 Interface_type* it = p->type()->interface_type();
5688 error_at(p->location(), "interface contains embedded non-interface");
5696 error_at(p->location(), "invalid recursive interface");
5697 is_recursive = true;
5703 Named_type* nt = p->type()->named_type();
5706 std::vector<Named_type*>::const_iterator q;
5707 for (q = seen.begin(); q != seen.end(); ++q)
5711 error_at(p->location(), "inherited interface loop");
5715 if (q != seen.end())
5723 const Typed_identifier_list* methods = it->methods();
5724 if (methods == NULL)
5729 for (Typed_identifier_list::const_iterator q = methods->begin();
5730 q != methods->end();
5733 if (q->name().empty())
5735 if (q->type()->forwarded() == p->type()->forwarded())
5736 error_at(p->location(), "interface inheritance loop");
5740 for (i = from + 1; i < this->methods_->size(); ++i)
5742 const Typed_identifier* r = &this->methods_->at(i);
5743 if (r->name().empty()
5744 && r->type()->forwarded() == q->type()->forwarded())
5746 error_at(p->location(),
5747 "inherited interface listed twice");
5751 if (i == this->methods_->size())
5752 this->methods_->push_back(Typed_identifier(q->name(),
5757 else if (this->find_method(q->name()) == NULL)
5758 this->methods_->push_back(Typed_identifier(q->name(), q->type(),
5763 error_at(p->location(), "inherited method %qs is ambiguous",
5764 Gogo::message_name(q->name()).c_str());
5771 delete this->methods_;
5772 this->methods_ = NULL;
5776 this->methods_->resize(to);
5777 this->methods_->sort_by_name();
5781 // Return the method NAME, or NULL.
5783 const Typed_identifier*
5784 Interface_type::find_method(const std::string& name) const
5786 if (this->methods_ == NULL)
5788 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5789 p != this->methods_->end();
5791 if (p->name() == name)
5796 // Return the method index.
5799 Interface_type::method_index(const std::string& name) const
5801 gcc_assert(this->methods_ != NULL);
5803 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5804 p != this->methods_->end();
5806 if (p->name() == name)
5811 // Return whether NAME is an unexported method, for better error
5815 Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
5817 if (this->methods_ == NULL)
5819 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5820 p != this->methods_->end();
5823 const std::string& method_name(p->name());
5824 if (Gogo::is_hidden_name(method_name)
5825 && name == Gogo::unpack_hidden_name(method_name)
5826 && gogo->pack_hidden_name(name, false) != method_name)
5832 // Whether this type is identical with T.
5835 Interface_type::is_identical(const Interface_type* t,
5836 bool errors_are_identical) const
5838 // We require the same methods with the same types. The methods
5839 // have already been sorted.
5840 if (this->methods() == NULL || t->methods() == NULL)
5841 return this->methods() == t->methods();
5843 Typed_identifier_list::const_iterator p1 = this->methods()->begin();
5844 for (Typed_identifier_list::const_iterator p2 = t->methods()->begin();
5845 p2 != t->methods()->end();
5848 if (p1 == this->methods()->end())
5850 if (p1->name() != p2->name()
5851 || !Type::are_identical(p1->type(), p2->type(),
5852 errors_are_identical, NULL))
5855 if (p1 != this->methods()->end())
5860 // Whether we can assign the interface type T to this type. The types
5861 // are known to not be identical. An interface assignment is only
5862 // permitted if T is known to implement all methods in THIS.
5863 // Otherwise a type guard is required.
5866 Interface_type::is_compatible_for_assign(const Interface_type* t,
5867 std::string* reason) const
5869 if (this->methods() == NULL)
5871 for (Typed_identifier_list::const_iterator p = this->methods()->begin();
5872 p != this->methods()->end();
5875 const Typed_identifier* m = t->find_method(p->name());
5881 snprintf(buf, sizeof buf,
5882 _("need explicit conversion; missing method %s%s%s"),
5883 open_quote, Gogo::message_name(p->name()).c_str(),
5885 reason->assign(buf);
5890 std::string subreason;
5891 if (!Type::are_identical(p->type(), m->type(), true, &subreason))
5895 std::string n = Gogo::message_name(p->name());
5896 size_t len = 100 + n.length() + subreason.length();
5897 char* buf = new char[len];
5898 if (subreason.empty())
5899 snprintf(buf, len, _("incompatible type for method %s%s%s"),
5900 open_quote, n.c_str(), close_quote);
5903 _("incompatible type for method %s%s%s (%s)"),
5904 open_quote, n.c_str(), close_quote,
5906 reason->assign(buf);
5919 Interface_type::do_hash_for_method(Gogo* gogo) const
5921 unsigned int ret = 0;
5922 if (this->methods_ != NULL)
5924 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5925 p != this->methods_->end();
5928 ret = Type::hash_string(p->name(), ret);
5929 ret += p->type()->hash_for_method(gogo);
5936 // Return true if T implements the interface. If it does not, and
5937 // REASON is not NULL, set *REASON to a useful error message.
5940 Interface_type::implements_interface(const Type* t, std::string* reason) const
5942 if (this->methods_ == NULL)
5945 bool is_pointer = false;
5946 const Named_type* nt = t->named_type();
5947 const Struct_type* st = t->struct_type();
5948 // If we start with a named type, we don't dereference it to find
5952 const Type* pt = t->points_to();
5955 // If T is a pointer to a named type, then we need to look at
5956 // the type to which it points.
5958 nt = pt->named_type();
5959 st = pt->struct_type();
5963 // If we have a named type, get the methods from it rather than from
5968 // Only named and struct types have methods.
5969 if (nt == NULL && st == NULL)
5973 if (t->points_to() != NULL
5974 && t->points_to()->interface_type() != NULL)
5975 reason->assign(_("pointer to interface type has no methods"));
5977 reason->assign(_("type has no methods"));
5982 if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
5986 if (t->points_to() != NULL
5987 && t->points_to()->interface_type() != NULL)
5988 reason->assign(_("pointer to interface type has no methods"));
5990 reason->assign(_("type has no methods"));
5995 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5996 p != this->methods_->end();
5999 bool is_ambiguous = false;
6000 Method* m = (nt != NULL
6001 ? nt->method_function(p->name(), &is_ambiguous)
6002 : st->method_function(p->name(), &is_ambiguous));
6007 std::string n = Gogo::message_name(p->name());
6008 size_t len = n.length() + 100;
6009 char* buf = new char[len];
6011 snprintf(buf, len, _("ambiguous method %s%s%s"),
6012 open_quote, n.c_str(), close_quote);
6014 snprintf(buf, len, _("missing method %s%s%s"),
6015 open_quote, n.c_str(), close_quote);
6016 reason->assign(buf);
6022 Function_type *p_fn_type = p->type()->function_type();
6023 Function_type* m_fn_type = m->type()->function_type();
6024 gcc_assert(p_fn_type != NULL && m_fn_type != NULL);
6025 std::string subreason;
6026 if (!p_fn_type->is_identical(m_fn_type, true, true, &subreason))
6030 std::string n = Gogo::message_name(p->name());
6031 size_t len = 100 + n.length() + subreason.length();
6032 char* buf = new char[len];
6033 if (subreason.empty())
6034 snprintf(buf, len, _("incompatible type for method %s%s%s"),
6035 open_quote, n.c_str(), close_quote);
6038 _("incompatible type for method %s%s%s (%s)"),
6039 open_quote, n.c_str(), close_quote,
6041 reason->assign(buf);
6047 if (!is_pointer && !m->is_value_method())
6051 std::string n = Gogo::message_name(p->name());
6052 size_t len = 100 + n.length();
6053 char* buf = new char[len];
6054 snprintf(buf, len, _("method %s%s%s requires a pointer"),
6055 open_quote, n.c_str(), close_quote);
6056 reason->assign(buf);
6066 // Return a tree for an interface type. An interface is a pointer to
6067 // a struct. The struct has three fields. The first field is a
6068 // pointer to the type descriptor for the dynamic type of the object.
6069 // The second field is a pointer to a table of methods for the
6070 // interface to be used with the object. The third field is the value
6071 // of the object itself.
6074 Interface_type::do_get_tree(Gogo* gogo)
6076 if (this->methods_ == NULL)
6077 return Interface_type::empty_type_tree(gogo);
6080 tree t = Interface_type::non_empty_type_tree(this->location_);
6081 return this->fill_in_tree(gogo, t);
6085 // Return a singleton struct for an empty interface type. We use the
6086 // same type for all empty interfaces. This lets us assign them to
6087 // each other directly without triggering GIMPLE type errors.
6090 Interface_type::empty_type_tree(Gogo* gogo)
6092 static tree empty_interface;
6093 if (empty_interface != NULL_TREE)
6094 return empty_interface;
6096 tree dtype = Type::make_type_descriptor_type()->get_tree(gogo);
6097 dtype = build_pointer_type(build_qualified_type(dtype, TYPE_QUAL_CONST));
6098 return Gogo::builtin_struct(&empty_interface, "__go_empty_interface",
6100 "__type_descriptor",
6106 // Return a new struct for a non-empty interface type. The correct
6107 // values are filled in by fill_in_tree.
6110 Interface_type::non_empty_type_tree(source_location location)
6112 tree ret = make_node(RECORD_TYPE);
6114 tree field_trees = NULL_TREE;
6115 tree* pp = &field_trees;
6117 tree name_tree = get_identifier("__methods");
6118 tree field = build_decl(location, FIELD_DECL, name_tree, ptr_type_node);
6119 DECL_CONTEXT(field) = ret;
6121 pp = &DECL_CHAIN(field);
6123 name_tree = get_identifier("__object");
6124 field = build_decl(location, FIELD_DECL, name_tree, ptr_type_node);
6125 DECL_CONTEXT(field) = ret;
6128 TYPE_FIELDS(ret) = field_trees;
6135 // Fill in the tree for an interface type. This is used for named
6139 Interface_type::fill_in_tree(Gogo* gogo, tree type)
6141 gcc_assert(this->methods_ != NULL);
6143 // Build the type of the table of methods.
6145 tree method_table = make_node(RECORD_TYPE);
6147 // The first field is a pointer to the type descriptor.
6148 tree name_tree = get_identifier("__type_descriptor");
6149 tree dtype = Type::make_type_descriptor_type()->get_tree(gogo);
6150 dtype = build_pointer_type(build_qualified_type(dtype, TYPE_QUAL_CONST));
6151 tree field = build_decl(this->location_, FIELD_DECL, name_tree, dtype);
6152 DECL_CONTEXT(field) = method_table;
6153 TYPE_FIELDS(method_table) = field;
6155 std::string last_name = "";
6156 tree* pp = &DECL_CHAIN(field);
6157 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6158 p != this->methods_->end();
6161 std::string name = Gogo::unpack_hidden_name(p->name());
6162 name_tree = get_identifier_with_length(name.data(), name.length());
6163 tree field_type = p->type()->get_tree(gogo);
6164 if (field_type == error_mark_node)
6165 return error_mark_node;
6166 field = build_decl(this->location_, FIELD_DECL, name_tree, field_type);
6167 DECL_CONTEXT(field) = method_table;
6169 pp = &DECL_CHAIN(field);
6170 // Sanity check: the names should be sorted.
6171 gcc_assert(p->name() > last_name);
6172 last_name = p->name();
6174 layout_type(method_table);
6176 // Update the type of the __methods field from a generic pointer to
6177 // a pointer to the method table.
6178 field = TYPE_FIELDS(type);
6179 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
6181 TREE_TYPE(field) = build_pointer_type(method_table);
6186 // Initialization value.
6189 Interface_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
6194 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
6195 for (tree field = TYPE_FIELDS(type_tree);
6197 field = DECL_CHAIN(field))
6199 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
6201 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
6204 tree ret = build_constructor(type_tree, init);
6205 TREE_CONSTANT(ret) = 1;
6209 // The type of an interface type descriptor.
6212 Interface_type::make_interface_type_descriptor_type()
6217 Type* tdt = Type::make_type_descriptor_type();
6218 Type* ptdt = Type::make_type_descriptor_ptr_type();
6220 Type* string_type = Type::lookup_string_type();
6221 Type* pointer_string_type = Type::make_pointer_type(string_type);
6224 Type::make_builtin_struct_type(3,
6225 "name", pointer_string_type,
6226 "pkgPath", pointer_string_type,
6229 Type* nsm = Type::make_builtin_named_type("imethod", sm);
6231 Type* slice_nsm = Type::make_array_type(nsm, NULL);
6233 Struct_type* s = Type::make_builtin_struct_type(2,
6235 "methods", slice_nsm);
6237 ret = Type::make_builtin_named_type("InterfaceType", s);
6243 // Build a type descriptor for an interface type.
6246 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6248 source_location bloc = BUILTINS_LOCATION;
6250 Type* itdt = Interface_type::make_interface_type_descriptor_type();
6252 const Struct_field_list* ifields = itdt->struct_type()->fields();
6254 Expression_list* ivals = new Expression_list();
6257 Struct_field_list::const_iterator pif = ifields->begin();
6258 gcc_assert(pif->field_name() == "commonType");
6259 ivals->push_back(this->type_descriptor_constructor(gogo,
6260 RUNTIME_TYPE_KIND_INTERFACE,
6264 gcc_assert(pif->field_name() == "methods");
6266 Expression_list* methods = new Expression_list();
6267 if (this->methods_ != NULL && !this->methods_->empty())
6269 Type* elemtype = pif->type()->array_type()->element_type();
6271 methods->reserve(this->methods_->size());
6272 for (Typed_identifier_list::const_iterator pm = this->methods_->begin();
6273 pm != this->methods_->end();
6276 const Struct_field_list* mfields = elemtype->struct_type()->fields();
6278 Expression_list* mvals = new Expression_list();
6281 Struct_field_list::const_iterator pmf = mfields->begin();
6282 gcc_assert(pmf->field_name() == "name");
6283 std::string s = Gogo::unpack_hidden_name(pm->name());
6284 Expression* e = Expression::make_string(s, bloc);
6285 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
6288 gcc_assert(pmf->field_name() == "pkgPath");
6289 if (!Gogo::is_hidden_name(pm->name()))
6290 mvals->push_back(Expression::make_nil(bloc));
6293 s = Gogo::hidden_name_prefix(pm->name());
6294 e = Expression::make_string(s, bloc);
6295 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
6299 gcc_assert(pmf->field_name() == "typ");
6300 mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
6303 gcc_assert(pmf == mfields->end());
6305 e = Expression::make_struct_composite_literal(elemtype, mvals,
6307 methods->push_back(e);
6311 ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
6315 gcc_assert(pif == ifields->end());
6317 return Expression::make_struct_composite_literal(itdt, ivals, bloc);
6320 // Reflection string.
6323 Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
6325 ret->append("interface {");
6326 if (this->methods_ != NULL)
6328 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6329 p != this->methods_->end();
6332 if (p != this->methods_->begin())
6334 ret->push_back(' ');
6335 ret->append(Gogo::unpack_hidden_name(p->name()));
6336 std::string sub = p->type()->reflection(gogo);
6337 gcc_assert(sub.compare(0, 4, "func") == 0);
6338 sub = sub.substr(4);
6348 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6350 ret->push_back('I');
6352 const Typed_identifier_list* methods = this->methods_;
6353 if (methods != NULL)
6355 for (Typed_identifier_list::const_iterator p = methods->begin();
6356 p != methods->end();
6359 std::string n = Gogo::unpack_hidden_name(p->name());
6361 snprintf(buf, sizeof buf, "%u_",
6362 static_cast<unsigned int>(n.length()));
6365 this->append_mangled_name(p->type(), gogo, ret);
6369 ret->push_back('e');
6375 Interface_type::do_export(Export* exp) const
6377 exp->write_c_string("interface { ");
6379 const Typed_identifier_list* methods = this->methods_;
6380 if (methods != NULL)
6382 for (Typed_identifier_list::const_iterator pm = methods->begin();
6383 pm != methods->end();
6386 exp->write_string(pm->name());
6387 exp->write_c_string(" (");
6389 const Function_type* fntype = pm->type()->function_type();
6392 const Typed_identifier_list* parameters = fntype->parameters();
6393 if (parameters != NULL)
6395 bool is_varargs = fntype->is_varargs();
6396 for (Typed_identifier_list::const_iterator pp =
6397 parameters->begin();
6398 pp != parameters->end();
6404 exp->write_c_string(", ");
6405 if (!is_varargs || pp + 1 != parameters->end())
6406 exp->write_type(pp->type());
6409 exp->write_c_string("...");
6410 Type *pptype = pp->type();
6411 exp->write_type(pptype->array_type()->element_type());
6416 exp->write_c_string(")");
6418 const Typed_identifier_list* results = fntype->results();
6419 if (results != NULL)
6421 exp->write_c_string(" ");
6422 if (results->size() == 1)
6423 exp->write_type(results->begin()->type());
6427 exp->write_c_string("(");
6428 for (Typed_identifier_list::const_iterator p =
6430 p != results->end();
6436 exp->write_c_string(", ");
6437 exp->write_type(p->type());
6439 exp->write_c_string(")");
6443 exp->write_c_string("; ");
6447 exp->write_c_string("}");
6450 // Import an interface type.
6453 Interface_type::do_import(Import* imp)
6455 imp->require_c_string("interface { ");
6457 Typed_identifier_list* methods = new Typed_identifier_list;
6458 while (imp->peek_char() != '}')
6460 std::string name = imp->read_identifier();
6461 imp->require_c_string(" (");
6463 Typed_identifier_list* parameters;
6464 bool is_varargs = false;
6465 if (imp->peek_char() == ')')
6469 parameters = new Typed_identifier_list;
6472 if (imp->match_c_string("..."))
6478 Type* ptype = imp->read_type();
6480 ptype = Type::make_array_type(ptype, NULL);
6481 parameters->push_back(Typed_identifier(Import::import_marker,
6482 ptype, imp->location()));
6483 if (imp->peek_char() != ',')
6485 gcc_assert(!is_varargs);
6486 imp->require_c_string(", ");
6489 imp->require_c_string(")");
6491 Typed_identifier_list* results;
6492 if (imp->peek_char() != ' ')
6496 results = new Typed_identifier_list;
6498 if (imp->peek_char() != '(')
6500 Type* rtype = imp->read_type();
6501 results->push_back(Typed_identifier(Import::import_marker,
6502 rtype, imp->location()));
6509 Type* rtype = imp->read_type();
6510 results->push_back(Typed_identifier(Import::import_marker,
6511 rtype, imp->location()));
6512 if (imp->peek_char() != ',')
6514 imp->require_c_string(", ");
6516 imp->require_c_string(")");
6520 Function_type* fntype = Type::make_function_type(NULL, parameters,
6524 fntype->set_is_varargs();
6525 methods->push_back(Typed_identifier(name, fntype, imp->location()));
6527 imp->require_c_string("; ");
6530 imp->require_c_string("}");
6532 if (methods->empty())
6538 return Type::make_interface_type(methods, imp->location());
6541 // Make an interface type.
6544 Type::make_interface_type(Typed_identifier_list* methods,
6545 source_location location)
6547 return new Interface_type(methods, location);
6552 // Bind a method to an object.
6555 Method::bind_method(Expression* expr, source_location location) const
6557 if (this->stub_ == NULL)
6559 // When there is no stub object, the binding is determined by
6561 return this->do_bind_method(expr, location);
6564 Expression* func = Expression::make_func_reference(this->stub_, NULL,
6566 return Expression::make_bound_method(expr, func, location);
6569 // Return the named object associated with a method. This may only be
6570 // called after methods are finalized.
6573 Method::named_object() const
6575 if (this->stub_ != NULL)
6577 return this->do_named_object();
6580 // Class Named_method.
6582 // The type of the method.
6585 Named_method::do_type() const
6587 if (this->named_object_->is_function())
6588 return this->named_object_->func_value()->type();
6589 else if (this->named_object_->is_function_declaration())
6590 return this->named_object_->func_declaration_value()->type();
6595 // Return the location of the method receiver.
6598 Named_method::do_receiver_location() const
6600 return this->do_type()->receiver()->location();
6603 // Bind a method to an object.
6606 Named_method::do_bind_method(Expression* expr, source_location location) const
6608 Expression* func = Expression::make_func_reference(this->named_object_, NULL,
6610 Bound_method_expression* bme = Expression::make_bound_method(expr, func,
6612 // If this is not a local method, and it does not use a stub, then
6613 // the real method expects a different type. We need to cast the
6615 if (this->depth() > 0 && !this->needs_stub_method())
6617 Function_type* ftype = this->do_type();
6618 gcc_assert(ftype->is_method());
6619 Type* frtype = ftype->receiver()->type();
6620 bme->set_first_argument_type(frtype);
6625 // Class Interface_method.
6627 // Bind a method to an object.
6630 Interface_method::do_bind_method(Expression* expr,
6631 source_location location) const
6633 return Expression::make_interface_field_reference(expr, this->name_,
6639 // Insert a new method. Return true if it was inserted, false
6643 Methods::insert(const std::string& name, Method* m)
6645 std::pair<Method_map::iterator, bool> ins =
6646 this->methods_.insert(std::make_pair(name, m));
6651 Method* old_method = ins.first->second;
6652 if (m->depth() < old_method->depth())
6655 ins.first->second = m;
6660 if (m->depth() == old_method->depth())
6661 old_method->set_is_ambiguous();
6667 // Return the number of unambiguous methods.
6670 Methods::count() const
6673 for (Method_map::const_iterator p = this->methods_.begin();
6674 p != this->methods_.end();
6676 if (!p->second->is_ambiguous())
6681 // Class Named_type.
6683 // Return the name of the type.
6686 Named_type::name() const
6688 return this->named_object_->name();
6691 // Return the name of the type to use in an error message.
6694 Named_type::message_name() const
6696 return this->named_object_->message_name();
6699 // Return the base type for this type. We have to be careful about
6700 // circular type definitions, which are invalid but may be seen here.
6703 Named_type::named_base()
6705 if (this->seen_ > 0)
6708 Type* ret = this->type_->base();
6714 Named_type::named_base() const
6716 if (this->seen_ > 0)
6719 const Type* ret = this->type_->base();
6724 // Return whether this is an error type. We have to be careful about
6725 // circular type definitions, which are invalid but may be seen here.
6728 Named_type::is_named_error_type() const
6730 if (this->seen_ > 0)
6733 bool ret = this->type_->is_error_type();
6738 // Add a method to this type.
6741 Named_type::add_method(const std::string& name, Function* function)
6743 if (this->local_methods_ == NULL)
6744 this->local_methods_ = new Bindings(NULL);
6745 return this->local_methods_->add_function(name, NULL, function);
6748 // Add a method declaration to this type.
6751 Named_type::add_method_declaration(const std::string& name, Package* package,
6752 Function_type* type,
6753 source_location location)
6755 if (this->local_methods_ == NULL)
6756 this->local_methods_ = new Bindings(NULL);
6757 return this->local_methods_->add_function_declaration(name, package, type,
6761 // Add an existing method to this type.
6764 Named_type::add_existing_method(Named_object* no)
6766 if (this->local_methods_ == NULL)
6767 this->local_methods_ = new Bindings(NULL);
6768 this->local_methods_->add_named_object(no);
6771 // Look for a local method NAME, and returns its named object, or NULL
6775 Named_type::find_local_method(const std::string& name) const
6777 if (this->local_methods_ == NULL)
6779 return this->local_methods_->lookup(name);
6782 // Return whether NAME is an unexported field or method, for better
6786 Named_type::is_unexported_local_method(Gogo* gogo,
6787 const std::string& name) const
6789 Bindings* methods = this->local_methods_;
6790 if (methods != NULL)
6792 for (Bindings::const_declarations_iterator p =
6793 methods->begin_declarations();
6794 p != methods->end_declarations();
6797 if (Gogo::is_hidden_name(p->first)
6798 && name == Gogo::unpack_hidden_name(p->first)
6799 && gogo->pack_hidden_name(name, false) != p->first)
6806 // Build the complete list of methods for this type, which means
6807 // recursively including all methods for anonymous fields. Create all
6811 Named_type::finalize_methods(Gogo* gogo)
6813 if (this->all_methods_ != NULL)
6816 if (this->local_methods_ != NULL
6817 && (this->points_to() != NULL || this->interface_type() != NULL))
6819 const Bindings* lm = this->local_methods_;
6820 for (Bindings::const_declarations_iterator p = lm->begin_declarations();
6821 p != lm->end_declarations();
6823 error_at(p->second->location(),
6824 "invalid pointer or interface receiver type");
6825 delete this->local_methods_;
6826 this->local_methods_ = NULL;
6830 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
6833 // Return the method NAME, or NULL if there isn't one or if it is
6834 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6838 Named_type::method_function(const std::string& name, bool* is_ambiguous) const
6840 return Type::method_function(this->all_methods_, name, is_ambiguous);
6843 // Return a pointer to the interface method table for this type for
6844 // the interface INTERFACE. IS_POINTER is true if this is for a
6848 Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
6851 gcc_assert(!interface->is_empty());
6853 Interface_method_tables** pimt = (is_pointer
6854 ? &this->interface_method_tables_
6855 : &this->pointer_interface_method_tables_);
6858 *pimt = new Interface_method_tables(5);
6860 std::pair<const Interface_type*, tree> val(interface, NULL_TREE);
6861 std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
6865 // This is a new entry in the hash table.
6866 gcc_assert(ins.first->second == NULL_TREE);
6867 ins.first->second = gogo->interface_method_table_for_type(interface,
6872 tree decl = ins.first->second;
6873 if (decl == error_mark_node)
6874 return error_mark_node;
6875 gcc_assert(decl != NULL_TREE && TREE_CODE(decl) == VAR_DECL);
6876 return build_fold_addr_expr(decl);
6879 // Return whether a named type has any hidden fields.
6882 Named_type::named_type_has_hidden_fields(std::string* reason) const
6884 if (this->seen_ > 0)
6887 bool ret = this->type_->has_hidden_fields(this, reason);
6892 // Look for a use of a complete type within another type. This is
6893 // used to check that we don't try to use a type within itself.
6895 class Find_type_use : public Traverse
6898 Find_type_use(Named_type* find_type)
6899 : Traverse(traverse_types),
6900 find_type_(find_type), found_(false)
6903 // Whether we found the type.
6906 { return this->found_; }
6913 // The type we are looking for.
6914 Named_type* find_type_;
6915 // Whether we found the type.
6919 // Check for FIND_TYPE in TYPE.
6922 Find_type_use::type(Type* type)
6924 if (type->named_type() != NULL && this->find_type_ == type->named_type())
6926 this->found_ = true;
6927 return TRAVERSE_EXIT;
6930 // It's OK if we see a reference to the type in any type which is
6931 // essentially a pointer: a pointer, a slice, a function, a map, or
6933 if (type->points_to() != NULL
6934 || type->is_open_array_type()
6935 || type->function_type() != NULL
6936 || type->map_type() != NULL
6937 || type->channel_type() != NULL)
6938 return TRAVERSE_SKIP_COMPONENTS;
6940 // For an interface, a reference to the type in a method type should
6941 // be ignored, but we have to consider direct inheritance. When
6942 // this is called, there may be cases of direct inheritance
6943 // represented as a method with no name.
6944 if (type->interface_type() != NULL)
6946 const Typed_identifier_list* methods = type->interface_type()->methods();
6947 if (methods != NULL)
6949 for (Typed_identifier_list::const_iterator p = methods->begin();
6950 p != methods->end();
6953 if (p->name().empty())
6955 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
6956 return TRAVERSE_EXIT;
6960 return TRAVERSE_SKIP_COMPONENTS;
6963 // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
6964 // to convert TYPE to the backend representation before we convert
6966 if (type->named_type() != NULL)
6968 switch (type->base()->classification())
6970 case Type::TYPE_ERROR:
6971 case Type::TYPE_BOOLEAN:
6972 case Type::TYPE_INTEGER:
6973 case Type::TYPE_FLOAT:
6974 case Type::TYPE_COMPLEX:
6975 case Type::TYPE_STRING:
6976 case Type::TYPE_NIL:
6979 case Type::TYPE_ARRAY:
6980 case Type::TYPE_STRUCT:
6981 this->find_type_->add_dependency(type->named_type());
6984 case Type::TYPE_VOID:
6985 case Type::TYPE_SINK:
6986 case Type::TYPE_FUNCTION:
6987 case Type::TYPE_POINTER:
6988 case Type::TYPE_CALL_MULTIPLE_RESULT:
6989 case Type::TYPE_MAP:
6990 case Type::TYPE_CHANNEL:
6991 case Type::TYPE_INTERFACE:
6992 case Type::TYPE_NAMED:
6993 case Type::TYPE_FORWARD:
6999 return TRAVERSE_CONTINUE;
7002 // Verify that a named type does not refer to itself.
7005 Named_type::do_verify()
7007 Find_type_use find(this);
7008 Type::traverse(this->type_, &find);
7011 error_at(this->location_, "invalid recursive type %qs",
7012 this->message_name().c_str());
7013 this->is_error_ = true;
7017 // Check whether any of the local methods overloads an existing
7018 // struct field or interface method. We don't need to check the
7019 // list of methods against itself: that is handled by the Bindings
7021 if (this->local_methods_ != NULL)
7023 Struct_type* st = this->type_->struct_type();
7024 Interface_type* it = this->type_->interface_type();
7025 bool found_dup = false;
7026 if (st != NULL || it != NULL)
7028 for (Bindings::const_declarations_iterator p =
7029 this->local_methods_->begin_declarations();
7030 p != this->local_methods_->end_declarations();
7033 const std::string& name(p->first);
7034 if (st != NULL && st->find_local_field(name, NULL) != NULL)
7036 error_at(p->second->location(),
7037 "method %qs redeclares struct field name",
7038 Gogo::message_name(name).c_str());
7041 if (it != NULL && it->find_method(name) != NULL)
7043 error_at(p->second->location(),
7044 "method %qs redeclares interface method name",
7045 Gogo::message_name(name).c_str());
7057 // Return whether this type is or contains a pointer.
7060 Named_type::do_has_pointer() const
7062 if (this->seen_ > 0)
7065 bool ret = this->type_->has_pointer();
7070 // Return a hash code. This is used for method lookup. We simply
7071 // hash on the name itself.
7074 Named_type::do_hash_for_method(Gogo* gogo) const
7076 const std::string& name(this->named_object()->name());
7077 unsigned int ret = Type::hash_string(name, 0);
7079 // GOGO will be NULL here when called from Type_hash_identical.
7080 // That is OK because that is only used for internal hash tables
7081 // where we are going to be comparing named types for equality. In
7082 // other cases, which are cases where the runtime is going to
7083 // compare hash codes to see if the types are the same, we need to
7084 // include the package prefix and name in the hash.
7085 if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
7087 const Package* package = this->named_object()->package();
7088 if (package == NULL)
7090 ret = Type::hash_string(gogo->unique_prefix(), ret);
7091 ret = Type::hash_string(gogo->package_name(), ret);
7095 ret = Type::hash_string(package->unique_prefix(), ret);
7096 ret = Type::hash_string(package->name(), ret);
7103 // Convert a named type to the backend representation. In order to
7104 // get dependencies right, we fill in a dummy structure for this type,
7105 // then convert all the dependencies, then complete this type. When
7106 // this function is complete, the size of the type is known.
7109 Named_type::convert(Gogo* gogo)
7111 if (this->is_error_ || this->is_converted_)
7114 this->create_placeholder(gogo);
7116 // Convert all the dependencies. If they refer indirectly back to
7117 // this type, they will pick up the intermediate tree we just
7119 for (std::vector<Named_type*>::const_iterator p = this->dependencies_.begin();
7120 p != this->dependencies_.end();
7122 (*p)->convert(gogo);
7124 // Complete this type.
7125 tree t = this->named_tree_;
7126 Type* base = this->type_->base();
7127 switch (base->classification())
7144 // The size of these types is already correct.
7148 t = base->struct_type()->fill_in_tree(gogo, t);
7152 if (!base->is_open_array_type())
7153 t = base->array_type()->fill_in_array_tree(gogo, t);
7156 case TYPE_INTERFACE:
7157 if (!base->interface_type()->is_empty())
7158 t = base->interface_type()->fill_in_tree(gogo, t);
7166 case TYPE_CALL_MULTIPLE_RESULT:
7172 this->named_tree_ = t;
7174 if (t == error_mark_node)
7175 this->is_error_ = true;
7177 gcc_assert(TYPE_SIZE(t) != NULL_TREE);
7179 this->is_converted_ = true;
7182 // Create the placeholder for a named type. This is the first step in
7183 // converting to the backend representation.
7186 Named_type::create_placeholder(Gogo* gogo)
7188 if (this->is_error_)
7189 this->named_tree_ = error_mark_node;
7191 if (this->named_tree_ != NULL_TREE)
7194 // Create the structure for this type. Note that because we call
7195 // base() here, we don't attempt to represent a named type defined
7196 // as another named type. Instead both named types will point to
7197 // different base representations.
7198 Type* base = this->type_->base();
7200 switch (base->classification())
7203 this->is_error_ = true;
7204 this->named_tree_ = error_mark_node;
7214 // These are simple basic types, we can just create them
7216 t = Type::get_named_type_tree(gogo, base);
7217 if (t == error_mark_node)
7219 this->is_error_ = true;
7220 this->named_tree_ = error_mark_node;
7223 t = build_variant_type_copy(t);
7228 // All maps and channels have the same type in GENERIC.
7229 t = Type::get_named_type_tree(gogo, base);
7230 if (t == error_mark_node)
7232 this->is_error_ = true;
7233 this->named_tree_ = error_mark_node;
7236 t = build_variant_type_copy(t);
7241 t = build_variant_type_copy(ptr_type_node);
7245 t = make_node(RECORD_TYPE);
7249 if (base->is_open_array_type())
7250 t = gogo->slice_type_tree(void_type_node);
7252 t = make_node(ARRAY_TYPE);
7255 case TYPE_INTERFACE:
7256 if (base->interface_type()->is_empty())
7258 t = Interface_type::empty_type_tree(gogo);
7259 t = build_variant_type_copy(t);
7263 source_location loc = base->interface_type()->location();
7264 t = Interface_type::non_empty_type_tree(loc);
7270 case TYPE_CALL_MULTIPLE_RESULT:
7276 // Create the named type.
7278 tree id = this->named_object_->get_id(gogo);
7279 tree decl = build_decl(this->location_, TYPE_DECL, id, t);
7280 TYPE_NAME(t) = decl;
7282 this->named_tree_ = t;
7285 // Get a tree for a named type.
7288 Named_type::do_get_tree(Gogo* gogo)
7290 if (this->is_error_)
7291 return error_mark_node;
7293 tree t = this->named_tree_;
7295 // FIXME: GOGO can be NULL when called from go_type_for_size, which
7296 // is only used for basic types.
7297 if (gogo == NULL || !gogo->named_types_are_converted())
7299 // We have not completed converting named types. NAMED_TREE_ is
7300 // a placeholder and we shouldn't do anything further.
7304 // We don't build dependencies for types whose sizes do not
7305 // change or are not relevant, so we may see them here while
7306 // converting types.
7307 this->create_placeholder(gogo);
7308 t = this->named_tree_;
7309 gcc_assert(t != NULL_TREE);
7313 // We are not converting types. This should only be called if the
7314 // type has already been converted.
7315 if (!this->is_converted_)
7317 gcc_assert(saw_errors());
7318 return error_mark_node;
7321 gcc_assert(t != NULL_TREE && TYPE_SIZE(t) != NULL_TREE);
7323 // Complete the tree.
7324 Type* base = this->type_->base();
7326 switch (base->classification())
7329 return error_mark_node;
7341 case TYPE_INTERFACE:
7345 // Don't build a circular data structure. GENERIC can't handle
7347 if (this->seen_ > 0)
7349 this->is_circular_ = true;
7350 return ptr_type_node;
7353 t1 = Type::get_named_type_tree(gogo, base);
7355 if (t1 == error_mark_node)
7356 return error_mark_node;
7357 if (this->is_circular_)
7359 gcc_assert(t != NULL_TREE && TREE_CODE(t) == POINTER_TYPE);
7360 gcc_assert(TREE_CODE(t1) == POINTER_TYPE);
7361 TREE_TYPE(t) = TREE_TYPE(t1);
7365 // Don't build a circular data structure. GENERIC can't handle
7367 if (this->seen_ > 0)
7369 this->is_circular_ = true;
7370 return ptr_type_node;
7373 t1 = Type::get_named_type_tree(gogo, base);
7375 if (t1 == error_mark_node)
7376 return error_mark_node;
7377 if (this->is_circular_)
7379 gcc_assert(t != NULL_TREE && TREE_CODE(t) == POINTER_TYPE);
7380 gcc_assert(TREE_CODE(t1) == POINTER_TYPE);
7381 TREE_TYPE(t) = TREE_TYPE(t1);
7385 if (base->is_open_array_type())
7387 if (this->seen_ > 0)
7392 t = base->array_type()->fill_in_slice_tree(gogo, t);
7400 case TYPE_CALL_MULTIPLE_RESULT:
7409 // Build a type descriptor for a named type.
7412 Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
7414 // If NAME is not NULL, then we don't really want the type
7415 // descriptor for this type; we want the descriptor for the
7416 // underlying type, giving it the name NAME.
7417 return this->named_type_descriptor(gogo, this->type_,
7418 name == NULL ? this : name);
7421 // Add to the reflection string. This is used mostly for the name of
7422 // the type used in a type descriptor, not for actual reflection
7426 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
7428 if (this->location() != BUILTINS_LOCATION)
7430 const Package* package = this->named_object_->package();
7431 if (package != NULL)
7432 ret->append(package->name());
7434 ret->append(gogo->package_name());
7435 ret->push_back('.');
7437 if (this->in_function_ != NULL)
7439 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
7440 ret->push_back('$');
7442 ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
7445 // Get the mangled name.
7448 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
7450 Named_object* no = this->named_object_;
7452 if (this->location() == BUILTINS_LOCATION)
7453 gcc_assert(this->in_function_ == NULL);
7456 const std::string& unique_prefix(no->package() == NULL
7457 ? gogo->unique_prefix()
7458 : no->package()->unique_prefix());
7459 const std::string& package_name(no->package() == NULL
7460 ? gogo->package_name()
7461 : no->package()->name());
7462 name = unique_prefix;
7463 name.append(1, '.');
7464 name.append(package_name);
7465 name.append(1, '.');
7466 if (this->in_function_ != NULL)
7468 name.append(Gogo::unpack_hidden_name(this->in_function_->name()));
7469 name.append(1, '$');
7472 name.append(Gogo::unpack_hidden_name(no->name()));
7474 snprintf(buf, sizeof buf, "N%u_", static_cast<unsigned int>(name.length()));
7479 // Export the type. This is called to export a global type.
7482 Named_type::export_named_type(Export* exp, const std::string&) const
7484 // We don't need to write the name of the type here, because it will
7485 // be written by Export::write_type anyhow.
7486 exp->write_c_string("type ");
7487 exp->write_type(this);
7488 exp->write_c_string(";\n");
7491 // Import a named type.
7494 Named_type::import_named_type(Import* imp, Named_type** ptype)
7496 imp->require_c_string("type ");
7497 Type *type = imp->read_type();
7498 *ptype = type->named_type();
7499 gcc_assert(*ptype != NULL);
7500 imp->require_c_string(";\n");
7503 // Export the type when it is referenced by another type. In this
7504 // case Export::export_type will already have issued the name.
7507 Named_type::do_export(Export* exp) const
7509 exp->write_type(this->type_);
7511 // To save space, we only export the methods directly attached to
7513 Bindings* methods = this->local_methods_;
7514 if (methods == NULL)
7517 exp->write_c_string("\n");
7518 for (Bindings::const_definitions_iterator p = methods->begin_definitions();
7519 p != methods->end_definitions();
7522 exp->write_c_string(" ");
7523 (*p)->export_named_object(exp);
7526 for (Bindings::const_declarations_iterator p = methods->begin_declarations();
7527 p != methods->end_declarations();
7530 if (p->second->is_function_declaration())
7532 exp->write_c_string(" ");
7533 p->second->export_named_object(exp);
7538 // Make a named type.
7541 Type::make_named_type(Named_object* named_object, Type* type,
7542 source_location location)
7544 return new Named_type(named_object, type, location);
7547 // Finalize the methods for TYPE. It will be a named type or a struct
7548 // type. This sets *ALL_METHODS to the list of methods, and builds
7549 // all required stubs.
7552 Type::finalize_methods(Gogo* gogo, const Type* type, source_location location,
7553 Methods** all_methods)
7555 *all_methods = NULL;
7556 Types_seen types_seen;
7557 Type::add_methods_for_type(type, NULL, 0, false, false, &types_seen,
7559 Type::build_stub_methods(gogo, type, *all_methods, location);
7562 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
7563 // build up the struct field indexes as we go. DEPTH is the depth of
7564 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
7565 // adding these methods for an anonymous field with pointer type.
7566 // NEEDS_STUB_METHOD is true if we need to use a stub method which
7567 // calls the real method. TYPES_SEEN is used to avoid infinite
7571 Type::add_methods_for_type(const Type* type,
7572 const Method::Field_indexes* field_indexes,
7574 bool is_embedded_pointer,
7575 bool needs_stub_method,
7576 Types_seen* types_seen,
7579 // Pointer types may not have methods.
7580 if (type->points_to() != NULL)
7583 const Named_type* nt = type->named_type();
7586 std::pair<Types_seen::iterator, bool> ins = types_seen->insert(nt);
7592 Type::add_local_methods_for_type(nt, field_indexes, depth,
7593 is_embedded_pointer, needs_stub_method,
7596 Type::add_embedded_methods_for_type(type, field_indexes, depth,
7597 is_embedded_pointer, needs_stub_method,
7598 types_seen, methods);
7600 // If we are called with depth > 0, then we are looking at an
7601 // anonymous field of a struct. If such a field has interface type,
7602 // then we need to add the interface methods. We don't want to add
7603 // them when depth == 0, because we will already handle them
7604 // following the usual rules for an interface type.
7606 Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
7609 // Add the local methods for the named type NT to *METHODS. The
7610 // parameters are as for add_methods_to_type.
7613 Type::add_local_methods_for_type(const Named_type* nt,
7614 const Method::Field_indexes* field_indexes,
7616 bool is_embedded_pointer,
7617 bool needs_stub_method,
7620 const Bindings* local_methods = nt->local_methods();
7621 if (local_methods == NULL)
7624 if (*methods == NULL)
7625 *methods = new Methods();
7627 for (Bindings::const_declarations_iterator p =
7628 local_methods->begin_declarations();
7629 p != local_methods->end_declarations();
7632 Named_object* no = p->second;
7633 bool is_value_method = (is_embedded_pointer
7634 || !Type::method_expects_pointer(no));
7635 Method* m = new Named_method(no, field_indexes, depth, is_value_method,
7637 || (depth > 0 && is_value_method)));
7638 if (!(*methods)->insert(no->name(), m))
7643 // Add the embedded methods for TYPE to *METHODS. These are the
7644 // methods attached to anonymous fields. The parameters are as for
7645 // add_methods_to_type.
7648 Type::add_embedded_methods_for_type(const Type* type,
7649 const Method::Field_indexes* field_indexes,
7651 bool is_embedded_pointer,
7652 bool needs_stub_method,
7653 Types_seen* types_seen,
7656 // Look for anonymous fields in TYPE. TYPE has fields if it is a
7658 const Struct_type* st = type->struct_type();
7662 const Struct_field_list* fields = st->fields();
7667 for (Struct_field_list::const_iterator pf = fields->begin();
7668 pf != fields->end();
7671 if (!pf->is_anonymous())
7674 Type* ftype = pf->type();
7675 bool is_pointer = false;
7676 if (ftype->points_to() != NULL)
7678 ftype = ftype->points_to();
7681 Named_type* fnt = ftype->named_type();
7684 // This is an error, but it will be diagnosed elsewhere.
7688 Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
7689 sub_field_indexes->next = field_indexes;
7690 sub_field_indexes->field_index = i;
7692 Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
7693 (is_embedded_pointer || is_pointer),
7702 // If TYPE is an interface type, then add its method to *METHODS.
7703 // This is for interface methods attached to an anonymous field. The
7704 // parameters are as for add_methods_for_type.
7707 Type::add_interface_methods_for_type(const Type* type,
7708 const Method::Field_indexes* field_indexes,
7712 const Interface_type* it = type->interface_type();
7716 const Typed_identifier_list* imethods = it->methods();
7717 if (imethods == NULL)
7720 if (*methods == NULL)
7721 *methods = new Methods();
7723 for (Typed_identifier_list::const_iterator pm = imethods->begin();
7724 pm != imethods->end();
7727 Function_type* fntype = pm->type()->function_type();
7730 // This is an error, but it should be reported elsewhere
7731 // when we look at the methods for IT.
7734 gcc_assert(!fntype->is_method());
7735 fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
7736 Method* m = new Interface_method(pm->name(), pm->location(), fntype,
7737 field_indexes, depth);
7738 if (!(*methods)->insert(pm->name(), m))
7743 // Build stub methods for TYPE as needed. METHODS is the set of
7744 // methods for the type. A stub method may be needed when a type
7745 // inherits a method from an anonymous field. When we need the
7746 // address of the method, as in a type descriptor, we need to build a
7747 // little stub which does the required field dereferences and jumps to
7748 // the real method. LOCATION is the location of the type definition.
7751 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
7752 source_location location)
7754 if (methods == NULL)
7756 for (Methods::const_iterator p = methods->begin();
7757 p != methods->end();
7760 Method* m = p->second;
7761 if (m->is_ambiguous() || !m->needs_stub_method())
7764 const std::string& name(p->first);
7766 // Build a stub method.
7768 const Function_type* fntype = m->type();
7770 static unsigned int counter;
7772 snprintf(buf, sizeof buf, "$this%u", counter);
7775 Type* receiver_type = const_cast<Type*>(type);
7776 if (!m->is_value_method())
7777 receiver_type = Type::make_pointer_type(receiver_type);
7778 source_location receiver_location = m->receiver_location();
7779 Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
7782 const Typed_identifier_list* fnparams = fntype->parameters();
7783 Typed_identifier_list* stub_params;
7784 if (fnparams == NULL || fnparams->empty())
7788 // We give each stub parameter a unique name.
7789 stub_params = new Typed_identifier_list();
7790 for (Typed_identifier_list::const_iterator pp = fnparams->begin();
7791 pp != fnparams->end();
7795 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
7796 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
7802 const Typed_identifier_list* fnresults = fntype->results();
7803 Typed_identifier_list* stub_results;
7804 if (fnresults == NULL || fnresults->empty())
7805 stub_results = NULL;
7808 // We create the result parameters without any names, since
7809 // we won't refer to them.
7810 stub_results = new Typed_identifier_list();
7811 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
7812 pr != fnresults->end();
7814 stub_results->push_back(Typed_identifier("", pr->type(),
7818 Function_type* stub_type = Type::make_function_type(receiver,
7821 fntype->location());
7822 if (fntype->is_varargs())
7823 stub_type->set_is_varargs();
7825 // We only create the function in the package which creates the
7827 const Package* package;
7828 if (type->named_type() == NULL)
7831 package = type->named_type()->named_object()->package();
7833 if (package != NULL)
7834 stub = Named_object::make_function_declaration(name, package,
7835 stub_type, location);
7838 stub = gogo->start_function(name, stub_type, false,
7839 fntype->location());
7840 Type::build_one_stub_method(gogo, m, buf, stub_params,
7841 fntype->is_varargs(), location);
7842 gogo->finish_function(fntype->location());
7845 m->set_stub_object(stub);
7849 // Build a stub method which adjusts the receiver as required to call
7850 // METHOD. RECEIVER_NAME is the name we used for the receiver.
7851 // PARAMS is the list of function parameters.
7854 Type::build_one_stub_method(Gogo* gogo, Method* method,
7855 const char* receiver_name,
7856 const Typed_identifier_list* params,
7858 source_location location)
7860 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
7861 gcc_assert(receiver_object != NULL);
7863 Expression* expr = Expression::make_var_reference(receiver_object, location);
7864 expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
7865 if (expr->type()->points_to() == NULL)
7866 expr = Expression::make_unary(OPERATOR_AND, expr, location);
7868 Expression_list* arguments;
7869 if (params == NULL || params->empty())
7873 arguments = new Expression_list();
7874 for (Typed_identifier_list::const_iterator p = params->begin();
7878 Named_object* param = gogo->lookup(p->name(), NULL);
7879 gcc_assert(param != NULL);
7880 Expression* param_ref = Expression::make_var_reference(param,
7882 arguments->push_back(param_ref);
7886 Expression* func = method->bind_method(expr, location);
7887 gcc_assert(func != NULL);
7888 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
7890 size_t count = call->result_count();
7892 gogo->add_statement(Statement::make_statement(call));
7895 Expression_list* retvals = new Expression_list();
7897 retvals->push_back(call);
7900 for (size_t i = 0; i < count; ++i)
7901 retvals->push_back(Expression::make_call_result(call, i));
7903 const Function* function = gogo->current_function()->func_value();
7904 const Typed_identifier_list* results = function->type()->results();
7905 Statement* retstat = Statement::make_return_statement(results, retvals,
7907 gogo->add_statement(retstat);
7911 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
7912 // in reverse order.
7915 Type::apply_field_indexes(Expression* expr,
7916 const Method::Field_indexes* field_indexes,
7917 source_location location)
7919 if (field_indexes == NULL)
7921 expr = Type::apply_field_indexes(expr, field_indexes->next, location);
7922 Struct_type* stype = expr->type()->deref()->struct_type();
7923 gcc_assert(stype != NULL
7924 && field_indexes->field_index < stype->field_count());
7925 if (expr->type()->struct_type() == NULL)
7927 gcc_assert(expr->type()->points_to() != NULL);
7928 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
7929 gcc_assert(expr->type()->struct_type() == stype);
7931 return Expression::make_field_reference(expr, field_indexes->field_index,
7935 // Return whether NO is a method for which the receiver is a pointer.
7938 Type::method_expects_pointer(const Named_object* no)
7940 const Function_type *fntype;
7941 if (no->is_function())
7942 fntype = no->func_value()->type();
7943 else if (no->is_function_declaration())
7944 fntype = no->func_declaration_value()->type();
7947 return fntype->receiver()->type()->points_to() != NULL;
7950 // Given a set of methods for a type, METHODS, return the method NAME,
7951 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
7952 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
7953 // but is ambiguous (and return NULL).
7956 Type::method_function(const Methods* methods, const std::string& name,
7959 if (is_ambiguous != NULL)
7960 *is_ambiguous = false;
7961 if (methods == NULL)
7963 Methods::const_iterator p = methods->find(name);
7964 if (p == methods->end())
7966 Method* m = p->second;
7967 if (m->is_ambiguous())
7969 if (is_ambiguous != NULL)
7970 *is_ambiguous = true;
7976 // Look for field or method NAME for TYPE. Return an Expression for
7977 // the field or method bound to EXPR. If there is no such field or
7978 // method, give an appropriate error and return an error expression.
7981 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
7982 const std::string& name,
7983 source_location location)
7985 if (type->deref()->is_error_type())
7986 return Expression::make_error(location);
7988 const Named_type* nt = type->deref()->named_type();
7989 const Struct_type* st = type->deref()->struct_type();
7990 const Interface_type* it = type->deref()->interface_type();
7992 // If this is a pointer to a pointer, then it is possible that the
7993 // pointed-to type has methods.
7997 && type->points_to() != NULL
7998 && type->points_to()->points_to() != NULL)
8000 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
8001 type = type->points_to();
8002 if (type->deref()->is_error_type())
8003 return Expression::make_error(location);
8004 nt = type->points_to()->named_type();
8005 st = type->points_to()->struct_type();
8006 it = type->points_to()->interface_type();
8009 bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
8010 || expr->is_addressable());
8011 std::vector<const Named_type*> seen;
8012 bool is_method = false;
8013 bool found_pointer_method = false;
8016 if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
8017 &seen, NULL, &is_method,
8018 &found_pointer_method, &ambig1, &ambig2))
8023 gcc_assert(st != NULL);
8024 if (type->struct_type() == NULL)
8026 gcc_assert(type->points_to() != NULL);
8027 expr = Expression::make_unary(OPERATOR_MULT, expr,
8029 gcc_assert(expr->type()->struct_type() == st);
8031 ret = st->field_reference(expr, name, location);
8033 else if (it != NULL && it->find_method(name) != NULL)
8034 ret = Expression::make_interface_field_reference(expr, name,
8040 m = nt->method_function(name, NULL);
8041 else if (st != NULL)
8042 m = st->method_function(name, NULL);
8045 gcc_assert(m != NULL);
8046 if (!m->is_value_method() && expr->type()->points_to() == NULL)
8047 expr = Expression::make_unary(OPERATOR_AND, expr, location);
8048 ret = m->bind_method(expr, location);
8050 gcc_assert(ret != NULL);
8055 if (!ambig1.empty())
8056 error_at(location, "%qs is ambiguous via %qs and %qs",
8057 Gogo::message_name(name).c_str(),
8058 Gogo::message_name(ambig1).c_str(),
8059 Gogo::message_name(ambig2).c_str());
8060 else if (found_pointer_method)
8061 error_at(location, "method requires a pointer");
8062 else if (nt == NULL && st == NULL && it == NULL)
8064 ("reference to field %qs in object which "
8065 "has no fields or methods"),
8066 Gogo::message_name(name).c_str());
8070 if (!Gogo::is_hidden_name(name))
8071 is_unexported = false;
8074 std::string unpacked = Gogo::unpack_hidden_name(name);
8076 is_unexported = Type::is_unexported_field_or_method(gogo, type,
8081 error_at(location, "reference to unexported field or method %qs",
8082 Gogo::message_name(name).c_str());
8084 error_at(location, "reference to undefined field or method %qs",
8085 Gogo::message_name(name).c_str());
8087 return Expression::make_error(location);
8091 // Look in TYPE for a field or method named NAME, return true if one
8092 // is found. This looks through embedded anonymous fields and handles
8093 // ambiguity. If a method is found, sets *IS_METHOD to true;
8094 // otherwise, if a field is found, set it to false. If
8095 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
8096 // whose address can not be taken. SEEN is used to avoid infinite
8097 // recursion on invalid types.
8099 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
8100 // method we couldn't use because it requires a pointer. LEVEL is
8101 // used for recursive calls, and can be NULL for a non-recursive call.
8102 // When this function returns false because it finds that the name is
8103 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
8104 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
8105 // will be unchanged.
8107 // This function just returns whether or not there is a field or
8108 // method, and whether it is a field or method. It doesn't build an
8109 // expression to refer to it. If it is a method, we then look in the
8110 // list of all methods for the type. If it is a field, the search has
8111 // to be done again, looking only for fields, and building up the
8112 // expression as we go.
8115 Type::find_field_or_method(const Type* type,
8116 const std::string& name,
8117 bool receiver_can_be_pointer,
8118 std::vector<const Named_type*>* seen,
8121 bool* found_pointer_method,
8122 std::string* ambig1,
8123 std::string* ambig2)
8125 // Named types can have locally defined methods.
8126 const Named_type* nt = type->named_type();
8127 if (nt == NULL && type->points_to() != NULL)
8128 nt = type->points_to()->named_type();
8131 Named_object* no = nt->find_local_method(name);
8134 if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
8140 // Record that we have found a pointer method in order to
8141 // give a better error message if we don't find anything
8143 *found_pointer_method = true;
8146 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
8152 // We've already seen this type when searching for methods.
8158 // Interface types can have methods.
8159 const Interface_type* it = type->deref()->interface_type();
8160 if (it != NULL && it->find_method(name) != NULL)
8166 // Struct types can have fields. They can also inherit fields and
8167 // methods from anonymous fields.
8168 const Struct_type* st = type->deref()->struct_type();
8171 const Struct_field_list* fields = st->fields();
8176 seen->push_back(nt);
8178 int found_level = 0;
8179 bool found_is_method = false;
8180 std::string found_ambig1;
8181 std::string found_ambig2;
8182 const Struct_field* found_parent = NULL;
8183 for (Struct_field_list::const_iterator pf = fields->begin();
8184 pf != fields->end();
8187 if (pf->field_name() == name)
8195 if (!pf->is_anonymous())
8198 if (pf->type()->deref()->is_error_type()
8199 || pf->type()->deref()->is_undefined())
8202 Named_type* fnt = pf->type()->named_type();
8204 fnt = pf->type()->deref()->named_type();
8205 gcc_assert(fnt != NULL);
8207 int sublevel = level == NULL ? 1 : *level + 1;
8209 std::string subambig1;
8210 std::string subambig2;
8211 bool subfound = Type::find_field_or_method(fnt,
8213 receiver_can_be_pointer,
8217 found_pointer_method,
8222 if (!subambig1.empty())
8224 // The name was found via this field, but is ambiguous.
8225 // if the ambiguity is lower or at the same level as
8226 // anything else we have already found, then we want to
8227 // pass the ambiguity back to the caller.
8228 if (found_level == 0 || sublevel <= found_level)
8230 found_ambig1 = pf->field_name() + '.' + subambig1;
8231 found_ambig2 = pf->field_name() + '.' + subambig2;
8232 found_level = sublevel;
8238 // The name was found via this field. Use the level to see
8239 // if we want to use this one, or whether it introduces an
8241 if (found_level == 0 || sublevel < found_level)
8243 found_level = sublevel;
8244 found_is_method = sub_is_method;
8245 found_ambig1.clear();
8246 found_ambig2.clear();
8247 found_parent = &*pf;
8249 else if (sublevel > found_level)
8251 else if (found_ambig1.empty())
8253 // We found an ambiguity.
8254 gcc_assert(found_parent != NULL);
8255 found_ambig1 = found_parent->field_name();
8256 found_ambig2 = pf->field_name();
8260 // We found an ambiguity, but we already know of one.
8261 // Just report the earlier one.
8266 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
8267 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
8268 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
8269 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
8274 if (found_level == 0)
8276 else if (!found_ambig1.empty())
8278 gcc_assert(!found_ambig1.empty());
8279 ambig1->assign(found_ambig1);
8280 ambig2->assign(found_ambig2);
8282 *level = found_level;
8288 *level = found_level;
8289 *is_method = found_is_method;
8294 // Return whether NAME is an unexported field or method for TYPE.
8297 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
8298 const std::string& name,
8299 std::vector<const Named_type*>* seen)
8301 const Named_type* nt = type->named_type();
8303 nt = type->deref()->named_type();
8306 if (nt->is_unexported_local_method(gogo, name))
8309 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
8315 // We've already seen this type.
8321 type = type->deref();
8323 const Interface_type* it = type->interface_type();
8324 if (it != NULL && it->is_unexported_method(gogo, name))
8327 const Struct_type* st = type->struct_type();
8328 if (st != NULL && st->is_unexported_local_field(gogo, name))
8334 const Struct_field_list* fields = st->fields();
8339 seen->push_back(nt);
8341 for (Struct_field_list::const_iterator pf = fields->begin();
8342 pf != fields->end();
8345 if (pf->is_anonymous()
8346 && !pf->type()->deref()->is_error_type()
8347 && !pf->type()->deref()->is_undefined())
8349 Named_type* subtype = pf->type()->named_type();
8350 if (subtype == NULL)
8351 subtype = pf->type()->deref()->named_type();
8352 if (subtype == NULL)
8354 // This is an error, but it will be diagnosed elsewhere.
8357 if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
8372 // Class Forward_declaration.
8374 Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
8375 : Type(TYPE_FORWARD),
8376 named_object_(named_object->resolve()), warned_(false)
8378 gcc_assert(this->named_object_->is_unknown()
8379 || this->named_object_->is_type_declaration());
8382 // Return the named object.
8385 Forward_declaration_type::named_object()
8387 return this->named_object_->resolve();
8391 Forward_declaration_type::named_object() const
8393 return this->named_object_->resolve();
8396 // Return the name of the forward declared type.
8399 Forward_declaration_type::name() const
8401 return this->named_object()->name();
8404 // Warn about a use of a type which has been declared but not defined.
8407 Forward_declaration_type::warn() const
8409 Named_object* no = this->named_object_->resolve();
8410 if (no->is_unknown())
8412 // The name was not defined anywhere.
8415 error_at(this->named_object_->location(),
8416 "use of undefined type %qs",
8417 no->message_name().c_str());
8418 this->warned_ = true;
8421 else if (no->is_type_declaration())
8423 // The name was seen as a type, but the type was never defined.
8424 if (no->type_declaration_value()->using_type())
8426 error_at(this->named_object_->location(),
8427 "use of undefined type %qs",
8428 no->message_name().c_str());
8429 this->warned_ = true;
8434 // The name was defined, but not as a type.
8437 error_at(this->named_object_->location(), "expected type");
8438 this->warned_ = true;
8443 // Get the base type of a declaration. This gives an error if the
8444 // type has not yet been defined.
8447 Forward_declaration_type::real_type()
8449 if (this->is_defined())
8450 return this->named_object()->type_value();
8454 return Type::make_error_type();
8459 Forward_declaration_type::real_type() const
8461 if (this->is_defined())
8462 return this->named_object()->type_value();
8466 return Type::make_error_type();
8470 // Return whether the base type is defined.
8473 Forward_declaration_type::is_defined() const
8475 return this->named_object()->is_type();
8478 // Add a method. This is used when methods are defined before the
8482 Forward_declaration_type::add_method(const std::string& name,
8485 Named_object* no = this->named_object();
8486 if (no->is_unknown())
8487 no->declare_as_type();
8488 return no->type_declaration_value()->add_method(name, function);
8491 // Add a method declaration. This is used when methods are declared
8495 Forward_declaration_type::add_method_declaration(const std::string& name,
8496 Function_type* type,
8497 source_location location)
8499 Named_object* no = this->named_object();
8500 if (no->is_unknown())
8501 no->declare_as_type();
8502 Type_declaration* td = no->type_declaration_value();
8503 return td->add_method_declaration(name, type, location);
8509 Forward_declaration_type::do_traverse(Traverse* traverse)
8511 if (this->is_defined()
8512 && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
8513 return TRAVERSE_EXIT;
8514 return TRAVERSE_CONTINUE;
8517 // Get a tree for the type.
8520 Forward_declaration_type::do_get_tree(Gogo* gogo)
8522 if (this->is_defined())
8523 return Type::get_named_type_tree(gogo, this->real_type());
8526 return error_mark_node;
8528 // We represent an undefined type as a struct with no fields. That
8529 // should work fine for the middle-end, since the same case can
8531 Named_object* no = this->named_object();
8532 tree type_tree = make_node(RECORD_TYPE);
8533 tree id = no->get_id(gogo);
8534 tree decl = build_decl(no->location(), TYPE_DECL, id, type_tree);
8535 TYPE_NAME(type_tree) = decl;
8536 layout_type(type_tree);
8540 // Build a type descriptor for a forwarded type.
8543 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8545 if (!this->is_defined())
8546 return Expression::make_nil(BUILTINS_LOCATION);
8549 Type* t = this->real_type();
8551 return this->named_type_descriptor(gogo, t, name);
8553 return Expression::make_type_descriptor(t, BUILTINS_LOCATION);
8557 // The reflection string.
8560 Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
8562 this->append_reflection(this->real_type(), gogo, ret);
8565 // The mangled name.
8568 Forward_declaration_type::do_mangled_name(Gogo* gogo, std::string* ret) const
8570 if (this->is_defined())
8571 this->append_mangled_name(this->real_type(), gogo, ret);
8574 const Named_object* no = this->named_object();
8576 if (no->package() == NULL)
8577 name = gogo->package_name();
8579 name = no->package()->name();
8581 name += Gogo::unpack_hidden_name(no->name());
8583 snprintf(buf, sizeof buf, "N%u_",
8584 static_cast<unsigned int>(name.length()));
8590 // Export a forward declaration. This can happen when a defined type
8591 // refers to a type which is only declared (and is presumably defined
8592 // in some other file in the same package).
8595 Forward_declaration_type::do_export(Export*) const
8597 // If there is a base type, that should be exported instead of this.
8598 gcc_assert(!this->is_defined());
8600 // We don't output anything.
8603 // Make a forward declaration.
8606 Type::make_forward_declaration(Named_object* named_object)
8608 return new Forward_declaration_type(named_object);
8611 // Class Typed_identifier_list.
8613 // Sort the entries by name.
8615 struct Typed_identifier_list_sort
8619 operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
8620 { return t1.name() < t2.name(); }
8624 Typed_identifier_list::sort_by_name()
8626 std::sort(this->entries_.begin(), this->entries_.end(),
8627 Typed_identifier_list_sort());
8633 Typed_identifier_list::traverse(Traverse* traverse)
8635 for (Typed_identifier_list::const_iterator p = this->begin();
8639 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
8640 return TRAVERSE_EXIT;
8642 return TRAVERSE_CONTINUE;
8647 Typed_identifier_list*
8648 Typed_identifier_list::copy() const
8650 Typed_identifier_list* ret = new Typed_identifier_list();
8651 for (Typed_identifier_list::const_iterator p = this->begin();
8654 ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));