comparison gcc/go/gofrontend/types.h @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
184 { 184 {
185 go_assert(this->stub_ == NULL); 185 go_assert(this->stub_ == NULL);
186 this->stub_ = no; 186 this->stub_ = no;
187 } 187 }
188 188
189 // Get the direct interface method stub object.
190 Named_object*
191 iface_stub_object() const
192 {
193 go_assert(this->iface_stub_ != NULL);
194 return this->iface_stub_;
195 }
196
197 // Set the direct interface method stub object.
198 void
199 set_iface_stub_object(Named_object* no)
200 {
201 go_assert(this->iface_stub_ == NULL);
202 this->iface_stub_ = no;
203 }
204
189 // Return true if this method should not participate in any 205 // Return true if this method should not participate in any
190 // interfaces. 206 // interfaces.
191 bool 207 bool
192 nointerface() const 208 nointerface() const
193 { return this->do_nointerface(); } 209 { return this->do_nointerface(); }
194 210
195 protected: 211 protected:
196 // These objects are only built by the child classes. 212 // These objects are only built by the child classes.
197 Method(const Field_indexes* field_indexes, unsigned int depth, 213 Method(const Field_indexes* field_indexes, unsigned int depth,
198 bool is_value_method, bool needs_stub_method) 214 bool is_value_method, bool needs_stub_method)
199 : field_indexes_(field_indexes), depth_(depth), stub_(NULL), 215 : field_indexes_(field_indexes), depth_(depth), stub_(NULL), iface_stub_(NULL),
200 is_value_method_(is_value_method), needs_stub_method_(needs_stub_method), 216 is_value_method_(is_value_method), needs_stub_method_(needs_stub_method),
201 is_ambiguous_(false) 217 is_ambiguous_(false)
202 { } 218 { }
203 219
204 // The named object for this method. 220 // The named object for this method.
228 // The depth at which this method was found. 244 // The depth at which this method was found.
229 unsigned int depth_; 245 unsigned int depth_;
230 // If a stub method is required, this is its object. This is only 246 // If a stub method is required, this is its object. This is only
231 // set after stub methods are built in finalize_methods. 247 // set after stub methods are built in finalize_methods.
232 Named_object* stub_; 248 Named_object* stub_;
249 // Stub object for direct interface type. This is only set after
250 // stub methods are built in finalize_methods.
251 Named_object* iface_stub_;
233 // Whether this is a value method--a method that does not require a 252 // Whether this is a value method--a method that does not require a
234 // pointer. 253 // pointer.
235 bool is_value_method_; 254 bool is_value_method_;
236 // Whether a stub method is required. 255 // Whether a stub method is required.
237 bool needs_stub_method_; 256 bool needs_stub_method_;
575 static const int COMPARE_TAGS = 2; 594 static const int COMPARE_TAGS = 2;
576 595
577 // Compare aliases: treat an alias to T as distinct from T. 596 // Compare aliases: treat an alias to T as distinct from T.
578 static const int COMPARE_ALIASES = 4; 597 static const int COMPARE_ALIASES = 4;
579 598
599 // When comparing interface types compare the interface embedding heirarchy,
600 // if any, rather than only comparing method sets. Useful primarily when
601 // exporting types.
602 static const int COMPARE_EMBEDDED_INTERFACES = 8;
603
580 // Return true if two types are identical. If this returns false, 604 // Return true if two types are identical. If this returns false,
581 // and REASON is not NULL, it may set *REASON. 605 // and REASON is not NULL, it may set *REASON.
582 static bool 606 static bool
583 are_identical(const Type* lhs, const Type* rhs, int flags, 607 are_identical(const Type* lhs, const Type* rhs, int flags,
584 std::string* reason); 608 std::string* reason);
633 // requires the key to be updated when an assignment is made. 657 // requires the key to be updated when an assignment is made.
634 bool 658 bool
635 needs_key_update() 659 needs_key_update()
636 { return this->do_needs_key_update(); } 660 { return this->do_needs_key_update(); }
637 661
662 // Return whether the hash function of this type might panic. This
663 // is only called for types used as a key in a map type.
664 bool
665 hash_might_panic()
666 { return this->do_hash_might_panic(); }
667
638 // Whether the type is permitted in the heap. 668 // Whether the type is permitted in the heap.
639 bool 669 bool
640 in_heap() 670 in_heap()
641 { return this->do_in_heap(); } 671 { return this->do_in_heap(); }
642 672
910 // represent that as pointer-to-void. 940 // represent that as pointer-to-void.
911 bool 941 bool
912 is_unsafe_pointer_type() const 942 is_unsafe_pointer_type() const
913 { return this->points_to() != NULL && this->points_to()->is_void_type(); } 943 { return this->points_to() != NULL && this->points_to()->is_void_type(); }
914 944
945 // Return whether this type is stored directly in an interface's
946 // data word.
947 bool
948 is_direct_iface_type() const;
949
915 // Return a version of this type with any expressions copied, but 950 // Return a version of this type with any expressions copied, but
916 // only if copying the expressions will affect the size of the type. 951 // only if copying the expressions will affect the size of the type.
917 // If there are no such expressions in the type (expressions can 952 // If there are no such expressions in the type (expressions can
918 // only occur in array types), just return the same type. If any 953 // only occur in array types), just return the same type. If any
919 // expressions can not affect the size of the type, just return the 954 // expressions can not affect the size of the type, just return the
1016 1051
1017 // Return whether the type needs specially built type functions. 1052 // Return whether the type needs specially built type functions.
1018 bool 1053 bool
1019 needs_specific_type_functions(Gogo*); 1054 needs_specific_type_functions(Gogo*);
1020 1055
1021 // Get the hash and equality functions for a type. 1056 // Get the equality function for a type. Returns NULL if the type
1022 void 1057 // is not comparable.
1023 type_functions(Gogo*, Named_type* name, Function_type* hash_fntype, 1058 Named_object*
1024 Function_type* equal_fntype, Named_object** hash_fn, 1059 equal_function(Gogo*, Named_type* name, Function_type* equal_fntype);
1025 Named_object** equal_fn); 1060
1026 1061 // Get the hash function for a type. Returns NULL if the type is
1027 // Write the hash and equality type functions. 1062 // not comparable.
1028 void 1063 Named_object*
1029 write_specific_type_functions(Gogo*, Named_type*, int64_t size, 1064 hash_function(Gogo*, Function_type* hash_fntype);
1030 const std::string& hash_name, 1065
1031 Function_type* hash_fntype, 1066 // Write the equal function for a type.
1032 const std::string& equal_name, 1067 void
1033 Function_type* equal_fntype); 1068 write_equal_function(Gogo*, Named_type*, int64_t size,
1069 const std::string& equal_name,
1070 Function_type* equal_fntype);
1071
1072 // Write the hash function for a type.
1073 void
1074 write_hash_function(Gogo*, int64_t size, const std::string& hash_name,
1075 Function_type* hash_fntype);
1034 1076
1035 // Return the alignment required by the memequalN function. 1077 // Return the alignment required by the memequalN function.
1036 static int64_t memequal_align(Gogo*, int size); 1078 static int64_t memequal_align(Gogo*, int size);
1037 1079
1038 // Export the type. 1080 // Export the type.
1069 do_is_reflexive() 1111 do_is_reflexive()
1070 { return true; } 1112 { return true; }
1071 1113
1072 virtual bool 1114 virtual bool
1073 do_needs_key_update() 1115 do_needs_key_update()
1116 { return false; }
1117
1118 virtual bool
1119 do_hash_might_panic()
1074 { return false; } 1120 { return false; }
1075 1121
1076 virtual bool 1122 virtual bool
1077 do_in_heap() 1123 do_in_heap()
1078 { return true; } 1124 { return true; }
1148 1194
1149 // For the benefit of child class mangling. 1195 // For the benefit of child class mangling.
1150 void 1196 void
1151 append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const 1197 append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
1152 { type->do_mangled_name(gogo, ret); } 1198 { type->do_mangled_name(gogo, ret); }
1153
1154 // Incorporate a string into a hash code.
1155 static unsigned int
1156 hash_string(const std::string&, unsigned int);
1157 1199
1158 // Return the backend representation for the underlying type of a 1200 // Return the backend representation for the underlying type of a
1159 // named type. 1201 // named type.
1160 static Btype* 1202 static Btype*
1161 get_named_base_btype(Gogo* gogo, Type* base_type) 1203 get_named_base_btype(Gogo* gogo, Type* base_type)
1237 // Make a composite literal for the garbage collection program for 1279 // Make a composite literal for the garbage collection program for
1238 // this type. 1280 // this type.
1239 Expression* 1281 Expression*
1240 gcprog_constructor(Gogo*, int64_t ptrsize, int64_t ptrdata); 1282 gcprog_constructor(Gogo*, int64_t ptrsize, int64_t ptrdata);
1241 1283
1242 // Build the hash and equality type functions for a type which needs 1284 // Build the hash function for a type that needs specific functions.
1243 // specific functions. 1285 Named_object*
1244 void 1286 build_hash_function(Gogo*, int64_t size, Function_type* hash_fntype);
1245 specific_type_functions(Gogo*, Named_type*, int64_t size, 1287
1246 Function_type* hash_fntype, 1288 // Build the equal function for a type that needs specific functions.
1247 Function_type* equal_fntype, Named_object** hash_fn, 1289 Named_object*
1248 Named_object** equal_fn); 1290 build_equal_function(Gogo*, Named_type*, int64_t size,
1291 Function_type* equal_fntype);
1249 1292
1250 void 1293 void
1251 write_identity_hash(Gogo*, int64_t size); 1294 write_identity_hash(Gogo*, int64_t size);
1252 1295
1253 void 1296 void
1254 write_identity_equal(Gogo*, int64_t size); 1297 write_identity_equal(Gogo*, int64_t size);
1255
1256 void
1257 write_named_hash(Gogo*, Named_type*, Function_type* hash_fntype,
1258 Function_type* equal_fntype);
1259 1298
1260 void 1299 void
1261 write_named_equal(Gogo*, Named_type*); 1300 write_named_equal(Gogo*, Named_type*);
1262 1301
1263 // Build a composite literal for the uncommon type information. 1302 // Build a composite literal for the uncommon type information.
1307 1346
1308 static void 1347 static void
1309 build_one_stub_method(Gogo*, Method*, const char* receiver_name, 1348 build_one_stub_method(Gogo*, Method*, const char* receiver_name,
1310 const Typed_identifier_list*, bool is_varargs, 1349 const Typed_identifier_list*, bool is_varargs,
1311 Location); 1350 Location);
1351
1352 // Build direct interface stub methods for a type.
1353 static void
1354 build_direct_iface_stub_methods(Gogo*, const Type*, Methods*, Location);
1355
1356 static void
1357 build_one_iface_stub_method(Gogo*, Method*, const char*,
1358 const Typed_identifier_list*,
1359 bool, Location);
1312 1360
1313 static Expression* 1361 static Expression*
1314 apply_field_indexes(Expression*, const Method::Field_indexes*, 1362 apply_field_indexes(Expression*, const Method::Field_indexes*,
1315 Location); 1363 Location);
1316 1364
1320 bool receiver_can_be_pointer, 1368 bool receiver_can_be_pointer,
1321 std::vector<const Named_type*>*, int* level, 1369 std::vector<const Named_type*>*, int* level,
1322 bool* is_method, bool* found_pointer_method, 1370 bool* is_method, bool* found_pointer_method,
1323 std::string* ambig1, std::string* ambig2); 1371 std::string* ambig1, std::string* ambig2);
1324 1372
1373 // Helper function for is_direct_iface_type, to prevent infinite
1374 // recursion.
1375 bool
1376 is_direct_iface_type_helper(Unordered_set(const Type*)*) const;
1377
1325 // Get the backend representation for a type without looking in the 1378 // Get the backend representation for a type without looking in the
1326 // hash table for identical types. 1379 // hash table for identical types.
1327 Btype* 1380 Btype*
1328 get_btype_without_hash(Gogo*); 1381 get_btype_without_hash(Gogo*);
1329 1382
1343 static Type_btypes type_btypes; 1396 static Type_btypes type_btypes;
1344 1397
1345 // A list of builtin named types. 1398 // A list of builtin named types.
1346 static std::vector<Named_type*> named_builtin_types; 1399 static std::vector<Named_type*> named_builtin_types;
1347 1400
1348 // A map from types which need specific type functions to the type 1401 // A map from types that need a specific hash or equality function
1349 // functions themselves. 1402 // to the hash or equality function.
1350 typedef std::pair<Named_object*, Named_object*> Hash_equal_fn; 1403 typedef Unordered_map_hash(const Type*, Named_object*, Type_hash_identical,
1351 typedef Unordered_map_hash(const Type*, Hash_equal_fn, Type_hash_identical, 1404 Type_identical) Type_function;
1352 Type_identical) Type_functions; 1405
1353 1406 static Type_function type_hash_functions_table;
1354 static Type_functions type_functions_table; 1407 static Type_function type_equal_functions_table;
1355 1408
1356 // Cache for reusing existing pointer types; maps from pointed-to-type 1409 // Cache for reusing existing pointer types; maps from pointed-to-type
1357 // to pointer type. 1410 // to pointer type.
1358 typedef Unordered_map(Type*, Pointer_type*) Pointer_type_table; 1411 typedef Unordered_map(Type*, Pointer_type*) Pointer_type_table;
1359 1412
1360 static Pointer_type_table pointer_types; 1413 static Pointer_type_table pointer_types;
1361 1414
1362 // List of placeholder pointer types. 1415 // List of placeholder pointer types.
1363 static std::vector<Pointer_type*> placeholder_pointers; 1416 static std::vector<Type*> placeholder_pointers;
1364 1417
1365 // The type classification. 1418 // The type classification.
1366 Type_classification classification_; 1419 Type_classification classification_;
1367 // The backend representation of the type, once it has been 1420 // The backend representation of the type, once it has been
1368 // determined. 1421 // determined.
1443 { return this->note_; } 1496 { return this->note_; }
1444 1497
1445 // Set the escape note. 1498 // Set the escape note.
1446 void 1499 void
1447 set_note(const std::string& note) 1500 set_note(const std::string& note)
1448 { this->note_ = new std::string(note); } 1501 {
1502 if (this->note_ != NULL)
1503 go_assert(*this->note_ == note);
1504 else
1505 this->note_ = new std::string(note);
1506 }
1449 1507
1450 private: 1508 private:
1451 // Identifier name. 1509 // Identifier name.
1452 std::string name_; 1510 std::string name_;
1453 // Type. 1511 // Type.
2430 { 2488 {
2431 public: 2489 public:
2432 Struct_type(Struct_field_list* fields, Location location) 2490 Struct_type(Struct_field_list* fields, Location location)
2433 : Type(TYPE_STRUCT), 2491 : Type(TYPE_STRUCT),
2434 fields_(fields), location_(location), all_methods_(NULL), 2492 fields_(fields), location_(location), all_methods_(NULL),
2435 is_struct_incomparable_(false) 2493 is_struct_incomparable_(false), has_padding_(false)
2436 { } 2494 { }
2437 2495
2438 // Return the field NAME. This only looks at local fields, not at 2496 // Return the field NAME. This only looks at local fields, not at
2439 // embedded types. If the field is found, and PINDEX is not NULL, 2497 // embedded types. If the field is found, and PINDEX is not NULL,
2440 // this sets *PINDEX to the field index. If the field is not found, 2498 // this sets *PINDEX to the field index. If the field is not found,
2550 // Record that this is a generated struct that is not comparable. 2608 // Record that this is a generated struct that is not comparable.
2551 void 2609 void
2552 set_is_struct_incomparable() 2610 set_is_struct_incomparable()
2553 { this->is_struct_incomparable_ = true; } 2611 { this->is_struct_incomparable_ = true; }
2554 2612
2613 // Return whether this struct's backend type has padding, due to
2614 // trailing zero-sized field.
2615 bool
2616 has_padding() const
2617 { return this->has_padding_; }
2618
2619 // Record that this struct's backend type has padding.
2620 void
2621 set_has_padding()
2622 { this->has_padding_ = true; }
2623
2555 // Write the hash function for this type. 2624 // Write the hash function for this type.
2556 void 2625 void
2557 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*); 2626 write_hash_function(Gogo*, Function_type*);
2558 2627
2559 // Write the equality function for this type. 2628 // Write the equality function for this type.
2560 void 2629 void
2561 write_equal_function(Gogo*, Named_type*); 2630 write_equal_function(Gogo*, Named_type*);
2562 2631
2586 bool 2655 bool
2587 do_is_reflexive(); 2656 do_is_reflexive();
2588 2657
2589 bool 2658 bool
2590 do_needs_key_update(); 2659 do_needs_key_update();
2660
2661 bool
2662 do_hash_might_panic();
2591 2663
2592 bool 2664 bool
2593 do_in_heap(); 2665 do_in_heap();
2594 2666
2595 unsigned int 2667 unsigned int
2654 // If this struct is unnamed, a list of methods. 2726 // If this struct is unnamed, a list of methods.
2655 Methods* all_methods_; 2727 Methods* all_methods_;
2656 // True if this is a generated struct that is not considered to be 2728 // True if this is a generated struct that is not considered to be
2657 // comparable. 2729 // comparable.
2658 bool is_struct_incomparable_; 2730 bool is_struct_incomparable_;
2731 // True if this struct's backend type has padding, due to trailing
2732 // zero-sized field.
2733 bool has_padding_;
2659 }; 2734 };
2660 2735
2661 // The type of an array. 2736 // The type of an array.
2662 2737
2663 class Array_type : public Type 2738 class Array_type : public Type
2681 2756
2682 // Store the length as an int64_t into *PLEN. Return false if the 2757 // Store the length as an int64_t into *PLEN. Return false if the
2683 // length can not be determined. This will assert if called for a 2758 // length can not be determined. This will assert if called for a
2684 // slice. 2759 // slice.
2685 bool 2760 bool
2686 int_length(int64_t* plen); 2761 int_length(int64_t* plen) const;
2687 2762
2688 // Whether this type is identical with T. 2763 // Whether this type is identical with T.
2689 bool 2764 bool
2690 is_identical(const Array_type* t, int) const; 2765 is_identical(const Array_type* t, int) const;
2691 2766
2733 set_is_array_incomparable() 2808 set_is_array_incomparable()
2734 { this->is_array_incomparable_ = true; } 2809 { this->is_array_incomparable_ = true; }
2735 2810
2736 // Write the hash function for this type. 2811 // Write the hash function for this type.
2737 void 2812 void
2738 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*); 2813 write_hash_function(Gogo*, Function_type*);
2739 2814
2740 // Write the equality function for this type. 2815 // Write the equality function for this type.
2741 void 2816 void
2742 write_equal_function(Gogo*, Named_type*); 2817 write_equal_function(Gogo*, Named_type*);
2743 2818
2761 } 2836 }
2762 2837
2763 bool 2838 bool
2764 do_needs_key_update() 2839 do_needs_key_update()
2765 { return this->element_type_->needs_key_update(); } 2840 { return this->element_type_->needs_key_update(); }
2841
2842 bool
2843 do_hash_might_panic()
2844 { return this->length_ != NULL && this->element_type_->hash_might_panic(); }
2766 2845
2767 bool 2846 bool
2768 do_in_heap() 2847 do_in_heap()
2769 { return this->length_ == NULL || this->element_type_->in_heap(); } 2848 { return this->length_ == NULL || this->element_type_->in_heap(); }
2770 2849
2840 // pass as the zero value to those functions. Otherwise, in the 2919 // pass as the zero value to those functions. Otherwise, in the
2841 // normal case, returns NULL. 2920 // normal case, returns NULL.
2842 Expression* 2921 Expression*
2843 fat_zero_value(Gogo*); 2922 fat_zero_value(Gogo*);
2844 2923
2924 // Map algorithm to use for this map type. We may use specialized
2925 // fast map routines for certain key types.
2926 enum Map_alg
2927 {
2928 // 32-bit key.
2929 MAP_ALG_FAST32,
2930 // 32-bit pointer key.
2931 MAP_ALG_FAST32PTR,
2932 // 64-bit key.
2933 MAP_ALG_FAST64,
2934 // 64-bit pointer key.
2935 MAP_ALG_FAST64PTR,
2936 // String key.
2937 MAP_ALG_FASTSTR,
2938 // Anything else.
2939 MAP_ALG_SLOW,
2940 };
2941
2942 Map_alg
2943 algorithm(Gogo*);
2944
2845 // Return whether VAR is the map zero value. 2945 // Return whether VAR is the map zero value.
2846 static bool 2946 static bool
2847 is_zero_value(Variable* var); 2947 is_zero_value(Variable* var);
2848 2948
2849 // Return the backend representation of the map zero value. 2949 // Return the backend representation of the map zero value.
2859 do_import(Import*); 2959 do_import(Import*);
2860 2960
2861 static Type* 2961 static Type*
2862 make_map_type_descriptor_type(); 2962 make_map_type_descriptor_type();
2863 2963
2864 // This must be in sync with libgo/go/runtime/hashmap.go. 2964 // This must be in sync with libgo/go/runtime/map.go.
2865 static const int bucket_size = 8; 2965 static const int bucket_size = 8;
2866 2966
2867 protected: 2967 protected:
2868 int 2968 int
2869 do_traverse(Traverse*); 2969 do_traverse(Traverse*);
2902 3002
2903 void 3003 void
2904 do_export(Export*) const; 3004 do_export(Export*) const;
2905 3005
2906 private: 3006 private:
2907 // These must be in sync with libgo/go/runtime/hashmap.go. 3007 // These must be in sync with libgo/go/runtime/map.go.
2908 static const int max_key_size = 128; 3008 static const int max_key_size = 128;
2909 static const int max_val_size = 128; 3009 static const int max_val_size = 128;
2910 static const int max_zero_size = 1024; 3010 static const int max_zero_size = 1024;
2911 3011
2912 // Maps with value types larger than max_zero_size require passing a 3012 // Maps with value types larger than max_zero_size require passing a
3057 { 3157 {
3058 go_assert(this->methods_are_finalized_); 3158 go_assert(this->methods_are_finalized_);
3059 return this->all_methods_ == NULL; 3159 return this->all_methods_ == NULL;
3060 } 3160 }
3061 3161
3062 // Return the list of locally defined methos. This will return NULL 3162 // Return the list of locally defined methods. This will return NULL
3063 // for an empty interface. Embedded interfaces will appear in this 3163 // for an empty interface. Embedded interfaces will appear in this
3064 // list as an entry with no name. 3164 // list as an entry with no name.
3065 const Typed_identifier_list* 3165 const Typed_identifier_list*
3066 local_methods() const 3166 local_methods() const
3067 { return this->parse_methods_; } 3167 { return this->parse_methods_; }
3131 finish_backend_methods(Gogo*); 3231 finish_backend_methods(Gogo*);
3132 3232
3133 static Type* 3233 static Type*
3134 make_interface_type_descriptor_type(); 3234 make_interface_type_descriptor_type();
3135 3235
3236 // Return whether methods are finalized for this interface.
3237 bool
3238 methods_are_finalized() const
3239 { return this->methods_are_finalized_; }
3240
3241 // Sort embedded interfaces by name. Needed when we are preparing
3242 // to emit types into the export data.
3243 void
3244 sort_embedded()
3245 {
3246 if (parse_methods_ != NULL)
3247 parse_methods_->sort_by_name();
3248 }
3249
3136 protected: 3250 protected:
3137 int 3251 int
3138 do_traverse(Traverse*); 3252 do_traverse(Traverse*);
3139 3253
3140 bool 3254 bool
3152 3266
3153 // Distinction between +0 and -0 requires a key update if it 3267 // Distinction between +0 and -0 requires a key update if it
3154 // contains a float. 3268 // contains a float.
3155 bool 3269 bool
3156 do_needs_key_update() 3270 do_needs_key_update()
3271 { return true; }
3272
3273 // Hashing an unhashable type stored in an interface might panic.
3274 bool
3275 do_hash_might_panic()
3157 { return true; } 3276 { return true; }
3158 3277
3159 unsigned int 3278 unsigned int
3160 do_hash_for_method(Gogo*, int) const; 3279 do_hash_for_method(Gogo*, int) const;
3161 3280