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