class.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290
  1. /* Functions related to building classes and their related objects.
  2. Copyright (C) 1996-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>.
  15. Java and all Java-based marks are trademarks or registered trademarks
  16. of Sun Microsystems, Inc. in the United States and other countries.
  17. The Free Software Foundation is independent of Sun Microsystems, Inc. */
  18. /* Written by Per Bothner <bothner@cygnus.com> */
  19. #include "config.h"
  20. #include "system.h"
  21. #include "coretypes.h"
  22. #include "hash-set.h"
  23. #include "machmode.h"
  24. #include "vec.h"
  25. #include "double-int.h"
  26. #include "input.h"
  27. #include "alias.h"
  28. #include "symtab.h"
  29. #include "options.h"
  30. #include "wide-int.h"
  31. #include "inchash.h"
  32. #include "tree.h"
  33. #include "fold-const.h"
  34. #include "stringpool.h"
  35. #include "stor-layout.h"
  36. #include "varasm.h"
  37. #include "flags.h"
  38. #include "java-tree.h"
  39. #include "jcf.h"
  40. #include "obstack.h"
  41. #include "diagnostic-core.h"
  42. #include "toplev.h"
  43. #include "output.h" /* for switch_to_section and get_section */
  44. #include "parse.h"
  45. #include "tm.h"
  46. #include "hard-reg-set.h"
  47. #include "input.h"
  48. #include "function.h"
  49. #include "ggc.h"
  50. #include "hash-map.h"
  51. #include "is-a.h"
  52. #include "plugin-api.h"
  53. #include "ipa-ref.h"
  54. #include "cgraph.h"
  55. #include "tree-iterator.h"
  56. #include "target.h"
  57. static tree make_method_value (tree);
  58. static tree build_java_method_type (tree, tree, int);
  59. static int32 hashUtf8String (const char *, int);
  60. static tree make_field_value (tree);
  61. static tree get_dispatch_vector (tree);
  62. static tree get_dispatch_table (tree, tree);
  63. static int supers_all_compiled (tree type);
  64. static tree maybe_layout_super_class (tree, tree);
  65. static void add_miranda_methods (tree, tree);
  66. static int assume_compiled (const char *);
  67. static tree build_symbol_entry (tree, tree);
  68. static tree emit_assertion_table (tree);
  69. static void register_class (void);
  70. struct obstack temporary_obstack;
  71. static const char *cyclic_inheritance_report;
  72. /* The compiler generates different code depending on whether or not
  73. it can assume certain classes have been compiled down to native
  74. code or not. The compiler options -fassume-compiled= and
  75. -fno-assume-compiled= are used to create a tree of
  76. class_flag_node objects. This tree is queried to determine if
  77. a class is assume to be compiled or not. Each node in the tree
  78. represents either a package or a specific class. */
  79. typedef struct class_flag_node_struct
  80. {
  81. /* The class or package name. */
  82. const char *ident;
  83. /* Nonzero if this represents an exclusion. */
  84. int value;
  85. /* Pointers to other nodes in the tree. */
  86. struct class_flag_node_struct *parent;
  87. struct class_flag_node_struct *sibling;
  88. struct class_flag_node_struct *child;
  89. } class_flag_node;
  90. static class_flag_node *find_class_flag_node (class_flag_node *, const char *);
  91. static void add_class_flag (class_flag_node **, const char *, int);
  92. /* This is the root of the include/exclude tree. */
  93. static class_flag_node *assume_compiled_tree;
  94. static class_flag_node *enable_assert_tree;
  95. static GTY(()) tree class_roots[4];
  96. #define fields_ident class_roots[0] /* get_identifier ("fields") */
  97. #define info_ident class_roots[1] /* get_identifier ("info") */
  98. #define class_list class_roots[2]
  99. #define class_dtable_decl class_roots[3]
  100. static GTY(()) vec<tree, va_gc> *registered_class;
  101. /* A tree that returns the address of the class$ of the class
  102. currently being compiled. */
  103. static GTY(()) tree this_classdollar;
  104. /* A list of static class fields. This is to emit proper debug
  105. info for them. */
  106. vec<tree, va_gc> *pending_static_fields;
  107. /* Return the node that most closely represents the class whose name
  108. is IDENT. Start the search from NODE (followed by its siblings).
  109. Return NULL if an appropriate node does not exist. */
  110. static class_flag_node *
  111. find_class_flag_node (class_flag_node *node, const char *ident)
  112. {
  113. while (node)
  114. {
  115. size_t node_ident_length = strlen (node->ident);
  116. /* node_ident_length is zero at the root of the tree. If the
  117. identifiers are the same length, then we have matching
  118. classes. Otherwise check if we've matched an enclosing
  119. package name. */
  120. if (node_ident_length == 0
  121. || (strncmp (ident, node->ident, node_ident_length) == 0
  122. && (ident[node_ident_length] == '\0'
  123. || ident[node_ident_length] == '.')))
  124. {
  125. /* We've found a match, however, there might be a more
  126. specific match. */
  127. class_flag_node *found = find_class_flag_node (node->child, ident);
  128. if (found)
  129. return found;
  130. else
  131. return node;
  132. }
  133. /* No match yet. Continue through the sibling list. */
  134. node = node->sibling;
  135. }
  136. /* No match at all in this tree. */
  137. return NULL;
  138. }
  139. void
  140. add_class_flag (class_flag_node **rootp, const char *ident, int value)
  141. {
  142. class_flag_node *root = *rootp;
  143. class_flag_node *parent, *node;
  144. /* Create the root of the tree if it doesn't exist yet. */
  145. if (NULL == root)
  146. {
  147. root = XNEW (class_flag_node);
  148. root->ident = "";
  149. root->value = 0;
  150. root->sibling = NULL;
  151. root->child = NULL;
  152. root->parent = NULL;
  153. *rootp = root;
  154. }
  155. /* Calling the function with the empty string means we're setting
  156. value for the root of the hierarchy. */
  157. if (0 == ident[0])
  158. {
  159. root->value = value;
  160. return;
  161. }
  162. /* Find the parent node for this new node. PARENT will either be a
  163. class or a package name. Adjust PARENT accordingly. */
  164. parent = find_class_flag_node (root, ident);
  165. if (strcmp (ident, parent->ident) == 0)
  166. parent->value = value;
  167. else
  168. {
  169. /* Insert new node into the tree. */
  170. node = XNEW (class_flag_node);
  171. node->ident = xstrdup (ident);
  172. node->value = value;
  173. node->child = NULL;
  174. node->parent = parent;
  175. node->sibling = parent->child;
  176. parent->child = node;
  177. }
  178. }
  179. /* Add a new IDENT to the include/exclude tree. It's an exclusion
  180. if EXCLUDEP is nonzero. */
  181. void
  182. add_assume_compiled (const char *ident, int excludep)
  183. {
  184. add_class_flag (&assume_compiled_tree, ident, excludep);
  185. }
  186. /* The default value returned by enable_assertions. */
  187. #define DEFAULT_ENABLE_ASSERT (optimize == 0)
  188. /* Enter IDENT (a class or package name) into the enable-assertions table.
  189. VALUE is true to enable and false to disable. */
  190. void
  191. add_enable_assert (const char *ident, int value)
  192. {
  193. if (enable_assert_tree == NULL)
  194. add_class_flag (&enable_assert_tree, "", DEFAULT_ENABLE_ASSERT);
  195. add_class_flag (&enable_assert_tree, ident, value);
  196. }
  197. /* Returns nonzero if IDENT is the name of a class that the compiler
  198. should assume has been compiled to object code. */
  199. static int
  200. assume_compiled (const char *ident)
  201. {
  202. class_flag_node *i;
  203. int result;
  204. if (NULL == assume_compiled_tree)
  205. return 1;
  206. i = find_class_flag_node (assume_compiled_tree, ident);
  207. result = ! i->value;
  208. return (result);
  209. }
  210. /* Return true if we should generate code to check assertions within KLASS. */
  211. bool
  212. enable_assertions (tree klass)
  213. {
  214. /* Check if command-line specifies whether we should check assertions. */
  215. if (klass != NULL_TREE && DECL_NAME (klass) && enable_assert_tree != NULL)
  216. {
  217. const char *ident = IDENTIFIER_POINTER (DECL_NAME (klass));
  218. class_flag_node *node
  219. = find_class_flag_node (enable_assert_tree, ident);
  220. return node->value;
  221. }
  222. /* The default is to enable assertions if generating class files,
  223. or not optimizing. */
  224. return DEFAULT_ENABLE_ASSERT;
  225. }
  226. /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
  227. except that characters matching OLD_CHAR are substituted by NEW_CHAR.
  228. Also, PREFIX is prepended, and SUFFIX is appended. */
  229. tree
  230. ident_subst (const char* old_name,
  231. int old_length,
  232. const char *prefix,
  233. int old_char,
  234. int new_char,
  235. const char *suffix)
  236. {
  237. int prefix_len = strlen (prefix);
  238. int suffix_len = strlen (suffix);
  239. int i = prefix_len + old_length + suffix_len + 1;
  240. char *buffer = (char *) alloca (i);
  241. strcpy (buffer, prefix);
  242. for (i = 0; i < old_length; i++)
  243. {
  244. char ch = old_name[i];
  245. if (ch == old_char)
  246. ch = new_char;
  247. buffer[prefix_len + i] = ch;
  248. }
  249. strcpy (buffer + prefix_len + old_length, suffix);
  250. return get_identifier (buffer);
  251. }
  252. /* Return an IDENTIFIER_NODE the same as OLD_ID,
  253. except that characters matching OLD_CHAR are substituted by NEW_CHAR.
  254. Also, PREFIX is prepended, and SUFFIX is appended. */
  255. tree
  256. identifier_subst (const tree old_id,
  257. const char *prefix,
  258. int old_char,
  259. int new_char,
  260. const char *suffix)
  261. {
  262. return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
  263. prefix, old_char, new_char, suffix);
  264. }
  265. /* Generate a valid C identifier from the name of the class TYPE,
  266. prefixed by PREFIX. */
  267. tree
  268. mangled_classname (const char *prefix, tree type)
  269. {
  270. tree result;
  271. tree ident = TYPE_NAME (type);
  272. if (TREE_CODE (ident) != IDENTIFIER_NODE)
  273. ident = DECL_NAME (ident);
  274. result = identifier_subst (ident, prefix, '.', '_', "");
  275. /* Replace any characters that aren't in the set [0-9a-zA-Z_$] with
  276. "_0xXX". Class names containing such chracters are uncommon, but
  277. they do sometimes occur in class files. Without this check,
  278. these names cause assembly errors.
  279. There is a possibility that a real class name could conflict with
  280. the identifier we generate, but it is unlikely and will
  281. immediately be detected as an assembler error. At some point we
  282. should do something more elaborate (perhaps using the full
  283. unicode mangling scheme) in order to prevent such a conflict. */
  284. {
  285. int i;
  286. const int len = IDENTIFIER_LENGTH (result);
  287. const char *p = IDENTIFIER_POINTER (result);
  288. int illegal_chars = 0;
  289. /* Make two passes over the identifier. The first pass is merely
  290. to count illegal characters; we need to do this in order to
  291. allocate a buffer. */
  292. for (i = 0; i < len; i++)
  293. {
  294. char c = p[i];
  295. illegal_chars += (! ISALNUM (c) && c != '_' && c != '$');
  296. }
  297. /* And the second pass, which is rarely executed, does the
  298. rewriting. */
  299. if (illegal_chars != 0)
  300. {
  301. char *buffer = (char *) alloca (illegal_chars * 4 + len + 1);
  302. int j;
  303. for (i = 0, j = 0; i < len; i++)
  304. {
  305. char c = p[i];
  306. if (! ISALNUM (c) && c != '_' && c != '$')
  307. {
  308. buffer[j++] = '_';
  309. sprintf (&buffer[j], "0x%02x", c);
  310. j += 4;
  311. }
  312. else
  313. buffer[j++] = c;
  314. }
  315. buffer[j] = 0;
  316. result = get_identifier (buffer);
  317. }
  318. }
  319. return result;
  320. }
  321. tree
  322. make_class (void)
  323. {
  324. tree type;
  325. type = make_node (RECORD_TYPE);
  326. /* Unfortunately we must create the binfo here, so that class
  327. loading works. */
  328. TYPE_BINFO (type) = make_tree_binfo (0);
  329. MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
  330. TYPE_CATCH_CLASSES (type) = NULL;
  331. /* Push a dummy entry; we can't call make_catch_class_record here
  332. because other infrastructure may not be set up yet. We'll come
  333. back and fill it in later once said infrastructure is
  334. initialized. */
  335. CONSTRUCTOR_APPEND_ELT (TYPE_CATCH_CLASSES (type), NULL_TREE, NULL_TREE);
  336. return type;
  337. }
  338. /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
  339. and where each of the constituents is separated by '/',
  340. return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
  341. tree
  342. unmangle_classname (const char *name, int name_length)
  343. {
  344. tree to_return = ident_subst (name, name_length, "", '/', '.', "");
  345. /* It's not sufficient to compare to_return and get_identifier
  346. (name) to determine whether to_return is qualified. There are
  347. cases in signature analysis where name will be stripped of a
  348. trailing ';'. */
  349. name = IDENTIFIER_POINTER (to_return);
  350. while (*name)
  351. if (*name++ == '.')
  352. {
  353. QUALIFIED_P (to_return) = 1;
  354. break;
  355. }
  356. return to_return;
  357. }
  358. #define GEN_TABLE(TABLE, NAME, TABLE_TYPE, TYPE) \
  359. do \
  360. { \
  361. const char *type_name = IDENTIFIER_POINTER (mangled_classname ("", TYPE)); \
  362. char *buf = (char *) alloca (strlen (type_name) \
  363. + strlen (#NAME "_syms_") + 1); \
  364. tree decl; \
  365. \
  366. sprintf (buf, #NAME "_%s", type_name); \
  367. TYPE_## TABLE ##_DECL (type) = decl = \
  368. build_decl (input_location, VAR_DECL, get_identifier (buf), TABLE_TYPE); \
  369. DECL_EXTERNAL (decl) = 1; \
  370. TREE_STATIC (decl) = 1; \
  371. TREE_READONLY (decl) = 1; \
  372. TREE_CONSTANT (decl) = 1; \
  373. DECL_IGNORED_P (decl) = 1; \
  374. /* Mark the table as belonging to this class. */ \
  375. pushdecl (decl); \
  376. MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl); \
  377. DECL_OWNER (decl) = TYPE; \
  378. sprintf (buf, #NAME "_syms_%s", type_name); \
  379. TYPE_## TABLE ##_SYMS_DECL (TYPE) = \
  380. build_decl (input_location, VAR_DECL, get_identifier (buf), symbols_array_type); \
  381. TREE_STATIC (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
  382. TREE_CONSTANT (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
  383. DECL_IGNORED_P (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
  384. } \
  385. while (0)
  386. /* Given a class, create the DECLs for all its associated indirect
  387. dispatch tables. */
  388. void
  389. gen_indirect_dispatch_tables (tree type)
  390. {
  391. const char *type_name = IDENTIFIER_POINTER (mangled_classname ("", type));
  392. {
  393. tree field = NULL;
  394. char *buf = (char *) alloca (strlen (type_name)
  395. + strlen ("_catch_classes_") + 1);
  396. tree catch_class_type = make_node (RECORD_TYPE);
  397. sprintf (buf, "_catch_classes_%s", type_name);
  398. PUSH_FIELD (input_location,
  399. catch_class_type, field, "address", utf8const_ptr_type);
  400. PUSH_FIELD (input_location,
  401. catch_class_type, field, "classname", ptr_type_node);
  402. FINISH_RECORD (catch_class_type);
  403. TYPE_CTABLE_DECL (type)
  404. = build_decl (input_location, VAR_DECL, get_identifier (buf),
  405. build_array_type (catch_class_type, 0));
  406. DECL_EXTERNAL (TYPE_CTABLE_DECL (type)) = 1;
  407. TREE_STATIC (TYPE_CTABLE_DECL (type)) = 1;
  408. TREE_READONLY (TYPE_CTABLE_DECL (type)) = 1;
  409. TREE_CONSTANT (TYPE_CTABLE_DECL (type)) = 1;
  410. DECL_IGNORED_P (TYPE_CTABLE_DECL (type)) = 1;
  411. pushdecl (TYPE_CTABLE_DECL (type));
  412. }
  413. if (flag_indirect_dispatch)
  414. {
  415. GEN_TABLE (ATABLE, _atable, atable_type, type);
  416. GEN_TABLE (OTABLE, _otable, otable_type, type);
  417. GEN_TABLE (ITABLE, _itable, itable_type, type);
  418. }
  419. }
  420. #undef GEN_TABLE
  421. tree
  422. push_class (tree class_type, tree class_name)
  423. {
  424. tree decl, signature;
  425. location_t saved_loc = input_location;
  426. CLASS_P (class_type) = 1;
  427. decl = build_decl (input_location, TYPE_DECL, class_name, class_type);
  428. TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
  429. /* dbxout needs a DECL_SIZE if in gstabs mode */
  430. DECL_SIZE (decl) = integer_zero_node;
  431. input_location = saved_loc;
  432. signature = identifier_subst (class_name, "L", '.', '/', ";");
  433. IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
  434. /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
  435. both a typedef and in the struct name-space. We may want to re-visit
  436. this later, but for now it reduces the changes needed for gdb. */
  437. DECL_ARTIFICIAL (decl) = 1;
  438. pushdecl_top_level (decl);
  439. return decl;
  440. }
  441. /* Finds the (global) class named NAME. Creates the class if not found.
  442. Also creates associated TYPE_DECL.
  443. Does not check if the class actually exists, load the class,
  444. fill in field or methods, or do layout_type. */
  445. tree
  446. lookup_class (tree name)
  447. {
  448. tree decl = IDENTIFIER_CLASS_VALUE (name);
  449. if (decl == NULL_TREE)
  450. decl = push_class (make_class (), name);
  451. return TREE_TYPE (decl);
  452. }
  453. void
  454. set_super_info (int access_flags, tree this_class,
  455. tree super_class, int interfaces_count)
  456. {
  457. int total_supers = interfaces_count;
  458. tree class_decl = TYPE_NAME (this_class);
  459. if (super_class)
  460. total_supers++;
  461. if (total_supers)
  462. TYPE_BINFO (this_class) = make_tree_binfo (total_supers);
  463. TYPE_VFIELD (this_class) = TYPE_VFIELD (object_type_node);
  464. if (super_class)
  465. {
  466. tree super_binfo = make_tree_binfo (0);
  467. BINFO_TYPE (super_binfo) = super_class;
  468. BINFO_OFFSET (super_binfo) = integer_zero_node;
  469. BINFO_BASE_APPEND (TYPE_BINFO (this_class), super_binfo);
  470. CLASS_HAS_SUPER_FLAG (TYPE_BINFO (this_class)) = 1;
  471. }
  472. set_class_decl_access_flags (access_flags, class_decl);
  473. }
  474. void
  475. set_class_decl_access_flags (int access_flags, tree class_decl)
  476. {
  477. if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
  478. if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
  479. if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
  480. if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
  481. if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
  482. if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
  483. if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
  484. if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
  485. if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
  486. if (access_flags & ACC_ENUM) CLASS_ENUM (class_decl) = 1;
  487. if (access_flags & ACC_SYNTHETIC) CLASS_SYNTHETIC (class_decl) = 1;
  488. if (access_flags & ACC_ANNOTATION) CLASS_ANNOTATION (class_decl) = 1;
  489. }
  490. /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
  491. direct sub-classes of Object are 1, and so on. */
  492. int
  493. class_depth (tree clas)
  494. {
  495. int depth = 0;
  496. if (! CLASS_LOADED_P (clas))
  497. load_class (clas, 1);
  498. if (TYPE_SIZE (clas) == error_mark_node)
  499. return -1;
  500. while (clas != object_type_node)
  501. {
  502. depth++;
  503. clas = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (clas), 0));
  504. }
  505. return depth;
  506. }
  507. /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
  508. int
  509. interface_of_p (tree type1, tree type2)
  510. {
  511. int i;
  512. tree binfo, base_binfo;
  513. if (! TYPE_BINFO (type2))
  514. return 0;
  515. for (binfo = TYPE_BINFO (type2), i = 0;
  516. BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  517. if (BINFO_TYPE (base_binfo) == type1)
  518. return 1;
  519. for (binfo = TYPE_BINFO (type2), i = 0;
  520. BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) /* */
  521. if (BINFO_TYPE (base_binfo)
  522. && interface_of_p (type1, BINFO_TYPE (base_binfo)))
  523. return 1;
  524. return 0;
  525. }
  526. /* Return true iff TYPE1 inherits from TYPE2. */
  527. int
  528. inherits_from_p (tree type1, tree type2)
  529. {
  530. while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
  531. {
  532. if (type1 == type2)
  533. return 1;
  534. if (! CLASS_LOADED_P (type1))
  535. load_class (type1, 1);
  536. type1 = maybe_layout_super_class (CLASSTYPE_SUPER (type1), type1);
  537. }
  538. return 0;
  539. }
  540. /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
  541. int
  542. enclosing_context_p (tree type1, tree type2)
  543. {
  544. if (!INNER_CLASS_TYPE_P (type2))
  545. return 0;
  546. for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
  547. type2;
  548. type2 = (INNER_CLASS_TYPE_P (type2) ?
  549. TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
  550. {
  551. if (type2 == type1)
  552. return 1;
  553. }
  554. return 0;
  555. }
  556. /* Return 1 iff TYPE1 and TYPE2 share a common enclosing class, regardless of
  557. nesting level. */
  558. int
  559. common_enclosing_context_p (tree type1, tree type2)
  560. {
  561. while (type1)
  562. {
  563. tree current;
  564. for (current = type2; current;
  565. current = (INNER_CLASS_TYPE_P (current) ?
  566. TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
  567. NULL_TREE))
  568. if (type1 == current)
  569. return 1;
  570. if (INNER_CLASS_TYPE_P (type1))
  571. type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1)));
  572. else
  573. break;
  574. }
  575. return 0;
  576. }
  577. /* Return 1 iff there exists a common enclosing "this" between TYPE1
  578. and TYPE2, without crossing any static context. */
  579. int
  580. common_enclosing_instance_p (tree type1, tree type2)
  581. {
  582. if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
  583. return 0;
  584. for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
  585. type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
  586. TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
  587. {
  588. tree current;
  589. for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
  590. current = (PURE_INNER_CLASS_TYPE_P (current) ?
  591. TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
  592. NULL_TREE))
  593. if (type1 == current)
  594. return 1;
  595. }
  596. return 0;
  597. }
  598. /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
  599. found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
  600. if attempt is made to add it twice. */
  601. tree
  602. maybe_add_interface (tree this_class, tree interface_class)
  603. {
  604. tree binfo, base_binfo;
  605. int i;
  606. for (binfo = TYPE_BINFO (this_class), i = 0;
  607. BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  608. if (BINFO_TYPE (base_binfo) == interface_class)
  609. return interface_class;
  610. add_interface (this_class, interface_class);
  611. return NULL_TREE;
  612. }
  613. /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
  614. void
  615. add_interface (tree this_class, tree interface_class)
  616. {
  617. tree interface_binfo = make_tree_binfo (0);
  618. BINFO_TYPE (interface_binfo) = interface_class;
  619. BINFO_OFFSET (interface_binfo) = integer_zero_node;
  620. BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
  621. BINFO_VIRTUAL_P (interface_binfo) = 1;
  622. BINFO_BASE_APPEND (TYPE_BINFO (this_class), interface_binfo);
  623. }
  624. static tree
  625. build_java_method_type (tree fntype, tree this_class, int access_flags)
  626. {
  627. if (access_flags & ACC_STATIC)
  628. return fntype;
  629. fntype = build_method_type (this_class, fntype);
  630. /* We know that arg 1 of every nonstatic method is non-null; tell
  631. the back-end so. */
  632. TYPE_ATTRIBUTES (fntype) = (tree_cons
  633. (get_identifier ("nonnull"),
  634. tree_cons (NULL_TREE,
  635. build_int_cst (NULL_TREE, 1),
  636. NULL_TREE),
  637. TYPE_ATTRIBUTES (fntype)));
  638. return fntype;
  639. }
  640. void
  641. java_hide_decl (tree decl ATTRIBUTE_UNUSED)
  642. {
  643. #ifdef HAVE_GAS_HIDDEN
  644. DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
  645. DECL_VISIBILITY_SPECIFIED (decl) = 1;
  646. #endif
  647. }
  648. tree
  649. add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
  650. {
  651. tree method_type, fndecl;
  652. method_type = build_java_method_type (function_type,
  653. this_class, access_flags);
  654. fndecl = build_decl (input_location, FUNCTION_DECL, name, method_type);
  655. DECL_CONTEXT (fndecl) = this_class;
  656. DECL_LANG_SPECIFIC (fndecl) = ggc_cleared_alloc<struct lang_decl> ();
  657. DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
  658. /* Initialize the static initializer test table. */
  659. DECL_FUNCTION_INIT_TEST_TABLE (fndecl) = java_treetreehash_create (10);
  660. /* Initialize the initialized (static) class table. */
  661. if (access_flags & ACC_STATIC)
  662. DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
  663. hash_table<ict_hasher>::create_ggc (50);
  664. DECL_CHAIN (fndecl) = TYPE_METHODS (this_class);
  665. TYPE_METHODS (this_class) = fndecl;
  666. /* If pointers to member functions use the least significant bit to
  667. indicate whether a function is virtual, ensure a pointer
  668. to this function will have that bit clear. */
  669. if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
  670. && !(access_flags & ACC_STATIC)
  671. && DECL_ALIGN (fndecl) < 2 * BITS_PER_UNIT)
  672. DECL_ALIGN (fndecl) = 2 * BITS_PER_UNIT;
  673. /* Notice that this is a finalizer and update the class type
  674. accordingly. This is used to optimize instance allocation. */
  675. if (name == finalize_identifier_node
  676. && TREE_TYPE (function_type) == void_type_node
  677. && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
  678. HAS_FINALIZER_P (this_class) = 1;
  679. if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
  680. if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
  681. if (access_flags & ACC_PRIVATE)
  682. METHOD_PRIVATE (fndecl) = 1;
  683. if (access_flags & ACC_NATIVE)
  684. {
  685. METHOD_NATIVE (fndecl) = 1;
  686. DECL_EXTERNAL (fndecl) = 1;
  687. }
  688. else
  689. /* FNDECL is external unless we are compiling it into this object
  690. file. */
  691. DECL_EXTERNAL (fndecl) = CLASS_FROM_CURRENTLY_COMPILED_P (this_class) == 0;
  692. if (access_flags & ACC_STATIC)
  693. METHOD_STATIC (fndecl) = 1;
  694. if (access_flags & ACC_FINAL)
  695. METHOD_FINAL (fndecl) = 1;
  696. if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
  697. if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
  698. if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
  699. if (access_flags & ACC_SYNTHETIC) DECL_ARTIFICIAL (fndecl) = 1;
  700. if (access_flags & ACC_BRIDGE) METHOD_BRIDGE (fndecl) = 1;
  701. if (access_flags & ACC_VARARGS) METHOD_VARARGS (fndecl) = 1;
  702. return fndecl;
  703. }
  704. /* Add a method to THIS_CLASS.
  705. The method's name is NAME.
  706. Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
  707. tree
  708. add_method (tree this_class, int access_flags, tree name, tree method_sig)
  709. {
  710. tree function_type, fndecl;
  711. const unsigned char *sig
  712. = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
  713. if (sig[0] != '(')
  714. fatal_error (input_location, "bad method signature");
  715. function_type = get_type_from_signature (method_sig);
  716. fndecl = add_method_1 (this_class, access_flags, name, function_type);
  717. set_java_signature (TREE_TYPE (fndecl), method_sig);
  718. return fndecl;
  719. }
  720. tree
  721. add_field (tree klass, tree name, tree field_type, int flags)
  722. {
  723. int is_static = (flags & ACC_STATIC) != 0;
  724. tree field;
  725. field = build_decl (input_location,
  726. is_static ? VAR_DECL : FIELD_DECL, name, field_type);
  727. DECL_CHAIN (field) = TYPE_FIELDS (klass);
  728. TYPE_FIELDS (klass) = field;
  729. DECL_CONTEXT (field) = klass;
  730. MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
  731. if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
  732. if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
  733. if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
  734. if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
  735. if (flags & ACC_VOLATILE)
  736. {
  737. FIELD_VOLATILE (field) = 1;
  738. TREE_THIS_VOLATILE (field) = 1;
  739. }
  740. if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
  741. if (flags & ACC_ENUM) FIELD_ENUM (field) = 1;
  742. if (flags & ACC_SYNTHETIC) FIELD_SYNTHETIC (field) = 1;
  743. if (is_static)
  744. {
  745. FIELD_STATIC (field) = 1;
  746. /* Always make field externally visible. This is required so
  747. that native methods can always access the field. */
  748. TREE_PUBLIC (field) = 1;
  749. /* Hide everything that shouldn't be visible outside a DSO. */
  750. if (flag_indirect_classes
  751. || (FIELD_PRIVATE (field)))
  752. java_hide_decl (field);
  753. /* Considered external unless we are compiling it into this
  754. object file. */
  755. DECL_EXTERNAL (field) = (is_compiled_class (klass) != 2);
  756. if (!DECL_EXTERNAL (field))
  757. vec_safe_push (pending_static_fields, field);
  758. }
  759. return field;
  760. }
  761. /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
  762. void
  763. set_constant_value (tree field, tree constant)
  764. {
  765. if (field == NULL_TREE)
  766. warning (OPT_Wattributes,
  767. "misplaced ConstantValue attribute (not in any field)");
  768. else if (DECL_INITIAL (field) != NULL_TREE)
  769. warning (OPT_Wattributes,
  770. "duplicate ConstantValue attribute for field '%s'",
  771. IDENTIFIER_POINTER (DECL_NAME (field)));
  772. else
  773. {
  774. DECL_INITIAL (field) = constant;
  775. if (TREE_TYPE (constant) != TREE_TYPE (field)
  776. && ! (TREE_TYPE (constant) == int_type_node
  777. && INTEGRAL_TYPE_P (TREE_TYPE (field))
  778. && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
  779. && ! (TREE_TYPE (constant) == utf8const_ptr_type
  780. && TREE_TYPE (field) == string_ptr_type_node))
  781. error ("ConstantValue attribute of field '%s' has wrong type",
  782. IDENTIFIER_POINTER (DECL_NAME (field)));
  783. }
  784. }
  785. /* Calculate a hash value for a string encoded in Utf8 format.
  786. * This returns the same hash value as specified for java.lang.String.hashCode.
  787. */
  788. static int32
  789. hashUtf8String (const char *str, int len)
  790. {
  791. const unsigned char* ptr = (const unsigned char*) str;
  792. const unsigned char *limit = ptr + len;
  793. uint32 hash = 0;
  794. for (; ptr < limit;)
  795. {
  796. int ch = UTF8_GET (ptr, limit);
  797. /* Updated specification from
  798. http://www.javasoft.com/docs/books/jls/clarify.html. */
  799. hash = (31 * hash) + ch;
  800. }
  801. return hash;
  802. }
  803. tree
  804. build_utf8_ref (tree name)
  805. {
  806. const char * name_ptr = IDENTIFIER_POINTER (name);
  807. int name_len = IDENTIFIER_LENGTH (name), name_pad;
  808. char buf[60];
  809. tree ctype, field = NULL_TREE, str_type, cinit, string;
  810. static int utf8_count = 0;
  811. int name_hash;
  812. tree ref = IDENTIFIER_UTF8_REF (name);
  813. tree decl;
  814. vec<constructor_elt, va_gc> *v = NULL;
  815. if (ref != NULL_TREE)
  816. return ref;
  817. ctype = make_node (RECORD_TYPE);
  818. /* '\0' byte plus padding to utf8const_type's alignment. */
  819. name_pad = TYPE_ALIGN_UNIT (utf8const_type)
  820. - (name_len & (TYPE_ALIGN_UNIT (utf8const_type) - 1));
  821. str_type = build_prim_array_type (unsigned_byte_type_node,
  822. name_len + name_pad);
  823. PUSH_FIELD (input_location, ctype, field, "hash", unsigned_short_type_node);
  824. PUSH_FIELD (input_location,
  825. ctype, field, "length", unsigned_short_type_node);
  826. PUSH_FIELD (input_location, ctype, field, "data", str_type);
  827. FINISH_RECORD (ctype);
  828. START_RECORD_CONSTRUCTOR (v, ctype);
  829. name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
  830. PUSH_FIELD_VALUE (v, "hash", build_int_cst (NULL_TREE, name_hash));
  831. PUSH_FIELD_VALUE (v, "length", build_int_cst (NULL_TREE, name_len));
  832. string = build_string (name_len, name_ptr);
  833. TREE_TYPE (string) = str_type;
  834. PUSH_FIELD_VALUE (v, "data", string);
  835. FINISH_RECORD_CONSTRUCTOR (cinit, v, ctype);
  836. TREE_CONSTANT (cinit) = 1;
  837. /* Generate a unique-enough identifier. */
  838. sprintf(buf, "_Utf%d", ++utf8_count);
  839. decl = build_decl (input_location,
  840. VAR_DECL, get_identifier (buf), utf8const_type);
  841. TREE_STATIC (decl) = 1;
  842. DECL_ARTIFICIAL (decl) = 1;
  843. DECL_IGNORED_P (decl) = 1;
  844. TREE_READONLY (decl) = 1;
  845. TREE_THIS_VOLATILE (decl) = 0;
  846. DECL_INITIAL (decl) = cinit;
  847. DECL_USER_ALIGN (decl) = 1;
  848. if (HAVE_GAS_SHF_MERGE)
  849. {
  850. int decl_size;
  851. /* Ensure decl_size is a multiple of utf8const_type's alignment. */
  852. decl_size = name_len + 4 + name_pad;
  853. if (flag_merge_constants && decl_size < 256)
  854. {
  855. char buf[32];
  856. int flags = (SECTION_OVERRIDE
  857. | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
  858. sprintf (buf, ".rodata.jutf8.%d", decl_size);
  859. switch_to_section (get_section (buf, flags, NULL));
  860. set_decl_section_name (decl, buf);
  861. }
  862. }
  863. layout_decl (decl, 0);
  864. DECL_SIZE (decl) = TYPE_SIZE (ctype);
  865. DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (ctype);
  866. pushdecl (decl);
  867. rest_of_decl_compilation (decl, global_bindings_p (), 0);
  868. ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
  869. IDENTIFIER_UTF8_REF (name) = ref;
  870. return ref;
  871. }
  872. /* Like build_class_ref, but instead of a direct reference generate a
  873. pointer into the constant pool. */
  874. static tree
  875. build_indirect_class_ref (tree type)
  876. {
  877. int index;
  878. tree cl;
  879. index = alloc_class_constant (type);
  880. cl = build_ref_from_constant_pool (index);
  881. return convert (promote_type (class_ptr_type), cl);
  882. }
  883. static tree
  884. build_static_class_ref (tree type)
  885. {
  886. tree decl_name, decl, ref;
  887. if (TYPE_SIZE (type) == error_mark_node)
  888. return null_pointer_node;
  889. decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
  890. "", '/', '/', ".class$$");
  891. decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
  892. if (decl == NULL_TREE)
  893. {
  894. decl = build_decl (input_location, VAR_DECL, decl_name, class_type_node);
  895. TREE_STATIC (decl) = 1;
  896. if (! flag_indirect_classes)
  897. {
  898. TREE_PUBLIC (decl) = 1;
  899. if (CLASS_PRIVATE (TYPE_NAME (type)))
  900. java_hide_decl (decl);
  901. }
  902. DECL_IGNORED_P (decl) = 1;
  903. DECL_ARTIFICIAL (decl) = 1;
  904. if (is_compiled_class (type) == 1)
  905. DECL_EXTERNAL (decl) = 1;
  906. MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
  907. DECL_CLASS_FIELD_P (decl) = 1;
  908. DECL_CONTEXT (decl) = type;
  909. /* ??? We want to preserve the DECL_CONTEXT we set just above,
  910. that that means not calling pushdecl_top_level. */
  911. IDENTIFIER_GLOBAL_VALUE (decl_name) = decl;
  912. }
  913. ref = build1 (ADDR_EXPR, class_ptr_type, decl);
  914. return ref;
  915. }
  916. static tree
  917. build_classdollar_field (tree type)
  918. {
  919. tree decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
  920. "", '/', '/', ".class$");
  921. tree decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
  922. if (decl == NULL_TREE)
  923. {
  924. decl
  925. = build_decl (input_location,
  926. VAR_DECL, decl_name,
  927. (build_qualified_type
  928. (build_pointer_type
  929. (build_qualified_type (class_type_node,
  930. TYPE_QUAL_CONST)),
  931. TYPE_QUAL_CONST)));
  932. TREE_STATIC (decl) = 1;
  933. TREE_CONSTANT (decl) = 1;
  934. TREE_PUBLIC (decl) = 1;
  935. java_hide_decl (decl);
  936. DECL_IGNORED_P (decl) = 1;
  937. DECL_ARTIFICIAL (decl) = 1;
  938. MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
  939. IDENTIFIER_GLOBAL_VALUE (decl_name) = decl;
  940. DECL_CLASS_FIELD_P (decl) = 1;
  941. DECL_CONTEXT (decl) = type;
  942. }
  943. return decl;
  944. }
  945. /* Create a local variable that holds the current class$. */
  946. void
  947. cache_this_class_ref (tree fndecl)
  948. {
  949. if (optimize)
  950. {
  951. tree classdollar_field;
  952. if (flag_indirect_classes)
  953. classdollar_field = build_classdollar_field (output_class);
  954. else
  955. classdollar_field = build_static_class_ref (output_class);
  956. this_classdollar = build_decl (input_location,
  957. VAR_DECL, NULL_TREE,
  958. TREE_TYPE (classdollar_field));
  959. java_add_local_var (this_classdollar);
  960. java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (this_classdollar),
  961. this_classdollar, classdollar_field));
  962. }
  963. else
  964. this_classdollar = build_classdollar_field (output_class);
  965. /* Prepend class initialization for static methods reachable from
  966. other classes. */
  967. if (METHOD_STATIC (fndecl)
  968. && (! METHOD_PRIVATE (fndecl)
  969. || INNER_CLASS_P (DECL_CONTEXT (fndecl)))
  970. && ! DECL_CLINIT_P (fndecl)
  971. && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl))))
  972. {
  973. tree init = build_call_expr (soft_initclass_node, 1,
  974. this_classdollar);
  975. java_add_stmt (init);
  976. }
  977. }
  978. /* Remove the reference to the local variable that holds the current
  979. class$. */
  980. void
  981. uncache_this_class_ref (tree fndecl ATTRIBUTE_UNUSED)
  982. {
  983. this_classdollar = build_classdollar_field (output_class);
  984. }
  985. /* Build a reference to the class TYPE.
  986. Also handles primitive types and array types. */
  987. tree
  988. build_class_ref (tree type)
  989. {
  990. int is_compiled = is_compiled_class (type);
  991. if (is_compiled)
  992. {
  993. tree ref, decl;
  994. if (TREE_CODE (type) == POINTER_TYPE)
  995. type = TREE_TYPE (type);
  996. if (flag_indirect_dispatch
  997. && type != output_class
  998. && TREE_CODE (type) == RECORD_TYPE)
  999. return build_indirect_class_ref (type);
  1000. if (type == output_class && flag_indirect_classes)
  1001. {
  1002. /* This can be NULL if we see a JNI stub before we see any
  1003. other method. */
  1004. if (! this_classdollar)
  1005. this_classdollar = build_classdollar_field (output_class);
  1006. return this_classdollar;
  1007. }
  1008. if (TREE_CODE (type) == RECORD_TYPE)
  1009. return build_static_class_ref (type);
  1010. else
  1011. {
  1012. const char *name;
  1013. tree decl_name;
  1014. char buffer[25];
  1015. decl_name = TYPE_NAME (type);
  1016. if (TREE_CODE (decl_name) == TYPE_DECL)
  1017. decl_name = DECL_NAME (decl_name);
  1018. name = IDENTIFIER_POINTER (decl_name);
  1019. if (strncmp (name, "promoted_", 9) == 0)
  1020. name += 9;
  1021. sprintf (buffer, "_Jv_%sClass", name);
  1022. decl_name = get_identifier (buffer);
  1023. decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
  1024. if (decl == NULL_TREE)
  1025. {
  1026. decl = build_decl (input_location,
  1027. VAR_DECL, decl_name, class_type_node);
  1028. TREE_STATIC (decl) = 1;
  1029. TREE_PUBLIC (decl) = 1;
  1030. DECL_EXTERNAL (decl) = 1;
  1031. DECL_ARTIFICIAL (decl) = 1;
  1032. pushdecl_top_level (decl);
  1033. }
  1034. }
  1035. ref = build1 (ADDR_EXPR, class_ptr_type, decl);
  1036. return ref;
  1037. }
  1038. else
  1039. return build_indirect_class_ref (type);
  1040. }
  1041. /* Create a local statically allocated variable that will hold a
  1042. pointer to a static field. */
  1043. static tree
  1044. build_fieldref_cache_entry (int index, tree fdecl ATTRIBUTE_UNUSED)
  1045. {
  1046. tree decl, decl_name;
  1047. const char *name = IDENTIFIER_POINTER (mangled_classname ("_cpool_", output_class));
  1048. char *buf = (char *) alloca (strlen (name) + 20);
  1049. sprintf (buf, "%s_%d_ref", name, index);
  1050. decl_name = get_identifier (buf);
  1051. decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
  1052. if (decl == NULL_TREE)
  1053. {
  1054. decl = build_decl (input_location,
  1055. VAR_DECL, decl_name, ptr_type_node);
  1056. TREE_STATIC (decl) = 1;
  1057. TREE_PUBLIC (decl) = 0;
  1058. DECL_EXTERNAL (decl) = 0;
  1059. DECL_ARTIFICIAL (decl) = 1;
  1060. DECL_IGNORED_P (decl) = 1;
  1061. pushdecl_top_level (decl);
  1062. }
  1063. return decl;
  1064. }
  1065. tree
  1066. build_static_field_ref (tree fdecl)
  1067. {
  1068. tree fclass = DECL_CONTEXT (fdecl);
  1069. int is_compiled = is_compiled_class (fclass);
  1070. /* Allow static final fields to fold to a constant. When using
  1071. -findirect-dispatch, we simply never do this folding if compiling
  1072. from .class; in the .class file constants will be referred to via
  1073. the constant pool. */
  1074. if (!flag_indirect_dispatch
  1075. && (is_compiled
  1076. || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
  1077. && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
  1078. || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
  1079. && TREE_CONSTANT (DECL_INITIAL (fdecl)))))
  1080. {
  1081. if (is_compiled == 1)
  1082. DECL_EXTERNAL (fdecl) = 1;
  1083. }
  1084. else
  1085. {
  1086. /* Generate a CONSTANT_FieldRef for FDECL in the constant pool
  1087. and a class local static variable CACHE_ENTRY, then
  1088. *(fdecl **)((__builtin_expect (cache_entry == null, false))
  1089. ? cache_entry = _Jv_ResolvePoolEntry (output_class, cpool_index)
  1090. : cache_entry)
  1091. This can mostly be optimized away, so that the usual path is a
  1092. load followed by a test and branch. _Jv_ResolvePoolEntry is
  1093. only called once for each constant pool entry.
  1094. There is an optimization that we don't do: at the start of a
  1095. method, create a local copy of CACHE_ENTRY and use that instead.
  1096. */
  1097. int cpool_index = alloc_constant_fieldref (output_class, fdecl);
  1098. tree cache_entry = build_fieldref_cache_entry (cpool_index, fdecl);
  1099. tree test
  1100. = build_call_expr (builtin_decl_implicit (BUILT_IN_EXPECT), 2,
  1101. build2 (EQ_EXPR, boolean_type_node,
  1102. cache_entry, null_pointer_node),
  1103. boolean_false_node);
  1104. tree cpool_index_cst = build_int_cst (NULL_TREE, cpool_index);
  1105. tree init
  1106. = build_call_expr (soft_resolvepoolentry_node, 2,
  1107. build_class_ref (output_class),
  1108. cpool_index_cst);
  1109. init = build2 (MODIFY_EXPR, ptr_type_node, cache_entry, init);
  1110. init = build3 (COND_EXPR, ptr_type_node, test, init, cache_entry);
  1111. init = fold_convert (build_pointer_type (TREE_TYPE (fdecl)), init);
  1112. fdecl = build1 (INDIRECT_REF, TREE_TYPE (fdecl), init);
  1113. }
  1114. return fdecl;
  1115. }
  1116. int
  1117. get_access_flags_from_decl (tree decl)
  1118. {
  1119. int access_flags = 0;
  1120. if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
  1121. {
  1122. if (FIELD_STATIC (decl))
  1123. access_flags |= ACC_STATIC;
  1124. if (FIELD_PUBLIC (decl))
  1125. access_flags |= ACC_PUBLIC;
  1126. if (FIELD_PROTECTED (decl))
  1127. access_flags |= ACC_PROTECTED;
  1128. if (FIELD_PRIVATE (decl))
  1129. access_flags |= ACC_PRIVATE;
  1130. if (FIELD_FINAL (decl))
  1131. access_flags |= ACC_FINAL;
  1132. if (FIELD_VOLATILE (decl))
  1133. access_flags |= ACC_VOLATILE;
  1134. if (FIELD_TRANSIENT (decl))
  1135. access_flags |= ACC_TRANSIENT;
  1136. if (FIELD_ENUM (decl))
  1137. access_flags |= ACC_ENUM;
  1138. if (FIELD_SYNTHETIC (decl))
  1139. access_flags |= ACC_SYNTHETIC;
  1140. return access_flags;
  1141. }
  1142. if (TREE_CODE (decl) == TYPE_DECL)
  1143. {
  1144. if (CLASS_PUBLIC (decl))
  1145. access_flags |= ACC_PUBLIC;
  1146. if (CLASS_FINAL (decl))
  1147. access_flags |= ACC_FINAL;
  1148. if (CLASS_SUPER (decl))
  1149. access_flags |= ACC_SUPER;
  1150. if (CLASS_INTERFACE (decl))
  1151. access_flags |= ACC_INTERFACE;
  1152. if (CLASS_ABSTRACT (decl))
  1153. access_flags |= ACC_ABSTRACT;
  1154. if (CLASS_STATIC (decl))
  1155. access_flags |= ACC_STATIC;
  1156. if (CLASS_PRIVATE (decl))
  1157. access_flags |= ACC_PRIVATE;
  1158. if (CLASS_PROTECTED (decl))
  1159. access_flags |= ACC_PROTECTED;
  1160. if (CLASS_STRICTFP (decl))
  1161. access_flags |= ACC_STRICT;
  1162. if (CLASS_ENUM (decl))
  1163. access_flags |= ACC_ENUM;
  1164. if (CLASS_SYNTHETIC (decl))
  1165. access_flags |= ACC_SYNTHETIC;
  1166. if (CLASS_ANNOTATION (decl))
  1167. access_flags |= ACC_ANNOTATION;
  1168. return access_flags;
  1169. }
  1170. if (TREE_CODE (decl) == FUNCTION_DECL)
  1171. {
  1172. if (METHOD_PUBLIC (decl))
  1173. access_flags |= ACC_PUBLIC;
  1174. if (METHOD_PRIVATE (decl))
  1175. access_flags |= ACC_PRIVATE;
  1176. if (METHOD_PROTECTED (decl))
  1177. access_flags |= ACC_PROTECTED;
  1178. if (METHOD_STATIC (decl))
  1179. access_flags |= ACC_STATIC;
  1180. if (METHOD_FINAL (decl))
  1181. access_flags |= ACC_FINAL;
  1182. if (METHOD_SYNCHRONIZED (decl))
  1183. access_flags |= ACC_SYNCHRONIZED;
  1184. if (METHOD_NATIVE (decl))
  1185. access_flags |= ACC_NATIVE;
  1186. if (METHOD_ABSTRACT (decl))
  1187. access_flags |= ACC_ABSTRACT;
  1188. if (METHOD_STRICTFP (decl))
  1189. access_flags |= ACC_STRICT;
  1190. if (METHOD_INVISIBLE (decl))
  1191. access_flags |= ACC_INVISIBLE;
  1192. if (DECL_ARTIFICIAL (decl))
  1193. access_flags |= ACC_SYNTHETIC;
  1194. if (METHOD_BRIDGE (decl))
  1195. access_flags |= ACC_BRIDGE;
  1196. if (METHOD_VARARGS (decl))
  1197. access_flags |= ACC_VARARGS;
  1198. return access_flags;
  1199. }
  1200. gcc_unreachable ();
  1201. }
  1202. static GTY (()) int alias_labelno = 0;
  1203. /* Create a private alias for METHOD. Using this alias instead of the method
  1204. decl ensures that ncode entries in the method table point to the real function
  1205. at runtime, not a PLT entry. */
  1206. static tree
  1207. make_local_function_alias (tree method)
  1208. {
  1209. #ifdef ASM_OUTPUT_DEF
  1210. tree alias;
  1211. const char *method_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method));
  1212. char *name = (char *) alloca (strlen (method_name) + 2);
  1213. char *buf = (char *) alloca (strlen (method_name) + 128);
  1214. /* Only create aliases for local functions. */
  1215. if (DECL_EXTERNAL (method))
  1216. return method;
  1217. /* Prefix method_name with 'L' for the alias label. */
  1218. *name = 'L';
  1219. strcpy (name + 1, method_name);
  1220. targetm.asm_out.generate_internal_label (buf, name, alias_labelno++);
  1221. alias = build_decl (input_location,
  1222. FUNCTION_DECL, get_identifier (buf),
  1223. TREE_TYPE (method));
  1224. DECL_CONTEXT (alias) = NULL;
  1225. TREE_READONLY (alias) = TREE_READONLY (method);
  1226. TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (method);
  1227. TREE_PUBLIC (alias) = 0;
  1228. DECL_EXTERNAL (alias) = 0;
  1229. DECL_ARTIFICIAL (alias) = 1;
  1230. DECL_INITIAL (alias) = error_mark_node;
  1231. TREE_ADDRESSABLE (alias) = 1;
  1232. TREE_USED (alias) = 1;
  1233. if (!flag_syntax_only)
  1234. assemble_alias (alias, DECL_ASSEMBLER_NAME (method));
  1235. return alias;
  1236. #else
  1237. return method;
  1238. #endif
  1239. }
  1240. /** Make reflection data (_Jv_Field) for field FDECL. */
  1241. static tree
  1242. make_field_value (tree fdecl)
  1243. {
  1244. tree finit;
  1245. int flags;
  1246. tree type = TREE_TYPE (fdecl);
  1247. int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
  1248. vec<constructor_elt, va_gc> *v = NULL;
  1249. START_RECORD_CONSTRUCTOR (v, field_type_node);
  1250. PUSH_FIELD_VALUE (v, "name", build_utf8_ref (DECL_NAME (fdecl)));
  1251. if (resolved)
  1252. type = build_class_ref (type);
  1253. else
  1254. {
  1255. tree signature = build_java_signature (type);
  1256. type = build_utf8_ref (unmangle_classname
  1257. (IDENTIFIER_POINTER (signature),
  1258. IDENTIFIER_LENGTH (signature)));
  1259. }
  1260. PUSH_FIELD_VALUE (v, "type", type);
  1261. flags = get_access_flags_from_decl (fdecl);
  1262. if (! resolved)
  1263. flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
  1264. PUSH_FIELD_VALUE (v, "accflags", build_int_cst (NULL_TREE, flags));
  1265. PUSH_FIELD_VALUE (v, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
  1266. {
  1267. tree field_address = integer_zero_node;
  1268. tree index, value;
  1269. if ((DECL_INITIAL (fdecl) || ! flag_indirect_classes)
  1270. && FIELD_STATIC (fdecl))
  1271. field_address = build_address_of (fdecl);
  1272. index = (FIELD_STATIC (fdecl)
  1273. ? DECL_CHAIN (TYPE_FIELDS (field_info_union_node))
  1274. : TYPE_FIELDS (field_info_union_node));
  1275. value = (FIELD_STATIC (fdecl)
  1276. ? field_address
  1277. : byte_position (fdecl));
  1278. PUSH_FIELD_VALUE
  1279. (v, "info",
  1280. build_constructor_single (field_info_union_node, index, value));
  1281. }
  1282. FINISH_RECORD_CONSTRUCTOR (finit, v, field_type_node);
  1283. return finit;
  1284. }
  1285. /** Make reflection data (_Jv_Method) for method MDECL. */
  1286. static tree
  1287. make_method_value (tree mdecl)
  1288. {
  1289. static int method_name_count = 0;
  1290. tree minit;
  1291. tree index;
  1292. tree code;
  1293. tree class_decl;
  1294. #define ACC_TRANSLATED 0x4000
  1295. int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
  1296. vec<constructor_elt, va_gc> *v = NULL;
  1297. class_decl = DECL_CONTEXT (mdecl);
  1298. /* For interfaces, the index field contains the dispatch index. */
  1299. if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
  1300. index = build_int_cst (NULL_TREE,
  1301. get_interface_method_index (mdecl, class_decl));
  1302. else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
  1303. index = get_method_index (mdecl);
  1304. else
  1305. index = integer_minus_one_node;
  1306. code = null_pointer_node;
  1307. if (METHOD_ABSTRACT (mdecl))
  1308. code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
  1309. soft_abstractmethod_node);
  1310. else
  1311. code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
  1312. make_local_function_alias (mdecl));
  1313. START_RECORD_CONSTRUCTOR (v, method_type_node);
  1314. PUSH_FIELD_VALUE (v, "name",
  1315. build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
  1316. init_identifier_node
  1317. : DECL_NAME (mdecl)));
  1318. {
  1319. tree signature = build_java_signature (TREE_TYPE (mdecl));
  1320. PUSH_FIELD_VALUE (v, "signature",
  1321. (build_utf8_ref
  1322. (unmangle_classname
  1323. (IDENTIFIER_POINTER(signature),
  1324. IDENTIFIER_LENGTH(signature)))));
  1325. }
  1326. PUSH_FIELD_VALUE (v, "accflags", build_int_cst (NULL_TREE, accflags));
  1327. PUSH_FIELD_VALUE (v, "index", index);
  1328. PUSH_FIELD_VALUE (v, "ncode", code);
  1329. {
  1330. /* Compute the `throws' information for the method. */
  1331. tree table = null_pointer_node;
  1332. if (!vec_safe_is_empty (DECL_FUNCTION_THROWS (mdecl)))
  1333. {
  1334. int length = 1 + DECL_FUNCTION_THROWS (mdecl)->length ();
  1335. tree t, type, array;
  1336. char buf[60];
  1337. vec<constructor_elt, va_gc> *v = NULL;
  1338. int idx = length - 1;
  1339. unsigned ix;
  1340. constructor_elt *e;
  1341. vec_alloc (v, length);
  1342. v->quick_grow_cleared (length);
  1343. e = &(*v)[idx--];
  1344. e->value = null_pointer_node;
  1345. FOR_EACH_VEC_SAFE_ELT (DECL_FUNCTION_THROWS (mdecl), ix, t)
  1346. {
  1347. tree sig = DECL_NAME (TYPE_NAME (t));
  1348. tree utf8
  1349. = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
  1350. IDENTIFIER_LENGTH (sig)));
  1351. e = &(*v)[idx--];
  1352. e->value = utf8;
  1353. }
  1354. gcc_assert (idx == -1);
  1355. type = build_prim_array_type (ptr_type_node, length);
  1356. table = build_constructor (type, v);
  1357. /* Compute something unique enough. */
  1358. sprintf (buf, "_methods%d", method_name_count++);
  1359. array = build_decl (input_location,
  1360. VAR_DECL, get_identifier (buf), type);
  1361. DECL_INITIAL (array) = table;
  1362. TREE_STATIC (array) = 1;
  1363. DECL_ARTIFICIAL (array) = 1;
  1364. DECL_IGNORED_P (array) = 1;
  1365. rest_of_decl_compilation (array, 1, 0);
  1366. table = build1 (ADDR_EXPR, ptr_type_node, array);
  1367. }
  1368. PUSH_FIELD_VALUE (v, "throws", table);
  1369. }
  1370. FINISH_RECORD_CONSTRUCTOR (minit, v, method_type_node);
  1371. return minit;
  1372. }
  1373. static tree
  1374. get_dispatch_vector (tree type)
  1375. {
  1376. tree vtable = TYPE_VTABLE (type);
  1377. if (vtable == NULL_TREE)
  1378. {
  1379. HOST_WIDE_INT i;
  1380. tree method;
  1381. tree super = CLASSTYPE_SUPER (type);
  1382. HOST_WIDE_INT nvirtuals = tree_to_shwi (TYPE_NVIRTUALS (type));
  1383. vtable = make_tree_vec (nvirtuals);
  1384. TYPE_VTABLE (type) = vtable;
  1385. if (super != NULL_TREE)
  1386. {
  1387. tree super_vtable = get_dispatch_vector (super);
  1388. for (i = tree_to_shwi (TYPE_NVIRTUALS (super)); --i >= 0; )
  1389. TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
  1390. }
  1391. for (method = TYPE_METHODS (type); method != NULL_TREE;
  1392. method = DECL_CHAIN (method))
  1393. {
  1394. tree method_index = get_method_index (method);
  1395. if (method_index != NULL_TREE
  1396. && tree_fits_shwi_p (method_index))
  1397. TREE_VEC_ELT (vtable, tree_to_shwi (method_index)) = method;
  1398. }
  1399. }
  1400. return vtable;
  1401. }
  1402. static tree
  1403. get_dispatch_table (tree type, tree this_class_addr)
  1404. {
  1405. int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
  1406. tree vtable = get_dispatch_vector (type);
  1407. int i, j;
  1408. int nvirtuals = TREE_VEC_LENGTH (vtable);
  1409. int arraysize;
  1410. tree gc_descr;
  1411. vec<constructor_elt, va_gc> *v = NULL;
  1412. constructor_elt *e;
  1413. tree arraytype;
  1414. arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
  1415. if (TARGET_VTABLE_USES_DESCRIPTORS)
  1416. arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
  1417. arraysize += 2;
  1418. vec_safe_grow_cleared (v, arraysize);
  1419. e = &(*v)[arraysize - 1];
  1420. #define CONSTRUCTOR_PREPEND_VALUE(E, V) E->value = V, E--
  1421. for (i = nvirtuals; --i >= 0; )
  1422. {
  1423. tree method = TREE_VEC_ELT (vtable, i);
  1424. if (METHOD_ABSTRACT (method))
  1425. {
  1426. if (! abstract_p)
  1427. warning_at (DECL_SOURCE_LOCATION (method), 0,
  1428. "abstract method in non-abstract class");
  1429. if (TARGET_VTABLE_USES_DESCRIPTORS)
  1430. for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
  1431. CONSTRUCTOR_PREPEND_VALUE (e, null_pointer_node);
  1432. else
  1433. CONSTRUCTOR_PREPEND_VALUE (e, null_pointer_node);
  1434. }
  1435. else
  1436. {
  1437. if (TARGET_VTABLE_USES_DESCRIPTORS)
  1438. for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
  1439. {
  1440. tree fdesc = build2 (FDESC_EXPR, nativecode_ptr_type_node,
  1441. method, build_int_cst (NULL_TREE, j));
  1442. TREE_CONSTANT (fdesc) = 1;
  1443. CONSTRUCTOR_PREPEND_VALUE (e, fdesc);
  1444. }
  1445. else
  1446. CONSTRUCTOR_PREPEND_VALUE (e,
  1447. build1 (ADDR_EXPR,
  1448. nativecode_ptr_type_node,
  1449. method));
  1450. }
  1451. }
  1452. /* Dummy entry for compatibility with G++ -fvtable-thunks. When
  1453. using the Boehm GC we sometimes stash a GC type descriptor
  1454. there. We set the PURPOSE to NULL_TREE not to interfere (reset)
  1455. the emitted byte count during the output to the assembly file. */
  1456. /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
  1457. fake "function descriptor". It's first word is the is the class
  1458. pointer, and subsequent words (usually one) contain the GC descriptor.
  1459. In all other cases, we reserve two extra vtable slots. */
  1460. gc_descr = get_boehm_type_descriptor (type);
  1461. CONSTRUCTOR_PREPEND_VALUE (e, gc_descr);
  1462. for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
  1463. CONSTRUCTOR_PREPEND_VALUE (e, gc_descr);
  1464. CONSTRUCTOR_PREPEND_VALUE (e, this_class_addr);
  1465. /** Pointer to type_info object (to be implemented), according to g++ ABI. */
  1466. CONSTRUCTOR_PREPEND_VALUE (e, null_pointer_node);
  1467. /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
  1468. gcc_assert (e == v->address ());
  1469. e->index = integer_zero_node;
  1470. e->value = null_pointer_node;
  1471. #undef CONSTRUCTOR_PREPEND_VALUE
  1472. arraytype = build_prim_array_type (nativecode_ptr_type_node, arraysize);
  1473. return build_constructor (arraytype, v);
  1474. }
  1475. /* Set the method_index for a method decl. */
  1476. void
  1477. set_method_index (tree decl, tree method_index)
  1478. {
  1479. if (method_index != NULL_TREE)
  1480. {
  1481. /* method_index is null if we're using indirect dispatch. */
  1482. method_index = fold (convert (sizetype, method_index));
  1483. if (TARGET_VTABLE_USES_DESCRIPTORS)
  1484. /* Add one to skip bogus descriptor for class and GC descriptor. */
  1485. method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
  1486. else
  1487. /* Add 1 to skip "class" field of dtable, and 1 to skip GC
  1488. descriptor. */
  1489. method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
  1490. }
  1491. DECL_VINDEX (decl) = method_index;
  1492. }
  1493. /* Get the method_index for a method decl. */
  1494. tree
  1495. get_method_index (tree decl)
  1496. {
  1497. tree method_index = DECL_VINDEX (decl);
  1498. if (! method_index)
  1499. return NULL;
  1500. if (TARGET_VTABLE_USES_DESCRIPTORS)
  1501. /* Sub one to skip bogus descriptor for class and GC descriptor. */
  1502. method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
  1503. else
  1504. /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor. */
  1505. method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
  1506. return method_index;
  1507. }
  1508. static int
  1509. supers_all_compiled (tree type)
  1510. {
  1511. while (type != NULL_TREE)
  1512. {
  1513. if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
  1514. return 0;
  1515. type = CLASSTYPE_SUPER (type);
  1516. }
  1517. return 1;
  1518. }
  1519. static void
  1520. add_table_and_syms (vec<constructor_elt, va_gc> **v,
  1521. vec<method_entry, va_gc> *methods,
  1522. const char *table_name, tree table_slot, tree table_type,
  1523. const char *syms_name, tree syms_slot)
  1524. {
  1525. if (methods == NULL)
  1526. {
  1527. PUSH_FIELD_VALUE (*v, table_name, null_pointer_node);
  1528. PUSH_FIELD_VALUE (*v, syms_name, null_pointer_node);
  1529. }
  1530. else
  1531. {
  1532. pushdecl_top_level (syms_slot);
  1533. PUSH_FIELD_VALUE (*v, table_name,
  1534. build1 (ADDR_EXPR, table_type, table_slot));
  1535. PUSH_FIELD_VALUE (*v, syms_name,
  1536. build1 (ADDR_EXPR, symbols_array_ptr_type,
  1537. syms_slot));
  1538. TREE_CONSTANT (table_slot) = 1;
  1539. }
  1540. }
  1541. void
  1542. make_class_data (tree type)
  1543. {
  1544. tree decl, cons, temp;
  1545. tree field, fields_decl;
  1546. HOST_WIDE_INT static_field_count = 0;
  1547. HOST_WIDE_INT instance_field_count = 0;
  1548. HOST_WIDE_INT field_count;
  1549. tree field_array_type;
  1550. tree method;
  1551. tree dtable_decl = NULL_TREE;
  1552. HOST_WIDE_INT method_count = 0;
  1553. tree method_array_type;
  1554. tree methods_decl;
  1555. tree super;
  1556. tree this_class_addr;
  1557. tree constant_pool_constructor;
  1558. tree interfaces = null_pointer_node;
  1559. int interface_len = 0;
  1560. int uses_jv_markobj = 0;
  1561. tree type_decl = TYPE_NAME (type);
  1562. tree id_main = get_identifier("main");
  1563. tree id_class = get_identifier("java.lang.Class");
  1564. /** Offset from start of virtual function table declaration
  1565. to where objects actually point at, following new g++ ABI. */
  1566. tree dtable_start_offset = size_int (2 * POINTER_SIZE / BITS_PER_UNIT);
  1567. vec<int> field_indexes;
  1568. tree first_real_field;
  1569. vec<constructor_elt, va_gc> *v1 = NULL, *v2 = NULL;
  1570. tree reflection_data;
  1571. vec<constructor_elt, va_gc> *static_fields = NULL;
  1572. vec<constructor_elt, va_gc> *instance_fields = NULL;
  1573. vec<constructor_elt, va_gc> *methods = NULL;
  1574. this_class_addr = build_static_class_ref (type);
  1575. decl = TREE_OPERAND (this_class_addr, 0);
  1576. if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
  1577. && !flag_indirect_dispatch)
  1578. {
  1579. tree dtable = get_dispatch_table (type, this_class_addr);
  1580. uses_jv_markobj = uses_jv_markobj_p (dtable);
  1581. if (type == class_type_node && class_dtable_decl != NULL_TREE)
  1582. {
  1583. /* We've already created some other class, and consequently
  1584. we made class_dtable_decl. Now we just want to fill it
  1585. in. */
  1586. dtable_decl = class_dtable_decl;
  1587. }
  1588. else
  1589. {
  1590. dtable_decl = build_dtable_decl (type);
  1591. TREE_STATIC (dtable_decl) = 1;
  1592. DECL_ARTIFICIAL (dtable_decl) = 1;
  1593. DECL_IGNORED_P (dtable_decl) = 1;
  1594. }
  1595. TREE_PUBLIC (dtable_decl) = 1;
  1596. DECL_INITIAL (dtable_decl) = dtable;
  1597. /* The only dispatch table exported from a DSO is the dispatch
  1598. table for java.lang.Class. */
  1599. if (DECL_NAME (type_decl) != id_class)
  1600. java_hide_decl (dtable_decl);
  1601. if (! flag_indirect_classes)
  1602. rest_of_decl_compilation (dtable_decl, 1, 0);
  1603. /* Maybe we're compiling Class as the first class. If so, set
  1604. class_dtable_decl to the decl we just made. */
  1605. if (type == class_type_node && class_dtable_decl == NULL_TREE)
  1606. class_dtable_decl = dtable_decl;
  1607. }
  1608. /* Build Field array. */
  1609. field = TYPE_FIELDS (type);
  1610. while (field && DECL_ARTIFICIAL (field))
  1611. field = DECL_CHAIN (field); /* Skip dummy fields. */
  1612. if (field && DECL_NAME (field) == NULL_TREE)
  1613. field = DECL_CHAIN (field); /* Skip dummy field for inherited data. */
  1614. first_real_field = field;
  1615. /* First count static and instance fields. */
  1616. for ( ; field != NULL_TREE; field = DECL_CHAIN (field))
  1617. {
  1618. if (! DECL_ARTIFICIAL (field))
  1619. {
  1620. if (FIELD_STATIC (field))
  1621. static_field_count++;
  1622. else if (uses_jv_markobj || !flag_reduced_reflection)
  1623. instance_field_count++;
  1624. }
  1625. }
  1626. field_count = static_field_count + instance_field_count;
  1627. field_indexes.create (field_count);
  1628. /* gcj sorts fields so that static fields come first, followed by
  1629. instance fields. Unfortunately, by the time this takes place we
  1630. have already generated the reflection_data for this class, and
  1631. that data contains indexes into the fields. So, we generate a
  1632. permutation that maps each original field index to its final
  1633. position. Then we pass this permutation to
  1634. rewrite_reflection_indexes(), which fixes up the reflection
  1635. data. */
  1636. {
  1637. int i;
  1638. int static_count = 0;
  1639. int instance_count = static_field_count;
  1640. int field_index;
  1641. for (i = 0, field = first_real_field;
  1642. field != NULL_TREE;
  1643. field = DECL_CHAIN (field), i++)
  1644. {
  1645. if (! DECL_ARTIFICIAL (field))
  1646. {
  1647. field_index = 0;
  1648. if (FIELD_STATIC (field))
  1649. field_index = static_count++;
  1650. else if (uses_jv_markobj || !flag_reduced_reflection)
  1651. field_index = instance_count++;
  1652. else
  1653. continue;
  1654. field_indexes.quick_push (field_index);
  1655. }
  1656. }
  1657. }
  1658. for (field = first_real_field; field != NULL_TREE;
  1659. field = DECL_CHAIN (field))
  1660. {
  1661. if (! DECL_ARTIFICIAL (field))
  1662. {
  1663. if (FIELD_STATIC (field))
  1664. {
  1665. /* We must always create reflection data for static fields
  1666. as it is used in the creation of the field itself. */
  1667. tree init = make_field_value (field);
  1668. tree initial = DECL_INITIAL (field);
  1669. CONSTRUCTOR_APPEND_ELT (static_fields, NULL_TREE, init);
  1670. /* If the initial value is a string constant,
  1671. prevent output_constant from trying to assemble the value. */
  1672. if (initial != NULL_TREE
  1673. && TREE_TYPE (initial) == string_ptr_type_node)
  1674. DECL_INITIAL (field) = NULL_TREE;
  1675. rest_of_decl_compilation (field, 1, 1);
  1676. DECL_INITIAL (field) = initial;
  1677. }
  1678. else if (uses_jv_markobj || !flag_reduced_reflection)
  1679. {
  1680. tree init = make_field_value (field);
  1681. CONSTRUCTOR_APPEND_ELT (instance_fields, NULL_TREE, init);
  1682. }
  1683. }
  1684. }
  1685. gcc_assert (static_field_count == (int) vec_safe_length (static_fields));
  1686. gcc_assert (instance_field_count == (int) vec_safe_length (instance_fields));
  1687. if (field_count > 0)
  1688. {
  1689. vec_safe_splice (static_fields, instance_fields);
  1690. field_array_type = build_prim_array_type (field_type_node, field_count);
  1691. fields_decl = build_decl (input_location,
  1692. VAR_DECL, mangled_classname ("_FL_", type),
  1693. field_array_type);
  1694. DECL_INITIAL (fields_decl)
  1695. = build_constructor (field_array_type, static_fields);
  1696. TREE_STATIC (fields_decl) = 1;
  1697. DECL_ARTIFICIAL (fields_decl) = 1;
  1698. DECL_IGNORED_P (fields_decl) = 1;
  1699. rest_of_decl_compilation (fields_decl, 1, 0);
  1700. }
  1701. else
  1702. fields_decl = NULL_TREE;
  1703. /* Build Method array. */
  1704. for (method = TYPE_METHODS (type);
  1705. method != NULL_TREE; method = DECL_CHAIN (method))
  1706. {
  1707. tree init;
  1708. if (METHOD_PRIVATE (method)
  1709. && ! flag_keep_inline_functions
  1710. && optimize)
  1711. continue;
  1712. /* Even if we have a decl, we don't necessarily have the code.
  1713. This can happen if we inherit a method from a superclass for
  1714. which we don't have a .class file. */
  1715. if (METHOD_DUMMY (method))
  1716. continue;
  1717. /* Generate method reflection data if:
  1718. - !flag_reduced_reflection.
  1719. - <clinit> -- The runtime uses reflection to initialize the
  1720. class.
  1721. - Any method in class java.lang.Class -- Class.forName() and
  1722. perhaps other things require it.
  1723. - class$ -- It does not work if reflection data missing.
  1724. - main -- Reflection is used to find main(String[]) methods.
  1725. - public not static -- It is potentially part of an
  1726. interface. The runtime uses reflection data to build
  1727. interface dispatch tables. */
  1728. if (!flag_reduced_reflection
  1729. || DECL_CLINIT_P (method)
  1730. || DECL_NAME (type_decl) == id_class
  1731. || DECL_NAME (method) == id_main
  1732. || (METHOD_PUBLIC (method) && !METHOD_STATIC (method)))
  1733. {
  1734. init = make_method_value (method);
  1735. method_count++;
  1736. CONSTRUCTOR_APPEND_ELT (methods, NULL_TREE, init);
  1737. }
  1738. }
  1739. method_array_type = build_prim_array_type (method_type_node, method_count);
  1740. methods_decl = build_decl (input_location,
  1741. VAR_DECL, mangled_classname ("_MT_", type),
  1742. method_array_type);
  1743. DECL_INITIAL (methods_decl) = build_constructor (method_array_type, methods);
  1744. TREE_STATIC (methods_decl) = 1;
  1745. DECL_ARTIFICIAL (methods_decl) = 1;
  1746. DECL_IGNORED_P (methods_decl) = 1;
  1747. rest_of_decl_compilation (methods_decl, 1, 0);
  1748. if (class_dtable_decl == NULL_TREE)
  1749. {
  1750. class_dtable_decl = build_dtable_decl (class_type_node);
  1751. TREE_STATIC (class_dtable_decl) = 1;
  1752. DECL_ARTIFICIAL (class_dtable_decl) = 1;
  1753. DECL_IGNORED_P (class_dtable_decl) = 1;
  1754. if (is_compiled_class (class_type_node) != 2)
  1755. {
  1756. DECL_EXTERNAL (class_dtable_decl) = 1;
  1757. rest_of_decl_compilation (class_dtable_decl, 1, 0);
  1758. }
  1759. }
  1760. super = CLASSTYPE_SUPER (type);
  1761. if (super == NULL_TREE)
  1762. super = null_pointer_node;
  1763. else if (! flag_indirect_dispatch
  1764. && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
  1765. && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
  1766. super = build_class_ref (super);
  1767. else
  1768. {
  1769. int super_index = alloc_class_constant (super);
  1770. super = build_int_cst (ptr_type_node, super_index);
  1771. }
  1772. /* Build and emit the array of implemented interfaces. */
  1773. if (type != object_type_node)
  1774. interface_len = BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) - 1;
  1775. if (interface_len > 0)
  1776. {
  1777. int i;
  1778. tree interface_array_type, idecl;
  1779. vec<constructor_elt, va_gc> *init;
  1780. vec_alloc (init, interface_len);
  1781. interface_array_type
  1782. = build_prim_array_type (class_ptr_type, interface_len);
  1783. idecl = build_decl (input_location,
  1784. VAR_DECL, mangled_classname ("_IF_", type),
  1785. interface_array_type);
  1786. for (i = 1; i <= interface_len; i++)
  1787. {
  1788. tree child = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
  1789. tree iclass = BINFO_TYPE (child);
  1790. tree index;
  1791. if (! flag_indirect_dispatch
  1792. && (assume_compiled
  1793. (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
  1794. index = build_class_ref (iclass);
  1795. else
  1796. {
  1797. int int_index = alloc_class_constant (iclass);
  1798. index = build_int_cst (ptr_type_node, int_index);
  1799. }
  1800. CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, index);
  1801. }
  1802. DECL_INITIAL (idecl) = build_constructor (interface_array_type, init);
  1803. TREE_STATIC (idecl) = 1;
  1804. DECL_ARTIFICIAL (idecl) = 1;
  1805. DECL_IGNORED_P (idecl) = 1;
  1806. interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
  1807. rest_of_decl_compilation (idecl, 1, 0);
  1808. }
  1809. constant_pool_constructor = build_constants_constructor ();
  1810. if (flag_indirect_dispatch)
  1811. {
  1812. TYPE_OTABLE_DECL (type)
  1813. = emit_symbol_table
  1814. (DECL_NAME (TYPE_OTABLE_DECL (type)),
  1815. TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type),
  1816. TYPE_OTABLE_SYMS_DECL (type), integer_type_node, 1);
  1817. TYPE_ATABLE_DECL (type)
  1818. = emit_symbol_table
  1819. (DECL_NAME (TYPE_ATABLE_DECL (type)),
  1820. TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type),
  1821. TYPE_ATABLE_SYMS_DECL (type), ptr_type_node, 1);
  1822. TYPE_ITABLE_DECL (type)
  1823. = emit_symbol_table
  1824. (DECL_NAME (TYPE_ITABLE_DECL (type)),
  1825. TYPE_ITABLE_DECL (type), TYPE_ITABLE_METHODS (type),
  1826. TYPE_ITABLE_SYMS_DECL (type), ptr_type_node, 2);
  1827. }
  1828. TYPE_CTABLE_DECL (type) = emit_catch_table (type);
  1829. START_RECORD_CONSTRUCTOR (v1, object_type_node);
  1830. PUSH_FIELD_VALUE (v1, "vtable",
  1831. (flag_indirect_classes
  1832. ? null_pointer_node
  1833. : fold_build_pointer_plus
  1834. (build1 (ADDR_EXPR, dtable_ptr_type,
  1835. class_dtable_decl),
  1836. dtable_start_offset)));
  1837. if (! flag_hash_synchronization)
  1838. PUSH_FIELD_VALUE (v1, "sync_info", null_pointer_node);
  1839. FINISH_RECORD_CONSTRUCTOR (temp, v1, object_type_node);
  1840. START_RECORD_CONSTRUCTOR (v2, class_type_node);
  1841. PUSH_SUPER_VALUE (v2, temp);
  1842. PUSH_FIELD_VALUE (v2, "next_or_version", gcj_abi_version);
  1843. PUSH_FIELD_VALUE (v2, "name", build_utf8_ref (DECL_NAME (type_decl)));
  1844. PUSH_FIELD_VALUE (v2, "accflags",
  1845. build_int_cst (NULL_TREE,
  1846. get_access_flags_from_decl (type_decl)));
  1847. PUSH_FIELD_VALUE (v2, "superclass",
  1848. CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
  1849. PUSH_FIELD_VALUE (v2, "constants", constant_pool_constructor);
  1850. PUSH_FIELD_VALUE (v2, "methods",
  1851. methods_decl == NULL_TREE ? null_pointer_node
  1852. : build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
  1853. PUSH_FIELD_VALUE (v2, "method_count",
  1854. build_int_cst (NULL_TREE, method_count));
  1855. PUSH_FIELD_VALUE (v2, "vtable_method_count",
  1856. (flag_indirect_dispatch
  1857. ? integer_minus_one_node
  1858. : TYPE_NVIRTUALS (type)));
  1859. PUSH_FIELD_VALUE (v2, "fields",
  1860. fields_decl == NULL_TREE ? null_pointer_node
  1861. : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
  1862. /* If we're using the binary compatibility ABI we don't know the
  1863. size until load time. */
  1864. PUSH_FIELD_VALUE (v2, "size_in_bytes",
  1865. (flag_indirect_dispatch
  1866. ? integer_minus_one_node
  1867. : size_in_bytes (type)));
  1868. PUSH_FIELD_VALUE (v2, "field_count",
  1869. build_int_cst (NULL_TREE, field_count));
  1870. PUSH_FIELD_VALUE (v2, "static_field_count",
  1871. build_int_cst (NULL_TREE, static_field_count));
  1872. PUSH_FIELD_VALUE (v2, "vtable",
  1873. (flag_indirect_dispatch || dtable_decl == NULL_TREE
  1874. ? null_pointer_node
  1875. : fold_build_pointer_plus
  1876. (build1 (ADDR_EXPR, dtable_ptr_type,
  1877. dtable_decl),
  1878. dtable_start_offset)));
  1879. add_table_and_syms (&v2, TYPE_OTABLE_METHODS (type),
  1880. "otable", TYPE_OTABLE_DECL (type), otable_ptr_type,
  1881. "otable_syms", TYPE_OTABLE_SYMS_DECL (type));
  1882. add_table_and_syms (&v2, TYPE_ATABLE_METHODS (type),
  1883. "atable", TYPE_ATABLE_DECL (type), atable_ptr_type,
  1884. "atable_syms", TYPE_ATABLE_SYMS_DECL (type));
  1885. add_table_and_syms (&v2, TYPE_ITABLE_METHODS (type),
  1886. "itable", TYPE_ITABLE_DECL (type), itable_ptr_type,
  1887. "itable_syms", TYPE_ITABLE_SYMS_DECL (type));
  1888. PUSH_FIELD_VALUE (v2, "catch_classes",
  1889. build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type)));
  1890. PUSH_FIELD_VALUE (v2, "interfaces", interfaces);
  1891. PUSH_FIELD_VALUE (v2, "loader", null_pointer_node);
  1892. PUSH_FIELD_VALUE (v2, "interface_count",
  1893. build_int_cst (NULL_TREE, interface_len));
  1894. PUSH_FIELD_VALUE (v2, "state",
  1895. convert (byte_type_node,
  1896. build_int_cst (NULL_TREE, JV_STATE_PRELOADING)));
  1897. PUSH_FIELD_VALUE (v2, "thread", null_pointer_node);
  1898. PUSH_FIELD_VALUE (v2, "depth", integer_zero_node);
  1899. PUSH_FIELD_VALUE (v2, "ancestors", null_pointer_node);
  1900. PUSH_FIELD_VALUE (v2, "idt", null_pointer_node);
  1901. PUSH_FIELD_VALUE (v2, "arrayclass", null_pointer_node);
  1902. PUSH_FIELD_VALUE (v2, "protectionDomain", null_pointer_node);
  1903. {
  1904. tree assertion_table_ref;
  1905. if (TYPE_ASSERTIONS (type) == NULL)
  1906. assertion_table_ref = null_pointer_node;
  1907. else
  1908. assertion_table_ref = build1 (ADDR_EXPR,
  1909. build_pointer_type (assertion_table_type),
  1910. emit_assertion_table (type));
  1911. PUSH_FIELD_VALUE (v2, "assertion_table", assertion_table_ref);
  1912. }
  1913. PUSH_FIELD_VALUE (v2, "hack_signers", null_pointer_node);
  1914. PUSH_FIELD_VALUE (v2, "chain", null_pointer_node);
  1915. PUSH_FIELD_VALUE (v2, "aux_info", null_pointer_node);
  1916. PUSH_FIELD_VALUE (v2, "engine", null_pointer_node);
  1917. if (TYPE_REFLECTION_DATA (current_class))
  1918. {
  1919. int i;
  1920. int count = TYPE_REFLECTION_DATASIZE (current_class);
  1921. vec<constructor_elt, va_gc> *v;
  1922. vec_alloc (v, count);
  1923. unsigned char *data = TYPE_REFLECTION_DATA (current_class);
  1924. tree max_index = build_int_cst (sizetype, count);
  1925. tree index = build_index_type (max_index);
  1926. tree type = build_array_type (unsigned_byte_type_node, index);
  1927. char buf[64];
  1928. tree array;
  1929. static int reflection_data_count;
  1930. sprintf (buf, "_reflection_data_%d", reflection_data_count++);
  1931. array = build_decl (input_location,
  1932. VAR_DECL, get_identifier (buf), type);
  1933. rewrite_reflection_indexes (&field_indexes);
  1934. for (i = 0; i < count; i++)
  1935. {
  1936. constructor_elt elt;
  1937. elt.index = build_int_cst (sizetype, i);
  1938. elt.value = build_int_cstu (byte_type_node, data[i]);
  1939. v->quick_push (elt);
  1940. }
  1941. DECL_INITIAL (array) = build_constructor (type, v);
  1942. TREE_STATIC (array) = 1;
  1943. DECL_ARTIFICIAL (array) = 1;
  1944. DECL_IGNORED_P (array) = 1;
  1945. TREE_READONLY (array) = 1;
  1946. TREE_CONSTANT (DECL_INITIAL (array)) = 1;
  1947. rest_of_decl_compilation (array, 1, 0);
  1948. reflection_data = build_address_of (array);
  1949. free (data);
  1950. TYPE_REFLECTION_DATA (current_class) = NULL;
  1951. }
  1952. else
  1953. reflection_data = null_pointer_node;
  1954. PUSH_FIELD_VALUE (v2, "reflection_data", reflection_data);
  1955. FINISH_RECORD_CONSTRUCTOR (cons, v2, class_type_node);
  1956. DECL_INITIAL (decl) = cons;
  1957. /* Hash synchronization requires at least 64-bit alignment. */
  1958. if (flag_hash_synchronization && POINTER_SIZE < 64)
  1959. DECL_ALIGN (decl) = 64;
  1960. if (flag_indirect_classes)
  1961. {
  1962. TREE_READONLY (decl) = 1;
  1963. TREE_CONSTANT (DECL_INITIAL (decl)) = 1;
  1964. }
  1965. rest_of_decl_compilation (decl, 1, 0);
  1966. {
  1967. tree classdollar_field = build_classdollar_field (type);
  1968. if (!flag_indirect_classes)
  1969. DECL_INITIAL (classdollar_field) = build_static_class_ref (type);
  1970. rest_of_decl_compilation (classdollar_field, 1, 0);
  1971. }
  1972. TYPE_OTABLE_DECL (type) = NULL_TREE;
  1973. TYPE_ATABLE_DECL (type) = NULL_TREE;
  1974. TYPE_CTABLE_DECL (type) = NULL_TREE;
  1975. }
  1976. void
  1977. finish_class (void)
  1978. {
  1979. java_expand_catch_classes (current_class);
  1980. current_function_decl = NULL_TREE;
  1981. TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (current_class)) = 0;
  1982. make_class_data (current_class);
  1983. register_class ();
  1984. rest_of_decl_compilation (TYPE_NAME (current_class), 1, 0);
  1985. }
  1986. /* Return 2 if KLASS is compiled by this compilation job;
  1987. return 1 if KLASS can otherwise be assumed to be compiled;
  1988. return 0 if we cannot assume that KLASS is compiled.
  1989. Returns 1 for primitive and 0 for array types. */
  1990. int
  1991. is_compiled_class (tree klass)
  1992. {
  1993. int seen_in_zip;
  1994. if (TREE_CODE (klass) == POINTER_TYPE)
  1995. klass = TREE_TYPE (klass);
  1996. if (TREE_CODE (klass) != RECORD_TYPE) /* Primitive types are static. */
  1997. return 1;
  1998. if (TYPE_ARRAY_P (klass))
  1999. return 0;
  2000. seen_in_zip = (TYPE_JCF (klass) && JCF_SEEN_IN_ZIP (TYPE_JCF (klass)));
  2001. if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
  2002. {
  2003. /* The class was seen in the current ZIP file and will be
  2004. available as a compiled class in the future but may not have
  2005. been loaded already. Load it if necessary. This prevent
  2006. build_class_ref () from crashing. */
  2007. if (seen_in_zip && !CLASS_LOADED_P (klass) && (klass != current_class))
  2008. load_class (klass, 1);
  2009. /* We return 2 for class seen in ZIP and class from files
  2010. belonging to the same compilation unit */
  2011. return 2;
  2012. }
  2013. if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (klass)))))
  2014. {
  2015. if (!CLASS_LOADED_P (klass))
  2016. {
  2017. if (klass != current_class)
  2018. load_class (klass, 1);
  2019. }
  2020. return 1;
  2021. }
  2022. return 0;
  2023. }
  2024. /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
  2025. tree
  2026. build_dtable_decl (tree type)
  2027. {
  2028. tree dtype, decl;
  2029. /* We need to build a new dtable type so that its size is uniquely
  2030. computed when we're dealing with the class for real and not just
  2031. faking it (like java.lang.Class during the initialization of the
  2032. compiler.) We know we're not faking a class when CURRENT_CLASS is
  2033. TYPE. */
  2034. if (current_class == type)
  2035. {
  2036. tree dummy = NULL_TREE;
  2037. int n;
  2038. dtype = make_node (RECORD_TYPE);
  2039. PUSH_FIELD (input_location, dtype, dummy, "top_offset", ptr_type_node);
  2040. PUSH_FIELD (input_location, dtype, dummy, "type_info", ptr_type_node);
  2041. PUSH_FIELD (input_location, dtype, dummy, "class", class_ptr_type);
  2042. for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
  2043. {
  2044. tree tmp_field = build_decl (input_location,
  2045. FIELD_DECL, NULL_TREE, ptr_type_node);
  2046. TREE_CHAIN (dummy) = tmp_field;
  2047. DECL_CONTEXT (tmp_field) = dtype;
  2048. DECL_ARTIFICIAL (tmp_field) = 1;
  2049. dummy = tmp_field;
  2050. }
  2051. PUSH_FIELD (input_location, dtype, dummy, "gc_descr", ptr_type_node);
  2052. for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
  2053. {
  2054. tree tmp_field = build_decl (input_location,
  2055. FIELD_DECL, NULL_TREE, ptr_type_node);
  2056. TREE_CHAIN (dummy) = tmp_field;
  2057. DECL_CONTEXT (tmp_field) = dtype;
  2058. DECL_ARTIFICIAL (tmp_field) = 1;
  2059. dummy = tmp_field;
  2060. }
  2061. n = TREE_VEC_LENGTH (get_dispatch_vector (type));
  2062. if (TARGET_VTABLE_USES_DESCRIPTORS)
  2063. n *= TARGET_VTABLE_USES_DESCRIPTORS;
  2064. PUSH_FIELD (input_location, dtype, dummy, "methods",
  2065. build_prim_array_type (nativecode_ptr_type_node, n));
  2066. layout_type (dtype);
  2067. }
  2068. else
  2069. dtype = dtable_type;
  2070. decl = build_decl (input_location,
  2071. VAR_DECL, get_identifier ("vt$"), dtype);
  2072. DECL_CONTEXT (decl) = type;
  2073. MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
  2074. DECL_VTABLE_P (decl) = 1;
  2075. return decl;
  2076. }
  2077. /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
  2078. fields inherited from SUPER_CLASS. */
  2079. void
  2080. push_super_field (tree this_class, tree super_class)
  2081. {
  2082. tree base_decl;
  2083. /* Don't insert the field if we're just re-laying the class out. */
  2084. if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
  2085. return;
  2086. base_decl = build_decl (input_location,
  2087. FIELD_DECL, NULL_TREE, super_class);
  2088. DECL_IGNORED_P (base_decl) = 1;
  2089. DECL_CHAIN (base_decl) = TYPE_FIELDS (this_class);
  2090. TYPE_FIELDS (this_class) = base_decl;
  2091. DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
  2092. DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
  2093. }
  2094. /* Handle the different manners we may have to lay out a super class. */
  2095. static tree
  2096. maybe_layout_super_class (tree super_class, tree this_class ATTRIBUTE_UNUSED)
  2097. {
  2098. if (!super_class)
  2099. return NULL_TREE;
  2100. else if (TREE_CODE (super_class) == RECORD_TYPE)
  2101. {
  2102. if (!CLASS_LOADED_P (super_class))
  2103. load_class (super_class, 1);
  2104. }
  2105. /* We might have to layout the class before its dependency on
  2106. the super class gets resolved by java_complete_class */
  2107. else if (TREE_CODE (super_class) == POINTER_TYPE)
  2108. {
  2109. if (TREE_TYPE (super_class) != NULL_TREE)
  2110. super_class = TREE_TYPE (super_class);
  2111. else
  2112. gcc_unreachable ();
  2113. }
  2114. if (!TYPE_SIZE (super_class))
  2115. safe_layout_class (super_class);
  2116. return super_class;
  2117. }
  2118. /* safe_layout_class just makes sure that we can load a class without
  2119. disrupting the current_class, input_location, etc, information
  2120. about the class processed currently. */
  2121. void
  2122. safe_layout_class (tree klass)
  2123. {
  2124. tree save_current_class = current_class;
  2125. location_t save_location = input_location;
  2126. layout_class (klass);
  2127. current_class = save_current_class;
  2128. input_location = save_location;
  2129. }
  2130. void
  2131. layout_class (tree this_class)
  2132. {
  2133. int i;
  2134. tree super_class = CLASSTYPE_SUPER (this_class);
  2135. class_list = tree_cons (this_class, NULL_TREE, class_list);
  2136. if (CLASS_BEING_LAIDOUT (this_class))
  2137. {
  2138. char buffer [1024];
  2139. char *report;
  2140. tree current;
  2141. sprintf (buffer, " with '%s'",
  2142. IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
  2143. obstack_grow (&temporary_obstack, buffer, strlen (buffer));
  2144. for (current = TREE_CHAIN (class_list); current;
  2145. current = TREE_CHAIN (current))
  2146. {
  2147. tree decl = TYPE_NAME (TREE_PURPOSE (current));
  2148. sprintf (buffer, "\n which inherits from '%s' (%s:%d)",
  2149. IDENTIFIER_POINTER (DECL_NAME (decl)),
  2150. DECL_SOURCE_FILE (decl),
  2151. DECL_SOURCE_LINE (decl));
  2152. obstack_grow (&temporary_obstack, buffer, strlen (buffer));
  2153. }
  2154. obstack_1grow (&temporary_obstack, '\0');
  2155. report = XOBFINISH (&temporary_obstack, char *);
  2156. cyclic_inheritance_report = ggc_strdup (report);
  2157. obstack_free (&temporary_obstack, report);
  2158. TYPE_SIZE (this_class) = error_mark_node;
  2159. return;
  2160. }
  2161. CLASS_BEING_LAIDOUT (this_class) = 1;
  2162. if (super_class && !CLASS_BEING_LAIDOUT (super_class))
  2163. {
  2164. tree maybe_super_class
  2165. = maybe_layout_super_class (super_class, this_class);
  2166. if (maybe_super_class == NULL
  2167. || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
  2168. {
  2169. TYPE_SIZE (this_class) = error_mark_node;
  2170. CLASS_BEING_LAIDOUT (this_class) = 0;
  2171. class_list = TREE_CHAIN (class_list);
  2172. return;
  2173. }
  2174. if (TYPE_SIZE (this_class) == NULL_TREE)
  2175. push_super_field (this_class, maybe_super_class);
  2176. }
  2177. layout_type (this_class);
  2178. /* Also recursively load/layout any superinterfaces. */
  2179. if (TYPE_BINFO (this_class))
  2180. {
  2181. for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (this_class)) - 1; i > 0; i--)
  2182. {
  2183. tree binfo = BINFO_BASE_BINFO (TYPE_BINFO (this_class), i);
  2184. tree super_interface = BINFO_TYPE (binfo);
  2185. tree maybe_super_interface
  2186. = maybe_layout_super_class (super_interface, NULL_TREE);
  2187. if (maybe_super_interface == NULL
  2188. || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
  2189. {
  2190. TYPE_SIZE (this_class) = error_mark_node;
  2191. CLASS_BEING_LAIDOUT (this_class) = 0;
  2192. class_list = TREE_CHAIN (class_list);
  2193. return;
  2194. }
  2195. }
  2196. }
  2197. /* Convert the size back to an SI integer value. */
  2198. TYPE_SIZE_UNIT (this_class) =
  2199. fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
  2200. CLASS_BEING_LAIDOUT (this_class) = 0;
  2201. class_list = TREE_CHAIN (class_list);
  2202. }
  2203. static void
  2204. add_miranda_methods (tree base_class, tree search_class)
  2205. {
  2206. int i;
  2207. tree binfo, base_binfo;
  2208. if (!CLASS_PARSED_P (search_class))
  2209. load_class (search_class, 1);
  2210. for (binfo = TYPE_BINFO (search_class), i = 1;
  2211. BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  2212. {
  2213. tree method_decl;
  2214. tree elt = BINFO_TYPE (base_binfo);
  2215. /* FIXME: This is totally bogus. We should not be handling
  2216. Miranda methods at all if we're using the BC ABI. */
  2217. if (TYPE_DUMMY (elt))
  2218. continue;
  2219. /* Ensure that interface methods are seen in declared order. */
  2220. if (!CLASS_LOADED_P (elt))
  2221. load_class (elt, 1);
  2222. layout_class_methods (elt);
  2223. /* All base classes will have been laid out at this point, so the order
  2224. will be correct. This code must match similar layout code in the
  2225. runtime. */
  2226. for (method_decl = TYPE_METHODS (elt);
  2227. method_decl; method_decl = DECL_CHAIN (method_decl))
  2228. {
  2229. tree sig, override;
  2230. /* An interface can have <clinit>. */
  2231. if (ID_CLINIT_P (DECL_NAME (method_decl)))
  2232. continue;
  2233. sig = build_java_argument_signature (TREE_TYPE (method_decl));
  2234. override = lookup_argument_method (base_class,
  2235. DECL_NAME (method_decl), sig);
  2236. if (override == NULL_TREE)
  2237. {
  2238. /* Found a Miranda method. Add it. */
  2239. tree new_method;
  2240. sig = build_java_signature (TREE_TYPE (method_decl));
  2241. new_method
  2242. = add_method (base_class,
  2243. get_access_flags_from_decl (method_decl),
  2244. DECL_NAME (method_decl), sig);
  2245. METHOD_INVISIBLE (new_method) = 1;
  2246. }
  2247. }
  2248. /* Try superinterfaces. */
  2249. add_miranda_methods (base_class, elt);
  2250. }
  2251. }
  2252. void
  2253. layout_class_methods (tree this_class)
  2254. {
  2255. tree method_decl, dtable_count;
  2256. tree super_class, type_name;
  2257. if (TYPE_NVIRTUALS (this_class))
  2258. return;
  2259. super_class = CLASSTYPE_SUPER (this_class);
  2260. if (super_class)
  2261. {
  2262. super_class = maybe_layout_super_class (super_class, this_class);
  2263. if (!TYPE_NVIRTUALS (super_class))
  2264. layout_class_methods (super_class);
  2265. dtable_count = TYPE_NVIRTUALS (super_class);
  2266. }
  2267. else
  2268. dtable_count = integer_zero_node;
  2269. type_name = TYPE_NAME (this_class);
  2270. if (!flag_indirect_dispatch
  2271. && (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name)))
  2272. {
  2273. /* An abstract class can have methods which are declared only in
  2274. an implemented interface. These are called "Miranda
  2275. methods". We make a dummy method entry for such methods
  2276. here. */
  2277. add_miranda_methods (this_class, this_class);
  2278. }
  2279. TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
  2280. for (method_decl = TYPE_METHODS (this_class);
  2281. method_decl; method_decl = DECL_CHAIN (method_decl))
  2282. dtable_count = layout_class_method (this_class, super_class,
  2283. method_decl, dtable_count);
  2284. TYPE_NVIRTUALS (this_class) = dtable_count;
  2285. }
  2286. /* Return the index of METHOD in INTERFACE. This index begins at 1
  2287. and is used as an argument for _Jv_LookupInterfaceMethodIdx(). */
  2288. int
  2289. get_interface_method_index (tree method, tree interface)
  2290. {
  2291. tree meth;
  2292. int i = 1;
  2293. for (meth = TYPE_METHODS (interface); ; meth = DECL_CHAIN (meth))
  2294. {
  2295. if (meth == method)
  2296. return i;
  2297. /* We don't want to put <clinit> into the interface table. */
  2298. if (! ID_CLINIT_P (DECL_NAME (meth)))
  2299. ++i;
  2300. gcc_assert (meth != NULL_TREE);
  2301. }
  2302. }
  2303. /* Lay METHOD_DECL out, returning a possibly new value of
  2304. DTABLE_COUNT. Also mangle the method's name. */
  2305. tree
  2306. layout_class_method (tree this_class, tree super_class,
  2307. tree method_decl, tree dtable_count)
  2308. {
  2309. tree method_name = DECL_NAME (method_decl);
  2310. TREE_PUBLIC (method_decl) = 1;
  2311. if (flag_indirect_classes
  2312. || (METHOD_PRIVATE (method_decl) && METHOD_STATIC (method_decl)
  2313. && ! METHOD_NATIVE (method_decl)
  2314. && ! special_method_p (method_decl)))
  2315. java_hide_decl (method_decl);
  2316. /* Considered external unless it is being compiled into this object
  2317. file, or it was already flagged as external. */
  2318. if (!DECL_EXTERNAL (method_decl))
  2319. DECL_EXTERNAL (method_decl) = ((is_compiled_class (this_class) != 2)
  2320. || METHOD_NATIVE (method_decl));
  2321. if (ID_INIT_P (method_name))
  2322. {
  2323. const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
  2324. const char *ptr;
  2325. for (ptr = p; *ptr; )
  2326. {
  2327. if (*ptr++ == '.')
  2328. p = ptr;
  2329. }
  2330. DECL_CONSTRUCTOR_P (method_decl) = 1;
  2331. build_java_signature (TREE_TYPE (method_decl));
  2332. }
  2333. else if (! METHOD_STATIC (method_decl))
  2334. {
  2335. tree method_sig =
  2336. build_java_signature (TREE_TYPE (method_decl));
  2337. bool method_override = false;
  2338. tree super_method = lookup_java_method (super_class, method_name,
  2339. method_sig);
  2340. if (super_method != NULL_TREE
  2341. && ! METHOD_DUMMY (super_method))
  2342. {
  2343. method_override = true;
  2344. if (! METHOD_PUBLIC (super_method) &&
  2345. ! METHOD_PROTECTED (super_method))
  2346. {
  2347. /* Don't override private method, or default-access method in
  2348. another package. */
  2349. if (METHOD_PRIVATE (super_method) ||
  2350. ! in_same_package (TYPE_NAME (this_class),
  2351. TYPE_NAME (super_class)))
  2352. method_override = false;
  2353. }
  2354. }
  2355. if (method_override)
  2356. {
  2357. tree method_index = get_method_index (super_method);
  2358. set_method_index (method_decl, method_index);
  2359. if (method_index == NULL_TREE
  2360. && ! flag_indirect_dispatch
  2361. && ! DECL_ARTIFICIAL (super_method))
  2362. error ("non-static method %q+D overrides static method",
  2363. method_decl);
  2364. }
  2365. else if (this_class == object_type_node
  2366. && (METHOD_FINAL (method_decl)
  2367. || METHOD_PRIVATE (method_decl)))
  2368. {
  2369. /* We don't generate vtable entries for final Object
  2370. methods. This is simply to save space, since every
  2371. object would otherwise have to define them. */
  2372. }
  2373. else if (! METHOD_PRIVATE (method_decl)
  2374. && dtable_count)
  2375. {
  2376. /* We generate vtable entries for final methods because they
  2377. may one day be changed to non-final. */
  2378. set_method_index (method_decl, dtable_count);
  2379. dtable_count = fold_build2 (PLUS_EXPR, integer_type_node,
  2380. dtable_count, integer_one_node);
  2381. }
  2382. }
  2383. return dtable_count;
  2384. }
  2385. static void
  2386. register_class (void)
  2387. {
  2388. tree node;
  2389. if (!registered_class)
  2390. vec_alloc (registered_class, 8);
  2391. if (flag_indirect_classes)
  2392. node = current_class;
  2393. else
  2394. node = TREE_OPERAND (build_class_ref (current_class), 0);
  2395. vec_safe_push (registered_class, node);
  2396. }
  2397. /* Emit a function that calls _Jv_RegisterNewClasses with a list of
  2398. all the classes we have emitted. */
  2399. static void
  2400. emit_indirect_register_classes (tree *list_p)
  2401. {
  2402. tree klass, t, register_class_fn;
  2403. int i;
  2404. int size = vec_safe_length (registered_class) * 2 + 1;
  2405. vec<constructor_elt, va_gc> *init;
  2406. vec_alloc (init, size);
  2407. tree class_array_type
  2408. = build_prim_array_type (ptr_type_node, size);
  2409. tree cdecl = build_decl (input_location,
  2410. VAR_DECL, get_identifier ("_Jv_CLS"),
  2411. class_array_type);
  2412. tree reg_class_list;
  2413. FOR_EACH_VEC_SAFE_ELT (registered_class, i, klass)
  2414. {
  2415. t = fold_convert (ptr_type_node, build_static_class_ref (klass));
  2416. CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, t);
  2417. t = fold_convert (ptr_type_node,
  2418. build_address_of (build_classdollar_field (klass)));
  2419. CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, t);
  2420. }
  2421. CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, integer_zero_node);
  2422. DECL_INITIAL (cdecl) = build_constructor (class_array_type, init);
  2423. TREE_CONSTANT (DECL_INITIAL (cdecl)) = 1;
  2424. TREE_STATIC (cdecl) = 1;
  2425. DECL_ARTIFICIAL (cdecl) = 1;
  2426. DECL_IGNORED_P (cdecl) = 1;
  2427. TREE_READONLY (cdecl) = 1;
  2428. TREE_CONSTANT (cdecl) = 1;
  2429. rest_of_decl_compilation (cdecl, 1, 0);
  2430. reg_class_list = fold_convert (ptr_type_node, build_address_of (cdecl));
  2431. t = build_function_type_list (void_type_node,
  2432. build_pointer_type (ptr_type_node), NULL);
  2433. t = build_decl (input_location,
  2434. FUNCTION_DECL,
  2435. get_identifier ("_Jv_RegisterNewClasses"), t);
  2436. TREE_PUBLIC (t) = 1;
  2437. DECL_EXTERNAL (t) = 1;
  2438. register_class_fn = t;
  2439. t = build_call_expr (register_class_fn, 1, reg_class_list);
  2440. append_to_statement_list (t, list_p);
  2441. }
  2442. /* Emit a list of pointers to all classes we have emitted to JCR_SECTION. */
  2443. static void
  2444. emit_register_classes_in_jcr_section (void)
  2445. {
  2446. #ifdef JCR_SECTION_NAME
  2447. tree klass, cdecl, class_array_type;
  2448. int i;
  2449. int size = vec_safe_length (registered_class);
  2450. vec<constructor_elt, va_gc> *init;
  2451. vec_alloc (init, size);
  2452. FOR_EACH_VEC_SAFE_ELT (registered_class, i, klass)
  2453. CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, build_fold_addr_expr (klass));
  2454. /* ??? I would like to use tree_output_constant_def() but there is no way
  2455. to put the data in a named section name, or to set the alignment,
  2456. via that function. So do everything manually here. */
  2457. class_array_type = build_prim_array_type (ptr_type_node, size);
  2458. cdecl = build_decl (UNKNOWN_LOCATION,
  2459. VAR_DECL, get_identifier ("_Jv_JCR_SECTION_data"),
  2460. class_array_type);
  2461. DECL_ALIGN (cdecl) = POINTER_SIZE;
  2462. DECL_USER_ALIGN (cdecl) = 1;
  2463. DECL_INITIAL (cdecl) = build_constructor (class_array_type, init);
  2464. TREE_CONSTANT (DECL_INITIAL (cdecl)) = 1;
  2465. TREE_STATIC (cdecl) = 1;
  2466. TREE_READONLY (cdecl) = 0;
  2467. TREE_CONSTANT (cdecl) = 1;
  2468. DECL_ARTIFICIAL (cdecl) = 1;
  2469. DECL_IGNORED_P (cdecl) = 1;
  2470. DECL_PRESERVE_P (cdecl) = 1;
  2471. set_decl_section_name (cdecl, JCR_SECTION_NAME);
  2472. pushdecl_top_level (cdecl);
  2473. relayout_decl (cdecl);
  2474. rest_of_decl_compilation (cdecl, 1, 0);
  2475. #else
  2476. /* A target has defined TARGET_USE_JCR_SECTION,
  2477. but doesn't have a JCR_SECTION_NAME. */
  2478. gcc_unreachable ();
  2479. #endif
  2480. }
  2481. /* Emit a series of calls to _Jv_RegisterClass for every class we emitted.
  2482. A series of calls is added to LIST_P. */
  2483. static void
  2484. emit_Jv_RegisterClass_calls (tree *list_p)
  2485. {
  2486. tree klass, t, register_class_fn;
  2487. int i;
  2488. t = build_function_type_list (void_type_node, class_ptr_type, NULL);
  2489. t = build_decl (input_location,
  2490. FUNCTION_DECL, get_identifier ("_Jv_RegisterClass"), t);
  2491. TREE_PUBLIC (t) = 1;
  2492. DECL_EXTERNAL (t) = 1;
  2493. register_class_fn = t;
  2494. FOR_EACH_VEC_SAFE_ELT (registered_class, i, klass)
  2495. {
  2496. t = build_fold_addr_expr (klass);
  2497. t = build_call_expr (register_class_fn, 1, t);
  2498. append_to_statement_list (t, list_p);
  2499. }
  2500. }
  2501. /* Emit something to register classes at start-up time.
  2502. The default mechanism is to generate instances at run-time.
  2503. An alternative mechanism is through the .jcr section, which contain
  2504. a list of pointers to classes which get registered during constructor
  2505. invocation time.
  2506. The fallback mechanism is to add statements to *LIST_P to call
  2507. _Jv_RegisterClass for each class in this file. These statements will
  2508. be added to a static constructor function for this translation unit. */
  2509. void
  2510. emit_register_classes (tree *list_p)
  2511. {
  2512. if (registered_class == NULL)
  2513. return;
  2514. /* By default, generate instances of Class at runtime. */
  2515. if (flag_indirect_classes)
  2516. emit_indirect_register_classes (list_p);
  2517. /* TARGET_USE_JCR_SECTION defaults to 1 if SUPPORTS_WEAK and
  2518. TARGET_ASM_NAMED_SECTION, else 0. Some targets meet those conditions
  2519. but lack suitable crtbegin/end objects or linker support. These
  2520. targets can override the default in tm.h to use the fallback mechanism. */
  2521. else if (TARGET_USE_JCR_SECTION)
  2522. emit_register_classes_in_jcr_section ();
  2523. /* Use the fallback mechanism. */
  2524. else
  2525. emit_Jv_RegisterClass_calls (list_p);
  2526. }
  2527. /* Build a constructor for an entry in the symbol table. */
  2528. static tree
  2529. build_symbol_table_entry (tree clname, tree name, tree signature)
  2530. {
  2531. tree symbol;
  2532. vec<constructor_elt, va_gc> *v = NULL;
  2533. START_RECORD_CONSTRUCTOR (v, symbol_type);
  2534. PUSH_FIELD_VALUE (v, "clname", clname);
  2535. PUSH_FIELD_VALUE (v, "name", name);
  2536. PUSH_FIELD_VALUE (v, "signature", signature);
  2537. FINISH_RECORD_CONSTRUCTOR (symbol, v, symbol_type);
  2538. TREE_CONSTANT (symbol) = 1;
  2539. return symbol;
  2540. }
  2541. /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
  2542. static tree
  2543. build_symbol_entry (tree decl, tree special)
  2544. {
  2545. tree clname, name, signature;
  2546. clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
  2547. /* ??? Constructors are given the name foo.foo all the way through
  2548. the compiler, but in the method table they're all renamed
  2549. foo.<init>. So, we have to do the same here unless we want an
  2550. unresolved reference at runtime. */
  2551. name = build_utf8_ref ((TREE_CODE (decl) == FUNCTION_DECL
  2552. && DECL_CONSTRUCTOR_P (decl))
  2553. ? init_identifier_node
  2554. : DECL_NAME (decl));
  2555. signature = build_java_signature (TREE_TYPE (decl));
  2556. signature = build_utf8_ref (unmangle_classname
  2557. (IDENTIFIER_POINTER (signature),
  2558. IDENTIFIER_LENGTH (signature)));
  2559. /* SPECIAL is either NULL_TREE or integer_one_node. We emit
  2560. signature addr+1 if SPECIAL, and this indicates to the runtime
  2561. system that this is a "special" symbol, i.e. one that should
  2562. bypass access controls. */
  2563. if (special != NULL_TREE)
  2564. signature = fold_build_pointer_plus (signature, special);
  2565. return build_symbol_table_entry (clname, name, signature);
  2566. }
  2567. /* Emit a symbol table: used by -findirect-dispatch. */
  2568. tree
  2569. emit_symbol_table (tree name, tree the_table,
  2570. vec<method_entry, va_gc> *decl_table,
  2571. tree the_syms_decl, tree the_array_element_type,
  2572. int element_size)
  2573. {
  2574. tree table, null_symbol, table_size, the_array_type;
  2575. unsigned index;
  2576. method_entry *e;
  2577. vec<constructor_elt, va_gc> *v = NULL;
  2578. /* Only emit a table if this translation unit actually made any
  2579. references via it. */
  2580. if (!decl_table)
  2581. return the_table;
  2582. /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
  2583. FOR_EACH_VEC_ELT (*decl_table, index, e)
  2584. CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
  2585. build_symbol_entry (e->method, e->special));
  2586. /* Terminate the list with a "null" entry. */
  2587. null_symbol = build_symbol_table_entry (null_pointer_node,
  2588. null_pointer_node,
  2589. null_pointer_node);
  2590. CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, null_symbol);
  2591. tree symbols_arr_type
  2592. = build_prim_array_type (symbol_type, vec_safe_length (v));
  2593. table = build_constructor (symbols_arr_type, v);
  2594. /* Make it the initial value for otable_syms and emit the decl. */
  2595. TREE_TYPE (the_syms_decl) = symbols_arr_type;
  2596. relayout_decl (the_syms_decl);
  2597. DECL_INITIAL (the_syms_decl) = table;
  2598. DECL_ARTIFICIAL (the_syms_decl) = 1;
  2599. DECL_IGNORED_P (the_syms_decl) = 1;
  2600. rest_of_decl_compilation (the_syms_decl, 1, 0);
  2601. /* Now that its size is known, redefine the table as an
  2602. uninitialized static array of INDEX + 1 elements. The extra entry
  2603. is used by the runtime to track whether the table has been
  2604. initialized. */
  2605. table_size
  2606. = build_index_type (build_int_cst (NULL_TREE, index * element_size + 1));
  2607. the_array_type = build_array_type (the_array_element_type, table_size);
  2608. the_table = build_decl (input_location,
  2609. VAR_DECL, name, the_array_type);
  2610. TREE_STATIC (the_table) = 1;
  2611. TREE_READONLY (the_table) = 1;
  2612. rest_of_decl_compilation (the_table, 1, 0);
  2613. return the_table;
  2614. }
  2615. /* Make an entry for the catch_classes list. */
  2616. tree
  2617. make_catch_class_record (tree catch_class, tree classname)
  2618. {
  2619. tree entry;
  2620. tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
  2621. vec<constructor_elt, va_gc> *v = NULL;
  2622. START_RECORD_CONSTRUCTOR (v, type);
  2623. PUSH_FIELD_VALUE (v, "address", catch_class);
  2624. PUSH_FIELD_VALUE (v, "classname", classname);
  2625. FINISH_RECORD_CONSTRUCTOR (entry, v, type);
  2626. return entry;
  2627. }
  2628. /* Generate the list of Throwable classes that are caught by exception
  2629. handlers in this class. */
  2630. tree
  2631. emit_catch_table (tree this_class)
  2632. {
  2633. tree table, table_size, array_type;
  2634. int n_catch_classes;
  2635. constructor_elt *e;
  2636. /* Fill in the dummy entry that make_class created. */
  2637. e = &(*TYPE_CATCH_CLASSES (this_class))[0];
  2638. e->value = make_catch_class_record (null_pointer_node, null_pointer_node);
  2639. CONSTRUCTOR_APPEND_ELT (TYPE_CATCH_CLASSES (this_class), NULL_TREE,
  2640. make_catch_class_record (null_pointer_node,
  2641. null_pointer_node));
  2642. n_catch_classes = TYPE_CATCH_CLASSES (this_class)->length ();
  2643. table_size = build_index_type (build_int_cst (NULL_TREE, n_catch_classes));
  2644. array_type
  2645. = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
  2646. table_size);
  2647. table =
  2648. build_decl (input_location,
  2649. VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
  2650. DECL_INITIAL (table) =
  2651. build_constructor (array_type, TYPE_CATCH_CLASSES (this_class));
  2652. TREE_STATIC (table) = 1;
  2653. TREE_READONLY (table) = 1;
  2654. DECL_IGNORED_P (table) = 1;
  2655. rest_of_decl_compilation (table, 1, 0);
  2656. return table;
  2657. }
  2658. /* Given a type, return the signature used by
  2659. _Jv_FindClassFromSignature() in libgcj. This isn't exactly the
  2660. same as build_java_signature() because we want the canonical array
  2661. type. */
  2662. static tree
  2663. build_signature_for_libgcj (tree type)
  2664. {
  2665. tree sig, ref;
  2666. sig = build_java_signature (type);
  2667. ref = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
  2668. IDENTIFIER_LENGTH (sig)));
  2669. return ref;
  2670. }
  2671. /* Build an entry in the type assertion table. */
  2672. static tree
  2673. build_assertion_table_entry (tree code, tree op1, tree op2)
  2674. {
  2675. vec<constructor_elt, va_gc> *v = NULL;
  2676. tree entry;
  2677. START_RECORD_CONSTRUCTOR (v, assertion_entry_type);
  2678. PUSH_FIELD_VALUE (v, "assertion_code", code);
  2679. PUSH_FIELD_VALUE (v, "op1", op1);
  2680. PUSH_FIELD_VALUE (v, "op2", op2);
  2681. FINISH_RECORD_CONSTRUCTOR (entry, v, assertion_entry_type);
  2682. return entry;
  2683. }
  2684. /* Add an entry to the type assertion table. Callback used during hashtable
  2685. traversal. */
  2686. int
  2687. add_assertion_table_entry (type_assertion **slot, vec<constructor_elt, va_gc> **v)
  2688. {
  2689. tree entry;
  2690. tree code_val, op1_utf8, op2_utf8;
  2691. type_assertion *as = *slot;
  2692. code_val = build_int_cst (NULL_TREE, as->assertion_code);
  2693. if (as->op1 == NULL_TREE)
  2694. op1_utf8 = null_pointer_node;
  2695. else
  2696. op1_utf8 = build_signature_for_libgcj (as->op1);
  2697. if (as->op2 == NULL_TREE)
  2698. op2_utf8 = null_pointer_node;
  2699. else
  2700. op2_utf8 = build_signature_for_libgcj (as->op2);
  2701. entry = build_assertion_table_entry (code_val, op1_utf8, op2_utf8);
  2702. CONSTRUCTOR_APPEND_ELT (*v, NULL_TREE, entry);
  2703. return true;
  2704. }
  2705. /* Generate the type assertion table for KLASS, and return its DECL. */
  2706. static tree
  2707. emit_assertion_table (tree klass)
  2708. {
  2709. tree null_entry, ctor, table_decl;
  2710. hash_table<type_assertion_hasher> *assertions_htab = TYPE_ASSERTIONS (klass);
  2711. vec<constructor_elt, va_gc> *v = NULL;
  2712. /* Iterate through the hash table. */
  2713. assertions_htab
  2714. ->traverse<vec<constructor_elt, va_gc> **, add_assertion_table_entry> (&v);
  2715. /* Finish with a null entry. */
  2716. null_entry = build_assertion_table_entry (integer_zero_node,
  2717. null_pointer_node,
  2718. null_pointer_node);
  2719. CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, null_entry);
  2720. tree type
  2721. = build_prim_array_type (assertion_entry_type, vec_safe_length (v));
  2722. ctor = build_constructor (type, v);
  2723. table_decl = build_decl (input_location,
  2724. VAR_DECL, mangled_classname ("_type_assert_", klass),
  2725. type);
  2726. TREE_STATIC (table_decl) = 1;
  2727. TREE_READONLY (table_decl) = 1;
  2728. TREE_CONSTANT (table_decl) = 1;
  2729. DECL_IGNORED_P (table_decl) = 1;
  2730. DECL_INITIAL (table_decl) = ctor;
  2731. DECL_ARTIFICIAL (table_decl) = 1;
  2732. rest_of_decl_compilation (table_decl, 1, 0);
  2733. return table_decl;
  2734. }
  2735. void
  2736. init_class_processing (void)
  2737. {
  2738. fields_ident = get_identifier ("fields");
  2739. info_ident = get_identifier ("info");
  2740. gcc_obstack_init (&temporary_obstack);
  2741. }
  2742. /* A hash table mapping trees to trees. Used generally. */
  2743. #define JAVA_TREEHASHHASH_H(t) ((hashval_t)TYPE_UID (t))
  2744. hashval_t
  2745. treetreehasher::hash (treetreehash_entry *k)
  2746. {
  2747. return JAVA_TREEHASHHASH_H (k->key);
  2748. }
  2749. bool
  2750. treetreehasher::equal (treetreehash_entry *k1, tree k2)
  2751. {
  2752. return (k1->key == k2);
  2753. }
  2754. tree
  2755. java_treetreehash_find (hash_table<treetreehasher> *ht, tree t)
  2756. {
  2757. struct treetreehash_entry *e;
  2758. hashval_t hv = JAVA_TREEHASHHASH_H (t);
  2759. e = ht->find_with_hash (t, hv);
  2760. if (e == NULL)
  2761. return NULL;
  2762. else
  2763. return e->value;
  2764. }
  2765. tree *
  2766. java_treetreehash_new (hash_table<treetreehasher> *ht, tree t)
  2767. {
  2768. struct treetreehash_entry *tthe;
  2769. hashval_t hv = JAVA_TREEHASHHASH_H (t);
  2770. treetreehash_entry **e = ht->find_slot_with_hash (t, hv, INSERT);
  2771. if (*e == NULL)
  2772. {
  2773. tthe = ggc_cleared_alloc<treetreehash_entry> ();
  2774. tthe->key = t;
  2775. *e = tthe;
  2776. }
  2777. else
  2778. tthe = *e;
  2779. return &tthe->value;
  2780. }
  2781. hash_table<treetreehasher> *
  2782. java_treetreehash_create (size_t size)
  2783. {
  2784. return hash_table<treetreehasher>::create_ggc (size);
  2785. }
  2786. /* Break down qualified IDENTIFIER into package and class-name components.
  2787. For example, given SOURCE "pkg.foo.Bar", LEFT will be set to
  2788. "pkg.foo", and RIGHT to "Bar". */
  2789. int
  2790. split_qualified_name (tree *left, tree *right, tree source)
  2791. {
  2792. char *p, *base;
  2793. int l = IDENTIFIER_LENGTH (source);
  2794. base = (char *) alloca (l + 1);
  2795. memcpy (base, IDENTIFIER_POINTER (source), l + 1);
  2796. /* Breakdown NAME into REMAINDER . IDENTIFIER. */
  2797. p = base + l - 1;
  2798. while (*p != '.' && p != base)
  2799. p--;
  2800. /* We didn't find a '.'. Return an error. */
  2801. if (p == base)
  2802. return 1;
  2803. *p = '\0';
  2804. if (right)
  2805. *right = get_identifier (p+1);
  2806. *left = get_identifier (base);
  2807. return 0;
  2808. }
  2809. /* Given two classes (TYPE_DECL) or class names (IDENTIFIER), return TRUE
  2810. if the classes are from the same package. */
  2811. int
  2812. in_same_package (tree name1, tree name2)
  2813. {
  2814. tree tmp;
  2815. tree pkg1;
  2816. tree pkg2;
  2817. if (TREE_CODE (name1) == TYPE_DECL)
  2818. name1 = DECL_NAME (name1);
  2819. if (TREE_CODE (name2) == TYPE_DECL)
  2820. name2 = DECL_NAME (name2);
  2821. if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
  2822. /* One in empty package. */
  2823. return 0;
  2824. if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
  2825. /* Both in empty package. */
  2826. return 1;
  2827. split_qualified_name (&pkg1, &tmp, name1);
  2828. split_qualified_name (&pkg2, &tmp, name2);
  2829. return (pkg1 == pkg2);
  2830. }
  2831. /* lang_hooks.decls.final_write_globals: perform final processing on
  2832. global variables. */
  2833. void
  2834. java_write_globals (void)
  2835. {
  2836. tree *vec = vec_safe_address (pending_static_fields);
  2837. int len = vec_safe_length (pending_static_fields);
  2838. write_global_declarations ();
  2839. emit_debug_global_declarations (vec, len);
  2840. vec_free (pending_static_fields);
  2841. }
  2842. #include "gt-java-class.h"