jcf-parse.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232
  1. /* Parser for Java(TM) .class files.
  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 "real.h"
  31. #include "wide-int.h"
  32. #include "inchash.h"
  33. #include "tree.h"
  34. #include "stringpool.h"
  35. #include "obstack.h"
  36. #include "flags.h"
  37. #include "java-except.h"
  38. #include "input.h"
  39. #include "javaop.h"
  40. #include "java-tree.h"
  41. #include "diagnostic-core.h"
  42. #include "parse.h"
  43. #include "ggc.h"
  44. #include "debug.h"
  45. #include "hash-map.h"
  46. #include "is-a.h"
  47. #include "plugin-api.h"
  48. #include "tm.h"
  49. #include "hard-reg-set.h"
  50. #include "function.h"
  51. #include "ipa-ref.h"
  52. #include "cgraph.h"
  53. #include "bitmap.h"
  54. #include "target.h"
  55. #include "wide-int.h"
  56. #ifdef HAVE_LOCALE_H
  57. #include <locale.h>
  58. #endif
  59. #ifdef HAVE_LANGINFO_CODESET
  60. #include <langinfo.h>
  61. #endif
  62. /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
  63. #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
  64. #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
  65. #define JPOOL_UTF_DATA(JCF, INDEX) \
  66. ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
  67. #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
  68. do { \
  69. unsigned char save; unsigned char *text; \
  70. JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
  71. text = (JCF)->read_ptr; \
  72. save = text[LENGTH]; \
  73. text[LENGTH] = 0; \
  74. (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
  75. text[LENGTH] = save; \
  76. JCF_SKIP (JCF, LENGTH); } while (0)
  77. #include "jcf.h"
  78. extern struct obstack temporary_obstack;
  79. static GTY(()) tree parse_roots[2];
  80. /* The FIELD_DECL for the current field. */
  81. #define current_field parse_roots[0]
  82. /* The METHOD_DECL for the current method. */
  83. #define current_method parse_roots[1]
  84. /* Line 0 in current file, if compiling from bytecode. */
  85. static location_t file_start_location;
  86. /* The Java archive that provides main_class; the main input file. */
  87. static GTY(()) struct JCF * main_jcf;
  88. /* A list of all the class DECLs seen so far. */
  89. static GTY(()) vec<tree, va_gc> *all_class_list;
  90. /* The number of source files passed to us by -fsource-filename and an
  91. array of pointers to each name. Used by find_sourcefile(). */
  92. static int num_files = 0;
  93. static char **filenames;
  94. static struct ZipFile *localToFile;
  95. /* A map of byte offsets in the reflection data that are fields which
  96. need renumbering. */
  97. bitmap field_offsets;
  98. bitmap_obstack bit_obstack;
  99. /* Declarations of some functions used here. */
  100. static void handle_innerclass_attribute (int count, JCF *, int len);
  101. static tree give_name_to_class (JCF *jcf, int index);
  102. static char *compute_class_name (struct ZipDirectory *zdir);
  103. static int classify_zip_file (struct ZipDirectory *zdir);
  104. static void parse_zip_file_entries (void);
  105. static void process_zip_dir (FILE *);
  106. static void parse_class_file (void);
  107. static void handle_deprecated (void);
  108. static void set_source_filename (JCF *, int);
  109. static void jcf_parse (struct JCF*);
  110. static void load_inner_classes (tree);
  111. static void handle_annotation (JCF *jcf, int level);
  112. static void java_layout_seen_class_methods (void);
  113. /* Handle "Deprecated" attribute. */
  114. static void
  115. handle_deprecated (void)
  116. {
  117. if (current_field != NULL_TREE)
  118. FIELD_DEPRECATED (current_field) = 1;
  119. else if (current_method != NULL_TREE)
  120. METHOD_DEPRECATED (current_method) = 1;
  121. else if (current_class != NULL_TREE)
  122. CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
  123. else
  124. {
  125. /* Shouldn't happen. */
  126. gcc_unreachable ();
  127. }
  128. }
  129. /* Reverse a string. */
  130. static char *
  131. reverse (const char *s)
  132. {
  133. if (s == NULL)
  134. return NULL;
  135. else
  136. {
  137. int len = strlen (s);
  138. char *d = XNEWVAR (char, len + 1);
  139. const char *sp;
  140. char *dp;
  141. d[len] = 0;
  142. for (dp = &d[0], sp = &s[len-1]; sp >= s; dp++, sp--)
  143. *dp = *sp;
  144. return d;
  145. }
  146. }
  147. /* Compare two strings for qsort(). */
  148. static int
  149. cmpstringp (const void *p1, const void *p2)
  150. {
  151. /* The arguments to this function are "pointers to
  152. pointers to char", but strcmp() arguments are "pointers
  153. to char", hence the following cast plus dereference */
  154. return strcmp(*(const char *const*) p1, *(const char *const*) p2);
  155. }
  156. /* Create an array of strings, one for each source file that we've
  157. seen. fsource_filename can either be the name of a single .java
  158. file or a file that contains a list of filenames separated by
  159. newlines. */
  160. void
  161. java_read_sourcefilenames (const char *fsource_filename)
  162. {
  163. if (fsource_filename
  164. && filenames == 0
  165. && strlen (fsource_filename) > strlen (".java")
  166. && filename_cmp ((fsource_filename
  167. + strlen (fsource_filename)
  168. - strlen (".java")),
  169. ".java") != 0)
  170. {
  171. /* fsource_filename isn't a .java file but a list of filenames
  172. separated by newlines */
  173. FILE *finput = fopen (fsource_filename, "r");
  174. int len = 0;
  175. int longest_line = 0;
  176. gcc_assert (finput);
  177. /* Find out how many files there are, and how long the filenames are. */
  178. while (! feof (finput))
  179. {
  180. int ch = getc (finput);
  181. if (ch == '\n')
  182. {
  183. num_files++;
  184. if (len > longest_line)
  185. longest_line = len;
  186. len = 0;
  187. continue;
  188. }
  189. if (ch == EOF)
  190. break;
  191. len++;
  192. }
  193. rewind (finput);
  194. /* Read the filenames. Put a pointer to each filename into the
  195. array FILENAMES. */
  196. {
  197. char *linebuf = (char *) alloca (longest_line + 1);
  198. int i = 0;
  199. int charpos;
  200. filenames = XNEWVEC (char *, num_files);
  201. charpos = 0;
  202. for (;;)
  203. {
  204. int ch = getc (finput);
  205. if (ch == EOF)
  206. break;
  207. if (ch == '\n')
  208. {
  209. linebuf[charpos] = 0;
  210. gcc_assert (i < num_files);
  211. /* ??? Perhaps we should use lrealpath() here. Doing
  212. so would tidy up things like /../ but the rest of
  213. gcc seems to assume relative pathnames, not
  214. absolute pathnames. */
  215. /* realname = lrealpath (linebuf); */
  216. filenames[i++] = reverse (linebuf);
  217. charpos = 0;
  218. continue;
  219. }
  220. gcc_assert (charpos < longest_line);
  221. linebuf[charpos++] = ch;
  222. }
  223. if (num_files > 1)
  224. qsort (filenames, num_files, sizeof (char *), cmpstringp);
  225. }
  226. fclose (finput);
  227. }
  228. else
  229. {
  230. filenames = XNEWVEC (char *, 1);
  231. filenames[0] = reverse (fsource_filename);
  232. num_files = 1;
  233. }
  234. }
  235. /* Given a relative pathname such as foo/bar.java, attempt to find a
  236. longer pathname with the same suffix.
  237. This is a best guess heuristic; with some weird class hierarchies we
  238. may fail to pick the correct source file. For example, if we have
  239. the filenames foo/bar.java and also foo/foo/bar.java, we do not
  240. have enough information to know which one is the right match for
  241. foo/bar.java. */
  242. static const char *
  243. find_sourcefile (const char *name)
  244. {
  245. int i = 0, j = num_files-1;
  246. char *found = NULL;
  247. if (filenames)
  248. {
  249. char *revname = reverse (name);
  250. do
  251. {
  252. int k = (i+j) / 2;
  253. int cmp = strncmp (revname, filenames[k], strlen (revname));
  254. if (cmp == 0)
  255. {
  256. /* OK, so we found one. But is it a unique match? */
  257. if ((k > i
  258. && strncmp (revname, filenames[k-1], strlen (revname)) == 0)
  259. || (k < j
  260. && (strncmp (revname, filenames[k+1], strlen (revname))
  261. == 0)))
  262. ;
  263. else
  264. found = filenames[k];
  265. break;
  266. }
  267. if (cmp > 0)
  268. i = k+1;
  269. else
  270. j = k-1;
  271. }
  272. while (i <= j);
  273. free (revname);
  274. }
  275. if (found && strlen (found) > strlen (name))
  276. return reverse (found);
  277. else
  278. return name;
  279. }
  280. /* Handle "SourceFile" attribute. */
  281. static void
  282. set_source_filename (JCF *jcf, int index)
  283. {
  284. tree sfname_id = get_name_constant (jcf, index);
  285. const char *sfname = IDENTIFIER_POINTER (sfname_id);
  286. const char *old_filename = LOCATION_FILE (input_location);
  287. int new_len = IDENTIFIER_LENGTH (sfname_id);
  288. if (old_filename != NULL)
  289. {
  290. int old_len = strlen (old_filename);
  291. /* Use the filename from current input_location (derived from the
  292. class name) if it has a directory prefix, but otherwise matches
  293. sfname. */
  294. if (old_len > new_len
  295. && filename_cmp (sfname, old_filename + old_len - new_len) == 0
  296. && (old_filename[old_len - new_len - 1] == '/'
  297. || old_filename[old_len - new_len - 1] == '\\'))
  298. return;
  299. }
  300. if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
  301. {
  302. const char *class_name
  303. = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
  304. const char *dot = strrchr (class_name, '.');
  305. if (dot != NULL)
  306. {
  307. /* Length of prefix, not counting final dot. */
  308. int i = dot - class_name;
  309. /* Concatenate current package prefix with new sfname. */
  310. char *buf = XNEWVEC (char, i + new_len + 2); /* Space for '.' and '\0'. */
  311. strcpy (buf + i + 1, sfname);
  312. /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
  313. Note we start at the end with the final package dot. */
  314. for (; i >= 0; i--)
  315. {
  316. char c = class_name[i];
  317. if (c == '.')
  318. c = DIR_SEPARATOR;
  319. buf[i] = c;
  320. }
  321. sfname_id = get_identifier (buf);
  322. free (buf);
  323. sfname = IDENTIFIER_POINTER (sfname_id);
  324. }
  325. }
  326. sfname = find_sourcefile (sfname);
  327. ORDINARY_MAP_FILE_NAME (LINEMAPS_LAST_ORDINARY_MAP (line_table)) = sfname;
  328. if (current_class == main_class) main_input_filename = sfname;
  329. }
  330. /* Annotation handling.
  331. The technique we use here is to copy the annotation data directly
  332. from the input class file into the output file. We don't decode the
  333. data at all, merely rewriting constant indexes whenever we come
  334. across them: this is necessary because the constant pool in the
  335. output file isn't the same as the constant pool in the input.
  336. The main advantage of this technique is that the resulting
  337. annotation data is pointer-free, so it doesn't have to be relocated
  338. at startup time. As a consequence of this, annotations have no
  339. performance impact unless they are used. Also, this representation
  340. is very dense. */
  341. /* Expand TYPE_REFLECTION_DATA by DELTA bytes. Return the address of
  342. the start of the newly allocated region. */
  343. static unsigned char*
  344. annotation_grow (int delta)
  345. {
  346. unsigned char **data = &TYPE_REFLECTION_DATA (current_class);
  347. long *datasize = &TYPE_REFLECTION_DATASIZE (current_class);
  348. long len = *datasize;
  349. if (*data == NULL)
  350. {
  351. *data = XNEWVAR (unsigned char, delta);
  352. }
  353. else
  354. {
  355. int newlen = *datasize + delta;
  356. if (floor_log2 (newlen) != floor_log2 (*datasize))
  357. *data = XRESIZEVAR (unsigned char, *data, 2 << (floor_log2 (newlen)));
  358. }
  359. *datasize += delta;
  360. return *data + len;
  361. }
  362. /* annotation_rewrite_TYPE. Rewrite various int types at p. Use Java
  363. byte order (i.e. big endian.) */
  364. static void
  365. annotation_rewrite_byte (unsigned int n, unsigned char *p)
  366. {
  367. p[0] = n;
  368. }
  369. static void
  370. annotation_rewrite_short (unsigned int n, unsigned char *p)
  371. {
  372. p[0] = n>>8;
  373. p[1] = n;
  374. }
  375. static void
  376. annotation_rewrite_int (unsigned int n, unsigned char *p)
  377. {
  378. p[0] = n>>24;
  379. p[1] = n>>16;
  380. p[2] = n>>8;
  381. p[3] = n;
  382. }
  383. /* Read a 16-bit unsigned int in Java byte order (i.e. big
  384. endian.) */
  385. static uint16
  386. annotation_read_short (unsigned char *p)
  387. {
  388. uint16 tmp = p[0];
  389. tmp = (tmp << 8) | p[1];
  390. return tmp;
  391. }
  392. /* annotation_write_TYPE. Rewrite various int types, appending them
  393. to TYPE_REFLECTION_DATA. Use Java byte order (i.e. big
  394. endian.) */
  395. static void
  396. annotation_write_byte (unsigned int n)
  397. {
  398. annotation_rewrite_byte (n, annotation_grow (1));
  399. }
  400. static void
  401. annotation_write_short (unsigned int n)
  402. {
  403. annotation_rewrite_short (n, annotation_grow (2));
  404. }
  405. static void
  406. annotation_write_int (unsigned int n)
  407. {
  408. annotation_rewrite_int (n, annotation_grow (4));
  409. }
  410. /* Create a 64-bit constant in the constant pool.
  411. This is used for both integer and floating-point types. As a
  412. consequence, it will not work if the target floating-point format
  413. is anything other than IEEE-754. While this is arguably a bug, the
  414. runtime library makes exactly the same assumption and it's unlikely
  415. that Java will ever run on a non-IEEE machine. */
  416. static int
  417. handle_long_constant (JCF *jcf, CPool *cpool, enum cpool_tag kind,
  418. int index, bool big_endian)
  419. {
  420. /* If we're on a 64-bit platform we can fit a long or double
  421. into the same space as a jword. */
  422. if (POINTER_SIZE >= 64)
  423. index = find_constant1 (cpool, kind, JPOOL_LONG (jcf, index));
  424. /* In a compiled program the constant pool is in native word
  425. order. How weird is that??? */
  426. else if (big_endian)
  427. index = find_constant2 (cpool, kind,
  428. JPOOL_INT (jcf, index),
  429. JPOOL_INT (jcf, index+1));
  430. else
  431. index = find_constant2 (cpool, kind,
  432. JPOOL_INT (jcf, index+1),
  433. JPOOL_INT (jcf, index));
  434. return index;
  435. }
  436. /* Given a class file and an index into its constant pool, create an
  437. entry in the outgoing constant pool for the same item. */
  438. static uint16
  439. handle_constant (JCF *jcf, int index, enum cpool_tag purpose)
  440. {
  441. unsigned int kind;
  442. CPool *cpool = cpool_for_class (output_class);
  443. if (index == 0)
  444. return 0;
  445. if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index))
  446. error ("<constant pool index %d not in range>", index);
  447. kind = JPOOL_TAG (jcf, index);
  448. if ((kind & ~CONSTANT_ResolvedFlag) != purpose)
  449. {
  450. if (purpose == CONSTANT_Class
  451. && kind == CONSTANT_Utf8)
  452. ;
  453. else
  454. error ("<constant pool index %d unexpected type", index);
  455. }
  456. switch (kind)
  457. {
  458. case CONSTANT_Class:
  459. case CONSTANT_ResolvedClass:
  460. {
  461. /* For some reason I know not the what of, class names in
  462. annotations are UTF-8 strings in the constant pool but
  463. class names in EnclosingMethod attributes are real class
  464. references. Set CONSTANT_LazyFlag here so that the VM
  465. doesn't attempt to resolve them at class initialization
  466. time. */
  467. tree resolved_class, class_name;
  468. resolved_class = get_class_constant (jcf, index);
  469. class_name = build_internal_class_name (resolved_class);
  470. index = alloc_name_constant (CONSTANT_Class | CONSTANT_LazyFlag,
  471. (unmangle_classname
  472. (IDENTIFIER_POINTER(class_name),
  473. IDENTIFIER_LENGTH(class_name))));
  474. break;
  475. }
  476. case CONSTANT_Utf8:
  477. {
  478. tree utf8 = get_constant (jcf, index);
  479. if (purpose == CONSTANT_Class)
  480. /* Create a constant pool entry for a type signature. This
  481. one has '.' rather than '/' because it isn't going into a
  482. class file, it's going into a compiled object.
  483. This has to match the logic in
  484. _Jv_ClassReader::prepare_pool_entry(). */
  485. utf8 = unmangle_classname (IDENTIFIER_POINTER(utf8),
  486. IDENTIFIER_LENGTH(utf8));
  487. index = alloc_name_constant (kind, utf8);
  488. }
  489. break;
  490. case CONSTANT_Long:
  491. index = handle_long_constant (jcf, cpool, CONSTANT_Long, index,
  492. targetm.words_big_endian ());
  493. break;
  494. case CONSTANT_Double:
  495. index = handle_long_constant (jcf, cpool, CONSTANT_Double, index,
  496. targetm.float_words_big_endian ());
  497. break;
  498. case CONSTANT_Float:
  499. case CONSTANT_Integer:
  500. index = find_constant1 (cpool, kind, JPOOL_INT (jcf, index));
  501. break;
  502. case CONSTANT_NameAndType:
  503. {
  504. uint16 name = JPOOL_USHORT1 (jcf, index);
  505. uint16 sig = JPOOL_USHORT2 (jcf, index);
  506. uint32 name_index = handle_constant (jcf, name, CONSTANT_Utf8);
  507. uint32 sig_index = handle_constant (jcf, sig, CONSTANT_Class);
  508. jword new_index = (name_index << 16) | sig_index;
  509. index = find_constant1 (cpool, kind, new_index);
  510. }
  511. break;
  512. default:
  513. abort ();
  514. }
  515. return index;
  516. }
  517. /* Read an element_value structure from an annotation in JCF. Return
  518. the constant pool index for the resulting constant pool entry. */
  519. static int
  520. handle_element_value (JCF *jcf, int level)
  521. {
  522. uint8 tag = JCF_readu (jcf);
  523. int index = 0;
  524. annotation_write_byte (tag);
  525. switch (tag)
  526. {
  527. case 'B':
  528. case 'C':
  529. case 'S':
  530. case 'Z':
  531. case 'I':
  532. {
  533. uint16 cindex = JCF_readu2 (jcf);
  534. index = handle_constant (jcf, cindex,
  535. CONSTANT_Integer);
  536. annotation_write_short (index);
  537. }
  538. break;
  539. case 'D':
  540. {
  541. uint16 cindex = JCF_readu2 (jcf);
  542. index = handle_constant (jcf, cindex,
  543. CONSTANT_Double);
  544. annotation_write_short (index);
  545. }
  546. break;
  547. case 'F':
  548. {
  549. uint16 cindex = JCF_readu2 (jcf);
  550. index = handle_constant (jcf, cindex,
  551. CONSTANT_Float);
  552. annotation_write_short (index);
  553. }
  554. break;
  555. case 'J':
  556. {
  557. uint16 cindex = JCF_readu2 (jcf);
  558. index = handle_constant (jcf, cindex,
  559. CONSTANT_Long);
  560. annotation_write_short (index);
  561. }
  562. break;
  563. case 's':
  564. {
  565. uint16 cindex = JCF_readu2 (jcf);
  566. /* Despite what the JVM spec says, compilers generate a Utf8
  567. constant here, not a String. */
  568. index = handle_constant (jcf, cindex,
  569. CONSTANT_Utf8);
  570. annotation_write_short (index);
  571. }
  572. break;
  573. case 'e':
  574. {
  575. uint16 type_name_index = JCF_readu2 (jcf);
  576. uint16 const_name_index = JCF_readu2 (jcf);
  577. index = handle_constant (jcf, type_name_index,
  578. CONSTANT_Class);
  579. annotation_write_short (index);
  580. index = handle_constant (jcf, const_name_index,
  581. CONSTANT_Utf8);
  582. annotation_write_short (index);
  583. }
  584. break;
  585. case 'c':
  586. {
  587. uint16 class_info_index = JCF_readu2 (jcf);
  588. index = handle_constant (jcf, class_info_index,
  589. CONSTANT_Class);
  590. annotation_write_short (index);
  591. }
  592. break;
  593. case '@':
  594. {
  595. handle_annotation (jcf, level + 1);
  596. }
  597. break;
  598. case '[':
  599. {
  600. uint16 n_array_elts = JCF_readu2 (jcf);
  601. annotation_write_short (n_array_elts);
  602. while (n_array_elts--)
  603. handle_element_value (jcf, level + 1);
  604. }
  605. break;
  606. default:
  607. abort();
  608. break;
  609. }
  610. return index;
  611. }
  612. /* Read an annotation structure from JCF. Write it to the
  613. reflection_data field of the outgoing class. */
  614. static void
  615. handle_annotation (JCF *jcf, int level)
  616. {
  617. uint16 type_index = JCF_readu2 (jcf);
  618. uint16 npairs = JCF_readu2 (jcf);
  619. int index = handle_constant (jcf, type_index,
  620. CONSTANT_Class);
  621. annotation_write_short (index);
  622. annotation_write_short (npairs);
  623. while (npairs--)
  624. {
  625. uint16 name_index = JCF_readu2 (jcf);
  626. index = handle_constant (jcf, name_index,
  627. CONSTANT_Utf8);
  628. annotation_write_short (index);
  629. handle_element_value (jcf, level + 2);
  630. }
  631. }
  632. /* Read an annotation count from JCF, and write the following
  633. annotations to the reflection_data field of the outgoing class. */
  634. static void
  635. handle_annotations (JCF *jcf, int level)
  636. {
  637. uint16 num = JCF_readu2 (jcf);
  638. annotation_write_short (num);
  639. while (num--)
  640. handle_annotation (jcf, level);
  641. }
  642. /* As handle_annotations(), but perform a sanity check that we write
  643. the same number of bytes that we were expecting. */
  644. static void
  645. handle_annotation_attribute (int ATTRIBUTE_UNUSED index, JCF *jcf,
  646. long length)
  647. {
  648. long old_datasize = TYPE_REFLECTION_DATASIZE (current_class);
  649. handle_annotations (jcf, 0);
  650. gcc_assert (old_datasize + length
  651. == TYPE_REFLECTION_DATASIZE (current_class));
  652. }
  653. /* gcj permutes its fields array after generating annotation_data, so
  654. we have to fixup field indexes for fields that have moved. Given
  655. ARG, a VEC_int, fixup the field indexes in the reflection_data of
  656. the outgoing class. We use field_offsets to tell us where the
  657. fixups must go. */
  658. void
  659. rewrite_reflection_indexes (void *arg)
  660. {
  661. bitmap_iterator bi;
  662. unsigned int offset;
  663. vec<int> *map = (vec<int> *) arg;
  664. unsigned char *data = TYPE_REFLECTION_DATA (current_class);
  665. if (map)
  666. {
  667. EXECUTE_IF_SET_IN_BITMAP (field_offsets, 0, offset, bi)
  668. {
  669. uint16 index = annotation_read_short (data + offset);
  670. annotation_rewrite_short
  671. ((*map)[index], data + offset);
  672. }
  673. }
  674. }
  675. /* Read the RuntimeVisibleAnnotations from JCF and write them to the
  676. reflection_data of the outgoing class. */
  677. static void
  678. handle_member_annotations (int member_index, JCF *jcf,
  679. const unsigned char *name ATTRIBUTE_UNUSED,
  680. long len, jv_attr_type member_type)
  681. {
  682. int new_len = len + 1;
  683. annotation_write_byte (member_type);
  684. if (member_type != JV_CLASS_ATTR)
  685. new_len += 2;
  686. annotation_write_int (new_len);
  687. annotation_write_byte (JV_ANNOTATIONS_KIND);
  688. if (member_type == JV_FIELD_ATTR)
  689. bitmap_set_bit (field_offsets, TYPE_REFLECTION_DATASIZE (current_class));
  690. if (member_type != JV_CLASS_ATTR)
  691. annotation_write_short (member_index);
  692. handle_annotation_attribute (member_index, jcf, len);
  693. }
  694. /* Read the RuntimeVisibleParameterAnnotations from JCF and write them
  695. to the reflection_data of the outgoing class. */
  696. static void
  697. handle_parameter_annotations (int member_index, JCF *jcf,
  698. const unsigned char *name ATTRIBUTE_UNUSED,
  699. long len, jv_attr_type member_type)
  700. {
  701. int new_len = len + 1;
  702. uint8 num;
  703. annotation_write_byte (member_type);
  704. if (member_type != JV_CLASS_ATTR)
  705. new_len += 2;
  706. annotation_write_int (new_len);
  707. annotation_write_byte (JV_PARAMETER_ANNOTATIONS_KIND);
  708. if (member_type != JV_CLASS_ATTR)
  709. annotation_write_short (member_index);
  710. num = JCF_readu (jcf);
  711. annotation_write_byte (num);
  712. while (num--)
  713. handle_annotations (jcf, 0);
  714. }
  715. /* Read the AnnotationDefault data from JCF and write them to the
  716. reflection_data of the outgoing class. */
  717. static void
  718. handle_default_annotation (int member_index, JCF *jcf,
  719. const unsigned char *name ATTRIBUTE_UNUSED,
  720. long len, jv_attr_type member_type)
  721. {
  722. int new_len = len + 1;
  723. annotation_write_byte (member_type);
  724. if (member_type != JV_CLASS_ATTR)
  725. new_len += 2;
  726. annotation_write_int (new_len);
  727. annotation_write_byte (JV_ANNOTATION_DEFAULT_KIND);
  728. if (member_type != JV_CLASS_ATTR)
  729. annotation_write_short (member_index);
  730. handle_element_value (jcf, 0);
  731. }
  732. /* As above, for the EnclosingMethod attribute. */
  733. static void
  734. handle_enclosingmethod_attribute (int member_index, JCF *jcf,
  735. const unsigned char *name ATTRIBUTE_UNUSED,
  736. long len, jv_attr_type member_type)
  737. {
  738. int new_len = len + 1;
  739. uint16 index;
  740. annotation_write_byte (member_type);
  741. if (member_type != JV_CLASS_ATTR)
  742. new_len += 2;
  743. annotation_write_int (new_len);
  744. annotation_write_byte (JV_ENCLOSING_METHOD_KIND);
  745. if (member_type != JV_CLASS_ATTR)
  746. annotation_write_short (member_index);
  747. index = JCF_readu2 (jcf);
  748. index = handle_constant (jcf, index, CONSTANT_Class);
  749. annotation_write_short (index);
  750. index = JCF_readu2 (jcf);
  751. index = handle_constant (jcf, index, CONSTANT_NameAndType);
  752. annotation_write_short (index);
  753. }
  754. /* As above, for the Signature attribute. */
  755. static void
  756. handle_signature_attribute (int member_index, JCF *jcf,
  757. const unsigned char *name ATTRIBUTE_UNUSED,
  758. long len, jv_attr_type member_type)
  759. {
  760. int new_len = len + 1;
  761. uint16 index;
  762. annotation_write_byte (member_type);
  763. if (member_type != JV_CLASS_ATTR)
  764. new_len += 2;
  765. annotation_write_int (new_len);
  766. annotation_write_byte (JV_SIGNATURE_KIND);
  767. if (member_type != JV_CLASS_ATTR)
  768. annotation_write_short (member_index);
  769. index = JCF_readu2 (jcf);
  770. index = handle_constant (jcf, index, CONSTANT_Utf8);
  771. annotation_write_short (index);
  772. }
  773. #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
  774. #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
  775. { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
  776. output_class = current_class = give_name_to_class (jcf, THIS); \
  777. set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
  778. #define HANDLE_CLASS_INTERFACE(INDEX) \
  779. add_interface (current_class, get_class_constant (jcf, INDEX))
  780. #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
  781. { int sig_index = SIGNATURE; \
  782. current_field = add_field (current_class, get_name_constant (jcf, NAME), \
  783. parse_signature (jcf, sig_index), ACCESS_FLAGS); \
  784. set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
  785. if ((ACCESS_FLAGS) & ACC_FINAL) \
  786. MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
  787. }
  788. #define HANDLE_END_FIELDS() \
  789. (current_field = NULL_TREE)
  790. #define HANDLE_CONSTANTVALUE(INDEX) \
  791. { tree constant; int index = INDEX; \
  792. if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
  793. tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
  794. constant = build_utf8_ref (name); \
  795. } \
  796. else \
  797. constant = get_constant (jcf, index); \
  798. set_constant_value (current_field, constant); }
  799. #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
  800. (current_method = add_method (current_class, ACCESS_FLAGS, \
  801. get_name_constant (jcf, NAME), \
  802. get_name_constant (jcf, SIGNATURE)), \
  803. DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
  804. DECL_LINENUMBERS_OFFSET (current_method) = 0)
  805. #define HANDLE_END_METHODS() \
  806. { current_method = NULL_TREE; }
  807. #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
  808. { DECL_MAX_STACK (current_method) = (MAX_STACK); \
  809. DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
  810. DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
  811. DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
  812. #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
  813. { int n = (COUNT); \
  814. DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
  815. JCF_SKIP (jcf, n * 10); }
  816. #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
  817. { int n = (COUNT); \
  818. DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
  819. JCF_SKIP (jcf, n * 4); }
  820. #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
  821. { \
  822. int n = COUNT; \
  823. vec<tree, va_gc> *v; \
  824. vec_alloc (v, n); \
  825. gcc_assert (!DECL_FUNCTION_THROWS (current_method)); \
  826. while (--n >= 0) \
  827. { \
  828. tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
  829. v->quick_push (thrown_class); \
  830. } \
  831. DECL_FUNCTION_THROWS (current_method) = v; \
  832. }
  833. #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
  834. /* Link seen inner classes to their outer context and register the
  835. inner class to its outer context. They will be later loaded. */
  836. #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
  837. handle_innerclass_attribute (COUNT, jcf, attribute_length)
  838. #define HANDLE_SYNTHETIC_ATTRIBUTE() \
  839. { \
  840. /* Irrelevant decls should have been nullified by the END macros. \
  841. DECL_ARTIFICIAL on fields is used for something else (See \
  842. PUSH_FIELD in java-tree.h) */ \
  843. if (current_method) \
  844. DECL_ARTIFICIAL (current_method) = 1; \
  845. else if (current_field) \
  846. FIELD_SYNTHETIC (current_field) = 1; \
  847. else \
  848. TYPE_SYNTHETIC (current_class) = 1; \
  849. }
  850. #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
  851. { \
  852. if (current_class == object_type_node) \
  853. jcf->right_zip = 1; \
  854. }
  855. #define HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE() \
  856. { \
  857. handle_member_annotations (index, jcf, name_data, attribute_length, attr_type); \
  858. }
  859. #define HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE() \
  860. { \
  861. JCF_SKIP(jcf, attribute_length); \
  862. }
  863. #define HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
  864. { \
  865. handle_parameter_annotations (index, jcf, name_data, attribute_length, attr_type); \
  866. }
  867. #define HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
  868. { \
  869. JCF_SKIP(jcf, attribute_length); \
  870. }
  871. #define HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE() \
  872. { \
  873. handle_default_annotation (index, jcf, name_data, attribute_length, attr_type); \
  874. }
  875. #define HANDLE_ENCLOSINGMETHOD_ATTRIBUTE() \
  876. { \
  877. handle_enclosingmethod_attribute (index, jcf, name_data, \
  878. attribute_length, attr_type); \
  879. }
  880. #define HANDLE_SIGNATURE_ATTRIBUTE() \
  881. { \
  882. handle_signature_attribute (index, jcf, name_data, \
  883. attribute_length, attr_type); \
  884. }
  885. #include "jcf-reader.c"
  886. tree
  887. parse_signature (JCF *jcf, int sig_index)
  888. {
  889. gcc_assert (sig_index > 0
  890. && sig_index < JPOOL_SIZE (jcf)
  891. && JPOOL_TAG (jcf, sig_index) == CONSTANT_Utf8);
  892. return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
  893. JPOOL_UTF_LENGTH (jcf, sig_index));
  894. }
  895. tree
  896. get_constant (JCF *jcf, int index)
  897. {
  898. tree value;
  899. int tag;
  900. if (index <= 0 || index >= JPOOL_SIZE(jcf))
  901. goto bad;
  902. tag = JPOOL_TAG (jcf, index);
  903. if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
  904. return jcf->cpool.data[index].t;
  905. switch (tag)
  906. {
  907. case CONSTANT_Integer:
  908. {
  909. jint num = JPOOL_INT(jcf, index);
  910. value = build_int_cst (int_type_node, num);
  911. break;
  912. }
  913. case CONSTANT_Long:
  914. {
  915. unsigned HOST_WIDE_INT num;
  916. num = JPOOL_UINT (jcf, index);
  917. wide_int val = wi::lshift (wide_int::from (num, 64, SIGNED), 32);
  918. num = JPOOL_UINT (jcf, index + 1);
  919. val |= num;
  920. value = wide_int_to_tree (long_type_node, val);
  921. break;
  922. }
  923. case CONSTANT_Float:
  924. {
  925. jint num = JPOOL_INT(jcf, index);
  926. long buf = num;
  927. REAL_VALUE_TYPE d;
  928. real_from_target_fmt (&d, &buf, &ieee_single_format);
  929. value = build_real (float_type_node, d);
  930. break;
  931. }
  932. case CONSTANT_Double:
  933. {
  934. long buf[2], lo, hi;
  935. REAL_VALUE_TYPE d;
  936. hi = JPOOL_UINT (jcf, index);
  937. lo = JPOOL_UINT (jcf, index+1);
  938. if (targetm.float_words_big_endian ())
  939. buf[0] = hi, buf[1] = lo;
  940. else
  941. buf[0] = lo, buf[1] = hi;
  942. real_from_target_fmt (&d, buf, &ieee_double_format);
  943. value = build_real (double_type_node, d);
  944. break;
  945. }
  946. case CONSTANT_String:
  947. {
  948. tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
  949. const char *utf8_ptr = IDENTIFIER_POINTER (name);
  950. int utf8_len = IDENTIFIER_LENGTH (name);
  951. const unsigned char *utf8;
  952. int i;
  953. /* Check for a malformed Utf8 string. */
  954. utf8 = (const unsigned char *) utf8_ptr;
  955. i = utf8_len;
  956. while (i > 0)
  957. {
  958. int char_len = UT8_CHAR_LENGTH (*utf8);
  959. if (char_len < 0 || char_len > 3 || char_len > i)
  960. fatal_error (input_location, "bad string constant");
  961. utf8 += char_len;
  962. i -= char_len;
  963. }
  964. /* Allocate a new string value. */
  965. value = build_string (utf8_len, utf8_ptr);
  966. TREE_TYPE (value) = build_pointer_type (string_type_node);
  967. }
  968. break;
  969. default:
  970. goto bad;
  971. }
  972. JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
  973. jcf->cpool.data[index].t = value;
  974. return value;
  975. bad:
  976. fatal_error (input_location, "bad value constant type %d, index %d",
  977. JPOOL_TAG (jcf, index), index);
  978. }
  979. tree
  980. get_name_constant (JCF *jcf, int index)
  981. {
  982. tree name = get_constant (jcf, index);
  983. gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
  984. return name;
  985. }
  986. /* Handle reading innerclass attributes. If a nonzero entry (denoting
  987. a non anonymous entry) is found, We augment the inner class list of
  988. the outer context with the newly resolved innerclass. */
  989. static void
  990. handle_innerclass_attribute (int count, JCF *jcf, int attribute_length)
  991. {
  992. int c = count;
  993. annotation_write_byte (JV_CLASS_ATTR);
  994. annotation_write_int (attribute_length+1);
  995. annotation_write_byte (JV_INNER_CLASSES_KIND);
  996. annotation_write_short (count);
  997. while (c--)
  998. {
  999. /* Read inner_class_info_index. This may be 0 */
  1000. int icii = JCF_readu2 (jcf);
  1001. /* Read outer_class_info_index. If the innerclasses attribute
  1002. entry isn't a member (like an inner class) the value is 0. */
  1003. int ocii = JCF_readu2 (jcf);
  1004. /* Read inner_name_index. If the class we're dealing with is
  1005. an anonymous class, it must be 0. */
  1006. int ini = JCF_readu2 (jcf);
  1007. /* Read the access flag. */
  1008. int acc = JCF_readu2 (jcf);
  1009. annotation_write_short (handle_constant (jcf, icii, CONSTANT_Class));
  1010. annotation_write_short (handle_constant (jcf, ocii, CONSTANT_Class));
  1011. annotation_write_short (handle_constant (jcf, ini, CONSTANT_Utf8));
  1012. annotation_write_short (acc);
  1013. /* If icii is 0, don't try to read the class. */
  1014. if (icii >= 0)
  1015. {
  1016. tree klass = get_class_constant (jcf, icii);
  1017. tree decl = TYPE_NAME (klass);
  1018. /* Skip reading further if ocii is null */
  1019. if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
  1020. {
  1021. tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
  1022. tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
  1023. set_class_decl_access_flags (acc, decl);
  1024. DECL_CONTEXT (decl) = outer;
  1025. DECL_INNER_CLASS_LIST (outer) =
  1026. tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
  1027. CLASS_COMPLETE_P (decl) = 1;
  1028. }
  1029. }
  1030. }
  1031. }
  1032. static tree
  1033. give_name_to_class (JCF *jcf, int i)
  1034. {
  1035. gcc_assert (i > 0
  1036. && i < JPOOL_SIZE (jcf)
  1037. && JPOOL_TAG (jcf, i) == CONSTANT_Class);
  1038. {
  1039. tree package_name = NULL_TREE, tmp;
  1040. tree this_class;
  1041. int j = JPOOL_USHORT1 (jcf, i);
  1042. /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
  1043. tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
  1044. JPOOL_UTF_LENGTH (jcf, j));
  1045. this_class = lookup_class (class_name);
  1046. {
  1047. tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
  1048. const char *sfname = find_sourcefile (IDENTIFIER_POINTER (source_name));
  1049. linemap_add (line_table, LC_ENTER, false, sfname, 0);
  1050. input_location = linemap_line_start (line_table, 0, 1);
  1051. file_start_location = input_location;
  1052. DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
  1053. if (main_input_filename == NULL && jcf == main_jcf)
  1054. main_input_filename = sfname;
  1055. }
  1056. jcf->cpool.data[i].t = this_class;
  1057. JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
  1058. split_qualified_name (&package_name, &tmp,
  1059. DECL_NAME (TYPE_NAME (this_class)));
  1060. TYPE_PACKAGE (this_class) = package_name;
  1061. return this_class;
  1062. }
  1063. }
  1064. /* Get the class of the CONSTANT_Class whose constant pool index is I. */
  1065. tree
  1066. get_class_constant (JCF *jcf, int i)
  1067. {
  1068. tree type;
  1069. gcc_assert (i > 0
  1070. && i < JPOOL_SIZE (jcf)
  1071. && (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) == CONSTANT_Class);
  1072. if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
  1073. {
  1074. int name_index = JPOOL_USHORT1 (jcf, i);
  1075. /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
  1076. const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
  1077. int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
  1078. if (name[0] == '[') /* Handle array "classes". */
  1079. type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
  1080. else
  1081. {
  1082. tree cname = unmangle_classname (name, nlength);
  1083. type = lookup_class (cname);
  1084. }
  1085. jcf->cpool.data[i].t = type;
  1086. JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
  1087. }
  1088. else
  1089. type = jcf->cpool.data[i].t;
  1090. return type;
  1091. }
  1092. /* Read a class with the fully qualified-name NAME.
  1093. Return 1 iff we read the requested file.
  1094. (It is still possible we failed if the file did not
  1095. define the class it is supposed to.) */
  1096. int
  1097. read_class (tree name)
  1098. {
  1099. JCF this_jcf, *jcf;
  1100. tree icv, klass = NULL_TREE;
  1101. tree save_current_class = current_class;
  1102. tree save_output_class = output_class;
  1103. location_t save_location = input_location;
  1104. JCF *save_current_jcf = current_jcf;
  1105. if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
  1106. {
  1107. klass = TREE_TYPE (icv);
  1108. jcf = TYPE_JCF (klass);
  1109. }
  1110. else
  1111. jcf = NULL;
  1112. if (jcf == NULL)
  1113. {
  1114. const char* path_name;
  1115. this_jcf.zipd = NULL;
  1116. jcf = &this_jcf;
  1117. path_name = find_class (IDENTIFIER_POINTER (name),
  1118. IDENTIFIER_LENGTH (name),
  1119. &this_jcf);
  1120. if (path_name == 0)
  1121. return 0;
  1122. else
  1123. free(CONST_CAST (char *, path_name));
  1124. }
  1125. current_jcf = jcf;
  1126. if (klass == NULL_TREE || ! CLASS_PARSED_P (klass))
  1127. {
  1128. output_class = current_class = klass;
  1129. if (JCF_SEEN_IN_ZIP (current_jcf))
  1130. read_zip_member(current_jcf,
  1131. current_jcf->zipd, current_jcf->zipd->zipf);
  1132. jcf_parse (current_jcf);
  1133. /* Parsing might change the class, in which case we have to
  1134. put it back where we found it. */
  1135. if (current_class != klass && icv != NULL_TREE)
  1136. TREE_TYPE (icv) = current_class;
  1137. klass = current_class;
  1138. }
  1139. layout_class (klass);
  1140. load_inner_classes (klass);
  1141. output_class = save_output_class;
  1142. current_class = save_current_class;
  1143. input_location = save_location;
  1144. current_jcf = save_current_jcf;
  1145. return 1;
  1146. }
  1147. /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
  1148. called from the parser, otherwise it's a RECORD_TYPE node. If
  1149. VERBOSE is 1, print error message on failure to load a class. */
  1150. void
  1151. load_class (tree class_or_name, int verbose)
  1152. {
  1153. tree name, saved;
  1154. int class_loaded = 0;
  1155. tree class_decl = NULL_TREE;
  1156. bool is_compiled_class = false;
  1157. /* We've already failed, don't try again. */
  1158. if (TREE_CODE (class_or_name) == RECORD_TYPE
  1159. && TYPE_DUMMY (class_or_name))
  1160. return;
  1161. /* class_or_name can be the name of the class we want to load */
  1162. if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
  1163. name = class_or_name;
  1164. /* In some cases, it's a dependency that we process earlier that
  1165. we though */
  1166. else if (TREE_CODE (class_or_name) == TREE_LIST)
  1167. name = TYPE_NAME (TREE_PURPOSE (class_or_name));
  1168. /* Or it's a type in the making */
  1169. else
  1170. name = DECL_NAME (TYPE_NAME (class_or_name));
  1171. class_decl = IDENTIFIER_CLASS_VALUE (name);
  1172. if (class_decl != NULL_TREE)
  1173. {
  1174. tree type = TREE_TYPE (class_decl);
  1175. is_compiled_class
  1176. = ((TYPE_JCF (type) && JCF_SEEN_IN_ZIP (TYPE_JCF (type)))
  1177. || CLASS_FROM_CURRENTLY_COMPILED_P (type));
  1178. }
  1179. saved = name;
  1180. /* If flag_verify_invocations is unset, we don't try to load a class
  1181. unless we're looking for Object (which is fixed by the ABI) or
  1182. it's a class that we're going to compile. */
  1183. if (flag_verify_invocations
  1184. || class_or_name == object_type_node
  1185. || is_compiled_class
  1186. || TREE_CODE (class_or_name) == IDENTIFIER_NODE)
  1187. {
  1188. while (1)
  1189. {
  1190. const char *separator;
  1191. /* We've already loaded it. */
  1192. if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE)
  1193. {
  1194. tree tmp_decl = IDENTIFIER_CLASS_VALUE (name);
  1195. if (CLASS_PARSED_P (TREE_TYPE (tmp_decl)))
  1196. break;
  1197. }
  1198. if (read_class (name))
  1199. break;
  1200. /* We failed loading name. Now consider that we might be looking
  1201. for an inner class. */
  1202. if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
  1203. || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
  1204. name = get_identifier_with_length (IDENTIFIER_POINTER (name),
  1205. (separator
  1206. - IDENTIFIER_POINTER (name)));
  1207. /* Otherwise, we failed, we bail. */
  1208. else
  1209. break;
  1210. }
  1211. {
  1212. /* have we found the class we're looking for? */
  1213. tree type_decl = IDENTIFIER_CLASS_VALUE (saved);
  1214. tree type = type_decl ? TREE_TYPE (type_decl) : NULL;
  1215. class_loaded = type && CLASS_PARSED_P (type);
  1216. }
  1217. }
  1218. if (!class_loaded)
  1219. {
  1220. if (flag_verify_invocations || ! flag_indirect_dispatch)
  1221. {
  1222. if (verbose)
  1223. error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
  1224. }
  1225. else if (verbose)
  1226. {
  1227. /* This is just a diagnostic during testing, not a real problem. */
  1228. if (!quiet_flag)
  1229. warning (0, "cannot find file for class %s",
  1230. IDENTIFIER_POINTER (saved));
  1231. /* Fake it. */
  1232. if (TREE_CODE (class_or_name) == RECORD_TYPE)
  1233. {
  1234. set_super_info (0, class_or_name, object_type_node, 0);
  1235. TYPE_DUMMY (class_or_name) = 1;
  1236. /* We won't be able to output any debug info for this class. */
  1237. DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;
  1238. }
  1239. }
  1240. }
  1241. }
  1242. /* Parse the .class file JCF. */
  1243. static void
  1244. jcf_parse (JCF* jcf)
  1245. {
  1246. int i, code;
  1247. bitmap_clear (field_offsets);
  1248. if (jcf_parse_preamble (jcf) != 0)
  1249. fatal_error (input_location, "not a valid Java .class file");
  1250. code = jcf_parse_constant_pool (jcf);
  1251. if (code != 0)
  1252. fatal_error (input_location, "error while parsing constant pool");
  1253. code = verify_constant_pool (jcf);
  1254. if (code > 0)
  1255. fatal_error (input_location, "error in constant pool entry #%d\n", code);
  1256. jcf_parse_class (jcf);
  1257. if (main_class == NULL_TREE)
  1258. main_class = current_class;
  1259. if (! quiet_flag && TYPE_NAME (current_class))
  1260. fprintf (stderr, " %s %s",
  1261. (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
  1262. IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
  1263. if (CLASS_PARSED_P (current_class))
  1264. {
  1265. /* FIXME - where was first time */
  1266. fatal_error (input_location,
  1267. "reading class %s for the second time from %s",
  1268. IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
  1269. jcf->filename);
  1270. }
  1271. CLASS_PARSED_P (current_class) = 1;
  1272. for (i = 1; i < JPOOL_SIZE(jcf); i++)
  1273. {
  1274. switch (JPOOL_TAG (jcf, i))
  1275. {
  1276. case CONSTANT_Class:
  1277. get_class_constant (jcf, i);
  1278. break;
  1279. }
  1280. }
  1281. code = jcf_parse_fields (jcf);
  1282. if (code != 0)
  1283. fatal_error (input_location, "error while parsing fields");
  1284. code = jcf_parse_methods (jcf);
  1285. if (code != 0)
  1286. fatal_error (input_location, "error while parsing methods");
  1287. code = jcf_parse_final_attributes (jcf);
  1288. if (code != 0)
  1289. fatal_error (input_location, "error while parsing final attributes");
  1290. if (TYPE_REFLECTION_DATA (current_class))
  1291. annotation_write_byte (JV_DONE_ATTR);
  1292. linemap_add (line_table, LC_LEAVE, false, NULL, 0);
  1293. /* The fields of class_type_node are already in correct order. */
  1294. if (current_class != class_type_node && current_class != object_type_node)
  1295. TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
  1296. if (current_class == object_type_node)
  1297. layout_class_methods (object_type_node);
  1298. else
  1299. vec_safe_push (all_class_list, TYPE_NAME (current_class));
  1300. }
  1301. /* If we came across inner classes, load them now. */
  1302. static void
  1303. load_inner_classes (tree cur_class)
  1304. {
  1305. tree current;
  1306. for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
  1307. current = TREE_CHAIN (current))
  1308. {
  1309. tree name = DECL_NAME (TREE_PURPOSE (current));
  1310. tree decl = IDENTIFIER_GLOBAL_VALUE (name);
  1311. if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
  1312. && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
  1313. load_class (name, 1);
  1314. }
  1315. }
  1316. static void
  1317. duplicate_class_warning (const char *filename)
  1318. {
  1319. location_t warn_loc;
  1320. linemap_add (line_table, LC_RENAME, 0, filename, 0);
  1321. warn_loc = linemap_line_start (line_table, 0, 1);
  1322. warning_at (warn_loc, 0, "duplicate class will only be compiled once");
  1323. }
  1324. static void
  1325. java_layout_seen_class_methods (void)
  1326. {
  1327. unsigned start = 0;
  1328. unsigned end = vec_safe_length (all_class_list);
  1329. while (1)
  1330. {
  1331. unsigned ix;
  1332. unsigned new_length;
  1333. for (ix = start; ix != end; ix++)
  1334. {
  1335. tree decl = (*all_class_list)[ix];
  1336. tree cls = TREE_TYPE (decl);
  1337. input_location = DECL_SOURCE_LOCATION (decl);
  1338. if (! CLASS_LOADED_P (cls))
  1339. load_class (cls, 0);
  1340. layout_class_methods (cls);
  1341. }
  1342. /* Note that new classes might have been added while laying out
  1343. methods, changing the value of all_class_list. */
  1344. new_length = vec_safe_length (all_class_list);
  1345. if (end != new_length)
  1346. {
  1347. start = end;
  1348. end = new_length;
  1349. }
  1350. else
  1351. break;
  1352. }
  1353. }
  1354. static void
  1355. parse_class_file (void)
  1356. {
  1357. tree method;
  1358. location_t save_location = input_location;
  1359. java_layout_seen_class_methods ();
  1360. input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
  1361. {
  1362. /* Re-enter the current file. */
  1363. expanded_location loc = expand_location (input_location);
  1364. linemap_add (line_table, LC_ENTER, 0, loc.file, loc.line);
  1365. }
  1366. file_start_location = input_location;
  1367. (*debug_hooks->start_source_file) (LOCATION_LINE (input_location),
  1368. LOCATION_FILE (input_location));
  1369. java_mark_class_local (current_class);
  1370. gen_indirect_dispatch_tables (current_class);
  1371. for (method = TYPE_METHODS (current_class);
  1372. method != NULL_TREE; method = DECL_CHAIN (method))
  1373. {
  1374. JCF *jcf = current_jcf;
  1375. if (METHOD_ABSTRACT (method) || METHOD_DUMMY (method))
  1376. continue;
  1377. if (METHOD_NATIVE (method))
  1378. {
  1379. tree arg;
  1380. int decl_max_locals;
  1381. if (! flag_jni)
  1382. continue;
  1383. /* We need to compute the DECL_MAX_LOCALS. We need to take
  1384. the wide types into account too. */
  1385. for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
  1386. arg != end_params_node;
  1387. arg = TREE_CHAIN (arg), decl_max_locals += 1)
  1388. {
  1389. if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
  1390. decl_max_locals += 1;
  1391. }
  1392. DECL_MAX_LOCALS (method) = decl_max_locals;
  1393. start_java_method (method);
  1394. give_name_to_locals (jcf);
  1395. *get_stmts () = build_jni_stub (method);
  1396. end_java_method ();
  1397. continue;
  1398. }
  1399. if (DECL_CODE_OFFSET (method) == 0)
  1400. {
  1401. current_function_decl = method;
  1402. error ("missing Code attribute");
  1403. continue;
  1404. }
  1405. input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
  1406. if (DECL_LINENUMBERS_OFFSET (method))
  1407. {
  1408. int i;
  1409. int min_line = 0;
  1410. unsigned char *ptr;
  1411. JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
  1412. linenumber_count = i = JCF_readu2 (jcf);
  1413. linenumber_table = ptr = jcf->read_ptr;
  1414. for (ptr += 2; --i >= 0; ptr += 4)
  1415. {
  1416. int line = GET_u2 (ptr);
  1417. /* Set initial line of input_location to smallest
  1418. * linenumber.
  1419. * Needs to be set before init_function_start. */
  1420. if (min_line == 0 || line < min_line)
  1421. min_line = line;
  1422. }
  1423. if (min_line != 0)
  1424. input_location = linemap_line_start (line_table, min_line, 1);
  1425. }
  1426. else
  1427. {
  1428. linenumber_table = NULL;
  1429. linenumber_count = 0;
  1430. }
  1431. start_java_method (method);
  1432. note_instructions (jcf, method);
  1433. give_name_to_locals (jcf);
  1434. /* Bump up start_label_pc_this_method so we get a unique label number
  1435. and reset highest_label_pc_this_method. */
  1436. if (highest_label_pc_this_method >= 0)
  1437. {
  1438. /* We adjust to the next multiple of 1000. This is just a frill
  1439. so the last 3 digits of the label number match the bytecode
  1440. offset, which might make debugging marginally more convenient. */
  1441. start_label_pc_this_method
  1442. = ((((start_label_pc_this_method + highest_label_pc_this_method)
  1443. / 1000)
  1444. + 1)
  1445. * 1000);
  1446. highest_label_pc_this_method = -1;
  1447. }
  1448. /* Convert bytecode to trees. */
  1449. expand_byte_code (jcf, method);
  1450. end_java_method ();
  1451. }
  1452. finish_class ();
  1453. (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
  1454. input_location = save_location;
  1455. }
  1456. static vec<tree, va_gc> *predefined_filenames;
  1457. void
  1458. add_predefined_file (tree name)
  1459. {
  1460. vec_safe_push (predefined_filenames, name);
  1461. }
  1462. int
  1463. predefined_filename_p (tree node)
  1464. {
  1465. unsigned ix;
  1466. tree f;
  1467. FOR_EACH_VEC_SAFE_ELT (predefined_filenames, ix, f)
  1468. if (f == node)
  1469. return 1;
  1470. return 0;
  1471. }
  1472. /* Generate a function that does all static initialization for this
  1473. translation unit. */
  1474. static void
  1475. java_emit_static_constructor (void)
  1476. {
  1477. tree body = NULL;
  1478. emit_register_classes (&body);
  1479. write_resource_constructor (&body);
  1480. if (body)
  1481. {
  1482. tree name = get_identifier ("_Jv_global_static_constructor");
  1483. tree decl
  1484. = build_decl (input_location, FUNCTION_DECL, name,
  1485. build_function_type_list (void_type_node, NULL_TREE));
  1486. tree resdecl = build_decl (input_location,
  1487. RESULT_DECL, NULL_TREE, void_type_node);
  1488. DECL_ARTIFICIAL (resdecl) = 1;
  1489. DECL_RESULT (decl) = resdecl;
  1490. current_function_decl = decl;
  1491. allocate_struct_function (decl, false);
  1492. TREE_STATIC (decl) = 1;
  1493. TREE_USED (decl) = 1;
  1494. DECL_ARTIFICIAL (decl) = 1;
  1495. DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
  1496. DECL_SAVED_TREE (decl) = body;
  1497. DECL_UNINLINABLE (decl) = 1;
  1498. DECL_INITIAL (decl) = make_node (BLOCK);
  1499. TREE_USED (DECL_INITIAL (decl)) = 1;
  1500. DECL_STATIC_CONSTRUCTOR (decl) = 1;
  1501. java_genericize (decl);
  1502. cgraph_node::finalize_function (decl, false);
  1503. }
  1504. }
  1505. void
  1506. java_parse_file (void)
  1507. {
  1508. int filename_count = 0;
  1509. location_t save_location = input_location;
  1510. char *file_list = NULL, *list, *next;
  1511. tree node;
  1512. FILE *finput = NULL;
  1513. int in_quotes = 0;
  1514. unsigned ix;
  1515. bitmap_obstack_initialize (&bit_obstack);
  1516. field_offsets = BITMAP_ALLOC (&bit_obstack);
  1517. if (flag_filelist_file)
  1518. {
  1519. int avail = 2000;
  1520. finput = fopen (main_input_filename, "r");
  1521. if (finput == NULL)
  1522. fatal_error (input_location,
  1523. "can%'t open %s: %m", LOCATION_FILE (input_location));
  1524. list = XNEWVEC (char, avail);
  1525. next = list;
  1526. for (;;)
  1527. {
  1528. int count;
  1529. if (avail < 500)
  1530. {
  1531. count = next - list;
  1532. avail = 2 * (count + avail);
  1533. list = XRESIZEVEC (char, list, avail);
  1534. next = list + count;
  1535. avail = avail - count;
  1536. }
  1537. /* Subtract one to guarantee space for final '\0'. */
  1538. count = fread (next, 1, avail - 1, finput);
  1539. if (count == 0)
  1540. {
  1541. if (! feof (finput))
  1542. fatal_error (input_location, "error closing %s: %m",
  1543. LOCATION_FILE (input_location));
  1544. *next = '\0';
  1545. break;
  1546. }
  1547. avail -= count;
  1548. next += count;
  1549. }
  1550. fclose (finput);
  1551. finput = NULL;
  1552. file_list = list;
  1553. }
  1554. else
  1555. list = CONST_CAST (char *, main_input_filename);
  1556. while (list)
  1557. {
  1558. for (next = list; ; )
  1559. {
  1560. char ch = *next;
  1561. if (flag_filelist_file && ! in_quotes
  1562. && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
  1563. || ch == '&') /* FIXME */)
  1564. {
  1565. if (next == list)
  1566. {
  1567. next++;
  1568. list = next;
  1569. continue;
  1570. }
  1571. else
  1572. {
  1573. *next++ = '\0';
  1574. break;
  1575. }
  1576. }
  1577. if (flag_filelist_file && ch == '"')
  1578. {
  1579. in_quotes = ! in_quotes;
  1580. *next++ = '\0';
  1581. if (in_quotes)
  1582. list = next;
  1583. else
  1584. break;
  1585. }
  1586. if (ch == '\0')
  1587. {
  1588. next = NULL;
  1589. break;
  1590. }
  1591. next++;
  1592. }
  1593. /* Exclude .java files. */
  1594. if (strlen (list) > 5 && ! strcmp (list + strlen (list) - 5, ".java"))
  1595. {
  1596. /* Nothing. */
  1597. }
  1598. else if (list[0])
  1599. {
  1600. node = get_identifier (list);
  1601. filename_count++;
  1602. /* Exclude file that we see twice on the command line. */
  1603. if (IS_A_COMMAND_LINE_FILENAME_P (node))
  1604. duplicate_class_warning (IDENTIFIER_POINTER (node));
  1605. else
  1606. {
  1607. build_translation_unit_decl (node);
  1608. IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
  1609. }
  1610. }
  1611. list = next;
  1612. }
  1613. free (file_list);
  1614. if (filename_count == 0)
  1615. warning (0, "no input file specified");
  1616. if (resource_name)
  1617. {
  1618. const char *resource_filename;
  1619. /* Only one resource file may be compiled at a time. */
  1620. gcc_assert (all_translation_units->length () == 1);
  1621. resource_filename
  1622. = IDENTIFIER_POINTER (DECL_NAME ((*all_translation_units)[0]));
  1623. compile_resource_file (resource_name, resource_filename);
  1624. goto finish;
  1625. }
  1626. current_jcf = main_jcf;
  1627. FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
  1628. {
  1629. unsigned char magic_string[4];
  1630. char *real_path;
  1631. uint32 magic = 0;
  1632. tree name = DECL_NAME (node);
  1633. tree real_file;
  1634. const char *filename = IDENTIFIER_POINTER (name);
  1635. /* Skip already parsed files */
  1636. real_path = lrealpath (filename);
  1637. real_file = get_identifier (real_path);
  1638. free (real_path);
  1639. if (HAS_BEEN_ALREADY_PARSED_P (real_file))
  1640. continue;
  1641. /* Close previous descriptor, if any */
  1642. if (finput && fclose (finput))
  1643. fatal_error (input_location,
  1644. "can%'t close input file %s: %m", main_input_filename);
  1645. finput = fopen (filename, "rb");
  1646. if (finput == NULL)
  1647. fatal_error (input_location, "can%'t open %s: %m", filename);
  1648. #ifdef IO_BUFFER_SIZE
  1649. setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
  1650. _IOFBF, IO_BUFFER_SIZE);
  1651. #endif
  1652. /* Figure what kind of file we're dealing with */
  1653. if (fread (magic_string, 1, 4, finput) == 4)
  1654. {
  1655. fseek (finput, 0L, SEEK_SET);
  1656. magic = GET_u4 (magic_string);
  1657. }
  1658. if (magic == 0xcafebabe)
  1659. {
  1660. CLASS_FILE_P (node) = 1;
  1661. current_jcf = ggc_cleared_alloc<JCF> ();
  1662. current_jcf->read_state = finput;
  1663. current_jcf->filbuf = jcf_filbuf_from_stdio;
  1664. jcf_parse (current_jcf);
  1665. DECL_SOURCE_LOCATION (node) = file_start_location;
  1666. TYPE_JCF (current_class) = current_jcf;
  1667. if (CLASS_FROM_CURRENTLY_COMPILED_P (current_class))
  1668. {
  1669. /* We've already compiled this class. */
  1670. duplicate_class_warning (filename);
  1671. continue;
  1672. }
  1673. CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
  1674. TREE_TYPE (node) = current_class;
  1675. }
  1676. else if (magic == (JCF_u4)ZIPMAGIC)
  1677. {
  1678. main_jcf = ggc_cleared_alloc<JCF> ();
  1679. main_jcf->read_state = finput;
  1680. main_jcf->filbuf = jcf_filbuf_from_stdio;
  1681. linemap_add (line_table, LC_ENTER, false, filename, 0);
  1682. input_location = linemap_line_start (line_table, 0, 1);
  1683. if (open_in_zip (main_jcf, filename, NULL, 0) < 0)
  1684. fatal_error (input_location, "bad zip/jar file %s", filename);
  1685. localToFile = SeenZipFiles;
  1686. /* Register all the classes defined there. */
  1687. process_zip_dir ((FILE *) main_jcf->read_state);
  1688. linemap_add (line_table, LC_LEAVE, false, NULL, 0);
  1689. parse_zip_file_entries ();
  1690. }
  1691. else if (magic == (JCF_u4) ZIPEMPTYMAGIC)
  1692. {
  1693. /* Ignore an empty input jar. */
  1694. }
  1695. else
  1696. {
  1697. gcc_unreachable ();
  1698. #if 0
  1699. java_push_parser_context ();
  1700. java_parser_context_save_global ();
  1701. parse_source_file_1 (real_file, filename, finput);
  1702. java_parser_context_restore_global ();
  1703. java_pop_parser_context (1);
  1704. linemap_add (line_table, LC_LEAVE, false, NULL, 0);
  1705. #endif
  1706. }
  1707. }
  1708. FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
  1709. {
  1710. input_location = DECL_SOURCE_LOCATION (node);
  1711. if (CLASS_FILE_P (node))
  1712. {
  1713. /* FIXME: These two flags really should be independent. We
  1714. should be able to compile fully binary compatible, but
  1715. with flag_verify_invocations on. */
  1716. flag_verify_invocations = ! flag_indirect_dispatch;
  1717. output_class = current_class = TREE_TYPE (node);
  1718. current_jcf = TYPE_JCF (current_class);
  1719. layout_class (current_class);
  1720. load_inner_classes (current_class);
  1721. parse_class_file ();
  1722. JCF_FINISH (current_jcf);
  1723. }
  1724. }
  1725. input_location = save_location;
  1726. bitmap_obstack_release (&bit_obstack);
  1727. finish:
  1728. /* Arrange for any necessary initialization to happen. */
  1729. java_emit_static_constructor ();
  1730. gcc_assert (global_bindings_p ());
  1731. }
  1732. /* Return the name of the class corresponding to the name of the file
  1733. in this zip entry. The result is newly allocated using ALLOC. */
  1734. static char *
  1735. compute_class_name (struct ZipDirectory *zdir)
  1736. {
  1737. char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
  1738. char *class_name;
  1739. int i;
  1740. int filename_length = zdir->filename_length;
  1741. while (filename_length > 2 && strncmp (class_name_in_zip_dir, "./", 2) == 0)
  1742. {
  1743. class_name_in_zip_dir += 2;
  1744. filename_length -= 2;
  1745. }
  1746. filename_length -= strlen (".class");
  1747. class_name = XNEWVEC (char, filename_length + 1);
  1748. memcpy (class_name, class_name_in_zip_dir, filename_length);
  1749. class_name [filename_length] = '\0';
  1750. for (i = 0; i < filename_length; i++)
  1751. if (class_name[i] == '/')
  1752. class_name[i] = '.';
  1753. return class_name;
  1754. }
  1755. /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
  1756. if it is a property file of some sort. */
  1757. static int
  1758. classify_zip_file (struct ZipDirectory *zdir)
  1759. {
  1760. char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
  1761. if (zdir->filename_length > 6
  1762. && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
  1763. ".class", 6))
  1764. return 1;
  1765. /* For now we drop the manifest, but not other information. */
  1766. if (zdir->filename_length == 20
  1767. && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
  1768. return 0;
  1769. /* Drop directory entries. */
  1770. if (zdir->filename_length > 0
  1771. && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
  1772. return 0;
  1773. return 2;
  1774. }
  1775. /* Process all class entries found in the zip file. */
  1776. static void
  1777. parse_zip_file_entries (void)
  1778. {
  1779. struct ZipDirectory *zdir;
  1780. int i;
  1781. for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
  1782. i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
  1783. {
  1784. tree klass;
  1785. switch (classify_zip_file (zdir))
  1786. {
  1787. case 0:
  1788. continue;
  1789. case 1:
  1790. {
  1791. char *class_name = compute_class_name (zdir);
  1792. int previous_alias_set = -1;
  1793. klass = lookup_class (get_identifier (class_name));
  1794. FREE (class_name);
  1795. current_jcf = TYPE_JCF (klass);
  1796. output_class = current_class = klass;
  1797. /* This is a dummy class, and now we're compiling it for
  1798. real. */
  1799. gcc_assert (! TYPE_DUMMY (klass));
  1800. /* This is for a corner case where we have a superclass
  1801. but no superclass fields.
  1802. This can happen if we earlier failed to lay out this
  1803. class because its superclass was still in the process
  1804. of being laid out; this occurs when we have recursive
  1805. class dependencies via inner classes. We must record
  1806. the previous alias set and restore it after laying out
  1807. the class.
  1808. FIXME: this really is a kludge. We should figure out a
  1809. way to lay out the class properly before this
  1810. happens. */
  1811. if (TYPE_SIZE (klass) && CLASSTYPE_SUPER (klass)
  1812. && integer_zerop (TYPE_SIZE (klass)))
  1813. {
  1814. TYPE_SIZE (klass) = NULL_TREE;
  1815. previous_alias_set = TYPE_ALIAS_SET (klass);
  1816. TYPE_ALIAS_SET (klass) = -1;
  1817. }
  1818. if (! CLASS_LOADED_P (klass))
  1819. {
  1820. if (! CLASS_PARSED_P (klass))
  1821. {
  1822. read_zip_member (current_jcf, zdir, localToFile);
  1823. jcf_parse (current_jcf);
  1824. }
  1825. layout_class (current_class);
  1826. load_inner_classes (current_class);
  1827. }
  1828. if (previous_alias_set != -1)
  1829. TYPE_ALIAS_SET (klass) = previous_alias_set;
  1830. if (TYPE_SIZE (current_class) != error_mark_node)
  1831. {
  1832. parse_class_file ();
  1833. free (current_jcf->buffer); /* No longer necessary */
  1834. /* Note: there is a way to free this buffer right after a
  1835. class seen in a zip file has been parsed. The idea is the
  1836. set its jcf in such a way that buffer will be reallocated
  1837. the time the code for the class will be generated. FIXME. */
  1838. }
  1839. }
  1840. break;
  1841. case 2:
  1842. {
  1843. char *file_name, *class_name_in_zip_dir, *buffer;
  1844. JCF *jcf;
  1845. file_name = XNEWVEC (char, zdir->filename_length + 1);
  1846. class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
  1847. strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
  1848. file_name[zdir->filename_length] = '\0';
  1849. jcf = XNEW (JCF);
  1850. JCF_ZERO (jcf);
  1851. jcf->read_state = finput;
  1852. jcf->filbuf = jcf_filbuf_from_stdio;
  1853. jcf->classname = NULL;
  1854. jcf->filename = file_name;
  1855. jcf->zipd = zdir;
  1856. if (read_zip_member (jcf, zdir, localToFile) < 0)
  1857. fatal_error (input_location,
  1858. "error while reading %s from zip file", file_name);
  1859. buffer = XNEWVEC (char, zdir->filename_length + 1 +
  1860. (jcf->buffer_end - jcf->buffer));
  1861. strcpy (buffer, file_name);
  1862. /* This is not a typo: we overwrite the trailing \0 of the
  1863. file name; this is just how the data is laid out. */
  1864. memcpy (buffer + zdir->filename_length,
  1865. jcf->buffer, jcf->buffer_end - jcf->buffer);
  1866. compile_resource_data (file_name, buffer,
  1867. jcf->buffer_end - jcf->buffer);
  1868. JCF_FINISH (jcf);
  1869. free (jcf);
  1870. free (buffer);
  1871. }
  1872. break;
  1873. default:
  1874. gcc_unreachable ();
  1875. }
  1876. }
  1877. }
  1878. /* Read all the entries of the zip file, creates a class and a JCF. Sets the
  1879. jcf up for further processing and link it to the created class. */
  1880. static void
  1881. process_zip_dir (FILE *finput)
  1882. {
  1883. int i;
  1884. ZipDirectory *zdir;
  1885. for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
  1886. i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
  1887. {
  1888. char *class_name, *file_name, *class_name_in_zip_dir;
  1889. tree klass;
  1890. JCF *jcf;
  1891. class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
  1892. /* Here we skip non-class files; we handle them later. */
  1893. if (classify_zip_file (zdir) != 1)
  1894. continue;
  1895. class_name = compute_class_name (zdir);
  1896. file_name = XNEWVEC (char, zdir->filename_length+1);
  1897. jcf = ggc_cleared_alloc<JCF> ();
  1898. strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
  1899. file_name [zdir->filename_length] = '\0';
  1900. klass = lookup_class (get_identifier (class_name));
  1901. if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
  1902. {
  1903. /* We've already compiled this class. */
  1904. duplicate_class_warning (file_name);
  1905. continue;
  1906. }
  1907. /* This function is only called when processing a zip file seen
  1908. on the command line. */
  1909. CLASS_FROM_CURRENTLY_COMPILED_P (klass) = 1;
  1910. jcf->read_state = finput;
  1911. jcf->filbuf = jcf_filbuf_from_stdio;
  1912. jcf->classname = class_name;
  1913. jcf->filename = file_name;
  1914. jcf->zipd = zdir;
  1915. TYPE_JCF (klass) = jcf;
  1916. }
  1917. }
  1918. #include "gt-java-jcf-parse.h"
  1919. #include "gtype-java.h"