typeck.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /* Handle types for the GNU compiler for the Java(TM) language.
  2. Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>.
  15. Java and all Java-based marks are trademarks or registered trademarks
  16. of Sun Microsystems, Inc. in the United States and other countries.
  17. The Free Software Foundation is independent of Sun Microsystems, Inc. */
  18. /* Written by Per Bothner <bothner@cygnus.com> */
  19. #include "config.h"
  20. #include "system.h"
  21. #include "coretypes.h"
  22. #include "hash-set.h"
  23. #include "machmode.h"
  24. #include "vec.h"
  25. #include "double-int.h"
  26. #include "input.h"
  27. #include "alias.h"
  28. #include "symtab.h"
  29. #include "options.h"
  30. #include "wide-int.h"
  31. #include "inchash.h"
  32. #include "tree.h"
  33. #include "fold-const.h"
  34. #include "stor-layout.h"
  35. #include "stringpool.h"
  36. #include "obstack.h"
  37. #include "flags.h"
  38. #include "java-tree.h"
  39. #include "jcf.h"
  40. #include "convert.h"
  41. #include "diagnostic-core.h"
  42. #include "ggc.h"
  43. static tree convert_ieee_real_to_integer (tree, tree);
  44. static tree parse_signature_type (const unsigned char **,
  45. const unsigned char *);
  46. static tree lookup_do (tree, int, tree, tree, tree (*)(tree));
  47. static tree build_null_signature (tree);
  48. tree * type_map;
  49. /* Set the type of the local variable with index SLOT to TYPE. */
  50. void
  51. set_local_type (int slot, tree type)
  52. {
  53. int max_locals = DECL_MAX_LOCALS(current_function_decl);
  54. int nslots = TYPE_IS_WIDE (type) ? 2 : 1;
  55. gcc_assert (slot >= 0 && (slot + nslots - 1 < max_locals));
  56. type_map[slot] = type;
  57. while (--nslots > 0)
  58. type_map[++slot] = void_type_node;
  59. }
  60. /* Convert an IEEE real to an integer type. The result of such a
  61. conversion when the source operand is a NaN isn't defined by
  62. IEEE754, but by the Java language standard: it must be zero. Also,
  63. overflows must be clipped to within range. This conversion
  64. produces something like:
  65. ((expr >= (float)MAX_INT)
  66. ? MAX_INT
  67. : ((expr <= (float)MIN_INT)
  68. ? MIN_INT
  69. : ((expr != expr)
  70. ? 0
  71. : (int)expr))) */
  72. static tree
  73. convert_ieee_real_to_integer (tree type, tree expr)
  74. {
  75. tree result;
  76. expr = save_expr (expr);
  77. result = fold_build3 (COND_EXPR, type,
  78. fold_build2 (NE_EXPR, boolean_type_node, expr, expr),
  79. convert (type, integer_zero_node),
  80. convert_to_integer (type, expr));
  81. result = fold_build3 (COND_EXPR, type,
  82. fold_build2 (LE_EXPR, boolean_type_node, expr,
  83. convert (TREE_TYPE (expr),
  84. TYPE_MIN_VALUE (type))),
  85. TYPE_MIN_VALUE (type),
  86. result);
  87. result = fold_build3 (COND_EXPR, type,
  88. fold_build2 (GE_EXPR, boolean_type_node, expr,
  89. convert (TREE_TYPE (expr),
  90. TYPE_MAX_VALUE (type))),
  91. TYPE_MAX_VALUE (type),
  92. result);
  93. return result;
  94. }
  95. /* Create an expression whose value is that of EXPR,
  96. converted to type TYPE. The TREE_TYPE of the value
  97. is always TYPE. This function implements all reasonable
  98. conversions; callers should filter out those that are
  99. not permitted by the language being compiled. */
  100. tree
  101. convert (tree type, tree expr)
  102. {
  103. enum tree_code code = TREE_CODE (type);
  104. if (!expr)
  105. return error_mark_node;
  106. if (type == TREE_TYPE (expr)
  107. || TREE_CODE (expr) == ERROR_MARK)
  108. return expr;
  109. if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
  110. return error_mark_node;
  111. if (code == VOID_TYPE)
  112. return build1 (CONVERT_EXPR, type, expr);
  113. if (code == BOOLEAN_TYPE)
  114. return fold_convert (type, expr);
  115. if (code == INTEGER_TYPE)
  116. {
  117. if (type == char_type_node || type == promoted_char_type_node)
  118. return fold_convert (type, expr);
  119. if ((really_constant_p (expr) || ! flag_unsafe_math_optimizations)
  120. && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
  121. return convert_ieee_real_to_integer (type, expr);
  122. else
  123. {
  124. /* fold very helpfully sets the overflow status if a type
  125. overflows in a narrowing integer conversion, but Java
  126. doesn't care. */
  127. tree tmp = fold (convert_to_integer (type, expr));
  128. if (TREE_CODE (tmp) == INTEGER_CST)
  129. TREE_OVERFLOW (tmp) = 0;
  130. return tmp;
  131. }
  132. }
  133. if (code == REAL_TYPE)
  134. return fold (convert_to_real (type, expr));
  135. if (code == POINTER_TYPE)
  136. return fold (convert_to_pointer (type, expr));
  137. error ("conversion to non-scalar type requested");
  138. return error_mark_node;
  139. }
  140. /* Return a data type that has machine mode MODE.
  141. If the mode is an integer,
  142. then UNSIGNEDP selects between signed and unsigned types. */
  143. tree
  144. java_type_for_mode (machine_mode mode, int unsignedp)
  145. {
  146. if (mode == TYPE_MODE (int_type_node))
  147. return unsignedp ? unsigned_int_type_node : int_type_node;
  148. if (mode == TYPE_MODE (long_type_node))
  149. return unsignedp ? unsigned_long_type_node : long_type_node;
  150. if (mode == TYPE_MODE (short_type_node))
  151. return unsignedp ? unsigned_short_type_node : short_type_node;
  152. if (mode == TYPE_MODE (byte_type_node))
  153. return unsignedp ? unsigned_byte_type_node : byte_type_node;
  154. if (mode == TYPE_MODE (float_type_node))
  155. return float_type_node;
  156. if (mode == TYPE_MODE (double_type_node))
  157. return double_type_node;
  158. return 0;
  159. }
  160. /* Return an integer type with BITS bits of precision,
  161. that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
  162. tree
  163. java_type_for_size (unsigned bits, int unsignedp)
  164. {
  165. if (bits <= TYPE_PRECISION (byte_type_node))
  166. return unsignedp ? unsigned_byte_type_node : byte_type_node;
  167. if (bits <= TYPE_PRECISION (short_type_node))
  168. return unsignedp ? unsigned_short_type_node : short_type_node;
  169. if (bits <= TYPE_PRECISION (int_type_node))
  170. return unsignedp ? unsigned_int_type_node : int_type_node;
  171. if (bits <= TYPE_PRECISION (long_type_node))
  172. return unsignedp ? unsigned_long_type_node : long_type_node;
  173. return 0;
  174. }
  175. /* Thorough checking of the arrayness of TYPE. */
  176. int
  177. is_array_type_p (tree type)
  178. {
  179. return TREE_CODE (type) == POINTER_TYPE
  180. && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
  181. && TYPE_ARRAY_P (TREE_TYPE (type));
  182. }
  183. /* Return the length of a Java array type.
  184. Return -1 if the length is unknown or non-constant. */
  185. HOST_WIDE_INT
  186. java_array_type_length (tree array_type)
  187. {
  188. tree arfld;
  189. if (TREE_CODE (array_type) == POINTER_TYPE)
  190. array_type = TREE_TYPE (array_type);
  191. arfld = DECL_CHAIN (DECL_CHAIN (TYPE_FIELDS (array_type)));
  192. if (arfld != NULL_TREE)
  193. {
  194. tree index_type = TYPE_DOMAIN (TREE_TYPE (arfld));
  195. if (index_type != NULL_TREE)
  196. {
  197. tree high = TYPE_MAX_VALUE (index_type);
  198. if (TREE_CODE (high) == INTEGER_CST)
  199. return TREE_INT_CST_LOW (high) + 1;
  200. }
  201. }
  202. return -1;
  203. }
  204. /* An array of unknown length will be ultimately given a length of
  205. -2, so that we can still have `length' producing a negative value
  206. even if found. This was part of an optimization aiming at removing
  207. `length' from static arrays. We could restore it, FIXME. */
  208. tree
  209. build_prim_array_type (tree element_type, HOST_WIDE_INT length)
  210. {
  211. tree index = NULL;
  212. if (length != -1)
  213. {
  214. tree max_index = build_int_cst (sizetype, length - 1);
  215. index = build_index_type (max_index);
  216. }
  217. return build_array_type (element_type, index);
  218. }
  219. /* Return a Java array type with a given ELEMENT_TYPE and LENGTH.
  220. These are hashed (shared) using IDENTIFIER_SIGNATURE_TYPE.
  221. The LENGTH is -1 if the length is unknown. */
  222. tree
  223. build_java_array_type (tree element_type, HOST_WIDE_INT length)
  224. {
  225. tree sig, t, fld, atype, arfld;
  226. char buf[23];
  227. tree elsig = build_java_signature (element_type);
  228. tree el_name = element_type;
  229. buf[0] = '[';
  230. if (length >= 0)
  231. sprintf (buf+1, HOST_WIDE_INT_PRINT_DEC, length);
  232. else
  233. buf[1] = '\0';
  234. sig = ident_subst (IDENTIFIER_POINTER (elsig), IDENTIFIER_LENGTH (elsig),
  235. buf, 0, 0, "");
  236. t = IDENTIFIER_SIGNATURE_TYPE (sig);
  237. if (t != NULL_TREE)
  238. return TREE_TYPE (t);
  239. t = make_class ();
  240. IDENTIFIER_SIGNATURE_TYPE (sig) = build_pointer_type (t);
  241. TYPE_ARRAY_P (t) = 1;
  242. if (TREE_CODE (el_name) == POINTER_TYPE)
  243. el_name = TREE_TYPE (el_name);
  244. el_name = TYPE_NAME (el_name);
  245. if (TREE_CODE (el_name) == TYPE_DECL)
  246. el_name = DECL_NAME (el_name);
  247. {
  248. char suffix[23];
  249. if (length >= 0)
  250. sprintf (suffix, "[%d]", (int)length);
  251. else
  252. strcpy (suffix, "[]");
  253. TYPE_NAME (t)
  254. = TYPE_STUB_DECL (t)
  255. = build_decl (input_location, TYPE_DECL,
  256. identifier_subst (el_name, "", '.', '.', suffix),
  257. t);
  258. TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (t)) = true;
  259. }
  260. set_java_signature (t, sig);
  261. set_super_info (0, t, object_type_node, 0);
  262. if (TREE_CODE (element_type) == RECORD_TYPE)
  263. element_type = promote_type (element_type);
  264. TYPE_ARRAY_ELEMENT (t) = element_type;
  265. /* Add length pseudo-field. */
  266. fld = build_decl (input_location,
  267. FIELD_DECL, get_identifier ("length"), int_type_node);
  268. TYPE_FIELDS (t) = fld;
  269. DECL_CONTEXT (fld) = t;
  270. FIELD_PUBLIC (fld) = 1;
  271. FIELD_FINAL (fld) = 1;
  272. TREE_READONLY (fld) = 1;
  273. atype = build_prim_array_type (element_type, length);
  274. arfld = build_decl (input_location,
  275. FIELD_DECL, get_identifier ("data"), atype);
  276. DECL_CONTEXT (arfld) = t;
  277. DECL_CHAIN (fld) = arfld;
  278. DECL_ALIGN (arfld) = TYPE_ALIGN (element_type);
  279. /* We could layout_class, but that loads java.lang.Object prematurely.
  280. * This is called by the parser, and it is a bad idea to do load_class
  281. * in the middle of parsing, because of possible circularity problems. */
  282. push_super_field (t, object_type_node);
  283. layout_type (t);
  284. return t;
  285. }
  286. /* Promote TYPE to the type actually used for fields and parameters. */
  287. tree
  288. promote_type (tree type)
  289. {
  290. switch (TREE_CODE (type))
  291. {
  292. case RECORD_TYPE:
  293. return build_pointer_type (type);
  294. case BOOLEAN_TYPE:
  295. if (type == boolean_type_node)
  296. return promoted_boolean_type_node;
  297. goto handle_int;
  298. case INTEGER_TYPE:
  299. if (type == char_type_node)
  300. return promoted_char_type_node;
  301. handle_int:
  302. if (TYPE_PRECISION (type) < TYPE_PRECISION (int_type_node))
  303. {
  304. if (type == short_type_node)
  305. return promoted_short_type_node;
  306. if (type == byte_type_node)
  307. return promoted_byte_type_node;
  308. return int_type_node;
  309. }
  310. /* ... else fall through ... */
  311. default:
  312. return type;
  313. }
  314. }
  315. /* Parse a signature string, starting at *PTR and ending at LIMIT.
  316. Return the seen TREE_TYPE, updating *PTR. */
  317. static tree
  318. parse_signature_type (const unsigned char **ptr, const unsigned char *limit)
  319. {
  320. tree type;
  321. gcc_assert (*ptr < limit);
  322. switch (**ptr)
  323. {
  324. case 'B': (*ptr)++; return byte_type_node;
  325. case 'C': (*ptr)++; return char_type_node;
  326. case 'D': (*ptr)++; return double_type_node;
  327. case 'F': (*ptr)++; return float_type_node;
  328. case 'S': (*ptr)++; return short_type_node;
  329. case 'I': (*ptr)++; return int_type_node;
  330. case 'J': (*ptr)++; return long_type_node;
  331. case 'Z': (*ptr)++; return boolean_type_node;
  332. case 'V': (*ptr)++; return void_type_node;
  333. case '[':
  334. for ((*ptr)++; (*ptr) < limit && ISDIGIT (**ptr); ) (*ptr)++;
  335. type = parse_signature_type (ptr, limit);
  336. type = build_java_array_type (type, -1);
  337. break;
  338. case 'L':
  339. {
  340. const unsigned char *start = ++(*ptr);
  341. const unsigned char *str = start;
  342. for ( ; ; str++)
  343. {
  344. gcc_assert (str < limit);
  345. if (*str == ';')
  346. break;
  347. }
  348. *ptr = str+1;
  349. type = lookup_class (unmangle_classname ((const char *) start, str - start));
  350. break;
  351. }
  352. default:
  353. gcc_unreachable ();
  354. }
  355. return promote_type (type);
  356. }
  357. /* Parse a Java "mangled" signature string, starting at SIG_STRING,
  358. and SIG_LENGTH bytes long.
  359. Return a gcc type node. */
  360. tree
  361. parse_signature_string (const unsigned char *sig_string, int sig_length)
  362. {
  363. tree result_type;
  364. const unsigned char *str = sig_string;
  365. const unsigned char *limit = str + sig_length;
  366. if (str < limit && str[0] == '(')
  367. {
  368. tree argtype_list = NULL_TREE;
  369. str++;
  370. while (str < limit && str[0] != ')')
  371. {
  372. tree argtype = parse_signature_type (&str, limit);
  373. argtype_list = tree_cons (NULL_TREE, argtype, argtype_list);
  374. }
  375. if (str++, str >= limit)
  376. abort ();
  377. result_type = parse_signature_type (&str, limit);
  378. argtype_list = chainon (nreverse (argtype_list), end_params_node);
  379. result_type = build_function_type (result_type, argtype_list);
  380. }
  381. else
  382. result_type = parse_signature_type (&str, limit);
  383. if (str != limit)
  384. error ("junk at end of signature string");
  385. return result_type;
  386. }
  387. /* Convert a signature to its type.
  388. * Uses IDENTIFIER_SIGNATURE_TYPE as a cache (except for primitive types).
  389. */
  390. tree
  391. get_type_from_signature (tree signature)
  392. {
  393. const unsigned char *sig = (const unsigned char *) IDENTIFIER_POINTER (signature);
  394. int len = IDENTIFIER_LENGTH (signature);
  395. tree type;
  396. /* Primitive types aren't cached. */
  397. if (len <= 1)
  398. return parse_signature_string (sig, len);
  399. type = IDENTIFIER_SIGNATURE_TYPE (signature);
  400. if (type == NULL_TREE)
  401. {
  402. type = parse_signature_string (sig, len);
  403. IDENTIFIER_SIGNATURE_TYPE (signature) = type;
  404. }
  405. return type;
  406. }
  407. /* Ignore signature and always return null. Used by has_method. */
  408. static tree
  409. build_null_signature (tree type ATTRIBUTE_UNUSED)
  410. {
  411. return NULL_TREE;
  412. }
  413. /* Return the signature string for the arguments of method type TYPE. */
  414. tree
  415. build_java_argument_signature (tree type)
  416. {
  417. extern struct obstack temporary_obstack;
  418. tree sig = TYPE_ARGUMENT_SIGNATURE (type);
  419. if (sig == NULL_TREE)
  420. {
  421. tree args = TYPE_ARG_TYPES (type);
  422. if (TREE_CODE (type) == METHOD_TYPE)
  423. args = TREE_CHAIN (args); /* Skip "this" argument. */
  424. for (; args != end_params_node; args = TREE_CHAIN (args))
  425. {
  426. tree t = build_java_signature (TREE_VALUE (args));
  427. obstack_grow (&temporary_obstack,
  428. IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
  429. }
  430. obstack_1grow (&temporary_obstack, '\0');
  431. sig = get_identifier ((char *) obstack_base (&temporary_obstack));
  432. TYPE_ARGUMENT_SIGNATURE (type) = sig;
  433. obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
  434. }
  435. return sig;
  436. }
  437. /* Return the signature of the given TYPE. */
  438. tree
  439. build_java_signature (tree type)
  440. {
  441. tree sig, t;
  442. while (TREE_CODE (type) == POINTER_TYPE)
  443. type = TREE_TYPE (type);
  444. MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
  445. sig = TYPE_SIGNATURE (type);
  446. if (sig == NULL_TREE)
  447. {
  448. char sg[2];
  449. switch (TREE_CODE (type))
  450. {
  451. case BOOLEAN_TYPE: sg[0] = 'Z'; goto native;
  452. case VOID_TYPE: sg[0] = 'V'; goto native;
  453. case INTEGER_TYPE:
  454. if (type == char_type_node || type == promoted_char_type_node)
  455. {
  456. sg[0] = 'C';
  457. goto native;
  458. }
  459. switch (TYPE_PRECISION (type))
  460. {
  461. case 8: sg[0] = 'B'; goto native;
  462. case 16: sg[0] = 'S'; goto native;
  463. case 32: sg[0] = 'I'; goto native;
  464. case 64: sg[0] = 'J'; goto native;
  465. default: goto bad_type;
  466. }
  467. case REAL_TYPE:
  468. switch (TYPE_PRECISION (type))
  469. {
  470. case 32: sg[0] = 'F'; goto native;
  471. case 64: sg[0] = 'D'; goto native;
  472. default: goto bad_type;
  473. }
  474. native:
  475. sg[1] = 0;
  476. sig = get_identifier (sg);
  477. break;
  478. case RECORD_TYPE:
  479. if (TYPE_ARRAY_P (type))
  480. {
  481. t = build_java_signature (TYPE_ARRAY_ELEMENT (type));
  482. sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
  483. "[", 0, 0, "");
  484. }
  485. else
  486. {
  487. t = DECL_NAME (TYPE_NAME (type));
  488. sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
  489. "L", '.', '/', ";");
  490. }
  491. break;
  492. case METHOD_TYPE:
  493. case FUNCTION_TYPE:
  494. {
  495. extern struct obstack temporary_obstack;
  496. sig = build_java_argument_signature (type);
  497. obstack_1grow (&temporary_obstack, '(');
  498. obstack_grow (&temporary_obstack,
  499. IDENTIFIER_POINTER (sig), IDENTIFIER_LENGTH (sig));
  500. obstack_1grow (&temporary_obstack, ')');
  501. t = build_java_signature (TREE_TYPE (type));
  502. obstack_grow0 (&temporary_obstack,
  503. IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
  504. sig = get_identifier ((char *) obstack_base (&temporary_obstack));
  505. obstack_free (&temporary_obstack,
  506. obstack_base (&temporary_obstack));
  507. }
  508. break;
  509. bad_type:
  510. default:
  511. gcc_unreachable ();
  512. }
  513. TYPE_SIGNATURE (type) = sig;
  514. }
  515. return sig;
  516. }
  517. /* Save signature string SIG (an IDENTIFIER_NODE) in TYPE for future use. */
  518. void
  519. set_java_signature (tree type, tree sig)
  520. {
  521. tree old_sig;
  522. while (TREE_CODE (type) == POINTER_TYPE)
  523. type = TREE_TYPE (type);
  524. MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
  525. old_sig = TYPE_SIGNATURE (type);
  526. if (old_sig != NULL_TREE && old_sig != sig)
  527. abort ();
  528. TYPE_SIGNATURE (type) = sig;
  529. #if 0 /* careful about METHOD_TYPE */
  530. if (IDENTIFIER_SIGNATURE_TYPE (sig) == NULL_TREE && TREE_PERMANENT (type))
  531. IDENTIFIER_SIGNATURE_TYPE (sig) = type;
  532. #endif
  533. }
  534. /* Search in SEARCHED_CLASS and its superclasses for a method matching
  535. METHOD_NAME and signature METHOD_SIGNATURE. This function will
  536. only search for methods declared in the class hierarchy; interfaces
  537. will not be considered. Returns NULL_TREE if the method is not
  538. found. */
  539. tree
  540. lookup_argument_method (tree searched_class, tree method_name,
  541. tree method_signature)
  542. {
  543. return lookup_do (searched_class, 0,
  544. method_name, method_signature,
  545. build_java_argument_signature);
  546. }
  547. /* Like lookup_argument_method, but lets the caller set any flags
  548. desired. */
  549. tree
  550. lookup_argument_method_generic (tree searched_class, tree method_name,
  551. tree method_signature, int flags)
  552. {
  553. return lookup_do (searched_class, flags,
  554. method_name, method_signature,
  555. build_java_argument_signature);
  556. }
  557. /* Search in class SEARCHED_CLASS (and its superclasses) for a method
  558. matching METHOD_NAME and signature METHOD_SIGNATURE. Return a
  559. FUNCTION_DECL on success, or NULL_TREE if none found. (Contrast
  560. lookup_argument_method, which ignores return type.) If
  561. SEARCHED_CLASS is an interface, search it too. */
  562. tree
  563. lookup_java_method (tree searched_class, tree method_name,
  564. tree method_signature)
  565. {
  566. return lookup_do (searched_class, SEARCH_INTERFACE, method_name,
  567. method_signature, build_java_signature);
  568. }
  569. /* Return true iff KLASS (or its ancestors) has a method METHOD_NAME.  */
  570. int
  571. has_method (tree klass, tree method_name)
  572. {
  573. return lookup_do (klass, SEARCH_INTERFACE,
  574. method_name, NULL_TREE,
  575. build_null_signature) != NULL_TREE;
  576. }
  577. /* Search in class SEARCHED_CLASS, but not its superclasses, for a
  578. method matching METHOD_NAME and signature SIGNATURE. A private
  579. helper for lookup_do. */
  580. static tree
  581. shallow_find_method (tree searched_class, int flags, tree method_name,
  582. tree signature, tree (*signature_builder) (tree))
  583. {
  584. tree method;
  585. for (method = TYPE_METHODS (searched_class);
  586. method != NULL_TREE; method = DECL_CHAIN (method))
  587. {
  588. tree method_sig = (*signature_builder) (TREE_TYPE (method));
  589. if (DECL_NAME (method) == method_name && method_sig == signature)
  590. {
  591. /* If the caller requires a visible method, then we
  592. skip invisible methods here. */
  593. if (! (flags & SEARCH_VISIBLE)
  594. || ! METHOD_INVISIBLE (method))
  595. return method;
  596. }
  597. }
  598. return NULL_TREE;
  599. }
  600. /* Search in the superclasses of SEARCHED_CLASS for a method matching
  601. METHOD_NAME and signature SIGNATURE. A private helper for
  602. lookup_do. */
  603. static tree
  604. find_method_in_superclasses (tree searched_class, int flags,
  605. tree method_name, tree signature,
  606. tree (*signature_builder) (tree))
  607. {
  608. tree klass;
  609. for (klass = CLASSTYPE_SUPER (searched_class); klass != NULL_TREE;
  610. klass = CLASSTYPE_SUPER (klass))
  611. {
  612. tree method;
  613. method = shallow_find_method (klass, flags, method_name,
  614. signature, signature_builder);
  615. if (method != NULL_TREE)
  616. return method;
  617. }
  618. return NULL_TREE;
  619. }
  620. /* Search in the interfaces of SEARCHED_CLASS and its superinterfaces
  621. for a method matching METHOD_NAME and signature SIGNATURE. A
  622. private helper for lookup_do. */
  623. static tree
  624. find_method_in_interfaces (tree searched_class, int flags, tree method_name,
  625. tree signature, tree (*signature_builder) (tree))
  626. {
  627. int i;
  628. tree binfo, base_binfo;
  629. for (binfo = TYPE_BINFO (searched_class), i = 1;
  630. BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  631. {
  632. tree iclass = BINFO_TYPE (base_binfo);
  633. tree method;
  634. /* If the superinterface hasn't been loaded yet, do so now. */
  635. if (!CLASS_LOADED_P (iclass))
  636. load_class (iclass, 1);
  637. /* First, we look in ICLASS. If that doesn't work we'll
  638. recursively look through all its superinterfaces. */
  639. method = shallow_find_method (iclass, flags, method_name,
  640. signature, signature_builder);
  641. if (method != NULL_TREE)
  642. return method;
  643. method = find_method_in_interfaces
  644. (iclass, flags, method_name, signature, signature_builder);
  645. if (method != NULL_TREE)
  646. return method;
  647. }
  648. return NULL_TREE;
  649. }
  650. /* Search in class SEARCHED_CLASS (and its superclasses) for a method
  651. matching METHOD_NAME and signature SIGNATURE. FLAGS control some
  652. parameters of the search.
  653. SEARCH_INTERFACE means also search interfaces and superinterfaces
  654. of SEARCHED_CLASS.
  655. SEARCH_SUPER means skip SEARCHED_CLASS and start with its
  656. superclass.
  657. SEARCH_VISIBLE means skip methods for which METHOD_INVISIBLE is
  658. set.
  659. Return the matched method DECL or NULL_TREE. SIGNATURE_BUILDER is
  660. used on method candidates to build their (sometimes partial)
  661. signature. */
  662. static tree
  663. lookup_do (tree searched_class, int flags, tree method_name,
  664. tree signature, tree (*signature_builder) (tree))
  665. {
  666. tree method;
  667. tree orig_class = searched_class;
  668. if (searched_class == NULL_TREE)
  669. return NULL_TREE;
  670. if (flags & SEARCH_SUPER)
  671. {
  672. searched_class = CLASSTYPE_SUPER (searched_class);
  673. if (searched_class == NULL_TREE)
  674. return NULL_TREE;
  675. }
  676. /* First look in our own methods. */
  677. method = shallow_find_method (searched_class, flags, method_name,
  678. signature, signature_builder);
  679. if (method)
  680. return method;
  681. /* Then look in our superclasses. */
  682. if (! CLASS_INTERFACE (TYPE_NAME (searched_class)))
  683. method = find_method_in_superclasses (searched_class, flags, method_name,
  684. signature, signature_builder);
  685. if (method)
  686. return method;
  687. /* If that doesn't work, look in our interfaces. */
  688. if (flags & SEARCH_INTERFACE)
  689. method = find_method_in_interfaces (orig_class, flags, method_name,
  690. signature, signature_builder);
  691. return method;
  692. }
  693. /* Search in class CLAS for a constructor matching METHOD_SIGNATURE.
  694. Return a FUNCTION_DECL on success, or NULL_TREE if none found. */
  695. tree
  696. lookup_java_constructor (tree clas, tree method_signature)
  697. {
  698. tree method = TYPE_METHODS (clas);
  699. for ( ; method != NULL_TREE; method = DECL_CHAIN (method))
  700. {
  701. tree method_sig = build_java_signature (TREE_TYPE (method));
  702. if (DECL_CONSTRUCTOR_P (method) && method_sig == method_signature)
  703. return method;
  704. }
  705. return NULL_TREE;
  706. }
  707. /* Return a type which is the Binary Numeric Promotion of the pair T1,
  708. T2 and convert EXP1 and/or EXP2. See 5.6.2 Binary Numeric
  709. Promotion. It assumes that both T1 and T2 are eligible to BNP. */
  710. tree
  711. binary_numeric_promotion (tree t1, tree t2, tree *exp1, tree *exp2)
  712. {
  713. if (t1 == double_type_node || t2 == double_type_node)
  714. {
  715. if (t1 != double_type_node)
  716. *exp1 = convert (double_type_node, *exp1);
  717. if (t2 != double_type_node)
  718. *exp2 = convert (double_type_node, *exp2);
  719. return double_type_node;
  720. }
  721. if (t1 == float_type_node || t2 == float_type_node)
  722. {
  723. if (t1 != float_type_node)
  724. *exp1 = convert (float_type_node, *exp1);
  725. if (t2 != float_type_node)
  726. *exp2 = convert (float_type_node, *exp2);
  727. return float_type_node;
  728. }
  729. if (t1 == long_type_node || t2 == long_type_node)
  730. {
  731. if (t1 != long_type_node)
  732. *exp1 = convert (long_type_node, *exp1);
  733. if (t2 != long_type_node)
  734. *exp2 = convert (long_type_node, *exp2);
  735. return long_type_node;
  736. }
  737. if (t1 != int_type_node)
  738. *exp1 = convert (int_type_node, *exp1);
  739. if (t2 != int_type_node)
  740. *exp2 = convert (int_type_node, *exp2);
  741. return int_type_node;
  742. }