lang.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. /* Java(TM) language-specific utility routines.
  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. /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
  19. #include "config.h"
  20. #include "system.h"
  21. #include "coretypes.h"
  22. #include "tm.h"
  23. #include "hash-set.h"
  24. #include "machmode.h"
  25. #include "vec.h"
  26. #include "double-int.h"
  27. #include "input.h"
  28. #include "alias.h"
  29. #include "symtab.h"
  30. #include "wide-int.h"
  31. #include "inchash.h"
  32. #include "tree.h"
  33. #include "input.h"
  34. #include "java-tree.h"
  35. #include "jcf.h"
  36. #include "langhooks.h"
  37. #include "langhooks-def.h"
  38. #include "flags.h"
  39. #include "ggc.h"
  40. #include "diagnostic.h"
  41. #include "tree-inline.h"
  42. #include "splay-tree.h"
  43. #include "tree-dump.h"
  44. #include "opts.h"
  45. #include "options.h"
  46. #include "target.h"
  47. #include "context.h"
  48. static bool java_init (void);
  49. static void java_finish (void);
  50. static unsigned int java_option_lang_mask (void);
  51. static void java_init_options_struct (struct gcc_options *);
  52. static void java_init_options (unsigned int, struct cl_decoded_option *);
  53. static bool java_post_options (const char **);
  54. static bool java_handle_option (size_t, const char *, int, int, location_t,
  55. const struct cl_option_handlers *);
  56. static void put_decl_string (const char *, int);
  57. static void put_decl_node (tree, int);
  58. static void java_print_error_function (diagnostic_context *, const char *,
  59. diagnostic_info *);
  60. static bool java_dump_tree (void *, tree);
  61. static void dump_compound_expr (dump_info_p, tree);
  62. static bool java_decl_ok_for_sibcall (const_tree);
  63. static enum classify_record java_classify_record (tree type);
  64. static tree java_eh_personality (void);
  65. #ifndef TARGET_OBJECT_SUFFIX
  66. # define TARGET_OBJECT_SUFFIX ".o"
  67. #endif
  68. /* Table of machine-independent attributes. */
  69. const struct attribute_spec java_attribute_table[] =
  70. {
  71. { "nonnull", 0, -1, false, true, true,
  72. NULL, false },
  73. { NULL, 0, 0, false, false, false, NULL, false }
  74. };
  75. /* Used to avoid printing error messages with bogus function
  76. prototypes. Starts out false. */
  77. static bool inhibit_error_function_printing;
  78. const char *resource_name;
  79. /* When nonzero, -Wall was turned on. */
  80. int flag_wall = 0;
  81. /* When nonzero, report use of deprecated classes, methods, or fields. */
  82. int flag_deprecated = 1;
  83. /* When zero, don't optimize static class initialization. This flag shouldn't
  84. be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead. */
  85. /* FIXME: Make this work with gimplify. */
  86. /* int flag_optimize_sci = 0; */
  87. /* Don't attempt to verify invocations. */
  88. int flag_verify_invocations = 0;
  89. /* When nonzero, print extra version information. */
  90. static int v_flag = 0;
  91. JCF *current_jcf;
  92. /* Variable controlling how dependency tracking is enabled in
  93. java_init. */
  94. static int dependency_tracking = 0;
  95. /* Flag values for DEPENDENCY_TRACKING. */
  96. #define DEPEND_SET_FILE 1
  97. #define DEPEND_ENABLE 2
  98. #define DEPEND_TARGET_SET 4
  99. #define DEPEND_FILE_ALREADY_SET 8
  100. struct GTY(()) language_function {
  101. int unused;
  102. };
  103. #undef LANG_HOOKS_NAME
  104. #define LANG_HOOKS_NAME "GNU Java"
  105. #undef LANG_HOOKS_INIT
  106. #define LANG_HOOKS_INIT java_init
  107. #undef LANG_HOOKS_FINISH
  108. #define LANG_HOOKS_FINISH java_finish
  109. #undef LANG_HOOKS_OPTION_LANG_MASK
  110. #define LANG_HOOKS_OPTION_LANG_MASK java_option_lang_mask
  111. #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
  112. #define LANG_HOOKS_INIT_OPTIONS_STRUCT java_init_options_struct
  113. #undef LANG_HOOKS_INIT_OPTIONS
  114. #define LANG_HOOKS_INIT_OPTIONS java_init_options
  115. #undef LANG_HOOKS_HANDLE_OPTION
  116. #define LANG_HOOKS_HANDLE_OPTION java_handle_option
  117. #undef LANG_HOOKS_POST_OPTIONS
  118. #define LANG_HOOKS_POST_OPTIONS java_post_options
  119. #undef LANG_HOOKS_PARSE_FILE
  120. #define LANG_HOOKS_PARSE_FILE java_parse_file
  121. #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
  122. #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL java_dup_lang_specific_decl
  123. #undef LANG_HOOKS_DECL_PRINTABLE_NAME
  124. #define LANG_HOOKS_DECL_PRINTABLE_NAME lang_printable_name
  125. #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
  126. #define LANG_HOOKS_PRINT_ERROR_FUNCTION java_print_error_function
  127. #undef LANG_HOOKS_WRITE_GLOBALS
  128. #define LANG_HOOKS_WRITE_GLOBALS java_write_globals
  129. #undef LANG_HOOKS_TYPE_FOR_MODE
  130. #define LANG_HOOKS_TYPE_FOR_MODE java_type_for_mode
  131. #undef LANG_HOOKS_TYPE_FOR_SIZE
  132. #define LANG_HOOKS_TYPE_FOR_SIZE java_type_for_size
  133. #undef LANG_HOOKS_CLASSIFY_RECORD
  134. #define LANG_HOOKS_CLASSIFY_RECORD java_classify_record
  135. #undef LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN
  136. #define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN java_dump_tree
  137. #undef LANG_HOOKS_GIMPLIFY_EXPR
  138. #define LANG_HOOKS_GIMPLIFY_EXPR java_gimplify_expr
  139. #undef LANG_HOOKS_DECL_OK_FOR_SIBCALL
  140. #define LANG_HOOKS_DECL_OK_FOR_SIBCALL java_decl_ok_for_sibcall
  141. #undef LANG_HOOKS_SET_DECL_ASSEMBLER_NAME
  142. #define LANG_HOOKS_SET_DECL_ASSEMBLER_NAME java_mangle_decl
  143. #undef LANG_HOOKS_ATTRIBUTE_TABLE
  144. #define LANG_HOOKS_ATTRIBUTE_TABLE java_attribute_table
  145. #undef LANG_HOOKS_EH_PERSONALITY
  146. #define LANG_HOOKS_EH_PERSONALITY java_eh_personality
  147. #undef LANG_HOOKS_EH_USE_CXA_END_CLEANUP
  148. #define LANG_HOOKS_EH_USE_CXA_END_CLEANUP true
  149. /* Each front end provides its own. */
  150. struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
  151. /*
  152. * process java-specific compiler command-line options
  153. * return false, but do not complain if the option is not recognized.
  154. */
  155. static bool
  156. java_handle_option (size_t scode, const char *arg, int value,
  157. int kind ATTRIBUTE_UNUSED, location_t loc ATTRIBUTE_UNUSED,
  158. const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
  159. {
  160. enum opt_code code = (enum opt_code) scode;
  161. switch (code)
  162. {
  163. case OPT_I:
  164. jcf_path_include_arg (arg);
  165. break;
  166. case OPT_M:
  167. jcf_dependency_init (1);
  168. dependency_tracking |= DEPEND_ENABLE;
  169. break;
  170. case OPT_MD_:
  171. jcf_dependency_init (1);
  172. dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
  173. break;
  174. case OPT_MF:
  175. jcf_dependency_set_dep_file (arg);
  176. dependency_tracking |= DEPEND_FILE_ALREADY_SET;
  177. break;
  178. case OPT_MM:
  179. jcf_dependency_init (0);
  180. dependency_tracking |= DEPEND_ENABLE;
  181. break;
  182. case OPT_MMD_:
  183. jcf_dependency_init (0);
  184. dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
  185. break;
  186. case OPT_MP:
  187. jcf_dependency_print_dummies ();
  188. break;
  189. case OPT_MT:
  190. jcf_dependency_set_target (arg);
  191. dependency_tracking |= DEPEND_TARGET_SET;
  192. break;
  193. case OPT_Wall:
  194. flag_wall = value;
  195. /* When -Wall given, enable -Wunused. We do this because the C
  196. compiler does it, and people expect it. */
  197. warn_unused = value;
  198. break;
  199. case OPT_fenable_assertions_:
  200. add_enable_assert (arg, value);
  201. break;
  202. case OPT_fenable_assertions:
  203. add_enable_assert ("", value);
  204. break;
  205. case OPT_fdisable_assertions_:
  206. add_enable_assert (arg, !value);
  207. break;
  208. case OPT_fdisable_assertions:
  209. add_enable_assert ("", !value);
  210. break;
  211. case OPT_fassume_compiled_:
  212. add_assume_compiled (arg, !value);
  213. break;
  214. case OPT_fassume_compiled:
  215. add_assume_compiled ("", !value);
  216. break;
  217. case OPT_fbootclasspath_:
  218. jcf_path_bootclasspath_arg (arg);
  219. break;
  220. case OPT_faux_classpath:
  221. case OPT_fclasspath_:
  222. jcf_path_classpath_arg (arg);
  223. break;
  224. case OPT_fcompile_resource_:
  225. resource_name = arg;
  226. break;
  227. case OPT_fdump_:
  228. if (!g->get_dumps ()->dump_switch_p (arg))
  229. return false;
  230. break;
  231. case OPT_fencoding_:
  232. /* Nothing. */
  233. break;
  234. case OPT_fextdirs_:
  235. jcf_path_extdirs_arg (arg);
  236. break;
  237. case OPT_foutput_class_dir_:
  238. /* FIXME: remove; this is handled by ecj1 now. */
  239. break;
  240. case OPT_version:
  241. v_flag = 1;
  242. break;
  243. case OPT_fsource_filename_:
  244. java_read_sourcefilenames (arg);
  245. break;
  246. default:
  247. if (cl_options[code].flags & CL_Java)
  248. break;
  249. gcc_unreachable ();
  250. }
  251. return true;
  252. }
  253. /* Global open file. */
  254. FILE *finput;
  255. static bool
  256. java_init (void)
  257. {
  258. /* FIXME: Indirect dispatch isn't yet compatible with static class
  259. init optimization. */
  260. if (flag_indirect_dispatch)
  261. always_initialize_class_p = true;
  262. if (!flag_indirect_dispatch)
  263. flag_indirect_classes = false;
  264. jcf_path_seal (v_flag);
  265. java_init_decl_processing ();
  266. using_eh_for_cleanups ();
  267. return true;
  268. }
  269. static void
  270. java_finish (void)
  271. {
  272. jcf_dependency_write ();
  273. }
  274. /* Buffer used by lang_printable_name. */
  275. static char *decl_buf = NULL;
  276. /* Allocated size of decl_buf. */
  277. static int decl_buflen = 0;
  278. /* Length of used part of decl_buf; position for next character. */
  279. static int decl_bufpos = 0;
  280. /* Append the string STR to decl_buf.
  281. It length is given by LEN; -1 means the string is nul-terminated. */
  282. static void
  283. put_decl_string (const char *str, int len)
  284. {
  285. if (len < 0)
  286. len = strlen (str);
  287. if (decl_bufpos + len >= decl_buflen)
  288. {
  289. if (decl_buf == NULL)
  290. {
  291. decl_buflen = len + 100;
  292. decl_buf = XNEWVEC (char, decl_buflen);
  293. }
  294. else
  295. {
  296. decl_buflen *= 2;
  297. decl_buf = XRESIZEVAR (char, decl_buf, decl_buflen);
  298. }
  299. }
  300. strcpy (decl_buf + decl_bufpos, str);
  301. decl_bufpos += len;
  302. }
  303. /* Append to decl_buf a printable name for NODE.
  304. Depending on VERBOSITY, more information about NODE
  305. is printed. Read the comments of decl_printable_name in
  306. langhooks.h for more. */
  307. static void
  308. put_decl_node (tree node, int verbosity)
  309. {
  310. int was_pointer = 0;
  311. if (TREE_CODE (node) == POINTER_TYPE)
  312. {
  313. node = TREE_TYPE (node);
  314. was_pointer = 1;
  315. }
  316. if (DECL_P (node) && DECL_NAME (node) != NULL_TREE)
  317. {
  318. if (TREE_CODE (node) == FUNCTION_DECL)
  319. {
  320. if (verbosity == 0 && DECL_NAME (node))
  321. /* We have been instructed to just print the bare name
  322. of the function. */
  323. {
  324. put_decl_node (DECL_NAME (node), 0);
  325. return;
  326. }
  327. /* We want to print the type the DECL belongs to. We don't do
  328. that when we handle constructors. */
  329. if (! DECL_CONSTRUCTOR_P (node)
  330. && ! DECL_ARTIFICIAL (node) && DECL_CONTEXT (node)
  331. /* We want to print qualified DECL names only
  332. if verbosity is higher than 1. */
  333. && verbosity >= 1)
  334. {
  335. put_decl_node (TREE_CODE (DECL_CONTEXT (node)) == FUNCTION_DECL
  336. ? DECL_CONTEXT (node)
  337. : TYPE_NAME (DECL_CONTEXT (node)),
  338. verbosity);
  339. put_decl_string (".", 1);
  340. }
  341. if (! DECL_CONSTRUCTOR_P (node))
  342. put_decl_node (DECL_NAME (node), verbosity);
  343. if (TREE_TYPE (node) != NULL_TREE
  344. /* We want to print function parameters only if verbosity
  345. is higher than 2. */
  346. && verbosity >= 2)
  347. {
  348. int i = 0;
  349. tree args = TYPE_ARG_TYPES (TREE_TYPE (node));
  350. if (TREE_CODE (TREE_TYPE (node)) == METHOD_TYPE)
  351. args = TREE_CHAIN (args);
  352. put_decl_string ("(", 1);
  353. for ( ; args != NULL_TREE && args != end_params_node;
  354. args = TREE_CHAIN (args), i++)
  355. {
  356. if (i > 0)
  357. put_decl_string (",", 1);
  358. put_decl_node (TREE_VALUE (args), verbosity);
  359. }
  360. put_decl_string (")", 1);
  361. }
  362. }
  363. else
  364. put_decl_node (DECL_NAME (node), verbosity);
  365. }
  366. else if (TYPE_P (node) && TYPE_NAME (node) != NULL_TREE)
  367. {
  368. if (TREE_CODE (node) == RECORD_TYPE && TYPE_ARRAY_P (node)
  369. /* Print detailed array information only if verbosity is higher
  370. than 2. */
  371. && verbosity >= 2)
  372. {
  373. put_decl_node (TYPE_ARRAY_ELEMENT (node), verbosity);
  374. put_decl_string("[]", 2);
  375. }
  376. else if (node == promoted_byte_type_node)
  377. put_decl_string ("byte", 4);
  378. else if (node == promoted_short_type_node)
  379. put_decl_string ("short", 5);
  380. else if (node == promoted_char_type_node)
  381. put_decl_string ("char", 4);
  382. else if (node == promoted_boolean_type_node)
  383. put_decl_string ("boolean", 7);
  384. else if (node == void_type_node && was_pointer)
  385. put_decl_string ("null", 4);
  386. else
  387. put_decl_node (TYPE_NAME (node), verbosity);
  388. }
  389. else if (TREE_CODE (node) == IDENTIFIER_NODE)
  390. put_decl_string (IDENTIFIER_POINTER (node), IDENTIFIER_LENGTH (node));
  391. else
  392. put_decl_string ("<unknown>", -1);
  393. }
  394. /* Return a user-friendly name for DECL.
  395. The resulting string is only valid until the next call.
  396. The value of the hook decl_printable_name is this function,
  397. which is also called directly by java_print_error_function. */
  398. const char *
  399. lang_printable_name (tree decl, int v)
  400. {
  401. decl_bufpos = 0;
  402. put_decl_node (decl, v);
  403. put_decl_string ("", 1);
  404. return decl_buf;
  405. }
  406. /* Print on stderr the current class and method context. This function
  407. is the value of the hook print_error_function. */
  408. static GTY(()) tree last_error_function_context;
  409. static GTY(()) tree last_error_function;
  410. static void
  411. java_print_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
  412. const char *file,
  413. diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
  414. {
  415. /* Don't print error messages with bogus function prototypes. */
  416. if (inhibit_error_function_printing)
  417. return;
  418. if (current_function_decl != NULL
  419. && DECL_CONTEXT (current_function_decl) != NULL
  420. && DECL_CONTEXT (current_function_decl) != last_error_function_context)
  421. {
  422. if (file)
  423. fprintf (stderr, "%s: ", file);
  424. last_error_function_context = DECL_CONTEXT (current_function_decl);
  425. fprintf (stderr, "In class '%s':\n",
  426. lang_printable_name (last_error_function_context, 0));
  427. }
  428. if (last_error_function != current_function_decl)
  429. {
  430. if (file)
  431. fprintf (stderr, "%s: ", file);
  432. if (current_function_decl == NULL)
  433. fprintf (stderr, "At top level:\n");
  434. else
  435. {
  436. const char *name = lang_printable_name (current_function_decl, 2);
  437. fprintf (stderr, "In %s '%s':\n",
  438. (DECL_CONSTRUCTOR_P (current_function_decl) ? "constructor"
  439. : "method"),
  440. name);
  441. }
  442. last_error_function = current_function_decl;
  443. }
  444. }
  445. /* Called to install the PRINT_ERROR_FUNCTION hook differently
  446. according to LEVEL. LEVEL is 1 during early parsing, when function
  447. prototypes aren't fully resolved. java_print_error_function is set
  448. so it doesn't print incomplete function prototypes. When LEVEL is
  449. 2, function prototypes are fully resolved and can be printed when
  450. reporting errors. */
  451. void
  452. lang_init_source (int level)
  453. {
  454. inhibit_error_function_printing = (level == 1);
  455. }
  456. static unsigned int
  457. java_option_lang_mask (void)
  458. {
  459. return CL_Java;
  460. }
  461. /* Initialize options structure OPTS. */
  462. static void
  463. java_init_options_struct (struct gcc_options *opts)
  464. {
  465. opts->x_flag_bounds_check = 1;
  466. opts->x_flag_exceptions = 1;
  467. opts->x_flag_non_call_exceptions = 1;
  468. /* In Java floating point operations never trap. */
  469. opts->x_flag_trapping_math = 0;
  470. opts->frontend_set_flag_trapping_math = true;
  471. /* In Java arithmetic overflow always wraps around. */
  472. opts->x_flag_wrapv = 1;
  473. /* Java requires left-to-right evaluation of subexpressions. */
  474. opts->x_flag_evaluation_order = 1;
  475. /* Java catches NULL pointer exceptions, thus we can not necessarily
  476. rely on a pointer having a non-NULL value after a dereference. */
  477. opts->x_flag_delete_null_pointer_checks = 0;
  478. }
  479. static void
  480. java_init_options (unsigned int decoded_options_count ATTRIBUTE_UNUSED,
  481. struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED)
  482. {
  483. jcf_path_init ();
  484. }
  485. /* Post-switch processing. */
  486. static bool
  487. java_post_options (const char **pfilename)
  488. {
  489. const char *filename = *pfilename;
  490. /* Excess precision other than "fast" requires front-end
  491. support. */
  492. if (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
  493. && TARGET_FLT_EVAL_METHOD_NON_DEFAULT)
  494. sorry ("-fexcess-precision=standard for Java");
  495. flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
  496. /* An absolute requirement: if we're not using indirect dispatch, we
  497. must always verify everything. */
  498. if (! flag_indirect_dispatch)
  499. flag_verify_invocations = true;
  500. if (flag_reduced_reflection)
  501. {
  502. if (flag_indirect_dispatch)
  503. error ("-findirect-dispatch is incompatible "
  504. "with -freduced-reflection");
  505. if (flag_jni)
  506. error ("-fjni is incompatible with -freduced-reflection");
  507. }
  508. /* Open input file. */
  509. if (filename == 0 || !strcmp (filename, "-"))
  510. {
  511. finput = stdin;
  512. filename = "stdin";
  513. if (dependency_tracking)
  514. error ("can%'t do dependency tracking with input from stdin");
  515. }
  516. else
  517. {
  518. if (dependency_tracking)
  519. {
  520. const char *dot;
  521. /* If the target is set and the output filename is set, then
  522. there's no processing to do here. Otherwise we must
  523. compute one or the other. */
  524. if (! ((dependency_tracking & DEPEND_TARGET_SET)
  525. && (dependency_tracking & DEPEND_FILE_ALREADY_SET)))
  526. {
  527. dot = strrchr (filename, '.');
  528. if (dot == NULL)
  529. error ("couldn%'t determine target name for dependency tracking");
  530. else
  531. {
  532. char *buf = XNEWVEC (char, dot - filename +
  533. 3 + sizeof (TARGET_OBJECT_SUFFIX));
  534. strncpy (buf, filename, dot - filename);
  535. /* If emitting class files, we might have multiple
  536. targets. The class generation code takes care of
  537. registering them. Otherwise we compute the
  538. target name here. */
  539. if ((dependency_tracking & DEPEND_TARGET_SET))
  540. ; /* Nothing. */
  541. else
  542. {
  543. strcpy (buf + (dot - filename), TARGET_OBJECT_SUFFIX);
  544. jcf_dependency_set_target (buf);
  545. }
  546. if ((dependency_tracking & DEPEND_FILE_ALREADY_SET))
  547. ; /* Nothing. */
  548. else if ((dependency_tracking & DEPEND_SET_FILE))
  549. {
  550. strcpy (buf + (dot - filename), ".d");
  551. jcf_dependency_set_dep_file (buf);
  552. }
  553. else
  554. jcf_dependency_set_dep_file ("-");
  555. free (buf);
  556. }
  557. }
  558. }
  559. }
  560. linemap_add (line_table, LC_ENTER, false, filename, 0);
  561. linemap_add (line_table, LC_RENAME, false, "<built-in>", 0);
  562. /* Initialize the compiler back end. */
  563. return false;
  564. }
  565. /* Return either DECL or its known constant value (if it has one). */
  566. tree
  567. decl_constant_value (tree decl)
  568. {
  569. if (/* Don't change a variable array bound or initial value to a constant
  570. in a place where a variable is invalid. */
  571. current_function_decl != 0
  572. && ! TREE_THIS_VOLATILE (decl)
  573. && TREE_READONLY (decl)
  574. && DECL_INITIAL (decl) != 0
  575. && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
  576. /* This is invalid if initial value is not constant.
  577. If it has either a function call, a memory reference,
  578. or a variable, then re-evaluating it could give different results. */
  579. && TREE_CONSTANT (DECL_INITIAL (decl))
  580. /* Check for cases where this is sub-optimal, even though valid. */
  581. && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
  582. return DECL_INITIAL (decl);
  583. return decl;
  584. }
  585. /* Every call to a static constructor has an associated boolean
  586. variable which is in the outermost scope of the calling method.
  587. This variable is used to avoid multiple calls to the static
  588. constructor for each class.
  589. It looks something like this:
  590. foo ()
  591. {
  592. boolean dummy = OtherClass.is_initialized;
  593. ...
  594. if (! dummy)
  595. OtherClass.initialize();
  596. ... use OtherClass.data ...
  597. }
  598. Each of these boolean variables has an entry in the
  599. DECL_FUNCTION_INIT_TEST_TABLE of a method. When inlining a method
  600. we must merge the DECL_FUNCTION_INIT_TEST_TABLE from the function
  601. being inlined and create the boolean variables in the outermost
  602. scope of the method being inlined into. */
  603. /* Create a mapping from a boolean variable in a method being inlined
  604. to one in the scope of the method being inlined into. */
  605. int
  606. merge_init_test_initialization (treetreehash_entry **entry, void *x)
  607. {
  608. struct treetreehash_entry *ite = *entry;
  609. splay_tree decl_map = (splay_tree)x;
  610. splay_tree_node n;
  611. tree *init_test_decl;
  612. /* See if we have remapped this declaration. If we haven't there's
  613. a bug in the inliner. */
  614. n = splay_tree_lookup (decl_map, (splay_tree_key) ite->value);
  615. gcc_assert (n);
  616. /* Create a new entry for the class and its remapped boolean
  617. variable. If we already have a mapping for this class we've
  618. already initialized it, so don't overwrite the value. */
  619. init_test_decl = java_treetreehash_new
  620. (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
  621. if (!*init_test_decl)
  622. *init_test_decl = (tree)n->value;
  623. /* This fixes a weird case.
  624. The front end assumes that once we have called a method that
  625. initializes some class, we can assume the class is initialized. It
  626. does this by setting the DECL_INITIAL of the init_test_decl for that
  627. class, and no initializations are emitted for that class.
  628. However, what if the method that is supposed to do the initialization
  629. is itself inlined in the caller? When expanding the called method
  630. we'll assume that the class initialization has already been done,
  631. because the DECL_INITIAL of the init_test_decl is set.
  632. To fix this we remove the DECL_INITIAL (in the caller scope) of all
  633. the init_test_decls corresponding to classes initialized by the
  634. inlined method. This makes the caller no longer assume that the
  635. method being inlined does any class initializations. */
  636. DECL_INITIAL (*init_test_decl) = NULL;
  637. return true;
  638. }
  639. /* Merge the DECL_FUNCTION_INIT_TEST_TABLE from the function we're
  640. inlining. */
  641. void
  642. java_inlining_merge_static_initializers (tree fn, void *decl_map)
  643. {
  644. DECL_FUNCTION_INIT_TEST_TABLE (fn)
  645. ->traverse<void *, merge_init_test_initialization> (decl_map);
  646. }
  647. /* Lookup a DECL_FUNCTION_INIT_TEST_TABLE entry in the method we're
  648. inlining into. If we already have a corresponding entry in that
  649. class we don't need to create another one, so we create a mapping
  650. from the variable in the inlined class to the corresponding
  651. pre-existing one. */
  652. int
  653. inline_init_test_initialization (treetreehash_entry **entry, void *x)
  654. {
  655. struct treetreehash_entry *ite = *entry;
  656. splay_tree decl_map = (splay_tree)x;
  657. tree h = java_treetreehash_find
  658. (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
  659. if (! h)
  660. return true;
  661. splay_tree_insert (decl_map,
  662. (splay_tree_key) ite->value,
  663. (splay_tree_value) h);
  664. return true;
  665. }
  666. /* Look up the boolean variables in the DECL_FUNCTION_INIT_TEST_TABLE
  667. of a method being inlined. For each hone, if we already have a
  668. variable associated with the same class in the method being inlined
  669. into, create a new mapping for it. */
  670. void
  671. java_inlining_map_static_initializers (tree fn, void *decl_map)
  672. {
  673. DECL_FUNCTION_INIT_TEST_TABLE (fn)
  674. ->traverse<void *, inline_init_test_initialization> (decl_map);
  675. }
  676. /* Avoid voluminous output for deep recursion of compound exprs. */
  677. static void
  678. dump_compound_expr (dump_info_p di, tree t)
  679. {
  680. int i;
  681. for (i=0; i<2; i++)
  682. {
  683. switch (TREE_CODE (TREE_OPERAND (t, i)))
  684. {
  685. case COMPOUND_EXPR:
  686. dump_compound_expr (di, TREE_OPERAND (t, i));
  687. break;
  688. default:
  689. dump_child ("expr", TREE_OPERAND (t, i));
  690. }
  691. }
  692. }
  693. static bool
  694. java_dump_tree (void *dump_info, tree t)
  695. {
  696. enum tree_code code;
  697. dump_info_p di = (dump_info_p) dump_info;
  698. /* Figure out what kind of node this is. */
  699. code = TREE_CODE (t);
  700. switch (code)
  701. {
  702. case FUNCTION_DECL:
  703. dump_child ("args", DECL_ARGUMENTS (t));
  704. if (DECL_EXTERNAL (t))
  705. dump_string (di, "undefined");
  706. if (TREE_PUBLIC (t))
  707. dump_string (di, "extern");
  708. else
  709. dump_string (di, "static");
  710. if (DECL_LANG_SPECIFIC (t) && !dump_flag (di, TDF_SLIM, t))
  711. dump_child ("inline body", DECL_SAVED_TREE (t));
  712. return true;
  713. case RETURN_EXPR:
  714. dump_child ("expr", TREE_OPERAND (t, 0));
  715. return true;
  716. case GOTO_EXPR:
  717. dump_child ("goto", TREE_OPERAND (t, 0));
  718. return true;
  719. case LABEL_EXPR:
  720. dump_child ("label", TREE_OPERAND (t, 0));
  721. return true;
  722. case BLOCK:
  723. if (BLOCK_EXPR_BODY (t))
  724. {
  725. tree local = BLOCK_VARS (t);
  726. while (local)
  727. {
  728. tree next = TREE_CHAIN (local);
  729. dump_child ("var", local);
  730. local = next;
  731. }
  732. {
  733. tree block = BLOCK_EXPR_BODY (t);
  734. dump_child ("body", block);
  735. block = TREE_CHAIN (block);
  736. }
  737. }
  738. return true;
  739. case COMPOUND_EXPR:
  740. if (!dump_flag (di, TDF_SLIM, t))
  741. return false;
  742. dump_compound_expr (di, t);
  743. return true;
  744. default:
  745. break;
  746. }
  747. return false;
  748. }
  749. /* Java calls can't, in general, be sibcalls because we need an
  750. accurate stack trace in order to guarantee correct operation of
  751. methods such as Class.forName(String) and
  752. SecurityManager.getClassContext(). */
  753. static bool
  754. java_decl_ok_for_sibcall (const_tree decl)
  755. {
  756. return (decl != NULL && DECL_CONTEXT (decl) == output_class
  757. && !DECL_UNINLINABLE (decl));
  758. }
  759. static enum classify_record
  760. java_classify_record (tree type)
  761. {
  762. if (! CLASS_P (type))
  763. return RECORD_IS_STRUCT;
  764. if (CLASS_INTERFACE (TYPE_NAME (type)))
  765. return RECORD_IS_INTERFACE;
  766. return RECORD_IS_CLASS;
  767. }
  768. static GTY(()) tree java_eh_personality_decl;
  769. static tree
  770. java_eh_personality (void)
  771. {
  772. if (!java_eh_personality_decl)
  773. java_eh_personality_decl = build_personality_function ("gcj");
  774. return java_eh_personality_decl;
  775. }
  776. #include "gt-java-lang.h"