home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / gcc / objc-act.c < prev    next >
C/C++ Source or Header  |  1996-12-21  |  252KB  |  9,134 lines

  1. /* Implement classes and message passing for Objective C.
  2.    Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  3.    Contributed by Steve Naroff.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.  */
  21.  
  22. /* Purpose: This module implements the Objective-C 4.0 language.
  23.  
  24.    compatibility issues (with the Stepstone translator):
  25.  
  26.    - does not recognize the following 3.3 constructs.
  27.      @requires, @classes, @messages, = (...)
  28.    - methods with variable arguments must conform to ANSI standard.
  29.    - tagged structure definitions that appear in BOTH the interface
  30.      and implementation are not allowed.
  31.    - public/private: all instance variables are public within the
  32.      context of the implementation...I consider this to be a bug in
  33.      the translator.
  34.    - statically allocated objects are not supported. the user will
  35.      receive an error if this service is requested.
  36.  
  37.    code generation `options':
  38.  
  39.    - OBJC_INT_SELECTORS  */
  40.  
  41. #include <stdio.h>
  42. #include "config.h"
  43. #include "tree.h"
  44. #include "flags.h"
  45.  
  46. #ifdef OBJCPLUS
  47. #include "cp-tree.h"
  48. #include "lex.h"
  49. #else
  50. #include "c-tree.h"
  51. #include "c-lex.h"
  52. #endif
  53.  
  54. #include "objc-act.h"
  55. #include "input.h"
  56. #include "function.h"
  57.  
  58.  
  59. /* This is the default way of generating a method name.  */
  60. /* I am not sure it is really correct.
  61.    Perhaps there's a danger that it will make name conflicts
  62.    if method names contain underscores. -- rms.  */
  63. #ifndef OBJC_GEN_METHOD_LABEL
  64. #define OBJC_GEN_METHOD_LABEL(BUF, IS_INST, CLASS_NAME, CAT_NAME, SEL_NAME, NUM) \
  65.   do {                        \
  66.     char *temp;                    \
  67.     sprintf ((BUF), "_%s_%s_%s_%s",        \
  68.          ((IS_INST) ? "i" : "c"),        \
  69.          (CLASS_NAME),            \
  70.          ((CAT_NAME)? (CAT_NAME) : ""), \
  71.          (SEL_NAME));            \
  72.     for (temp = (BUF); *temp; temp++)        \
  73.       if (*temp == ':') *temp = '_';        \
  74.   } while (0)
  75. #endif
  76.  
  77. /* These need specifying.  */
  78. #ifndef OBJC_FORWARDING_STACK_OFFSET
  79. #define OBJC_FORWARDING_STACK_OFFSET 0
  80. #endif
  81.  
  82. #ifndef OBJC_FORWARDING_MIN_OFFSET
  83. #define OBJC_FORWARDING_MIN_OFFSET 0
  84. #endif
  85.  
  86. /* Define the special tree codes that we use.  */
  87.  
  88. /* Table indexed by tree code giving a string containing a character
  89.    classifying the tree code.  Possibilities are
  90.    t, d, s, c, r, <, 1 and 2.  See objc-tree.def for details.  */
  91.  
  92. #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
  93.  
  94. char *objc_tree_code_type[] = {
  95.   "x",
  96. #include "objc-tree.def"
  97. };
  98. #undef DEFTREECODE
  99.  
  100. /* Table indexed by tree code giving number of expression
  101.    operands beyond the fixed part of the node structure.
  102.    Not used for types or decls.  */
  103.  
  104. #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
  105.  
  106. int objc_tree_code_length[] = {
  107.   0,
  108. #include "objc-tree.def"
  109. };
  110. #undef DEFTREECODE
  111.  
  112. /* Names of tree components.
  113.    Used for printing out the tree and error messages.  */
  114. #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
  115.  
  116. char *objc_tree_code_name[] = {
  117.   "@@dummy",
  118. #include "objc-tree.def"
  119. };
  120. #undef DEFTREECODE
  121.  
  122. /* Set up for use of obstacks.  */
  123.  
  124. #include "obstack.h"
  125.  
  126. #define obstack_chunk_alloc xmalloc
  127. #define obstack_chunk_free free
  128.  
  129. /* This obstack is used to accumulate the encoding of a data type.  */
  130. static struct obstack util_obstack;
  131. /* This points to the beginning of obstack contents,
  132.    so we can free the whole contents.  */
  133. char *util_firstobj;
  134.  
  135. /* List of classes with list of their static instances.  */
  136. static tree objc_static_instances;
  137.  
  138. /* The declaration of the array administrating the static instances.  */
  139. static tree static_instances_decl;
  140.  
  141. /* for encode_method_def */
  142. #include "rtl.h"
  143. #include "regs.h"
  144.  
  145. #ifdef OBJCPLUS
  146. #include <parse.h>
  147. #else
  148. #include <objc-parse.h>
  149. #endif
  150.  
  151. #define OBJC_VERSION    5
  152. #define PROTOCOL_VERSION 2
  153.  
  154. #define OBJC_ENCODE_INLINE_DEFS     0
  155. #define OBJC_ENCODE_DONT_INLINE_DEFS    1
  156.  
  157. #ifdef OBJCPLUS
  158.  
  159. /* from cp-decl.c */
  160. extern tree make_anon_name         PROTO((void));
  161. extern tree const_string_type_node;
  162.  
  163. // FIXME: should arg 4 always be 0 ???
  164. #define xref_tag(code, name) xref_tag (record_type_node, name, 0, 0)
  165.  
  166. /* Hacks to simulate start_struct() and finish_struct(). */
  167.  
  168. static int cplus_struct_hack = 0;
  169.  
  170. static tree 
  171. objcplus_start_struct (code, name)
  172.      enum tree_code code; 
  173.      tree name;
  174.   tree s = xref_tag (record_type_node, name ? name : make_anon_name ());
  175.   
  176.   /* simulate `LC' production */
  177.   int temp = allocation_temporary_p ();
  178.   int momentary = suspend_momentary ();
  179.  
  180.   if (temp)
  181.     end_temporary_allocation ();
  182.   cplus_struct_hack = (momentary << 1) | temp;
  183.   pushclass (s, 0); 
  184.  
  185.   return s;    
  186. }
  187.  
  188. static tree 
  189. objcplus_finish_struct (t, fieldlist)
  190.      tree t; 
  191.      tree fieldlist;
  192. {
  193.   tree fieldlist_list = build_tree_list ((tree) access_default, fieldlist);
  194.   tree s = finish_struct (t, fieldlist_list, 0);
  195.  
  196.   if (cplus_struct_hack & 1)
  197.     resume_temporary_allocation ();
  198.   if (cplus_struct_hack & 2)
  199.     resume_momentary (1);
  200.  
  201.   return s;
  202. }
  203.  
  204. static void
  205. objcplus_finish_function (nested)
  206.      int nested;
  207. {
  208.   /* C++ finish_decl allows you to specify if it should poplevel... */
  209.   finish_function (lineno, 1, 0);
  210. }
  211.  
  212. extern tree groktypename_in_parm_context         PROTO ((tree));
  213.  
  214. static void
  215. objcplus_finish_decl (decl, init, asmspec)
  216.      tree decl, init, asmspec;
  217. {
  218.   /* C++ finish_decl allows you to specify if it should pop_obstacks... */
  219.   finish_decl (decl, init, asmspec, 1);
  220. }
  221.  
  222. static tree
  223. objcplus_lookup_name (name)
  224.      tree name;
  225. {
  226.   return lookup_name (name, -1);
  227. }
  228.  
  229. /* Hacks to simulate push_parm_decl() and objcplus_get_parm_info(). */
  230.  
  231. static tree objcplus_parmlist = NULL_TREE;
  232.  
  233. tree
  234. objcplus_push_parm_decl (parm)
  235.      tree parm;
  236. {
  237.   if (objcplus_parmlist)
  238.     objcplus_parmlist = chainon (objcplus_parmlist, build_tree_list (0, parm));
  239.   else
  240.     objcplus_parmlist = build_tree_list (0, parm);
  241.  
  242.   return objcplus_parmlist;
  243. }
  244.  
  245. static tree 
  246. objcplus_get_parm_info (void_at_end) 
  247.      int void_at_end;
  248. {
  249.   tree parm_info = objcplus_parmlist;
  250.   
  251.   TREE_PARMLIST (parm_info) = 1;
  252.  
  253.   if (void_at_end)
  254.     chainon (parm_info, void_list_node);
  255.  
  256.   objcplus_parmlist = NULL_TREE;
  257.  
  258.   return parm_info;
  259. }
  260.  
  261. static tree 
  262. objcplus_type_name (type)
  263.      tree type;
  264. {
  265.   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
  266.     return DECL_NAME (TYPE_NAME (type));
  267.   else
  268.     return TYPE_NAME (type);
  269. }
  270.  
  271. static tree 
  272. objcplus_type_size (type)
  273.      tree type;
  274. {
  275.   int x = TREE_INT_CST_LOW (TYPE_SIZE (type));
  276.   
  277.   return build_int_2 (x, 0);
  278. }
  279.  
  280. /* Macros to cover functions with changed interfaces. */
  281.  
  282. #define lookup_name(name) objcplus_lookup_name (name)
  283.  
  284. #define start_struct(code, name) objcplus_start_struct (code, name)
  285.  
  286. #define finish_decl(decl, init, asmspec) \
  287.       objcplus_finish_decl (decl, init, asmspec)
  288.  
  289. #define finish_function(nested) objcplus_finish_function(nested)
  290.  
  291. #define finish_struct(code, name) objcplus_finish_struct (code, name)
  292.  
  293. #define start_function(declspecs, declarator, nested) \
  294.   start_function (declspecs, declarator, NULL_TREE, NULL_TREE, 0)
  295.  
  296. // FIXME: should arg 3 always be 0 ???
  297. #define build_c_cast(type, expr) \
  298.     build_c_cast (type, expr, 0)
  299.  
  300. #define pushlevel(tag_transparent) /* noop */
  301.  
  302. #define poplevel(keep, reverse, functionbody) /* noop */
  303.  
  304. #define push_parm_decl(parm) objcplus_push_parm_decl (parm)
  305.  
  306. #define get_parm_info(void_at_end) objcplus_get_parm_info (void_at_end)
  307.  
  308. // FIXME: should last arg always be 0 ???
  309. #define grokfield(filename, line, declarator, declspecs, width) \
  310.          ((width) ? grokbitfield (declarator, declspecs, width) \
  311.                   : grokfield (declarator, declspecs, width, 0, 0, 0))
  312.  
  313. #define build_component_ref(datum, component) \
  314.         build_component_ref (datum, component, NULL_TREE, 1)
  315.  
  316. #define comptypes(type1, type2) comptypes (type1, type2, 0)
  317.  
  318. #define start_decl(declarator, declspecs, spspecs) \
  319.   start_decl (declarator, declspecs, spspecs, NULL_TREE)
  320.  
  321. #undef TYPE_NAME
  322. #define TYPE_NAME(NODE) objcplus_type_name (NODE)
  323.  
  324. #undef TYPE_SIZE
  325. #define TYPE_SIZE(NODE) objcplus_type_size (NODE)
  326.  
  327. extern tree define_function PROTO((char*, tree, enum built_in_function, void(*)(),
  328.                            char*));
  329.  
  330.  
  331. #define builtin_function(NAME, TYPE, CODE, LIBNAME) \
  332.   define_function (NAME, TYPE, CODE, (void (*)())pushdecl, LIBNAME)
  333.  
  334. #elif defined (NEXT_SEMANTICS) || defined (NEXT_PDO)
  335. #define start_decl(declarator, declspecs, spspecs) \
  336.   start_decl (declarator, declspecs, spspecs, NULL_TREE, NULL_TREE)
  337. #define start_function(declspecs, declarator, nested) \
  338.   start_function (declspecs, declarator, NULL_TREE, NULL_TREE, 0)
  339. #define finish_struct(code, name) finish_struct (code, name, NULL_TREE)
  340. #define xref_teg (code_type_node, name, binfo, globalize) \
  341.     xref_teg (code_type_node, name, binfo)
  342. #endif /* OBJCPLUS */
  343.  
  344.  
  345. /*** Private Interface (procedures) ***/
  346.  
  347. /* Used by compile_file. */
  348.  
  349. static void init_objc                PROTO((void));
  350. static void finish_objc                PROTO((void));
  351.  
  352. /* code generation */
  353.  
  354. tree is_class_name                 PROTO((tree));
  355.  
  356. static void synth_module_prologue        PROTO((void));
  357. static tree build_constructor            PROTO((tree, tree));
  358. static char *build_module_descriptor        PROTO((void));
  359. static tree init_module_descriptor        PROTO((tree));
  360. static tree build_objc_method_call        PROTO((int, tree, tree, tree, tree, tree));
  361. static void generate_strings            PROTO((void));
  362. static void build_selector_translation_table    PROTO((void));
  363. static tree build_ivar_chain            PROTO((tree, int));
  364.  
  365. static tree build_ivar_template            PROTO((void));
  366. static tree build_method_template        PROTO((void));
  367. static tree build_private_template        PROTO((tree));
  368. static void build_class_template        PROTO((void));
  369. static void build_category_template        PROTO((void));
  370. static tree build_super_template        PROTO((void));
  371. static tree build_category_initializer        PROTO((tree, tree, tree, tree, tree, tree));
  372. static tree build_protocol_initializer        PROTO((tree, tree, tree, tree, tree));
  373.  
  374. static void synth_forward_declarations        PROTO((void));
  375. static void generate_ivar_lists            PROTO((void));
  376. static void generate_dispatch_tables        PROTO((void));
  377. static void generate_shared_structures        PROTO((void));
  378. static tree generate_protocol_list        PROTO((tree));
  379. static void generate_forward_declaration_to_string_table PROTO((void));
  380. static void build_protocol_reference        PROTO((tree));
  381.  
  382. static tree init_selector            PROTO((int));
  383. static tree build_keyword_selector        PROTO((tree));
  384. static tree synth_id_with_class_suffix        PROTO((char *, tree));
  385.  
  386. /* From expr.c */
  387. extern int apply_args_register_offset           PROTO((int));
  388.  
  389. /* Misc. bookkeeping */
  390.  
  391. typedef struct hashed_entry     *hash;
  392. typedef struct hashed_attribute  *attr;
  393.  
  394. struct hashed_attribute
  395. {
  396.   attr next;
  397.   tree value;
  398. };
  399. struct hashed_entry
  400. {
  401.   attr list;
  402.   hash next;
  403.   tree key;
  404. };
  405.  
  406. static void hash_init                PROTO((void));
  407. static void hash_enter                PROTO((hash *, tree));
  408. static hash hash_lookup                PROTO((hash *, tree));
  409. static void hash_add_attr            PROTO((hash, tree));
  410. static tree lookup_method            PROTO((tree, tree));
  411. static tree lookup_instance_method_static    PROTO((tree, tree));
  412. static tree lookup_class_method_static        PROTO((tree, tree));
  413. static tree add_class                PROTO((tree));
  414. static void add_category            PROTO((tree, tree));
  415. #if defined (NEXT_SEMANTICS) || defined (NEXT_PDO)
  416. static void add_static_object                   PROTO((tree, tree));
  417. #endif /*  NEXT_PDO */
  418.  
  419. enum string_section
  420. {
  421.   class_names,        /* class, category, protocol, module names */
  422.   meth_var_names,    /* method and variable names */
  423.   meth_var_types    /* method and variable type descriptors */
  424. };
  425.  
  426. static tree add_objc_string            PROTO((tree,
  427.                                enum string_section));
  428. static tree build_objc_string_decl        PROTO((tree,
  429.                                enum string_section));
  430. static tree build_selector_reference_decl    PROTO((tree));
  431.  
  432. /* Protocol additions. */
  433.  
  434. static tree add_protocol            PROTO((tree));
  435. static tree lookup_protocol            PROTO((tree));
  436. static tree lookup_and_install_protocols    PROTO((tree));
  437.  
  438. /* Type encoding. */
  439.  
  440. static void encode_type_qualifiers        PROTO((tree));
  441. static void encode_pointer            PROTO((tree, int, int));
  442. static void encode_array            PROTO((tree, int, int));
  443. static void encode_aggregate            PROTO((tree, int, int));
  444. static void encode_bitfield            PROTO((int, int));
  445. static void encode_type                PROTO((tree, int, int));
  446. static void encode_field_decl            PROTO((tree, int, int));
  447.  
  448. static void really_start_method            PROTO((tree, tree));
  449. static int comp_method_with_proto        PROTO((tree, tree));
  450. static int comp_proto_with_proto        PROTO((tree, tree));
  451. static tree get_arg_type_list            PROTO((tree, int, int));
  452. static tree expr_last                PROTO((tree));
  453.  
  454. /* Utilities for debugging and error diagnostics. */
  455.  
  456. static void warn_with_method            PROTO((char *, int, tree));
  457. static void error_with_ivar            PROTO((char *, tree, tree));
  458. static char *gen_method_decl            PROTO((tree, char *));
  459. static char *gen_declaration            PROTO((tree, char *));
  460. static char *gen_declarator            PROTO((tree, char *, char *));
  461. static int is_complex_decl            PROTO((tree));
  462. static void adorn_decl                PROTO((tree, char *));
  463. static void dump_interface            PROTO((FILE *, tree));
  464.  
  465. /* Everything else. */
  466.  
  467. static void objc_fatal                PROTO((void));
  468. static tree define_decl                PROTO((tree, tree));
  469. static tree lookup_method_in_protocol_list    PROTO((tree, tree, int));
  470. static tree lookup_protocol_in_reflist        PROTO((tree, tree));
  471. static tree create_builtin_decl            PROTO((enum tree_code,
  472.                                tree, char *));
  473. static tree my_build_string            PROTO((int, char *));
  474. static void build_objc_symtab_template        PROTO((void));
  475. static tree init_def_list            PROTO((tree));
  476. static tree init_objc_symtab            PROTO((tree));
  477. static void forward_declare_categories        PROTO((void));
  478. static void generate_objc_symtab_decl        PROTO((void));
  479. static tree build_selector            PROTO((tree));
  480. static tree build_msg_pool_reference        PROTO((int));
  481. static tree build_typed_selector_reference         PROTO((tree, tree));
  482. static tree build_selector_reference        PROTO((tree));
  483. static tree build_class_reference_decl        PROTO((tree));
  484. static void add_class_reference            PROTO((tree));
  485. static tree objc_copy_list            PROTO((tree, tree *));
  486. static tree build_protocol_template        PROTO((void));
  487. static tree build_descriptor_table_initializer    PROTO((tree, tree));
  488. static tree build_method_prototype_list_template PROTO((tree, int));
  489. static tree build_method_prototype_template    PROTO((void));
  490. static int forwarding_offset            PROTO((tree));
  491. static tree encode_method_prototype        PROTO((tree, tree));
  492. static tree generate_descriptor_table        PROTO((tree, char *, int, tree, tree));
  493. static void generate_method_descriptors        PROTO((tree));
  494. static tree build_tmp_function_decl        PROTO((void));
  495. static void hack_method_prototype        PROTO((tree, tree));
  496. static void generate_protocol_references    PROTO((tree));
  497. static void generate_protocols            PROTO((void));
  498. static void check_ivars                PROTO((tree, tree));
  499. static tree build_ivar_list_template        PROTO((tree, int));
  500. static tree build_method_list_template        PROTO((tree, int));
  501. static tree build_ivar_list_initializer        PROTO((tree, tree));
  502. static tree generate_ivars_list            PROTO((tree, char *, int, tree));
  503. static tree build_dispatch_table_initializer    PROTO((tree, tree));
  504. static tree generate_dispatch_table        PROTO((tree, char *, int, tree));
  505. static tree build_shared_structure_initializer    PROTO((tree, tree, tree, tree, tree, int, tree, tree, tree));
  506. static void generate_category            PROTO((tree));
  507. static int is_objc_type_qualifier        PROTO((tree));
  508. static tree adjust_type_for_id_default        PROTO((tree, int));
  509. static tree check_duplicates            PROTO((hash));
  510. static tree receiver_is_class_object        PROTO((tree));
  511. static int check_methods            PROTO((tree, tree, int));
  512. static int check_methods_accessible         PROTO((tree, tree, int));
  513. static int conforms_to_protocol            PROTO((tree, tree));
  514. static void check_protocols            PROTO((tree, char *, char *));
  515. static tree encode_method_def            PROTO((tree));
  516. static void gen_declspecs            PROTO((tree, char *, int));
  517. static void generate_classref_translation_entry    PROTO((tree));
  518. static void handle_class_ref            PROTO((tree));
  519.  
  520. /*** Private Interface (data) ***/
  521.  
  522. /* Reserved tag definitions. */
  523.  
  524. #define TYPE_ID            "id"
  525. #define TAG_OBJECT        "objc_object"
  526. #define TAG_CLASS        "objc_class"
  527. #define TAG_SUPER        "objc_super"
  528. #define TAG_SELECTOR        "objc_selector"
  529.  
  530. #define UTAG_CLASS        "_objc_class"
  531. #define UTAG_IVAR        "_objc_ivar"
  532. #define UTAG_IVAR_LIST        "_objc_ivar_list"
  533. #define UTAG_METHOD        "_objc_method"
  534. #define UTAG_METHOD_LIST    "_objc_method_list"
  535. #define UTAG_CATEGORY        "_objc_category"
  536. #define UTAG_MODULE        "_objc_module"
  537. #define UTAG_SYMTAB        "_objc_symtab"
  538. #define UTAG_SUPER        "_objc_super"
  539.  
  540. #define UTAG_PROTOCOL        "_objc_protocol"
  541. #define UTAG_PROTOCOL_LIST    "_objc_protocol_list"
  542. #define UTAG_METHOD_PROTOTYPE    "_objc_method_prototype"
  543. #define UTAG_METHOD_PROTOTYPE_LIST "_objc__method_prototype_list"
  544.  
  545. #define STRING_OBJECT_CLASS_NAME_NEW "NSConstantString"
  546. #define STRING_OBJECT_CLASS_NAME_OLD "NXConstantString"
  547. #define STRING_OBJECT_GLOBAL_NAME "_NSConstantStringClassReference"
  548. #define PROTOCOL_OBJECT_CLASS_NAME "Protocol"
  549.  
  550. static char* TAG_GETCLASS;
  551. static char* TAG_GETMETACLASS;
  552. static char* TAG_GETORIGCLASS;
  553. static char* TAG_MSGSEND;
  554. static char* TAG_MSGSENDSUPER;
  555. static char* TAG_EXECCLASS;
  556. static char* TAG_ATOM;
  557.  
  558. /* Set by `continue_class' and checked by `is_public'.  */
  559.  
  560. #define TREE_STATIC_TEMPLATE(record_type) (TREE_PUBLIC (record_type))
  561. #define TYPED_OBJECT(type) \
  562.        (TREE_CODE (type) == RECORD_TYPE && TREE_STATIC_TEMPLATE (type))
  563.  
  564. /* Some commonly used instances of "identifier_node".  */
  565.  
  566. static tree self_id, ucmd_id;
  567. static tree unused_list;
  568.  
  569. static tree self_decl, umsg_decl, umsg_super_decl;
  570. static tree objc_get_class_decl, objc_get_meta_class_decl;
  571. static tree objc_get_orig_class_decl;
  572.  
  573. static tree super_type, selector_type, id_type, objc_class_type;
  574. static tree instance_type, protocol_type;
  575. #ifdef OBJCPLUS
  576. static tree objc_module_type;
  577. #endif
  578.  
  579. /* Type checking macros.  */
  580.  
  581. #define IS_ID(TYPE) \
  582.   (id_type && TYPE_MAIN_VARIANT (TYPE) == TYPE_MAIN_VARIANT (id_type))
  583. #define IS_PROTOCOL_QUALIFIED_ID(TYPE) \
  584.   (IS_ID (TYPE) && TYPE_PROTOCOL_LIST (TYPE))
  585. #define IS_SUPER(TYPE) \
  586.   (super_type && TYPE_MAIN_VARIANT (TYPE) == TYPE_MAIN_VARIANT (super_type))
  587.  
  588. static tree class_chain = NULL_TREE;
  589. static tree alias_chain = NULL_TREE;
  590. static tree interface_chain = NULL_TREE;
  591. static tree protocol_chain = NULL_TREE;
  592.  
  593. /* chains to manage selectors that are referenced and defined in the module */
  594.  
  595. static tree cls_ref_chain = NULL_TREE;    /* classes referenced */
  596. static tree sel_ref_chain = NULL_TREE;    /* selectors referenced */
  597.  
  598. #if defined (NEXT_SEMANTICS) || defined (NEXT_PDO)
  599. /* chain to manage objects defined in this module */
  600.  
  601. static tree obj_def_chain = NULL_TREE;      /* objects defined */
  602.  
  603. /* chain to manage protocols defined in this module */
  604.  
  605. static tree proto_def_chain = NULL_TREE;    /* protocols defined */
  606.  
  607. #endif
  608.  
  609. /* Chains to manage uniquing of strings. */
  610.  
  611. static tree class_names_chain = NULL_TREE;
  612. static tree meth_var_names_chain = NULL_TREE;
  613. static tree meth_var_types_chain = NULL_TREE;
  614.  
  615. /* Hash tables to manage the global pool of method prototypes. */
  616.  
  617. static hash *nst_method_hash_list = 0;
  618. static hash *cls_method_hash_list = 0;
  619.  
  620. /* Backend data declarations. */
  621.  
  622. static tree UOBJC_SYMBOLS_decl;
  623. static tree UOBJC_INSTANCE_VARIABLES_decl, UOBJC_CLASS_VARIABLES_decl;
  624. static tree UOBJC_INSTANCE_METHODS_decl, UOBJC_CLASS_METHODS_decl;
  625. static tree UOBJC_CLASS_decl, UOBJC_METACLASS_decl;
  626. static tree UOBJC_SELECTOR_TABLE_decl = 0;
  627. static tree UOBJC_MODULES_decl;
  628. static tree UOBJC_STRINGS_decl;
  629.  
  630. /* The following are used when compiling a class implementation.
  631.    implementation_template will normally be an interface, however if
  632.    none exists this will be equal to implementation_context...it is
  633.    set in start_class.  */
  634.  
  635. static tree implementation_context = NULL_TREE,
  636.         implementation_template = NULL_TREE;
  637.  
  638. extern tree objc_implementation_context;
  639.  
  640. struct imp_entry
  641. {
  642.   struct imp_entry *next;
  643.   tree imp_context;
  644.   tree imp_template;
  645.   tree class_decl;        /* _OBJC_CLASS_<my_name>; */
  646.   tree meta_decl;        /* _OBJC_METACLASS_<my_name>; */
  647. };
  648.  
  649. static void handle_impent            PROTO((struct imp_entry *));
  650.  
  651. static struct imp_entry *imp_list = 0;
  652. static int imp_count = 0;    /* `@implementation' */
  653. static int cat_count = 0;    /* `@category' */
  654. static int obj_count = 0;       /* Static objects */
  655. static int proto_count = 0;     /* Static protocols */
  656. static int sel_count = 0;       /* Selector reference count */
  657. static int no_objc = 1;         /* Remains 1 if no Objective-C */
  658.  
  659. static tree objc_class_template, objc_category_template, uprivate_record;
  660. static tree objc_protocol_template;
  661. static tree ucls_super_ref, uucls_super_ref;
  662.  
  663. static tree objc_method_template, objc_ivar_template;
  664. static tree objc_symtab_template, objc_module_template;
  665. static tree objc_super_template, objc_object_reference;
  666.  
  667. static tree objc_object_id, objc_class_id, objc_id_id;
  668. static int constantStringTypeSet = 0;
  669. static tree constant_string_id;
  670. static tree constant_string_id_old;
  671. static tree constant_string_id_new;
  672. static tree constant_string_global_id = 0;
  673. static tree constant_string_type;
  674. static tree constant_string_type_old;
  675. static tree constant_string_type_new;
  676. static tree string_class_decl = 0;
  677. static tree UOBJC_SUPER_decl;
  678. static tree protocol_id;
  679.  
  680. static tree method_context = NULL_TREE;
  681. static int  method_slot = 0;    /* used by start_method_def */
  682.  
  683. #define BUFSIZE        1024
  684.  
  685. static char *errbuf;    /* a buffer for error diagnostics */
  686.  
  687. /* data imported from tree.c */
  688.  
  689. extern struct obstack permanent_obstack,
  690.   *current_obstack, *rtl_obstack, *expression_obstack,
  691.   *function_maybepermanent_obstack;
  692.  
  693. /* data imported from toplev.c  */
  694.  
  695. extern char *dump_base_name;
  696.  
  697. /* Generate code for GNU or NeXT runtime environment.  */
  698.  
  699. #ifdef NEXT_OBJC_RUNTIME
  700. int flag_next_runtime = 1;
  701. #else
  702. int flag_next_runtime = 0;
  703. #endif
  704.  
  705. #if defined (NEXT_SEMANTICS) || defined (NEXT_PDO)
  706. static int flag_threeThreeMethodEncoding = 0;
  707. #endif
  708.  
  709. int flag_selector_table;
  710. int flag_class_references;
  711. int flag_static_objects;
  712.  
  713. int flag_typed_selectors;
  714.  
  715. /* Open and close the file for outputting class declarations, if requested.  */
  716.  
  717. int flag_gen_declaration = 0;
  718.  
  719. FILE *gen_declaration_file;
  720.  
  721. /* Warn if multiple methods are seen for the same selector, but with
  722.    different argument types. */
  723.  
  724. int warn_selector = 0;
  725.  
  726. /* Warn if methods required by a protocol are not implemented in the 
  727.    class adopting it.  When turned off, methods inherited to that
  728.    class are also considered implemented */
  729.  
  730. int flag_warn_protocol = 1;
  731.  
  732. /* Tells "encode_pointer/encode_aggregate" whether we are generating
  733.    type descriptors for instance variables (as opposed to methods).
  734.    Type descriptors for instance variables contain more information
  735.    than methods (for static typing and embedded structures). This
  736.    was added to support features being planned for dbkit2. */
  737.  
  738. static int generating_instance_variables = 0;
  739.  
  740. /* for use with extern "objective-c" { ... } */
  741.  
  742. #ifdef OBJCPLUS
  743. extern tree lang_name_objc;
  744. int doing_objc_thang;
  745. #endif
  746.  
  747. #ifdef OBJCPLUS
  748. void objc_lang_init ()
  749. #else
  750. void lang_init ()
  751. #endif
  752. {
  753. #ifndef OBJCPLUS
  754.   /* the beginning of the file is a new line; check for # */
  755.   /* With luck, we discover the real source file's name from that
  756.      and put it in input_filename.  */
  757.   ungetc (check_newline (), finput);
  758. #endif
  759.  
  760.   /* If gen_declaration desired, open the output file.  */
  761.   if (flag_gen_declaration)
  762.     {
  763.       int dump_base_name_length = strlen (dump_base_name);
  764.       register char *dumpname = (char *) xmalloc (dump_base_name_length + 7);
  765.       strcpy (dumpname, dump_base_name);
  766.       strcat (dumpname, ".decl");
  767.       gen_declaration_file = fopen (dumpname, "w");
  768.       if (gen_declaration_file == 0)
  769.     pfatal_with_name (dumpname);
  770.     }
  771.  
  772.   if (flag_next_runtime)
  773.     {
  774.       TAG_GETCLASS = "objc_getClass";
  775.       TAG_GETMETACLASS = "objc_getMetaClass";
  776.       TAG_GETORIGCLASS = "objc_getOrigClass";
  777.       TAG_MSGSEND = "objc_msgSend";
  778.       TAG_MSGSENDSUPER = "objc_msgSendSuper";
  779.       TAG_EXECCLASS = "__objc_execClass";
  780.       TAG_ATOM = "NXAtom";
  781. #ifdef NEXT_PDO
  782.       flag_selector_table = 1;
  783.       flag_class_references = 0;
  784.       flag_static_objects = 1;
  785. #else
  786.       flag_selector_table = 0;
  787.       flag_class_references = 1;
  788.       flag_static_objects = 0;
  789. #endif
  790.     }
  791.   else
  792.     {
  793.       TAG_GETCLASS = "objc_get_class";
  794.       TAG_GETMETACLASS = "objc_get_meta_class";
  795.       TAG_GETORIGCLASS = "objc_get_orig_class";
  796.       TAG_MSGSEND = "objc_msg_lookup";
  797.       TAG_MSGSENDSUPER = "objc_msg_lookup_super";
  798.       TAG_EXECCLASS = "__objc_exec_class";
  799.       TAG_ATOM = "GNUAtom";
  800.       flag_selector_table = 1;
  801.       flag_class_references = 0;
  802.     }
  803.  
  804. #ifndef OBJCPLUS
  805.   if (doing_objc_thang)
  806. #else
  807.     doing_objc_thang = 1;
  808. #endif
  809.     init_objc ();
  810. }
  811.  
  812. static void
  813. objc_fatal ()
  814. {
  815. #ifdef OBJCPLUS
  816.   fatal ("Objective-C text in C++ source file: use -x objective-c++");
  817. #else /* OBJCPLUS */
  818.   fatal ("Objective-C text in C source file: use -x objective-c");
  819. #endif
  820. }
  821.  
  822. void
  823. #ifdef OBJCPLUS
  824. objc_finish ()
  825. #else /* OBJCPLUS */
  826. finish_file ()
  827. #endif
  828. {
  829.   if (doing_objc_thang)
  830.     finish_objc ();        /* Objective-C finalization */
  831.  
  832.   if (gen_declaration_file)
  833.     fclose (gen_declaration_file);
  834. }
  835.  
  836. void
  837. #ifdef OBJCPLUS
  838. objc_lang_finish ()
  839. #else
  840.      lang_finish ()
  841. #endif
  842. {
  843. }
  844.  
  845. #ifndef OBJCPLUS
  846. char *
  847. lang_identify ()
  848. {
  849.   if (no_objc)
  850.     return "c";
  851.   else
  852.     return "objc";
  853. }
  854. #endif
  855.  
  856. int
  857. lang_decode_option (p)
  858.      char *p;
  859. {
  860.   if (!strcmp (p, "-fobjc")
  861. #ifdef NEXT_SEMANTICS
  862.       || !strcmp (p, "-ObjC")
  863.       || !strcmp (p, "-ObjC++")
  864. #endif
  865.       )
  866.     doing_objc_thang = 1;
  867.   else if (!strcmp (p, "-gen-decls"))
  868.     flag_gen_declaration = 1;
  869.   else if (!strcmp (p, "-Wselector"))
  870.     warn_selector = 1;
  871.   else if (!strcmp (p, "-Wno-selector"))
  872.     warn_selector = 0;
  873.   else if (!strcmp (p, "-Wprotocol"))
  874.     flag_warn_protocol = 1;
  875.   else if (!strcmp (p, "-Wno-protocol"))
  876.     flag_warn_protocol = 0;
  877.   else if (!strcmp (p, "-fgnu-runtime"))
  878.     flag_next_runtime = 0;
  879.   else if (!strcmp (p, "-fno-next-runtime"))
  880.     flag_next_runtime = 0;
  881.   else if (!strcmp (p, "-fno-gnu-runtime"))
  882.     flag_next_runtime = 1;
  883.   else if (!strcmp (p, "-fnext-runtime"))
  884.     flag_next_runtime = 1;
  885.   else if (!strcmp (p, "-fselector-table"))
  886.     flag_selector_table = 1;
  887. #ifdef NEXT_SEMANTICS /* this switch is only for OPENSTEP on Mach */
  888.   else if (!strcmp (p, "-threeThreeMethodEncoding"))
  889.      flag_threeThreeMethodEncoding = 1;
  890. #endif
  891.   else
  892. #ifdef OBJCPLUS
  893.     return cplus_decode_option (p);
  894. #else
  895.     return c_decode_option (p);
  896. #endif
  897.  
  898.   return 1;
  899. }
  900.  
  901. static tree
  902. define_decl (declarator, declspecs)
  903.      tree declarator;
  904.      tree declspecs;
  905. {
  906.   tree decl = start_decl (declarator, declspecs, 0);
  907.   finish_decl (decl, NULL_TREE, NULL_TREE);
  908.   return decl;
  909. }
  910.  
  911. /* Return 1 if LHS and RHS are compatible types for assignment or
  912.    various other operations.  Return 0 if they are incompatible, and
  913.    return -1 if we choose to not decide.  When the operation is
  914.    REFLEXIVE, check for compatibility in either direction.
  915.  
  916.    For statically typed objects, an assignment of the form `a' = `b'
  917.    is permitted if:
  918.  
  919.    `a' is of type "id",
  920.    `a' and `b' are the same class type, or
  921.    `a' and `b' are of class types A and B such that B is a descendant of A.  */
  922.  
  923. int
  924. maybe_objc_comptypes (lhs, rhs, reflexive)
  925.      tree lhs, rhs;
  926.      int reflexive;
  927. {
  928.   if (doing_objc_thang)
  929.     return objc_comptypes (lhs, rhs, reflexive);
  930.   return -1;
  931. }
  932.  
  933. static tree
  934. lookup_method_in_protocol_list (rproto_list, sel_name, class_meth)
  935.    tree rproto_list;
  936.    tree sel_name;
  937.    int class_meth;
  938. {
  939.    tree rproto, p;
  940.    tree fnd = 0;
  941.  
  942.    for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
  943.      {
  944.         p = TREE_VALUE (rproto);
  945.  
  946.     if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
  947.       {
  948.         if ((fnd = lookup_method (class_meth
  949.                       ? PROTOCOL_CLS_METHODS (p)
  950.                       : PROTOCOL_NST_METHODS (p), sel_name)))
  951.           ;
  952.         else if (PROTOCOL_LIST (p))
  953.           fnd = lookup_method_in_protocol_list (PROTOCOL_LIST (p),
  954.                             sel_name, class_meth);
  955.       }
  956.     else
  957.       ; /* An identifier...if we could not find a protocol.  */
  958.  
  959.     if (fnd)
  960.       return fnd;
  961.      }
  962. /* Turn this off for NeXT */
  963. #if 0
  964.    for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
  965.      {
  966.        p = TREE_VALUE (rproto);
  967.        if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
  968.      if (! PROTOCOL_DEFINED (p))
  969.        warning ("protocol definition for `%s' needed for typechecking",
  970.             IDENTIFIER_POINTER (PROTOCOL_NAME (p)));
  971.      }
  972. #endif  
  973.    return 0;
  974. }
  975.  
  976. static tree
  977. lookup_protocol_in_reflist (rproto_list, lproto)
  978.    tree rproto_list;
  979.    tree lproto;
  980. {
  981.    tree rproto, p;
  982.  
  983.    /* Make sure the protocol is support by the object on the rhs. */
  984.    if (TREE_CODE (lproto) == PROTOCOL_INTERFACE_TYPE)
  985.      {
  986.        tree fnd = 0;
  987.        for (rproto = rproto_list; rproto; rproto = TREE_CHAIN (rproto))
  988.      {
  989.        p = TREE_VALUE (rproto);
  990.  
  991.        if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
  992.          {
  993.            if (lproto == p)
  994.          fnd = lproto;
  995.  
  996.            else if (PROTOCOL_LIST (p))
  997.          fnd = lookup_protocol_in_reflist (PROTOCOL_LIST (p), lproto);
  998.          }
  999.  
  1000.        if (fnd)
  1001.          return fnd;
  1002.      }
  1003.      }
  1004.    else
  1005.      ; /* An identifier...if we could not find a protocol. */
  1006.  
  1007.    return 0;
  1008. }
  1009.  
  1010. /* Return 1 if LHS and RHS are compatible types for assignment
  1011.    or various other operations.  Return 0 if they are incompatible,
  1012.    and return -1 if we choose to not decide.  When the operation
  1013.    is REFLEXIVE, check for compatibility in either direction.  */
  1014.  
  1015. int
  1016. objc_comptypes (lhs, rhs, reflexive)
  1017.      tree lhs;
  1018.      tree rhs;
  1019.      int reflexive;
  1020. {
  1021.   /* New clause for protocols. */
  1022.  
  1023.   if (TREE_CODE (lhs) == POINTER_TYPE
  1024.       && TREE_CODE (TREE_TYPE (lhs)) == RECORD_TYPE
  1025.       && TREE_CODE (rhs) == POINTER_TYPE
  1026.       && TREE_CODE (TREE_TYPE (rhs)) == RECORD_TYPE)
  1027.     {
  1028.       int lhs_is_proto = IS_PROTOCOL_QUALIFIED_ID (lhs);
  1029.       int rhs_is_proto = IS_PROTOCOL_QUALIFIED_ID (rhs);
  1030.  
  1031.       if (lhs_is_proto)
  1032.         {
  1033.       tree lproto, lproto_list = TYPE_PROTOCOL_LIST (lhs);
  1034.       tree rproto, rproto_list;
  1035.       tree p;
  1036.  
  1037.       if (rhs_is_proto)
  1038.         {
  1039.           rproto_list = TYPE_PROTOCOL_LIST (rhs);
  1040.  
  1041.           /* Make sure the protocol is supported by the object
  1042.          on the rhs.  */
  1043.           for (lproto = lproto_list; lproto; lproto = TREE_CHAIN (lproto))
  1044.         {
  1045.           p = TREE_VALUE (lproto);
  1046.           rproto = lookup_protocol_in_reflist (rproto_list, p);
  1047.  
  1048.           if (!rproto)
  1049.             warning ("object does not conform to the `%s' protocol",
  1050.                  IDENTIFIER_POINTER (PROTOCOL_NAME (p)));
  1051.         }
  1052.         }
  1053.       else if (TYPED_OBJECT (TREE_TYPE (rhs)))
  1054.         {
  1055.           tree rname = TYPE_NAME (TREE_TYPE (rhs));
  1056.           tree rinter;
  1057.  
  1058.           /* Make sure the protocol is supported by the object
  1059.          on the rhs.  */
  1060.           for (lproto = lproto_list; lproto; lproto = TREE_CHAIN (lproto))
  1061.         {
  1062.           p = TREE_VALUE (lproto);
  1063.           rproto = 0;
  1064.           rinter = lookup_interface (rname);
  1065.  
  1066.           while (rinter && !rproto)
  1067.             {
  1068.               tree cat;
  1069.  
  1070.               rproto_list = CLASS_PROTOCOL_LIST (rinter);
  1071.               rproto = lookup_protocol_in_reflist (rproto_list, p);
  1072.  
  1073.               /* Check for protocols adopted by categories. */
  1074.               cat = CLASS_CATEGORY_LIST (rinter);
  1075.               while (cat && !rproto)
  1076.             {
  1077.               rproto_list = CLASS_PROTOCOL_LIST (cat);
  1078.               rproto = lookup_protocol_in_reflist (rproto_list, p);
  1079.  
  1080.               cat = CLASS_CATEGORY_LIST (cat);
  1081.             }
  1082.  
  1083.               rinter = lookup_interface (CLASS_SUPER_NAME (rinter));
  1084.             }
  1085.           if (!rproto)
  1086.             warning ("class `%s' does not implement the `%s' protocol",
  1087.                          IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (rhs))),
  1088.                      IDENTIFIER_POINTER (PROTOCOL_NAME (p)));
  1089.         }
  1090.         }
  1091.  
  1092.       /* May change...based on whether there was any mismatch */
  1093.           return 1;
  1094.         }
  1095.       else if (rhs_is_proto)
  1096.         {
  1097.       /* lhs is not a protocol...warn if it is statically typed */
  1098.  
  1099.       if (TYPED_OBJECT (TREE_TYPE (lhs)))
  1100.         return 0;
  1101.       else
  1102.         return 1;    /*  One of the types is a protocol */
  1103.     }
  1104.       else
  1105.     /*  Defer to comptypes .*/
  1106.     return -1;
  1107.     }
  1108.  
  1109.   else if (TREE_CODE (lhs) == RECORD_TYPE && TREE_CODE (rhs) == RECORD_TYPE)
  1110.     ; /* Fall thru.  This is the case we have been handling all along */
  1111.   else
  1112.     /* Defer to comptypes. */
  1113.     return -1;
  1114.  
  1115.   /* `id' = `<class> *', `<class> *' = `id' */
  1116.  
  1117.   if ((TYPE_NAME (lhs) == objc_object_id && TYPED_OBJECT (rhs))
  1118.       || (TYPE_NAME (rhs) == objc_object_id && TYPED_OBJECT (lhs)))
  1119.     return 1;
  1120.  
  1121.   /* `id' = `Class', `Class' = `id' */
  1122.  
  1123.   else if ((TYPE_NAME (lhs) == objc_object_id
  1124.         && TYPE_NAME (rhs) == objc_class_id)
  1125.        || (TYPE_NAME (lhs) == objc_class_id
  1126.            && TYPE_NAME (rhs) == objc_object_id))
  1127.     return 1;
  1128.  
  1129.   /* `<class> *' = `<class> *' */
  1130.  
  1131.   else if (TYPED_OBJECT (lhs) && TYPED_OBJECT (rhs))
  1132.     {
  1133.       tree lname = TYPE_NAME (lhs);
  1134.       tree rname = TYPE_NAME (rhs);
  1135.       tree inter;
  1136.  
  1137.       if (lname == rname)
  1138.     return 1;
  1139.  
  1140.       /* If the left hand side is a super class of the right hand side,
  1141.      allow it.  */
  1142.       for (inter = lookup_interface (rname); inter;
  1143.        inter = lookup_interface (CLASS_SUPER_NAME (inter)))
  1144.     if (lname == CLASS_SUPER_NAME (inter))
  1145.       return 1;
  1146.  
  1147.       /* Allow the reverse when reflexive.  */
  1148.       if (reflexive)
  1149.     for (inter = lookup_interface (lname); inter;
  1150.          inter = lookup_interface (CLASS_SUPER_NAME (inter)))
  1151.       if (rname == CLASS_SUPER_NAME (inter))
  1152.         return 1;
  1153.  
  1154.       return 0;
  1155.     }
  1156.   else
  1157.     /* Defer to comptypes. */
  1158.     return -1;
  1159. }
  1160.  
  1161. /* Called from c-decl.c before all calls to rest_of_decl_compilation.  */
  1162.  
  1163. void
  1164. objc_check_decl (decl)
  1165.      tree decl;
  1166. {
  1167.   tree type = TREE_TYPE (decl);
  1168.  
  1169.   if (TREE_CODE (type) == RECORD_TYPE
  1170.       && TREE_STATIC_TEMPLATE (type)
  1171.       && type != constant_string_type)
  1172.     {
  1173.       error_with_decl (decl, "`%s' cannot be statically allocated");
  1174.       fatal ("statically allocated objects not supported");
  1175.     }
  1176. }
  1177.  
  1178. void
  1179. maybe_objc_check_decl (decl)
  1180.      tree decl;
  1181. {
  1182.   if (doing_objc_thang)
  1183.     objc_check_decl (decl);
  1184. }
  1185.  
  1186. /* Implement static typing.  At this point, we know we have an interface.  */
  1187.  
  1188. tree
  1189. get_static_reference (interface, protocols)
  1190.      tree interface;
  1191.      tree protocols;
  1192. {
  1193.   tree type = xref_tag (RECORD_TYPE, interface);
  1194.  
  1195.   if (protocols)
  1196.     {
  1197.       tree t, m = TYPE_MAIN_VARIANT (type);
  1198.       struct obstack *ambient_obstack = current_obstack;
  1199.  
  1200.       current_obstack = &permanent_obstack;
  1201.       t = copy_node (type);
  1202.       TYPE_BINFO (t) = make_tree_vec (2);
  1203.  
  1204. #if 0 /* See comments for corresponding lines in get_object_reference().  */
  1205.       /* Add this type to the chain of variants of TYPE.  */
  1206.       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
  1207.       TYPE_NEXT_VARIANT (m) = t;
  1208. #endif
  1209.  
  1210.       current_obstack = ambient_obstack;
  1211.  
  1212.       /* Look up protocols and install in lang specific list.  */
  1213.       TYPE_PROTOCOL_LIST (t) = lookup_and_install_protocols (protocols);
  1214.  
  1215.       /* This forces a new pointer type to be created later
  1216.      (in build_pointer_type)...so that the new template
  1217.      we just created will actually be used...what a hack!  */
  1218.       if (TYPE_POINTER_TO (t))
  1219.     TYPE_POINTER_TO (t) = NULL;
  1220.  
  1221.       type = t;
  1222.     }
  1223.  
  1224.   return type;
  1225. }
  1226.  
  1227. int
  1228. is_protocol_qualified_id (type)
  1229.      tree type;
  1230. {
  1231.   return IS_PROTOCOL_QUALIFIED_ID (type);
  1232. }
  1233.  
  1234. tree
  1235. get_object_reference (protocols)
  1236.      tree protocols;
  1237. {
  1238.   tree type_decl = lookup_name (objc_id_id);
  1239.   tree type;
  1240.  
  1241.   if (type_decl && TREE_CODE (type_decl) == TYPE_DECL)
  1242.     {
  1243.       type = TREE_TYPE (type_decl);
  1244.       if (TYPE_MAIN_VARIANT (type) != id_type)
  1245.     warning ("Unexpected type for `id' (%s)",
  1246.         gen_declaration (type, errbuf));
  1247.     }
  1248.   else
  1249.     {
  1250.       fatal ("Undefined type `id', please import <objc/objc.h>");
  1251.     }
  1252.  
  1253.   /* This clause creates a new pointer type that is qualified with
  1254.      the protocol specification...this info is used later to do more
  1255.      elaborate type checking.  */
  1256.  
  1257.   if (protocols)
  1258.     {
  1259.       tree t, m = TYPE_MAIN_VARIANT (type);
  1260.       /* It's probably not necessary to switch obstacks, since copy_node seems
  1261.      to ensure that the copied node is from the same obstack as the
  1262.      original, regardless of what the current obstack is.  */
  1263.       struct obstack *ambient_obstack = current_obstack;
  1264.  
  1265.       current_obstack = &permanent_obstack;
  1266.       t = copy_node (type);
  1267.       /* The following line is probably not needed.  */
  1268.       TYPE_BINFO (t) = make_tree_vec (2);
  1269.  
  1270. #if 0
  1271.       /* The purpose of these two lines seems to be to avoid duplicating types
  1272.      when the same type occurs multiple times in a source file.  However,
  1273.      it seems this code doesn't quite achieve this, and can cause problems
  1274.      later on, since the wrong type information may be retrieved, and the
  1275.      compiler can crash due to dangling pointers.  */
  1276.       /* Add this type to the chain of variants of TYPE.  */
  1277.       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
  1278.       TYPE_NEXT_VARIANT (m) = t;
  1279. #endif
  1280.  
  1281.       current_obstack = ambient_obstack;
  1282.  
  1283.       /* Look up protocols...and install in lang specific list.  Note that
  1284.      the protocol list can have a different lifetime than T!  */
  1285.       TYPE_PROTOCOL_LIST (t) = lookup_and_install_protocols (protocols);
  1286.  
  1287.       /* This forces a new pointer type to be created later
  1288.      (in build_pointer_type)...so that the new template
  1289.      we just created will actually be used...what a hack!  */
  1290.       if (TYPE_POINTER_TO (t))
  1291.     TYPE_POINTER_TO (t) = NULL;
  1292.  
  1293.       type = t;
  1294.     }
  1295.   return type;
  1296. }
  1297.  
  1298. static tree
  1299. lookup_and_install_protocols (protocols)
  1300.      tree protocols;
  1301. {
  1302.   tree proto;
  1303.   tree prev = NULL;
  1304.   tree return_value = protocols;
  1305.  
  1306.   for (proto = protocols; proto; proto = TREE_CHAIN (proto))
  1307.     {
  1308.       tree ident = TREE_VALUE (proto);
  1309.       tree p = lookup_protocol (ident);
  1310.  
  1311.       if (!p)
  1312.     {
  1313.       error ("Cannot find protocol declaration for `%s'",
  1314.          IDENTIFIER_POINTER (ident));
  1315.       if (prev)
  1316.         TREE_CHAIN (prev) = TREE_CHAIN (proto);
  1317.       else
  1318.         return_value = TREE_CHAIN (proto);
  1319.     }
  1320.       else
  1321.     {
  1322.       /* Replace identifier with actual protocol node. */
  1323.       TREE_VALUE (proto) = p;
  1324.       prev = proto;
  1325.     }
  1326.     }
  1327.   return return_value;
  1328. }
  1329.  
  1330. /* Create and push a decl for a built-in external variable or field NAME.
  1331.    CODE says which.
  1332.    TYPE is its data type.  */
  1333.  
  1334. static tree
  1335. create_builtin_decl (code, type, name)
  1336.      enum tree_code code;
  1337.      tree type;
  1338.      char *name;
  1339. {
  1340. #ifdef OBJCPLUS
  1341.   tree decl = build_lang_field_decl (code, get_identifier (name), type);
  1342. #else
  1343.   tree decl = build_decl (code, get_identifier (name), type);
  1344. #endif
  1345.   if (code == VAR_DECL)
  1346.     {
  1347.       TREE_STATIC (decl) = 1;
  1348.       make_decl_rtl (decl, 0, 1);
  1349.       pushdecl (decl);
  1350.     }
  1351.  
  1352.   DECL_ARTIFICIAL (decl) = 1;
  1353.   return decl;
  1354. }
  1355.  
  1356. static void
  1357. setup_string_decl ()
  1358. {
  1359.   if (!string_class_decl) 
  1360.   {
  1361.     if (!constant_string_global_id)
  1362.     {
  1363.     constant_string_global_id = get_identifier(STRING_OBJECT_GLOBAL_NAME);
  1364.     if (constant_string_global_id == NULL_TREE)
  1365.     {
  1366.         return;
  1367.     }
  1368.     }
  1369.     string_class_decl = lookup_name(constant_string_global_id);
  1370.   }
  1371. }
  1372.  
  1373. /* purpose: "play" parser, creating/installing representations
  1374.    of the declarations that are required by Objective-C.
  1375.  
  1376.    Model:
  1377.  
  1378.      type_spec--------->sc_spec
  1379.      (tree_list)        (tree_list)
  1380.          |                  |
  1381.          |                  |
  1382.      identifier_node    identifier_node  */
  1383.  
  1384. static void
  1385. synth_module_prologue ()
  1386. {
  1387.   tree temp_type;
  1388.   tree super_p;
  1389.  
  1390. #ifdef OBJCPLUS
  1391.   push_lang_context (lang_name_c); /* extern "C" */
  1392. #endif
  1393.  
  1394.   /* defined in `objc.h' */
  1395.   objc_object_id = get_identifier (TAG_OBJECT);
  1396.  
  1397.   objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
  1398.  
  1399.   id_type = build_pointer_type (objc_object_reference);
  1400.  
  1401.   objc_id_id = get_identifier (TYPE_ID);
  1402.   objc_class_id = get_identifier (TAG_CLASS);
  1403.  
  1404.   objc_class_type = build_pointer_type (xref_tag (RECORD_TYPE, objc_class_id));
  1405.   protocol_type = build_pointer_type (xref_tag (RECORD_TYPE,
  1406.                 get_identifier (PROTOCOL_OBJECT_CLASS_NAME)));
  1407. #ifdef OBJCPLUS
  1408.   objc_module_type = build_pointer_type (xref_tag (RECORD_TYPE, get_identifier ("_OBJC_MODULES")));
  1409. #endif
  1410.   /* Declare type of selector-objects that represent an operation name.  */
  1411.  
  1412. #ifdef OBJC_INT_SELECTORS
  1413.   /* `unsigned int' */
  1414.   selector_type = unsigned_type_node;
  1415. #else
  1416.   /* `struct objc_selector *' */
  1417.   selector_type
  1418.     = build_pointer_type (xref_tag (RECORD_TYPE,
  1419.                     get_identifier (TAG_SELECTOR)));
  1420. #endif /* not OBJC_INT_SELECTORS */
  1421.  
  1422.   /* Forward declare type, or else the prototype for msgSendSuper will
  1423.      complain.  */
  1424.  
  1425.   super_p = build_pointer_type (xref_tag (RECORD_TYPE,
  1426.                       get_identifier (TAG_SUPER)));
  1427.  
  1428.  
  1429.   /* id objc_msgSend (id, SEL, ...); */
  1430.  
  1431.   temp_type
  1432.     = build_function_type (id_type,
  1433.                tree_cons (NULL_TREE, id_type,
  1434.                       tree_cons (NULL_TREE, selector_type,
  1435.                          NULL_TREE)));
  1436.  
  1437.   if (! flag_next_runtime)
  1438.     {
  1439.       umsg_decl = build_decl (FUNCTION_DECL,
  1440.                   get_identifier (TAG_MSGSEND), temp_type);
  1441.       DECL_EXTERNAL (umsg_decl) = 1;
  1442.       TREE_PUBLIC (umsg_decl) = 1;
  1443.       DECL_INLINE (umsg_decl) = 1;
  1444.       DECL_ARTIFICIAL (umsg_decl) = 1;
  1445.  
  1446.       if (flag_traditional && TAG_MSGSEND[0] != '_')
  1447.     DECL_BUILT_IN_NONANSI (umsg_decl) = 1;
  1448.  
  1449.       make_decl_rtl (umsg_decl, NULL_PTR, 1);
  1450.       pushdecl (umsg_decl);
  1451.     }
  1452.   else
  1453.     umsg_decl = builtin_function (TAG_MSGSEND, temp_type, NOT_BUILT_IN, 0);
  1454.  
  1455.   /* id objc_msgSendSuper (struct objc_super *, SEL, ...); */
  1456.  
  1457.   temp_type
  1458.     = build_function_type (id_type,
  1459.                tree_cons (NULL_TREE, super_p,
  1460.                       tree_cons (NULL_TREE, selector_type,
  1461.                          NULL_TREE)));
  1462.  
  1463.   umsg_super_decl = builtin_function (TAG_MSGSENDSUPER,
  1464.                      temp_type, NOT_BUILT_IN, 0);
  1465.  
  1466.   /* id objc_getClass (const char *); */
  1467.  
  1468. #ifdef OBJCPLUS
  1469.   temp_type = build_function_type (id_type,
  1470.             tree_cons (NULL_TREE,
  1471.                    const_string_type_node,
  1472.                    void_list_node));
  1473. #else
  1474.   temp_type = build_function_type (id_type,
  1475.             tree_cons (NULL_TREE,
  1476.                    const_string_type_node,
  1477.                    tree_cons (NULL_TREE, void_type_node, NULL_TREE)));
  1478. #endif
  1479.  
  1480.   objc_get_class_decl
  1481.     = builtin_function (TAG_GETCLASS, temp_type, NOT_BUILT_IN, 0);
  1482.  
  1483.   objc_get_orig_class_decl
  1484.     = builtin_function (TAG_GETORIGCLASS, temp_type, NOT_BUILT_IN, 0);
  1485.  
  1486.   /* id objc_getMetaClass (const char *); */
  1487.  
  1488.   objc_get_meta_class_decl
  1489.     = builtin_function (TAG_GETMETACLASS, temp_type, NOT_BUILT_IN, 0);
  1490.  
  1491.   /* static SEL _OBJC_SELECTOR_TABLE[]; */
  1492.  
  1493. #ifdef OBJCPLUS
  1494.   {
  1495.     extern tree unsigned_char_type_node;
  1496.     temp_type = build_array_type (selector_type, unsigned_char_type_node);
  1497.   }
  1498.   /* temp_type = build_pointer_type (selector_type); */
  1499. #else
  1500.   temp_type = build_array_type (selector_type, NULL_TREE);
  1501. #endif
  1502.   layout_type (temp_type);
  1503.  
  1504.   if (flag_selector_table)
  1505.     {
  1506.       UOBJC_SELECTOR_TABLE_decl
  1507.     = create_builtin_decl (VAR_DECL, temp_type,
  1508.                    "_OBJC_SELECTOR_TABLE");
  1509.       DECL_SIZE (UOBJC_SELECTOR_TABLE_decl) = 0;
  1510.       TREE_USED (UOBJC_SELECTOR_TABLE_decl) = 1;
  1511.     }
  1512.  
  1513.   generate_forward_declaration_to_string_table ();
  1514.  
  1515.   /* Forward declare constant_string_id and constant_string_type.  */
  1516.   constant_string_id_old = get_identifier (STRING_OBJECT_CLASS_NAME_OLD);
  1517.   constant_string_id_new = get_identifier (STRING_OBJECT_CLASS_NAME_NEW);
  1518.   constant_string_type_old = xref_tag (RECORD_TYPE, constant_string_id_old);
  1519.   constant_string_type_new = xref_tag (RECORD_TYPE, constant_string_id_new);
  1520.        
  1521.   protocol_id = get_identifier (PROTOCOL_OBJECT_CLASS_NAME);
  1522.  
  1523. #ifdef OBJCPLUS
  1524.   pop_lang_context ();
  1525. #endif
  1526. }
  1527.  
  1528. /* Custom build_string which sets TREE_TYPE!  */
  1529.  
  1530. static tree
  1531. my_build_string (len, str)
  1532.      int len;
  1533.      char *str;
  1534. {
  1535.   int wide_flag = 0;
  1536.   tree a_string = build_string (len, str);
  1537.  
  1538.   /* Some code from combine_strings, which is local to c-parse.y.  */
  1539.   if (TREE_TYPE (a_string) == int_array_type_node)
  1540.     wide_flag = 1;
  1541.  
  1542.   TREE_TYPE (a_string)
  1543.     = build_array_type (wide_flag ? integer_type_node : char_type_node,
  1544.             build_index_type (build_int_2 (len - 1, 0)));
  1545.  
  1546.   TREE_CONSTANT (a_string) = 1;    /* Puts string in the readonly segment */
  1547.   TREE_STATIC (a_string) = 1;
  1548.  
  1549.   return a_string;
  1550. }
  1551.  
  1552. /* Return a newly constructed OBJC_STRING_CST node whose value is
  1553.    the LEN characters at STR.
  1554.    The TREE_TYPE is not initialized.  */
  1555.  
  1556. tree
  1557. build_objc_string (len, str)
  1558.      int len;
  1559.      char *str;
  1560. {
  1561.   tree s = build_string (len, str);
  1562.  
  1563.   TREE_SET_CODE (s, OBJC_STRING_CST);
  1564.   return s;
  1565. }
  1566.  
  1567. /* Given a chain of OBJC_STRING_CST's, build a static instance of
  1568.    NXConstantString which points at the concatenation of those strings.
  1569.    We place the string object in the __string_objects section of the
  1570.    __OBJC segment.  The Objective-C runtime will initialize the isa
  1571.    pointers of the string objects to point at the NXConstantString
  1572.    class object.  */
  1573.  
  1574. tree
  1575. build_objc_string_object (strings)
  1576.      tree strings;
  1577. {
  1578.   tree string, initlist, constructor;
  1579.   int length;
  1580.  
  1581.   if (!doing_objc_thang)
  1582.     objc_fatal ();
  1583.  
  1584.   if (!constantStringTypeSet)
  1585.     {
  1586.       if (lookup_interface (constant_string_id_new) == NULL_TREE)
  1587.     {
  1588.       /* Take a shot at the old NXConstantString, in case
  1589.          this is some of our own 3.X compatibility code  */
  1590.       if (lookup_interface (constant_string_id_old) == NULL_TREE)
  1591.         {
  1592.           /* Complain about new constant string type, though  */ 
  1593.           error ("Cannot find interface declaration for `%s'",
  1594.              IDENTIFIER_POINTER (constant_string_id_new));
  1595.           return error_mark_node;
  1596.         }
  1597.       /*warning ("Creating static NXConstantString");*/
  1598.       constant_string_id = constant_string_id_old;
  1599.       constant_string_type = constant_string_type_old;
  1600.       constantStringTypeSet = 1;
  1601.       add_class_reference (constant_string_id);
  1602.     }
  1603.       else
  1604.     {
  1605.       constant_string_id = constant_string_id_new;
  1606.       constant_string_type = constant_string_type_new;
  1607.       constantStringTypeSet = 1;
  1608.     }
  1609.     }
  1610.   
  1611.   /* combine_strings will work for OBJC_STRING_CST's too.  */
  1612.   string = combine_strings (strings);
  1613.   TREE_SET_CODE (string, STRING_CST);
  1614.   length = TREE_STRING_LENGTH (string) - 1;
  1615.  
  1616.   /* & ((NXConstantString) {0, string, length})  */
  1617.   {
  1618.      /* Make this entire new node constant, since we will
  1619.         need to take the address of it later... */
  1620.      
  1621.      struct obstack *save_current_obstack    = current_obstack;
  1622.      struct obstack *save_expression_obstack = expression_obstack;
  1623.  
  1624.      current_obstack    = &permanent_obstack;
  1625.      expression_obstack = &permanent_obstack;
  1626.  
  1627.      string = copy_node (string);
  1628.  
  1629. #ifdef NEXT_SEMANTICS
  1630.    if (constant_string_id == constant_string_id_old)
  1631. #endif
  1632.      initlist = build_tree_list (NULL_TREE, build_int_2 (0, 0));
  1633. #ifdef NEXT_SEMANTICS
  1634.    else
  1635.      {
  1636.        setup_string_decl();
  1637.        if (string_class_decl == NULL_TREE)
  1638.      {
  1639.        error ("Cannot find reference tag for class `%s'",
  1640.           IDENTIFIER_POINTER (constant_string_id_new));
  1641.        return error_mark_node;
  1642.      }
  1643.        initlist = build_tree_list 
  1644.      (NULL_TREE,
  1645.       copy_node (build_unary_op (ADDR_EXPR,    string_class_decl, 0)));
  1646.      }
  1647. #endif
  1648.      initlist = tree_cons (NULL_TREE, build_unary_op (ADDR_EXPR, string, 1),
  1649.                initlist);
  1650.      initlist = tree_cons (NULL_TREE, build_int_2 (length, 0), initlist);
  1651.      constructor = build_constructor (constant_string_type,
  1652.                       nreverse (initlist));
  1653.  
  1654.      /* This puts a pointer to the static NXConstantString object in a 
  1655.     table for the runtime. */
  1656.      constructor = copy_node (constructor);
  1657.      add_static_object (constructor, constant_string_id);
  1658.  
  1659.      /* return the address of this NXConstantString */
  1660.      constructor = build_unary_op (ADDR_EXPR, constructor, 1);
  1661.  
  1662.      current_obstack = save_current_obstack;
  1663.      expression_obstack = save_expression_obstack;
  1664.    }
  1665.  
  1666.   return constructor;
  1667. }
  1668.  
  1669. /* Build a static constant CONSTRUCTOR
  1670.    with type TYPE and elements ELTS.  */
  1671.  
  1672. static tree
  1673. build_constructor (type, elts)
  1674.      tree type, elts;
  1675. {
  1676.   tree constructor = build (CONSTRUCTOR, type, NULL_TREE, elts);
  1677.  
  1678.   TREE_CONSTANT (constructor) = 1;
  1679.   TREE_STATIC (constructor) = 1;
  1680.   TREE_READONLY (constructor) = 1;
  1681.  
  1682.   return constructor;
  1683. }
  1684.  
  1685. /* Take care of defining and initializing _OBJC_SYMBOLS.  */
  1686.  
  1687. /* Predefine the following data type:
  1688.  
  1689.    struct _objc_symtab
  1690.    {
  1691.      long sel_ref_cnt;
  1692.      SEL *refs;
  1693.      short cls_def_cnt;
  1694.      short cat_def_cnt;
  1695.      if (flag_static_objects)
  1696.        int obj_def_cnt;
  1697.        int proto_def_cnt;
  1698.      void *defs[cls_def_cnt + cat_def_cnt + obj_def_cnt + proto_def_cnt];
  1699.    }; */
  1700.  
  1701. static void
  1702. build_objc_symtab_template ()
  1703. {
  1704.   tree field_decl, field_decl_chain, index;
  1705.  
  1706.   objc_symtab_template
  1707.     = start_struct (RECORD_TYPE, get_identifier (UTAG_SYMTAB));
  1708.  
  1709.   /* long sel_ref_cnt; */
  1710.  
  1711.   field_decl = create_builtin_decl (FIELD_DECL,
  1712.                     long_integer_type_node,
  1713.                     "sel_ref_cnt");
  1714.   field_decl_chain = field_decl;
  1715.  
  1716.   /* SEL *refs; */
  1717.  
  1718.   field_decl = create_builtin_decl (FIELD_DECL,
  1719.                     build_pointer_type (selector_type),
  1720.                     "refs");
  1721.   chainon (field_decl_chain, field_decl);
  1722.  
  1723.   /* short cls_def_cnt; */
  1724.  
  1725.   field_decl = create_builtin_decl (FIELD_DECL,
  1726.                     short_integer_type_node,
  1727.                     "cls_def_cnt");
  1728.   chainon (field_decl_chain, field_decl);
  1729.  
  1730.   /* short cat_def_cnt; */
  1731.  
  1732.   field_decl = create_builtin_decl (FIELD_DECL,
  1733.                     short_integer_type_node,
  1734.                     "cat_def_cnt");
  1735.   chainon (field_decl_chain, field_decl);
  1736.  
  1737.   /* void *defs[cls_def_cnt + cat_def_cnt + obj_def_cnt + proto_def_cnt]; */
  1738.  
  1739.    if (flag_static_objects)
  1740.      {
  1741.        /* int obj_def_cnt; */
  1742.  
  1743.        field_decl = create_builtin_decl (FIELD_DECL,
  1744.                      long_integer_type_node,
  1745.                      "obj_def_cnt");
  1746.        chainon (field_decl_chain, field_decl);
  1747.  
  1748.        /* int proto_def_cnt; */
  1749.  
  1750.        field_decl = create_builtin_decl (FIELD_DECL,
  1751.                      long_integer_type_node,
  1752.                      "proto_def_cnt");
  1753.        chainon (field_decl_chain, field_decl);
  1754.      }
  1755.  
  1756.    index
  1757.      = build_index_type (build_int_2 (imp_count + cat_count 
  1758.                       + obj_count + proto_count - 1,
  1759.                   (imp_count == 0 
  1760.                   && cat_count == 0
  1761.                   && obj_count == 0
  1762.                   && proto_count == 0)
  1763.                   ? -1 : 0));
  1764.  
  1765.   field_decl = create_builtin_decl (FIELD_DECL,
  1766.                     build_array_type (ptr_type_node, index),
  1767.                     "defs");
  1768.   chainon (field_decl_chain, field_decl);
  1769.  
  1770.   finish_struct (objc_symtab_template, field_decl_chain);
  1771. }
  1772.  
  1773. /* Create the initial value for the `defs' field of _objc_symtab.
  1774.    This is a CONSTRUCTOR.  */
  1775.  
  1776. static tree
  1777. init_def_list (type)
  1778.      tree type;
  1779. {
  1780.   tree expr, initlist = NULL_TREE;
  1781.   struct imp_entry *impent;
  1782.  
  1783.   if (imp_count)
  1784.     for (impent = imp_list; impent; impent = impent->next)
  1785.       {
  1786.     if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
  1787.       {
  1788.         expr = build_unary_op (ADDR_EXPR, impent->class_decl, 0);
  1789.         initlist = tree_cons (NULL_TREE, expr, initlist);
  1790.       }
  1791.       }
  1792.  
  1793.   if (cat_count)
  1794.     for (impent = imp_list; impent; impent = impent->next)
  1795.       {
  1796.     if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
  1797.       {
  1798.         expr = build_unary_op (ADDR_EXPR, impent->class_decl, 0);
  1799.         initlist = tree_cons (NULL_TREE, expr, initlist);
  1800.       }
  1801.       }
  1802.  
  1803. #ifdef NEXT_PDO
  1804.    if (obj_count)
  1805.      {
  1806.        tree elem;
  1807.        for (elem = obj_def_chain; elem; elem = TREE_CHAIN (elem))
  1808.      {
  1809.        initlist = tree_cons (NULL_TREE, TREE_VALUE (elem), initlist);
  1810.      }
  1811.      }
  1812.  
  1813.    if (proto_count)
  1814.      {
  1815.        tree elem;
  1816.        for (elem = proto_def_chain; elem; elem = TREE_CHAIN (elem))
  1817.      {
  1818.        initlist = tree_cons (NULL_TREE, TREE_VALUE (elem), initlist);
  1819.      }
  1820.      }
  1821. #endif  /*  NEXT_PDO */
  1822.  
  1823.   return build_constructor (type, nreverse (initlist));
  1824. }
  1825.  
  1826. /* Construct the initial value for all of _objc_symtab.  */
  1827.  
  1828. static tree
  1829. init_objc_symtab (type)
  1830.      tree type;
  1831. {
  1832.   tree initlist;
  1833.  
  1834.   /* sel_ref_cnt = { ..., 5, ... } */
  1835.  
  1836.   initlist = build_tree_list (NULL_TREE, build_int_2 (sel_count, 0));
  1837.  
  1838.   /* refs = { ..., _OBJC_SELECTOR_TABLE, ... } */
  1839.  
  1840.   if (! flag_selector_table || ! sel_ref_chain)
  1841.     initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  1842.   else
  1843.     initlist = tree_cons (NULL_TREE,
  1844.               build_unary_op (ADDR_EXPR,
  1845.                       UOBJC_SELECTOR_TABLE_decl, 1),
  1846.               initlist);
  1847.  
  1848.   /* cls_def_cnt = { ..., 5, ... } */
  1849.  
  1850.   initlist = tree_cons (NULL_TREE, build_int_2 (imp_count, 0), initlist);
  1851.  
  1852.   /* cat_def_cnt = { ..., 5, ... } */
  1853.  
  1854.   initlist = tree_cons (NULL_TREE, build_int_2 (cat_count, 0), initlist);
  1855.    
  1856.   /* obj_def_cnt = { ..., 6, ...} */
  1857.  
  1858.   /* proto_def_cnt = { ..., 6, ...} */
  1859.  
  1860.   if (flag_static_objects) {
  1861.     initlist = tree_cons (NULL_TREE, build_int_2 (obj_count, 0), initlist);
  1862.     initlist = tree_cons (NULL_TREE, build_int_2 (proto_count, 0), initlist);
  1863.   }
  1864.  
  1865.   /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
  1866.  
  1867.   if (imp_count || cat_count || obj_count || proto_count)
  1868.     {
  1869.       tree field = TYPE_FIELDS (type);
  1870.       while (TREE_CHAIN (field))
  1871.     field = TREE_CHAIN (field);
  1872.  
  1873.       initlist = tree_cons (NULL_TREE, init_def_list (TREE_TYPE (field)),
  1874.                 initlist);
  1875.     }
  1876.  
  1877.   return build_constructor (type, nreverse (initlist));
  1878. }
  1879.  
  1880. /* Push forward-declarations of all the categories
  1881.    so that init_def_list can use them in a CONSTRUCTOR.  */
  1882.  
  1883. static void
  1884. forward_declare_categories ()
  1885. {
  1886.   struct imp_entry *impent;
  1887.   tree sav = implementation_context;
  1888.  
  1889.   for (impent = imp_list; impent; impent = impent->next)
  1890.     {
  1891.       if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
  1892.     {
  1893.       /* Set an invisible arg to synth_id_with_class_suffix.  */
  1894.       implementation_context = impent->imp_context;
  1895.       impent->class_decl
  1896.         = create_builtin_decl (VAR_DECL, objc_category_template,
  1897.                    IDENTIFIER_POINTER (synth_id_with_class_suffix ("_OBJC_CATEGORY", implementation_context)));
  1898.     }
  1899.     }
  1900.   implementation_context = sav;
  1901. }
  1902.  
  1903. /* Create the declaration of _OBJC_SYMBOLS, with type `strict _objc_symtab'
  1904.    and initialized appropriately.  */
  1905.  
  1906. static void
  1907. generate_objc_symtab_decl ()
  1908. {
  1909.   tree sc_spec;
  1910.  
  1911.   if (!objc_category_template)
  1912.     build_category_template ();
  1913.  
  1914.   /* forward declare categories */
  1915.   if (cat_count)
  1916.     forward_declare_categories ();
  1917.  
  1918.   if (!objc_symtab_template)
  1919.     build_objc_symtab_template ();
  1920.  
  1921.   sc_spec = build_tree_list (NULL_TREE, ridpointers[(int) RID_STATIC]);
  1922.  
  1923.   UOBJC_SYMBOLS_decl = start_decl (get_identifier ("_OBJC_SYMBOLS"),
  1924.                    tree_cons (NULL_TREE, objc_symtab_template, sc_spec), 1);
  1925.  
  1926.   end_temporary_allocation ();    /* start_decl trying to be smart about inits */
  1927.   TREE_USED (UOBJC_SYMBOLS_decl) = 1;
  1928.   DECL_IGNORED_P (UOBJC_SYMBOLS_decl) = 1;
  1929.   DECL_ARTIFICIAL (UOBJC_SYMBOLS_decl) = 1;
  1930.   finish_decl (UOBJC_SYMBOLS_decl,
  1931.            init_objc_symtab (TREE_TYPE (UOBJC_SYMBOLS_decl)),
  1932.            NULL_TREE);
  1933. }
  1934.  
  1935. static tree
  1936. init_module_descriptor (type)
  1937.      tree type;
  1938. {
  1939.   tree initlist, expr;
  1940.  
  1941.   /* version = { 1, ... } */
  1942.  
  1943.   expr = build_int_2 (OBJC_VERSION, 0);
  1944.   initlist = build_tree_list (NULL_TREE, expr);
  1945.  
  1946.   /* size = { ..., sizeof (struct objc_module), ... } */
  1947.  
  1948.   expr = size_in_bytes (objc_module_template);
  1949.   initlist = tree_cons (NULL_TREE, expr, initlist);
  1950.  
  1951.   /* name = { ..., "foo.m", ... } */
  1952.  
  1953.   expr = add_objc_string (get_identifier (input_filename), class_names);
  1954.   initlist = tree_cons (NULL_TREE, expr, initlist);
  1955.  
  1956. #if ! defined(NEXT_SEMANTICS) && ! defined(NEXT_PDO)
  1957.   if (!flag_next_runtime)
  1958.     {
  1959.       /* statics = { ..., _OBJC_STATIC_INSTANCES, ... }  */
  1960.       if (static_instances_decl)
  1961.     expr = build_unary_op (ADDR_EXPR, static_instances_decl, 0);
  1962.       else
  1963.     expr = build_int_2 (0, 0);
  1964.       initlist = tree_cons (NULL_TREE, expr, initlist);
  1965.     }
  1966. #endif /*  ! NEXT_SEMANTICS  &&  ! NEXT_PDO  */
  1967.  
  1968.   /* symtab = { ..., _OBJC_SYMBOLS, ... } */
  1969.  
  1970.   if (UOBJC_SYMBOLS_decl)
  1971.     expr = build_unary_op (ADDR_EXPR, UOBJC_SYMBOLS_decl, 0);
  1972.   else
  1973.     expr = build_int_2 (0, 0);
  1974.   initlist = tree_cons (NULL_TREE, expr, initlist);
  1975.  
  1976.   return build_constructor (type, nreverse (initlist));
  1977. }
  1978.  
  1979. /* Write out the data structures to describe Objective C classes defined.
  1980.    If appropriate, compile and output a setup function to initialize them.
  1981.    Return a string which is the name of a function to call to initialize
  1982.    the Objective C data structures for this file (and perhaps for other files
  1983.    also).
  1984.  
  1985.    struct objc_module { ... } _OBJC_MODULE = { ... };
  1986.  
  1987. */
  1988.  
  1989. static char *
  1990. build_module_descriptor ()
  1991. {
  1992.   tree decl_specs, field_decl, field_decl_chain;
  1993.  
  1994.   objc_module_template
  1995.     = start_struct (RECORD_TYPE, get_identifier (UTAG_MODULE));
  1996.  
  1997.   /* long version; */
  1998.  
  1999.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_LONG]);
  2000.   field_decl = get_identifier ("version");
  2001.   field_decl
  2002.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  2003.   field_decl_chain = field_decl;
  2004.  
  2005.   /* long  size; */
  2006.  
  2007.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_LONG]);
  2008.   field_decl = get_identifier ("size");
  2009.   field_decl
  2010.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  2011.   chainon (field_decl_chain, field_decl);
  2012.  
  2013.   /* char  *name; */
  2014.  
  2015.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_CHAR]);
  2016.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("name"));
  2017.   field_decl
  2018.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  2019.   chainon (field_decl_chain, field_decl);
  2020.  
  2021.   /* struct objc_symtab *symtab; */
  2022.  
  2023.   decl_specs = get_identifier (UTAG_SYMTAB);
  2024.   decl_specs = build_tree_list (NULL_TREE, xref_tag (RECORD_TYPE, decl_specs));
  2025.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("symtab"));
  2026.   field_decl
  2027.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  2028.   chainon (field_decl_chain, field_decl);
  2029.  
  2030.   finish_struct (objc_module_template, field_decl_chain);
  2031.  
  2032.   /* create an instance of "objc_module" */
  2033.  
  2034.   decl_specs = tree_cons (NULL_TREE, objc_module_template,
  2035.               build_tree_list (NULL_TREE,
  2036.                        ridpointers[(int) RID_STATIC]));
  2037.  
  2038.   UOBJC_MODULES_decl = start_decl (get_identifier ("_OBJC_MODULES"),
  2039.                    decl_specs, 1);
  2040.  
  2041.   end_temporary_allocation ();    /* start_decl trying to be smart about inits */
  2042.   DECL_IGNORED_P (UOBJC_MODULES_decl) = 1;
  2043.   finish_decl (UOBJC_MODULES_decl,
  2044.            init_module_descriptor (TREE_TYPE (UOBJC_MODULES_decl)),
  2045.            NULL_TREE);
  2046.  
  2047.   /* Mark the decl to avoid "defined but not used" warning. */
  2048.   DECL_IN_SYSTEM_HEADER (UOBJC_MODULES_decl) = 1;
  2049.  
  2050.   /* Generate a constructor call for the module descriptor.
  2051.      This code was generated by reading the grammar rules
  2052.      of c-parse.in;  Therefore, it may not be the most efficient
  2053.      way of generating the requisite code. */
  2054.  
  2055. #ifndef NEXT_PDO
  2056.   if (flag_next_runtime)
  2057.     return 0;
  2058. #endif
  2059.   {
  2060.     tree parms, function_decl, decelerator, function_type;
  2061. #ifndef OBJCPLUS
  2062.     tree void_list_node = build_tree_list (NULL_TREE, void_type_node);
  2063. #endif
  2064.     extern tree get_file_function_name ();
  2065.     tree init_function_name
  2066. #if defined (_WIN32) && defined (NEXT_PDO)
  2067.       = get_identifier ("_OBJC_MODULE_CONSTRUCTOR");
  2068. #else
  2069.       = get_file_function_name ('I');
  2070. #endif
  2071.  
  2072.     /* Declare void __objc_execClass (void*); */
  2073.  
  2074.     function_type
  2075.       = build_function_type (void_type_node,
  2076.                  tree_cons (NULL_TREE, ptr_type_node,
  2077.                     void_list_node));
  2078. #ifdef OBJCPLUS
  2079.     function_decl = build_lang_decl (FUNCTION_DECL,
  2080.                 get_identifier (TAG_EXECCLASS),
  2081.                 function_type);
  2082. #else
  2083.     function_decl = build_decl (FUNCTION_DECL,
  2084.                 get_identifier (TAG_EXECCLASS),
  2085.                 function_type);
  2086. #endif
  2087.     DECL_EXTERNAL (function_decl) = 1;
  2088.     DECL_ARTIFICIAL (function_decl) = 1;
  2089.     TREE_PUBLIC (function_decl) = 1;
  2090.     pushdecl (function_decl);
  2091.     rest_of_decl_compilation (function_decl, 0, 0, 0);
  2092.  
  2093.     parms
  2094.       = build_tree_list (NULL_TREE,
  2095.              build_unary_op (ADDR_EXPR, UOBJC_MODULES_decl, 0));
  2096.     decelerator = build_function_call (function_decl, parms);
  2097.  
  2098.     /* void _GLOBAL_$I$<gnyf> () {objc_execClass (&L_OBJC_MODULES);}  */
  2099.  
  2100. #ifdef OBJCPLUS
  2101.     start_function (void_list_node,
  2102.             build_parse_node (CALL_EXPR, init_function_name,
  2103.                       /* This has the format of the output
  2104.                      of get_parm_info.  */
  2105.                       void_list_node,
  2106.                       NULL_TREE),
  2107.             0);
  2108. #else
  2109.     start_function (void_list_node,
  2110.             build_parse_node (CALL_EXPR, init_function_name,
  2111.                       /* This has the format of the output
  2112.                      of get_parm_info.  */
  2113.                       tree_cons (NULL_TREE, NULL_TREE,
  2114.                          void_list_node),
  2115.                       NULL_TREE),
  2116.             0);
  2117. #endif
  2118.  
  2119. #if defined (_WIN32) && defined (NEXT_PDO)
  2120.     /* This should be turned on
  2121.        for the systems where collect is not needed.  */
  2122.     /* Make these functions nonglobal
  2123.        so each file can use the same name.  */
  2124.     TREE_PUBLIC (current_function_decl) = 0;
  2125. #endif
  2126. #ifndef OBJCPLUS
  2127.     TREE_USED (current_function_decl) = 1;
  2128. #endif
  2129.     store_parm_decls ();
  2130.     assemble_external (function_decl);
  2131. #ifndef OBJCPLUS
  2132.     c_expand_expr_stmt (decelerator);
  2133. #else
  2134. #if 1
  2135.     pushlevel (0);
  2136.     clear_last_expr ();
  2137.     push_momentary ();
  2138.     expand_start_bindings (0);
  2139.     c_expand_expr_stmt (decelerator);
  2140.     expand_end_bindings (getdecls(), 1, 0);
  2141.     poplevel (1, 0, 0);
  2142.     pop_momentary ();
  2143. #endif
  2144. #endif
  2145.  
  2146.     finish_function (0);
  2147.  
  2148. #ifdef OBJCPLUS
  2149.     pop_lang_context ();
  2150. #endif
  2151.  
  2152.     /* Return the name of the constructor function.  */
  2153.     return IDENTIFIER_POINTER (init_function_name);
  2154.   }
  2155. }
  2156.  
  2157. /* extern const char _OBJC_STRINGS[]; */
  2158.  
  2159. static void
  2160. generate_forward_declaration_to_string_table ()
  2161. {
  2162.   tree sc_spec, decl_specs, expr_decl;
  2163.  
  2164.   sc_spec = tree_cons (NULL_TREE, ridpointers[(int) RID_EXTERN], NULL_TREE);
  2165.   decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_CHAR], sc_spec);
  2166.  
  2167.   expr_decl
  2168.     = build_nt (ARRAY_REF, get_identifier ("_OBJC_STRINGS"), NULL_TREE);
  2169.  
  2170.   UOBJC_STRINGS_decl = define_decl (expr_decl, decl_specs);
  2171. }
  2172.  
  2173. /* Output all strings. */
  2174.  
  2175. static void
  2176. generate_strings ()
  2177. {
  2178.   tree sc_spec, decl_specs, expr_decl;
  2179.   tree chain, string_expr;
  2180.   tree string, decl;
  2181.  
  2182.   for (chain = class_names_chain; chain; chain = TREE_CHAIN (chain))
  2183.     {
  2184.       string = TREE_VALUE (chain);
  2185.       decl = TREE_PURPOSE (chain);
  2186.       sc_spec
  2187.     = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC], NULL_TREE);
  2188.       decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_CHAR], sc_spec);
  2189.       expr_decl = build_nt (ARRAY_REF, DECL_NAME (decl), NULL_TREE);
  2190.       decl = start_decl (expr_decl, decl_specs, 1);
  2191.       end_temporary_allocation ();
  2192.       string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
  2193.                      IDENTIFIER_POINTER (string));
  2194.       finish_decl (decl, string_expr, NULL_TREE);
  2195.     }
  2196.  
  2197.   for (chain = meth_var_names_chain; chain; chain = TREE_CHAIN (chain))
  2198.     {
  2199.       string = TREE_VALUE (chain);
  2200.       decl = TREE_PURPOSE (chain);
  2201.       sc_spec
  2202.     = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC], NULL_TREE);
  2203.       decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_CHAR], sc_spec);
  2204.       expr_decl = build_nt (ARRAY_REF, DECL_NAME (decl), NULL_TREE);
  2205.       decl = start_decl (expr_decl, decl_specs, 1);
  2206.       end_temporary_allocation ();
  2207.       string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
  2208.                      IDENTIFIER_POINTER (string));
  2209.       finish_decl (decl, string_expr, NULL_TREE);
  2210.     }
  2211.  
  2212.   for (chain = meth_var_types_chain; chain; chain = TREE_CHAIN (chain))
  2213.     {
  2214.       string = TREE_VALUE (chain);
  2215.       decl = TREE_PURPOSE (chain);
  2216.       sc_spec
  2217.     = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC], NULL_TREE);
  2218.       decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_CHAR], sc_spec);
  2219.       expr_decl = build_nt (ARRAY_REF, DECL_NAME (decl), NULL_TREE);
  2220.       decl = start_decl (expr_decl, decl_specs, 1);
  2221.       end_temporary_allocation ();
  2222.       string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
  2223.                 IDENTIFIER_POINTER (string));
  2224.       finish_decl (decl, string_expr, NULL_TREE);
  2225.     }
  2226. }
  2227.  
  2228. static tree
  2229. build_selector_reference_decl (name)
  2230.       tree name;
  2231. {
  2232.   tree decl, ident;
  2233.   char buf[256];
  2234.   struct obstack *save_current_obstack = current_obstack;
  2235.   struct obstack *save_rtl_obstack = rtl_obstack;
  2236.   static int idx = 0;
  2237.  
  2238.   sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", idx++);
  2239.  
  2240.   /* new stuff */
  2241.   rtl_obstack = current_obstack = &permanent_obstack;
  2242.   ident = get_identifier (buf);
  2243. #ifdef MACHO_PIC
  2244.   machopic_define_ident (ident);
  2245. #endif
  2246.  
  2247.   decl = build_decl (VAR_DECL, ident, selector_type);
  2248.   DECL_EXTERNAL (decl) = 1;
  2249.   TREE_PUBLIC (decl) = 1;
  2250.   TREE_USED (decl) = 1;
  2251.   TREE_READONLY (decl) = 1;
  2252.   DECL_ARTIFICIAL (decl) = 1;
  2253.   DECL_CONTEXT (decl) = 0;
  2254.  
  2255.   make_decl_rtl (decl, 0, 1); /* usually called from `rest_of_decl_compilation' */
  2256.   pushdecl_top_level (decl);  /* our `extended/custom' pushdecl in c-decl.c */
  2257.  
  2258.   current_obstack = save_current_obstack;
  2259.   rtl_obstack = save_rtl_obstack;
  2260.  
  2261.   return decl;
  2262. }
  2263.  
  2264. /* Just a handy wrapper for add_objc_string.  */
  2265.  
  2266. static tree
  2267. build_selector (ident)
  2268.      tree ident;
  2269. {
  2270.   tree expr = add_objc_string (ident, meth_var_names);
  2271.  
  2272.   return build_c_cast (selector_type, expr); /* cast! */
  2273. }
  2274.  
  2275. /* Synthesize the following expr: (char *)&_OBJC_STRINGS[<offset>]
  2276.    The cast stops the compiler from issuing the following message:
  2277.    grok.m: warning: initialization of non-const * pointer from const *
  2278.    grok.m: warning: initialization between incompatible pointer types.  */
  2279.  
  2280. static tree
  2281. build_msg_pool_reference (offset)
  2282.      int offset;
  2283. {
  2284.   tree expr = build_int_2 (offset, 0);
  2285.   tree cast;
  2286.  
  2287.   expr = build_array_ref (UOBJC_STRINGS_decl, expr);
  2288.   expr = build_unary_op (ADDR_EXPR, expr, 0);
  2289.  
  2290.   cast = build_tree_list (build_tree_list (NULL_TREE,
  2291.                        ridpointers[(int) RID_CHAR]),
  2292.               build1 (INDIRECT_REF, NULL_TREE, NULL_TREE));
  2293.   TREE_TYPE (expr) = groktypename (cast);
  2294.   return expr;
  2295. }
  2296.  
  2297. static tree
  2298. init_selector (offset)
  2299.      int offset;
  2300. {
  2301.   tree expr = build_msg_pool_reference (offset);
  2302.   TREE_TYPE (expr) = selector_type; /* cast */
  2303.   return expr;
  2304. }
  2305.  
  2306. static void
  2307. build_selector_translation_table ()
  2308. {
  2309.   tree sc_spec, decl_specs;
  2310.   tree chain, initlist = NULL_TREE;
  2311.   int offset = 0;
  2312.   tree decl, var_decl, name;
  2313.  
  2314.   /* The corresponding pop_obstacks is in finish_decl,
  2315.      called at the end of this function.  */
  2316.   if (flag_selector_table)
  2317.     push_obstacks_nochange ();
  2318.  
  2319.   for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
  2320.     {
  2321.       tree expr;
  2322.  
  2323.       expr = build_selector (TREE_VALUE (chain));
  2324.  
  2325.       if (! flag_selector_table)
  2326.     {
  2327.       name = DECL_NAME (TREE_PURPOSE (chain));
  2328.  
  2329.       sc_spec = build_tree_list (NULL_TREE, ridpointers[(int) RID_STATIC]);
  2330.  
  2331.       /* static SEL _OBJC_SELECTOR_REFERENCES_n = ...; */
  2332.       decl_specs = tree_cons (NULL_TREE, selector_type, sc_spec);
  2333.  
  2334.       var_decl = name;
  2335.  
  2336.       /* The `decl' that is returned from start_decl is the one that we
  2337.          forward declared in `build_selector_reference'  */
  2338.       decl = start_decl (var_decl, decl_specs, 1);
  2339.     }
  2340.  
  2341.       /* add one for the '\0' character */
  2342.       offset += IDENTIFIER_LENGTH (TREE_VALUE (chain)) + 1;
  2343.  
  2344.       if (! flag_selector_table)
  2345.     {
  2346.       end_temporary_allocation ();
  2347.       finish_decl (decl, expr, NULL_TREE);
  2348.     }
  2349.       else
  2350.     initlist = tree_cons (NULL_TREE, expr, initlist);
  2351.     }
  2352.  
  2353.   if (flag_selector_table)
  2354.     {
  2355.       /* Cause the variable and its initial value to be actually output.  */
  2356.       DECL_EXTERNAL (UOBJC_SELECTOR_TABLE_decl) = 0;
  2357.       TREE_STATIC (UOBJC_SELECTOR_TABLE_decl) = 1;
  2358.       /* NULL terminate the list and fix the decl for output. */
  2359.       initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  2360.       DECL_INITIAL (UOBJC_SELECTOR_TABLE_decl) = (tree) 1;
  2361. #ifdef OBJCPLUS
  2362.       TREE_TYPE (UOBJC_SELECTOR_TABLE_decl) = build_array_type (selector_type, NULL_TREE);
  2363. #endif
  2364.       initlist = build_constructor (TREE_TYPE (UOBJC_SELECTOR_TABLE_decl),
  2365.                     nreverse (initlist));
  2366.       finish_decl (UOBJC_SELECTOR_TABLE_decl, initlist, NULL_TREE);
  2367.     }
  2368. }
  2369.  
  2370. static tree
  2371. build_selector_reference (ident)
  2372.      tree ident;
  2373. {
  2374.   tree *chain = &sel_ref_chain;
  2375.   tree expr;
  2376.   int index = 0;
  2377.  
  2378. #ifdef NEXT_PDO
  2379.   sel_count++;
  2380. #endif
  2381.  
  2382.   while (*chain)
  2383.     {
  2384.       if (TREE_VALUE (*chain) == ident)
  2385.     return ((flag_selector_table == 0)
  2386.         ? TREE_PURPOSE (*chain)
  2387.         : build_array_ref (UOBJC_SELECTOR_TABLE_decl,
  2388.                    build_int_2 (index, 0)));
  2389.  
  2390.       index++;
  2391.       chain = &TREE_CHAIN (*chain);
  2392.     }
  2393.  
  2394.   if (flag_selector_table)
  2395.     expr = build_array_ref (UOBJC_SELECTOR_TABLE_decl, 
  2396.                 build_int_2 (index, 0));
  2397.   else
  2398.     expr = build_selector_reference_decl (ident);
  2399.     
  2400.   *chain = perm_tree_cons (expr, ident, NULL_TREE);
  2401.   return expr; 
  2402. }
  2403.  
  2404. #define OBJECT_CLASS_POINTER(CONSTR) TREE_VALUE(CONSTRUCTOR_ELTS (object))
  2405.  
  2406. #if defined (NEXT_SEMANTICS) || defined (NEXT_PDO)
  2407.  /* Add a static object to the core object file.  The OBJECT argument 
  2408.     is a CONSTRUCTOR node for the object, and the IDENT argument is
  2409.     the name of the class.
  2410.  
  2411.     The effect of this is that the "isa" field of the OBJECT is replaced
  2412.     by a pointer to the class name, and the object itself is enlisted
  2413.     and added to the objc_symtab. */
  2414.  
  2415. static void
  2416. add_static_object (object, ident)
  2417.       tree object;
  2418.       tree ident;
  2419. {
  2420.    tree chain;
  2421.    tree addr;
  2422.  
  2423.    if (!flag_static_objects)
  2424.      return;
  2425.  
  2426.  
  2427.    /* make sure the OBJECT passed is recorded properly */
  2428.    preserve_data ();
  2429. #if 0   
  2430.    if (TREE_CODE (object) == CONSTRUCTOR)
  2431.      chain = CONSTRUCTOR_ELTS (object);
  2432.    else
  2433.      abort ();
  2434. #endif 
  2435.  
  2436.    if (TREE_CODE (ident) != IDENTIFIER_NODE)
  2437.      abort ();
  2438.    /* make sure we can replace the isa field with something 
  2439.       appropriate at runtime.  Otherwise we will get a segmentation
  2440.       violation when the run time system initializes it. */
  2441.    TREE_READONLY (object) = 0;
  2442.    if (TREE_TYPE(object))
  2443.      TYPE_READONLY (TREE_TYPE (object)) = 0;
  2444.  
  2445. #ifdef XXX
  2446.    TREE_VALUE (chain) = add_objc_string (ident, class_names);
  2447. #endif
  2448.  
  2449.    if (mark_addressable (object))
  2450.      {
  2451.        struct obstack *ambient_obstack = current_obstack;
  2452.        struct obstack *save_expr_stack = expression_obstack;
  2453.  
  2454.        current_obstack = &permanent_obstack;
  2455.        expression_obstack = &permanent_obstack;
  2456.  
  2457.        if (ident == protocol_id)
  2458.      {
  2459.        proto_def_chain
  2460.          = tree_cons (NULL_TREE,
  2461.               copy_node (build_unary_op (ADDR_EXPR, object, 1)),
  2462.               proto_def_chain);
  2463.        proto_count += 1;
  2464.      }
  2465.      else
  2466.        {
  2467.          obj_def_chain
  2468.            = tree_cons (NULL_TREE,
  2469.                 copy_node (build_unary_op (ADDR_EXPR, object, 1)),
  2470.                 obj_def_chain);
  2471.          obj_count += 1;
  2472.        }
  2473.        add_class_reference (ident);
  2474.  
  2475.        current_obstack = ambient_obstack;
  2476.        expression_obstack = save_expr_stack;
  2477.      }
  2478.    else
  2479.      abort ();
  2480.  
  2481. }
  2482. #endif  /* NEXT_SEMANTICS || NEXT_PDO */
  2483.  
  2484. static tree
  2485. build_class_reference_decl (name)
  2486.       tree name;
  2487. {
  2488.   tree decl, ident;
  2489.   char buf[256];
  2490.   struct obstack *save_current_obstack = current_obstack;
  2491.   struct obstack *save_rtl_obstack = rtl_obstack;
  2492.   static int idx = 0;
  2493.  
  2494.   sprintf (buf, "_OBJC_CLASS_REFERENCES_%d", idx++);
  2495.  
  2496.   /* new stuff */
  2497.   rtl_obstack = current_obstack = &permanent_obstack;
  2498.   ident = get_identifier (buf);
  2499. #ifdef MACHO_PIC
  2500.   machopic_define_ident (ident);
  2501. #endif
  2502.  
  2503.   decl = build_decl (VAR_DECL, ident, objc_class_type);
  2504.   DECL_EXTERNAL (decl) = 1;
  2505.   TREE_PUBLIC (decl) = 1;
  2506.   TREE_USED (decl) = 1;
  2507.   TREE_READONLY (decl) = 1;
  2508.   DECL_CONTEXT (decl) = 0;
  2509.   DECL_ARTIFICIAL (decl) = 1;
  2510.  
  2511.   make_decl_rtl (decl, 0, 1); /* usually called from `rest_of_decl_compilation' */
  2512.   pushdecl_top_level (decl);  /* our `extended/custom' pushdecl in c-decl.c */
  2513.  
  2514.   current_obstack = save_current_obstack;
  2515.   rtl_obstack = save_rtl_obstack;
  2516.  
  2517.   return decl;
  2518. }
  2519.  
  2520. /* Create a class reference, but don't create a variable to reference
  2521.    it.  */
  2522.  
  2523. static void
  2524. add_class_reference (ident)
  2525.      tree ident;
  2526. {
  2527.   tree chain;
  2528.  
  2529.   if ((chain = cls_ref_chain))
  2530.     {
  2531.       tree tail;
  2532.       do
  2533.         {
  2534.       if (ident == TREE_VALUE (chain))
  2535.         return;
  2536.  
  2537.       tail = chain;
  2538.       chain = TREE_CHAIN (chain);
  2539.         }
  2540.       while (chain);
  2541.  
  2542.       /* Append to the end of the list */
  2543.       TREE_CHAIN (tail) = perm_tree_cons (NULL_TREE, ident, NULL_TREE);
  2544.     }
  2545.   else
  2546.     cls_ref_chain = perm_tree_cons (NULL_TREE, ident, NULL_TREE);
  2547. }
  2548.  
  2549. /* Get a class reference, creating it if necessary.  Also create the
  2550.    reference variable.  */
  2551.  
  2552. tree
  2553. get_class_reference (ident)
  2554.     tree ident;
  2555. {
  2556.   if (flag_class_references)
  2557.     {
  2558.       tree *chain;
  2559.       tree decl;
  2560.  
  2561.       for (chain = &cls_ref_chain; *chain; chain = &TREE_CHAIN (*chain))
  2562.     if (TREE_VALUE (*chain) == ident)
  2563.       {
  2564.         if (! TREE_PURPOSE (*chain))
  2565.           TREE_PURPOSE (*chain) = build_class_reference_decl (ident);
  2566.         return TREE_PURPOSE (*chain);
  2567.       }
  2568.  
  2569.       decl = build_class_reference_decl (ident);
  2570.       *chain = perm_tree_cons (decl, ident, NULL_TREE);
  2571.       return decl;
  2572.     }
  2573.   else
  2574.     {
  2575.       tree params;
  2576.  
  2577.       add_class_reference (ident);
  2578.  
  2579.       params = build_tree_list (NULL_TREE,
  2580.                 my_build_string (IDENTIFIER_LENGTH (ident) + 1,
  2581.                          IDENTIFIER_POINTER (ident)));
  2582.  
  2583.       assemble_external (objc_get_class_decl);
  2584.       return build_function_call (objc_get_class_decl, params);
  2585.     }
  2586. }
  2587.  
  2588. tree
  2589. get_orig_class_reference (ident)
  2590.     tree ident;
  2591. {
  2592.     tree params;
  2593.  
  2594.     add_class_reference (ident);
  2595.  
  2596.     params = build_tree_list (NULL_TREE,
  2597.                               my_build_string (IDENTIFIER_LENGTH (ident) + 1,
  2598.                                                IDENTIFIER_POINTER (ident)));
  2599.  
  2600.     assemble_external (objc_get_orig_class_decl);
  2601.     return build_function_call (objc_get_orig_class_decl, params);
  2602. }
  2603.  
  2604.  
  2605. /* sel_refdef_chain is a list whose "value" fields will be instances
  2606.    of identifier_node that represent the selector. It returns the
  2607.    offset of the selector from the beginning of the _OBJC_STRINGS
  2608.    pool. This offset is typically used by init_selector during code
  2609.    generation.
  2610.  
  2611.    For each string section we have a chain which maps identifier nodes
  2612.    to decls for the strings. */
  2613.  
  2614. static tree
  2615. add_objc_string (ident, section)
  2616.      tree ident;
  2617.      enum string_section section;
  2618. {
  2619.   tree *chain, decl;
  2620.  
  2621.   if (section == class_names)
  2622.     chain = &class_names_chain;
  2623.   else if (section == meth_var_names)
  2624.     chain = &meth_var_names_chain;
  2625.   else if (section == meth_var_types)
  2626.     chain = &meth_var_types_chain;
  2627.  
  2628.   while (*chain)
  2629.     {
  2630.       if (TREE_VALUE (*chain) == ident)
  2631.     return build_unary_op (ADDR_EXPR, TREE_PURPOSE (*chain), 1);
  2632.  
  2633.       chain = &TREE_CHAIN (*chain);
  2634.     }
  2635.  
  2636.   decl = build_objc_string_decl (ident, section);
  2637.  
  2638.   *chain = perm_tree_cons (decl, ident, NULL_TREE);
  2639.  
  2640.   return build_unary_op (ADDR_EXPR, decl, 1);
  2641. }
  2642.  
  2643. static tree
  2644. build_objc_string_decl (name, section)
  2645.      tree name;
  2646.      enum string_section section;
  2647. {
  2648.   tree decl, ident;
  2649.   char buf[256];
  2650.   struct obstack *save_current_obstack = current_obstack;
  2651.   struct obstack *save_rtl_obstack = rtl_obstack;
  2652.   static int class_names_idx = 0;
  2653.   static int meth_var_names_idx = 0;
  2654.   static int meth_var_types_idx = 0;
  2655.  
  2656.   if (section == class_names)
  2657.     sprintf (buf, "_OBJC_CLASS_NAME_%d", class_names_idx++);
  2658.   else if (section == meth_var_names)
  2659.     sprintf (buf, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
  2660.   else if (section == meth_var_types)
  2661.     sprintf (buf, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
  2662.  
  2663.   rtl_obstack = current_obstack = &permanent_obstack;
  2664.   ident = get_identifier (buf);
  2665.  
  2666.   decl = build_decl (VAR_DECL, ident, build_array_type (char_type_node, 0));
  2667.   DECL_EXTERNAL (decl) = 1;
  2668.   TREE_PUBLIC (decl) = 1;
  2669.   TREE_USED (decl) = 1;
  2670.   TREE_READONLY (decl) = 1;
  2671.   TREE_CONSTANT (decl) = 1;
  2672.  
  2673.   make_decl_rtl (decl, 0, 1); /* usually called from `rest_of_decl_compilation */
  2674.   pushdecl_top_level (decl);  /* our `extended/custom' pushdecl in c-decl.c */
  2675.  
  2676.   current_obstack = save_current_obstack;
  2677.   rtl_obstack = save_rtl_obstack;
  2678.  
  2679.   return decl;
  2680. }
  2681.  
  2682.  
  2683. void
  2684. objc_declare_alias (alias_ident, class_ident)
  2685.      tree alias_ident;
  2686.      tree class_ident;
  2687. {
  2688.   if (!doing_objc_thang)
  2689.     objc_fatal ();
  2690.  
  2691.   if (is_class_name (class_ident) != class_ident)
  2692.     warning ("Cannot find class `%s'", IDENTIFIER_POINTER (class_ident));
  2693.   else if (is_class_name (alias_ident))
  2694.     warning ("Class `%s' already exists", IDENTIFIER_POINTER (alias_ident));
  2695.   else
  2696.     alias_chain = tree_cons (class_ident, alias_ident, alias_chain);
  2697. }
  2698.  
  2699. void
  2700. objc_declare_class (ident_list)
  2701.      tree ident_list;
  2702. {
  2703.   tree list;
  2704.  
  2705.   if (!doing_objc_thang)
  2706.     objc_fatal ();
  2707.  
  2708.   for (list = ident_list; list; list = TREE_CHAIN (list))
  2709.     {
  2710.       tree ident = TREE_VALUE (list);
  2711.       tree decl;
  2712.  
  2713. #ifdef OBJCPLUS
  2714.       if (((decl = lookup_name (ident)) != 0) && !is_class_name(ident))
  2715. #else
  2716.       if ((decl = lookup_name (ident)) != 0)
  2717. #endif
  2718.     {
  2719.       error ("`%s' redeclared as different kind of symbol",
  2720.           IDENTIFIER_POINTER (ident));
  2721.       error_with_decl (decl, "previous declaration of `%s'");
  2722.     }
  2723.  
  2724.       if (! is_class_name (ident))
  2725.         {
  2726.       tree record;
  2727. #ifdef OBJCPLUS
  2728.       push_lang_context (lang_name_c);
  2729. #endif
  2730.       record = xref_tag (RECORD_TYPE, ident);
  2731. #ifdef OBJCPLUS
  2732.       pop_lang_context ();
  2733. #endif
  2734.       TREE_STATIC_TEMPLATE (record) = 1;
  2735.       class_chain = tree_cons (NULL_TREE, ident, class_chain);
  2736.     }
  2737.     }
  2738. }
  2739.  
  2740. tree
  2741. is_class_name (ident)
  2742.      tree ident;
  2743. {
  2744.   tree chain;
  2745.  
  2746.   if (lookup_interface (ident))
  2747.     return ident;
  2748.  
  2749.   for (chain = class_chain; chain; chain = TREE_CHAIN (chain))
  2750.     {
  2751.       if (ident == TREE_VALUE (chain))
  2752.     return ident;
  2753.     }
  2754.  
  2755.   for (chain = alias_chain; chain; chain = TREE_CHAIN (chain))
  2756.     {
  2757.       if (ident == TREE_VALUE (chain))
  2758.     return TREE_PURPOSE (chain);
  2759.     }
  2760.  
  2761.   return 0;
  2762. }
  2763.  
  2764. tree
  2765. lookup_interface (ident)
  2766.      tree ident;
  2767. {
  2768.   tree chain;
  2769.  
  2770.   for (chain = interface_chain; chain; chain = TREE_CHAIN (chain))
  2771.     {
  2772.       if (ident == CLASS_NAME (chain))
  2773.     return chain;
  2774.     }
  2775.   return NULL_TREE;
  2776. }
  2777.  
  2778. static tree
  2779. objc_copy_list (list, head)
  2780.      tree list;
  2781.      tree *head;
  2782. {
  2783.   tree newlist = NULL_TREE, tail = NULL_TREE;
  2784.  
  2785.   while (list)
  2786.     {
  2787.       tail = copy_node (list);
  2788.  
  2789.       /* The following statement fixes a bug when inheriting instance
  2790.      variables that are declared to be bitfields. finish_struct
  2791.      expects to find the width of the bitfield in DECL_INITIAL,
  2792.      which it nulls out after processing the decl of the super
  2793.      class...rather than change the way finish_struct works (which
  2794.      is risky), I create the situation it expects...s.naroff
  2795.      (7/23/89).  */
  2796.  
  2797.       if (DECL_BIT_FIELD (tail) && DECL_INITIAL (tail) == 0)
  2798.     DECL_INITIAL (tail) = build_int_2 (DECL_FIELD_SIZE (tail), 0);
  2799.  
  2800.       newlist = chainon (newlist, tail);
  2801.       list = TREE_CHAIN (list);
  2802.     }
  2803.  
  2804.   *head = newlist;
  2805.   return tail;
  2806. }
  2807.  
  2808. /* Used by: build_private_template, get_class_ivars, and
  2809.    continue_class.  COPY is 1 when called from @defs.  In this case
  2810.    copy all fields.  Otherwise don't copy leaf ivars since we rely on
  2811.    them being side-effected exactly once by finish_struct.  */
  2812.  
  2813. static tree
  2814. build_ivar_chain (interface, copy)
  2815.      tree interface;
  2816.      int copy;
  2817. {
  2818.   tree my_name, super_name, ivar_chain;
  2819.  
  2820.   my_name = CLASS_NAME (interface);
  2821.   super_name = CLASS_SUPER_NAME (interface);
  2822.  
  2823.   /* Possibly copy leaf ivars.  */
  2824.   if (copy)
  2825.     objc_copy_list (CLASS_IVARS (interface), &ivar_chain);
  2826.   else
  2827.     ivar_chain = CLASS_IVARS (interface);
  2828.  
  2829.   while (super_name)
  2830.     {
  2831.       tree op1;
  2832.       tree super_interface = lookup_interface (super_name);
  2833.  
  2834.       if (!super_interface)
  2835.         {
  2836.       /* fatal did not work with 2 args...should fix */
  2837.       error ("Cannot find interface declaration for `%s', superclass of `%s'",
  2838.          IDENTIFIER_POINTER (super_name),
  2839.          IDENTIFIER_POINTER (my_name));
  2840.       exit (FATAL_EXIT_CODE);
  2841.         }
  2842.  
  2843.       if (super_interface == interface)
  2844.         {
  2845.           fatal ("Circular inheritance in interface declaration for `%s'",
  2846.                  IDENTIFIER_POINTER (super_name));
  2847.         }
  2848.  
  2849.       interface = super_interface;
  2850.       my_name = CLASS_NAME (interface);
  2851.       super_name = CLASS_SUPER_NAME (interface);
  2852.  
  2853.       op1 = CLASS_IVARS (interface);
  2854.       if (op1)
  2855.         {
  2856.       tree head, tail = objc_copy_list (op1, &head);
  2857.  
  2858.       /* Prepend super class ivars...make a copy of the list, we
  2859.          do not want to alter the original.  */
  2860.       TREE_CHAIN (tail) = ivar_chain;
  2861.       ivar_chain = head;
  2862.         }
  2863.     }
  2864.   return ivar_chain;
  2865. }
  2866.  
  2867. /* struct <classname> {
  2868.      struct objc_class *isa;
  2869.      ...
  2870.    };  */
  2871.  
  2872. static tree
  2873. build_private_template (class)
  2874.      tree class;
  2875. {
  2876.   tree ivar_context;
  2877.  
  2878.   if (CLASS_STATIC_TEMPLATE (class))
  2879.     {
  2880.       uprivate_record = CLASS_STATIC_TEMPLATE (class);
  2881.       ivar_context = TYPE_FIELDS (CLASS_STATIC_TEMPLATE (class));
  2882.     }
  2883.   else
  2884.     {
  2885.       uprivate_record = start_struct (RECORD_TYPE, CLASS_NAME (class));
  2886.  
  2887.       ivar_context = build_ivar_chain (class, 0);
  2888.  
  2889.       finish_struct (uprivate_record, ivar_context);
  2890.  
  2891.       CLASS_STATIC_TEMPLATE (class) = uprivate_record;
  2892.  
  2893.       /* mark this record as class template - for class type checking */
  2894.       TREE_STATIC_TEMPLATE (uprivate_record) = 1;
  2895.     }
  2896.  
  2897.   instance_type
  2898.     = groktypename (build_tree_list (build_tree_list (NULL_TREE,
  2899.                               uprivate_record),
  2900.                      build1 (INDIRECT_REF, NULL_TREE,
  2901.                          NULL_TREE)));
  2902.  
  2903.   return ivar_context;
  2904. }
  2905.  
  2906. /* Begin code generation for protocols... */
  2907.  
  2908. /* struct objc_protocol {
  2909.      struct objc_class *isa;
  2910.      char *protocol_name;
  2911.      struct objc_protocol **protocol_list;
  2912.      struct objc_method_desc *instance_methods;
  2913.      struct objc_method_desc *class_methods;
  2914.    };  */
  2915.  
  2916. static tree
  2917. build_protocol_template ()
  2918. {
  2919.   tree decl_specs, field_decl, field_decl_chain;
  2920.   tree template;
  2921.  
  2922.   template = start_struct (RECORD_TYPE, get_identifier (UTAG_PROTOCOL));
  2923.  
  2924.   /* struct objc_class *isa; */
  2925.  
  2926.   decl_specs = build_tree_list (NULL_TREE, xref_tag (RECORD_TYPE,
  2927.                     get_identifier (UTAG_CLASS)));
  2928.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("isa"));
  2929.   field_decl
  2930.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  2931.   field_decl_chain = field_decl;
  2932.  
  2933.   /* char *protocol_name; */
  2934.  
  2935.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_CHAR]);
  2936.   field_decl
  2937.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("protocol_name"));
  2938.   field_decl
  2939.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  2940.   chainon (field_decl_chain, field_decl);
  2941.  
  2942.   /* struct objc_protocol **protocol_list; */
  2943.  
  2944.   decl_specs = build_tree_list (NULL_TREE, template);
  2945.   field_decl
  2946.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("protocol_list"));
  2947.   field_decl = build1 (INDIRECT_REF, NULL_TREE, field_decl);
  2948.   field_decl
  2949.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  2950.   chainon (field_decl_chain, field_decl);
  2951.  
  2952.   /* struct objc_method_list *instance_methods; */
  2953.  
  2954.   decl_specs
  2955.     = build_tree_list (NULL_TREE,
  2956.                xref_tag (RECORD_TYPE,
  2957.                  get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
  2958.   field_decl
  2959.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("instance_methods"));
  2960.   field_decl
  2961.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  2962.   chainon (field_decl_chain, field_decl);
  2963.  
  2964.   /* struct objc_method_list *class_methods; */
  2965.  
  2966.   decl_specs
  2967.     = build_tree_list (NULL_TREE,
  2968.                xref_tag (RECORD_TYPE,
  2969.                  get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
  2970.   field_decl
  2971.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("class_methods"));
  2972.   field_decl
  2973.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  2974.   chainon (field_decl_chain, field_decl);
  2975.  
  2976. #ifdef OBJC_HPUX_PADDING
  2977.   /* unsigned long risc pad -- for hppa processors; */
  2978.  
  2979.   decl_specs = build_tree_list (NULL_TREE, xref_tag (RECORD_TYPE,
  2980.                     get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
  2981.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("risc_pad"));
  2982.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  2983.   chainon (field_decl_chain, field_decl);
  2984. #endif /* OBJC_HPUX_PADDING */
  2985.  
  2986.   return finish_struct (template, field_decl_chain);
  2987. }
  2988.  
  2989. static tree
  2990. build_descriptor_table_initializer (type, entries)
  2991.      tree type;
  2992.      tree entries;
  2993. {
  2994.   tree initlist = NULL_TREE;
  2995.  
  2996.   do
  2997.     {
  2998.       tree eltlist = NULL_TREE;
  2999.  
  3000.       eltlist
  3001.     = tree_cons (NULL_TREE,
  3002.              build_selector (METHOD_SEL_NAME (entries)), NULL_TREE);
  3003.       eltlist
  3004.     = tree_cons (NULL_TREE,
  3005.              add_objc_string (METHOD_ENCODING (entries),
  3006.                       meth_var_types),
  3007.              eltlist);
  3008.  
  3009.       initlist
  3010.     = tree_cons (NULL_TREE,
  3011.              build_constructor (type, nreverse (eltlist)), initlist);
  3012.  
  3013.       entries = TREE_CHAIN (entries);
  3014.     }
  3015.   while (entries);
  3016.  
  3017.   return build_constructor (build_array_type (type, 0), nreverse (initlist));
  3018. }
  3019.  
  3020. /* struct objc_method_prototype_list {
  3021.      int count;
  3022.      struct objc_method_prototype {
  3023.      SEL name;
  3024.      char *types;
  3025.      } list[1];
  3026.    };  */
  3027.  
  3028. static tree
  3029. build_method_prototype_list_template (list_type, size)
  3030.      tree list_type;
  3031.      int size;
  3032. {
  3033.   tree objc_ivar_list_record;
  3034.   tree decl_specs, field_decl, field_decl_chain;
  3035.  
  3036.   /* Generate an unnamed struct definition. */
  3037.  
  3038.   objc_ivar_list_record = start_struct (RECORD_TYPE, NULL_TREE);
  3039.  
  3040.   /* int method_count; */
  3041.  
  3042.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_INT]);
  3043.   field_decl = get_identifier ("method_count");
  3044.  
  3045.   field_decl
  3046.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3047.   field_decl_chain = field_decl;
  3048.  
  3049.   /* struct objc_method method_list[]; */
  3050.  
  3051.   decl_specs = build_tree_list (NULL_TREE, list_type);
  3052.   field_decl = build_nt (ARRAY_REF, get_identifier ("method_list"),
  3053.              build_int_2 (size, 0));
  3054.  
  3055.   field_decl
  3056.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3057.   chainon (field_decl_chain, field_decl);
  3058.  
  3059.   finish_struct (objc_ivar_list_record, field_decl_chain);
  3060.  
  3061.   return objc_ivar_list_record;
  3062. }
  3063.  
  3064. static tree
  3065. build_method_prototype_template ()
  3066. {
  3067.   tree proto_record;
  3068.   tree decl_specs, field_decl, field_decl_chain;
  3069.  
  3070.   proto_record
  3071.     = start_struct (RECORD_TYPE, get_identifier (UTAG_METHOD_PROTOTYPE));
  3072.  
  3073. #ifdef OBJC_INT_SELECTORS
  3074.   /* unsigned int _cmd; */
  3075.   decl_specs
  3076.     = tree_cons (NULL_TREE, ridpointers[(int) RID_UNSIGNED], NULL_TREE);
  3077.   decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_INT], decl_specs);
  3078.   field_decl = get_identifier ("_cmd");
  3079. #else /* OBJC_INT_SELECTORS */
  3080.   /* struct objc_selector *_cmd; */
  3081.   decl_specs = tree_cons (NULL_TREE, xref_tag (RECORD_TYPE,
  3082.                   get_identifier (TAG_SELECTOR)), NULL_TREE);
  3083.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("_cmd"));
  3084. #endif /* OBJC_INT_SELECTORS */
  3085.  
  3086.   field_decl
  3087.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3088.   field_decl_chain = field_decl;
  3089.  
  3090.   decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_CHAR], NULL_TREE);
  3091.   field_decl
  3092.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("method_types"));
  3093.   field_decl
  3094.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3095.   chainon (field_decl_chain, field_decl);
  3096.  
  3097.   finish_struct (proto_record, field_decl_chain);
  3098.  
  3099.   return proto_record;
  3100. }
  3101.  
  3102. /* True if last call to forwarding_offset yielded a register offset. */
  3103. static int offset_is_register;
  3104.  
  3105. static int
  3106. forwarding_offset (parm)
  3107.       tree parm;
  3108. {
  3109.   int offset_in_bytes;
  3110.  
  3111.   if (GET_CODE (DECL_INCOMING_RTL (parm)) == MEM)
  3112.     {
  3113.       rtx addr = XEXP (DECL_INCOMING_RTL (parm), 0);
  3114.  
  3115.       /* ??? Here we assume that the parm address is indexed
  3116.       off the frame pointer or arg pointer.
  3117.       If that is not true, we produce meaningless results,
  3118.       but do not crash.  */
  3119.       if (GET_CODE (addr) == PLUS
  3120.       && GET_CODE (XEXP (addr, 1)) == CONST_INT)
  3121.     offset_in_bytes = INTVAL (XEXP (addr, 1));
  3122.       else
  3123.     offset_in_bytes = 0;
  3124.  
  3125.       if (flag_next_runtime)
  3126.     offset_in_bytes += OBJC_FORWARDING_STACK_OFFSET;
  3127.       offset_is_register = 0;
  3128.     }
  3129.   else if (GET_CODE (DECL_INCOMING_RTL (parm)) == REG)
  3130.     {
  3131.       int regno = REGNO (DECL_INCOMING_RTL (parm));
  3132. #ifdef OBJC_FORWARDING_REG_OFFSET
  3133.       if (flag_next_runtime)
  3134.     {
  3135.       OBJC_FORWARDING_REG_OFFSET (offset_is_register, offset_in_bytes, regno);
  3136.     }
  3137.       else
  3138. #endif
  3139.     {
  3140.       offset_in_bytes = apply_args_register_offset (regno);
  3141.       offset_is_register = 1;
  3142.     }
  3143.     }
  3144.   else
  3145.     return 0;
  3146.  
  3147.   /* This is the case where the parm is passed as an int or double
  3148.       and it is converted to a char, short or float and stored back
  3149.       in the parmlist.  In this case, describe the parm
  3150.       with the variable's declared type, and adjust the address
  3151.       if the least significant bytes (which we are using) are not
  3152.       the first ones.  */
  3153. #if BYTES_BIG_ENDIAN
  3154.   if (TREE_TYPE (parm) != DECL_ARG_TYPE (parm))
  3155.     offset_in_bytes += (GET_MODE_SIZE (TYPE_MODE (DECL_ARG_TYPE (parm)))
  3156.             - GET_MODE_SIZE (GET_MODE (DECL_RTL (parm))));
  3157. #endif
  3158.  
  3159.   return offset_in_bytes;
  3160. }
  3161.  
  3162. static tree
  3163. encode_method_prototype (method_decl, func_decl)
  3164.       tree method_decl;
  3165.       tree func_decl;
  3166. {
  3167.   tree parms;
  3168.   int stack_size, i;
  3169.   tree user_args;
  3170.   int max_parm_end = 0;
  3171.   char buf[40];
  3172.   tree result;
  3173.  
  3174.   /* ONEWAY and BYCOPY, for remote object are the only method qualifiers. */
  3175.   /* BYREF, for Distributed Objects. */
  3176.   encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_decl)));
  3177.  
  3178.   /* C type. */
  3179.   encode_type (TREE_TYPE (TREE_TYPE (func_decl)),
  3180.            obstack_object_size (&util_obstack),
  3181.            OBJC_ENCODE_INLINE_DEFS);
  3182.  
  3183.   /* Stack size. */
  3184.   for (parms = DECL_ARGUMENTS (func_decl); parms;
  3185.        parms = TREE_CHAIN (parms))
  3186.     {
  3187.       int parm_end = forwarding_offset (parms);
  3188.  
  3189.       if (parm_end > 0)
  3190.     parm_end += TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (parms))) / BITS_PER_UNIT;
  3191.       else
  3192.     parm_end = -parm_end;
  3193.  
  3194.       if (max_parm_end < parm_end)
  3195.     max_parm_end = parm_end;
  3196.     }
  3197.  
  3198.   stack_size = max_parm_end - ( flag_next_runtime 
  3199.                    ? OBJC_FORWARDING_MIN_OFFSET 
  3200.                    : 0);
  3201.  
  3202.   sprintf (buf, "%d", stack_size);
  3203.   obstack_grow (&util_obstack, buf, strlen (buf));
  3204.  
  3205.   user_args = METHOD_SEL_ARGS (method_decl);
  3206.  
  3207.   /* Argument types. */
  3208.   for (parms = DECL_ARGUMENTS (func_decl), i = 0; parms;
  3209.        parms = TREE_CHAIN (parms), i++)
  3210.     {
  3211.       /* Process argument qualifiers for user supplied arguments. */
  3212.       if (i > 1)
  3213.         {
  3214.       encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (user_args)));
  3215.       user_args = TREE_CHAIN (user_args);
  3216.      }
  3217.  
  3218.       /* Type. */
  3219.       encode_type (TREE_TYPE (parms),
  3220.            obstack_object_size (&util_obstack),
  3221.            OBJC_ENCODE_INLINE_DEFS);
  3222.  
  3223.       /* Compute offset. */
  3224.       sprintf (buf, "%d", forwarding_offset (parms));
  3225.  
  3226. #ifndef TARGET_SPARC
  3227.       /* Indicate register. */
  3228.       if (offset_is_register && !flag_next_runtime)
  3229.     obstack_1grow (&util_obstack, '+');
  3230. #endif
  3231.  
  3232.       obstack_grow (&util_obstack, buf, strlen (buf));
  3233.     }
  3234.  
  3235.   obstack_1grow (&util_obstack, '\0');
  3236.   result = get_identifier (obstack_finish (&util_obstack));
  3237.   obstack_free (&util_obstack, util_firstobj);
  3238.   return result;
  3239. }
  3240.  
  3241. static tree
  3242. generate_descriptor_table (type, name, size, list, proto)
  3243.      tree type;
  3244.      char *name;
  3245.      int size;
  3246.      tree list;
  3247.      tree proto;
  3248. {
  3249.   tree sc_spec, decl_specs, decl, initlist;
  3250.  
  3251.   sc_spec = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC], NULL_TREE);
  3252.   decl_specs = tree_cons (NULL_TREE, type, sc_spec);
  3253.  
  3254.   decl = start_decl (synth_id_with_class_suffix (name, proto),
  3255.                 decl_specs, 1);
  3256.   end_temporary_allocation ();
  3257.  
  3258.   initlist = build_tree_list (NULL_TREE, build_int_2 (size, 0));
  3259.   initlist = tree_cons (NULL_TREE, list, initlist);
  3260.  
  3261.   finish_decl (decl, build_constructor (type, nreverse (initlist)),
  3262.            NULL_TREE);
  3263.  
  3264.   return decl;
  3265. }
  3266.  
  3267. static void
  3268. generate_method_descriptors (protocol)    /* generate_dispatch_tables */
  3269.   tree protocol;
  3270. {
  3271.   static tree objc_method_prototype_template;
  3272.   tree initlist, chain, method_list_template;
  3273.   tree cast, variable_length_type;
  3274.   int size;
  3275.  
  3276.   if (!objc_method_prototype_template)
  3277.     objc_method_prototype_template = build_method_prototype_template ();
  3278.  
  3279.   cast = build_tree_list (build_tree_list (NULL_TREE, xref_tag (RECORD_TYPE,
  3280.                 get_identifier (UTAG_METHOD_PROTOTYPE_LIST))),
  3281.               NULL_TREE);
  3282.   variable_length_type = groktypename (cast);
  3283.  
  3284.   chain = PROTOCOL_CLS_METHODS (protocol);
  3285.   if (chain)
  3286.     {
  3287.       size = list_length (chain);
  3288.  
  3289.       method_list_template
  3290.     = build_method_prototype_list_template (objc_method_prototype_template,
  3291.                         size);
  3292.  
  3293.       initlist 
  3294.     = build_descriptor_table_initializer (objc_method_prototype_template,
  3295.                           chain);
  3296.  
  3297.       UOBJC_CLASS_METHODS_decl
  3298.     = generate_descriptor_table (method_list_template,
  3299.                      "_OBJC_PROTOCOL_CLASS_METHODS",
  3300.                      size, initlist, protocol);
  3301.       TREE_TYPE (UOBJC_CLASS_METHODS_decl) = variable_length_type;
  3302.     }
  3303.   else
  3304.     UOBJC_CLASS_METHODS_decl = 0;
  3305.  
  3306.   chain = PROTOCOL_NST_METHODS (protocol);
  3307.   if (chain)
  3308.     {
  3309.       size = list_length (chain);
  3310.  
  3311.       method_list_template
  3312.     = build_method_prototype_list_template (objc_method_prototype_template,
  3313.                         size);
  3314.       initlist
  3315.     = build_descriptor_table_initializer (objc_method_prototype_template,
  3316.                           chain);
  3317.  
  3318.       UOBJC_INSTANCE_METHODS_decl
  3319.     = generate_descriptor_table (method_list_template,
  3320.                      "_OBJC_PROTOCOL_INSTANCE_METHODS",
  3321.                      size, initlist, protocol);
  3322.       TREE_TYPE (UOBJC_INSTANCE_METHODS_decl) = variable_length_type;
  3323.     }
  3324.   else
  3325.     UOBJC_INSTANCE_METHODS_decl = 0;
  3326. }
  3327.  
  3328. /* 
  3329.   Generate a temporary FUNCTION_DECL node to be used in hack_method_prototype
  3330.   below. 
  3331.  */
  3332. static tree
  3333. build_tmp_function_decl ()
  3334. {
  3335.   tree decl_specs, expr_decl, parms;
  3336.   static int xxx = 0;
  3337.   char buffer[80];
  3338.  
  3339.   /* struct objc_object *objc_xxx (id, SEL, ...); */
  3340.   pushlevel (0);
  3341.   decl_specs = build_tree_list (NULL_TREE, objc_object_reference);
  3342.   push_parm_decl (build_tree_list
  3343.           (build_tree_list (decl_specs,
  3344.                     build1 (INDIRECT_REF, NULL_TREE,
  3345.                         NULL_TREE)),
  3346.            build_tree_list (NULL_TREE, NULL_TREE)));
  3347.  
  3348.   decl_specs = build_tree_list (NULL_TREE, xref_tag (RECORD_TYPE,
  3349.                       get_identifier (TAG_SELECTOR)));
  3350.   expr_decl = build1 (INDIRECT_REF, NULL_TREE, NULL_TREE);
  3351.  
  3352.   push_parm_decl (build_tree_list (build_tree_list (decl_specs, expr_decl),
  3353.                    build_tree_list (NULL_TREE, NULL_TREE)));
  3354.   parms = get_parm_info (0);
  3355.   poplevel (0, 0, 0);
  3356.  
  3357.   decl_specs = build_tree_list (NULL_TREE, objc_object_reference);
  3358.   sprintf (buffer, "__objc_tmp_%x", xxx++);
  3359.   expr_decl = build_nt (CALL_EXPR, get_identifier (buffer), parms, NULL_TREE);
  3360.   expr_decl = build1 (INDIRECT_REF, NULL_TREE, expr_decl);
  3361.  
  3362.   return define_decl (expr_decl, decl_specs);
  3363. }
  3364.  
  3365.  
  3366. /* 
  3367.    Generate the prototypes for protocol methods.
  3368.    This is used to generate method encodings for these.
  3369.  
  3370.    NST_METHODS is the method to generate a _DECL node for
  3371.    TMP_DECL is a decl node to be used.  This is also
  3372.      where the return value is given.
  3373.  */
  3374. static void
  3375. hack_method_prototype (nst_methods, tmp_decl)
  3376.      tree nst_methods;
  3377.      tree tmp_decl;
  3378. {
  3379.   tree parms;
  3380. #ifdef OBJCPLUS
  3381.   extern tree last_function_parms;
  3382. #endif /* OBJCPLUS */
  3383.  
  3384.   /* Hack to avoid problem with static typing of self arg. */
  3385.   TREE_SET_CODE (nst_methods, CLASS_METHOD_DECL);
  3386.   start_method_def (nst_methods);
  3387.   TREE_SET_CODE (nst_methods, INSTANCE_METHOD_DECL);
  3388.  
  3389.   if (METHOD_ADD_ARGS (nst_methods) == (tree) 1)
  3390.     parms = get_parm_info (0); /* we have a `, ...' */
  3391.   else
  3392.     parms = get_parm_info (1); /* place a `void_at_end' */
  3393.  
  3394.   poplevel (0, 0, 0);    /* Must be called BEFORE start_function.  */
  3395.  
  3396.   /* Usually called from store_parm_decls -> init_function_start.  */
  3397.  
  3398.   init_emit ();    /* needed to make assign_parms work (with -O).  */
  3399.   temp_slots = 0; /* prevent compiler from using a freed chunk of memory.  */
  3400.   /* Does anything else need to be initialized?  */
  3401.  
  3402. #ifdef OBJCPLUS
  3403.   /* usually called from start_function()->grokdeclarator(). */ 
  3404.   grokparms (parms, 1);
  3405.   /* usually done by store_parm_decls(). */
  3406.   DECL_ARGUMENTS(tmp_decl) = last_function_parms;
  3407. #else /* OBJCPLUS */
  3408.   DECL_ARGUMENTS(tmp_decl) = TREE_PURPOSE(parms);
  3409. #endif /* OBJCPLUS */
  3410.  
  3411.   {
  3412.     /* Code taken from start_function.  */
  3413.     tree restype = TREE_TYPE (TREE_TYPE (tmp_decl));
  3414.     /* Promote the value to int before returning it.  */
  3415.     if (TREE_CODE (restype) == INTEGER_TYPE
  3416.     && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
  3417.       restype = integer_type_node;
  3418.     DECL_RESULT (tmp_decl) = build_decl (RESULT_DECL, 0, restype);
  3419.   }
  3420.  
  3421.   /* Typically called from expand_function_start for function definitions.  */
  3422.   assign_parms (tmp_decl, 0);
  3423.  
  3424.   /* install return type */
  3425.   TREE_TYPE (TREE_TYPE (tmp_decl)) = groktypename (TREE_TYPE (nst_methods));
  3426.  
  3427. }
  3428.  
  3429. static void
  3430. generate_protocol_references (plist)
  3431.      tree plist;
  3432. {
  3433.   tree lproto;
  3434.  
  3435.   /* Forward declare protocols referenced. */
  3436.   for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
  3437.     {
  3438.       tree proto = TREE_VALUE (lproto);
  3439.  
  3440.       if (TREE_CODE (proto) == PROTOCOL_INTERFACE_TYPE
  3441.       && PROTOCOL_NAME (proto))
  3442.     {
  3443.           if (! PROTOCOL_FORWARD_DECL (proto))
  3444.             build_protocol_reference (proto);
  3445.  
  3446.           if (PROTOCOL_LIST (proto))
  3447.             generate_protocol_references (PROTOCOL_LIST (proto));
  3448.         }
  3449.     }
  3450. }
  3451.  
  3452. static void
  3453. generate_protocols ()
  3454. {
  3455.   tree p, tmp_decl, encoding;
  3456.   tree sc_spec, decl_specs, decl;
  3457.   tree initlist, protocol_name_expr, refs_decl, refs_expr;
  3458.   tree cast_type2 = 0;
  3459.  
  3460.   tmp_decl = build_tmp_function_decl ();
  3461.  
  3462.   if (! objc_protocol_template)
  3463.     objc_protocol_template = build_protocol_template ();
  3464.  
  3465.   /* If a protocol was directly referenced, pull in indirect references.  */
  3466.   for (p = protocol_chain; p; p = TREE_CHAIN (p))
  3467.     if (PROTOCOL_FORWARD_DECL (p) && PROTOCOL_LIST (p))
  3468.       generate_protocol_references (PROTOCOL_LIST (p));
  3469.  
  3470.   for (p = protocol_chain; p; p = TREE_CHAIN (p))
  3471.     {
  3472.       tree nst_methods = PROTOCOL_NST_METHODS (p);
  3473.       tree cls_methods = PROTOCOL_CLS_METHODS (p);
  3474.  
  3475.       /* If protocol wasn't referenced, don't generate any code.  */
  3476.       if (! PROTOCOL_FORWARD_DECL (p))
  3477.     continue;
  3478.  
  3479.       /* Make sure we link in the Protocol class. */
  3480.       add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
  3481.  
  3482.       while (nst_methods)
  3483.     {
  3484.       if (! METHOD_ENCODING (nst_methods))
  3485.         {
  3486.           hack_method_prototype (nst_methods, tmp_decl);
  3487.           encoding = encode_method_prototype (nst_methods, tmp_decl);
  3488.           METHOD_ENCODING (nst_methods) = encoding;
  3489.         }
  3490.       nst_methods = TREE_CHAIN (nst_methods);
  3491.     }
  3492.  
  3493.       while (cls_methods)
  3494.     {
  3495.       if (! METHOD_ENCODING (cls_methods))
  3496.         {
  3497.           hack_method_prototype (cls_methods, tmp_decl);
  3498.           encoding = encode_method_prototype (cls_methods, tmp_decl);
  3499.           METHOD_ENCODING (cls_methods) = encoding;
  3500.         }
  3501.  
  3502.       cls_methods = TREE_CHAIN (cls_methods);
  3503.     }
  3504.       generate_method_descriptors (p);
  3505.  
  3506.       if (PROTOCOL_LIST (p))
  3507.     refs_decl = generate_protocol_list (p);
  3508.       else
  3509.     refs_decl = 0;
  3510.  
  3511.       /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
  3512.  
  3513.       sc_spec = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC],
  3514.                NULL_TREE);
  3515.       decl_specs = tree_cons (NULL_TREE, objc_protocol_template, sc_spec);
  3516.  
  3517.       decl = start_decl (synth_id_with_class_suffix ("_OBJC_PROTOCOL", p),
  3518.              decl_specs, 1);
  3519.       end_temporary_allocation ();
  3520.  
  3521.       protocol_name_expr = add_objc_string (PROTOCOL_NAME (p), class_names);
  3522.  
  3523.       if (refs_decl)
  3524.     {
  3525.       if (!cast_type2)
  3526.         cast_type2
  3527.           = groktypename
  3528.         (build_tree_list (build_tree_list (NULL_TREE,
  3529.                            objc_protocol_template),
  3530.                   build1 (INDIRECT_REF, NULL_TREE,
  3531.                       build1 (INDIRECT_REF, NULL_TREE,
  3532.                           NULL_TREE))));
  3533.  
  3534.       refs_expr = build_unary_op (ADDR_EXPR, refs_decl, 0);
  3535.       TREE_TYPE (refs_expr) = cast_type2;
  3536.     }
  3537.       else
  3538.     refs_expr = build_int_2 (0, 0);
  3539.  
  3540.       /* UOBJC_INSTANCE_METHODS_decl/UOBJC_CLASS_METHODS_decl are set
  3541.      by generate_method_descriptors, which is called above.  */
  3542.       initlist = build_protocol_initializer (TREE_TYPE (decl),
  3543.                          protocol_name_expr, refs_expr,
  3544.                          UOBJC_INSTANCE_METHODS_decl,
  3545.                          UOBJC_CLASS_METHODS_decl);
  3546.       TREE_PUBLIC (decl) = 0;
  3547.       finish_decl (decl, initlist, NULL_TREE);
  3548.  
  3549.       /* Mark the decl as used to avoid "defined but not used" warning. */
  3550.       TREE_USED (decl) = 1;
  3551.     }
  3552. }
  3553.  
  3554. static tree
  3555. build_protocol_initializer (type, protocol_name, protocol_list,
  3556.                 instance_methods, class_methods)
  3557.      tree type;
  3558.      tree protocol_name;
  3559.      tree protocol_list;
  3560.      tree instance_methods;
  3561.      tree class_methods;
  3562. {
  3563.   tree initlist = NULL_TREE, expr;
  3564.   static tree cast_type = 0;
  3565.      
  3566.   struct obstack *save_current_obstack = current_obstack;
  3567.   struct obstack *save_expression_obstack = expression_obstack;
  3568.  
  3569.   if (!cast_type)
  3570.     cast_type
  3571.       = groktypename
  3572.     (build_tree_list
  3573.      (build_tree_list (NULL_TREE,
  3574.                xref_tag (RECORD_TYPE,
  3575.                      get_identifier (UTAG_CLASS))),
  3576.       build1 (INDIRECT_REF, NULL_TREE, NULL_TREE)));
  3577.  
  3578.   /* Filling the "isa" in with one allows the runtime system to
  3579.      detect that the version change...should remove before final release.  */
  3580.  
  3581.   expr = build_int_2 (PROTOCOL_VERSION, 0);
  3582.   TREE_TYPE (expr) = cast_type;
  3583.   initlist = tree_cons (NULL_TREE, expr, initlist);
  3584.   initlist = tree_cons (NULL_TREE, protocol_name, initlist);
  3585.   initlist = tree_cons (NULL_TREE, protocol_list, initlist);
  3586.  
  3587.   if (!instance_methods)
  3588.     initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  3589.   else
  3590.     {
  3591.       expr = build_unary_op (ADDR_EXPR, instance_methods, 0);
  3592.       initlist = tree_cons (NULL_TREE, expr, initlist);
  3593.     }
  3594.  
  3595.   if (!class_methods)
  3596.     initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  3597.   else
  3598.     {
  3599.       expr = build_unary_op (ADDR_EXPR, class_methods, 0);
  3600.       initlist = tree_cons (NULL_TREE, expr, initlist);
  3601.     }
  3602.  
  3603.   return build_constructor (type, nreverse (initlist));
  3604. }
  3605. /* end code generation for protocols... */
  3606.  
  3607. /* struct objc_category {
  3608.      char *category_name;
  3609.      char *class_name;
  3610.      struct objc_method_list *instance_methods;
  3611.      struct objc_method_list *class_methods;
  3612.      struct objc_protocol_list *protocols;
  3613.    };   */
  3614.  
  3615. static void
  3616. build_category_template ()
  3617. {
  3618.   tree decl_specs, field_decl, field_decl_chain;
  3619.  
  3620.   objc_category_template = start_struct (RECORD_TYPE,
  3621.                      get_identifier (UTAG_CATEGORY));
  3622.   /* char *category_name; */
  3623.  
  3624.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_CHAR]);
  3625.   field_decl
  3626.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("category_name"));
  3627.   field_decl
  3628.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3629.   field_decl_chain = field_decl;
  3630.  
  3631.   /* char *class_name; */
  3632.  
  3633.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_CHAR]);
  3634.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("class_name"));
  3635.   field_decl
  3636.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3637.   chainon (field_decl_chain, field_decl);
  3638.  
  3639.   /* struct objc_method_list *instance_methods; */
  3640.  
  3641.   decl_specs = build_tree_list (NULL_TREE,
  3642.                 xref_tag (RECORD_TYPE,
  3643.                       get_identifier (UTAG_METHOD_LIST)));
  3644.   field_decl
  3645.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("instance_methods"));
  3646.   field_decl
  3647.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3648.   chainon (field_decl_chain, field_decl);
  3649.  
  3650.   /* struct objc_method_list *class_methods; */
  3651.  
  3652.   decl_specs = build_tree_list (NULL_TREE,
  3653.                 xref_tag (RECORD_TYPE,
  3654.                       get_identifier (UTAG_METHOD_LIST)));
  3655.   field_decl
  3656.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("class_methods"));
  3657.   field_decl
  3658.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3659.   chainon (field_decl_chain, field_decl);
  3660.  
  3661.   /* struct objc_protocol **protocol_list; */
  3662.  
  3663.   decl_specs = build_tree_list (NULL_TREE,
  3664.                 xref_tag (RECORD_TYPE,
  3665.                       get_identifier (UTAG_PROTOCOL)));
  3666.   field_decl
  3667.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("protocol_list"));
  3668.   field_decl = build1 (INDIRECT_REF, NULL_TREE, field_decl);
  3669.   field_decl
  3670.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3671.   chainon (field_decl_chain, field_decl);
  3672.  
  3673.   finish_struct (objc_category_template, field_decl_chain);
  3674. }
  3675.  
  3676. /* struct objc_class {
  3677.      struct objc_class *isa;
  3678.      struct objc_class *super_class;
  3679.      char *name;
  3680.      long version;
  3681.      long info;
  3682.      long instance_size;
  3683.      struct objc_ivar_list *ivars;
  3684.      struct objc_method_list *methods;
  3685.      if (flag_next_runtime)
  3686.        struct objc_cache *cache;
  3687.      else {
  3688.        struct sarray *dtable;
  3689.        struct objc_class *subclass_list;
  3690.        struct objc_class *sibling_class;
  3691.      }
  3692.      struct objc_protocol_list *protocols;
  3693.    };  */
  3694.  
  3695. static void
  3696. build_class_template ()
  3697. {
  3698.   tree decl_specs, field_decl, field_decl_chain;
  3699.  
  3700.   objc_class_template
  3701.     = start_struct (RECORD_TYPE, get_identifier (UTAG_CLASS));
  3702.  
  3703.   /* struct objc_class *isa; */
  3704.  
  3705.   decl_specs = build_tree_list (NULL_TREE, objc_class_template);
  3706.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("isa"));
  3707.   field_decl
  3708.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3709.   field_decl_chain = field_decl;
  3710.  
  3711.   /* struct objc_class *super_class; */
  3712.  
  3713.   decl_specs = build_tree_list (NULL_TREE, objc_class_template);
  3714.   field_decl
  3715.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("super_class"));
  3716.   field_decl
  3717.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3718.   chainon (field_decl_chain, field_decl);
  3719.  
  3720.   /* char *name; */
  3721.  
  3722.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_CHAR]);
  3723.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("name"));
  3724.   field_decl
  3725.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3726.   chainon (field_decl_chain, field_decl);
  3727.  
  3728.   /* long version; */
  3729.  
  3730.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_LONG]);
  3731.   field_decl = get_identifier ("version");
  3732.   field_decl
  3733.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3734.   chainon (field_decl_chain, field_decl);
  3735.  
  3736.   /* long info; */
  3737.  
  3738.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_LONG]);
  3739.   field_decl = get_identifier ("info");
  3740.   field_decl
  3741.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3742.   chainon (field_decl_chain, field_decl);
  3743.  
  3744.   /* long instance_size; */
  3745.  
  3746.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_LONG]);
  3747.   field_decl = get_identifier ("instance_size");
  3748.   field_decl
  3749.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3750.   chainon (field_decl_chain, field_decl);
  3751.  
  3752.   /* struct objc_ivar_list *ivars; */
  3753.  
  3754.   decl_specs = build_tree_list (NULL_TREE,
  3755.                 xref_tag (RECORD_TYPE,
  3756.                       get_identifier (UTAG_IVAR_LIST)));
  3757.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("ivars"));
  3758.   field_decl
  3759.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3760.   chainon (field_decl_chain, field_decl);
  3761.  
  3762.   /* struct objc_method_list *methods; */
  3763.  
  3764.   decl_specs = build_tree_list (NULL_TREE,
  3765.                 xref_tag (RECORD_TYPE,
  3766.                       get_identifier (UTAG_METHOD_LIST)));
  3767.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("methods"));
  3768.   field_decl
  3769.     = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  3770.   chainon (field_decl_chain, field_decl);
  3771.  
  3772.   if (flag_next_runtime)
  3773.     {
  3774.       /* struct objc_cache *cache; */
  3775.  
  3776.       decl_specs = build_tree_list (NULL_TREE,
  3777.                     xref_tag (RECORD_TYPE,
  3778.                           get_identifier ("objc_cache")));
  3779.       field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("cache"));
  3780.       field_decl = grokfield (input_filename, lineno, field_decl,
  3781.                   decl_specs, NULL_TREE);
  3782.       chainon (field_decl_chain, field_decl);
  3783.     }
  3784.   else
  3785.     {
  3786.       /* struct sarray *dtable; */
  3787.  
  3788.       decl_specs = build_tree_list (NULL_TREE,
  3789.                     xref_tag (RECORD_TYPE,
  3790.                           get_identifier ("sarray")));
  3791.       field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("dtable"));
  3792.       field_decl = grokfield (input_filename, lineno, field_decl,
  3793.                   decl_specs, NULL_TREE);
  3794.       chainon (field_decl_chain, field_decl);
  3795.  
  3796.       /* struct objc_class *subclass_list; */
  3797.  
  3798.       decl_specs = build_tree_list (NULL_TREE, objc_class_template);
  3799.       field_decl
  3800.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("subclass_list"));
  3801.       field_decl = grokfield (input_filename, lineno, field_decl,
  3802.                   decl_specs, NULL_TREE);
  3803.       chainon (field_decl_chain, field_decl);
  3804.  
  3805.       /* struct objc_class *sibling_class; */
  3806.  
  3807.       decl_specs = build_tree_list (NULL_TREE, objc_class_template);
  3808.       field_decl
  3809.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("sibling_class"));
  3810.       field_decl = grokfield (input_filename, lineno, field_decl,
  3811.                   decl_specs, NULL_TREE);
  3812.       chainon (field_decl_chain, field_decl);
  3813.     }
  3814.  
  3815.   /* struct objc_protocol **protocol_list; */
  3816.  
  3817.   decl_specs = build_tree_list (NULL_TREE, 
  3818.                 xref_tag (RECORD_TYPE,
  3819.                       get_identifier (UTAG_PROTOCOL)));
  3820.   field_decl
  3821.     = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("protocol_list"));
  3822.   field_decl
  3823.     = build1 (INDIRECT_REF, NULL_TREE, field_decl);
  3824.   field_decl = grokfield (input_filename, lineno, field_decl,
  3825.               decl_specs, NULL_TREE);
  3826.   chainon (field_decl_chain, field_decl);
  3827.  
  3828.  
  3829.   finish_struct (objc_class_template, field_decl_chain);
  3830. }
  3831.  
  3832. /* Generate appropriate forward declarations for an implementation.  */
  3833.  
  3834. static void
  3835. synth_forward_declarations ()
  3836. {
  3837.   tree sc_spec, decl_specs, an_id;
  3838.  
  3839.   /* extern const struct objc_class _OBJC_CLASS_<my_name>; */
  3840.  
  3841.   an_id = synth_id_with_class_suffix ("_OBJC_CLASS", implementation_context);
  3842.  
  3843.   sc_spec = build_tree_list (NULL_TREE, ridpointers[(int) RID_CONST]);
  3844.   sc_spec = tree_cons (NULL_TREE, ridpointers[(int) RID_EXTERN], sc_spec);
  3845.   decl_specs = tree_cons (NULL_TREE, objc_class_template, sc_spec);
  3846.   UOBJC_CLASS_decl = define_decl (an_id, decl_specs);
  3847.   TREE_USED (UOBJC_CLASS_decl) = 1;
  3848.   DECL_ARTIFICIAL (UOBJC_CLASS_decl) = 1;
  3849.  
  3850.   /* extern const struct objc_class _OBJC_METACLASS_<my_name>; */
  3851.  
  3852.   an_id = synth_id_with_class_suffix ("_OBJC_METACLASS",
  3853.                       implementation_context);
  3854.  
  3855.   UOBJC_METACLASS_decl = define_decl (an_id, decl_specs);
  3856.   TREE_USED (UOBJC_METACLASS_decl) = 1;
  3857.  
  3858.   /* Pre-build the following entities - for speed/convenience. */
  3859.  
  3860.   an_id = get_identifier ("super_class");
  3861.   ucls_super_ref = build_component_ref (UOBJC_CLASS_decl, an_id);
  3862.   uucls_super_ref = build_component_ref (UOBJC_METACLASS_decl, an_id);
  3863. }
  3864.  
  3865. static void
  3866. error_with_ivar (message, decl, rawdecl)
  3867.      char *message;
  3868.      tree decl;
  3869.      tree rawdecl;
  3870. {
  3871.   count_error (0);
  3872.  
  3873.   report_error_function (DECL_SOURCE_FILE (decl));
  3874.  
  3875.   bzero (errbuf, BUFSIZE);
  3876.   error_with_file_and_line (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
  3877.                 "%s `%s'", message,
  3878.                 gen_declaration (rawdecl, errbuf));
  3879. }
  3880.  
  3881. #define USERTYPE(t)    (TREE_CODE (t) == RECORD_TYPE || \
  3882.              TREE_CODE (t) == UNION_TYPE ||  \
  3883.              TREE_CODE (t) == ENUMERAL_TYPE)
  3884.  
  3885. static void
  3886. check_ivars (inter, imp)
  3887.      tree inter;
  3888.      tree imp;
  3889. {
  3890.   tree intdecls = CLASS_IVARS (inter);
  3891.   tree impdecls = CLASS_IVARS (imp);
  3892.   tree rawintdecls = CLASS_RAW_IVARS (inter);
  3893.   tree rawimpdecls = CLASS_RAW_IVARS (imp);
  3894.  
  3895.   while (1)
  3896.     {
  3897.       tree t1, t2;
  3898.  
  3899.       if (intdecls == 0 && impdecls == 0)
  3900.     break;
  3901.       if (intdecls == 0 || impdecls == 0)
  3902.     {
  3903.       error ("inconsistent instance variable specification");
  3904.       break;
  3905.     }
  3906.  
  3907.       t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
  3908.  
  3909.       if (!comptypes (t1, t2))
  3910.     {
  3911.       if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
  3912.         {
  3913.           error_with_ivar ("conflicting instance variable type",
  3914.                    impdecls, rawimpdecls);
  3915.           error_with_ivar ("previous declaration of",
  3916.                    intdecls, rawintdecls);
  3917.         }
  3918.       else            /* both the type and the name don't match */
  3919.         {
  3920.           error ("inconsistent instance variable specification");
  3921.           break;
  3922.         }
  3923.     }
  3924.  
  3925.       else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
  3926.     {
  3927.       error_with_ivar ("conflicting instance variable name",
  3928.                impdecls, rawimpdecls);
  3929.       error_with_ivar ("previous declaration of",
  3930.                intdecls, rawintdecls);
  3931.     }
  3932.  
  3933.       intdecls = TREE_CHAIN (intdecls);
  3934.       impdecls = TREE_CHAIN (impdecls);
  3935.       rawintdecls = TREE_CHAIN (rawintdecls);
  3936.       rawimpdecls = TREE_CHAIN (rawimpdecls);
  3937.     }
  3938. }
  3939.  
  3940. /* Set super_type to the data type node for struct objc_super *,
  3941.    first defining struct objc_super itself.
  3942.    This needs to be done just once per compilation.  */
  3943.  
  3944. static tree
  3945. build_super_template ()
  3946. {
  3947.   tree record, decl_specs, field_decl, field_decl_chain;
  3948.  
  3949.   record = start_struct (RECORD_TYPE, get_identifier (UTAG_SUPER));
  3950.  
  3951.   /* struct objc_object *self; */
  3952.  
  3953.   decl_specs = build_tree_list (NULL_TREE, objc_object_reference);
  3954.   field_decl = get_identifier ("self");
  3955.   field_decl = build1 (INDIRECT_REF, NULL_TREE, field_decl);
  3956.   field_decl = grokfield (input_filename, lineno,
  3957.               field_decl, decl_specs, NULL_TREE);
  3958.   field_decl_chain = field_decl;
  3959.  
  3960.   /* struct objc_class *class; */
  3961.  
  3962.   decl_specs = get_identifier (UTAG_CLASS);
  3963.   decl_specs = build_tree_list (NULL_TREE, xref_tag (RECORD_TYPE, decl_specs));
  3964.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("class"));
  3965.  
  3966.   field_decl = grokfield (input_filename, lineno,
  3967.               field_decl, decl_specs, NULL_TREE);
  3968.   chainon (field_decl_chain, field_decl);
  3969.  
  3970.   finish_struct (record, field_decl_chain);
  3971.  
  3972.   /* `struct objc_super *' */
  3973.   super_type = groktypename (build_tree_list (build_tree_list (NULL_TREE,
  3974.                                    record),
  3975.                           build1 (INDIRECT_REF,
  3976.                               NULL_TREE, NULL_TREE)));
  3977.   return record;
  3978. }
  3979.  
  3980. /* struct objc_ivar {
  3981.      char *ivar_name;
  3982.      char *ivar_type;
  3983.      int ivar_offset;
  3984.    };  */
  3985.  
  3986. static tree
  3987. build_ivar_template ()
  3988. {
  3989.   tree objc_ivar_id, objc_ivar_record;
  3990.   tree decl_specs, field_decl, field_decl_chain;
  3991.  
  3992.   objc_ivar_id = get_identifier (UTAG_IVAR);
  3993.   objc_ivar_record = start_struct (RECORD_TYPE, objc_ivar_id);
  3994.  
  3995.   /* char *ivar_name; */
  3996.  
  3997.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_CHAR]);
  3998.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("ivar_name"));
  3999.  
  4000.   field_decl = grokfield (input_filename, lineno, field_decl,
  4001.               decl_specs, NULL_TREE);
  4002.   field_decl_chain = field_decl;
  4003.  
  4004.   /* char *ivar_type; */
  4005.  
  4006.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_CHAR]);
  4007.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("ivar_type"));
  4008.  
  4009.   field_decl = grokfield (input_filename, lineno, field_decl,
  4010.               decl_specs, NULL_TREE);
  4011.   chainon (field_decl_chain, field_decl);
  4012.  
  4013.   /* int ivar_offset; */
  4014.  
  4015.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_INT]);
  4016.   field_decl = get_identifier ("ivar_offset");
  4017.  
  4018.   field_decl = grokfield (input_filename, lineno, field_decl,
  4019.               decl_specs, NULL_TREE);
  4020.   chainon (field_decl_chain, field_decl);
  4021.  
  4022.   finish_struct (objc_ivar_record, field_decl_chain);
  4023.  
  4024.   return objc_ivar_record;
  4025. }
  4026.  
  4027. /* struct {
  4028.      int ivar_count;
  4029.      struct objc_ivar ivar_list[ivar_count];
  4030.    };  */
  4031.  
  4032. static tree
  4033. build_ivar_list_template (list_type, size)
  4034.      tree list_type;
  4035.      int size;
  4036. {
  4037.   tree objc_ivar_list_record;
  4038.   tree decl_specs, field_decl, field_decl_chain;
  4039.  
  4040.   objc_ivar_list_record = start_struct (RECORD_TYPE, NULL_TREE);
  4041.  
  4042.   /* int ivar_count; */
  4043.  
  4044.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_INT]);
  4045.   field_decl = get_identifier ("ivar_count");
  4046.  
  4047.   field_decl = grokfield (input_filename, lineno, field_decl,
  4048.               decl_specs, NULL_TREE);
  4049.   field_decl_chain = field_decl;
  4050.  
  4051.   /* struct objc_ivar ivar_list[]; */
  4052.  
  4053.   decl_specs = build_tree_list (NULL_TREE, list_type);
  4054.   field_decl = build_nt (ARRAY_REF, get_identifier ("ivar_list"),
  4055.              build_int_2 (size, 0));
  4056.  
  4057.   field_decl = grokfield (input_filename, lineno,
  4058.               field_decl, decl_specs, NULL_TREE);
  4059.   chainon (field_decl_chain, field_decl);
  4060.  
  4061.   finish_struct (objc_ivar_list_record, field_decl_chain);
  4062.  
  4063.   return objc_ivar_list_record;
  4064. }
  4065.  
  4066. /* struct {
  4067.      int method_next;
  4068.      int method_count;
  4069.      struct objc_method method_list[method_count];
  4070.    };  */
  4071.  
  4072. static tree
  4073. build_method_list_template (list_type, size)
  4074.      tree list_type;
  4075.      int size;
  4076. {
  4077.   tree objc_ivar_list_record;
  4078.   tree decl_specs, field_decl, field_decl_chain;
  4079.  
  4080.   objc_ivar_list_record = start_struct (RECORD_TYPE, NULL_TREE);
  4081.  
  4082. #ifdef __osf__
  4083. /* In the runtime supplied by NeXT the structure is defined as:
  4084.    struct {
  4085.         struct objc_method_list *method_next;
  4086.         int method_count;
  4087.         struct objc_method method_list[method_count];
  4088.    }
  4089.  
  4090.    therefore we need to change the code so that it produces a pointer 
  4091.    instead of an integer.
  4092. */
  4093.         /* struct objc_method_list *method_next; */
  4094.  
  4095.   decl_specs = build_tree_list (NULL_TREE, xref_tag (RECORD_TYPE,
  4096.   get_identifier (UTAG_METHOD_PROTOTYPE_LIST)));
  4097.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("method_next"));
  4098.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  4099.   field_decl_chain = field_decl;
  4100. #else
  4101.   /* int method_next; */
  4102.  
  4103.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_INT]);
  4104.  
  4105.   field_decl = get_identifier ("method_next");
  4106.  
  4107.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
  4108.   field_decl_chain = field_decl;
  4109. #endif __osf__
  4110.   /* int method_count; */
  4111.  
  4112.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_INT]);
  4113.   field_decl = get_identifier ("method_count");
  4114.  
  4115.   field_decl = grokfield (input_filename, lineno,
  4116.               field_decl, decl_specs, NULL_TREE);
  4117.   chainon (field_decl_chain, field_decl);
  4118.  
  4119.   /* struct objc_method method_list[]; */
  4120.  
  4121.   decl_specs = build_tree_list (NULL_TREE, list_type);
  4122.   field_decl = build_nt (ARRAY_REF, get_identifier ("method_list"),
  4123.              build_int_2 (size, 0));
  4124.  
  4125.   field_decl = grokfield (input_filename, lineno,
  4126.               field_decl, decl_specs, NULL_TREE);
  4127.   chainon (field_decl_chain, field_decl);
  4128.  
  4129.   finish_struct (objc_ivar_list_record, field_decl_chain);
  4130.  
  4131.   return objc_ivar_list_record;
  4132. }
  4133.  
  4134. static tree
  4135. build_ivar_list_initializer (type, field_decl)
  4136.      tree type;
  4137.      tree field_decl;
  4138. {
  4139.   tree initlist = NULL_TREE;
  4140.  
  4141.   do
  4142.     {
  4143.       tree ivar = NULL_TREE;
  4144.  
  4145.       /* Set name. */
  4146.       if (DECL_NAME (field_decl))
  4147.     ivar = tree_cons (NULL_TREE,
  4148.               add_objc_string (DECL_NAME (field_decl),
  4149.                        meth_var_names),
  4150.               ivar);
  4151.       else
  4152.     /* Unnamed bit-field ivar (yuck). */
  4153.     ivar = tree_cons (NULL_TREE, build_int_2 (0, 0), ivar);
  4154.  
  4155.       /* Set type. */
  4156.       encode_field_decl (field_decl,
  4157.              obstack_object_size (&util_obstack),
  4158.              OBJC_ENCODE_DONT_INLINE_DEFS);
  4159.  
  4160.       /* Null terminate string.  */
  4161.       obstack_1grow (&util_obstack, 0);
  4162.       ivar
  4163.     = tree_cons
  4164.       (NULL_TREE,
  4165.        add_objc_string (get_identifier (obstack_finish (&util_obstack)),
  4166.                 meth_var_types),
  4167.        ivar);
  4168.       obstack_free (&util_obstack, util_firstobj);
  4169.  
  4170.       /* set offset */
  4171.       ivar
  4172.     = tree_cons
  4173.       (NULL_TREE,
  4174.        build_int_2 ((TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field_decl))
  4175.              / BITS_PER_UNIT),
  4176.             0),
  4177.        ivar);
  4178.  
  4179.       initlist = tree_cons (NULL_TREE, 
  4180.                 build_constructor (type, nreverse (ivar)),
  4181.                 initlist);
  4182.  
  4183.       field_decl = TREE_CHAIN (field_decl);
  4184.     }
  4185.   while (field_decl);
  4186.  
  4187.   return build_constructor (build_array_type (type, 0), nreverse (initlist));
  4188. }
  4189.  
  4190. static tree
  4191. generate_ivars_list (type, name, size, list)
  4192.      tree type;
  4193.      char *name;
  4194.      int size;
  4195.      tree list;
  4196. {
  4197.   tree sc_spec, decl_specs, decl, initlist;
  4198.  
  4199.   sc_spec = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC], NULL_TREE);
  4200.   decl_specs = tree_cons (NULL_TREE, type, sc_spec);
  4201.  
  4202.   decl = start_decl (synth_id_with_class_suffix (name, implementation_context),
  4203.              decl_specs, 1);
  4204.   end_temporary_allocation ();
  4205.  
  4206.   initlist = build_tree_list (NULL_TREE, build_int_2 (size, 0));
  4207.   initlist = tree_cons (NULL_TREE, list, initlist);
  4208.  
  4209.   finish_decl (decl,
  4210.            build_constructor (TREE_TYPE (decl), nreverse (initlist)),
  4211.            NULL_TREE);
  4212.  
  4213.   return decl;
  4214. }
  4215.  
  4216. static void
  4217. generate_ivar_lists ()
  4218. {
  4219.   tree initlist, ivar_list_template, chain;
  4220.   tree cast, variable_length_type;
  4221.   int size;
  4222.  
  4223.   generating_instance_variables = 1;
  4224.  
  4225.   if (!objc_ivar_template)
  4226.     objc_ivar_template = build_ivar_template ();
  4227.  
  4228.   cast
  4229.     = build_tree_list
  4230.       (build_tree_list (NULL_TREE, xref_tag (RECORD_TYPE,
  4231.                      get_identifier (UTAG_IVAR_LIST))),
  4232.        NULL_TREE);
  4233.   variable_length_type = groktypename (cast);
  4234.  
  4235.   /* Only generate class variables for the root of the inheritance
  4236.      hierarchy since these will be the same for every class. */
  4237.  
  4238.   if (CLASS_SUPER_NAME (implementation_template) == NULL_TREE
  4239.       && (chain = TYPE_FIELDS (objc_class_template)))
  4240.     {
  4241.       size = list_length (chain);
  4242.  
  4243.       ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
  4244.       initlist = build_ivar_list_initializer (objc_ivar_template, chain);
  4245.  
  4246.       UOBJC_CLASS_VARIABLES_decl
  4247.     = generate_ivars_list (ivar_list_template, "_OBJC_CLASS_VARIABLES",
  4248.                    size, initlist);
  4249.       TREE_TYPE (UOBJC_CLASS_VARIABLES_decl) = variable_length_type;
  4250.     }
  4251.   else
  4252.     UOBJC_CLASS_VARIABLES_decl = 0;
  4253.  
  4254.   chain = CLASS_IVARS (implementation_template);
  4255.   if (chain)
  4256.     {
  4257.       size = list_length (chain);
  4258.       ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
  4259.       initlist = build_ivar_list_initializer (objc_ivar_template, chain);
  4260.  
  4261.       UOBJC_INSTANCE_VARIABLES_decl
  4262.     = generate_ivars_list (ivar_list_template, "_OBJC_INSTANCE_VARIABLES",
  4263.                    size, initlist);
  4264.       TREE_TYPE (UOBJC_INSTANCE_VARIABLES_decl) = variable_length_type;
  4265.     }
  4266.   else
  4267.     UOBJC_INSTANCE_VARIABLES_decl = 0;
  4268.  
  4269.   generating_instance_variables = 0;
  4270. }
  4271.  
  4272. static tree
  4273. build_dispatch_table_initializer (type, entries)
  4274.      tree type;
  4275.      tree entries;
  4276. {
  4277.   tree initlist = NULL_TREE;
  4278.  
  4279.   do
  4280.     {
  4281.       tree elemlist = NULL_TREE;
  4282.  
  4283.       elemlist = tree_cons (NULL_TREE, build_selector (METHOD_SEL_NAME (entries)),
  4284.                 NULL_TREE);
  4285.  
  4286.       elemlist = tree_cons (NULL_TREE, add_objc_string (METHOD_ENCODING (entries),
  4287.                             meth_var_types),
  4288.                 elemlist);
  4289.  
  4290.       elemlist = tree_cons (NULL_TREE, 
  4291.                 build_unary_op (ADDR_EXPR, METHOD_DEFINITION (entries), 1),
  4292.                 elemlist);
  4293.  
  4294.       initlist = tree_cons (NULL_TREE, 
  4295.                 build_constructor (type, nreverse (elemlist)),
  4296.                 initlist);
  4297.  
  4298.       entries = TREE_CHAIN (entries);
  4299.     }
  4300.   while (entries);
  4301.  
  4302.   return build_constructor (build_array_type (type, 0), nreverse (initlist));
  4303. }
  4304.  
  4305. /* To accomplish method prototyping without generating all kinds of
  4306.    inane warnings, the definition of the dispatch table entries were
  4307.    changed from:
  4308.  
  4309.        struct objc_method { SEL _cmd; ...; id (*_imp)(); };
  4310.    to:
  4311.        struct objc_method { SEL _cmd; ...; void *_imp; };  */
  4312.  
  4313. static tree
  4314. build_method_template ()
  4315. {
  4316.   tree _SLT_record;
  4317.   tree decl_specs, field_decl, field_decl_chain;
  4318.  
  4319.   _SLT_record = start_struct (RECORD_TYPE, get_identifier (UTAG_METHOD));
  4320.  
  4321. #ifdef OBJC_INT_SELECTORS
  4322.   /* unsigned int _cmd; */
  4323.   decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_UNSIGNED],
  4324.               NULL_TREE);
  4325.   decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_INT], decl_specs);
  4326.   field_decl = get_identifier ("_cmd");
  4327. #else /* not OBJC_INT_SELECTORS */
  4328.   /* struct objc_selector *_cmd; */
  4329.   decl_specs = tree_cons (NULL_TREE,
  4330.               xref_tag (RECORD_TYPE,
  4331.                     get_identifier (TAG_SELECTOR)),
  4332.               NULL_TREE);
  4333.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("_cmd"));
  4334. #endif /* not OBJC_INT_SELECTORS */
  4335.  
  4336.   field_decl = grokfield (input_filename, lineno, field_decl,
  4337.               decl_specs, NULL_TREE);
  4338.   field_decl_chain = field_decl;
  4339.  
  4340.   decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_CHAR], NULL_TREE);
  4341.   field_decl = build1 (INDIRECT_REF, NULL_TREE,
  4342.                get_identifier ("method_types"));
  4343.   field_decl = grokfield (input_filename, lineno, field_decl,
  4344.               decl_specs, NULL_TREE);
  4345.   chainon (field_decl_chain, field_decl);
  4346.  
  4347.   /* void *_imp; */
  4348.  
  4349.   decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_VOID], NULL_TREE);
  4350.   field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("_imp"));
  4351.   field_decl = grokfield (input_filename, lineno, field_decl,
  4352.               decl_specs, NULL_TREE);
  4353.   chainon (field_decl_chain, field_decl);
  4354.  
  4355.   finish_struct (_SLT_record, field_decl_chain);
  4356.  
  4357.   return _SLT_record;
  4358. }
  4359.  
  4360.  
  4361. static tree
  4362. generate_dispatch_table (type, name, size, list)
  4363.      tree type;
  4364.      char *name;
  4365.      int size;
  4366.      tree list;
  4367. {
  4368.   tree sc_spec, decl_specs, decl, initlist;
  4369.  
  4370.   sc_spec = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC], NULL_TREE);
  4371.   decl_specs = tree_cons (NULL_TREE, type, sc_spec);
  4372.  
  4373.   decl = start_decl (synth_id_with_class_suffix (name, implementation_context),
  4374.              decl_specs, 1);
  4375.   end_temporary_allocation ();
  4376.  
  4377.   initlist = build_tree_list (NULL_TREE, build_int_2 (0, 0));
  4378.   initlist = tree_cons (NULL_TREE, build_int_2 (size, 0), initlist);
  4379.   initlist = tree_cons (NULL_TREE, list, initlist);
  4380.  
  4381.   finish_decl (decl,
  4382.            build_constructor (TREE_TYPE (decl), nreverse (initlist)),
  4383.            NULL_TREE);
  4384.  
  4385.   return decl;
  4386. }
  4387.  
  4388. static void
  4389. generate_dispatch_tables ()
  4390. {
  4391.   tree initlist, chain, method_list_template;
  4392.   tree cast, variable_length_type;
  4393.   int size;
  4394.  
  4395.   if (!objc_method_template)
  4396.     objc_method_template = build_method_template ();
  4397.  
  4398.   cast
  4399.     = build_tree_list
  4400.       (build_tree_list (NULL_TREE,
  4401.             xref_tag (RECORD_TYPE,
  4402.                   get_identifier (UTAG_METHOD_LIST))),
  4403.        NULL_TREE);
  4404.  
  4405.   variable_length_type = groktypename (cast);
  4406.  
  4407.   chain = CLASS_CLS_METHODS (implementation_context);
  4408.   if (chain)
  4409.     {
  4410.       size = list_length (chain);
  4411.  
  4412.       method_list_template
  4413.     = build_method_list_template (objc_method_template, size);
  4414.       initlist
  4415.     = build_dispatch_table_initializer (objc_method_template, chain);
  4416.  
  4417.       UOBJC_CLASS_METHODS_decl
  4418.     = generate_dispatch_table (method_list_template,
  4419.                    ((TREE_CODE (implementation_context)
  4420.                      == CLASS_IMPLEMENTATION_TYPE)
  4421.                     ? "_OBJC_CLASS_METHODS"
  4422.                     : "_OBJC_CATEGORY_CLASS_METHODS"),
  4423.                    size, initlist);
  4424.       TREE_TYPE (UOBJC_CLASS_METHODS_decl) = variable_length_type;
  4425.     }
  4426.   else
  4427.     UOBJC_CLASS_METHODS_decl = 0;
  4428.  
  4429.   chain = CLASS_NST_METHODS (implementation_context);
  4430.   if (chain)
  4431.     {
  4432.       size = list_length (chain);
  4433.  
  4434.       method_list_template
  4435.     = build_method_list_template (objc_method_template, size);
  4436.       initlist
  4437.     = build_dispatch_table_initializer (objc_method_template, chain);
  4438.  
  4439.       if (TREE_CODE (implementation_context) == CLASS_IMPLEMENTATION_TYPE)
  4440.     UOBJC_INSTANCE_METHODS_decl
  4441.       = generate_dispatch_table (method_list_template,
  4442.                      "_OBJC_INSTANCE_METHODS",
  4443.                      size, initlist);
  4444.       else
  4445.     /* We have a category. */
  4446.     UOBJC_INSTANCE_METHODS_decl
  4447.       = generate_dispatch_table (method_list_template,
  4448.                      "_OBJC_CATEGORY_INSTANCE_METHODS",
  4449.                      size, initlist);
  4450.       TREE_TYPE (UOBJC_INSTANCE_METHODS_decl) = variable_length_type;
  4451.     }
  4452.   else
  4453.     UOBJC_INSTANCE_METHODS_decl = 0;
  4454. }
  4455.  
  4456. static tree
  4457. generate_protocol_list (i_or_p)
  4458.      tree i_or_p;
  4459. {
  4460.   static tree cast_type = 0;
  4461.   tree initlist, decl_specs, sc_spec;
  4462.   tree refs_decl, expr_decl, lproto, e, plist;
  4463.   int size = 0;
  4464.  
  4465.   if (TREE_CODE (i_or_p) == CLASS_INTERFACE_TYPE
  4466.       || TREE_CODE (i_or_p) == CATEGORY_INTERFACE_TYPE)
  4467.     plist = CLASS_PROTOCOL_LIST (i_or_p);
  4468.   else if (TREE_CODE (i_or_p) == PROTOCOL_INTERFACE_TYPE)
  4469.     plist = PROTOCOL_LIST (i_or_p);
  4470.   else
  4471.     abort ();
  4472.  
  4473.   if (!cast_type)
  4474.     cast_type
  4475.       = groktypename
  4476.     (build_tree_list
  4477.      (build_tree_list (NULL_TREE,
  4478.                xref_tag (RECORD_TYPE,
  4479.                      get_identifier (UTAG_PROTOCOL))),
  4480.       build1 (INDIRECT_REF, NULL_TREE, NULL_TREE)));
  4481.  
  4482.   /* Compute size. */
  4483.   for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
  4484.     if (TREE_CODE (TREE_VALUE (lproto)) == PROTOCOL_INTERFACE_TYPE
  4485.     && PROTOCOL_FORWARD_DECL (TREE_VALUE (lproto)))
  4486.       size++;
  4487.  
  4488.   /* Build initializer. */
  4489.   initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), NULL_TREE);
  4490.  
  4491.   e = build_int_2 (size, 0);
  4492.   TREE_TYPE (e) = cast_type;
  4493.   initlist = tree_cons (NULL_TREE, e, initlist);
  4494.  
  4495.   for (lproto = plist; lproto; lproto = TREE_CHAIN (lproto))
  4496.     {
  4497.       tree pval = TREE_VALUE (lproto);
  4498.  
  4499.       if (TREE_CODE (pval) == PROTOCOL_INTERFACE_TYPE
  4500.       && PROTOCOL_FORWARD_DECL (pval))
  4501.     {
  4502.       e = build_unary_op (ADDR_EXPR, PROTOCOL_FORWARD_DECL (pval), 0);
  4503.       initlist = tree_cons (NULL_TREE, e, initlist);
  4504.     }
  4505.     }
  4506.  
  4507.   /* static struct objc_protocol *refs[n]; */
  4508.  
  4509.   sc_spec = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC], NULL_TREE);
  4510.   decl_specs = tree_cons (NULL_TREE, xref_tag (RECORD_TYPE,
  4511.                        get_identifier (UTAG_PROTOCOL)),
  4512.               sc_spec);
  4513.  
  4514.   if (TREE_CODE (i_or_p) == PROTOCOL_INTERFACE_TYPE)
  4515.     expr_decl = build_nt (ARRAY_REF,
  4516.               synth_id_with_class_suffix ("_OBJC_PROTOCOL_REFS",
  4517.                               i_or_p),
  4518.               build_int_2 (size + 2, 0));
  4519.   else if (TREE_CODE (i_or_p) == CLASS_INTERFACE_TYPE)
  4520.     expr_decl = build_nt (ARRAY_REF,
  4521.               synth_id_with_class_suffix ("_OBJC_CLASS_PROTOCOLS",
  4522.                               i_or_p),
  4523.               build_int_2 (size + 2, 0));
  4524.   else if (TREE_CODE (i_or_p) == CATEGORY_INTERFACE_TYPE)
  4525.     expr_decl
  4526.       = build_nt (ARRAY_REF,
  4527.           synth_id_with_class_suffix ("_OBJC_CATEGORY_PROTOCOLS",
  4528.                           i_or_p),
  4529.           build_int_2 (size + 2, 0));
  4530.  
  4531.   expr_decl = build1 (INDIRECT_REF, NULL_TREE, expr_decl);
  4532.  
  4533.   refs_decl = start_decl (expr_decl, decl_specs, 1);
  4534.   end_temporary_allocation ();
  4535.  
  4536.   finish_decl (refs_decl, build_constructor (TREE_TYPE (refs_decl),
  4537.                          nreverse (initlist)),
  4538.            NULL_TREE);
  4539.  
  4540.   return refs_decl;
  4541. }
  4542.  
  4543. static tree
  4544. build_category_initializer (type, cat_name, class_name,
  4545.                 instance_methods, class_methods, protocol_list)
  4546.      tree type;
  4547.      tree cat_name;
  4548.      tree class_name;
  4549.      tree instance_methods;
  4550.      tree class_methods;
  4551.      tree protocol_list;
  4552. {
  4553.   tree initlist = NULL_TREE, expr;
  4554.  
  4555.   initlist = tree_cons (NULL_TREE, cat_name, initlist);
  4556.   initlist = tree_cons (NULL_TREE, class_name, initlist);
  4557.  
  4558.   if (!instance_methods)
  4559.     initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  4560.   else
  4561.     {
  4562.       expr = build_unary_op (ADDR_EXPR, instance_methods, 0);
  4563.       initlist = tree_cons (NULL_TREE, expr, initlist);
  4564.     }
  4565.   if (!class_methods)
  4566.     initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  4567.   else
  4568.     {
  4569.       expr = build_unary_op (ADDR_EXPR, class_methods, 0);
  4570.       initlist = tree_cons (NULL_TREE, expr, initlist);
  4571.     }
  4572.  
  4573.   /* protocol_list = */
  4574.   if (!protocol_list)
  4575.      initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  4576.   else
  4577.      {
  4578.     static tree cast_type2;
  4579.  
  4580.     if (!cast_type2)
  4581.       cast_type2
  4582.         = groktypename
  4583.           (build_tree_list
  4584.            (build_tree_list (NULL_TREE,
  4585.                  xref_tag (RECORD_TYPE,
  4586.                        get_identifier (UTAG_PROTOCOL))),
  4587.         build1 (INDIRECT_REF, NULL_TREE,
  4588.             build1 (INDIRECT_REF, NULL_TREE, NULL_TREE))));
  4589.  
  4590.     expr = build_unary_op (ADDR_EXPR, protocol_list, 0);
  4591.     TREE_TYPE (expr) = cast_type2;
  4592.     initlist = tree_cons (NULL_TREE, expr, initlist);
  4593.      }
  4594.  
  4595.   return build_constructor (type, nreverse (initlist));
  4596. }
  4597.  
  4598. /* struct objc_class {
  4599.      struct objc_class *isa;
  4600.      struct objc_class *super_class;
  4601.      char *name;
  4602.      long version;
  4603.      long info;
  4604.      long instance_size;
  4605.      struct objc_ivar_list *ivars;
  4606.      struct objc_method_list *methods;
  4607.      if (flag_next_runtime)
  4608.        struct objc_cache *cache;
  4609.      else {
  4610.        struct sarray *dtable;
  4611.        struct objc_class *subclass_list;
  4612.        struct objc_class *sibling_class;
  4613.      }
  4614.      struct objc_protocol_list *protocols;
  4615.    };  */
  4616.  
  4617. static tree
  4618. build_shared_structure_initializer (type, isa, super, name, size, status,
  4619.                     dispatch_table, ivar_list, protocol_list)
  4620.      tree type;
  4621.      tree isa;
  4622.      tree super;
  4623.      tree name;
  4624.      tree size;
  4625.      int status;
  4626.      tree dispatch_table;
  4627.      tree ivar_list;
  4628.      tree protocol_list;
  4629. {
  4630.   tree initlist = NULL_TREE, expr;
  4631.  
  4632.   /* isa = */
  4633.   initlist = tree_cons (NULL_TREE, isa, initlist);
  4634.  
  4635.   /* super_class = */
  4636.   initlist = tree_cons (NULL_TREE, super, initlist);
  4637.  
  4638.   /* name = */
  4639.   initlist = tree_cons (NULL_TREE, default_conversion (name), initlist);
  4640.  
  4641.   /* version = */
  4642.   initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  4643.  
  4644.   /* info = */
  4645.   initlist = tree_cons (NULL_TREE, build_int_2 (status, 0), initlist);
  4646.  
  4647.   /* instance_size = */
  4648.   initlist = tree_cons (NULL_TREE, size, initlist);
  4649.  
  4650.   /* objc_ivar_list = */
  4651.   if (!ivar_list)
  4652.     initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  4653.   else
  4654.     {
  4655.       expr = build_unary_op (ADDR_EXPR, ivar_list, 0);
  4656.       initlist = tree_cons (NULL_TREE, expr, initlist);
  4657.     }
  4658.  
  4659.   /* objc_method_list = */
  4660.   if (!dispatch_table)
  4661.     initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  4662.   else
  4663.     {
  4664.       expr = build_unary_op (ADDR_EXPR, dispatch_table, 0);
  4665.       initlist = tree_cons (NULL_TREE, expr, initlist);
  4666.     }
  4667.  
  4668.   if (flag_next_runtime)
  4669.     /* method_cache = */
  4670.     initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  4671.   else
  4672.     {
  4673.       /* dtable = */
  4674.       initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  4675.  
  4676.       /* subclass_list = */
  4677.       initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  4678.  
  4679.       /* sibling_class = */
  4680.       initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  4681.     }
  4682.  
  4683.   /* protocol_list = */
  4684.   if (! protocol_list)
  4685.     initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
  4686.   else
  4687.      {
  4688.      static tree cast_type2;
  4689.  
  4690.      if (!cast_type2)
  4691.         cast_type2
  4692.       = groktypename
  4693.         (build_tree_list
  4694.          (build_tree_list (NULL_TREE,
  4695.                    xref_tag (RECORD_TYPE,
  4696.                      get_identifier (UTAG_PROTOCOL))),
  4697.           build1 (INDIRECT_REF, NULL_TREE,
  4698.               build1 (INDIRECT_REF, NULL_TREE, NULL_TREE))));
  4699.  
  4700.      expr = build_unary_op (ADDR_EXPR, protocol_list, 0);
  4701.      TREE_TYPE (expr) = cast_type2;
  4702.      initlist = tree_cons (NULL_TREE, expr, initlist);
  4703.      }
  4704.  
  4705.   return build_constructor (type, nreverse (initlist));
  4706. }
  4707.  
  4708. /* static struct objc_category _OBJC_CATEGORY_<name> = { ... };  */
  4709. static void
  4710. generate_category (cat)
  4711.      tree cat;
  4712. {
  4713.   tree sc_spec, decl_specs, decl;
  4714.   tree initlist, cat_name_expr, class_name_expr;
  4715.   tree protocol_decl, category;
  4716.  
  4717.   add_class_reference (CLASS_NAME (cat));
  4718.   cat_name_expr = add_objc_string (CLASS_SUPER_NAME (cat), class_names);
  4719.  
  4720.   class_name_expr = add_objc_string (CLASS_NAME (cat), class_names);
  4721.  
  4722.   category = CLASS_CATEGORY_LIST (implementation_template);
  4723.  
  4724.   /* find the category interface from the class it is associated with */
  4725.   while (category)
  4726.     {
  4727.       if (CLASS_SUPER_NAME (cat) == CLASS_SUPER_NAME (category))
  4728.     break;
  4729.       category = CLASS_CATEGORY_LIST (category);
  4730.     }
  4731.  
  4732.   if (category && CLASS_PROTOCOL_LIST (category))
  4733.     {
  4734.       generate_protocol_references (CLASS_PROTOCOL_LIST (category));
  4735.       protocol_decl = generate_protocol_list (category);
  4736.     }
  4737.   else
  4738.     protocol_decl = 0;
  4739.  
  4740.   sc_spec = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC], NULL_TREE);
  4741.   decl_specs = tree_cons (NULL_TREE, objc_category_template, sc_spec);
  4742.  
  4743.   decl = start_decl (synth_id_with_class_suffix ("_OBJC_CATEGORY",
  4744.                          implementation_context),
  4745.              decl_specs, 1);
  4746.   end_temporary_allocation ();
  4747.  
  4748.   initlist = build_category_initializer (TREE_TYPE (decl),
  4749.                      cat_name_expr, class_name_expr,
  4750.                      UOBJC_INSTANCE_METHODS_decl,
  4751.                      UOBJC_CLASS_METHODS_decl,
  4752.                      protocol_decl);
  4753.  
  4754.   TREE_USED (decl) = 1;
  4755.   finish_decl (decl, initlist, NULL_TREE);
  4756. }
  4757.  
  4758. /* const struct objc_class _OBJC_METACLASS_Foo={ ... };
  4759.    const struct objc_class _OBJC_CLASS_Foo={ ... };  */
  4760.  
  4761. static void
  4762. generate_shared_structures ()
  4763. {
  4764.   tree sc_spec, decl_specs, decl;
  4765.   tree name_expr, super_expr, root_expr;
  4766.   tree my_root_id = NULL_TREE, my_super_id = NULL_TREE;
  4767.   tree cast_type, initlist, protocol_decl;
  4768.  
  4769.   my_super_id = CLASS_SUPER_NAME (implementation_template);
  4770.   if (my_super_id)
  4771.     {
  4772.       add_class_reference (my_super_id);
  4773.  
  4774.       /* Compute "my_root_id" - this is required for code generation.
  4775.          the "isa" for all meta class structures points to the root of
  4776.          the inheritance hierarchy (e.g. "__Object")...  */
  4777.       my_root_id = my_super_id;
  4778.       do
  4779.     {
  4780.       tree my_root_int = lookup_interface (my_root_id);
  4781.  
  4782.       if (my_root_int && CLASS_SUPER_NAME (my_root_int))
  4783.         my_root_id = CLASS_SUPER_NAME (my_root_int);
  4784.       else
  4785.         break;
  4786.     }
  4787.       while (1);
  4788.     }
  4789.   else
  4790.     /* No super class. */
  4791.     my_root_id = CLASS_NAME (implementation_template);
  4792.  
  4793.   cast_type
  4794.     = groktypename (build_tree_list (build_tree_list (NULL_TREE,
  4795.                               objc_class_template),
  4796.                      build1 (INDIRECT_REF,
  4797.                          NULL_TREE, NULL_TREE)));
  4798.  
  4799.   name_expr = add_objc_string (CLASS_NAME (implementation_template),
  4800.                    class_names);
  4801.  
  4802.   /* Install class `isa' and `super' pointers at runtime. */
  4803.   if (my_super_id)
  4804.     {
  4805.       super_expr = add_objc_string (my_super_id, class_names);
  4806.       super_expr = build_c_cast (cast_type, super_expr);
  4807.     }
  4808.   else
  4809.     super_expr = build_int_2 (0, 0);
  4810.  
  4811.   root_expr = add_objc_string (my_root_id, class_names);
  4812.   root_expr = build_c_cast (cast_type, root_expr);
  4813.  
  4814.   if (CLASS_PROTOCOL_LIST (implementation_template))
  4815.     {
  4816.       generate_protocol_references
  4817.     (CLASS_PROTOCOL_LIST (implementation_template));
  4818.       protocol_decl = generate_protocol_list (implementation_template);
  4819.     }
  4820.   else
  4821.     protocol_decl = 0;
  4822.  
  4823.   /* const struct objc_class _OBJC_METACLASS_Foo = { ... }; */
  4824.  
  4825.   sc_spec = build_tree_list (NULL_TREE, ridpointers[(int) RID_CONST]);
  4826.   decl_specs = tree_cons (NULL_TREE, objc_class_template, sc_spec);
  4827.  
  4828.   decl = start_decl (DECL_NAME (UOBJC_METACLASS_decl), decl_specs, 1);
  4829.   end_temporary_allocation ();
  4830.  
  4831.   initlist
  4832.     = build_shared_structure_initializer
  4833.       (TREE_TYPE (decl),
  4834.        root_expr, super_expr, name_expr,
  4835.        build_int_2 ((TREE_INT_CST_LOW (TYPE_SIZE (objc_class_template))
  4836.             / BITS_PER_UNIT),
  4837.             0),
  4838.        2 /*CLS_META*/,
  4839.        UOBJC_CLASS_METHODS_decl,
  4840.        UOBJC_CLASS_VARIABLES_decl,
  4841.        protocol_decl);
  4842.  
  4843. #ifdef SHOULD_THIS_REALLY_BE_PRIVATE
  4844.   /* ??? If so, how can one generate global variables guaranteed to be unique?
  4845.      (Cf. get_file_function_name in tree.c.) figueroa@next.com  */
  4846.   TREE_PUBLIC (decl) = 0;
  4847. #endif
  4848.   finish_decl (decl, initlist, NULL_TREE);
  4849.  
  4850.   /* const struct objc_class _OBJC_CLASS_Foo={ ... }; */
  4851.  
  4852.   decl = start_decl (DECL_NAME (UOBJC_CLASS_decl), decl_specs, 1);
  4853.   end_temporary_allocation ();
  4854.  
  4855.   initlist
  4856.     = build_shared_structure_initializer
  4857.       (TREE_TYPE (decl),
  4858.        build_unary_op (ADDR_EXPR, UOBJC_METACLASS_decl, 0),
  4859.        super_expr, name_expr,
  4860.        build_int_2
  4861.        ((TREE_INT_CST_LOW
  4862.      (TYPE_SIZE (CLASS_STATIC_TEMPLATE (implementation_template)))
  4863.      / BITS_PER_UNIT),
  4864.     0),
  4865.        1 /*CLS_FACTORY*/,
  4866.        UOBJC_INSTANCE_METHODS_decl,
  4867.        UOBJC_INSTANCE_VARIABLES_decl,
  4868.        protocol_decl);
  4869.  
  4870. #ifdef SHOULD_THIS_REALLY_BE_PRIVATE
  4871.   /* ??? If so, how can one generate global variables guaranteed to be unique?
  4872.      (Cf. get_file_function_name in tree.c.) figueroa@next.com  */
  4873.   TREE_PUBLIC (decl) = 0;
  4874. #endif
  4875.   finish_decl (decl, initlist, NULL_TREE);
  4876. }
  4877.  
  4878. static tree
  4879. synth_id_with_class_suffix (preamble, ctxt)
  4880.      char *preamble;
  4881.      tree ctxt;
  4882. {
  4883.   char *string;
  4884.   if (TREE_CODE (ctxt) == CLASS_IMPLEMENTATION_TYPE
  4885.       || TREE_CODE (ctxt) == CLASS_INTERFACE_TYPE)
  4886.     {
  4887.       char *class_name
  4888.     = IDENTIFIER_POINTER (CLASS_NAME (implementation_context));
  4889.       string = (char *) alloca (strlen (preamble) + strlen (class_name) + 3);
  4890.       sprintf (string, "%s_%s", preamble,
  4891.            IDENTIFIER_POINTER (CLASS_NAME (ctxt)));
  4892.     }
  4893.   else if (TREE_CODE (ctxt) == CATEGORY_IMPLEMENTATION_TYPE
  4894.        || TREE_CODE (ctxt) == CATEGORY_INTERFACE_TYPE)
  4895.     {
  4896.       /* We have a category. */
  4897.       char *class_name
  4898.     = IDENTIFIER_POINTER (CLASS_NAME (implementation_context));
  4899.       char *class_super_name
  4900.     = IDENTIFIER_POINTER (CLASS_SUPER_NAME (implementation_context));
  4901.       string = (char *) alloca (strlen (preamble)
  4902.                 + strlen (class_name)
  4903.                 + strlen (class_super_name)
  4904.                 + 3);
  4905.       sprintf (string, "%s_%s_%s", preamble, class_name, class_super_name);
  4906.     }
  4907.   else if (TREE_CODE (ctxt) == PROTOCOL_INTERFACE_TYPE)
  4908.     {
  4909.       char *protocol_name = IDENTIFIER_POINTER (PROTOCOL_NAME (ctxt));
  4910.       string
  4911.     = (char *) alloca (strlen (preamble) + strlen (protocol_name) + 3);
  4912.       sprintf (string, "%s_%s", preamble, protocol_name);
  4913.     }
  4914.   return get_identifier (string);
  4915. }
  4916.  
  4917. static int
  4918. is_objc_type_qualifier (node)
  4919.      tree node;
  4920. {
  4921.   return (TREE_CODE (node) == IDENTIFIER_NODE
  4922.       && (node == ridpointers [(int) RID_CONST]
  4923.           || node == ridpointers [(int) RID_VOLATILE]
  4924.           || node == ridpointers [(int) RID_IN]
  4925.           || node == ridpointers [(int) RID_OUT]
  4926.           || node == ridpointers [(int) RID_INOUT]
  4927.           || node == ridpointers [(int) RID_BYCOPY]
  4928.           || node == ridpointers [(int) RID_BYREF]
  4929.           || node == ridpointers [(int) RID_ONEWAY]));
  4930. }
  4931.  
  4932. /* If type is empty or only type qualifiers are present, add default
  4933.    type of id (otherwise grokdeclarator will default to int).  */
  4934.  
  4935. static tree
  4936. adjust_type_for_id_default (type, is_return_type)
  4937.      tree type;
  4938. {
  4939.   tree declspecs, chain, result = 0;
  4940.   int synth_void = 0;
  4941.  
  4942.   if (!type)
  4943.     return build_tree_list (build_tree_list (NULL_TREE, objc_object_reference),
  4944.                 build1 (INDIRECT_REF, NULL_TREE, NULL_TREE));
  4945.  
  4946.   declspecs = TREE_PURPOSE (type);
  4947.  
  4948.   /* Determine if a typespec is present.  */
  4949.   for (chain = declspecs;
  4950.        chain;
  4951.        chain = TREE_CHAIN (chain))
  4952.     {
  4953.       tree node = TREE_VALUE (chain);
  4954.  
  4955.       if (!is_objc_type_qualifier (node))
  4956.     {
  4957.       result = type;
  4958.       break;
  4959.     }
  4960.       else if (node == ridpointers [(int) RID_ONEWAY])
  4961.     synth_void = 1;
  4962.     }
  4963.  
  4964.   if (!result)
  4965.     {
  4966.       if (is_return_type && synth_void)
  4967.     result = build_tree_list (tree_cons (NULL_TREE, 
  4968.                          ridpointers [(int) RID_VOID],
  4969.                          declspecs),
  4970.                   NULL_TREE);
  4971.       else
  4972.     result = build_tree_list (tree_cons (NULL_TREE, 
  4973.                          objc_object_reference, 
  4974.                          declspecs),
  4975.                   build1 (INDIRECT_REF, NULL_TREE, NULL_TREE));
  4976.     }
  4977.   
  4978.   /* Now, scan the declspecs of the resulting type, and
  4979.      issue warnings for incorrect usage.  */
  4980.   {
  4981.     int inout_seen = 0;
  4982.     int pointerp = 0;
  4983.  
  4984.     if (TREE_VALUE (result))
  4985.       pointerp = (TREE_CODE (TREE_VALUE (result)) == INDIRECT_REF);
  4986.  
  4987.     for (chain = declspecs;
  4988.      chain;
  4989.      chain = TREE_CHAIN (chain))
  4990.       {
  4991.     tree node = TREE_VALUE (chain);
  4992.     
  4993.     if (node == ridpointers [(int) RID_OUT]
  4994.         || node == ridpointers [(int) RID_INOUT]
  4995.         || node == ridpointers [(int) RID_IN])
  4996.       inout_seen++;
  4997.  
  4998.     if (!pointerp)
  4999.       {
  5000.         if (node == ridpointers [(int) RID_OUT]
  5001.         || node == ridpointers [(int) RID_INOUT])
  5002.           warning ("qualifiers \"out\" and \"inout\" are for pointers only");
  5003.       }
  5004.  
  5005.     else if (!is_return_type && node == ridpointers [(int) RID_ONEWAY])
  5006.       warning ("qualifier \"oneway\" is for return types only");
  5007.       }    
  5008.  
  5009.     if (inout_seen > 1)
  5010.       warning ("inconsistent combination of \"in\", \"out\", and \"inout\"");
  5011.  
  5012.     if (inout_seen && is_return_type)
  5013.       warning ("qualifiers \"in\", \"out\", and \"inout\" are for arguments only");
  5014.       
  5015.   }
  5016.  
  5017.   return result;
  5018.   
  5019. }
  5020.  
  5021. /*   usage:
  5022.           keyworddecl:
  5023.               selector ':' '(' typename ')' identifier
  5024.   
  5025.      purpose:
  5026.           transform an Objective-C keyword argument into
  5027.           the C equivalent parameter declarator.
  5028.   
  5029.      in:    key_name, an "identifier_node" (optional).
  5030.           arg_type, a  "tree_list" (optional).
  5031.           arg_name, an "identifier_node".
  5032.   
  5033.      Note:    It would be really nice to strongly type the preceding
  5034.           arguments in the function prototype; however, then I
  5035.           could not use the "accessor" macros defined in "tree.h".
  5036.   
  5037.      Out:    an instance of "keyword_decl".  */
  5038.  
  5039. tree
  5040. build_keyword_decl (key_name, arg_type, arg_name)
  5041.      tree key_name;
  5042.      tree arg_type;
  5043.      tree arg_name;
  5044. {
  5045.   tree keyword_decl;
  5046.  
  5047.   /* If no type is specified, default to "id". */
  5048.   arg_type = adjust_type_for_id_default (arg_type, 0);
  5049.  
  5050.   keyword_decl = make_node (KEYWORD_DECL);
  5051.  
  5052.   TREE_TYPE (keyword_decl) = arg_type;
  5053.   KEYWORD_ARG_NAME (keyword_decl) = arg_name;
  5054.   KEYWORD_KEY_NAME (keyword_decl) = key_name;
  5055.  
  5056.   return keyword_decl;
  5057. }
  5058.  
  5059. /* Given a chain of keyword_decl's, synthesize the full keyword selector.  */
  5060.  
  5061. static tree
  5062. build_keyword_selector (selector)
  5063.      tree selector;
  5064. {
  5065.   int len = 0;
  5066.   tree key_chain, key_name;
  5067.   char *buf;
  5068.  
  5069.   for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
  5070.     {
  5071.       if (TREE_CODE (selector) == KEYWORD_DECL)
  5072.     key_name = KEYWORD_KEY_NAME (key_chain);
  5073.       else if (TREE_CODE (selector) == TREE_LIST)
  5074.     key_name = TREE_PURPOSE (key_chain);
  5075.  
  5076.       if (key_name)
  5077.     len += IDENTIFIER_LENGTH (key_name) + 1;
  5078.       else
  5079.     /* Just a ':' arg. */
  5080.     len++;
  5081.     }
  5082.  
  5083.   buf = (char *)alloca (len + 1);
  5084.   bzero (buf, len + 1);
  5085.  
  5086.   for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
  5087.     {
  5088.       if (TREE_CODE (selector) == KEYWORD_DECL)
  5089.     key_name = KEYWORD_KEY_NAME (key_chain);
  5090.       else if (TREE_CODE (selector) == TREE_LIST)
  5091.     key_name = TREE_PURPOSE (key_chain);
  5092.  
  5093.       if (key_name)
  5094.     strcat (buf, IDENTIFIER_POINTER (key_name));
  5095.       strcat (buf, ":");
  5096.     }
  5097.  
  5098.   return get_identifier (buf);
  5099. }
  5100.  
  5101. static tree
  5102. adjust_for_return_type_mods (method_decl, ret_type_mods)
  5103.      tree method_decl;
  5104.      tree ret_type_mods;
  5105. {
  5106.   tree chain;
  5107.   int directp = 0;
  5108.   int staticp = 0;
  5109.   int externp = 0;
  5110.   int inlinep = 0;
  5111.  
  5112.   if (!ret_type_mods)
  5113.     return method_decl;
  5114.  
  5115.   /* Determine if a storage class specifier is present.  */
  5116.  
  5117.   for (chain = ret_type_mods;
  5118.        chain;
  5119.        chain = TREE_CHAIN (chain))
  5120.     {
  5121.       tree node = TREE_VALUE (chain);
  5122.  
  5123.       if (TREE_CODE (node) != IDENTIFIER_NODE)
  5124.     continue;
  5125.  
  5126.       if (node == ridpointers[(int) RID_STATIC])
  5127.     {
  5128.       if (staticp++ > 0)
  5129.         pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (node));
  5130.         }
  5131.       else if (node == ridpointers[(int) RID_INLINE])
  5132.     {
  5133.       if (inlinep++ > 0)
  5134.         pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (node));
  5135.         }
  5136. #ifdef NEXT_SEMANTICS
  5137.       else if (node == ridpointers[(int) RID_DIRECT])
  5138.     {
  5139.       if (directp++ > 0)
  5140.         pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (node));
  5141.         }
  5142. #endif
  5143.       else if (node == ridpointers[(int) RID_EXTERN])
  5144.     {
  5145.       if (externp++ > 0)
  5146.         pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (node));
  5147.         }
  5148.       else if (node == ridpointers[(int) RID_AUTO]
  5149.            || node == ridpointers[(int) RID_REGISTER]
  5150.            || node == ridpointers[(int) RID_TYPEDEF])
  5151.       error ("invalid storage class `%s' in Objective-C return type",
  5152.          IDENTIFIER_POINTER (node));
  5153. #ifdef OBJCPLUS
  5154.       else if (node == ridpointers[(int) RID_FRIEND]
  5155.            || node == ridpointers[(int) RID_MUTABLE]
  5156.            || node == ridpointers[(int) RID_VIRTUAL])
  5157.       error ("invalid storage class `%s' in Objective-C++ return type",
  5158.          IDENTIFIER_POINTER (node));
  5159. #endif
  5160.     }    
  5161.  
  5162.   if (externp && staticp)
  5163.     error ("multiple storage classes in declaration of `%s'",
  5164.        IDENTIFIER_POINTER (METHOD_SEL_NAME (method_decl)));
  5165.   if (!directp && externp)
  5166.     error ("`extern' is not allowed without `__direct__'");
  5167.   if (!directp && staticp)
  5168.     error ("`static' is not allowed without `__direct__'");
  5169.   if (!directp && inlinep)
  5170.     error ("`inline' is not allowed without `__direct__'");
  5171.   if (inlinep && ! staticp)
  5172.     error ("`inline' is not allowed without `static'");
  5173.  
  5174.   DIRECT_METHOD_FLAG (method_decl) = directp || staticp || externp || inlinep;
  5175.   STATIC_METHOD_FLAG (method_decl) = staticp;
  5176.   INLINE_METHOD_FLAG (method_decl) = inlinep;
  5177.  
  5178.   return method_decl;
  5179. }
  5180.  
  5181. /* Used for declarations and definitions */
  5182.  
  5183. tree
  5184. build_method_decl (code, ret_type, selector, add_args, ret_type_mods)
  5185.      enum tree_code code;
  5186.      tree ret_type;
  5187.      tree selector;
  5188.      tree add_args;
  5189.      tree ret_type_mods;
  5190. {
  5191.   tree method_decl;
  5192.  
  5193.   /* If no type is specified, default to "id" */
  5194.   ret_type = adjust_type_for_id_default (ret_type, 1);
  5195.  
  5196.   method_decl = make_node (code);
  5197.   TREE_TYPE (method_decl) = ret_type;
  5198.  
  5199.   /* If we have a keyword selector, create an identifier_node that
  5200.      represents the full selector name (`:' included)...  */
  5201.   if (TREE_CODE (selector) == KEYWORD_DECL)
  5202.     {
  5203.       METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
  5204.       METHOD_SEL_ARGS (method_decl) = selector;
  5205.       METHOD_ADD_ARGS (method_decl) = add_args;
  5206.     }
  5207.   else
  5208.     {
  5209.       METHOD_SEL_NAME (method_decl) = selector;
  5210.       METHOD_SEL_ARGS (method_decl) = NULL_TREE;
  5211.       METHOD_ADD_ARGS (method_decl) = NULL_TREE;
  5212.     }
  5213.  
  5214.   /* Handle any return type modifiers that were present. */
  5215.   method_decl = adjust_for_return_type_mods (method_decl, ret_type_mods);
  5216.  
  5217. #if 0
  5218.   {
  5219.     char buffer[512];
  5220.     sprintf(buffer, "`%s' <%s%s%s>",
  5221.         IDENTIFIER_POINTER(METHOD_SEL_NAME(method_decl)),
  5222.         STATIC_METHOD_FLAG(method_decl) ? "static" : "extern",
  5223.         DIRECT_METHOD_FLAG(method_decl) ? " direct" : "",
  5224.         INLINE_METHOD_FLAG(method_decl) ? " inline" : "");
  5225.     warning ("build_method_decl:%s\n", buffer);
  5226.   }
  5227. #endif
  5228.  
  5229.   return method_decl;
  5230. }
  5231.  
  5232. #define METHOD_DEF 0
  5233. #define METHOD_REF 1
  5234.  
  5235. /* Used by `build_message_expr' and `comp_method_types'.  Return an
  5236.    argument list for method METH.  CONTEXT is either METHOD_DEF or
  5237.    METHOD_REF, saying whether we are trying to define a method or call
  5238.    one.  SUPERFLAG says this is for a send to super; this makes a
  5239.    difference for the NeXT calling sequence in which the lookup and
  5240.    the method call are done together.  */
  5241.  
  5242. static tree
  5243. get_arg_type_list (meth, context, superflag)
  5244.      tree meth;
  5245.      int context;
  5246.      int superflag;
  5247. {
  5248.   tree arglist, akey;
  5249.  
  5250.   /* Receiver type. */
  5251.   if (flag_next_runtime && superflag)
  5252.     arglist = build_tree_list (NULL_TREE, super_type);
  5253.   else if (context == METHOD_DEF)
  5254.     arglist = build_tree_list (NULL_TREE, TREE_TYPE (self_decl));
  5255.   else
  5256.     arglist = build_tree_list (NULL_TREE, id_type);
  5257.  
  5258.   /* Selector type - will eventually change to `int'. */
  5259.   chainon (arglist, build_tree_list (NULL_TREE, selector_type));
  5260.  
  5261.   /* Build a list of argument types. */
  5262.   for (akey = METHOD_SEL_ARGS (meth); akey; akey = TREE_CHAIN (akey))
  5263.     {
  5264.       tree arg_decl = groktypename_in_parm_context (TREE_TYPE (akey));
  5265.       chainon (arglist, build_tree_list (NULL_TREE, TREE_TYPE (arg_decl)));
  5266.     }
  5267.  
  5268.   if (METHOD_ADD_ARGS (meth) == (tree)1)
  5269.     /* We have a `, ...' immediately following the selector,
  5270.        finalize the arglist...simulate get_parm_info (0).  */
  5271.     ;
  5272.   else if (METHOD_ADD_ARGS (meth))
  5273.     {
  5274.       /* we have a variable length selector */
  5275.       tree add_arg_list = TREE_CHAIN (METHOD_ADD_ARGS (meth));
  5276.       chainon (arglist, add_arg_list);
  5277.     }
  5278.   else
  5279.     {
  5280.     /* finalize the arglist...simulate get_parm_info (1) */
  5281. #ifdef OBJCPLUS
  5282.       chainon (arglist, void_list_node);
  5283. #else /* OBJCPLUS */
  5284.       chainon (arglist, build_tree_list (NULL_TREE, void_type_node));
  5285. #endif /* OBJCPLUS */
  5286.     }
  5287.  
  5288.   return arglist;
  5289. }
  5290.  
  5291. static tree
  5292. check_duplicates (hsh)
  5293.      hash hsh;
  5294. {
  5295.   tree meth = NULL_TREE;
  5296.  
  5297.   if (hsh)
  5298.     {
  5299.       meth = hsh->key;
  5300.  
  5301.       if (hsh->list)
  5302.         {
  5303.       /* We have two methods with the same name and different types. */
  5304.       attr loop;
  5305.       char type = (TREE_CODE (meth) == INSTANCE_METHOD_DECL) ? '-' : '+';
  5306.  
  5307.       warning ("multiple declarations for method `%s'",
  5308.            IDENTIFIER_POINTER (METHOD_SEL_NAME (meth)));
  5309.  
  5310.       warn_with_method ("using", type, meth);
  5311.       for (loop = hsh->list; loop; loop = loop->next)
  5312.         warn_with_method ("also found", type, loop->value);
  5313.         }
  5314.     }
  5315.   return meth;
  5316. }
  5317.  
  5318. /* If RECEIVER is a class reference, return the identifier node for the
  5319.    referenced class.  RECEIVER is created by get_class_reference, so we
  5320.    check the exact form created depending on which runtimes are used.  */
  5321.  
  5322. static tree
  5323. receiver_is_class_object (receiver)
  5324.       tree receiver;
  5325. {
  5326.   tree chain, exp, arg;
  5327.   if (flag_class_references)
  5328.     {
  5329.       /* The receiver is a variable created by build_class_reference_decl.  */
  5330.       if (TREE_CODE (receiver) == VAR_DECL
  5331.       && TREE_TYPE (receiver) == objc_class_type)
  5332.     /* Look up the identifier. */
  5333.     for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
  5334.       if (TREE_PURPOSE (chain) == receiver)
  5335.         return TREE_VALUE (chain);
  5336.     }
  5337.   else
  5338.     {
  5339.       /* The receiver is a function call that returns an id.  Check if
  5340.      it is a call to objc_getClass, if so, pick up the class name.  */
  5341.       if (TREE_CODE (receiver) == CALL_EXPR
  5342.       && (exp = TREE_OPERAND (receiver, 0))
  5343.       && TREE_CODE (exp) == ADDR_EXPR
  5344.       && (exp = TREE_OPERAND (exp, 0))
  5345.       && TREE_CODE (exp) == FUNCTION_DECL
  5346.       && exp == objc_get_class_decl
  5347.       /* we have a call to objc_getClass! */
  5348.       && (arg = TREE_OPERAND (receiver, 1))
  5349.       && TREE_CODE (arg) == TREE_LIST
  5350.       && (arg = TREE_VALUE (arg)))
  5351.     {
  5352.       STRIP_NOPS (arg);
  5353.       if (TREE_CODE (arg) == ADDR_EXPR
  5354.           && (arg = TREE_OPERAND (arg, 0))
  5355.           && TREE_CODE (arg) == STRING_CST)
  5356.         /* Finally, we have the class name.  */
  5357.         return get_identifier (TREE_STRING_POINTER (arg));
  5358.     }
  5359.     }
  5360.   return 0;
  5361. }
  5362.  
  5363. /* If we are currently building a message expr, this holds
  5364.    the identifier of the selector of the message.  This is
  5365.    used when printing warnings about argument mismatches. */
  5366.  
  5367. static tree building_objc_message_expr = 0;
  5368.  
  5369. tree
  5370. maybe_building_objc_message_expr ()
  5371. {
  5372.   return building_objc_message_expr;
  5373. }
  5374.  
  5375. /* Construct an expression for sending a message.
  5376.    MESS has the object to send to in TREE_PURPOSE
  5377.    and the argument list (including selector) in TREE_VALUE.
  5378.  
  5379.    (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
  5380.    (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...);  */
  5381.  
  5382. tree
  5383. build_message_expr (mess)
  5384.      tree mess;
  5385. {
  5386.   tree receiver = TREE_PURPOSE (mess);
  5387.   tree selector, self_object;
  5388.   tree rtype, sel_name;
  5389.   tree args = TREE_VALUE (mess);
  5390.   tree method_params = NULL_TREE;
  5391.   tree method_prototype = NULL_TREE;
  5392.   tree retval;
  5393.   int statically_typed = 0, statically_allocated = 0;
  5394.   tree class_ident = 0;
  5395.  
  5396.   /* 1 if this is sending to the superclass.  */
  5397.   int super;
  5398.  
  5399.   if (!doing_objc_thang)
  5400.     objc_fatal ();
  5401.  
  5402.   if (TREE_CODE (receiver) == ERROR_MARK)
  5403.     return error_mark_node;
  5404.  
  5405.   /* Determine receiver type. */
  5406.   rtype = TREE_TYPE (receiver);
  5407.   super = IS_SUPER (rtype);
  5408.  
  5409.   if (! super)
  5410.     {
  5411.       if (TREE_STATIC_TEMPLATE (rtype))
  5412.     statically_allocated = 1;
  5413.       else if (TREE_CODE (rtype) == POINTER_TYPE
  5414.            && TREE_STATIC_TEMPLATE (TREE_TYPE (rtype)))
  5415.     statically_typed = 1;
  5416.       else if ((flag_next_runtime
  5417.         || (TREE_CODE (receiver) == CALL_EXPR && IS_ID (rtype)))
  5418.            && (class_ident = receiver_is_class_object (receiver)))
  5419.     ;
  5420.       else if (! IS_ID (rtype)
  5421.            /* Allow any type that matches objc_class_type.  */
  5422.            && ! comptypes (rtype, objc_class_type))
  5423.     {
  5424. #ifdef OBJCPLUS
  5425.       tree converted;
  5426.  
  5427.       converted = convert (id_type, receiver);
  5428.  
  5429.       if (converted != NULL_TREE && converted != error_mark_node)
  5430.         {
  5431.           STRIP_NOPS (converted);
  5432.           receiver = converted;
  5433.           rtype = TREE_TYPE (receiver);
  5434.           if (TREE_CODE (rtype) == POINTER_TYPE
  5435.           && TREE_STATIC_TEMPLATE (TREE_TYPE (rtype)))
  5436.         statically_typed = 1;        
  5437.         }
  5438.       else
  5439. #endif
  5440.         {
  5441.           bzero (errbuf, BUFSIZE);
  5442.           warning ("invalid receiver type `%s'",
  5443.                gen_declaration (rtype, errbuf));
  5444.         }
  5445.     }
  5446.       if (statically_allocated)
  5447.     receiver = build_unary_op (ADDR_EXPR, receiver, 0);
  5448.  
  5449.       /* Don't evaluate the receiver twice. */
  5450.       receiver = save_expr (receiver);
  5451.       self_object = receiver;
  5452.     }
  5453.   else
  5454.     /* If sending to `super', use current self as the object.  */
  5455.     self_object = self_decl;
  5456.  
  5457.   /* Obtain the full selector name.  */
  5458.  
  5459.   if (TREE_CODE (args) == IDENTIFIER_NODE)
  5460.     /* A unary selector. */
  5461.     sel_name = args;
  5462.   else if (TREE_CODE (args) == TREE_LIST)
  5463.     {
  5464.       sel_name = build_keyword_selector (args);
  5465.     }
  5466.   else
  5467.     {
  5468.       /* internal parser error */
  5469.       fprintf (stderr, "Internal objc parser error, please report to bug-gcc\n");
  5470.       fflush (stderr);
  5471.       abort ();
  5472.     }
  5473.  
  5474.   /* Build the parameter list to give to the method.  */
  5475.  
  5476.   method_params = NULL_TREE;
  5477.   if (TREE_CODE (args) == TREE_LIST)
  5478.     {
  5479.       tree chain = args, prev = NULL_TREE;
  5480.  
  5481.       /* We have a keyword selector--check for comma expressions.  */
  5482.       while (chain)
  5483.     {
  5484.       tree element = TREE_VALUE (chain);
  5485.  
  5486.       /* We have a comma expression, must collapse...  */
  5487.       if (TREE_CODE (element) == TREE_LIST)
  5488.         {
  5489.           if (prev)
  5490.         TREE_CHAIN (prev) = element;
  5491.           else
  5492.         args = element;
  5493.         }
  5494.       prev = chain;
  5495.       chain = TREE_CHAIN (chain);
  5496.         }
  5497.       method_params = args;
  5498.     }
  5499.  
  5500.   /* Determine operation return type.  */
  5501.  
  5502.   if (IS_SUPER (rtype))
  5503.     {
  5504.       tree iface;
  5505.  
  5506.       if (CLASS_SUPER_NAME (implementation_template))
  5507.     {
  5508.       iface
  5509.         = lookup_interface (CLASS_SUPER_NAME (implementation_template));
  5510.  
  5511.       if (TREE_CODE (method_context) == INSTANCE_METHOD_DECL)
  5512.         method_prototype = lookup_instance_method_static (iface, sel_name);
  5513.       else
  5514.         method_prototype = lookup_class_method_static (iface, sel_name);
  5515.  
  5516.       if (iface && !method_prototype)
  5517.         warning ("`%s' does not respond to `%s'",
  5518.              IDENTIFIER_POINTER (CLASS_SUPER_NAME (implementation_template)),
  5519.              IDENTIFIER_POINTER (sel_name));
  5520.     }
  5521.       else
  5522.     {
  5523.       error ("no super class declared in interface for `%s'",
  5524.          IDENTIFIER_POINTER (CLASS_NAME (implementation_template)));
  5525.       return error_mark_node;
  5526.     }
  5527.  
  5528.     }
  5529.   else if (statically_allocated)
  5530.     {
  5531.       tree ctype = TREE_TYPE (rtype);
  5532.       tree iface = lookup_interface (TYPE_NAME (rtype));
  5533.  
  5534.       if (iface)
  5535.     method_prototype = lookup_instance_method_static (iface, sel_name);
  5536.  
  5537.       if (! method_prototype && TYPE_PROTOCOL_LIST (ctype))
  5538.     method_prototype
  5539.       = lookup_method_in_protocol_list (TYPE_PROTOCOL_LIST (ctype),
  5540.                         sel_name, 0);
  5541.  
  5542.       if (!method_prototype)
  5543.     warning ("`%s' does not respond to `%s'",
  5544.          IDENTIFIER_POINTER (TYPE_NAME (rtype)),
  5545.          IDENTIFIER_POINTER (sel_name));
  5546.     }
  5547.   else if (statically_typed)
  5548.     {
  5549.       tree ctype = TREE_TYPE (rtype);
  5550.  
  5551.       /* `self' is now statically_typed.  All methods should be visible
  5552.          within the context of the implementation.  */
  5553.       if (implementation_context
  5554.       && CLASS_NAME (implementation_context) == TYPE_NAME (ctype))
  5555.     {
  5556.       method_prototype
  5557.         = lookup_instance_method_static (implementation_template,
  5558.                          sel_name);
  5559.  
  5560.       if (! method_prototype && TYPE_PROTOCOL_LIST (ctype))
  5561.         method_prototype
  5562.           = lookup_method_in_protocol_list (TYPE_PROTOCOL_LIST (ctype),
  5563.                         sel_name, 0);
  5564.  
  5565.       if (! method_prototype
  5566.           && implementation_template != implementation_context)
  5567.         /* The method is not published in the interface.  Check locally. */
  5568.         method_prototype
  5569.           = lookup_method (CLASS_NST_METHODS (implementation_context),
  5570.                    sel_name);
  5571.     }
  5572.       else
  5573.     {
  5574.       tree iface;
  5575.  
  5576.       if ((iface = lookup_interface (TYPE_NAME (ctype))))
  5577.         method_prototype = lookup_instance_method_static (iface, sel_name);
  5578.  
  5579.           if (! method_prototype)
  5580.         {
  5581.           tree protocol_list = TYPE_PROTOCOL_LIST (ctype);
  5582.           if (protocol_list)
  5583.         method_prototype
  5584.           = lookup_method_in_protocol_list (protocol_list,
  5585.                             sel_name, 0);
  5586.         }
  5587.     }
  5588.  
  5589.       if (!method_prototype)
  5590.         warning ("`%s' does not respond to `%s'",
  5591.          IDENTIFIER_POINTER (TYPE_NAME (ctype)),
  5592.          IDENTIFIER_POINTER (sel_name));
  5593.     }
  5594.   else if (class_ident)
  5595.     {
  5596.       if (implementation_context
  5597.       && CLASS_NAME (implementation_context) == class_ident)
  5598.     {
  5599.       method_prototype
  5600.         = lookup_class_method_static (implementation_template, sel_name);
  5601.  
  5602.       if (!method_prototype
  5603.           && implementation_template != implementation_context)
  5604.         /* The method is not published in the interface. Check locally. */
  5605.         method_prototype
  5606.           = lookup_method (CLASS_CLS_METHODS (implementation_context),
  5607.                    sel_name);
  5608.     }
  5609.       else
  5610.     {
  5611.       tree iface;
  5612.  
  5613.       if ((iface = lookup_interface (class_ident)))
  5614.         method_prototype = lookup_class_method_static (iface, sel_name);
  5615.     }
  5616.  
  5617.       if (!method_prototype)
  5618.     {
  5619.       warning ("cannot find class (factory) method.");
  5620.       warning ("return type for `%s' defaults to id",
  5621.            IDENTIFIER_POINTER (sel_name));
  5622.     }
  5623.     }
  5624.   else if (IS_PROTOCOL_QUALIFIED_ID (rtype))
  5625.     {
  5626.       /* An anonymous object that has been qualified with a protocol.  */
  5627.  
  5628.       tree protocol_list = TYPE_PROTOCOL_LIST (rtype);
  5629.  
  5630.       method_prototype = lookup_method_in_protocol_list (protocol_list,
  5631.                              sel_name, 0);
  5632.  
  5633.       if (!method_prototype)
  5634.     {
  5635.           hash hsh;
  5636.  
  5637.       warning ("method `%s' not implemented by protocol(s).",
  5638.            IDENTIFIER_POINTER (sel_name));
  5639.  
  5640.           /* try and find the method signiture in the global pools! */
  5641.  
  5642.           if (!(hsh = hash_lookup (nst_method_hash_list, sel_name)))
  5643.         hsh = hash_lookup (cls_method_hash_list, sel_name);
  5644.  
  5645.           if (!(method_prototype = check_duplicates (hsh)))
  5646.         warning ("return type defaults to id");
  5647.     }
  5648.     }
  5649.   else
  5650.     {
  5651.       hash hsh;
  5652.  
  5653.       /* We think we have an instance...loophole: extern id Object; */
  5654.       hsh = hash_lookup (nst_method_hash_list, sel_name);
  5655.       if (!hsh)
  5656.     /* For various loopholes, like sending messages to self in a
  5657.        factory context. */
  5658.     hsh = hash_lookup (cls_method_hash_list, sel_name);
  5659.  
  5660.       method_prototype = check_duplicates (hsh);
  5661.       if (!method_prototype)
  5662.     {
  5663.       warning ("cannot find method.");
  5664.       warning ("return type for `%s' defaults to id",
  5665.            IDENTIFIER_POINTER (sel_name));
  5666.     }
  5667.     }
  5668.  
  5669.   /* Save the selector name for printing error messages.  */
  5670.   building_objc_message_expr = sel_name;
  5671.  
  5672.   selector = build_selector_reference (sel_name);
  5673.  
  5674.   retval = build_objc_method_call (super, method_prototype,
  5675.                    receiver, self_object,
  5676.                    selector, method_params);
  5677.  
  5678.   building_objc_message_expr = 0;
  5679.  
  5680.   return retval;
  5681. }
  5682.  
  5683. static int
  5684. is_protocol_contained_in_protocols PROTO((tree, tree));
  5685.  
  5686. is_protocol_contained_in_protocol (protocol1, protocol2)
  5687. tree protocol1;
  5688. tree protocol2;
  5689. {
  5690.     if (protocol1 == protocol2)
  5691.        return 1;
  5692.     return is_protocol_contained_in_protocols(protocol1, PROTOCOL_LIST(protocol2));
  5693. }
  5694.     
  5695. static int
  5696. is_protocol_contained_in_protocols (protocol, protocols)
  5697. tree protocol;
  5698. tree protocols;
  5699. {
  5700.     while (protocols) {
  5701.        if (is_protocol_contained_in_protocol(protocol, TREE_VALUE(protocols))) return 1;
  5702.        protocols = TREE_CHAIN (protocols);
  5703.     }
  5704.     return 0;
  5705. }
  5706.  
  5707. static int
  5708. check_protocol (p, type, name)
  5709.      tree p;
  5710.      char *type;
  5711.      char *name;
  5712. {
  5713.     if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
  5714.     {
  5715.     int f1, f2;
  5716.     
  5717.     /* Ensure that all protocols have bodies! */
  5718.     if (flag_warn_protocol) {
  5719.       f1 = check_methods (PROTOCOL_CLS_METHODS (p),
  5720.                   CLASS_CLS_METHODS (implementation_context),
  5721.                   '+');
  5722.       f2 = check_methods (PROTOCOL_NST_METHODS (p),
  5723.                   CLASS_NST_METHODS (implementation_context),
  5724.                   '-');
  5725.     } else {
  5726.       f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
  5727.                      implementation_context,
  5728.                      '+');
  5729.       f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
  5730.                      implementation_context,
  5731.                      '-');
  5732.     }
  5733.  
  5734.     if (!f1 || !f2)
  5735.     warning ("%s `%s' does not fully implement the `%s' protocol",
  5736.             type, name, IDENTIFIER_POINTER (PROTOCOL_NAME (p)));
  5737.  
  5738.     }
  5739.     else 
  5740.     ; /* an identifier...if we could not find a protocol.  */
  5741.     
  5742.     /* Check protocols recursively. */
  5743.     if (PROTOCOL_LIST (p))
  5744.     {
  5745.     tree subs = PROTOCOL_LIST(p);
  5746.     tree super_class = lookup_interface (CLASS_SUPER_NAME (implementation_template));
  5747.     while (subs) {
  5748.         tree sub = TREE_VALUE(subs);
  5749.         if (!conforms_to_protocol (super_class, sub))
  5750.         check_protocol (sub, type, name);
  5751.         subs = TREE_CHAIN(subs);
  5752.     }
  5753.     }
  5754. }
  5755.     
  5756.  
  5757. /* Build a tree expression to send OBJECT the operation SELECTOR,
  5758.    looking up the method on object LOOKUP_OBJECT (often same as OBJECT),
  5759.    assuming the method has prototype METHOD_PROTOTYPE.
  5760.    (That is an INSTANCE_METHOD_DECL or CLASS_METHOD_DECL.)
  5761.    Use METHOD_PARAMS as list of args to pass to the method.
  5762.    If SUPER_FLAG is nonzero, we look up the superclass's method.  */
  5763.  
  5764. static tree
  5765. build_objc_method_call (super_flag, method_prototype, lookup_object, object,
  5766.             selector, method_params)
  5767.      int super_flag;
  5768.      tree method_prototype, lookup_object, object, selector, method_params;
  5769. {
  5770.   tree sender = (super_flag ? umsg_super_decl : umsg_decl);
  5771.   tree rcv_p = (super_flag
  5772.         ? build_pointer_type (xref_tag (RECORD_TYPE,
  5773.                         get_identifier (TAG_SUPER)))
  5774.         : id_type);
  5775.  
  5776.   if (flag_next_runtime)
  5777.     {
  5778.       if (! method_prototype)
  5779.     {
  5780.       method_params = tree_cons (NULL_TREE, lookup_object,
  5781.                      tree_cons (NULL_TREE, selector,
  5782.                         method_params));
  5783.       assemble_external (sender);
  5784.       return build_function_call (sender, method_params);
  5785.     }
  5786.       else
  5787.     {
  5788.       /* This is a real kludge, but it is used only for the Next.
  5789.          Clobber the data type of SENDER temporarily to accept
  5790.          all the arguments for this operation, and to return
  5791.          whatever this operation returns.  */
  5792.       tree arglist = NULL_TREE;
  5793.       tree retval;
  5794.  
  5795.       /* Save the proper contents of SENDER's data type.  */
  5796.       tree savarg = TYPE_ARG_TYPES (TREE_TYPE (sender));
  5797.       tree savret = TREE_TYPE (TREE_TYPE (sender));
  5798.  
  5799.       /* Install this method's argument types.  */
  5800.       arglist = get_arg_type_list (method_prototype, METHOD_REF,
  5801.                        super_flag);
  5802.       TYPE_ARG_TYPES (TREE_TYPE (sender)) = arglist;
  5803.  
  5804.       /* Install this method's return type.  */
  5805.       TREE_TYPE (TREE_TYPE (sender))
  5806.         = groktypename (TREE_TYPE (method_prototype));
  5807.  
  5808.       /* Call SENDER with all the parameters.  This will do type
  5809.          checking using the arg types for this method.  */
  5810.       method_params = tree_cons (NULL_TREE, lookup_object,
  5811.                      tree_cons (NULL_TREE, selector,
  5812.                         method_params));
  5813.       assemble_external (sender);
  5814.       retval = build_function_call (sender, method_params);
  5815.  
  5816.       /* Restore SENDER's return/argument types.  */
  5817.       TYPE_ARG_TYPES (TREE_TYPE (sender)) = savarg;
  5818.       TREE_TYPE (TREE_TYPE (sender)) = savret;
  5819.       return retval;
  5820.     }
  5821.     }
  5822.   else
  5823.     {
  5824.       /* This is the portable way.
  5825.      First call the lookup function to get a pointer to the method,
  5826.      then cast the pointer, then call it with the method arguments.  */
  5827.       tree method;
  5828.  
  5829.       /* Avoid trouble since we may evaluate each of these twice.  */
  5830.       object = save_expr (object);
  5831.       selector = save_expr (selector);
  5832.  
  5833.       lookup_object = build_c_cast (rcv_p, lookup_object);
  5834.  
  5835.       assemble_external (sender);
  5836.       method
  5837.     = build_function_call (sender,
  5838.                    tree_cons (NULL_TREE, lookup_object,
  5839.                       tree_cons (NULL_TREE, selector,
  5840.                              NULL_TREE)));
  5841.  
  5842.       /* If we have a method prototype, construct the data type this
  5843.      method needs, and cast what we got from SENDER into a pointer
  5844.      to that type.  */
  5845.       if (method_prototype)
  5846.     {
  5847.       tree arglist = get_arg_type_list (method_prototype, METHOD_REF,
  5848.                         super_flag);
  5849.       tree valtype = groktypename (TREE_TYPE (method_prototype));
  5850.       tree fake_function_type = build_function_type (valtype, arglist);
  5851.       TREE_TYPE (method) = build_pointer_type (fake_function_type);
  5852.     }
  5853.       else
  5854.     TREE_TYPE (method)
  5855.       = build_pointer_type (build_function_type (ptr_type_node, NULL_TREE));
  5856.  
  5857.       /* Pass the object to the method.  */
  5858.       assemble_external (method);
  5859.       return build_function_call (method,
  5860.                   tree_cons (NULL_TREE, object,
  5861.                          tree_cons (NULL_TREE, selector,
  5862.                             method_params)));
  5863.     }
  5864. }
  5865.  
  5866. static void
  5867. build_protocol_reference (p)
  5868.      tree p;
  5869. {
  5870.   tree decl, ident, ptype, constructor;
  5871.   struct obstack *save_current_obstack = current_obstack;
  5872.   struct obstack *save_rtl_obstack = rtl_obstack;
  5873.  
  5874.   rtl_obstack = current_obstack = &permanent_obstack;
  5875.  
  5876.   /* extern struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
  5877.  
  5878.   ident = synth_id_with_class_suffix ("_OBJC_PROTOCOL", p);
  5879.   ptype
  5880.     = groktypename (build_tree_list (build_tree_list (NULL_TREE,
  5881.                               objc_protocol_template),
  5882.                      NULL_TREE));
  5883.  
  5884.   if (IDENTIFIER_GLOBAL_VALUE (ident))
  5885.     decl = IDENTIFIER_GLOBAL_VALUE (ident); /* Set by pushdecl.  */
  5886.   else
  5887.     {
  5888.       decl = build_decl (VAR_DECL, ident, ptype);
  5889.       DECL_EXTERNAL (decl) = 1;
  5890.       TREE_PUBLIC (decl) = 0;
  5891.       TREE_USED (decl) = 1;
  5892.       DECL_ARTIFICIAL (decl) = 1;
  5893.  
  5894.       /* usually called from `rest_of_decl_compilation' */
  5895.       make_decl_rtl (decl, 0, 1);
  5896.       /* our `extended/custom' pushdecl in c-decl.c */
  5897.       pushdecl_top_level (decl);
  5898.    }
  5899.   current_obstack = save_current_obstack;
  5900.   rtl_obstack = save_rtl_obstack;
  5901.  
  5902.   PROTOCOL_FORWARD_DECL (p) = decl;
  5903. }
  5904.  
  5905. tree
  5906. build_protocol_expr (protoname)
  5907.      tree protoname;
  5908. {
  5909.   tree expr;
  5910.   tree p;
  5911.  
  5912.   if (!doing_objc_thang)
  5913.     objc_fatal ();
  5914.  
  5915.   p = lookup_protocol (protoname);
  5916.  
  5917.   if (!p)
  5918.     {
  5919.       error ("Cannot find protocol declaration for `%s'",
  5920.          IDENTIFIER_POINTER (protoname));
  5921.       return error_mark_node;
  5922.     }
  5923.  
  5924.   if (!PROTOCOL_FORWARD_DECL (p))
  5925.     build_protocol_reference (p);
  5926.  
  5927.   expr = build_unary_op (ADDR_EXPR, PROTOCOL_FORWARD_DECL (p), 0);
  5928.  
  5929.   TREE_TYPE (expr) = protocol_type;
  5930.  
  5931. #ifdef NEXT_PDO
  5932.   add_static_object (PROTOCOL_FORWARD_DECL (p),protocol_id);
  5933. #endif /*  NEXT_PDO */
  5934.  
  5935.   return expr;
  5936. }
  5937.  
  5938. tree
  5939. build_selector_expr (selnamelist)
  5940.      tree selnamelist;
  5941. {
  5942.   tree selname;
  5943.  
  5944.   if (!doing_objc_thang)
  5945.     objc_fatal ();
  5946.  
  5947.   /* Obtain the full selector name. */
  5948.   if (TREE_CODE (selnamelist) == IDENTIFIER_NODE)
  5949.     /* A unary selector. */
  5950.     selname = selnamelist;
  5951.   else if (TREE_CODE (selnamelist) == TREE_LIST)
  5952.     selname = build_keyword_selector (selnamelist);
  5953.  
  5954.   return build_selector_reference (selname);
  5955. }
  5956.  
  5957. tree
  5958. build_encode_expr (type)
  5959.      tree type;
  5960. {
  5961.   tree result;
  5962.   char *string;
  5963.  
  5964.   if (!doing_objc_thang)
  5965.     objc_fatal ();
  5966.  
  5967.   encode_type (type, obstack_object_size (&util_obstack),
  5968.            OBJC_ENCODE_INLINE_DEFS);
  5969.   obstack_1grow (&util_obstack, 0);    /* null terminate string */
  5970.   string = obstack_finish (&util_obstack);
  5971.  
  5972.   /* Synthesize a string that represents the encoded struct/union. */
  5973.   result = my_build_string (strlen (string) + 1, string);
  5974.   obstack_free (&util_obstack, util_firstobj);
  5975.   return result;
  5976. }
  5977.  
  5978. tree
  5979. build_ivar_reference (id)
  5980.      tree id;
  5981. {
  5982.   if (TREE_CODE (method_context) == CLASS_METHOD_DECL)
  5983.     {
  5984.       /* Historically, a class method that produced objects (factory
  5985.      method) would assign `self' to the instance that it
  5986.      allocated.  This would effectively turn the class method into
  5987.      an instance method.  Following this assignment, the instance
  5988.      variables could be accessed.  That practice, while safe,
  5989.      violates the simple rule that a class method should not refer
  5990.      to an instance variable.  It's better to catch the cases
  5991.      where this is done unknowingly than to support the above
  5992.      paradigm.  */
  5993.       warning ("instance variable `%s' accessed in class method",
  5994.            IDENTIFIER_POINTER (id));
  5995.       TREE_TYPE (self_decl) = instance_type; /* cast */
  5996.     }
  5997.  
  5998.   return build_component_ref (build_indirect_ref (self_decl, "->"), id);
  5999. }
  6000.  
  6001. #define HASH_ALLOC_LIST_SIZE    170
  6002. #define ATTR_ALLOC_LIST_SIZE    170
  6003. #define SIZEHASHTABLE         257
  6004.  
  6005. /* make positive */
  6006. #define HASHFUNCTION(key)    ((HOST_WIDE_INT) key & 0x7fffffff)
  6007.  
  6008. static void
  6009. hash_init ()
  6010. {
  6011.   nst_method_hash_list = (hash *)xmalloc (SIZEHASHTABLE * sizeof (hash));
  6012.   cls_method_hash_list = (hash *)xmalloc (SIZEHASHTABLE * sizeof (hash));
  6013.  
  6014.   if (!nst_method_hash_list || !cls_method_hash_list)
  6015.     perror ("unable to allocate space in objc-act.c");
  6016.   else
  6017.     {
  6018.       int i;
  6019.  
  6020.       for (i = 0; i < SIZEHASHTABLE; i++)
  6021.     {
  6022.       nst_method_hash_list[i] = 0;
  6023.       cls_method_hash_list[i] = 0;
  6024.     }
  6025.     }
  6026. }
  6027.  
  6028. static void
  6029. hash_enter (hashlist, method)
  6030.      hash *hashlist;
  6031.      tree method;
  6032. {
  6033.   static hash     hash_alloc_list = 0;
  6034.   static int    hash_alloc_index = 0;
  6035.   hash obj;
  6036.   int slot = HASHFUNCTION (METHOD_SEL_NAME (method)) % SIZEHASHTABLE;
  6037.  
  6038.   if (! hash_alloc_list || hash_alloc_index >= HASH_ALLOC_LIST_SIZE)
  6039.     {
  6040.       hash_alloc_index = 0;
  6041.       hash_alloc_list = (hash) xmalloc (sizeof (struct hashed_entry)
  6042.                     * HASH_ALLOC_LIST_SIZE);
  6043.       if (! hash_alloc_list)
  6044.     perror ("unable to allocate in objc-act.c");
  6045.     }
  6046.   obj = &hash_alloc_list[hash_alloc_index++];
  6047.   obj->list = 0;
  6048.   obj->next = hashlist[slot];
  6049.   obj->key = method;
  6050.  
  6051.   hashlist[slot] = obj;        /* append to front */
  6052. }
  6053.  
  6054. static hash
  6055. hash_lookup (hashlist, sel_name)
  6056.      hash *hashlist;
  6057.      tree sel_name;
  6058. {
  6059.   hash target;
  6060.  
  6061.   target = hashlist[HASHFUNCTION (sel_name) % SIZEHASHTABLE];
  6062.  
  6063.   while (target)
  6064.     {
  6065.       if (sel_name == METHOD_SEL_NAME (target->key))
  6066.     return target;
  6067.  
  6068.       target = target->next;
  6069.     }
  6070.   return 0;
  6071. }
  6072.  
  6073. static void
  6074. hash_add_attr (entry, value)
  6075.      hash entry;
  6076.      tree value;
  6077. {
  6078.   static attr     attr_alloc_list = 0;
  6079.   static int    attr_alloc_index = 0;
  6080.   attr obj;
  6081.  
  6082.   if (! attr_alloc_list || attr_alloc_index >= ATTR_ALLOC_LIST_SIZE)
  6083.     {
  6084.       attr_alloc_index = 0;
  6085.       attr_alloc_list = (attr) xmalloc (sizeof (struct hashed_attribute)
  6086.                     * ATTR_ALLOC_LIST_SIZE);
  6087.       if (! attr_alloc_list)
  6088.     perror ("unable to allocate in objc-act.c");
  6089.     }
  6090.   obj = &attr_alloc_list[attr_alloc_index++];
  6091.   obj->next = entry->list;
  6092.   obj->value = value;
  6093.  
  6094.   entry->list = obj;        /* append to front */
  6095. }
  6096.  
  6097. static tree
  6098. lookup_method (mchain, method)
  6099.      tree mchain;
  6100.      tree method;
  6101. {
  6102.   tree key;
  6103.  
  6104.   if (TREE_CODE (method) == IDENTIFIER_NODE)
  6105.     key = method;
  6106.   else
  6107.     key = METHOD_SEL_NAME (method);
  6108.  
  6109.   while (mchain)
  6110.     {
  6111.       if (METHOD_SEL_NAME (mchain) == key)
  6112.     return mchain;
  6113.       mchain = TREE_CHAIN (mchain);
  6114.     }
  6115.   return NULL_TREE;
  6116. }
  6117.  
  6118. static tree
  6119. lookup_instance_method_static (interface, ident)
  6120.      tree interface;
  6121.      tree ident;
  6122. {
  6123.   tree inter = interface;
  6124.   tree chain = CLASS_NST_METHODS (inter);
  6125.   tree meth = NULL_TREE;
  6126.  
  6127.   do
  6128.     {
  6129.       if ((meth = lookup_method (chain, ident)))
  6130.     return meth;
  6131.  
  6132.       if (CLASS_CATEGORY_LIST (inter))
  6133.     {
  6134.       tree category = CLASS_CATEGORY_LIST (inter);
  6135.       chain = CLASS_NST_METHODS (category);
  6136.  
  6137.       do
  6138.         {
  6139.           if ((meth = lookup_method (chain, ident)))
  6140.         return meth;
  6141.  
  6142.           /* Check for instance methods in protocols in categories.  */
  6143.           if (CLASS_PROTOCOL_LIST (category))
  6144.         {
  6145.           if ((meth = (lookup_method_in_protocol_list
  6146.                    (CLASS_PROTOCOL_LIST (category), ident, 0))))
  6147.             return meth;
  6148.         }
  6149.  
  6150.           if ((category = CLASS_CATEGORY_LIST (category)))
  6151.         chain = CLASS_NST_METHODS (category);
  6152.         }
  6153.       while (category);
  6154.     }
  6155.  
  6156.       if (CLASS_PROTOCOL_LIST (inter))
  6157.     {
  6158.       if ((meth = (lookup_method_in_protocol_list
  6159.                (CLASS_PROTOCOL_LIST (inter), ident, 0))))
  6160.         return meth;
  6161.     }
  6162.  
  6163.       if ((inter = lookup_interface (CLASS_SUPER_NAME (inter))))
  6164.     chain = CLASS_NST_METHODS (inter);
  6165.     }
  6166.   while (inter);
  6167.  
  6168.   return meth;
  6169. }
  6170.  
  6171. static tree
  6172. lookup_class_method_static (interface, ident)
  6173.      tree interface;
  6174.      tree ident;
  6175. {
  6176.   tree inter = interface;
  6177.   tree chain = CLASS_CLS_METHODS (inter);
  6178.   tree meth = NULL_TREE;
  6179.   tree root_inter = NULL_TREE;
  6180.  
  6181.   do
  6182.     {
  6183.       if ((meth = lookup_method (chain, ident)))
  6184.     return meth;
  6185.  
  6186.       if (CLASS_CATEGORY_LIST (inter))
  6187.     {
  6188.       tree category = CLASS_CATEGORY_LIST (inter);
  6189.       chain = CLASS_CLS_METHODS (category);
  6190.  
  6191.       do
  6192.         {
  6193.           if ((meth = lookup_method (chain, ident)))
  6194.         return meth;
  6195.  
  6196.           /* Check for class methods in protocols in categories.  */
  6197.           if (CLASS_PROTOCOL_LIST (category))
  6198.         {
  6199.           if ((meth = (lookup_method_in_protocol_list
  6200.                    (CLASS_PROTOCOL_LIST (category), ident, 1))))
  6201.             return meth;
  6202.         }
  6203.  
  6204.           if ((category = CLASS_CATEGORY_LIST (category)))
  6205.         chain = CLASS_CLS_METHODS (category);
  6206.         }
  6207.       while (category);
  6208.     }
  6209.  
  6210.       /* Check for class methods in protocols.  */
  6211.       if (CLASS_PROTOCOL_LIST (inter))
  6212.     {
  6213.       if ((meth = (lookup_method_in_protocol_list
  6214.                (CLASS_PROTOCOL_LIST (inter), ident, 1))))
  6215.         return meth;
  6216.     }
  6217.  
  6218.       root_inter = inter;
  6219.       if ((inter = lookup_interface (CLASS_SUPER_NAME (inter))))
  6220.     chain = CLASS_CLS_METHODS (inter);
  6221.     }
  6222.   while (inter);
  6223.  
  6224.   /* Simulate wrap around.  */
  6225.   return lookup_instance_method_static (root_inter, ident);
  6226. }
  6227.  
  6228. tree
  6229. add_class_method (class, method)
  6230.      tree class;
  6231.      tree method;
  6232. {
  6233.   tree mth;
  6234.   hash hsh;
  6235.  
  6236.   /* We will have allocated the method parameter declarations on the
  6237.      maybepermanent_obstack.  Need to make sure they stick around!  */
  6238.   preserve_data ();
  6239.  
  6240.   if (!(mth = lookup_method (CLASS_CLS_METHODS (class), method)))
  6241.     {
  6242.       /* put method on list in reverse order */
  6243.       TREE_CHAIN (method) = CLASS_CLS_METHODS (class);
  6244.       CLASS_CLS_METHODS (class) = method;
  6245.     }
  6246.   else
  6247.     {
  6248.       if (TREE_CODE (class) == CLASS_IMPLEMENTATION_TYPE)
  6249.     error ("duplicate definition of class method `%s'.",
  6250.            IDENTIFIER_POINTER (METHOD_SEL_NAME (mth)));
  6251.       else
  6252.         {
  6253.       /* Check types; if different, complain. */
  6254.       if (!comp_proto_with_proto (method, mth))
  6255.         error ("duplicate declaration of class method `%s'.",
  6256.            IDENTIFIER_POINTER (METHOD_SEL_NAME (mth)));
  6257.         }
  6258.     }
  6259.  
  6260.   if (!(hsh = hash_lookup (cls_method_hash_list, METHOD_SEL_NAME (method))))
  6261.     {
  6262.       /* Install on a global chain. */
  6263.       hash_enter (cls_method_hash_list, method);
  6264.     }
  6265.   else
  6266.     {
  6267.       /* Check types; if different, add to a list. */
  6268.       if (!comp_proto_with_proto (method, hsh->key))
  6269.         hash_add_attr (hsh, method);
  6270.     }
  6271.   return method;
  6272. }
  6273.  
  6274. tree
  6275. add_instance_method (class, method)
  6276.      tree class;
  6277.      tree method;
  6278. {
  6279.   tree mth;
  6280.   hash hsh;
  6281.  
  6282.   /* We will have allocated the method parameter declarations on the
  6283.      maybepermanent_obstack.  Need to make sure they stick around!  */
  6284.   preserve_data ();
  6285.  
  6286.   if (!(mth = lookup_method (CLASS_NST_METHODS (class), method)))
  6287.     {
  6288.       /* put method on list in reverse order */
  6289.       TREE_CHAIN (method) = CLASS_NST_METHODS (class);
  6290.       CLASS_NST_METHODS (class) = method;
  6291.     }
  6292.   else
  6293.     {
  6294.       if (TREE_CODE (class) == CLASS_IMPLEMENTATION_TYPE)
  6295.     error ("duplicate definition of instance method `%s'.",
  6296.            IDENTIFIER_POINTER (METHOD_SEL_NAME (mth)));
  6297.       else
  6298.         {
  6299.       /* Check types; if different, complain. */
  6300.       if (!comp_proto_with_proto (method, mth))
  6301.         error ("duplicate declaration of instance method `%s'.",
  6302.            IDENTIFIER_POINTER (METHOD_SEL_NAME (mth)));
  6303.         }
  6304.     }
  6305.  
  6306.   if (!(hsh = hash_lookup (nst_method_hash_list, METHOD_SEL_NAME (method))))
  6307.     {
  6308.       /* Install on a global chain. */
  6309.       hash_enter (nst_method_hash_list, method);
  6310.     }
  6311.   else
  6312.     {
  6313.       /* Check types; if different, add to a list.  */
  6314.       if (!comp_proto_with_proto (method, hsh->key))
  6315.         hash_add_attr (hsh, method);
  6316.     }
  6317.   return method;
  6318. }
  6319.  
  6320. static tree
  6321. add_class (class)
  6322.      tree class;
  6323. {
  6324.   /* Put interfaces on list in reverse order. */
  6325.   TREE_CHAIN (class) = interface_chain;
  6326.   interface_chain = class;
  6327.   return interface_chain;
  6328. }
  6329.  
  6330. static void
  6331. add_category (class, category)
  6332.       tree class;
  6333.       tree category;
  6334. {
  6335.   /* Put categories on list in reverse order. */
  6336.  
  6337.   tree cat = CLASS_CATEGORY_LIST (class);
  6338.   while (cat)
  6339.     {
  6340.       if (CLASS_SUPER_NAME (cat) == CLASS_SUPER_NAME (category))
  6341.     warning ("duplicate interface declaration for category `%s(%s)'",
  6342.          IDENTIFIER_POINTER (CLASS_NAME (class)),
  6343.          IDENTIFIER_POINTER (CLASS_SUPER_NAME (category)));
  6344.       cat = CLASS_CATEGORY_LIST (cat);
  6345.     }
  6346.  
  6347.   CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (class);
  6348.   CLASS_CATEGORY_LIST (class) = category;
  6349. }
  6350.  
  6351. /* Called after parsing each instance variable declaration. Necessary to
  6352.    preserve typedefs and implement public/private...
  6353.  
  6354.    PUBLIC is 1 for public, 0 for protected, and 2 for private.  */
  6355.  
  6356. tree
  6357. add_instance_variable (class, public, declarator, declspecs, width)
  6358.      tree class;
  6359.      int public;
  6360.      tree declarator;
  6361.      tree declspecs;
  6362.      tree width;
  6363. {
  6364.   tree field_decl, raw_decl;
  6365.   raw_decl = build_tree_list (declspecs    /*purpose*/, declarator/*value*/);
  6366.  
  6367.   if (CLASS_RAW_IVARS (class))
  6368.     chainon (CLASS_RAW_IVARS (class), raw_decl);
  6369.   else
  6370.     CLASS_RAW_IVARS (class) = raw_decl;
  6371.  
  6372.   field_decl = grokfield (input_filename, lineno,
  6373.               declarator, declspecs, width);
  6374.  
  6375.   /* Overload the public attribute, it is not used for FIELD_DECLs. */
  6376.   switch (public)
  6377.     {
  6378.     case 0:
  6379.       TREE_PUBLIC (field_decl) = 0;
  6380.       TREE_PRIVATE (field_decl) = 0;
  6381.       TREE_PROTECTED (field_decl) = 1;
  6382.       break;
  6383.  
  6384.     case 1:
  6385.       TREE_PUBLIC (field_decl) = 1;
  6386.       TREE_PRIVATE (field_decl) = 0;
  6387.       TREE_PROTECTED (field_decl) = 0;
  6388.       break;
  6389.  
  6390.     case 2:
  6391.       TREE_PUBLIC (field_decl) = 0;
  6392.       TREE_PRIVATE (field_decl) = 1;
  6393.       TREE_PROTECTED (field_decl) = 0;
  6394.       break;
  6395.  
  6396.     }
  6397.  
  6398.   if (CLASS_IVARS (class))
  6399.     chainon (CLASS_IVARS (class), field_decl);
  6400.   else
  6401.     CLASS_IVARS (class) = field_decl;
  6402.  
  6403.   return class;
  6404. }
  6405.  
  6406. tree
  6407. is_ivar (decl_chain, ident)
  6408.      tree decl_chain;
  6409.      tree ident;
  6410. {
  6411.   for ( ; decl_chain; decl_chain = TREE_CHAIN (decl_chain))
  6412.     if (DECL_NAME (decl_chain) == ident)
  6413.       return decl_chain;
  6414.   return NULL_TREE;
  6415. }
  6416.  
  6417. /* True if the ivar is private and we are not in its implementation.  */
  6418.  
  6419. int
  6420. is_private (decl)
  6421.      tree decl;
  6422. {
  6423.   if (TREE_PRIVATE (decl)
  6424.       && ! is_ivar (CLASS_IVARS (implementation_template), DECL_NAME (decl)))
  6425.     {
  6426.       error ("instance variable `%s' is declared private",
  6427.          IDENTIFIER_POINTER (DECL_NAME (decl)));
  6428.       return 1;
  6429.     }
  6430.   else
  6431.     return 0;
  6432. }
  6433.  
  6434. /* We have an instance variable reference;, check to see if it is public.  */
  6435.  
  6436. int
  6437. is_public (expr, identifier)
  6438.      tree expr;
  6439.      tree identifier;
  6440. {
  6441.   tree basetype = TREE_TYPE (expr);
  6442.   enum tree_code code = TREE_CODE (basetype);
  6443.   tree decl;
  6444.  
  6445.   if (code == RECORD_TYPE)
  6446.     {
  6447.       if (TREE_STATIC_TEMPLATE (basetype))
  6448.     {
  6449.       if (!lookup_interface (TYPE_NAME (basetype)))
  6450.         {
  6451.           error ("Cannot find interface declaration for `%s'",
  6452.              IDENTIFIER_POINTER (TYPE_NAME (basetype)));
  6453.           return 0;
  6454.         }
  6455.  
  6456.       if ((decl = is_ivar (TYPE_FIELDS (basetype), identifier)))
  6457.         {
  6458.           if (TREE_PUBLIC (decl))
  6459.         return 1;
  6460.  
  6461.           /* Important difference between the Stepstone translator:
  6462.          all instance variables should be public within the context
  6463.          of the implementation.  */
  6464.           if (implementation_context
  6465.           && (((TREE_CODE (implementation_context)
  6466.             == CLASS_IMPLEMENTATION_TYPE)
  6467.                || (TREE_CODE (implementation_context)
  6468.                == CATEGORY_IMPLEMENTATION_TYPE))
  6469.               && (CLASS_NAME (implementation_context)
  6470.               == TYPE_NAME (basetype))))
  6471.         return ! is_private (decl);
  6472.  
  6473.           error ("instance variable `%s' is declared %s",
  6474.              IDENTIFIER_POINTER (identifier),
  6475.              TREE_PRIVATE (decl) ? "private" : "protected");
  6476.           return 0;
  6477.         }
  6478.     }
  6479.  
  6480.       else if (implementation_context && (basetype == objc_object_reference))
  6481.     {
  6482.       TREE_TYPE (expr) = uprivate_record;
  6483.       warning ("static access to object of type `id'");
  6484.     }
  6485.     }
  6486.  
  6487.   return 1;
  6488. }
  6489.  
  6490. /* Implement @defs (<classname>) within struct bodies. */
  6491.  
  6492. tree
  6493. get_class_ivars (interface)
  6494.      tree interface;
  6495. {
  6496.   if (!doing_objc_thang)
  6497.     objc_fatal ();
  6498.  
  6499.   /* Make sure we copy the leaf ivars in case @defs is used in a local
  6500.      context.  Otherwise finish_struct will overwrite the layout info
  6501.      using temporary storage.  */
  6502. #ifdef OBJCPLUS
  6503.   return build_tree_list ((tree)access_default, 
  6504.               build_ivar_chain (interface, 1));
  6505. #else
  6506.   return build_ivar_chain (interface, 1);
  6507. #endif
  6508. }
  6509.  
  6510. /* Make sure all entries in CHAIN are also in LIST. */
  6511.  
  6512. static int
  6513. check_methods (chain, list, mtype)
  6514.      tree chain;
  6515.      tree list;
  6516.      int mtype;
  6517. {
  6518.   int first = 1;
  6519.  
  6520.   while (chain)
  6521.     {
  6522.       if (!lookup_method (list, chain))
  6523.     {
  6524.       if (first)
  6525.         {
  6526.           if (TREE_CODE (implementation_context)
  6527.           == CLASS_IMPLEMENTATION_TYPE)
  6528.         warning ("incomplete implementation of class `%s'",
  6529.              IDENTIFIER_POINTER (CLASS_NAME (implementation_context)));
  6530.           else if (TREE_CODE (implementation_context)
  6531.                == CATEGORY_IMPLEMENTATION_TYPE)
  6532.         warning ("incomplete implementation of category `%s'",
  6533.              IDENTIFIER_POINTER (CLASS_SUPER_NAME (implementation_context)));
  6534.           first = 0;
  6535.         }
  6536.  
  6537.       warning ("method definition for `%c%s' not found",
  6538.            mtype, IDENTIFIER_POINTER (METHOD_SEL_NAME (chain)));
  6539.     }
  6540.  
  6541.       // XXX BG compare qualifiers?
  6542.       chain = TREE_CHAIN (chain);
  6543.     }
  6544.  
  6545.     return first;
  6546. }
  6547.  
  6548. static int
  6549. conforms_to_protocol (class, protocol)
  6550. tree class;
  6551. tree protocol;
  6552. {
  6553.    while (protocol)
  6554.      {
  6555.        tree p = CLASS_PROTOCOL_LIST (class);
  6556.        while (p && TREE_VALUE (p) != TREE_VALUE (protocol))
  6557.      p = TREE_CHAIN (p);
  6558.        if (!p)
  6559.      {
  6560.        tree super = (CLASS_SUPER_NAME (class)
  6561.              ? lookup_interface (CLASS_SUPER_NAME (class))
  6562.              : NULL_TREE);
  6563.        int tmp = super ? conforms_to_protocol (super, protocol) : 0;
  6564.        if (!tmp)
  6565.          return 0;
  6566.      }
  6567.        protocol = TREE_CHAIN (protocol);
  6568.      }
  6569.    return 1;
  6570. }
  6571.  
  6572. /* Make sure all methods in CHAIN are accessible as MTYPE methods in 
  6573.    CONTEXT.  This is one of two mechanisms to check protocol integrity. */
  6574.  
  6575. static int
  6576. check_methods_accessible (chain, context, mtype)
  6577.      tree chain;
  6578.      tree context; /* implementation_context */
  6579.      int mtype;
  6580. {
  6581.   int first = 1;
  6582.   tree list;
  6583.   tree base_context = context;
  6584.  
  6585.   while (chain)
  6586.     {
  6587.       context = base_context;
  6588.       while (context)
  6589.     {
  6590.       if (mtype == '+')
  6591.         list = CLASS_CLS_METHODS (context);
  6592.       else
  6593.         list = CLASS_NST_METHODS (context);
  6594.  
  6595.       if (lookup_method (list, chain))
  6596.           break; 
  6597.  
  6598.       else if (TREE_CODE (context) == CLASS_IMPLEMENTATION_TYPE
  6599.            || TREE_CODE (context) == CLASS_INTERFACE_TYPE)
  6600.         context = (CLASS_SUPER_NAME (context) 
  6601.                ? lookup_interface (CLASS_SUPER_NAME (context))
  6602.                : NULL_TREE);
  6603.  
  6604.       else if (TREE_CODE (context) == CATEGORY_IMPLEMENTATION_TYPE
  6605.            || TREE_CODE (context) == CATEGORY_INTERFACE_TYPE)
  6606.         context = (CLASS_NAME (context) 
  6607.                ? lookup_interface (CLASS_NAME (context))
  6608.                : NULL_TREE);
  6609.       else
  6610.         abort ();
  6611.     }
  6612.  
  6613.       if (context == NULL_TREE)
  6614.     {
  6615.       if (first)
  6616.         {
  6617.           if (TREE_CODE (implementation_context)
  6618.           == CLASS_IMPLEMENTATION_TYPE)
  6619.         warning ("incomplete implementation of class `%s'",
  6620.              IDENTIFIER_POINTER
  6621.                (CLASS_NAME (implementation_context)));
  6622.           else if (TREE_CODE (implementation_context)
  6623.                == CATEGORY_IMPLEMENTATION_TYPE)
  6624.         warning ("incomplete implementation of category `%s'",
  6625.              IDENTIFIER_POINTER
  6626.                (CLASS_SUPER_NAME (implementation_context)));
  6627.           first = 0;
  6628.         }
  6629.       warning ("method definition for `%c%s' not found",
  6630.            mtype, IDENTIFIER_POINTER (METHOD_SEL_NAME (chain)));
  6631.     }
  6632.  
  6633.       chain = TREE_CHAIN (chain); /* next method... */
  6634.     }
  6635.     return first;
  6636. }
  6637.  
  6638. static void
  6639. check_protocols (proto_list, type, name)
  6640.      tree proto_list;
  6641.      char *type;
  6642.      char *name;
  6643. {
  6644.   for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
  6645.     {
  6646.       tree p = TREE_VALUE (proto_list);
  6647.  
  6648.       if (TREE_CODE (p) == PROTOCOL_INTERFACE_TYPE)
  6649.     {
  6650.       int f1, f2;
  6651.       
  6652.       /* Ensure that all protocols have bodies! */
  6653.       if (flag_warn_protocol) {
  6654.         f1 = check_methods (PROTOCOL_CLS_METHODS (p),
  6655.                 CLASS_CLS_METHODS (implementation_context),
  6656.                 '+');
  6657.         f2 = check_methods (PROTOCOL_NST_METHODS (p),
  6658.                 CLASS_NST_METHODS (implementation_context),
  6659.                 '-');
  6660.       } else {
  6661.         f1 = check_methods_accessible (PROTOCOL_CLS_METHODS (p),
  6662.                        implementation_context,
  6663.                        '+');
  6664.         f2 = check_methods_accessible (PROTOCOL_NST_METHODS (p),
  6665.                        implementation_context,
  6666.                        '-');
  6667.       }
  6668.  
  6669.       if (!f1 || !f2)
  6670.         warning ("%s `%s' does not fully implement the `%s' protocol",
  6671.              type, name, IDENTIFIER_POINTER (PROTOCOL_NAME (p)));
  6672.  
  6673.     }
  6674.       else
  6675.     ; /* an identifier...if we could not find a protocol.  */
  6676.  
  6677.       /* Check protocols recursively. */
  6678.       if (PROTOCOL_LIST (p))
  6679.     {
  6680.       tree super_class
  6681.         = lookup_interface (CLASS_SUPER_NAME (implementation_template));
  6682.       if (super_class &&
  6683.           ! conforms_to_protocol (super_class, PROTOCOL_LIST (p)))
  6684.         check_protocols (PROTOCOL_LIST (p), type, name);
  6685.     }
  6686.     }
  6687. }
  6688.  
  6689.  
  6690. /* Make sure that the class CLASS_NAME is defined
  6691.    CODE says which kind of thing CLASS_NAME ought to be.
  6692.    It can be CLASS_INTERFACE_TYPE, CLASS_IMPLEMENTATION_TYPE,
  6693.    CATEGORY_INTERFACE_TYPE, or CATEGORY_IMPLEMENTATION_TYPE.
  6694.  
  6695.    If CODE is CLASS_INTERFACE_TYPE, we also do a push_obstacks_nochange
  6696.    whose matching pop is in continue_class.  */
  6697.  
  6698. tree
  6699. start_class (code, class_name, super_name, protocol_list)
  6700.      enum tree_code code;
  6701.      tree class_name;
  6702.      tree super_name;
  6703.      tree protocol_list;
  6704. {
  6705.   tree class, decl;
  6706.  
  6707.   if (objc_implementation_context)
  6708.     {
  6709.       warning ("`@end' missing in implementation context");
  6710.       finish_class (objc_implementation_context);
  6711.       objc_ivar_chain = NULL_TREE;
  6712.       objc_implementation_context = NULL_TREE;
  6713.     }
  6714.  
  6715.   if (code == CLASS_INTERFACE_TYPE)
  6716.     {
  6717.       push_obstacks_nochange ();
  6718.       end_temporary_allocation ();
  6719.     }
  6720.  
  6721.   if (!doing_objc_thang)
  6722.     objc_fatal ();
  6723.  
  6724. #ifdef OBJCPLUS
  6725.   {
  6726.     struct obstack *ambient_obstack = current_obstack;
  6727.     current_obstack = &permanent_obstack;
  6728. #endif
  6729.  
  6730.     class = make_node (code);
  6731.     TYPE_BINFO (class) = make_tree_vec (5);
  6732.  
  6733. #ifdef OBJCPLUS    
  6734.     current_obstack = ambient_obstack;
  6735.   }
  6736. #endif
  6737.  
  6738.   CLASS_NAME (class) = class_name;
  6739.   CLASS_SUPER_NAME (class) = super_name;
  6740.   CLASS_CLS_METHODS (class) = NULL_TREE;
  6741.  
  6742.   if (! is_class_name (class_name) && (decl = lookup_name (class_name)))
  6743.     {
  6744.       error ("`%s' redeclared as different kind of symbol",
  6745.          IDENTIFIER_POINTER (class_name));
  6746.       error_with_decl (decl, "previous declaration of `%s'");
  6747.     }
  6748.  
  6749.   if (code == CLASS_IMPLEMENTATION_TYPE)
  6750.     {
  6751.       {
  6752.         static tree implemented_classes = 0;
  6753.         tree chain = implemented_classes;
  6754.         for (chain = implemented_classes; chain; chain = TREE_CHAIN (chain))
  6755.            if (TREE_VALUE (chain) == class_name)
  6756.          {
  6757.            error ("reimplementation of class `%s'",
  6758.               IDENTIFIER_POINTER (class_name));
  6759.            return error_mark_node;
  6760.          }
  6761.         implemented_classes = perm_tree_cons (NULL_TREE, class_name,
  6762.                           implemented_classes);
  6763.       }
  6764.  
  6765.       /* Pre-build the following entities - for speed/convenience. */
  6766.       if (!self_id)
  6767.       self_id = get_identifier ("self");
  6768.       if (!ucmd_id)
  6769.         ucmd_id = get_identifier ("_cmd");
  6770.       if (!unused_list)
  6771.     unused_list
  6772.       = build_tree_list (get_identifier ("__unused__"), NULL_TREE);
  6773.       if (!objc_super_template)
  6774.     objc_super_template = build_super_template ();
  6775.  
  6776.       /* Reset for multiple classes per file.  */
  6777.       method_slot = 0;
  6778.  
  6779.       implementation_context = class;
  6780.  
  6781.       /* Lookup the interface for this implementation. */
  6782.  
  6783.       if (!(implementation_template = lookup_interface (class_name)))
  6784.         {
  6785.       warning ("Cannot find interface declaration for `%s'",
  6786.            IDENTIFIER_POINTER (class_name));
  6787.       add_class (implementation_template = implementation_context);
  6788.         }
  6789.  
  6790.       /* If a super class has been specified in the implementation,
  6791.      insure it conforms to the one specified in the interface.  */
  6792.  
  6793.       if (super_name
  6794.       && (super_name != CLASS_SUPER_NAME (implementation_template)))
  6795.         {
  6796.       tree previous_name = CLASS_SUPER_NAME (implementation_template);
  6797.           char *name = previous_name ? IDENTIFIER_POINTER (previous_name) : "";
  6798.       error ("conflicting super class name `%s'",
  6799.          IDENTIFIER_POINTER (super_name));
  6800.       error ("previous declaration of `%s'", name);
  6801.         }
  6802.  
  6803.       else if (! super_name)
  6804.     {
  6805.       CLASS_SUPER_NAME (implementation_context) 
  6806.         = CLASS_SUPER_NAME (implementation_template);
  6807.     }
  6808.     }
  6809.  
  6810.   else if (code == CLASS_INTERFACE_TYPE)
  6811.     {
  6812.       if (lookup_interface (class_name))
  6813.         warning ("duplicate interface declaration for class `%s'",
  6814.                  IDENTIFIER_POINTER (class_name));
  6815.       else
  6816.         add_class (class);
  6817.  
  6818.       if (protocol_list)
  6819.     CLASS_PROTOCOL_LIST (class)
  6820.       = lookup_and_install_protocols (protocol_list);
  6821.     }
  6822.  
  6823.   else if (code == CATEGORY_INTERFACE_TYPE)
  6824.     {
  6825.       tree class_category_is_assoc_with;
  6826.  
  6827.       /* For a category, class_name is really the name of the class that
  6828.      the following set of methods will be associated with. We must
  6829.      find the interface so that can derive the objects template.  */
  6830.  
  6831.       if (!(class_category_is_assoc_with = lookup_interface (class_name)))
  6832.     {
  6833.       error ("Cannot find interface declaration for `%s'",
  6834.          IDENTIFIER_POINTER (class_name));
  6835.       exit (FATAL_EXIT_CODE);
  6836.     }
  6837.       else
  6838.         add_category (class_category_is_assoc_with, class);
  6839.  
  6840.       if (protocol_list)
  6841.     CLASS_PROTOCOL_LIST (class)
  6842.       = lookup_and_install_protocols (protocol_list);
  6843.     }
  6844.  
  6845.   else if (code == CATEGORY_IMPLEMENTATION_TYPE)
  6846.     {
  6847.       /* Pre-build the following entities for speed/convenience. */
  6848.       if (!self_id)
  6849.         self_id = get_identifier ("self");
  6850.       if (!ucmd_id)
  6851.         ucmd_id = get_identifier ("_cmd");
  6852.       if (!unused_list)
  6853.     unused_list
  6854.       = build_tree_list (get_identifier ("__unused__"), NULL_TREE);
  6855.       if (!objc_super_template)
  6856.     objc_super_template = build_super_template ();
  6857.  
  6858.       /* Reset for multiple classes per file.  */
  6859.       method_slot = 0;
  6860.  
  6861.       implementation_context = class;
  6862.  
  6863.       /* For a category, class_name is really the name of the class that
  6864.      the following set of methods will be associated with.  We must
  6865.      find the interface so that can derive the objects template. */
  6866.  
  6867.       if (!(implementation_template = lookup_interface (class_name)))
  6868.         {
  6869.       error ("Cannot find interface declaration for `%s'",
  6870.          IDENTIFIER_POINTER (class_name));
  6871.       exit (FATAL_EXIT_CODE);
  6872.         }
  6873.     }
  6874.   return class;
  6875. }
  6876.  
  6877. tree
  6878. continue_class (class)
  6879.      tree class;
  6880. {
  6881.   if (TREE_CODE (class) == CLASS_IMPLEMENTATION_TYPE
  6882.       || TREE_CODE (class) == CATEGORY_IMPLEMENTATION_TYPE)
  6883.     {
  6884.       struct imp_entry *imp_entry;
  6885.       tree ivar_context;
  6886.  
  6887.       /* Check consistency of the instance variables. */
  6888.  
  6889.       if (CLASS_IVARS (class))
  6890.     check_ivars (implementation_template, class);
  6891.  
  6892.       /* code generation */
  6893.  
  6894. #ifdef OBJCPLUS
  6895.       push_lang_context (lang_name_c);
  6896. #endif
  6897.  
  6898.       ivar_context = build_private_template (implementation_template);
  6899.  
  6900.       if (!objc_class_template)
  6901.     build_class_template ();
  6902.  
  6903.       if (!(imp_entry = (struct imp_entry *) xmalloc (sizeof (struct imp_entry))))
  6904.     perror ("unable to allocate in objc-act.c");
  6905.  
  6906.       imp_entry->next = imp_list;
  6907.       imp_entry->imp_context = class;
  6908.       imp_entry->imp_template = implementation_template;
  6909.  
  6910.       synth_forward_declarations ();
  6911.       imp_entry->class_decl = UOBJC_CLASS_decl;
  6912.       imp_entry->meta_decl = UOBJC_METACLASS_decl;
  6913.  
  6914.       /* Append to front and increment count. */
  6915.       imp_list = imp_entry;
  6916.       if (TREE_CODE (class) == CLASS_IMPLEMENTATION_TYPE)
  6917.     imp_count++;
  6918.       else
  6919.     cat_count++;
  6920.  
  6921. #ifdef OBJCPLUS
  6922.       pop_lang_context ();
  6923. #endif /* OBJCPLUS */
  6924.  
  6925.       return ivar_context;
  6926.     }
  6927.   else if (TREE_CODE (class) == CLASS_INTERFACE_TYPE)
  6928.     {
  6929.       tree record;
  6930.  
  6931. #ifdef OBJCPLUS
  6932.       push_lang_context (lang_name_c);
  6933. #endif
  6934.  
  6935.       record = xref_tag (RECORD_TYPE, CLASS_NAME (class));
  6936.  
  6937.       if (!TYPE_FIELDS (record))
  6938.     {
  6939. #ifdef OBJCPLUS
  6940.       build_private_template (class);
  6941. #else
  6942.       finish_struct (record, build_ivar_chain (class, 0));
  6943.       CLASS_STATIC_TEMPLATE (class) = record;
  6944. #endif
  6945.  
  6946.       /* Mark this record as a class template for static typing.  */
  6947.       TREE_STATIC_TEMPLATE (record) = 1;
  6948.     }
  6949.  
  6950. #ifdef OBJCPLUS
  6951.       pop_lang_context ();
  6952. #endif /* OBJCPLUS */
  6953.  
  6954.       return NULL_TREE;
  6955.     }
  6956.  
  6957.   else
  6958.     return error_mark_node;
  6959. }
  6960.  
  6961. /* This is called once we see the "@end" in an interface/implementation.  */
  6962.  
  6963. void
  6964. finish_class (class)
  6965.      tree class;
  6966. {
  6967.   if (TREE_CODE (class) == CLASS_IMPLEMENTATION_TYPE)
  6968.     {
  6969.       /* All code generation is done in finish_objc. */
  6970.  
  6971.       if (implementation_template != implementation_context)
  6972.     {
  6973.       /* Ensure that all method listed in the interface contain bodies. */
  6974.       check_methods (CLASS_CLS_METHODS (implementation_template),
  6975.              CLASS_CLS_METHODS (implementation_context), '+');
  6976.       check_methods (CLASS_NST_METHODS (implementation_template),
  6977.              CLASS_NST_METHODS (implementation_context), '-');
  6978.  
  6979.       if (CLASS_PROTOCOL_LIST (implementation_template))
  6980.         check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
  6981.                  "class",
  6982.                  IDENTIFIER_POINTER (CLASS_NAME (implementation_context)));
  6983.     }
  6984.     }
  6985.  
  6986.   else if (TREE_CODE (class) == CATEGORY_IMPLEMENTATION_TYPE)
  6987.     {
  6988.       tree category = CLASS_CATEGORY_LIST (implementation_template);
  6989.  
  6990.       /* Find the category interface from the class it is associated with. */
  6991.       while (category)
  6992.     {
  6993.       if (CLASS_SUPER_NAME (class) == CLASS_SUPER_NAME (category))
  6994.         break;
  6995.       category = CLASS_CATEGORY_LIST (category);
  6996.     }
  6997.  
  6998.       if (category)
  6999.     {
  7000.       /* Ensure all method listed in the interface contain bodies. */
  7001.       check_methods (CLASS_CLS_METHODS (category),
  7002.              CLASS_CLS_METHODS (implementation_context), '+');
  7003.       check_methods (CLASS_NST_METHODS (category),
  7004.              CLASS_NST_METHODS (implementation_context), '-');
  7005.  
  7006.       if (CLASS_PROTOCOL_LIST (category))
  7007.         check_protocols (CLASS_PROTOCOL_LIST (category),
  7008.                  "category",
  7009.                  IDENTIFIER_POINTER (CLASS_SUPER_NAME (implementation_context)));
  7010.     }
  7011.     }
  7012.  
  7013.   else if (TREE_CODE (class) == CLASS_INTERFACE_TYPE)
  7014.     {
  7015.       tree decl_specs;
  7016.       char *class_name = IDENTIFIER_POINTER (CLASS_NAME (class));
  7017.       char *string = (char *) alloca (strlen (class_name) + 3);
  7018.  
  7019.       /* extern struct objc_object *_<my_name>; */
  7020.  
  7021.       sprintf (string, "_%s", class_name);
  7022.  
  7023.       decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_EXTERN]);
  7024.       decl_specs = tree_cons (NULL_TREE, objc_object_reference, decl_specs);
  7025.       define_decl (build1 (INDIRECT_REF, NULL_TREE, get_identifier (string)),
  7026.            decl_specs);
  7027.     }
  7028. }
  7029.  
  7030. static tree
  7031. add_protocol (protocol)
  7032.      tree protocol;
  7033. {
  7034.   /* Put protocol on list in reverse order. */
  7035.   TREE_CHAIN (protocol) = protocol_chain;
  7036.   protocol_chain = protocol;
  7037.   return protocol_chain;
  7038. }
  7039.  
  7040. static tree
  7041. lookup_protocol (ident)
  7042.      tree ident;
  7043. {
  7044.   tree chain;
  7045.  
  7046.   for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
  7047.     {
  7048.       if (ident == PROTOCOL_NAME (chain))
  7049.     return chain;
  7050.     }
  7051.  
  7052.   return NULL_TREE;
  7053. }
  7054.  
  7055. /*
  7056.  *  This function forward declares the protocols named by NAMES.  If
  7057.  *  they are already declared or defined, the function has no effect.
  7058.  */
  7059. void
  7060. objc_declare_protocols (names)
  7061.      tree names;
  7062. {
  7063.   tree list;
  7064.  
  7065.   if (!doing_objc_thang)
  7066.     objc_fatal ();
  7067.  
  7068.   for (list = names; list; list = TREE_CHAIN (list))
  7069.     {
  7070.       tree name = TREE_VALUE (list);
  7071.       if (lookup_protocol (name) == NULL_TREE)
  7072.     {
  7073.       tree protocol = make_node (PROTOCOL_INTERFACE_TYPE);
  7074.       TYPE_BINFO (protocol) = make_tree_vec (2);
  7075.       PROTOCOL_NAME (protocol) = name;
  7076.       PROTOCOL_LIST (protocol) = NULL_TREE;
  7077.       add_protocol (protocol);
  7078.       PROTOCOL_DEFINED (protocol) = 0;
  7079.       PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
  7080.     }
  7081.     }
  7082. }
  7083.  
  7084. tree
  7085. start_protocol (code, name, list)
  7086.      enum tree_code code;
  7087.      tree name;
  7088.      tree list;
  7089. {
  7090.   tree protocol;
  7091.  
  7092.   if (!doing_objc_thang)
  7093.     objc_fatal ();
  7094.  
  7095.   /* This is as good a place as any.  Need to invoke push_tag_toplevel.  */
  7096.   if (!objc_protocol_template)
  7097.     objc_protocol_template = build_protocol_template ();
  7098.  
  7099.   protocol = make_node (code);
  7100.   TYPE_BINFO (protocol) = make_tree_vec (2);
  7101.  
  7102.   PROTOCOL_NAME (protocol) = name;
  7103.   PROTOCOL_LIST (protocol) = list;
  7104.  
  7105.   lookup_and_install_protocols (list);
  7106.  
  7107.   if (lookup_protocol (name))
  7108.     warning ("duplicate declaration for protocol `%s'",
  7109.        IDENTIFIER_POINTER (name));
  7110.   else
  7111.     add_protocol (protocol);
  7112.  
  7113.   PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
  7114.  
  7115.   return protocol;
  7116. }
  7117.  
  7118. void
  7119. finish_protocol (protocol)
  7120.     tree protocol;
  7121. {
  7122. }
  7123.  
  7124.  
  7125. /* "Encode" a data type into a string, which grows in util_obstack.
  7126.    ??? What is the FORMAT?  Someone please document this!  */
  7127.  
  7128. static void
  7129. encode_type_qualifiers (declspecs)
  7130.      tree declspecs;
  7131. {
  7132.   tree spec;
  7133.  
  7134.   for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
  7135.     {
  7136.       if (ridpointers[(int) RID_CONST] == TREE_VALUE (spec))
  7137.     obstack_1grow (&util_obstack, 'r');
  7138.       else if (ridpointers[(int) RID_IN] == TREE_VALUE (spec))
  7139.     obstack_1grow (&util_obstack, 'n');
  7140.       else if (ridpointers[(int) RID_INOUT] == TREE_VALUE (spec))
  7141.     obstack_1grow (&util_obstack, 'N');
  7142.       else if (ridpointers[(int) RID_OUT] == TREE_VALUE (spec))
  7143.     obstack_1grow (&util_obstack, 'o');
  7144.       else if (ridpointers[(int) RID_BYCOPY] == TREE_VALUE (spec))
  7145.     obstack_1grow (&util_obstack, 'O');
  7146.       else if (ridpointers[(int) RID_BYREF] == TREE_VALUE (spec))
  7147.     obstack_1grow (&util_obstack, 'R');
  7148.       else if (ridpointers[(int) RID_ONEWAY] == TREE_VALUE (spec))
  7149.     obstack_1grow (&util_obstack, 'V');
  7150.     }
  7151. }
  7152.  
  7153. /* Encode a pointer type.  */
  7154.  
  7155. static void
  7156. encode_pointer (type, curtype, format)
  7157.      tree type;
  7158.      int curtype;
  7159.      int format;
  7160. {
  7161.   tree pointer_to = TREE_TYPE (type);
  7162.  
  7163.   if (TREE_CODE (pointer_to) == RECORD_TYPE)
  7164.     {
  7165.       if (TYPE_NAME (pointer_to)
  7166.       && TREE_CODE (TYPE_NAME (pointer_to)) == IDENTIFIER_NODE)
  7167.     {
  7168.       char *name = IDENTIFIER_POINTER (TYPE_NAME (pointer_to));
  7169.  
  7170.       if (strcmp (name, TAG_OBJECT) == 0) /* '@' */
  7171.         {
  7172.           obstack_1grow (&util_obstack, '@');
  7173.           return;
  7174.         }
  7175.       else if (TREE_STATIC_TEMPLATE (pointer_to))
  7176.         {
  7177.               if (generating_instance_variables)
  7178.             {
  7179.               obstack_1grow (&util_obstack, '@');
  7180.               obstack_1grow (&util_obstack, '"');
  7181.               obstack_grow (&util_obstack, name, strlen (name));
  7182.               obstack_1grow (&util_obstack, '"');
  7183.               return;
  7184.         }
  7185.               else
  7186.             {
  7187.               obstack_1grow (&util_obstack, '@');
  7188.               return;
  7189.         }
  7190.         }
  7191.       else if (strcmp (name, TAG_CLASS) == 0) /* '#' */
  7192.         {
  7193.           obstack_1grow (&util_obstack, '#');
  7194.           return;
  7195.         }
  7196. #ifndef OBJC_INT_SELECTORS
  7197.       else if (strcmp (name, TAG_SELECTOR) == 0) /* ':' */
  7198.         {
  7199.           obstack_1grow (&util_obstack, ':');
  7200.           return;
  7201.         }
  7202. #endif /* OBJC_INT_SELECTORS */
  7203.     }
  7204.     }
  7205.   else if (TREE_CODE (pointer_to) == INTEGER_TYPE
  7206.        && TYPE_MODE (pointer_to) == QImode)
  7207.     {
  7208.       tree pname;
  7209.  
  7210.       if (TYPE_NAME (type)) 
  7211.     {
  7212.       tree tname = TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
  7213.               ? TYPE_NAME (type) 
  7214.               : DECL_NAME (TYPE_NAME (type));
  7215.  
  7216.       if (!strcmp (IDENTIFIER_POINTER (tname), TAG_ATOM))
  7217.         {
  7218.           obstack_1grow (&util_obstack, '%');
  7219.           return;
  7220.         }
  7221.     }
  7222.  
  7223.       pname = TREE_CODE (TYPE_NAME (pointer_to)) == IDENTIFIER_NODE
  7224.               ? TYPE_NAME (pointer_to) 
  7225.               : DECL_NAME (TYPE_NAME (pointer_to));
  7226.       
  7227.       if (!!strcmp (IDENTIFIER_POINTER (pname), "BOOL"))
  7228.     {
  7229.       obstack_1grow (&util_obstack, '*');
  7230.       return;
  7231.     }
  7232.     }
  7233.  
  7234.   /* We have a type that does not get special treatment.  */
  7235.  
  7236.   /* NeXT extension */
  7237.   obstack_1grow (&util_obstack, '^');
  7238.   encode_type (pointer_to, curtype, format);
  7239. }
  7240.  
  7241. static void
  7242. encode_array (type, curtype, format)
  7243.      tree type;
  7244.      int curtype;
  7245.      int format;
  7246. {
  7247.   tree an_int_cst = TYPE_SIZE (type);
  7248.   tree array_of = TREE_TYPE (type);
  7249.   char buffer[40];
  7250.  
  7251.   /* An incomplete array is treated like a pointer.  */
  7252.   if (an_int_cst == NULL)
  7253.     {
  7254.       /* split for obvious reasons.  North-Keys 30 Mar 1991 */
  7255.       encode_pointer (type, curtype, format);
  7256.       return;
  7257.     }
  7258.  
  7259.   sprintf (buffer, "[%d",
  7260.        (TREE_INT_CST_LOW (an_int_cst)
  7261.         / TREE_INT_CST_LOW (TYPE_SIZE (array_of))));
  7262.  
  7263.   obstack_grow (&util_obstack, buffer, strlen (buffer));
  7264.   encode_type (array_of, curtype, format);
  7265.   obstack_1grow (&util_obstack, ']');
  7266.   return;
  7267. }
  7268.  
  7269. static void
  7270. encode_aggregate (type, curtype, format)
  7271.      tree type;
  7272.      int curtype;
  7273.      int format;
  7274. {
  7275.   enum tree_code code = TREE_CODE (type);
  7276.  
  7277.   switch (code)
  7278.     {
  7279.     case RECORD_TYPE:
  7280.       {
  7281.     if (obstack_object_size (&util_obstack) > 0
  7282.         && *(obstack_next_free (&util_obstack) - 1) == '^')
  7283.       {
  7284.         tree name = TYPE_NAME (type);
  7285.  
  7286.         /* We have a reference; this is a NeXT extension. */
  7287.  
  7288.         if (obstack_object_size (&util_obstack) - curtype == 1
  7289.         && format == OBJC_ENCODE_INLINE_DEFS)
  7290.           {
  7291.         /* Output format of struct for first level only. */
  7292.         tree fields = TYPE_FIELDS (type);
  7293.  
  7294.         if (name && TREE_CODE (name) == IDENTIFIER_NODE)
  7295.           {
  7296.             obstack_1grow (&util_obstack, '{');
  7297.             obstack_grow (&util_obstack,
  7298.                   IDENTIFIER_POINTER (name),
  7299.                   strlen (IDENTIFIER_POINTER (name)));
  7300.             obstack_1grow (&util_obstack, '=');
  7301.           }
  7302.  
  7303.         else
  7304.           obstack_grow (&util_obstack, "{?=", 3);
  7305.  
  7306.         for ( ; fields; fields = TREE_CHAIN (fields))
  7307.           encode_field_decl (fields, curtype, format);
  7308.  
  7309.         obstack_1grow (&util_obstack, '}');
  7310.           }
  7311.  
  7312.             else if (name && TREE_CODE (name) == IDENTIFIER_NODE)
  7313.           {
  7314.         obstack_1grow (&util_obstack, '{');
  7315.         obstack_grow (&util_obstack,
  7316.                   IDENTIFIER_POINTER (name),
  7317.                   strlen (IDENTIFIER_POINTER (name)));
  7318.         obstack_1grow (&util_obstack, '}');
  7319.           }
  7320.  
  7321.         else
  7322.           /* We have an untagged structure or a typedef. */
  7323.           obstack_grow (&util_obstack, "{?}", 3);
  7324.       }
  7325.  
  7326.     else
  7327.       {
  7328.         tree name = TYPE_NAME (type);
  7329.         tree fields = TYPE_FIELDS (type);
  7330.  
  7331.         if (format == OBJC_ENCODE_INLINE_DEFS
  7332.         || generating_instance_variables)
  7333.           {
  7334.         obstack_1grow (&util_obstack, '{');
  7335.         if (name && TREE_CODE (name) == IDENTIFIER_NODE)
  7336.           obstack_grow (&util_obstack,
  7337.                 IDENTIFIER_POINTER (name),
  7338.                 strlen (IDENTIFIER_POINTER (name)));
  7339.  
  7340.         else
  7341.           obstack_1grow (&util_obstack, '?');
  7342.  
  7343.         obstack_1grow (&util_obstack, '=');
  7344.  
  7345.         for (; fields; fields = TREE_CHAIN (fields))
  7346.           {
  7347.                   if (generating_instance_variables)
  7348.                     {
  7349.                       tree fname = DECL_NAME (fields);
  7350.  
  7351.               obstack_1grow (&util_obstack, '"');
  7352.               if (fname && TREE_CODE (fname) == IDENTIFIER_NODE)
  7353.                 {
  7354.               obstack_grow (&util_obstack,
  7355.                     IDENTIFIER_POINTER (fname),
  7356.                     strlen (IDENTIFIER_POINTER (fname)));
  7357.             }
  7358.  
  7359.               obstack_1grow (&util_obstack, '"');
  7360.                     }
  7361.  
  7362.           encode_field_decl (fields, curtype, format);
  7363.           }
  7364.  
  7365.         obstack_1grow (&util_obstack, '}');
  7366.           }
  7367.  
  7368.         else
  7369.           {
  7370.         obstack_1grow (&util_obstack, '{');
  7371.         if (name && TREE_CODE (name) == IDENTIFIER_NODE)
  7372.           obstack_grow (&util_obstack,
  7373.                 IDENTIFIER_POINTER (name),
  7374.                 strlen (IDENTIFIER_POINTER (name)));
  7375.         else
  7376.           /* We have an untagged structure or a typedef. */
  7377.           obstack_1grow (&util_obstack, '?');
  7378.  
  7379.         obstack_1grow (&util_obstack, '}');
  7380.           }
  7381.       }
  7382.     break;
  7383.       }
  7384.     case UNION_TYPE:
  7385.       {
  7386.     if (*obstack_next_free (&util_obstack) == '^'
  7387.         || format != OBJC_ENCODE_INLINE_DEFS)
  7388.       {
  7389.         /* We have a reference (this is a NeXT extension)
  7390.            or we don't want the details.  */
  7391.             if (TYPE_NAME (type)
  7392.         && TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
  7393.           {
  7394.         obstack_1grow (&util_obstack, '(');
  7395.         obstack_grow (&util_obstack,
  7396.                   IDENTIFIER_POINTER (TYPE_NAME (type)),
  7397.                   strlen (IDENTIFIER_POINTER (TYPE_NAME (type))));
  7398.         obstack_1grow (&util_obstack, ')');
  7399.           }
  7400.  
  7401.         else
  7402.           /* We have an untagged structure or a typedef. */
  7403.           obstack_grow (&util_obstack, "(?)", 3);
  7404.       }
  7405.     else
  7406.       {
  7407.         tree fields = TYPE_FIELDS (type);
  7408.         obstack_1grow (&util_obstack, '(');
  7409.         for ( ; fields; fields = TREE_CHAIN (fields))
  7410.           encode_field_decl (fields, curtype, format);
  7411.  
  7412.         obstack_1grow (&util_obstack, ')');
  7413.       }
  7414.     break;
  7415.       }
  7416.  
  7417.     case ENUMERAL_TYPE:
  7418.       obstack_1grow (&util_obstack, 'i');
  7419.       break;
  7420.     }
  7421. }
  7422.  
  7423. /* Support bitfields.  The current version of Objective-C does not support
  7424.    them.  The string will consist of one or more "b:n"'s where n is an
  7425.    integer describing the width of the bitfield. Currently, classes in
  7426.    the kit implement a method "-(char *)describeBitfieldStruct:" that
  7427.    simulates this. If they do not implement this method, the archiver
  7428.    assumes the bitfield is 16 bits wide (padded if necessary) and packed
  7429.    according to the GNU compiler. After looking at the "kit", it appears
  7430.    that all classes currently rely on this default behavior, rather than
  7431.    hand generating this string (which is tedious).  */
  7432.  
  7433. static void
  7434. encode_bitfield (width, format)
  7435.      int width;
  7436.      int format;
  7437. {
  7438.   char buffer[40];
  7439.   sprintf (buffer, "b%d", width);
  7440.   obstack_grow (&util_obstack, buffer, strlen (buffer));
  7441. }
  7442.  
  7443. /* FORMAT will be OBJC_ENCODE_INLINE_DEFS or OBJC_ENCODE_DONT_INLINE_DEFS.  */
  7444.  
  7445. static void
  7446. encode_type (type, curtype, format)
  7447.      tree type;
  7448.      int curtype;
  7449.      int format;
  7450. {
  7451.   enum tree_code code = TREE_CODE (type);
  7452.  
  7453.   if (code == INTEGER_TYPE)
  7454.     {
  7455.       if (TREE_INT_CST_LOW (TYPE_MIN_VALUE (type)) == 0
  7456.       && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type)) == 0)
  7457.     {
  7458.       /* Unsigned integer types. */
  7459.  
  7460.       if (TYPE_MODE (type) == QImode) /* 'C' */
  7461.         obstack_1grow (&util_obstack, 'C');
  7462.       else if (TYPE_MODE (type) == HImode) /* 'S' */
  7463.         obstack_1grow (&util_obstack, 'S');
  7464.       else if (TYPE_MODE (type) == SImode)
  7465.         {
  7466.           if (type == long_unsigned_type_node)
  7467.         obstack_1grow (&util_obstack, 'L'); /* 'L' */
  7468.           else
  7469.         obstack_1grow (&util_obstack, 'I'); /* 'I' */
  7470.         }
  7471.       else if (TYPE_MODE (type) == DImode) /* 'Q' */
  7472. #ifdef __alpha__
  7473.         obstack_1grow (&util_obstack, 'L');
  7474. #else
  7475.         obstack_1grow (&util_obstack, 'Q');
  7476. #endif
  7477.     }
  7478.  
  7479.       else
  7480.     /* Signed integer types. */
  7481.     {
  7482.       if (TYPE_MODE (type) == QImode) /* 'c' */
  7483.         obstack_1grow (&util_obstack, 'c');
  7484.       else if (TYPE_MODE (type) == HImode) /* 's' */
  7485.         obstack_1grow (&util_obstack, 's');
  7486.       else if (TYPE_MODE (type) == SImode) /* 'i' */
  7487.         {
  7488.           if (type == long_integer_type_node)
  7489.         obstack_1grow (&util_obstack, 'l'); /* 'l' */
  7490.           else
  7491.         obstack_1grow (&util_obstack, 'i'); /* 'i' */
  7492.         }
  7493.       else if (TYPE_MODE (type) == DImode) /* 'q' */
  7494. #ifdef __alpha__
  7495.         obstack_1grow (&util_obstack, 'l');
  7496. #else
  7497.         obstack_1grow (&util_obstack, 'q');
  7498. #endif
  7499.     }
  7500.     }
  7501.  
  7502.   else if (code == REAL_TYPE)
  7503.     {
  7504.       /* floating point types */
  7505.  
  7506.       if (TYPE_MODE (type) == SFmode) /* 'f' */
  7507.     obstack_1grow (&util_obstack, 'f');
  7508.       else if (TYPE_MODE (type) == DFmode
  7509.            || TYPE_MODE (type) == TFmode) /* 'd' */
  7510.     obstack_1grow (&util_obstack, 'd');
  7511.     }
  7512.  
  7513.   else if (code == VOID_TYPE)    /* 'v' */
  7514.     obstack_1grow (&util_obstack, 'v');
  7515.  
  7516.   else if (code == ARRAY_TYPE)
  7517.     encode_array (type, curtype, format);
  7518.  
  7519.   else if (code == POINTER_TYPE)
  7520.     encode_pointer (type, curtype, format);
  7521.  
  7522.   else if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
  7523.     encode_aggregate (type, curtype, format);
  7524.  
  7525.   else if (code == FUNCTION_TYPE) /* '?' */
  7526.     obstack_1grow (&util_obstack, '?');
  7527. }
  7528.  
  7529. static void
  7530. encode_field_decl (field_decl, curtype, format)
  7531.      tree field_decl;
  7532.      int curtype;
  7533.      int format;
  7534. {
  7535.   tree type;
  7536.  
  7537.  /* If this field is obviously a bitfield, or is a bitfield that has been
  7538.      clobbered to look like a ordinary integer mode, go ahead and generate
  7539.      the bitfield typing information. */
  7540.   type = TREE_TYPE (field_decl);
  7541. #ifdef OBJCPLUS
  7542.   /* C++ static members should not appear in the encoding */
  7543.   if (TREE_STATIC (field_decl))
  7544.     return;
  7545. #endif
  7546.   if (DECL_BIT_FIELD (field_decl))
  7547.     encode_bitfield (DECL_FIELD_SIZE (field_decl), format);
  7548.   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  7549.        && DECL_FIELD_SIZE (field_decl)
  7550.        && TYPE_MODE (type) > DECL_MODE (field_decl))
  7551.     encode_bitfield (DECL_FIELD_SIZE (field_decl), format);
  7552.   else
  7553.     encode_type (TREE_TYPE (field_decl), curtype, format);
  7554. }
  7555.  
  7556. static tree
  7557. expr_last (complex_expr)
  7558.      tree complex_expr;
  7559. {
  7560.   tree next;
  7561.  
  7562.   if (complex_expr)
  7563.     while ((next = TREE_OPERAND (complex_expr, 0)))
  7564.       complex_expr = next;
  7565.   return complex_expr;
  7566. }
  7567.  
  7568.  
  7569. /* Transform a method definition into a function definition as follows:
  7570.    - synthesize the first two arguments, "self" and "_cmd".  */
  7571.  
  7572. void
  7573. start_method_def (method)
  7574.      tree method;
  7575. {
  7576.   tree decl_specs;
  7577.  
  7578.   /* Required to implement _msgSuper.  */
  7579.   method_context = method;
  7580.   UOBJC_SUPER_decl = NULL_TREE;
  7581.  
  7582.   /* Must be called BEFORE start_function.  */
  7583.   pushlevel (0);
  7584.  
  7585.   /* Generate prototype declarations for arguments..."new-style".  */
  7586.  
  7587.   if (TREE_CODE (method_context) == INSTANCE_METHOD_DECL)
  7588.     decl_specs = build_tree_list (NULL_TREE, uprivate_record);
  7589.   else
  7590.     /* Really a `struct objc_class *'. However, we allow people to
  7591.        assign to self, which changes its type midstream.  */
  7592.     decl_specs = build_tree_list (NULL_TREE, objc_object_reference);
  7593.  
  7594.   push_parm_decl (build_tree_list
  7595.           (build_tree_list (decl_specs,
  7596.                     build1 (INDIRECT_REF, NULL_TREE, self_id)),
  7597.            build_tree_list (unused_list, NULL_TREE)));
  7598.  
  7599. #ifdef OBJC_INT_SELECTORS
  7600.   decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_UNSIGNED]);
  7601.   decl_specs = tree_cons (NULL_TREE, ridpointers[(int) RID_INT], decl_specs);
  7602.   push_parm_decl (build_tree_list (build_tree_list (decl_specs, ucmd_id),
  7603.                    build_tree_list (unused_list, NULL_TREE)));
  7604. #else /* not OBJC_INT_SELECTORS */
  7605.   decl_specs = build_tree_list (NULL_TREE,
  7606.                 xref_tag (RECORD_TYPE,
  7607.                       get_identifier (TAG_SELECTOR)));
  7608.   push_parm_decl (build_tree_list
  7609.           (build_tree_list (decl_specs,
  7610.                     build1 (INDIRECT_REF, NULL_TREE, ucmd_id)),
  7611.            build_tree_list (unused_list, NULL_TREE)));
  7612. #endif /* not OBJC_INT_SELECTORS */
  7613.  
  7614.   /* Generate argument declarations if a keyword_decl. */
  7615.   if (METHOD_SEL_ARGS (method))
  7616.     {
  7617.       tree arglist = METHOD_SEL_ARGS (method);
  7618.       do
  7619.     {
  7620.       tree arg_spec = TREE_PURPOSE (TREE_TYPE (arglist));
  7621.       tree arg_decl = TREE_VALUE (TREE_TYPE (arglist));
  7622.  
  7623.       if (arg_decl)
  7624.         {
  7625.           tree last_expr = expr_last (arg_decl);
  7626.  
  7627.           /* Unite the abstract decl with its name. */
  7628.           TREE_OPERAND (last_expr, 0) = KEYWORD_ARG_NAME (arglist);
  7629.           push_parm_decl (build_tree_list
  7630.                   (build_tree_list (arg_spec, arg_decl),
  7631.                    build_tree_list (NULL_TREE, NULL_TREE)));
  7632.  
  7633.           /* Unhook: restore the abstract declarator. */
  7634.           TREE_OPERAND (last_expr, 0) = NULL_TREE;
  7635.         }
  7636.  
  7637.       else
  7638.         push_parm_decl (build_tree_list
  7639.                 (build_tree_list (arg_spec,
  7640.                           KEYWORD_ARG_NAME (arglist)),
  7641.                  build_tree_list (NULL_TREE, NULL_TREE)));
  7642.  
  7643.       arglist = TREE_CHAIN (arglist);
  7644.     }
  7645.       while (arglist);
  7646.     }
  7647.  
  7648.   if (METHOD_ADD_ARGS (method) > (tree)1)
  7649.     {
  7650.       /* We have a variable length selector - in "prototype" format. */
  7651.       tree akey = TREE_PURPOSE (METHOD_ADD_ARGS (method));
  7652.       while (akey)
  7653.     {
  7654.       /* This must be done prior to calling pushdecl.  pushdecl is
  7655.          going to change our chain on us.  */
  7656.       tree nextkey = TREE_CHAIN (akey);
  7657.       pushdecl (akey);
  7658.       akey = nextkey;
  7659.     }
  7660.     }
  7661. }
  7662.  
  7663. static void
  7664. warn_with_method (message, mtype, method)
  7665.      char *message;
  7666.      char mtype;
  7667.      tree method;
  7668. {
  7669.   char method_name[BUFSIZE];
  7670.  
  7671.   if (count_error (1) == 0)
  7672.     return;
  7673.  
  7674.   report_error_function (DECL_SOURCE_FILE (method));
  7675.   method_name[0] = 0;
  7676.   sprintf (errbuf, "%s `%c%s'\0",
  7677.        message, mtype, gen_method_decl (method, method_name));
  7678.  
  7679.   warning_with_file_and_line (DECL_SOURCE_FILE (method),
  7680.                   DECL_SOURCE_LINE (method),
  7681.                   errbuf);
  7682. }
  7683.  
  7684. /* Return 1 if METHOD is consistent with PROTO. */
  7685.  
  7686. static int
  7687. comp_method_with_proto (method, proto)
  7688.      tree method, proto;
  7689. {
  7690.   static tree function_type = 0;
  7691.  
  7692.   /* Create a function_type node once. */
  7693.   if (!function_type)
  7694.     {
  7695.       struct obstack *ambient_obstack = current_obstack;
  7696.  
  7697.       current_obstack = &permanent_obstack;
  7698.       function_type = make_node (FUNCTION_TYPE);
  7699.       current_obstack = ambient_obstack;
  7700.     }
  7701.  
  7702.   /* Install argument types - normally set by build_function_type.  */
  7703.   TYPE_ARG_TYPES (function_type) = get_arg_type_list (proto, METHOD_DEF, 0);
  7704.  
  7705.   /* install return type */
  7706.   TREE_TYPE (function_type) = groktypename (TREE_TYPE (proto));
  7707.  
  7708.   return comptypes (TREE_TYPE (METHOD_DEFINITION (method)), function_type);
  7709. }
  7710.  
  7711. /* Return 1 if PROTO1 is consistent with PROTO2. */
  7712.  
  7713. static int
  7714. comp_proto_with_proto (proto1, proto2)
  7715.      tree proto1, proto2;
  7716. {
  7717.   static tree function_type1 = 0, function_type2 = 0;
  7718.  
  7719.   /* create a couple function_type node's once */
  7720.   if (!function_type1)
  7721.     {
  7722.       struct obstack *ambient_obstack = current_obstack;
  7723.  
  7724.       current_obstack = &permanent_obstack;
  7725.       function_type1 = make_node (FUNCTION_TYPE);
  7726.       function_type2 = make_node (FUNCTION_TYPE);
  7727.       current_obstack = ambient_obstack;
  7728.     }
  7729.  
  7730.   /* Install argument types; normally set by build_function_type.  */
  7731.   TYPE_ARG_TYPES (function_type1) = get_arg_type_list (proto1, METHOD_REF, 0);
  7732.   TYPE_ARG_TYPES (function_type2) = get_arg_type_list (proto2, METHOD_REF, 0);
  7733.  
  7734.   /* Install return type. */
  7735.   TREE_TYPE (function_type1) = groktypename (TREE_TYPE (proto1));
  7736.   TREE_TYPE (function_type2) = groktypename (TREE_TYPE (proto2));
  7737.  
  7738.   return comptypes (function_type1, function_type2);
  7739. }
  7740.  
  7741. /* - generate an identifier for the function. the format is "_n_cls",
  7742.      where 1 <= n <= nMethods, and cls is the name the implementation we
  7743.      are processing.
  7744.    - install the return type from the method declaration.
  7745.    - if we have a prototype, check for type consistency.  */
  7746.  
  7747. static void
  7748. really_start_method (method, parmlist)
  7749.      tree method, parmlist;
  7750. {
  7751.   tree sc_spec, ret_spec, ret_decl, decl_specs;
  7752.   tree method_decl, method_id;
  7753.   char *buf, *sel_name, *class_name, *cat_name;
  7754.  
  7755.   /* synth the storage class & assemble the return type */
  7756.   sc_spec = tree_cons (NULL_TREE, ridpointers[(int) RID_STATIC], NULL_TREE);
  7757.   ret_spec = TREE_PURPOSE (TREE_TYPE (method));
  7758.   decl_specs = chainon (sc_spec, ret_spec);
  7759.  
  7760.   sel_name = IDENTIFIER_POINTER (METHOD_SEL_NAME (method));
  7761.   class_name = IDENTIFIER_POINTER (CLASS_NAME (implementation_context));
  7762.   cat_name = ((TREE_CODE (implementation_context)
  7763.            == CLASS_IMPLEMENTATION_TYPE)
  7764.           ? NULL
  7765.           : IDENTIFIER_POINTER (CLASS_SUPER_NAME (implementation_context)));
  7766.   method_slot++;
  7767.   /* Make sure this is big enough for any plausible method label.  */
  7768.   buf = (char *) alloca (50 + strlen (sel_name) + strlen (class_name)
  7769.              + (cat_name ? strlen (cat_name) : 0));
  7770.  
  7771.   OBJC_GEN_METHOD_LABEL (buf, TREE_CODE (method) == INSTANCE_METHOD_DECL,
  7772.              class_name, cat_name, sel_name, method_slot);
  7773.  
  7774.   method_id = get_identifier (buf);
  7775.  
  7776. #ifdef OBJCPLUS
  7777.   /* Objective-C methods cannot be overloaded, so we don't need
  7778.      the type encoding appended.  It looks bad anyway... */
  7779.   push_lang_context (lang_name_c);
  7780. #endif
  7781.  
  7782.   method_decl = build_nt (CALL_EXPR, method_id, parmlist, NULL_TREE);
  7783.  
  7784.   /* check the declarator portion of the return type for the method */
  7785.   if ((ret_decl = TREE_VALUE (TREE_TYPE (method))))
  7786.     {
  7787.       /* unite the complex decl (specified in the abstract decl) with the
  7788.      function decl just synthesized..(int *), (int (*)()), (int (*)[]).  */
  7789.       tree save_expr = expr_last (ret_decl);
  7790.  
  7791.       TREE_OPERAND (save_expr, 0) = method_decl;
  7792.       method_decl = ret_decl;
  7793.       /* fool the parser into thinking it is starting a function */
  7794.       start_function (decl_specs, method_decl, 0);
  7795. #ifdef OBJCPLUS
  7796.       /* must be called AFTER "start_function()" */
  7797.       if (! current_function_parms_stored)
  7798.     store_parm_decls ();     
  7799. #endif
  7800.       /* unhook...this has the effect of restoring the abstract declarator */
  7801.       TREE_OPERAND (save_expr, 0) = NULL_TREE;
  7802.     }
  7803.   else
  7804.     {
  7805.       TREE_VALUE (TREE_TYPE (method)) = method_decl;
  7806.       /* fool the parser into thinking it is starting a function */
  7807.       start_function (decl_specs, method_decl, 0);
  7808. #ifdef OBJCPLUS
  7809.       /* must be called AFTER "start_function()" */
  7810.       if (! current_function_parms_stored)
  7811.     store_parm_decls ();     
  7812. #endif
  7813.       /* unhook...this has the effect of restoring the abstract declarator */
  7814.       TREE_VALUE (TREE_TYPE (method)) = NULL_TREE;
  7815.     }
  7816.  
  7817. #ifdef OBJCPLUS
  7818.   /* set self_decl from the first argument...this global is used by 
  7819.    * build_ivar_reference().build_indirect_ref().
  7820.    */
  7821.   self_decl = DECL_ARGUMENTS(current_function_decl);
  7822. #endif /* OBJCPLUS */
  7823.  
  7824. #ifdef OBJCPLUS
  7825.   pop_lang_context ();
  7826. #endif
  7827.  
  7828.   METHOD_DEFINITION (method) = current_function_decl;
  7829.  
  7830.   /* Check consistency...start_function, pushdecl, duplicate_decls.  */
  7831.  
  7832.   if (implementation_template != implementation_context)
  7833.     {
  7834.       tree proto;
  7835.  
  7836.       if (TREE_CODE (method) == INSTANCE_METHOD_DECL)
  7837.     proto = lookup_instance_method_static (implementation_template,
  7838.                            METHOD_SEL_NAME (method));
  7839.       else
  7840.     proto = lookup_class_method_static (implementation_template,
  7841.                         METHOD_SEL_NAME (method));
  7842.  
  7843.       if (proto && ! comp_method_with_proto (method, proto))
  7844.     {
  7845.       char type = (TREE_CODE (method) == INSTANCE_METHOD_DECL ? '-' : '+');
  7846.  
  7847.       warn_with_method ("conflicting types for", type, method);
  7848.       warn_with_method ("previous declaration of", type, proto);
  7849.     }
  7850.     }
  7851. }
  7852.  
  7853. /* The following routine is always called...this "architecture" is to
  7854.    accommodate "old-style" variable length selectors.
  7855.  
  7856.    - a:a b:b // prototype  ; id c; id d; // old-style.  */
  7857.  
  7858. void
  7859. continue_method_def ()
  7860. {
  7861.   tree parmlist;
  7862.  
  7863.   if (METHOD_ADD_ARGS (method_context) == (tree)1)
  7864.     /* We have a `, ...' immediately following the selector.  */
  7865.     parmlist = get_parm_info (0);
  7866.   else
  7867.     parmlist = get_parm_info (1); /* place a `void_at_end' */
  7868.  
  7869. #ifndef OBJCPLUS
  7870.   /* Set self_decl from the first argument...this global is used by
  7871.      build_ivar_reference calling build_indirect_ref.  */
  7872.   self_decl = TREE_PURPOSE (parmlist);
  7873. #endif /* !OBJCPLUS */
  7874.  
  7875.   poplevel (0, 0, 0);        /* must be called BEFORE start_function.  */
  7876.  
  7877.   really_start_method (method_context, parmlist);
  7878.  
  7879. #ifndef OBJCPLUS
  7880.   store_parm_decls ();        /* must be called AFTER start_function.  */
  7881. #endif
  7882. }
  7883.  
  7884. /* Called by the parser, from the `pushlevel' production.  */
  7885.  
  7886. void
  7887. add_objc_decls ()
  7888. {
  7889.   if (!UOBJC_SUPER_decl)
  7890.     {
  7891.       UOBJC_SUPER_decl = start_decl (get_identifier (UTAG_SUPER),
  7892.                      build_tree_list (NULL_TREE,
  7893.                               objc_super_template),
  7894.                      0);
  7895.  
  7896.       finish_decl (UOBJC_SUPER_decl, NULL_TREE, NULL_TREE);
  7897.  
  7898.       /* This prevents `unused variable' warnings when compiling with -Wall. */
  7899.       TREE_USED (UOBJC_SUPER_decl) = 1;
  7900.       DECL_ARTIFICIAL (UOBJC_SUPER_decl) = 1;
  7901.     }
  7902. }
  7903.  
  7904. /* _n_Method (id self, SEL sel, ...)
  7905.      {
  7906.        struct objc_super _S;
  7907.        _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
  7908.      }  */
  7909.  
  7910. tree
  7911. get_super_receiver ()
  7912. {
  7913.   if (method_context)
  7914.     {
  7915.       tree super_expr, super_expr_list;
  7916.  
  7917.       /* Set receiver to self. */
  7918.       super_expr = build_component_ref (UOBJC_SUPER_decl, self_id);
  7919.       super_expr = build_modify_expr (super_expr, NOP_EXPR, self_decl);
  7920.       super_expr_list = build_tree_list (NULL_TREE, super_expr);
  7921.  
  7922.       /* Set class to begin searching. */
  7923.       super_expr = build_component_ref (UOBJC_SUPER_decl,
  7924.                     get_identifier ("class"));
  7925.  
  7926.       if (TREE_CODE (implementation_context) == CLASS_IMPLEMENTATION_TYPE)
  7927.     {
  7928.       /* [_cls, __cls]Super are "pre-built" in
  7929.          synth_forward_declarations.  */
  7930.  
  7931.       super_expr = build_modify_expr (super_expr, NOP_EXPR,
  7932.                       ((TREE_CODE (method_context)
  7933.                         == INSTANCE_METHOD_DECL)
  7934.                        ? ucls_super_ref
  7935.                        : uucls_super_ref));
  7936.     }
  7937.  
  7938.       else
  7939.     /* We have a category. */
  7940.     {
  7941.       tree super_name = CLASS_SUPER_NAME (implementation_template);
  7942.       tree super_class;
  7943.  
  7944.       if (!super_name)  /* Barf if super used in a category of Object. */
  7945.         {
  7946.           error ("no super class declared in interface for `%s'",
  7947.             IDENTIFIER_POINTER (CLASS_NAME (implementation_template)));
  7948.           return error_mark_node;
  7949.         }
  7950.  
  7951.       {
  7952.         tree class_name = CLASS_NAME (implementation_template);
  7953.  
  7954.             if (0)
  7955.               {
  7956.                 /* #51856 */
  7957.                 tree this_class = get_class_reference (class_name);
  7958.                 super_class
  7959.                     = build_component_ref (build_indirect_ref (this_class, "->"),
  7960.                                            get_identifier ("super_class"));
  7961.               }
  7962.             else
  7963.               {
  7964.                 super_class = get_orig_class_reference (super_name);
  7965.               }
  7966.  
  7967.         if (TREE_CODE (method_context) == CLASS_METHOD_DECL)
  7968.           super_class
  7969.         = build_component_ref (build_indirect_ref (super_class, "->"),
  7970.                        get_identifier ("isa"));
  7971.       }
  7972.  
  7973.       /* cast! */
  7974.       super_class = build_c_cast (TREE_TYPE (ucls_super_ref), super_class);
  7975.       /* TREE_TYPE (super_class) = TREE_TYPE (ucls_super_ref);  */
  7976.       super_expr = build_modify_expr (super_expr, NOP_EXPR, super_class);
  7977.     }
  7978.  
  7979.       chainon (super_expr_list, build_tree_list (NULL_TREE, super_expr));
  7980.  
  7981.       super_expr = build_unary_op (ADDR_EXPR, UOBJC_SUPER_decl, 0);
  7982.       chainon (super_expr_list, build_tree_list (NULL_TREE, super_expr));
  7983.  
  7984.       return build_compound_expr (super_expr_list);
  7985.     }
  7986.   else
  7987.     {
  7988.       error ("[super ...] must appear in a method context");
  7989.       return error_mark_node;
  7990.     }
  7991. }
  7992.  
  7993. static tree
  7994. encode_method_def (func_decl)
  7995.       tree func_decl;
  7996. {
  7997.   tree parms;
  7998.   int stack_size;
  7999.   int max_parm_end = 0;
  8000.   char buffer[40];
  8001.   tree result;
  8002.  
  8003. #if defined (NEXT_SEMANTICS) || defined (NEXT_PDO)
  8004.   tree user_args;
  8005.   int i = 0;
  8006.  
  8007.   if (!flag_threeThreeMethodEncoding)
  8008.     {
  8009.       /* encode in, inout, bycopy, etc. before the return type */  
  8010.       user_args = METHOD_SEL_ARGS(method_context);
  8011.       encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (method_context)));
  8012.     }
  8013. #endif
  8014.   
  8015.   /* Return type. */
  8016.   encode_type (TREE_TYPE (TREE_TYPE (func_decl)),
  8017.            obstack_object_size (&util_obstack),
  8018.            OBJC_ENCODE_INLINE_DEFS);
  8019.  
  8020.   /* Stack size. */
  8021.   for (parms = DECL_ARGUMENTS (func_decl); parms;
  8022.        parms = TREE_CHAIN (parms))
  8023.     {
  8024.       int parm_end = forwarding_offset (parms);
  8025.  
  8026.       if (parm_end > 0)
  8027.     parm_end += TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (parms))) / BITS_PER_UNIT;
  8028.       else
  8029.     parm_end = -parm_end;
  8030.  
  8031.       if (max_parm_end < parm_end)
  8032.     max_parm_end = parm_end;
  8033.     }
  8034.  
  8035.   stack_size = max_parm_end - ( flag_next_runtime 
  8036.                    ? OBJC_FORWARDING_MIN_OFFSET 
  8037.                    : 0);
  8038.  
  8039.   sprintf (buffer, "%d", stack_size);
  8040.   obstack_grow (&util_obstack, buffer, strlen (buffer));
  8041.  
  8042.   /* Argument types. */
  8043.   for (parms = DECL_ARGUMENTS (func_decl); parms;
  8044.        parms = TREE_CHAIN (parms))
  8045.     {
  8046.       if (!flag_threeThreeMethodEncoding && i++ > 1)
  8047.     {
  8048.       encode_type_qualifiers (TREE_PURPOSE (TREE_TYPE (user_args)));
  8049.       user_args = TREE_CHAIN (user_args);
  8050.     }
  8051.  
  8052.       /* Type. */
  8053.       encode_type (TREE_TYPE (parms),
  8054.            obstack_object_size (&util_obstack),
  8055.            OBJC_ENCODE_INLINE_DEFS);
  8056.  
  8057.       /* Compute offset. */
  8058.       sprintf (buffer, "%d", forwarding_offset (parms));
  8059.  
  8060. #ifndef TARGET_SPARC     
  8061.       /* Indicate register. */
  8062.       if (offset_is_register && !flag_next_runtime)
  8063.     obstack_1grow (&util_obstack, '+');
  8064. #endif
  8065.  
  8066.       obstack_grow (&util_obstack, buffer, strlen (buffer));
  8067.     }
  8068.  
  8069.   obstack_1grow (&util_obstack, 0);    /* null terminate string */
  8070.   result = get_identifier (obstack_finish (&util_obstack));
  8071.   obstack_free (&util_obstack, util_firstobj);
  8072.   return result;
  8073. }
  8074.  
  8075. void
  8076. finish_method_def ()
  8077. {
  8078.   METHOD_ENCODING (method_context) = encode_method_def (current_function_decl);
  8079.  
  8080.   finish_function (0);
  8081.  
  8082.   /* Required to implement _msgSuper. This must be done AFTER finish_function,
  8083.      since the optimizer may find "may be used before set" errors.  */
  8084.   method_context = NULL_TREE;
  8085. }
  8086.  
  8087. int
  8088. lang_report_error_function (decl)
  8089.       tree decl;
  8090. {
  8091.   if (method_context)
  8092.     {
  8093.       fprintf (stderr, "In method `%s'\n",
  8094.            IDENTIFIER_POINTER (METHOD_SEL_NAME (method_context)));
  8095.       return 1;
  8096.     }
  8097.  
  8098.   else
  8099.     return 0;
  8100. }
  8101.  
  8102. static int
  8103. is_complex_decl (type)
  8104.      tree type;
  8105. {
  8106.   return (TREE_CODE (type) == ARRAY_TYPE
  8107.       || TREE_CODE (type) == FUNCTION_TYPE
  8108.       || (TREE_CODE (type) == POINTER_TYPE && ! IS_ID (type)));
  8109. }
  8110.  
  8111.  
  8112. /* Code to convert a decl node into text for a declaration in C.  */
  8113.  
  8114. static char tmpbuf[256];
  8115.  
  8116. static void
  8117. adorn_decl (decl, str)
  8118.      tree decl;
  8119.      char *str;
  8120. {
  8121.   enum tree_code code = TREE_CODE (decl);
  8122.  
  8123.   if (code == ARRAY_REF)
  8124.     {
  8125.       tree an_int_cst = TREE_OPERAND (decl, 1);
  8126.  
  8127.       if (an_int_cst && TREE_CODE (an_int_cst) == INTEGER_CST)
  8128.     sprintf (str + strlen (str), "[%d]", TREE_INT_CST_LOW (an_int_cst));
  8129.       else
  8130.     strcat (str, "[]");
  8131.     }
  8132.  
  8133.   else if (code == ARRAY_TYPE)
  8134.     {
  8135.       tree an_int_cst = TYPE_SIZE (decl);
  8136.       tree array_of = TREE_TYPE (decl);
  8137.  
  8138.       if (an_int_cst && TREE_CODE (an_int_cst) == INTEGER_TYPE)
  8139.     sprintf (str + strlen (str), "[%d]",
  8140.          (TREE_INT_CST_LOW (an_int_cst)
  8141.           / TREE_INT_CST_LOW (TYPE_SIZE (array_of))));
  8142.       else
  8143.     strcat (str, "[]");
  8144.     }
  8145.  
  8146.   else if (code == CALL_EXPR)
  8147.     {
  8148.       tree chain = TREE_PURPOSE (TREE_OPERAND (decl, 1));
  8149.  
  8150.       strcat (str, "(");
  8151.       while (chain)
  8152.     {
  8153.       gen_declaration (chain, str);
  8154.       chain = TREE_CHAIN (chain);
  8155.       if (chain)
  8156.         strcat (str, ", ");
  8157.     }
  8158.       strcat (str, ")");
  8159.     }
  8160.  
  8161.   else if (code == FUNCTION_TYPE)
  8162.     {
  8163.       tree chain  = TYPE_ARG_TYPES (decl); /* a list of types */
  8164.  
  8165.       strcat (str, "(");
  8166.       while (chain && TREE_VALUE (chain) != void_type_node)
  8167.     {
  8168.       gen_declaration (TREE_VALUE (chain), str);
  8169.       chain = TREE_CHAIN (chain);
  8170.       if (chain && TREE_VALUE (chain) != void_type_node)
  8171.         strcat (str, ", ");
  8172.     }
  8173.       strcat (str, ")");
  8174.     }
  8175.  
  8176.   else if (code == INDIRECT_REF)
  8177.     {
  8178.       strcpy (tmpbuf, "*");
  8179.       if (TREE_TYPE (decl) && TREE_CODE (TREE_TYPE (decl)) == TREE_LIST)
  8180.     {
  8181.       tree chain;
  8182.  
  8183.       for (chain = nreverse (copy_list (TREE_TYPE (decl)));
  8184.            chain;
  8185.            chain = TREE_CHAIN (chain))
  8186.         {
  8187.           if (TREE_CODE (TREE_VALUE (chain)) == IDENTIFIER_NODE)
  8188.         {
  8189.           strcat (tmpbuf, " ");
  8190.           strcat (tmpbuf, IDENTIFIER_POINTER (TREE_VALUE (chain)));
  8191.         }
  8192.         }
  8193.       if (str[0])
  8194.         strcat (tmpbuf, " ");
  8195.     }
  8196.       strcat (tmpbuf, str);
  8197.       strcpy (str, tmpbuf);
  8198.     }
  8199.  
  8200.   else if (code == POINTER_TYPE)
  8201.     {
  8202.       strcpy (tmpbuf, "*");
  8203.       if (TREE_READONLY (decl) || TYPE_VOLATILE (decl))
  8204.     {
  8205.       if (TREE_READONLY (decl))
  8206.         strcat (tmpbuf, " const");
  8207.       if (TYPE_VOLATILE (decl))
  8208.         strcat (tmpbuf, " volatile");
  8209.       if (str[0])
  8210.         strcat (tmpbuf, " ");
  8211.     }
  8212.       strcat (tmpbuf, str);
  8213.       strcpy (str, tmpbuf);
  8214.     }
  8215. }
  8216.  
  8217. static char *
  8218. gen_declarator (decl, buf, name)
  8219.      tree decl;
  8220.      char *buf;
  8221.      char *name;
  8222. {
  8223.   if (decl)
  8224.     {
  8225.       enum tree_code code = TREE_CODE (decl);
  8226.       char *str;
  8227.       tree op;
  8228.       int wrap = 0;
  8229.  
  8230.       switch (code)
  8231.     {
  8232.     case ARRAY_REF:
  8233.     case INDIRECT_REF:
  8234.     case CALL_EXPR:
  8235.       op = TREE_OPERAND (decl, 0);
  8236.  
  8237.       /* We have a pointer to a function or array...(*)(), (*)[] */
  8238.       if ((code == ARRAY_REF || code == CALL_EXPR)
  8239.           && op && TREE_CODE (op) == INDIRECT_REF)
  8240.         wrap = 1;
  8241.  
  8242.       str = gen_declarator (op, buf, name);
  8243.  
  8244.       if (wrap)
  8245.         {
  8246.           strcpy (tmpbuf, "(");
  8247.           strcat (tmpbuf, str);
  8248.           strcat (tmpbuf, ")");
  8249.           strcpy (str, tmpbuf);
  8250.         }
  8251.  
  8252.       adorn_decl (decl, str);
  8253.       break;
  8254.  
  8255.     case ARRAY_TYPE:
  8256.     case FUNCTION_TYPE:
  8257.     case POINTER_TYPE:
  8258.       strcpy (buf, name);
  8259.       str = buf;
  8260.  
  8261.       /* This clause is done iteratively rather than recursively. */
  8262.       do
  8263.         {
  8264.           op = (is_complex_decl (TREE_TYPE (decl))
  8265.             ? TREE_TYPE (decl) : NULL_TREE);
  8266.  
  8267.           adorn_decl (decl, str);
  8268.  
  8269.           /* We have a pointer to a function or array...(*)(), (*)[] */
  8270.           if (code == POINTER_TYPE
  8271.           && op && (TREE_CODE (op) == FUNCTION_TYPE
  8272.                 || TREE_CODE (op) == ARRAY_TYPE))
  8273.         {
  8274.           strcpy (tmpbuf, "(");
  8275.           strcat (tmpbuf, str);
  8276.           strcat (tmpbuf, ")");
  8277.           strcpy (str, tmpbuf);
  8278.         }
  8279.  
  8280.           decl = (is_complex_decl (TREE_TYPE (decl))
  8281.               ? TREE_TYPE (decl) : NULL_TREE);
  8282.         }
  8283.  
  8284.       while (decl && (code = TREE_CODE (decl)))
  8285.         ;
  8286.  
  8287.       break;
  8288.  
  8289.     case IDENTIFIER_NODE:
  8290.       /* Will only happen if we are processing a "raw" expr-decl. */
  8291.       strcpy (buf, IDENTIFIER_POINTER (decl));
  8292.       return buf;
  8293.     }
  8294.  
  8295.       return str;
  8296.     }
  8297.  
  8298.   else
  8299.     /* We have an abstract declarator or a _DECL node. */
  8300.     {
  8301.       strcpy (buf, name);
  8302.       return buf;
  8303.     }
  8304. }
  8305.  
  8306. static void
  8307. gen_declspecs (declspecs, buf, raw)
  8308.      tree declspecs;
  8309.      char *buf;
  8310.      int raw;
  8311. {
  8312.   if (raw)
  8313.     {
  8314.       tree chain;
  8315.  
  8316.       for (chain = nreverse (copy_list (declspecs));
  8317.        chain; chain = TREE_CHAIN (chain))
  8318.     {
  8319.       tree aspec = TREE_VALUE (chain);
  8320.  
  8321.       if (TREE_CODE (aspec) == IDENTIFIER_NODE)
  8322.         strcat (buf, IDENTIFIER_POINTER (aspec));
  8323.       else if (TREE_CODE (aspec) == RECORD_TYPE)
  8324.         {
  8325.           if (TYPE_NAME (aspec))
  8326.         {
  8327.           tree protocol_list = TYPE_PROTOCOL_LIST (aspec);
  8328.  
  8329.           if (! TREE_STATIC_TEMPLATE (aspec))
  8330.             strcat (buf, "struct ");
  8331.           strcat (buf, IDENTIFIER_POINTER (TYPE_NAME (aspec)));
  8332.  
  8333.           if (protocol_list)
  8334.             {
  8335.               tree chain = protocol_list;
  8336.  
  8337.               strcat (buf, " <");
  8338.               while (chain)
  8339.             {
  8340.               strcat (buf,
  8341.                   IDENTIFIER_POINTER
  8342.                   (PROTOCOL_NAME (TREE_VALUE (chain))));
  8343.               chain = TREE_CHAIN (chain);
  8344.               if (chain)
  8345.                 strcat (buf, ", ");
  8346.             }
  8347.               strcat (buf, ">");
  8348.             }
  8349.         }
  8350.  
  8351.           else
  8352.         strcat (buf, "untagged struct");
  8353.         }
  8354.  
  8355.       else if (TREE_CODE (aspec) == UNION_TYPE)
  8356.         {
  8357.           if (TYPE_NAME (aspec))
  8358.         {
  8359.           if (! TREE_STATIC_TEMPLATE (aspec))
  8360.             strcat (buf, "union ");
  8361.           strcat (buf, IDENTIFIER_POINTER (TYPE_NAME (aspec)));
  8362.         }
  8363.           else
  8364.         strcat (buf, "untagged union");
  8365.         }
  8366.  
  8367.       else if (TREE_CODE (aspec) == ENUMERAL_TYPE)
  8368.         {
  8369.           if (TYPE_NAME (aspec))
  8370.         {
  8371.           if (! TREE_STATIC_TEMPLATE (aspec))
  8372.             strcat (buf, "enum ");
  8373.           strcat (buf, IDENTIFIER_POINTER (TYPE_NAME (aspec)));
  8374.         }
  8375.           else
  8376.         strcat (buf, "untagged enum");
  8377.         }
  8378.  
  8379.       else if (TREE_CODE (aspec) == TYPE_DECL && DECL_NAME (aspec))
  8380.         strcat (buf, IDENTIFIER_POINTER (DECL_NAME (aspec)));
  8381.  
  8382.       else if (IS_ID (aspec))
  8383.         {
  8384.           tree protocol_list = TYPE_PROTOCOL_LIST (aspec);
  8385.  
  8386.           strcat (buf, "id");
  8387.           if (protocol_list)
  8388.         {
  8389.           tree chain = protocol_list;
  8390.  
  8391.           strcat (buf, " <");
  8392.           while (chain)
  8393.             {
  8394.               strcat (buf,
  8395.                   IDENTIFIER_POINTER
  8396.                   (PROTOCOL_NAME (TREE_VALUE (chain))));
  8397.               chain = TREE_CHAIN (chain);
  8398.               if (chain)
  8399.             strcat (buf, ", ");
  8400.             }
  8401.           strcat (buf, ">");
  8402.         }
  8403.         }
  8404.       if (TREE_CHAIN (chain))
  8405.         strcat (buf, " ");
  8406.     }
  8407.     }
  8408.   else
  8409.     {
  8410.     /* type qualifiers */
  8411.  
  8412.     if (TREE_READONLY (declspecs))
  8413.       strcat (buf, "const ");
  8414.     if (TYPE_VOLATILE (declspecs))
  8415.       strcat (buf, "volatile ");
  8416.  
  8417.     switch (TREE_CODE (declspecs))
  8418.       {
  8419.     /* type specifiers */
  8420.  
  8421.       case INTEGER_TYPE:    /* signed integer types */
  8422.     declspecs = TYPE_MAIN_VARIANT (declspecs);
  8423.  
  8424.         if (declspecs == short_integer_type_node) /* 's' */
  8425.           strcat (buf, "short int ");
  8426.         else if (declspecs == integer_type_node) /* 'i' */
  8427.           strcat (buf, "int ");
  8428.         else if (declspecs == long_integer_type_node) /* 'l' */
  8429.           strcat (buf, "long int ");
  8430.     else if (declspecs == long_long_integer_type_node) /* 'l' */
  8431.       strcat (buf, "long long int ");
  8432.         else if (declspecs == signed_char_type_node /* 'c' */
  8433.                || declspecs == char_type_node)
  8434.           strcat (buf, "char ");
  8435.  
  8436.         /* unsigned integer types */
  8437.  
  8438.         else if (declspecs == short_unsigned_type_node)    /* 'S' */
  8439.           strcat (buf, "unsigned short ");
  8440.         else if (declspecs == unsigned_type_node) /* 'I' */
  8441.           strcat (buf, "unsigned int ");
  8442.         else if (declspecs == long_unsigned_type_node) /* 'L' */
  8443.           strcat (buf, "unsigned long ");
  8444.     else if (declspecs == long_long_unsigned_type_node) /* 'L' */
  8445.       strcat (buf, "unsigned long long ");
  8446.         else if (declspecs == unsigned_char_type_node) /* 'C' */
  8447.           strcat (buf, "unsigned char ");
  8448.     break;
  8449.  
  8450.       case REAL_TYPE:        /* floating point types */
  8451.         declspecs = TYPE_MAIN_VARIANT (declspecs);
  8452.  
  8453.         if (declspecs == float_type_node) /* 'f' */
  8454.           strcat (buf, "float ");
  8455.         else if (declspecs == double_type_node)    /* 'd' */
  8456.           strcat (buf, "double ");
  8457.     else if (declspecs == long_double_type_node) /* 'd' */
  8458.           strcat (buf, "long double ");
  8459.     break;
  8460.  
  8461.       case RECORD_TYPE:
  8462.     if (TYPE_NAME (declspecs)
  8463.         && TREE_CODE (TYPE_NAME (declspecs)) == IDENTIFIER_NODE)
  8464.       {
  8465.         tree protocol_list = TYPE_PROTOCOL_LIST (declspecs);
  8466.  
  8467.         if (! TREE_STATIC_TEMPLATE (declspecs))
  8468.           strcat (buf, "struct ");
  8469.         strcat (buf, IDENTIFIER_POINTER (TYPE_NAME (declspecs)));
  8470.         if (protocol_list)
  8471.           {
  8472.         tree chain = protocol_list;
  8473.  
  8474.         strcat (buf, " <");
  8475.         while (chain)
  8476.           {
  8477.             strcat (buf,
  8478.                 IDENTIFIER_POINTER
  8479.                 (PROTOCOL_NAME (TREE_VALUE (chain))));
  8480.             chain = TREE_CHAIN (chain);
  8481.             if (chain)
  8482.               strcat (buf, ", ");
  8483.           }
  8484.  
  8485.         strcat (buf, ">");
  8486.           }
  8487.       }
  8488.     else
  8489.       strcat (buf, "untagged struct");
  8490.  
  8491.     strcat (buf, " ");
  8492.     break;
  8493.  
  8494.       case UNION_TYPE:
  8495.     if (TYPE_NAME (declspecs)
  8496.         && TREE_CODE (TYPE_NAME (declspecs)) == IDENTIFIER_NODE)
  8497.       {
  8498.         strcat (buf, "union ");
  8499.         strcat (buf, IDENTIFIER_POINTER (TYPE_NAME (declspecs)));
  8500.         strcat (buf, " ");
  8501.       }
  8502.     else
  8503.       strcat (buf, "untagged union ");
  8504.     break;
  8505.  
  8506.       case ENUMERAL_TYPE:
  8507.     if (TYPE_NAME (declspecs)
  8508.         && TREE_CODE (TYPE_NAME (declspecs)) == IDENTIFIER_NODE)
  8509.       {
  8510.         strcat (buf, "enum ");
  8511.         strcat (buf, IDENTIFIER_POINTER (TYPE_NAME (declspecs)));
  8512.         strcat (buf, " ");
  8513.       }
  8514.     else
  8515.       strcat (buf, "untagged enum ");
  8516.     break;
  8517.  
  8518.       case VOID_TYPE:
  8519.     strcat (buf, "void ");
  8520.         break;
  8521.  
  8522.       case POINTER_TYPE:
  8523.     {
  8524.       tree protocol_list = TYPE_PROTOCOL_LIST (declspecs);
  8525.  
  8526.       strcat (buf, "id");
  8527.       if (protocol_list)
  8528.         {
  8529.           tree chain = protocol_list;
  8530.  
  8531.           strcat (buf, " <");
  8532.           while (chain)
  8533.         {
  8534.           strcat (buf, IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (chain))));
  8535.           chain = TREE_CHAIN (chain);
  8536.           if (chain)
  8537.             strcat (buf, ", ");
  8538.         }
  8539.           strcat (buf, ">");
  8540.         }
  8541.     }
  8542.       }
  8543.     }
  8544. }
  8545.  
  8546. static char *
  8547. gen_declaration (atype_or_adecl, buf)
  8548.      tree atype_or_adecl;
  8549.      char *buf;
  8550. {
  8551.   char declbuf[256];
  8552.  
  8553.   if (TREE_CODE (atype_or_adecl) == TREE_LIST)
  8554.     {
  8555.       tree declspecs;    /* "identifier_node", "record_type" */
  8556.       tree declarator;    /* "array_ref", "indirect_ref", "call_expr"... */
  8557.  
  8558.       /* We have a "raw", abstract declarator (typename). */
  8559.       declarator = TREE_VALUE (atype_or_adecl);
  8560.       declspecs  = TREE_PURPOSE (atype_or_adecl);
  8561.  
  8562.       gen_declspecs (declspecs, buf, 1);
  8563.       if (declarator)
  8564.     {
  8565.       strcat (buf, " ");
  8566.       strcat (buf, gen_declarator (declarator, declbuf, ""));
  8567.     }
  8568.     }
  8569.  
  8570.   else
  8571.     {
  8572.       tree atype;
  8573.       tree declspecs;    /* "integer_type", "real_type", "record_type"... */
  8574.       tree declarator;    /* "array_type", "function_type", "pointer_type". */
  8575.  
  8576.       if (TREE_CODE (atype_or_adecl) == FIELD_DECL
  8577.       || TREE_CODE (atype_or_adecl) == PARM_DECL
  8578.       || TREE_CODE (atype_or_adecl) == FUNCTION_DECL)
  8579.     atype = TREE_TYPE (atype_or_adecl);
  8580.       else
  8581.     /* Assume we have a *_type node. */
  8582.     atype = atype_or_adecl;
  8583.  
  8584.       if (is_complex_decl (atype))
  8585.     {
  8586.       tree chain;
  8587.  
  8588.       /* Get the declaration specifier; it is at the end of the list. */
  8589.       declarator = chain = atype;
  8590.       do
  8591.         chain = TREE_TYPE (chain); /* not TREE_CHAIN (chain); */
  8592.       while (is_complex_decl (chain));
  8593.       declspecs = chain;
  8594.     }
  8595.  
  8596.       else
  8597.     {
  8598.       declspecs = atype;
  8599.       declarator = NULL_TREE;
  8600.     }
  8601.  
  8602.       gen_declspecs (declspecs, buf, 0);
  8603.  
  8604.       if (TREE_CODE (atype_or_adecl) == FIELD_DECL
  8605.       || TREE_CODE (atype_or_adecl) == PARM_DECL
  8606.       || TREE_CODE (atype_or_adecl) == FUNCTION_DECL)
  8607.     {
  8608.       char *decl_name = (DECL_NAME (atype_or_adecl)
  8609.                  ? IDENTIFIER_POINTER (DECL_NAME (atype_or_adecl))
  8610.                  : "");
  8611.  
  8612.       if (declarator)
  8613.         {
  8614.           strcat (buf, " ");
  8615.           strcat (buf, gen_declarator (declarator, declbuf, decl_name));
  8616.         }
  8617.  
  8618.       else if (decl_name[0])
  8619.         {
  8620.           strcat (buf, " ");
  8621.           strcat (buf, decl_name);
  8622.         }
  8623.     }
  8624.       else if (declarator)
  8625.     {
  8626.       strcat (buf, " ");
  8627.       strcat (buf, gen_declarator (declarator, declbuf, ""));
  8628.     }
  8629.     }
  8630.  
  8631.   return buf;
  8632. }
  8633.  
  8634. #define RAW_TYPESPEC(meth) (TREE_VALUE (TREE_PURPOSE (TREE_TYPE (meth))))
  8635.  
  8636. static char *
  8637. gen_method_decl (method, buf)
  8638.      tree method;
  8639.      char *buf;
  8640. {
  8641.   tree chain;
  8642.  
  8643.   if (RAW_TYPESPEC (method) != objc_object_reference)
  8644.     {
  8645.       strcpy (buf, "(");
  8646.       gen_declaration (TREE_TYPE (method), buf);
  8647.       strcat (buf, ")");
  8648.     }
  8649.  
  8650.   chain = METHOD_SEL_ARGS (method);
  8651.   if (chain)
  8652.     {
  8653.       /* We have a chain of keyword_decls. */
  8654.       do
  8655.         {
  8656.       if (KEYWORD_KEY_NAME (chain))
  8657.         strcat (buf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
  8658.  
  8659.       strcat (buf, ":");
  8660.       if (RAW_TYPESPEC (chain) != objc_object_reference)
  8661.         {
  8662.           strcat (buf, "(");
  8663.           gen_declaration (TREE_TYPE (chain), buf);
  8664.           strcat (buf, ")");
  8665.         }
  8666.       strcat (buf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
  8667.       if ((chain = TREE_CHAIN (chain)))
  8668.         strcat (buf, " ");
  8669.         }
  8670.       while (chain);
  8671.  
  8672.       if (METHOD_ADD_ARGS (method) == (tree)1)
  8673.         strcat (buf, ", ...");
  8674.       else if (METHOD_ADD_ARGS (method))
  8675.         {
  8676.       /* We have a tree list node as generate by get_parm_info.  */
  8677.       chain  = TREE_PURPOSE (METHOD_ADD_ARGS (method));
  8678.  
  8679.           /* Know we have a chain of parm_decls. */
  8680.           while (chain)
  8681.             {
  8682.           strcat (buf, ", ");
  8683.           gen_declaration (chain, buf);
  8684.           chain = TREE_CHAIN (chain);
  8685.             }
  8686.     }
  8687.     }
  8688.  
  8689.   else
  8690.     /* We have a unary selector. */
  8691.     strcat (buf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
  8692.  
  8693.   return buf;
  8694. }
  8695.  
  8696. /* Debug info.  */
  8697.  
  8698. static void
  8699. dump_interface (fp, chain)
  8700.      FILE *fp;
  8701.      tree chain;
  8702. {
  8703.   char *buf = (char *)xmalloc (256);
  8704.   char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
  8705.   tree ivar_decls = CLASS_RAW_IVARS (chain);
  8706.   tree nst_methods = CLASS_NST_METHODS (chain);
  8707.   tree cls_methods = CLASS_CLS_METHODS (chain);
  8708.  
  8709.   fprintf (fp, "\n@interface %s", my_name);
  8710.  
  8711.   if (CLASS_SUPER_NAME (chain))
  8712.     {
  8713.       char *super_name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
  8714.       fprintf (fp, " : %s\n", super_name);
  8715.     }
  8716.   else
  8717.     fprintf (fp, "\n");
  8718.  
  8719.   if (ivar_decls)
  8720.     {
  8721.       fprintf (fp, "{\n");
  8722.       do
  8723.     {
  8724.       bzero (buf, 256);
  8725.       fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls, buf));
  8726.       ivar_decls = TREE_CHAIN (ivar_decls);
  8727.     }
  8728.       while (ivar_decls);
  8729.       fprintf (fp, "}\n");
  8730.     }
  8731.  
  8732.   while (nst_methods)
  8733.     {
  8734.       bzero (buf, 256);
  8735.       fprintf (fp, "- %s;\n", gen_method_decl (nst_methods, buf));
  8736.       nst_methods = TREE_CHAIN (nst_methods);
  8737.     }
  8738.  
  8739.   while (cls_methods)
  8740.     {
  8741.       bzero (buf, 256);
  8742.       fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods, buf));
  8743.       cls_methods = TREE_CHAIN (cls_methods);
  8744.     }
  8745.   fprintf (fp, "\n@end");
  8746. }
  8747.  
  8748. static void
  8749. init_objc ()
  8750. {
  8751.   /* Add the special tree codes of Objective C to the tables.  */
  8752.  
  8753. #ifdef OBJCPLUS
  8754. #define LAST_CODE LAST_CPLUS_TREE_CODE
  8755. #else
  8756. #define LAST_CODE LAST_AND_UNUSED_TREE_CODE
  8757. #endif
  8758.  
  8759.   gcc_obstack_init (&util_obstack);
  8760.   util_firstobj = (char *) obstack_finish (&util_obstack);
  8761.  
  8762.   tree_code_type
  8763.     = (char **) xrealloc (tree_code_type,
  8764.               sizeof (char *) * LAST_OBJC_TREE_CODE);
  8765.   tree_code_length
  8766.     = (int *) xrealloc (tree_code_length,
  8767.             sizeof (int) * LAST_OBJC_TREE_CODE);
  8768.   tree_code_name
  8769.     = (char **) xrealloc (tree_code_name,
  8770.               sizeof (char *) * LAST_OBJC_TREE_CODE);
  8771.   bcopy ((char *) objc_tree_code_type,
  8772.      (char *) (tree_code_type + (int) LAST_CODE),
  8773.      (((int) LAST_OBJC_TREE_CODE - (int) LAST_CODE)
  8774.       * sizeof (char *)));
  8775.   bcopy ((char *) objc_tree_code_length,
  8776.      (char *) (tree_code_length + (int) LAST_CODE),
  8777.      (((int) LAST_OBJC_TREE_CODE - (int) LAST_CODE)
  8778.       * sizeof (int)));
  8779.   bcopy ((char *) objc_tree_code_name,
  8780.      (char *) (tree_code_name + (int) LAST_CODE),
  8781.      (((int) LAST_OBJC_TREE_CODE - (int) LAST_CODE)
  8782.       * sizeof (char *)));
  8783.  
  8784.   errbuf = (char *)xmalloc (BUFSIZE);
  8785.   hash_init ();
  8786.   synth_module_prologue ();
  8787. }
  8788.  
  8789. static void
  8790. finish_objc ()
  8791. {
  8792.   struct imp_entry *impent;
  8793.   tree chain;
  8794.   /* The internally generated initializers appear to have missing braces.
  8795.      Don't warn about this.  */
  8796.   int save_warn_missing_braces = warn_missing_braces;
  8797.   warn_missing_braces = 0;
  8798.  
  8799.   if (objc_implementation_context)
  8800.     {
  8801.       warning ("`@end' missing in implementation context");
  8802.       finish_class (implementation_context);
  8803.       objc_ivar_chain = NULL_TREE;
  8804.       objc_implementation_context = NULL_TREE;
  8805.     }
  8806.  
  8807.   generate_forward_declaration_to_string_table ();
  8808.  
  8809. #ifdef OBJC_PROLOGUE
  8810.   OBJC_PROLOGUE;
  8811. #endif
  8812.  
  8813.   if (implementation_context || class_names_chain
  8814.        || meth_var_names_chain || meth_var_types_chain
  8815.        || sel_ref_chain || obj_def_chain)
  8816.     {
  8817.         no_objc = 0; /* This file contains some Objective-C */
  8818.         generate_objc_symtab_decl ();
  8819.     }
  8820.   for (impent = imp_list; impent; impent = impent->next)
  8821.     {
  8822.       implementation_context = impent->imp_context;
  8823.       implementation_template = impent->imp_template;
  8824.  
  8825.       UOBJC_CLASS_decl = impent->class_decl;
  8826.       UOBJC_METACLASS_decl = impent->meta_decl;
  8827.  
  8828.       if (TREE_CODE (implementation_context) == CLASS_IMPLEMENTATION_TYPE)
  8829.     {
  8830.       /* all of the following reference the string pool...  */
  8831.       generate_ivar_lists ();
  8832.       generate_dispatch_tables ();
  8833.       generate_shared_structures ();
  8834.     }
  8835.       else
  8836.     {
  8837.       generate_dispatch_tables ();
  8838.       generate_category (implementation_context);
  8839.     }
  8840.     }
  8841.  
  8842.   /* If we are using an array of selectors, we must always
  8843.      finish up the array decl even if no selectors were used.  */
  8844.   if (! flag_next_runtime || sel_ref_chain)
  8845.     build_selector_translation_table ();
  8846.  
  8847.   if (protocol_chain)
  8848.     generate_protocols ();
  8849.  
  8850.   if (implementation_context || class_names_chain
  8851.       || meth_var_names_chain || meth_var_types_chain || sel_ref_chain
  8852.       || obj_count || proto_count)
  8853.     {
  8854.       /* Arrange for Objc data structures to be initialized at run time.  */
  8855.       char *init_name = build_module_descriptor ();
  8856.       if (init_name)
  8857.     assemble_constructor (init_name);
  8858.     }
  8859.  
  8860.   /* Dump the class references.  This forces the appropriate classes
  8861.      to be linked into the executable image, preserving unix archive
  8862.      semantics.  This can be removed when we move to a more dynamically
  8863.      linked environment.  */
  8864.  
  8865.   for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
  8866.     {
  8867.       handle_class_ref (chain);
  8868.       if (TREE_PURPOSE (chain))
  8869.     generate_classref_translation_entry (chain);
  8870.     }
  8871.  
  8872.   for (impent = imp_list; impent; impent = impent->next)
  8873.     handle_impent (impent);
  8874.  
  8875.   /* Dump the string table last. */
  8876.  
  8877.   generate_strings ();
  8878.  
  8879.   if (flag_gen_declaration)
  8880.     {
  8881.       add_class (implementation_context);
  8882.       dump_interface (gen_declaration_file, implementation_context);
  8883.     }
  8884.  
  8885.   if (warn_selector)
  8886.     {
  8887.       int slot;
  8888.       hash hsh;
  8889.  
  8890.       /* Run through the selector hash tables and print a warning for any
  8891.          selector which has multiple methods. */
  8892.  
  8893.       for (slot = 0; slot < SIZEHASHTABLE; slot++)
  8894.     for (hsh = cls_method_hash_list[slot]; hsh; hsh = hsh->next)
  8895.       if (hsh->list)
  8896.         {
  8897.           tree meth = hsh->key;
  8898.           char type = (TREE_CODE (meth) == INSTANCE_METHOD_DECL
  8899.                ? '-' : '+');
  8900.           attr loop;
  8901.  
  8902.           warning ("potential selector conflict for method `%s'",
  8903.                IDENTIFIER_POINTER (METHOD_SEL_NAME (meth)));
  8904.           warn_with_method ("found", type, meth);
  8905.           for (loop = hsh->list; loop; loop = loop->next)
  8906.         warn_with_method ("found", type, loop->value);
  8907.         }
  8908.  
  8909.       for (slot = 0; slot < SIZEHASHTABLE; slot++)
  8910.     for (hsh = nst_method_hash_list[slot]; hsh; hsh = hsh->next)
  8911.       if (hsh->list)
  8912.         {
  8913.           tree meth = hsh->key;
  8914.           char type = (TREE_CODE (meth) == INSTANCE_METHOD_DECL
  8915.                ? '-' : '+');
  8916.           attr loop;
  8917.  
  8918.           warning ("potential selector conflict for method `%s'",
  8919.                IDENTIFIER_POINTER (METHOD_SEL_NAME (meth)));
  8920.           warn_with_method ("found", type, meth);
  8921.           for (loop = hsh->list; loop; loop = loop->next)
  8922.         warn_with_method ("found", type, loop->value);
  8923.         }
  8924.     }
  8925.  
  8926.   warn_missing_braces = save_warn_missing_braces;
  8927. }
  8928.  
  8929. /* Subroutines of finish_objc.  */
  8930.  
  8931. static void
  8932. generate_classref_translation_entry (chain)
  8933.     tree chain;
  8934. {
  8935.   tree expr, name, decl_specs, decl, sc_spec;
  8936.   tree type;
  8937.  
  8938.   type = TREE_TYPE (TREE_PURPOSE (chain));
  8939.  
  8940.   expr = add_objc_string (TREE_VALUE (chain), class_names);
  8941.   expr = build_c_cast (type, expr);
  8942.  
  8943.   name = DECL_NAME (TREE_PURPOSE (chain));
  8944.  
  8945.   sc_spec = build_tree_list (NULL_TREE, ridpointers[(int) RID_STATIC]);
  8946.  
  8947.   /* static struct objc_class * _OBJC_CLASS_REFERENCES_n = ...; */
  8948.   decl_specs = tree_cons (NULL_TREE, type, sc_spec);
  8949.  
  8950.   /* The decl that is returned from start_decl is the one that we
  8951.      forward declared in build_class_reference.  */
  8952.   decl = start_decl (name, decl_specs, 1);
  8953.   end_temporary_allocation ();
  8954.   finish_decl (decl, expr, NULL_TREE);
  8955.   return;
  8956. }
  8957.  
  8958. static void
  8959. handle_class_ref (chain)
  8960.      tree chain;
  8961. {
  8962.   char *name = IDENTIFIER_POINTER (TREE_VALUE (chain));
  8963.   tree decl;
  8964.   char *string = (char *) alloca (strlen (name) + 30);
  8965.   tree exp;
  8966.  
  8967.   sprintf (string, "*%sobjc_class_name_%s", (flag_next_runtime ?".":"__"), name);
  8968.  
  8969. #ifdef DECLARE_UNRESOLVED_REFERENCE
  8970.   if (flag_next_runtime)
  8971.     {
  8972.       DECLARE_UNRESOLVED_REFERENCE(string);
  8973.       return;
  8974.     }
  8975. #endif
  8976.  
  8977.   /* Make a decl for this name, so we can use its address in a tree.  */
  8978.   decl = build_decl (VAR_DECL, get_identifier (string), char_type_node);
  8979.   DECL_EXTERNAL (decl) = 1;
  8980.   TREE_PUBLIC (decl) = 1;
  8981.  
  8982.   pushdecl (decl);
  8983.   rest_of_decl_compilation (decl, 0, 0, 0);
  8984.  
  8985.   exp = build1 (ADDR_EXPR, string_type_node, decl);
  8986.  
  8987.   /* Select text segment */
  8988.   text_section ();
  8989.  
  8990.   /* Align the section properly.  */
  8991.   assemble_constant_align (exp);
  8992.  
  8993.   /* Inform the assembler about this new external thing.  */
  8994.   assemble_external (decl);
  8995.  
  8996.   /* Output a constant to reference this address.  */
  8997.   output_constant (exp, int_size_in_bytes (string_type_node));
  8998. }
  8999.  
  9000. static void
  9001. handle_impent (impent)
  9002.      struct imp_entry *impent;
  9003. {
  9004.   char *string;
  9005.   implementation_context = impent->imp_context;
  9006.   implementation_template = impent->imp_template;
  9007.  
  9008.   if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION_TYPE)
  9009.     {
  9010.       char *class_name = IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
  9011.       
  9012.       string = (char *) malloc (strlen (class_name) + 30);
  9013.       sprintf (string, "*%sobjc_class_name_%s",
  9014.            (flag_next_runtime ? "." : "__"), class_name);
  9015.     }
  9016.   else if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION_TYPE)
  9017.     {
  9018.       char *class_name = IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context));
  9019.       char *class_super_name
  9020.     = IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent->imp_context));
  9021.       
  9022.       string = (char *) malloc (strlen (class_name)
  9023.                 + strlen (class_super_name) + 30);
  9024.  
  9025.       /* Do the same for categories.  Even though no references to these
  9026.      symbols are generated automatically by the compiler, it gives
  9027.      you a handle to pull them into an archive by hand. */
  9028.       sprintf (string, "*%sobjc_category_name_%s_%s",
  9029.            (flag_next_runtime ? "." : "__"), class_name, class_super_name);
  9030.     }
  9031.   else
  9032.     return;
  9033.  
  9034. #ifdef DECLARE_CLASS_REFERENCE
  9035.   if (flag_next_runtime)
  9036.     {
  9037.       DECLARE_CLASS_REFERENCE (string);
  9038.     }
  9039. #else
  9040.   text_section ();
  9041.   assemble_global (string);
  9042.   assemble_align (UNITS_PER_WORD);
  9043.   assemble_label (string);
  9044.   assemble_zeros (UNITS_PER_WORD);
  9045. #endif
  9046.  
  9047.   free (string);
  9048. }
  9049.  
  9050. #ifdef DEBUG
  9051.  
  9052. static void
  9053. objc_debug (fp)
  9054.      FILE *fp;
  9055. {
  9056.   char *buf = (char *)xmalloc (256);
  9057.  
  9058.   {                /* dump function prototypes */
  9059.     tree loop = UOBJC_MODULES_decl;
  9060.  
  9061.     fprintf (fp, "\n\nfunction prototypes:\n");
  9062.     while (loop)
  9063.       {
  9064.     if (TREE_CODE (loop) == FUNCTION_DECL && DECL_INITIAL (loop))
  9065.       {
  9066.         /* We have a function definition: generate prototype. */
  9067.             bzero (errbuf, BUFSIZE);
  9068.         gen_declaration (loop, errbuf);
  9069.         fprintf (fp, "%s;\n", errbuf);
  9070.       }
  9071.     loop = TREE_CHAIN (loop);
  9072.       }
  9073.   }
  9074.   {
  9075.     /* Dump global chains. */
  9076.     tree loop;
  9077.     int i, index = 0, offset = 0;
  9078.     hash hashlist;
  9079.  
  9080.     for (i = 0; i < SIZEHASHTABLE; i++)
  9081.       {
  9082.     if (hashlist = nst_method_hash_list[i])
  9083.       {
  9084.         fprintf (fp, "\n\nnst_method_hash_list[%d]:\n", i);
  9085.         do
  9086.           {
  9087.         bzero (buf, 256);
  9088.         fprintf (fp, "-%s;\n", gen_method_decl (hashlist->key, buf));
  9089.         hashlist = hashlist->next;
  9090.           }
  9091.         while (hashlist);
  9092.       }
  9093.       }
  9094.  
  9095.     for (i = 0; i < SIZEHASHTABLE; i++)
  9096.       {
  9097.     if (hashlist = cls_method_hash_list[i])
  9098.       {
  9099.         fprintf (fp, "\n\ncls_method_hash_list[%d]:\n", i);
  9100.         do
  9101.           {
  9102.         bzero (buf, 256);
  9103.         fprintf (fp, "-%s;\n", gen_method_decl (hashlist->key, buf));
  9104.         hashlist = hashlist->next;
  9105.           }
  9106.         while (hashlist);
  9107.       }
  9108.       }
  9109.  
  9110.     fprintf (fp, "\nsel_refdef_chain:\n");
  9111.     for (loop = sel_refdef_chain; loop; loop = TREE_CHAIN (loop))
  9112.       {
  9113.     fprintf (fp, "(index: %4d offset: %4d) %s\n", index, offset,
  9114.          IDENTIFIER_POINTER (TREE_VALUE (loop)));
  9115.     index++;
  9116.     /* add one for the '\0' character */
  9117.     offset += IDENTIFIER_LENGTH (TREE_VALUE (loop)) + 1;
  9118.       }
  9119.  
  9120.     fprintf (fp, "\n (max_selector_index: %4d.\n", max_selector_index);
  9121.   }
  9122. }
  9123. #endif
  9124.  
  9125. #ifndef OBJCPLUS
  9126.  
  9127. void
  9128. print_lang_statistics ()
  9129. {
  9130. }
  9131.  
  9132. #endif
  9133.