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