home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / objc-actions.c < prev    next >
C/C++ Source or Header  |  1992-05-27  |  190KB  |  6,846 lines

  1. /* Implement classes and message passing for Objective C.
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.    Author: 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  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_SELS_R_INTS, OBJC_SELS_R_STRUCT_PTRS
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include "config.h"
  44. #include "tree.h"
  45. #include "c-tree.h"
  46. #include "c-parse.h"
  47. #include "flags.h"
  48. #include "objc-actions.h"
  49. #include "input.h"
  50.  
  51. int doing_objc_thang;
  52.  
  53. /* Define the special tree codes that we use.  */
  54.  
  55. /* Table indexed by tree code giving a string containing a character
  56.    classifying the tree code.  Possibilities are
  57.    t, d, s, c, r, <, 1 and 2.  See objc-tree.def for details.  */
  58.  
  59. #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
  60.  
  61. char *objc_tree_code_type[] = {
  62.   "x",
  63. #include "objc-tree.def"
  64. };
  65. #undef DEFTREECODE
  66.  
  67. /* Table indexed by tree code giving number of expression
  68.    operands beyond the fixed part of the node structure.
  69.    Not used for types or decls.  */
  70.  
  71. #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
  72.  
  73. int objc_tree_code_length[] = {
  74.   0,
  75. #include "objc-tree.def"
  76. };
  77. #undef DEFTREECODE
  78.  
  79. /* Names of tree components.
  80.    Used for printing out the tree and error messages.  */
  81. #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
  82.  
  83. char *objc_tree_code_name[] = {
  84.   "@@dummy",
  85. #include "objc-tree.def"
  86. };
  87. #undef DEFTREECODE
  88.  
  89. /* for encode_method_def */
  90. #include "rtl.h"
  91.  
  92. #define OBJC_VERSION    5
  93. #define PROTOCOL_VERSION 2
  94.  
  95. #define NULLT    (tree) 0
  96.  
  97. #define OBJC_SELS_R_STRUCT_PTRS
  98.  
  99. #define OBJC_ENCODE_INLINE_DEFS     0
  100. #define OBJC_ENCODE_DONT_INLINE_DEFS    1
  101.  
  102. #define OBJC_CLASS_REFS
  103.  
  104. /*** Private Interface (procedures) ***/
  105.  
  106. /* code generation */
  107.  
  108. static void synth_module_prologue ();
  109. static void build_module_descriptor ();
  110. static tree   init_module_descriptor ();
  111. static void build_module_entry ();
  112. static void generate_strings ();
  113. static void build_selector_translation_table ();
  114. static tree build_ivar_chain ();
  115.  
  116. static tree build_ivar_template ();
  117. static tree build_method_template ();
  118. static tree build_private_template ();
  119. static void build_class_template ();
  120. static void build_category_template ();
  121. static tree build_super_template ();
  122. static void build_class_ref_template ();
  123. static tree build_category_initializer ();
  124. static tree build_protocol_initializer ();
  125.  
  126. static void synth_forward_declarations ();
  127. static void generate_ivar_lists ();
  128. static void generate_dispatch_tables ();
  129. static void generate_shared_structures ();
  130. static tree generate_protocol_list ();
  131. static void generate_forward_declaration_to_string_table ();
  132. static void build_protocol_reference ();
  133.  
  134. static tree init_selector ();
  135. static tree build_keword_selector ();
  136. static tree synth_id_with_class_suffix ();
  137.  
  138. /* misc. bookkeeping */
  139.  
  140. typedef struct hashedEntry     *hash;
  141. typedef struct hashedAttribute  *attr;
  142.  
  143. struct hashedAttribute {
  144.         attr    next;
  145.         tree    value;
  146. };
  147. struct hashedEntry {
  148.         attr    list;
  149.     hash    next;
  150.     tree     key;
  151. };
  152. static void hash_init ();
  153. static void hash_enter ();
  154. static hash hash_lookup ();
  155. static void hash_add_attr ();
  156. static tree lookup_method ();
  157. static tree lookup_instance_method_static ();
  158. static tree lookup_class_method_static ();
  159. static tree add_class ();
  160. static void add_class_reference ();
  161. enum stringSection {
  162.     class_names,        /* class, category, protocol, module names */
  163.     meth_var_names,        /* method and variable names */
  164.     meth_var_types,        /* method and variable type descriptors */
  165. };
  166. static tree add_objc_string ();
  167. static tree build_objc_string_decl ();
  168. static tree build_selector_reference_decl ();
  169.  
  170. /* protocol additions */
  171.  
  172. static tree add_protocol ();
  173. static tree lookup_protocol ();
  174. static void lookup_and_install_protocols ();
  175.  
  176. /* type encoding */
  177.  
  178. static void encode_aggregate ();
  179. static void encode_bitfield ();
  180. static void encode_type ();
  181. static void encode_field_decl ();
  182. static void encode_type_qualifiers ();
  183.  
  184. static void really_start_method ();
  185. static int  comp_method_with_proto ();
  186. static int  comp_proto_with_proto ();
  187. static tree get_arg_type_list ();
  188. static tree expr_last ();
  189.  
  190. /* utilities for debugging and error diagnostics: */
  191.  
  192. static void warn_with_method ();
  193. static void error_with_method ();
  194. static void error_with_ivar ();
  195. static char *gen_method_decl ();
  196. static char *gen_declaration ();
  197. static char *gen_declarator ();
  198. static int is_complex_decl ();
  199. static void adorn_decl ();
  200. static void dump_interfaces ();
  201.  
  202. /*** Private Interface (data) ***/
  203.  
  204. /* reserved tag definitions: */
  205.  
  206. #define TYPE_ID            "id"
  207. #define TAG_OBJECT        "objc_object"
  208. #define TAG_CLASS        "objc_class"
  209. #define TAG_SUPER        "objc_super"
  210. #define TAG_SELECTOR        "objc_selector"
  211.  
  212. #define _TAG_CLASS        "_objc_class"
  213. #define _TAG_IVAR        "_objc_ivar"
  214. #define _TAG_IVAR_LIST        "_objc_ivar_list"
  215. #define _TAG_METHOD        "_objc_method"
  216. #define _TAG_METHOD_LIST    "_objc_method_list"
  217. #define _TAG_CATEGORY        "_objc_category"
  218. #define _TAG_MODULE        "_objc_module"
  219. #define _TAG_SYMTAB        "_objc_symtab"
  220. #define _TAG_SUPER        "_objc_super"
  221.  
  222. #define _TAG_PROTOCOL             "_objc_protocol"
  223. #define _TAG_PROTOCOL_LIST         "_objc_protocol_list"
  224. #define _TAG_METHOD_PROTOTYPE        "_objc_method_prototype"
  225. #define _TAG_METHOD_PROTOTYPE_LIST   "_objc_method_prototype_list"
  226.  
  227. #define STRING_OBJECT_CLASS_NAME    "NXConstantString"
  228. #define PROTOCOL_OBJECT_CLASS_NAME    "Protocol"
  229.  
  230. /* set by `continue_class ()' and checked by `is_public ()' */
  231.  
  232. #define TREE_STATIC_TEMPLATE(record_type) (TREE_PUBLIC(record_type))
  233. #define TYPED_OBJECT(type) \
  234.        (TREE_CODE (type) == RECORD_TYPE && TREE_STATIC_TEMPLATE (type))
  235.  
  236. /* some commonly used instances of "identifier_node". */
  237.  
  238. static tree self_id, _cmd_id, _msg_id, _msgSuper_id;
  239. static tree objc_getClass_id, objc_getMetaClass_id;
  240.  
  241. static tree self_decl, _msg_decl, _msgSuper_decl;
  242. static tree objc_getClass_decl, objc_getMetaClass_decl;
  243.  
  244. static tree super_type, _selector_type, id_type, objc_class_type;
  245. static tree instance_type, protocol_type;
  246.  
  247. static tree class_chain = NULLT;
  248. static tree alias_chain = NULLT;
  249. static tree interface_chain = NULLT;
  250. static tree protocol_chain = NULLT;
  251.  
  252. /* chains to manage selectors that are referenced and defined in the module */
  253.  
  254. static tree cls_ref_chain = NULLT;    /* classes referenced */
  255. static tree sel_ref_chain = NULLT;    /* selectors referenced */
  256.  
  257. /* chains to manage uniquing of strings */
  258.  
  259. static tree class_names_chain = NULLT;
  260. static tree meth_var_names_chain = NULLT;
  261. static tree meth_var_types_chain = NULLT;
  262.  
  263. /* hash tables to manage the global pool of method prototypes */
  264.  
  265. static hash *nst_method_hash_list = 0;
  266. static hash *cls_method_hash_list = 0;
  267.  
  268. /* the following are used when compiling a class implementation.
  269.  *
  270.  * implementation_template will normally be an anInterface, however if
  271.  * none exists this will be equal to implementation_context...it is
  272.  * set in start_class.
  273.  */
  274.  
  275. /* backend data declarations */
  276.  
  277. static tree _OBJC_SYMBOLS_decl;
  278. static tree     _OBJC_INSTANCE_VARIABLES_decl, _OBJC_CLASS_VARIABLES_decl;
  279. static tree     _OBJC_INSTANCE_METHODS_decl, _OBJC_CLASS_METHODS_decl;
  280. static tree     _OBJC_CLASS_decl, _OBJC_METACLASS_decl;
  281. static tree _OBJC_MODULES_decl;
  282. static tree _OBJC_STRINGS_decl;
  283.  
  284. static tree implementation_context = NULLT,
  285.         implementation_template = NULLT;
  286.  
  287. struct imp_entry {
  288.     struct imp_entry *next;
  289.     tree imp_context;
  290.     tree imp_template;
  291.     tree class_decl;     /* _OBJC_CLASS_<my_name>; */
  292.     tree meta_decl;      /* _OBJC_METACLASS_<my_name>; */
  293. };
  294. static struct imp_entry *imp_list = 0;
  295. static int imp_count = 0;    /* `@implementation' */
  296. static int cat_count = 0;    /* `@category' */
  297.  
  298. static tree objc_class_template, objc_category_template, _PRIVATE_record;
  299. static tree objc_protocol_template;
  300. #ifdef OBJC_CLASS_REFS
  301. static tree objc_class_ref_template;
  302. #endif /* OBJC_CLASS_REFS */
  303. static tree _clsSuper_ref, __clsSuper_ref;
  304.  
  305. static tree objc_method_template, objc_ivar_template;
  306. static tree objc_symtab_template, objc_module_template;
  307. static tree objc_super_template, objc_object_reference;
  308.  
  309.  static tree objc_object_id, objc_class_id, objc_id_id;
  310. static tree constant_string_id;
  311. static tree constant_string_type;
  312. static tree _OBJC_SUPER_decl;
  313.  
  314. static tree method_context = NULLT;
  315. static int  method_slot = 0;    /* used by start_method_def */
  316.  
  317. #define BUFSIZE        1024
  318.  
  319. static char *errbuf;    /* a buffer for error diagnostics */
  320. static char *utlbuf;    /* a buffer for general utility */
  321.  
  322. /* services imported from "varasm.c" */
  323.  
  324. extern void assemble_asm ();
  325.  
  326. /* services imported from "final.c" */
  327.  
  328. extern void app_disable ();
  329.  
  330. extern char *strcpy (),*strcat ();
  331.  
  332. extern tree groktypename_in_parm_context ();
  333.  
  334. extern struct obstack permanent_obstack, *current_obstack,  *rtl_obstack;
  335.  
  336. /* data imported from toplev.c  */
  337.  
  338. extern char *dump_base_name;
  339.  
  340. /* Open and close the file for outputting class declarations, if requested.  */
  341.  
  342. int flag_gen_declaration = 0;
  343.  
  344. FILE *gen_declaration_file;
  345.  
  346. /* Warn if multiple methods are seen for the same selector, but with
  347.    different argument types. */
  348.  
  349. int warn_selector = 0;
  350.  
  351. void
  352. lang_init ()
  353. {
  354.   /* the beginning of the file is a new line; check for # */
  355.   /* With luck, we discover the real source file's name from that
  356.      and put it in input_filename.  */
  357.   ungetc (check_newline (), finput);
  358.  
  359.   /* If gen_declaration desired, open the output file.  */
  360.   if (flag_gen_declaration)
  361.     {
  362.       int dump_base_name_length = strlen (dump_base_name);
  363.       register char *dumpname = (char *) xmalloc (dump_base_name_length + 7);
  364.       strcpy (dumpname, dump_base_name);
  365.       strcat (dumpname, ".decl");
  366.       gen_declaration_file = fopen (dumpname, "w");
  367.       if (gen_declaration_file == 0)
  368.     pfatal_with_name (dumpname);
  369.     }
  370.  
  371.   if (doing_objc_thang)
  372.     init_objc ();
  373. }
  374.  
  375. static void
  376. objc_fatal ()
  377. {
  378.   fatal ("Objective-C text in C++ source file: use -ObjC");
  379. }
  380.  
  381. void
  382. objc_finish ()
  383. {
  384.   if (doing_objc_thang)
  385.     finish_objc ();        /* Objective-C finalization */
  386.  
  387.   if (gen_declaration_file)
  388.     fclose (gen_declaration_file);
  389. }
  390.  
  391. void
  392. lang_finish ()
  393. {
  394. }
  395.  
  396. int
  397. lang_decode_option (p)
  398.      char *p;
  399. {
  400.   if (!strcmp (p, "-lang-objc"))
  401.     doing_objc_thang = 1;
  402.   else if (!strcmp (p, "-gen-decls"))
  403.     flag_gen_declaration = 1;
  404.   else if (!strcmp (p, "-Wselector"))
  405.     warn_selector = 1;
  406.   else
  407.     return c_decode_option (p);
  408.  
  409.   return 1;
  410. }
  411.  
  412. static tree
  413. define_decl (declarator, declspecs)
  414.      tree declarator;
  415.      tree declspecs;
  416. {
  417.   tree decl = start_decl (declarator, declspecs, 0);
  418.   finish_decl (decl, NULLT, NULLT);
  419.   return decl;
  420. }
  421.  
  422. /*
  423.  * rules for statically typed objects...called from `c-typeck.comptypes'.
  424.  *
  425.  * an assignment of the form `a' = `b' is permitted if:
  426.  *
  427.  *   - `a' is of type "id".
  428.  *   - `a' and `b' are the same class type.
  429.  *   - `a' and `b' are of class types A and B such that B is a descendant
  430.  *     of A.
  431.  */
  432.  
  433. int
  434. maybe_objc_comptypes (lhs, rhs)
  435.      tree lhs, rhs;
  436. {
  437.   if (doing_objc_thang)
  438.     return objc_comptypes (lhs, rhs);
  439.   return 0;
  440. }
  441.  
  442. static tree
  443. lookup_method_in_protocol_list (rproto_list, sel_name, class_meth)
  444.    tree rproto_list;
  445.    tree sel_name;
  446.    int class_meth;
  447. {
  448.    tree rproto, p;
  449.    tree fnd = 0;
  450.     
  451.    for (rproto = rproto_list; rproto; rproto = TREE_CHAIN(rproto))
  452.      {
  453.         p = TREE_VALUE(rproto);
  454.       
  455.     if (TREE_CODE(p) == PROTOCOL_INTERFACE)
  456.       {
  457.       if (fnd = lookup_method (class_meth ? PROTOCOL_CLS_METHODS (p) : PROTOCOL_NST_METHODS (p), sel_name))
  458.             ;    
  459.       else if (PROTOCOL_LIST(rproto))
  460.         fnd = lookup_method_in_protocol_list(PROTOCOL_LIST(p), sel_name, class_meth);
  461.       }
  462.     else 
  463.       ; /* an identifier...if we could not find a protocol.  */
  464.       
  465.     if (fnd)
  466.       return fnd;
  467.      }
  468.    return 0;
  469. }
  470.  
  471. static tree
  472. lookup_protocol_in_reflist (rproto_list, lproto)
  473.    tree rproto_list;
  474.    tree lproto;
  475. {
  476.    tree rproto, p;
  477.     
  478.    /* make sure the protocol is support by the object on the rhs */
  479.    if (TREE_CODE(lproto) == PROTOCOL_INTERFACE)
  480.      {
  481.      tree fnd = 0;
  482.      for (rproto = rproto_list; rproto; rproto = TREE_CHAIN(rproto))
  483.        {
  484.           p = TREE_VALUE(rproto);
  485.       
  486.       if (TREE_CODE(p) == PROTOCOL_INTERFACE)
  487.         {
  488.         if (lproto == p)
  489.           fnd = lproto;
  490.         
  491.         else if (PROTOCOL_LIST(rproto))
  492.           fnd = lookup_protocol_in_reflist(PROTOCOL_LIST(p), lproto);
  493.         }
  494.       
  495.       if (fnd)
  496.         return fnd;
  497.        }
  498.      }
  499.    else 
  500.      ; /* an identifier...if we could not find a protocol. */
  501.  
  502.    return 0;
  503. }
  504.  
  505. int
  506. objc_comptypes (lhs, rhs)
  507.      tree lhs;
  508.      tree rhs;
  509. {
  510.   /* new clause for protocols */
  511.   
  512.   if ((TREE_CODE (lhs) == POINTER_TYPE
  513.        && TREE_CODE (TREE_TYPE (lhs)) == RECORD_TYPE)
  514.       && (TREE_CODE (rhs) == POINTER_TYPE
  515.        && TREE_CODE (TREE_TYPE (rhs)) == RECORD_TYPE))
  516.     {
  517.       int lhs_is_proto = (TYPE_MAIN_VARIANT (lhs) == id_type
  518.               && lhs != id_type);
  519.       int rhs_is_proto = (TYPE_MAIN_VARIANT (rhs) == id_type
  520.               && rhs != id_type);
  521.  
  522.       if (lhs_is_proto)
  523.         {
  524.          tree lproto, lproto_list = TYPE_PROTOCOL_LIST (lhs);
  525.          tree rproto, rproto_list;
  526.          tree p;
  527.       
  528.          if (rhs_is_proto)
  529.            {
  530.              rproto_list = TYPE_PROTOCOL_LIST (rhs);
  531.  
  532.              /* make sure the protocol is supported by the object on the rhs */
  533.              for (lproto = lproto_list; lproto; lproto = TREE_CHAIN(lproto))
  534.            {
  535.                  p = TREE_VALUE(lproto);
  536.              rproto = lookup_protocol_in_reflist(rproto_list, p); 
  537.       
  538.              if (!rproto)
  539.                    warning("object does not conform to the `%s' protocol",
  540.                      IDENTIFIER_POINTER(PROTOCOL_NAME(p)));
  541.            }
  542.        }
  543.          else if (TYPED_OBJECT (TREE_TYPE(rhs)))
  544.            {
  545.              tree rname = TYPE_NAME (TREE_TYPE(rhs));
  546.          tree rinter;
  547.  
  548.              /* make sure the protocol is supported by the object on the rhs */
  549.              for (lproto = lproto_list; lproto; lproto = TREE_CHAIN(lproto))
  550.            {
  551.                  p = TREE_VALUE(lproto);
  552.              rproto = 0;
  553.              rinter = lookup_interface (rname);
  554.       
  555.              while (rinter && !rproto)
  556.                {
  557.            tree cat;
  558.  
  559.                rproto_list = CLASS_PROTOCOL_LIST(rinter);
  560.                rproto = lookup_protocol_in_reflist(rproto_list, p);
  561.  
  562. /* NEW!!! */
  563.            /* Check for protocols adopted by categories. */
  564.            cat = CLASS_CATEGORY_LIST (rinter);
  565.            while (cat && !rproto)
  566.              {
  567.                rproto_list = CLASS_PROTOCOL_LIST (cat);
  568.                rproto = lookup_protocol_in_reflist (rproto_list, p);
  569.  
  570.                cat = CLASS_CATEGORY_LIST (cat);
  571.              }
  572.  
  573.                rinter = lookup_interface (CLASS_SUPER_NAME (rinter));
  574.                }
  575.              if (!rproto)
  576.                warning("class `%s' does not implement the `%s' protocol",
  577.                          IDENTIFIER_POINTER(TYPE_NAME (TREE_TYPE(rhs))),
  578.                      IDENTIFIER_POINTER(PROTOCOL_NAME(p)));
  579.                }
  580.        }
  581.     
  582.           return 1; /* may change...based on whether there was any mismatch */
  583.         }
  584.       else if (rhs_is_proto)
  585.         {
  586.     /* lhs is not a protocol...warn if it is statically typed */
  587.     
  588.     if (TYPED_OBJECT (TREE_TYPE(lhs)))
  589.       return 0;
  590.     else
  591.       return 1;    /* one of the types is a protocol */
  592.     }
  593.       else
  594.     return 2;    /* defer to comptypes */      
  595.     }
  596.   else if (TREE_CODE (lhs) == RECORD_TYPE && TREE_CODE (rhs) == RECORD_TYPE)
  597.     ; /* fall thru...this is the case we have been handling all along */
  598.   else
  599.     return 2;    /* defer to comptypes */
  600.  
  601.   /* End of new protocol support.  */
  602.  
  603.   /* `id' = `<class> *', `<class> *' = `id' */
  604.  
  605.   if ((TYPE_NAME (lhs) == objc_object_id && TYPED_OBJECT (rhs))
  606.       || (TYPED_OBJECT (lhs) && TYPE_NAME (rhs) == objc_object_id))
  607.     return 1;
  608.  
  609.   /* `id' = `Class', `Class' = `id' */
  610.  
  611.   else if ((TYPE_NAME (lhs) == objc_object_id &&
  612.         TYPE_NAME (rhs) == objc_class_id) ||
  613.        (TYPE_NAME (lhs) == objc_class_id &&
  614.         TYPE_NAME (rhs) == objc_object_id))
  615.     return 1;
  616.  
  617.   /* `<class> *' = `<class> *' */
  618.  
  619.   else if (TYPED_OBJECT (lhs) && TYPED_OBJECT (rhs))
  620.     {
  621.       tree lname = TYPE_NAME (lhs), rname = TYPE_NAME (rhs);
  622.  
  623.       if (lname == rname)
  624.     return 1;
  625.       else
  626.     {
  627.       /* if the left hand side is a super class of the right hand side,
  628.          allow it...
  629.          */
  630.       tree rinter = lookup_interface (rname);
  631.  
  632.       while (rinter)
  633.         {
  634.           if (lname == CLASS_SUPER_NAME (rinter))
  635.         return 1;
  636.  
  637.           rinter = lookup_interface (CLASS_SUPER_NAME (rinter));
  638.         }
  639.  
  640.       return 0;
  641.     }
  642.     }
  643.   else
  644.     return 0;
  645. }
  646.  
  647. /* Called from c-decl.c before all calls to rest_of_decl_compilation.  */
  648.  
  649. void
  650. maybe_objc_check_decl (decl)
  651.      tree decl;
  652. {
  653.   if (doing_objc_thang)
  654.     objc_check_decl (decl);
  655. }
  656.  
  657. void
  658. objc_check_decl (decl)
  659.      tree decl;
  660. {
  661.   tree type = TREE_TYPE (decl);
  662.   static int alreadyWarned = 0;
  663.  
  664.   if (TREE_CODE (type) == RECORD_TYPE && TREE_STATIC_TEMPLATE (type)
  665.       && type != constant_string_type)
  666.     {
  667.       if (!alreadyWarned)
  668.     {
  669.       error ("GNU compiler does not support statically allocated objects");
  670.       alreadyWarned = 1;
  671.     }
  672.       error_with_decl (decl, "`%s' cannot be statically allocated");
  673.     }
  674. }
  675.  
  676. /* implement static typing. at this point, we know we have an interface... */
  677.  
  678. tree
  679. get_static_reference (ident, protocols)
  680.      tree ident;
  681.      tree protocols;
  682. {
  683.      tree type = xref_tag (RECORD_TYPE, ident);
  684.  
  685.      if (protocols)
  686.        {
  687.       tree proto, t, m = TYPE_MAIN_VARIANT (type);
  688.       struct obstack *ambient_obstack = current_obstack;
  689.       
  690.       current_obstack = &permanent_obstack;
  691.       t = copy_node (type);
  692.       
  693.       /* Add this type to the chain of variants of TYPE.  */
  694.       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
  695.       TYPE_NEXT_VARIANT (m) = t;
  696.       
  697.       current_obstack = ambient_obstack;
  698.   
  699.       /* look up protocols...and install in lang specific list */
  700.       
  701.       TYPE_PROTOCOL_LIST (t) = protocols;
  702.       lookup_and_install_protocols (protocols);
  703.         
  704.       /* this forces a new pointer type to be created later 
  705.        * (in build_pointer_type())...so that the new template
  706.        * we just created will actually be used...what a hack!
  707.        */
  708.       if (TYPE_POINTER_TO (t))
  709.         TYPE_POINTER_TO(t) = NULL;
  710.  
  711.       type = t;
  712.        }
  713.  
  714.      return type;
  715. }
  716.  
  717. tree
  718. get_object_reference (protocols)
  719.      tree protocols;
  720. {
  721.      tree type_decl = lookup_name (objc_id_id);
  722.      tree type;
  723.      
  724.      if (type_decl && (TREE_CODE(type_decl) == TYPE_DECL))
  725.        {
  726.        type = TREE_TYPE (type_decl); 
  727.        if (TYPE_MAIN_VARIANT(type) != id_type)
  728.          warning("Unexpected type for `id' (%s)", 
  729.             gen_declaration(type,errbuf));
  730.        }
  731.      else
  732.        {
  733.        error ("Undefined type `id', please import <objc/objc.h>");
  734.        exit(1);
  735.        }
  736.  
  737.      /* this clause creates a new pointer type that is qualified with
  738.         the protocol specification...this info is used later to do more
  739.     elaborate type checking.
  740.       */
  741.      if (protocols)
  742.     {
  743.       tree proto, t, m = TYPE_MAIN_VARIANT (type);
  744.       struct obstack *ambient_obstack = current_obstack;
  745.       
  746.       current_obstack = &permanent_obstack;
  747.       t = copy_node (type);
  748.       
  749.       /* Add this type to the chain of variants of TYPE.  */
  750.       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
  751.       TYPE_NEXT_VARIANT (m) = t;
  752.       
  753.       current_obstack = ambient_obstack;
  754.   
  755.       /* look up protocols...and install in lang specific list */
  756.       
  757.       TYPE_PROTOCOL_LIST (t) = protocols;
  758.       lookup_and_install_protocols (protocols);
  759.         
  760.       /* this forces a new pointer type to be created later 
  761.        * (in build_pointer_type())...so that the new template
  762.        * we just created will actually be used...what a hack!
  763.        */
  764.       if (TYPE_POINTER_TO (t))
  765.         TYPE_POINTER_TO(t) = NULL;
  766.  
  767.       type = t;
  768.     }
  769.      return type;
  770. }
  771.  
  772. static void 
  773. lookup_and_install_protocols (protocols)
  774.      tree protocols;
  775.      tree proto;
  776.        
  777.      for (proto = protocols; proto; proto = TREE_CHAIN (proto))
  778.        {
  779.       tree ident = TREE_VALUE (proto);
  780.       tree p = lookup_protocol (ident);
  781.   
  782.       if (!p)
  783.         error ("Cannot find protocol declaration for `%s'",
  784.            IDENTIFIER_POINTER (ident));
  785.       else /* replace identifier with actual protocol node */
  786.         TREE_VALUE(proto) = p;
  787.        }
  788. }
  789.  
  790. /*
  791.  *    purpose: "play" parser, creating/installing representations
  792.  *         of the declarations that are required by Objective-C.
  793.  *
  794.  *    model:
  795.  *
  796.  *        type_spec--------->sc_spec
  797.  *        (tree_list)        (tree_list)
  798.  *            |                  |
  799.  *            |                  |
  800.  *        identifier_node    identifier_node
  801.  */
  802. static void
  803. synth_module_prologue ()
  804. {
  805.   tree sc_spec, type_spec, decl_specs, expr_decl, parms, record;
  806.  
  807.   /* defined in `objc.h' */
  808.   objc_object_id = get_identifier (TAG_OBJECT);
  809.  
  810.   objc_object_reference = xref_tag (RECORD_TYPE, objc_object_id);
  811.  
  812.   id_type = groktypename (build_tree_list (
  813.                        build_tree_list (NULLT, objc_object_reference),
  814.                        build1 (INDIRECT_REF, NULLT, NULLT)));
  815.  
  816.   objc_id_id = get_identifier(TYPE_ID);
  817.   objc_class_id = get_identifier (TAG_CLASS);
  818.   
  819.   objc_class_type = groktypename (build_tree_list
  820.                  (build_tree_list
  821.                   (NULLT, xref_tag (RECORD_TYPE, objc_class_id)),
  822.                   build1 (INDIRECT_REF, NULLT, NULLT)));
  823.  
  824.   protocol_type = groktypename (build_tree_list
  825.                 (build_tree_list 
  826.                              (NULLT, xref_tag (RECORD_TYPE,
  827.                   get_identifier (PROTOCOL_OBJECT_CLASS_NAME))), 
  828.                     build1 (INDIRECT_REF, NULLT, NULLT)));
  829.  
  830. /* Declare SEL type before prototypes for objc_msgSend(), or else those
  831.    struct tags are considered local to the prototype and won't match the one
  832.    in <objc/objc-runtime.h>. */
  833.  
  834. #ifdef OBJC_SELS_R_INTS
  835.   /* `unsigned int' */
  836.   _selector_type = unsigned_type_node;
  837. #endif
  838.  
  839. #ifdef OBJC_SELS_R_STRUCT_PTRS
  840.   /* `struct objc_selector *' */
  841.   _selector_type = groktypename (build_tree_list (
  842.          build_tree_list (NULLT,
  843.                   xref_tag (RECORD_TYPE,
  844.                     get_identifier (TAG_SELECTOR))),
  845.          build1 (INDIRECT_REF, NULLT, NULLT)));
  846. #endif
  847.  
  848.   /* forward declare type...or else the prototype for `super' will bitch */
  849.   groktypename (build_tree_list (build_tree_list (NULLT,
  850.                           xref_tag (RECORD_TYPE, get_identifier (TAG_SUPER))),
  851.                  build1 (INDIRECT_REF, NULLT, NULLT)));
  852.  
  853.   _msg_id = get_identifier ("objc_msgSend");
  854.   _msgSuper_id = get_identifier ("objc_msgSendSuper");
  855.   objc_getClass_id = get_identifier ("objc_getClass");
  856.   objc_getMetaClass_id = get_identifier ("objc_getMetaClass");
  857.  
  858.   /* struct objc_object *objc_msgSend (id, SEL, ...); */
  859.   pushlevel (0);
  860.   decl_specs = build_tree_list (NULLT, objc_object_reference);
  861.   push_parm_decl (build_tree_list (decl_specs,
  862.                    build1 (INDIRECT_REF, NULLT, NULLT)));
  863.  
  864. #ifdef OBJC_SELS_R_INTS
  865.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_UNSIGNED]);
  866.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_INT], decl_specs);
  867.   expr_decl = NULLT;
  868. #endif
  869.  
  870. #ifdef OBJC_SELS_R_STRUCT_PTRS
  871.   decl_specs = build_tree_list (NULLT,
  872.                 xref_tag (RECORD_TYPE,
  873.                       get_identifier (TAG_SELECTOR)));
  874.   expr_decl = build1 (INDIRECT_REF, NULLT, NULLT);
  875. #endif
  876.  
  877.   push_parm_decl (build_tree_list (decl_specs, expr_decl));
  878.   parms = get_parm_info (0);
  879.   poplevel (0, 0, 0);
  880.  
  881.   decl_specs = build_tree_list (NULLT, objc_object_reference);
  882.   expr_decl = build_nt (CALL_EXPR, _msg_id, parms, NULLT);
  883.   expr_decl = build1 (INDIRECT_REF, NULLT, expr_decl);
  884.  
  885.   _msg_decl = define_decl (expr_decl, decl_specs);
  886.  
  887.   /* struct objc_object *objc_msgSendSuper (struct objc_super *, SEL, ...); */
  888.   pushlevel (0);
  889.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  890.                          get_identifier (TAG_SUPER)));
  891.   push_parm_decl (build_tree_list (decl_specs,
  892.                    build1 (INDIRECT_REF, NULLT, NULLT)));
  893.  
  894. #ifdef OBJC_SELS_R_INTS
  895.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_UNSIGNED]);
  896.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_INT], decl_specs);
  897.   expr_decl = NULLT;
  898. #endif
  899.  
  900. #ifdef OBJC_SELS_R_STRUCT_PTRS
  901.   decl_specs = build_tree_list (NULLT,
  902.                 xref_tag (RECORD_TYPE,
  903.                       get_identifier (TAG_SELECTOR)));
  904.   expr_decl = build1 (INDIRECT_REF, NULLT, NULLT);
  905. #endif
  906.  
  907.   push_parm_decl (build_tree_list (decl_specs, expr_decl));
  908.   parms = get_parm_info (0);
  909.   poplevel (0, 0, 0);
  910.  
  911.   decl_specs = build_tree_list (NULLT, objc_object_reference);
  912.   expr_decl = build_nt (CALL_EXPR, _msgSuper_id, parms, NULLT);
  913.   expr_decl = build1 (INDIRECT_REF, NULLT, expr_decl);
  914.  
  915.   _msgSuper_decl = define_decl (expr_decl, decl_specs);
  916.  
  917.   /* id objc_getClass (const char *); */
  918.   pushlevel (0);
  919.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_CONST]);
  920.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_CHAR], decl_specs);
  921.   push_parm_decl (build_tree_list (decl_specs,
  922.                    build1 (INDIRECT_REF, NULLT, NULLT)));
  923.   parms = get_parm_info (1);    
  924.   poplevel (0, 0, 0);
  925.   decl_specs = build_tree_list (NULLT, objc_object_reference);
  926.   expr_decl = build_nt (CALL_EXPR, objc_getClass_id, parms, NULLT);
  927.   expr_decl = build1 (INDIRECT_REF, NULLT, expr_decl);
  928.  
  929.   objc_getClass_decl = define_decl (expr_decl, decl_specs);
  930.  
  931.   /* id objc_getMetaClass (const char *); */
  932.   pushlevel (0);
  933.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_CONST]);
  934.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_CHAR], decl_specs);
  935.   push_parm_decl (build_tree_list (decl_specs, 
  936.                     build1 (INDIRECT_REF, NULLT, NULLT)));
  937.   parms = get_parm_info (1);    
  938.   poplevel (0, 0, 0);
  939.   decl_specs = build_tree_list (NULLT, objc_object_reference);
  940.   expr_decl = build_nt (CALL_EXPR, objc_getMetaClass_id, parms, NULLT);
  941.   expr_decl = build1 (INDIRECT_REF, NULLT, expr_decl);
  942.  
  943.   objc_getMetaClass_decl = define_decl (expr_decl, decl_specs);
  944.  
  945.   generate_forward_declaration_to_string_table ();
  946.  
  947. /* Forward declare constant_string_id and constant_string_type.  */
  948.   constant_string_id = get_identifier (STRING_OBJECT_CLASS_NAME);
  949.   constant_string_type = xref_tag (RECORD_TYPE, constant_string_id);
  950.  
  951. #ifdef OBJC_CLASS_REFS
  952.   build_class_ref_template ();
  953. #endif /* OBJC_CLASS_REFS */
  954. }
  955.  
  956. /*
  957.  * custom "build_string ()" which sets TREE_TYPE!
  958.  */
  959. static tree
  960. my_build_string (len, str)
  961.      int len;
  962.      char *str;
  963. {
  964.   int wide_flag = 0;
  965.   tree aString = build_string (len, str);
  966.   /*
  967.    *  some code from "combine_strings ()", which is local to c-parse.y.
  968.    */
  969.   if (TREE_TYPE (aString) == int_array_type_node)
  970.     wide_flag = 1;
  971.  
  972.   TREE_TYPE (aString) =
  973.     build_array_type (wide_flag ? integer_type_node : char_type_node,
  974.               build_index_type (build_int_2 (len - 1, 0)));
  975.  
  976.   TREE_CONSTANT (aString) = 1;    /* puts string in the ".text" segment */
  977.   TREE_STATIC (aString) = 1;
  978.  
  979.   return aString;
  980. }
  981.  
  982. /* Return a newly constructed OBJC_STRING_CST node whose value is
  983.    the LEN characters at STR.
  984.    The TREE_TYPE is not initialized.  */
  985.  
  986. tree
  987. build_objc_string (len, str)
  988.      int len;
  989.      char *str;
  990. {
  991.   tree s = build_string (len, str);
  992.   
  993.   TREE_CODE (s) = OBJC_STRING_CST;
  994.   return s;
  995. }
  996.  
  997. /* Given a chain of OBJC_STRING_CST's, build a static instance of
  998.    NXConstantString which points at the concatenation of those strings.
  999.    We place the string object in the __string_objects section of the
  1000.    __OBJC segment.  The Objective-C runtime will initialize the isa
  1001.    pointers of the string objects to point at the NXConstantString
  1002.    class object.  */
  1003.  
  1004. tree
  1005. build_objc_string_object (strings)
  1006.      tree strings;
  1007. {
  1008.   tree string, initlist, constructor;
  1009.   int length;
  1010.   
  1011.   if (!doing_objc_thang)
  1012.     objc_fatal ();
  1013.  
  1014.   if (lookup_interface (constant_string_id) == NULLT)
  1015.     {
  1016.       error ("Cannot find interface declaration for `%s'",
  1017.            IDENTIFIER_POINTER (constant_string_id));
  1018.       return error_mark_node;
  1019.     }
  1020.  
  1021.   add_class_reference (constant_string_id);
  1022.  
  1023.   /* combine_strings will work for OBJC_STRING_CST's too.  */
  1024.   string = combine_strings (strings);
  1025.   TREE_CODE (string) = STRING_CST;
  1026.   length = TREE_STRING_LENGTH (string) - 1;
  1027.  
  1028.   /* & ((NXConstantString) {0, string, length})  */
  1029.  
  1030.   initlist = build_tree_list (NULLT, build_int_2 (0, 0));
  1031.   initlist = tree_cons (NULLT, build_unary_op (ADDR_EXPR, string, 1),
  1032.             initlist);
  1033.   initlist = tree_cons (NULLT, build_int_2 (length, 0), initlist);
  1034.   constructor = build (CONSTRUCTOR, constant_string_type, NULLT,
  1035.                nreverse (initlist));
  1036.   TREE_CONSTANT (constructor) = 1;
  1037.   TREE_STATIC (constructor) = 1;
  1038.   TREE_READONLY (constructor) = 1;
  1039.  
  1040.   return build_unary_op (ADDR_EXPR, constructor, 1);
  1041. }
  1042.  
  1043. /*
  1044.  *    struct objc_symtab {
  1045.  *        long sel_ref_cnt;
  1046.  *        char *refs;
  1047.  *        long cls_def_cnt;
  1048.  *        long cat_def_cnt;
  1049.  *        void *defs[cls_def_cnt + cat_def_cnt];
  1050.  *    };
  1051.  */
  1052. static void
  1053. build_objc_symtab_template ()
  1054. {
  1055.   tree decl_specs, field_decl, field_decl_chain;
  1056.  
  1057.   objc_symtab_template = start_struct (RECORD_TYPE, get_identifier (_TAG_SYMTAB));
  1058.  
  1059.   /* long sel_ref_cnt; */
  1060.  
  1061.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_LONG]);
  1062.   field_decl = get_identifier ("sel_ref_cnt");
  1063.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1064.   field_decl_chain = field_decl;
  1065.  
  1066. #ifdef OBJC_SELS_R_INTS
  1067.   /* unsigned int *sel_ref; */
  1068.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_UNSIGNED]);
  1069.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_INT], decl_specs);
  1070.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("refs"));
  1071. #endif
  1072.  
  1073. #ifdef OBJC_SELS_R_STRUCT_PTRS
  1074.   /* struct objc_selector **sel_ref; */
  1075.   decl_specs = build_tree_list (NULLT,
  1076.                 xref_tag (RECORD_TYPE,
  1077.                       get_identifier (TAG_SELECTOR)));
  1078.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("refs"));
  1079.   field_decl = build1 (INDIRECT_REF, NULLT, field_decl);
  1080. #endif
  1081.  
  1082.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1083.   chainon (field_decl_chain, field_decl);
  1084.  
  1085.   /* short cls_def_cnt; */
  1086.  
  1087.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_SHORT]);
  1088.   field_decl = get_identifier ("cls_def_cnt");
  1089.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1090.   chainon (field_decl_chain, field_decl);
  1091.  
  1092.   /* short cat_def_cnt; */
  1093.  
  1094.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_SHORT]);
  1095.   field_decl = get_identifier ("cat_def_cnt");
  1096.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1097.   chainon (field_decl_chain, field_decl);
  1098.  
  1099.   /* void *defs[cls_def_cnt + cat_def_cnt]; */
  1100.  
  1101.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_VOID]);
  1102.   field_decl = build_nt (ARRAY_REF, get_identifier ("defs"),
  1103.              build_int_2 (imp_count + cat_count, 0));
  1104.   field_decl = build1 (INDIRECT_REF, NULLT, field_decl);
  1105.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1106.   chainon (field_decl_chain, field_decl);
  1107.  
  1108.   finish_struct (objc_symtab_template, field_decl_chain);
  1109. }
  1110.  
  1111. static tree
  1112. init_def_list ()
  1113. {
  1114.   tree expr, initlist = NULLT;
  1115.   struct imp_entry *impent;
  1116.  
  1117.   if (imp_count)
  1118.     for (impent = imp_list; impent; impent = impent->next)
  1119.       {
  1120.     if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION)
  1121.       {
  1122.         expr = build_unary_op (ADDR_EXPR, impent->class_decl, 0);
  1123.         initlist = tree_cons (NULLT, expr, initlist);
  1124.       }
  1125.       }
  1126.  
  1127.   if (cat_count)
  1128.     for (impent = imp_list; impent; impent = impent->next)
  1129.       {
  1130.     if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION)
  1131.       {
  1132.         expr = build_unary_op (ADDR_EXPR, impent->class_decl, 0);
  1133.         initlist = tree_cons (NULLT, expr, initlist);
  1134.       }
  1135.       }
  1136.   return build_nt (CONSTRUCTOR, NULLT, nreverse (initlist));
  1137. }
  1138.  
  1139. /*
  1140.  *    struct objc_symtab {
  1141.  *        long sel_ref_cnt;
  1142.  *        char *refs;
  1143.  *        long cls_def_cnt;
  1144.  *        long cat_def_cnt;
  1145.  *        void *defs[cls_def_cnt + cat_def_cnt];
  1146.  *    };
  1147.  */
  1148. static tree
  1149. init_objc_symtab ()
  1150. {
  1151.   tree initlist;
  1152.  
  1153.   /* sel_ref_cnt = { ..., 5, ... } */
  1154.  
  1155.   initlist = build_tree_list (NULLT, build_int_2 (0, 0));
  1156.  
  1157.   /* refs = { ..., _OBJC_SELECTOR_REFERENCES, ... } */
  1158.  
  1159.   initlist = tree_cons (NULLT, build_int_2 (0, 0), initlist);
  1160.  
  1161.   /* cls_def_cnt = { ..., 5, ... } */
  1162.  
  1163.   initlist = tree_cons (NULLT, build_int_2 (imp_count, 0), initlist);
  1164.  
  1165.   /* cat_def_cnt = { ..., 5, ... } */
  1166.  
  1167.   initlist = tree_cons (NULLT, build_int_2 (cat_count, 0), initlist);
  1168.  
  1169.   /* cls_def = { ..., { &Foo, &Bar, ...}, ... } */
  1170.  
  1171.   if (imp_count || cat_count)
  1172.     initlist = tree_cons (NULLT, init_def_list (), initlist);
  1173.  
  1174.   return build_nt (CONSTRUCTOR, NULLT, nreverse (initlist));
  1175. }
  1176.  
  1177. static void
  1178. forward_declare_categories ()
  1179. {
  1180.   struct imp_entry *impent;
  1181.   tree sav = implementation_context;
  1182.   for (impent = imp_list; impent; impent = impent->next)
  1183.     {
  1184.       if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION)
  1185.     {
  1186.       tree sc_spec, decl_specs, decl;
  1187.  
  1188.       sc_spec = build_tree_list (NULLT, ridpointers[(int) RID_EXTERN]);
  1189.       decl_specs = tree_cons (NULLT, objc_category_template, sc_spec);
  1190.  
  1191.       implementation_context = impent->imp_context;
  1192.       impent->class_decl = define_decl (
  1193.         synth_id_with_class_suffix ("_OBJC_CATEGORY", implementation_context),
  1194.         decl_specs);
  1195.     }
  1196.     }
  1197.   implementation_context = sav;
  1198. }
  1199.  
  1200. static void
  1201. generate_objc_symtab_decl ()
  1202. {
  1203.   tree sc_spec;
  1204.  
  1205.   if (!objc_category_template)
  1206.     build_category_template ();
  1207.  
  1208.   /* forward declare categories */
  1209.   if (cat_count)
  1210.     forward_declare_categories ();
  1211.  
  1212.   if (!objc_symtab_template)
  1213.     build_objc_symtab_template ();
  1214.  
  1215.   sc_spec = build_tree_list (NULLT, ridpointers[(int) RID_STATIC]);
  1216.  
  1217.   _OBJC_SYMBOLS_decl = start_decl (get_identifier ("_OBJC_SYMBOLS"),
  1218.                    tree_cons (NULLT, objc_symtab_template, sc_spec), 1);
  1219.  
  1220.   finish_decl (_OBJC_SYMBOLS_decl, init_objc_symtab (), NULLT);
  1221. }
  1222.  
  1223. /*
  1224.  *    tree_node------->tree_node----->...
  1225.  *          |                |
  1226.  *          | (value)        | (value)
  1227.  *          |                |
  1228.  *      expr             expr
  1229.  */
  1230. static tree
  1231. init_module_descriptor ()
  1232. {
  1233.   tree initlist, expr;
  1234.  
  1235.   /* version = { 1, ... } */
  1236.  
  1237.   expr = build_int_2 (OBJC_VERSION, 0);
  1238.   initlist = build_tree_list (NULLT, expr);
  1239.  
  1240.   /* size = { ..., sizeof (struct objc_module), ... } */
  1241.  
  1242.   expr = build_int_2 (TREE_INT_CST_LOW (TYPE_SIZE (objc_module_template)) /
  1243.               BITS_PER_UNIT, 0);
  1244.   initlist = tree_cons (NULLT, expr, initlist);
  1245.  
  1246.   /* name = { ..., "foo.m", ... } */
  1247.  
  1248.   expr = add_objc_string (get_identifier (input_filename), class_names);
  1249.   initlist = tree_cons (NULLT, expr, initlist);
  1250.  
  1251.   /* symtab = { ..., _OBJC_SYMBOLS, ... } */
  1252.  
  1253.   if (_OBJC_SYMBOLS_decl)
  1254.     expr = build_unary_op (ADDR_EXPR, _OBJC_SYMBOLS_decl, 0);
  1255.   else
  1256.     expr = build_int_2 (0, 0);
  1257.   initlist = tree_cons (NULLT, expr, initlist);
  1258.  
  1259.   return build_nt (CONSTRUCTOR, NULLT, nreverse (initlist));
  1260. }
  1261.  
  1262. /*
  1263.  *  struct objc_module { ... } _OBJC_MODULE = { ... };
  1264.  */
  1265. static void
  1266. build_module_descriptor ()
  1267. {
  1268.   tree decl_specs, field_decl, field_decl_chain;
  1269.  
  1270.   objc_module_template = start_struct (RECORD_TYPE, get_identifier (_TAG_MODULE));
  1271.  
  1272.   /* long version; */
  1273.  
  1274.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_LONG]);
  1275.   field_decl = get_identifier ("version");
  1276.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1277.   field_decl_chain = field_decl;
  1278.  
  1279.   /* long  size; */
  1280.  
  1281.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_LONG]);
  1282.   field_decl = get_identifier ("size");
  1283.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1284.   chainon (field_decl_chain, field_decl);
  1285.  
  1286.   /* char  *name; */
  1287.  
  1288.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_CHAR]);
  1289.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("name"));
  1290.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1291.   chainon (field_decl_chain, field_decl);
  1292.  
  1293.   /* struct objc_symtab *symtab; */
  1294.  
  1295.   decl_specs = get_identifier (_TAG_SYMTAB);
  1296.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE, decl_specs));
  1297.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("symtab"));
  1298.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1299.   chainon (field_decl_chain, field_decl);
  1300.  
  1301.   finish_struct (objc_module_template, field_decl_chain);
  1302.  
  1303.   /* create an instance of "objc_module" */
  1304.  
  1305.   decl_specs = tree_cons (NULLT, objc_module_template,
  1306.               build_tree_list (NULLT, ridpointers[(int) RID_STATIC]));
  1307.  
  1308.   _OBJC_MODULES_decl = start_decl (get_identifier ("_OBJC_MODULES"),
  1309.                    decl_specs, 1);
  1310.  
  1311.   finish_decl (_OBJC_MODULES_decl, init_module_descriptor (), NULLT);
  1312.  
  1313.   /* Mark the decl as used to avoid "defined but not used" warning. */
  1314.   TREE_USED (_OBJC_MODULES_decl) = 1;
  1315. }
  1316.  
  1317. /* extern const char _OBJC_STRINGS[]; */
  1318.  
  1319. static void
  1320. generate_forward_declaration_to_string_table ()
  1321. {
  1322.   tree sc_spec, decl_specs, expr_decl;
  1323.  
  1324.   sc_spec = tree_cons (NULLT, ridpointers[(int) RID_EXTERN], NULLT);
  1325.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_CHAR], sc_spec);
  1326.  
  1327.   expr_decl = build_nt (ARRAY_REF, get_identifier ("_OBJC_STRINGS"), NULLT);
  1328.  
  1329.   _OBJC_STRINGS_decl = define_decl (expr_decl, decl_specs);
  1330. }
  1331.  
  1332. /* Output all strings. */
  1333.  
  1334. static void
  1335. generate_strings ()
  1336. {
  1337.   tree sc_spec, decl_specs, expr_decl;
  1338.   tree chain, string_expr;
  1339.   tree string, decl;
  1340.   
  1341.   for (chain = class_names_chain; chain; chain = TREE_CHAIN (chain))
  1342.     {
  1343.       string = TREE_VALUE (chain);
  1344.       decl = TREE_PURPOSE (chain);
  1345.       sc_spec = tree_cons (NULLT, ridpointers[(int) RID_STATIC], NULLT);
  1346.       decl_specs = tree_cons (NULLT, ridpointers[(int) RID_CHAR], sc_spec);
  1347.       expr_decl = build_nt (ARRAY_REF, DECL_NAME (decl), NULLT);
  1348.       decl = start_decl (expr_decl, decl_specs, 1);
  1349.       string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
  1350.                      IDENTIFIER_POINTER (string));
  1351.       finish_decl (decl, string_expr, NULLT);
  1352.     }
  1353.   
  1354.   for (chain = meth_var_names_chain; chain; chain = TREE_CHAIN (chain))
  1355.     {
  1356.       string = TREE_VALUE (chain);
  1357.       decl = TREE_PURPOSE (chain);
  1358.       sc_spec = tree_cons (NULLT, ridpointers[(int) RID_STATIC], NULLT);
  1359.       decl_specs = tree_cons (NULLT, ridpointers[(int) RID_CHAR], sc_spec);
  1360.       expr_decl = build_nt (ARRAY_REF, DECL_NAME (decl), NULLT);
  1361.       decl = start_decl (expr_decl, decl_specs, 1);
  1362.       string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
  1363.                      IDENTIFIER_POINTER (string));
  1364.       finish_decl (decl, string_expr, NULLT);
  1365.     }
  1366.   
  1367.   for (chain = meth_var_types_chain; chain; chain = TREE_CHAIN (chain))
  1368.     {
  1369.       string = TREE_VALUE (chain);
  1370.       decl = TREE_PURPOSE (chain);
  1371.       sc_spec = tree_cons (NULLT, ridpointers[(int) RID_STATIC], NULLT);
  1372.       decl_specs = tree_cons (NULLT, ridpointers[(int) RID_CHAR], sc_spec);
  1373.       expr_decl = build_nt (ARRAY_REF, DECL_NAME (decl), NULLT);
  1374.       decl = start_decl (expr_decl, decl_specs, 1);
  1375.       string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1,
  1376.                      IDENTIFIER_POINTER (string));
  1377.       finish_decl (decl, string_expr, NULLT);
  1378.     }
  1379. }
  1380.  
  1381. static tree
  1382. build_selector_reference_decl (name)
  1383.       tree name;
  1384. {
  1385.   tree decl, ident;
  1386.   char buf[256];
  1387.   struct obstack *save_current_obstack = current_obstack;
  1388.   struct obstack *save_rtl_obstack = rtl_obstack;
  1389.   static int idx = 0;
  1390.   
  1391.   sprintf (buf, "_OBJC_SELECTOR_REFERENCES_%d", idx++);
  1392.   
  1393.   /* new stuff */
  1394.   rtl_obstack = current_obstack = &permanent_obstack;
  1395.   ident = get_identifier (buf);
  1396.   
  1397.   decl = build_decl (VAR_DECL, ident, _selector_type);
  1398.   TREE_EXTERNAL (decl) = 1;
  1399.   TREE_PUBLIC (decl) = 1;
  1400.   TREE_USED (decl) = 1;
  1401.   TREE_READONLY (decl) = 1;
  1402.   
  1403.   make_decl_rtl (decl, 0, 1); /* usually called from `rest_of_decl_compilation' */
  1404.   pushdecl_top_level (decl);  /* our `extended/custom' pushdecl in c-decl.c */
  1405.   
  1406.   current_obstack = save_current_obstack;
  1407.   rtl_obstack = save_rtl_obstack;
  1408.   
  1409.   return decl;
  1410. }
  1411.  
  1412.  
  1413. /* Just a handy wrapper for add_objc_string() */
  1414.  
  1415. static tree
  1416. build_selector (ident)
  1417.      tree ident;
  1418. {
  1419.   tree expr = add_objc_string (ident, meth_var_names);
  1420.   
  1421.   return build_c_cast (_selector_type, expr);    /* cast! */
  1422. }
  1423.  
  1424. static void
  1425. build_selector_translation_table ()
  1426. {
  1427.   tree sc_spec, decl_specs, expr_decl;
  1428.   tree chain, initlist = NULLT;
  1429.   tree var_decl;
  1430.   tree expr, name, decl;
  1431.  
  1432.   for (chain = sel_ref_chain; chain; chain = TREE_CHAIN (chain))
  1433.     {
  1434.       expr = build_selector (TREE_VALUE (chain));
  1435.       name = DECL_NAME (TREE_PURPOSE (chain));
  1436.  
  1437.       sc_spec = build_tree_list (NULLT, ridpointers[RID_STATIC]);
  1438.  
  1439. #ifdef OBJC_SELS_R_INTS
  1440.       /* static unsigned int _OBJC_SELECTOR_REFERENCES_n = ...; */
  1441.       decl_specs = tree_cons (NULLT, ridpointers[(int) RID_UNSIGNED], sc_spec);
  1442.       decl_specs = tree_cons (NULLT, ridpointers[(int) RID_INT], decl_specs);
  1443.       var_decl = name;
  1444. #endif
  1445.  
  1446. #ifdef OBJC_SELS_R_STRUCT_PTRS
  1447.       /* static struct objc_selector *_OBJC_SELECTOR_REFERENCES_n = ...; */
  1448.       decl_specs = tree_cons (NULLT,
  1449.                   xref_tag (RECORD_TYPE,
  1450.                     get_identifier (TAG_SELECTOR)),
  1451.                   sc_spec);
  1452.       var_decl = build1 (INDIRECT_REF, NULLT, name);
  1453. #endif
  1454.  
  1455.       /* the `decl' that is returned from start_decl is the one that we
  1456.     * forward declared in `build_selector_reference()'
  1457.     */
  1458.       decl = start_decl (var_decl, decl_specs, 1); 
  1459.  
  1460.       finish_decl (decl, expr, NULLT);
  1461.     }
  1462. }
  1463.  
  1464. static void
  1465. add_class_reference (ident)
  1466.      tree ident;
  1467. {
  1468.   tree chain;
  1469.  
  1470.   if (chain = cls_ref_chain)
  1471.     {
  1472.       tree tail;
  1473.       do
  1474.         {
  1475.       if (ident == TREE_VALUE (chain))
  1476.         return;
  1477.  
  1478.       tail = chain;
  1479.       chain = TREE_CHAIN (chain);
  1480.         }
  1481.       while (chain);
  1482.  
  1483.       /* append to the end of the list */
  1484.       TREE_CHAIN (tail) = perm_tree_cons (NULLT, ident, NULLT);
  1485.     }
  1486.   else
  1487.     cls_ref_chain = perm_tree_cons (NULLT, ident, NULLT);
  1488. }
  1489.  
  1490. /*
  1491.  * sel_ref_chain is a list whose "value" fields will be instances of
  1492.  * identifier_node that represent the selector.
  1493.  */
  1494. static tree
  1495. build_selector_reference (ident)
  1496.      tree ident;
  1497. {
  1498.   tree *chain = &sel_ref_chain;
  1499.   tree decl;
  1500.  
  1501.   while (*chain)
  1502.     {
  1503.       if (TREE_VALUE (*chain) == ident)
  1504.     return TREE_PURPOSE (*chain);
  1505.  
  1506.       chain = &TREE_CHAIN (*chain);
  1507.     }
  1508.   
  1509.   decl = build_selector_reference_decl (ident);
  1510.   
  1511.   *chain = perm_tree_cons (decl, ident, NULLT);
  1512.   
  1513.   return decl;
  1514. }
  1515.  
  1516. /*
  1517.  * sel_refdef_chain is a list whose "value" fields will be instances of
  1518.  * identifier_node that represent the selector. It returns the offset of
  1519.  * the selector from the beginning of the _OBJC_STRINGS pool. This offset
  1520.  * is typically used by "init_selector ()" during code generation.
  1521.  */
  1522.  
  1523. /* For each string section we have a chain which maps identifier nodes to
  1524.    decls for the strings. */
  1525. static tree
  1526. add_objc_string (ident, section)
  1527.      tree ident;
  1528.      enum stringSection section;
  1529. {
  1530.   tree *chain, decl;
  1531.  
  1532.   if (section == class_names)
  1533.     chain = &class_names_chain;
  1534.   else if (section == meth_var_names)
  1535.     chain = &meth_var_names_chain;
  1536.   else if (section == meth_var_types)
  1537.     chain = &meth_var_types_chain;
  1538.  
  1539.   while (*chain)
  1540.     {
  1541.       if (TREE_VALUE (*chain) == ident)
  1542.         return TREE_PURPOSE (*chain);
  1543.       
  1544.       chain = &TREE_CHAIN (*chain);
  1545.     }
  1546.   
  1547.   decl = build_objc_string_decl (ident, section);
  1548.   
  1549.   *chain = perm_tree_cons (decl, ident, NULLT);
  1550.   
  1551.   return decl;
  1552. }
  1553.  
  1554. static tree
  1555. build_objc_string_decl (name, section)
  1556.      tree name;
  1557.      enum stringSection section;
  1558. {
  1559.   tree decl, ident;
  1560.   char buf[256];
  1561.   struct obstack *save_current_obstack = current_obstack;
  1562.   struct obstack *save_rtl_obstack = rtl_obstack;
  1563.   static int class_names_idx = 0;
  1564.   static int meth_var_names_idx = 0;
  1565.   static int meth_var_types_idx = 0;
  1566.   
  1567.   if (section == class_names)
  1568.     sprintf (buf, "_OBJC_CLASS_NAME_%d", class_names_idx++);
  1569.   else if (section == meth_var_names)
  1570.     sprintf (buf, "_OBJC_METH_VAR_NAME_%d", meth_var_names_idx++);
  1571.   else if (section == meth_var_types)
  1572.     sprintf (buf, "_OBJC_METH_VAR_TYPE_%d", meth_var_types_idx++);
  1573.   
  1574.   rtl_obstack = current_obstack = &permanent_obstack;
  1575.   ident = get_identifier (buf);
  1576.   
  1577.   decl = build_decl (VAR_DECL, ident, build_array_type (char_type_node, 0));
  1578.   TREE_EXTERNAL (decl) = 1;
  1579.   TREE_PUBLIC (decl) = 1;
  1580.   TREE_USED (decl) = 1;
  1581.   TREE_READONLY (decl) = 1;
  1582.   TREE_CONSTANT (decl) = 1;
  1583.   
  1584.   make_decl_rtl (decl, 0, 1); /* usually called from `rest_of_decl_compilation' */
  1585.   pushdecl_top_level (decl);  /* our `extended/custom' pushdecl in c-decl.c */
  1586.   
  1587.   current_obstack = save_current_obstack;
  1588.   rtl_obstack = save_rtl_obstack;
  1589.   
  1590.   return decl;
  1591. }
  1592.  
  1593.  
  1594. void
  1595. objc_declare_alias (alias_ident, class_ident)
  1596.      tree alias_ident;
  1597.      tree class_ident;
  1598. {
  1599.   if (!doing_objc_thang)
  1600.     objc_fatal ();
  1601.  
  1602.   if (is_class_name (class_ident) != class_ident)
  1603.     warning ("Cannot find class `%s'", IDENTIFIER_POINTER (class_ident));
  1604.   else if (is_class_name (alias_ident))
  1605.     warning ("Class `%s' already exists", IDENTIFIER_POINTER (alias_ident));
  1606.   else
  1607.     alias_chain = tree_cons (class_ident, alias_ident, alias_chain);
  1608. }
  1609.  
  1610. void
  1611. objc_declare_class (ident_list)
  1612.      tree ident_list;
  1613. {
  1614.   tree list;
  1615.  
  1616.   if (!doing_objc_thang)
  1617.     objc_fatal ();
  1618.  
  1619.   for (list = ident_list; list; list = TREE_CHAIN (list))
  1620.     {
  1621.       tree ident = TREE_VALUE (list);
  1622.       tree decl;
  1623.  
  1624.       if ((decl = lookup_name (ident)) != 0)
  1625.     {
  1626.       error ("`%s' redeclared as different kind of symbol",
  1627.           IDENTIFIER_POINTER (ident));
  1628.       error_with_decl (decl, "previous declaration of `%s'");
  1629.     }
  1630.  
  1631.       if (! is_class_name (ident))
  1632.         {
  1633.       tree record = xref_tag (RECORD_TYPE, ident);
  1634.  
  1635.       TREE_STATIC_TEMPLATE (record) = 1;
  1636.       class_chain = tree_cons (NULLT, ident, class_chain);
  1637.     }
  1638.     }
  1639. }
  1640.  
  1641. tree
  1642. is_class_name (ident)
  1643.      tree ident;
  1644. {
  1645.   tree chain;
  1646.  
  1647.   if (lookup_interface (ident))
  1648.     return ident;
  1649.  
  1650.   for (chain = class_chain; chain; chain = TREE_CHAIN (chain))
  1651.     {
  1652.       if (ident == TREE_VALUE (chain))
  1653.     return ident;
  1654.     }
  1655.  
  1656.   for (chain = alias_chain; chain; chain = TREE_CHAIN (chain))
  1657.     {
  1658.       if (ident == TREE_VALUE (chain))
  1659.     return TREE_PURPOSE (chain);
  1660.     }
  1661.  
  1662.   return 0;
  1663. }
  1664.  
  1665. tree
  1666. lookup_interface (ident)
  1667.      tree ident;
  1668. {
  1669.   tree chain;
  1670.  
  1671.   for (chain = interface_chain; chain; chain = TREE_CHAIN (chain))
  1672.     {
  1673.       if (ident == CLASS_NAME (chain))
  1674.     return chain;
  1675.     }
  1676.  
  1677.   return NULLT;
  1678. }
  1679.  
  1680. static tree
  1681. objc_copy_list (list, head)
  1682.      tree list;
  1683.      tree *head;
  1684. {
  1685.   tree newlist = NULL_TREE, tail = NULL_TREE;
  1686.  
  1687.   while (list)
  1688.     {
  1689.       tail = copy_node (list);
  1690.  
  1691.       /* the following statement fixes a bug when inheriting instance
  1692.      variables that are declared to be bitfields. finish_struct () expects
  1693.      to find the width of the bitfield in DECL_INITIAL (), which it
  1694.      nulls out after processing the decl of the super class...rather
  1695.      than change the way finish_struct () works (which is risky),
  1696.      I create the situation it expects...s.naroff (7/23/89).
  1697.      */
  1698.       if (DECL_BIT_FIELD (tail) && DECL_INITIAL (tail) == 0)
  1699.     DECL_INITIAL (tail) = build_int_2 (DECL_FRAME_SIZE (tail), 0);
  1700.  
  1701.       newlist = chainon (newlist, tail);
  1702.       list = TREE_CHAIN (list);
  1703.     }
  1704.   *head = newlist;
  1705.   return tail;
  1706. }
  1707.  
  1708. /* used by:
  1709.  * build_private_template (), get_class_ivars (), and continue_class ().
  1710.  */
  1711. /* COPY is 1 when called from @defs.  In this case copy all fields.
  1712.    Otherwise don't copy leaf ivars since we rely on them being side-effected
  1713.    exactly once by finish_struct().  (mself 7/26/91) */
  1714. static tree
  1715. build_ivar_chain (interface, copy)
  1716.      tree interface;
  1717.      int copy;
  1718. {
  1719.   tree my_name, super_name, ivar_chain;
  1720.  
  1721.   my_name = CLASS_NAME (interface);
  1722.   super_name = CLASS_SUPER_NAME (interface);
  1723.   
  1724.   /* Possibly copy leaf ivars.  */
  1725.   if (copy)
  1726.     objc_copy_list (CLASS_IVARS (interface), &ivar_chain);
  1727.   else
  1728.     ivar_chain = CLASS_IVARS (interface);
  1729.  
  1730.   while (super_name)
  1731.     {
  1732.       tree op1;
  1733.       tree super_interface = lookup_interface (super_name);
  1734.  
  1735.       if (!super_interface)
  1736.         {
  1737.       /* fatal did not work with 2 args...should fix */
  1738.       error ("Cannot find interface declaration for `%s', superclass of `%s'",
  1739.          IDENTIFIER_POINTER (super_name), IDENTIFIER_POINTER (my_name));
  1740.       exit (34);
  1741.         }
  1742.       if (super_interface == interface)
  1743.         {
  1744.           fatal ("Circular inheritance in interface declaration for `%s'",
  1745.                  IDENTIFIER_POINTER (super_name));
  1746.         }
  1747.       interface = super_interface;
  1748.       my_name = CLASS_NAME (interface);
  1749.       super_name = CLASS_SUPER_NAME (interface);
  1750.  
  1751.       op1 = CLASS_IVARS (interface);
  1752.       if (op1)
  1753.         {
  1754.       tree head, tail = objc_copy_list (op1, &head);
  1755.  
  1756.       /* prepend super class ivars...make a copy of the list, we
  1757.        * do not want to alter the original.
  1758.        */
  1759.       TREE_CHAIN (tail) = ivar_chain;
  1760.       ivar_chain = head;
  1761.         }
  1762.     }
  1763.   return ivar_chain;
  1764. }
  1765.  
  1766. /*
  1767.  *  struct <classname> {
  1768.  *    struct objc_class *isa;
  1769.  *    ...
  1770.  *  };
  1771.  */
  1772. static tree
  1773. build_private_template (class)
  1774.      tree class;
  1775. {
  1776.   tree ivar_context;
  1777.  
  1778.   if (CLASS_STATIC_TEMPLATE (class))
  1779.     {
  1780.       _PRIVATE_record = CLASS_STATIC_TEMPLATE (class);
  1781.       ivar_context = TYPE_FIELDS (CLASS_STATIC_TEMPLATE (class));
  1782.     }
  1783.   else
  1784.     {
  1785.       _PRIVATE_record = start_struct (RECORD_TYPE, CLASS_NAME (class));
  1786.  
  1787.       ivar_context = build_ivar_chain (class, 0);
  1788.  
  1789.       finish_struct (_PRIVATE_record, ivar_context);
  1790.  
  1791.       CLASS_STATIC_TEMPLATE (class) = _PRIVATE_record;
  1792.  
  1793.       /* mark this record as class template - for class type checking */
  1794.       TREE_STATIC_TEMPLATE (_PRIVATE_record) = 1;
  1795.     }
  1796.   instance_type = groktypename (
  1797.                 build_tree_list (build_tree_list (NULLT, _PRIVATE_record),
  1798.                          build1 (INDIRECT_REF, NULLT, NULLT)));
  1799.   return ivar_context;
  1800. }
  1801.  
  1802. /* begin code generation for protocols... */
  1803.  
  1804. /*
  1805.  *  struct objc_protocol {
  1806.  *    char *protocol_name;
  1807.  *    struct objc_protocol **protocol_list;
  1808.  *    struct objc_method_desc *instance_methods;
  1809.  *    struct objc_method_desc *class_methods;
  1810.  *  };
  1811.  */
  1812. static tree
  1813. build_protocol_template ()
  1814. {
  1815.   tree decl_specs, field_decl, field_decl_chain;
  1816.   tree template;
  1817.  
  1818.   template = start_struct (RECORD_TYPE, get_identifier (_TAG_PROTOCOL));
  1819.  
  1820.   /* struct objc_class *isa; */
  1821.  
  1822.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  1823.                     get_identifier (_TAG_CLASS)));
  1824.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("isa"));
  1825.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1826.   field_decl_chain = field_decl;
  1827.  
  1828.   /* char *protocol_name; */
  1829.  
  1830.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_CHAR]);
  1831.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("protocol_name"));
  1832.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1833.   chainon (field_decl_chain, field_decl);
  1834.  
  1835.   /* struct objc_protocol **protocol_list; */
  1836.  
  1837.   decl_specs = build_tree_list (NULLT, template);
  1838.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("protocol_list"));
  1839.   field_decl = build1 (INDIRECT_REF, NULLT, field_decl);
  1840.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1841.   chainon (field_decl_chain, field_decl);
  1842.  
  1843.   /* struct objc_method_list *instance_methods; */
  1844.  
  1845.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  1846.                     get_identifier (_TAG_METHOD_PROTOTYPE_LIST)));
  1847.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("instance_methods"));
  1848.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1849.   chainon (field_decl_chain, field_decl);
  1850.  
  1851.   /* struct objc_method_list *class_methods; */
  1852.  
  1853.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  1854.                     get_identifier (_TAG_METHOD_PROTOTYPE_LIST)));
  1855.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("class_methods"));
  1856.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1857.   chainon (field_decl_chain, field_decl);
  1858.  
  1859.   return finish_struct (template, field_decl_chain);
  1860. }
  1861.  
  1862. static tree
  1863. build_descriptor_table_initializer (entries, size)
  1864.      tree entries;
  1865.      int *size;
  1866. {
  1867.   tree initlist = NULLT;
  1868.  
  1869.   do
  1870.     {
  1871.       initlist = tree_cons (NULLT, build_selector (METHOD_SEL_NAME (entries)), initlist);
  1872.  
  1873.       initlist = tree_cons (NULLT, add_objc_string (METHOD_ENCODING (entries), meth_var_types), initlist);
  1874.  
  1875.       (*size)++;
  1876.       entries = TREE_CHAIN (entries);
  1877.     }
  1878.   while (entries);
  1879.  
  1880.   return build_nt (CONSTRUCTOR, NULLT, nreverse (initlist));
  1881. }
  1882.  
  1883. /*
  1884.  *  struct objc_method_prototype_list {
  1885.  *    int count;
  1886.  *    struct objc_method_prototype {
  1887.  *    SEL name;
  1888.  *    char *types;
  1889.  *    } list[1];    
  1890.  *  };
  1891.  */
  1892. static tree
  1893. build_method_prototype_list_template (list_type, size)
  1894.      tree list_type;
  1895.      int size;
  1896. {
  1897.   tree objc_ivar_list_id, objc_ivar_list_record;
  1898.   tree decl_specs, field_decl, field_decl_chain;
  1899.  
  1900.   /* generate an unnamed struct definition */
  1901.  
  1902.   objc_ivar_list_record = start_struct (RECORD_TYPE, NULLT); 
  1903.  
  1904.   /* int method_count; */
  1905.  
  1906.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_INT]);
  1907.   field_decl = get_identifier ("method_count");
  1908.  
  1909.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1910.   field_decl_chain = field_decl;
  1911.  
  1912.   /* struct objc_method method_list[]; */
  1913.  
  1914.   decl_specs = build_tree_list (NULLT, list_type);
  1915.   field_decl = build_nt (ARRAY_REF, get_identifier ("method_list"),
  1916.              build_int_2 (size, 0));
  1917.  
  1918.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1919.   chainon (field_decl_chain, field_decl);
  1920.  
  1921.   finish_struct (objc_ivar_list_record, field_decl_chain);
  1922.  
  1923.   return objc_ivar_list_record;
  1924. }
  1925.  
  1926. static tree
  1927. build_method_prototype_template ()
  1928. {
  1929.   tree proto_record;
  1930.   tree decl_specs, field_decl, field_decl_chain, parms;
  1931.  
  1932.   proto_record = start_struct (RECORD_TYPE, get_identifier (_TAG_METHOD_PROTOTYPE));
  1933.  
  1934. #ifdef OBJC_SELS_R_INTS
  1935.   /* unsigned int _cmd; */
  1936.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_UNSIGNED], NULLT);
  1937.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_INT], decl_specs);
  1938.   field_decl = get_identifier ("_cmd");
  1939. #endif
  1940.  
  1941. #ifdef OBJC_SELS_R_STRUCT_PTRS
  1942.   /* struct objc_selector *_cmd; */
  1943.   decl_specs = tree_cons (NULLT, xref_tag (RECORD_TYPE,
  1944.                   get_identifier (TAG_SELECTOR)), NULLT);
  1945.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("_cmd"));
  1946. #endif
  1947.  
  1948.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1949.   field_decl_chain = field_decl;
  1950.  
  1951.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_CHAR], NULLT);
  1952.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("method_types"));
  1953.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  1954.   chainon (field_decl_chain, field_decl);
  1955.  
  1956.   finish_struct (proto_record, field_decl_chain);
  1957.  
  1958.   return proto_record;
  1959. }
  1960.  
  1961. static tree
  1962. encode_method_prototype (method_decl, func_decl)
  1963.       tree method_decl;
  1964.       tree func_decl;
  1965. {
  1966.   tree parms;
  1967.   int stack_size = 0, i;
  1968.   tree user_args;
  1969.   
  1970.   bzero (utlbuf, BUFSIZE);
  1971.  
  1972.   /* `oneway' and 'bycopy', for remote object are the only method qualifiers */
  1973.   encode_type_qualifiers(TREE_PURPOSE(TREE_TYPE(method_decl)), utlbuf);
  1974.  
  1975.   /* C type */  
  1976.   encode_type (TREE_TYPE (TREE_TYPE (func_decl)), utlbuf, 
  1977.           utlbuf+strlen(utlbuf), OBJC_ENCODE_INLINE_DEFS);
  1978.        
  1979.   /* stack size */
  1980.   for (parms = DECL_ARGUMENTS (func_decl); parms;
  1981.        parms = TREE_CHAIN (parms))
  1982.     stack_size += TREE_INT_CST_LOW (TYPE_SIZE (DECL_ARG_TYPE (parms)))
  1983.           / BITS_PER_UNIT;
  1984.  
  1985.   sprintf (&utlbuf[strlen (utlbuf)], "%d", stack_size);
  1986.  
  1987.   user_args = METHOD_SEL_ARGS (method_decl);
  1988.  
  1989.   /* argument types */
  1990.   for (parms = DECL_ARGUMENTS (func_decl), i = 0; parms;
  1991.        parms = TREE_CHAIN (parms), i++)
  1992.     {
  1993.       int offset_in_bytes;
  1994.   
  1995.       /* process argument qualifiers for user supplied arguments */
  1996.       if (i > 1)
  1997.         {
  1998.         encode_type_qualifiers(TREE_PURPOSE(TREE_TYPE(user_args)), utlbuf);
  1999.     user_args = TREE_CHAIN(user_args);
  2000.      }
  2001.     
  2002.       /* type */ 
  2003.       encode_type (TREE_TYPE (parms), utlbuf, utlbuf+strlen(utlbuf), 
  2004.                   OBJC_ENCODE_INLINE_DEFS);
  2005.   
  2006.       /* compute offset */
  2007.       if (GET_CODE (DECL_INCOMING_RTL (parms)) == MEM)
  2008.         {
  2009.       rtx addr = XEXP (DECL_INCOMING_RTL (parms), 0);
  2010.       
  2011.       /* ??? Here we assume that the parm address is indexed
  2012.           off the frame pointer or arg pointer.
  2013.           If that is not true, we produce meaningless results,
  2014.           but do not crash.  */
  2015.       if (GET_CODE (addr) == PLUS
  2016.           && GET_CODE (XEXP (addr, 1)) == CONST_INT)
  2017.         offset_in_bytes = INTVAL (XEXP (addr, 1));
  2018.       else
  2019.         offset_in_bytes = 0;
  2020.       
  2021.       /* This is the case where the parm is passed as an int or double
  2022.           and it is converted to a char, short or float and stored back
  2023.           in the parmlist.  In this case, describe the parm
  2024.           with the variable's declared type, and adjust the address
  2025.           if the least significant bytes (which we are using) are not
  2026.           the first ones.  */
  2027. #if BYTES_BIG_ENDIAN
  2028.       if (TREE_TYPE (parms) != DECL_ARG_TYPE (parms))
  2029.         offset_in_bytes += (GET_MODE_SIZE (TYPE_MODE (DECL_ARG_TYPE (parms)))
  2030.                 - GET_MODE_SIZE (GET_MODE (DECL_RTL (parms))));
  2031. #endif
  2032.     }
  2033.       else
  2034.         offset_in_bytes = 0;
  2035.       
  2036.       /* The "+ 4" is a total hack to account for the return pc and
  2037.          saved fp on the 68k.  We should redefine this format! */
  2038.       sprintf (&utlbuf[strlen (utlbuf)], "%d", offset_in_bytes + 8);
  2039.     }
  2040.  
  2041.   return get_identifier (utlbuf);
  2042. }
  2043.  
  2044. static tree
  2045. generate_descriptor_table (type, name, size, list, proto)
  2046.      tree type;
  2047.      char *name;
  2048.      int size;
  2049.      tree list;
  2050.      tree proto;
  2051. {
  2052.   tree sc_spec, decl_specs, decl, initlist;
  2053.  
  2054.   sc_spec = tree_cons (NULLT, ridpointers[(int) RID_STATIC], NULLT);
  2055.   decl_specs = tree_cons (NULLT, type, sc_spec);
  2056.  
  2057.   decl = start_decl (synth_id_with_class_suffix (name, proto), 
  2058.                 decl_specs, 1);
  2059.  
  2060.   initlist = build_tree_list (NULLT, build_int_2 (size, 0));
  2061.   initlist = tree_cons (NULLT, list, initlist);
  2062.  
  2063.   finish_decl (decl, build_nt (CONSTRUCTOR, NULLT, nreverse (initlist)), NULLT);
  2064.  
  2065.   return decl;
  2066. }
  2067.  
  2068. static void
  2069. generate_method_descriptors (protocol)    /* generate_dispatch_tables */
  2070.   tree protocol;
  2071. {
  2072.   static tree objc_method_prototype_template;
  2073.   tree initlist, chain, method_list_template;
  2074.   tree cast, variable_length_type;
  2075.   int size;
  2076.  
  2077.   if (!objc_method_prototype_template)
  2078.     objc_method_prototype_template = build_method_prototype_template ();
  2079.  
  2080.   cast = build_tree_list (build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  2081.                 get_identifier (_TAG_METHOD_PROTOTYPE_LIST))), NULLT);
  2082.   variable_length_type = groktypename (cast);
  2083.  
  2084.   chain = PROTOCOL_CLS_METHODS (protocol);
  2085.   if (chain)
  2086.     {
  2087.       size = 0;
  2088.       
  2089.       initlist = build_descriptor_table_initializer (chain, &size);
  2090.  
  2091.       method_list_template = build_method_prototype_list_template(
  2092.                 objc_method_prototype_template, size);
  2093.  
  2094.       _OBJC_CLASS_METHODS_decl = 
  2095.         generate_descriptor_table (method_list_template,
  2096.                      "_OBJC_PROTOCOL_CLASS_METHODS", 
  2097.                      size, initlist, protocol);
  2098.       /* cast! */
  2099.       TREE_TYPE (_OBJC_CLASS_METHODS_decl) = variable_length_type;
  2100.     }
  2101.   else
  2102.     _OBJC_CLASS_METHODS_decl = 0;
  2103.  
  2104.   chain = PROTOCOL_NST_METHODS (protocol);
  2105.   if (chain)
  2106.     {
  2107.       size = 0;
  2108.       initlist = build_descriptor_table_initializer (chain, &size);
  2109.  
  2110.       method_list_template = build_method_prototype_list_template(
  2111.                 objc_method_prototype_template, size);
  2112.  
  2113.       _OBJC_INSTANCE_METHODS_decl = 
  2114.         generate_descriptor_table (method_list_template,
  2115.                      "_OBJC_PROTOCOL_INSTANCE_METHODS", 
  2116.                      size, initlist, protocol);
  2117.       /* cast! */
  2118.       TREE_TYPE (_OBJC_INSTANCE_METHODS_decl) = variable_length_type;
  2119.     }
  2120.   else
  2121.     _OBJC_INSTANCE_METHODS_decl = 0;
  2122. }
  2123.  
  2124. static tree
  2125. build_tmp_function_decl()
  2126. {
  2127.   tree decl_specs, expr_decl, parms;
  2128.   
  2129.   /* struct objc_object *objc_xxx (id, SEL, ...); */
  2130.   pushlevel (0);
  2131.   decl_specs = build_tree_list (NULLT, objc_object_reference);
  2132.   push_parm_decl (build_tree_list (decl_specs,
  2133.                    build1 (INDIRECT_REF, NULLT, NULLT)));
  2134.  
  2135.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  2136.                       get_identifier (TAG_SELECTOR)));
  2137.   expr_decl = build1 (INDIRECT_REF, NULLT, NULLT);
  2138.  
  2139.   push_parm_decl (build_tree_list (decl_specs, expr_decl));
  2140.   parms = get_parm_info (0);
  2141.   poplevel (0, 0, 0);
  2142.  
  2143.   decl_specs = build_tree_list (NULLT, objc_object_reference);
  2144.   expr_decl = build_nt (CALL_EXPR, get_identifier("objc_xxx"), parms, NULLT);
  2145.   expr_decl = build1 (INDIRECT_REF, NULLT, expr_decl);
  2146.  
  2147.   return define_decl (expr_decl, decl_specs);
  2148. }
  2149.  
  2150. static void
  2151. hack_method_prototype(nst_methods, _tmp_decl)
  2152.    tree nst_methods;
  2153.    tree _tmp_decl;
  2154. {
  2155.       tree parms;
  2156.  
  2157.       /* Hack to avoid problem with static typing of self arg. */
  2158.       TREE_CODE(nst_methods) = CLASS_METHOD_DECL;
  2159.       start_method_def(nst_methods);
  2160.       TREE_CODE(nst_methods) = INSTANCE_METHOD_DECL;
  2161.  
  2162.       if (METHOD_ADD_ARGS (nst_methods) == (tree)1)
  2163.         parms = get_parm_info (0); /* we have a `, ...' */
  2164.       else
  2165.         parms = get_parm_info (1); /* place a `void_at_end' */
  2166.  
  2167.       poplevel (0, 0, 0);    /* must be called BEFORE "start_function ()" */
  2168.   
  2169.       /* usually called from store_parm_decls()->init_function_start() */ 
  2170.  
  2171.       init_emit();    // needed to make assign_parms() work (with -O)
  2172.  
  2173.       DECL_ARGUMENTS(_tmp_decl) = TREE_PURPOSE(parms);
  2174.       {
  2175.       /* code take from start_function() */
  2176.       tree restype = TREE_TYPE (TREE_TYPE (_tmp_decl));
  2177.       /* Promote the value to int before returning it.  */
  2178.       if (TREE_CODE (restype) == INTEGER_TYPE
  2179.           && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
  2180.         restype = integer_type_node;
  2181.       DECL_RESULT (_tmp_decl) = build_decl (RESULT_DECL, 0, restype);
  2182.       }
  2183.       
  2184.       /* typically called from expand_function_start() for function definitions */
  2185.       assign_parms(_tmp_decl, 0);
  2186.       
  2187.       /* install return type */
  2188.       TREE_TYPE (TREE_TYPE (_tmp_decl)) = groktypename (TREE_TYPE (nst_methods));
  2189. }
  2190.  
  2191. static void 
  2192. generate_protocol_references (plist)
  2193.      tree plist;
  2194.      tree lproto;
  2195.  
  2196.      /* forward declare protocols referenced */
  2197.      for (lproto = plist; lproto; lproto = TREE_CHAIN(lproto)) {
  2198.         tree proto = TREE_VALUE(lproto);
  2199.  
  2200.         if ((TREE_CODE(proto) == PROTOCOL_INTERFACE) &&
  2201.               PROTOCOL_NAME(proto)) {
  2202.  
  2203.           if (!PROTOCOL_FORWARD_DECL (proto)) 
  2204.             build_protocol_reference(proto);
  2205.  
  2206.           if (PROTOCOL_LIST (proto))
  2207.             generate_protocol_references(PROTOCOL_LIST (proto));
  2208.         }
  2209.      }     
  2210. }
  2211.  
  2212. static void 
  2213. generate_protocols ()
  2214. {
  2215.   tree p, _tmp_decl, encoding;
  2216.   tree sc_spec, decl_specs, decl;
  2217.   tree initlist, protocol_name_expr, refs_decl, refs_expr;
  2218.   tree cast_type2 = 0, cast_type = 0;
  2219.   
  2220.   _tmp_decl = build_tmp_function_decl();
  2221.  
  2222.   if (!objc_protocol_template)
  2223.     objc_protocol_template = build_protocol_template ();
  2224.  
  2225.   /* if a protocol was directly referenced, pull in indirect references */
  2226.   for (p = protocol_chain; p; p = TREE_CHAIN(p))
  2227.     if (PROTOCOL_FORWARD_DECL (p) && PROTOCOL_LIST (p))
  2228.       generate_protocol_references(PROTOCOL_LIST (p));
  2229.  
  2230.   for (p = protocol_chain; p; p = TREE_CHAIN(p))
  2231.     {
  2232.     tree nst_methods = PROTOCOL_NST_METHODS(p);
  2233.     tree cls_methods = PROTOCOL_CLS_METHODS(p);
  2234.     char buf[256];
  2235.  
  2236.     /* if protocol wasn't referenced, don't generate any code */        
  2237.     if (!PROTOCOL_FORWARD_DECL (p))
  2238.       continue;
  2239.  
  2240.     /* Make sure we link in the Protocol class. */
  2241.     add_class_reference (get_identifier (PROTOCOL_OBJECT_CLASS_NAME));
  2242.  
  2243.     while (nst_methods)
  2244.       {
  2245.       hack_method_prototype(nst_methods, _tmp_decl);
  2246.       encoding = encode_method_prototype(nst_methods, _tmp_decl);
  2247.       METHOD_ENCODING (nst_methods) = encoding;
  2248.       
  2249.       nst_methods = TREE_CHAIN (nst_methods);
  2250.       }
  2251.  
  2252.     while (cls_methods)
  2253.       {
  2254.       hack_method_prototype(cls_methods, _tmp_decl);
  2255.       encoding = encode_method_prototype(cls_methods, _tmp_decl);
  2256.       METHOD_ENCODING (cls_methods) = encoding;
  2257.       
  2258.       cls_methods = TREE_CHAIN (cls_methods);
  2259.       }
  2260.     generate_method_descriptors(p);
  2261.  
  2262.     if (PROTOCOL_LIST(p))
  2263.       refs_decl = generate_protocol_list(p);
  2264.     else
  2265.       refs_decl = 0;
  2266.  
  2267.     /* static struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
  2268.  
  2269.     sc_spec = tree_cons (NULLT, ridpointers[(int) RID_STATIC], NULLT);
  2270.     decl_specs = tree_cons (NULLT, objc_protocol_template, sc_spec);
  2271.  
  2272.     decl = start_decl (
  2273.               synth_id_with_class_suffix ("_OBJC_PROTOCOL", p),
  2274.         decl_specs, 1);
  2275.  
  2276.     protocol_name_expr = add_objc_string (PROTOCOL_NAME (p), class_names);
  2277.  
  2278.     if (refs_decl)
  2279.       {
  2280.       if (!cast_type2)
  2281.         cast_type2 = groktypename (build_tree_list(
  2282.             build_tree_list (NULLT,    objc_protocol_template), 
  2283.             build1 (INDIRECT_REF, NULLT, 
  2284.               build1 (INDIRECT_REF, NULLT, NULLT))));
  2285.  
  2286.       refs_expr = build_unary_op (ADDR_EXPR, refs_decl, 0);
  2287.       TREE_TYPE(refs_expr) = cast_type2;
  2288.       }
  2289.     else
  2290.       refs_expr = build_int_2 (0, 0);
  2291.  
  2292.     /* _OBJC_INSTANCE_METHODS_decl/_OBJC_CLASS_METHODS_decl are set
  2293.        by generate_method_descriptors(), which is called above */
  2294.     initlist = build_protocol_initializer (protocol_name_expr, refs_expr,
  2295.                      _OBJC_INSTANCE_METHODS_decl, 
  2296.                      _OBJC_CLASS_METHODS_decl);
  2297.     finish_decl (decl, initlist, NULLT);
  2298.  
  2299.     /* Mark the decl as used to avoid "defined but not used" warning. */
  2300.     TREE_USED (decl) = 1;
  2301.     }
  2302. }
  2303.  
  2304. static tree
  2305. build_protocol_initializer (protocol_name, protocol_list,
  2306.                 instance_methods, class_methods)
  2307.      tree protocol_name;
  2308.      tree protocol_list;
  2309.      tree instance_methods;
  2310.      tree class_methods;
  2311. {
  2312.   tree initlist = NULLT, expr;
  2313.   static tree cast_type = 0;
  2314.  
  2315.   if (!cast_type)
  2316.     cast_type = groktypename (build_tree_list (
  2317.                    build_tree_list (NULLT, 
  2318.                      xref_tag (RECORD_TYPE, get_identifier (_TAG_CLASS))), 
  2319.                      build1 (INDIRECT_REF, NULLT, NULLT)));
  2320.  
  2321.   /* filling the "isa" in with one allows the runtime system to
  2322.      detect that the version change...should remove before final release */
  2323.  
  2324.   expr = build_int_2 (PROTOCOL_VERSION, 0);
  2325.   TREE_TYPE(expr) = cast_type;
  2326.   initlist = tree_cons (NULLT, expr, initlist);
  2327.   initlist = tree_cons (NULLT, protocol_name, initlist);
  2328.   initlist = tree_cons (NULLT, protocol_list, initlist);
  2329.  
  2330.   if (!instance_methods)
  2331.     initlist = tree_cons (NULLT, build_int_2 (0, 0), initlist);
  2332.   else
  2333.     {
  2334.       expr = build_unary_op (ADDR_EXPR, instance_methods, 0);
  2335.       initlist = tree_cons (NULLT, expr, initlist);
  2336.     }
  2337.   if (!class_methods)
  2338.     initlist = tree_cons (NULLT, build_int_2 (0, 0), initlist);
  2339.   else
  2340.     {
  2341.       expr = build_unary_op (ADDR_EXPR, class_methods, 0);
  2342.       initlist = tree_cons (NULLT, expr, initlist);
  2343.     }
  2344.   return build_nt (CONSTRUCTOR, NULLT, nreverse (initlist));
  2345. }
  2346.  
  2347. /* end code generation for protocols... */
  2348.  
  2349. /*
  2350.  *  struct objc_category {
  2351.  *    char *category_name;
  2352.  *    char *class_name;
  2353.  *    struct objc_method_list *instance_methods;
  2354.  *    struct objc_method_list *class_methods;
  2355.  *    struct objc_protocol_list *protocols;
  2356.  *  };
  2357.  */
  2358. static void
  2359. build_category_template ()
  2360. {
  2361.   tree decl_specs, field_decl, field_decl_chain;
  2362.  
  2363.   objc_category_template = start_struct (RECORD_TYPE,
  2364.                      get_identifier (_TAG_CATEGORY));
  2365.   /* char *category_name; */
  2366.  
  2367.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_CHAR]);
  2368.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("category_name"));
  2369.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2370.   field_decl_chain = field_decl;
  2371.  
  2372.   /* char *class_name; */
  2373.  
  2374.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_CHAR]);
  2375.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("class_name"));
  2376.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2377.   chainon (field_decl_chain, field_decl);
  2378.  
  2379.   /* struct objc_method_list *instance_methods; */
  2380.  
  2381.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  2382.                          get_identifier (_TAG_METHOD_LIST)));
  2383.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("instance_methods"));
  2384.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2385.   chainon (field_decl_chain, field_decl);
  2386.  
  2387.   /* struct objc_method_list *class_methods; */
  2388.  
  2389.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  2390.                          get_identifier (_TAG_METHOD_LIST)));
  2391.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("class_methods"));
  2392.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2393.   chainon (field_decl_chain, field_decl);
  2394.  
  2395.   /* struct objc_protocol **protocol_list; */
  2396.  
  2397.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  2398.                       get_identifier (_TAG_PROTOCOL)));
  2399.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("protocol_list"));
  2400.   field_decl = build1 (INDIRECT_REF, NULLT, field_decl);
  2401.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2402.   chainon (field_decl_chain, field_decl);
  2403.  
  2404.   finish_struct (objc_category_template, field_decl_chain);
  2405. }
  2406.  
  2407. /*
  2408.  *  struct objc_class {
  2409.  *    struct objc_class *isa;
  2410.  *    struct objc_class *super_class;
  2411.  *    char *name;
  2412.  *    long version;
  2413.  *    long info;
  2414.  *    long instance_size;
  2415.  *    struct objc_ivar_list *ivars;
  2416.  *    struct objc_method_list *methods;
  2417.  *    struct objc_cache *cache;
  2418.  *    struct objc_protocol_list *protocols;
  2419.  *  };
  2420.  */
  2421. static void
  2422. build_class_template ()
  2423. {
  2424.   tree decl_specs, field_decl, field_decl_chain;
  2425.  
  2426.   objc_class_template = start_struct (RECORD_TYPE, get_identifier (_TAG_CLASS));
  2427.  
  2428.   /* struct objc_class *isa; */
  2429.  
  2430.   decl_specs = build_tree_list (NULLT, objc_class_template);
  2431.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("isa"));
  2432.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2433.   field_decl_chain = field_decl;
  2434.  
  2435.   /* struct objc_class *super_class; */
  2436.  
  2437.   decl_specs = build_tree_list (NULLT, objc_class_template);
  2438.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("super_class"));
  2439.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2440.   chainon (field_decl_chain, field_decl);
  2441.  
  2442.   /* char *name; */
  2443.  
  2444.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_CHAR]);
  2445.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("name"));
  2446.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2447.   chainon (field_decl_chain, field_decl);
  2448.  
  2449.   /* long version; */
  2450.  
  2451.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_LONG]);
  2452.   field_decl = get_identifier ("version");
  2453.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2454.   chainon (field_decl_chain, field_decl);
  2455.  
  2456.   /* long info; */
  2457.  
  2458.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_LONG]);
  2459.   field_decl = get_identifier ("info");
  2460.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2461.   chainon (field_decl_chain, field_decl);
  2462.  
  2463.   /* long instance_size; */
  2464.  
  2465.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_LONG]);
  2466.   field_decl = get_identifier ("instance_size");
  2467.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2468.   chainon (field_decl_chain, field_decl);
  2469.  
  2470.   /* struct objc_ivar_list *ivars; */
  2471.  
  2472.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  2473.                          get_identifier (_TAG_IVAR_LIST)));
  2474.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("ivars"));
  2475.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2476.   chainon (field_decl_chain, field_decl);
  2477.  
  2478.   /* struct objc_method_list *methods; */
  2479.  
  2480.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  2481.                          get_identifier (_TAG_METHOD_LIST)));
  2482.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("methods"));
  2483.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2484.   chainon (field_decl_chain, field_decl);
  2485.  
  2486.   /* struct objc_cache *cache; */
  2487.  
  2488.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  2489.                          get_identifier ("objc_cache")));
  2490.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("cache"));
  2491.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2492.   chainon (field_decl_chain, field_decl);
  2493.  
  2494.   /* struct objc_protocol **protocol_list; */
  2495.  
  2496.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  2497.                       get_identifier (_TAG_PROTOCOL)));
  2498.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("protocol_list"));
  2499.   field_decl = build1 (INDIRECT_REF, NULLT, field_decl);
  2500.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2501.   chainon (field_decl_chain, field_decl);
  2502.  
  2503.   finish_struct (objc_class_template, field_decl_chain);
  2504. }
  2505.  
  2506. #ifdef OBJC_CLASS_REFS
  2507. /*
  2508.  *  struct _objc_class_ref {
  2509.  *    struct _objc_class *class;
  2510.  *  };
  2511.  */
  2512. static void
  2513. build_class_ref_template ()
  2514. {
  2515.   tree decl_specs, field_decl;
  2516.  
  2517.   objc_class_ref_template
  2518.     = start_struct (RECORD_TYPE, get_identifier ("_objc_class_ref"));
  2519.  
  2520.   /* struct objc_class *class; */
  2521.  
  2522.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE, get_identifier (TAG_CLASS)));
  2523.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("class"));
  2524.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2525.   finish_struct (objc_class_ref_template, field_decl);
  2526. }
  2527. #endif /* OBJC_CLASS_REFS */
  2528.  
  2529. /*
  2530.  * generate appropriate forward declarations for an implementation
  2531.  */
  2532. static void
  2533. synth_forward_declarations ()
  2534. {
  2535.   tree sc_spec, decl_specs, factory_id, anId;
  2536.  
  2537.   /* extern struct objc_class _OBJC_CLASS_<my_name>; */
  2538.  
  2539.   anId = synth_id_with_class_suffix ("_OBJC_CLASS", implementation_context);
  2540.  
  2541.   sc_spec = build_tree_list (NULLT, ridpointers[(int) RID_EXTERN]);
  2542.   decl_specs = tree_cons (NULLT, objc_class_template, sc_spec);
  2543.   _OBJC_CLASS_decl = define_decl (anId, decl_specs);
  2544.  
  2545.   /* extern struct objc_class _OBJC_METACLASS_<my_name>; */
  2546.  
  2547.   anId = synth_id_with_class_suffix ("_OBJC_METACLASS", implementation_context);
  2548.  
  2549.   _OBJC_METACLASS_decl = define_decl (anId, decl_specs);
  2550.  
  2551.   /* pre-build the following entities - for speed/convenience. */
  2552.  
  2553.   anId = get_identifier ("super_class");
  2554.   _clsSuper_ref = build_component_ref (_OBJC_CLASS_decl, anId);
  2555.   __clsSuper_ref = build_component_ref (_OBJC_METACLASS_decl, anId);
  2556. }
  2557.  
  2558. static void
  2559. error_with_ivar (message, decl, rawdecl)
  2560.      char *message;
  2561.      tree decl;
  2562.      tree rawdecl;
  2563. {
  2564.   extern int errorcount;
  2565.  
  2566. #ifdef REPORT_EVENT
  2567.   bzero (errbuf, BUFSIZE);
  2568.   report_event (0, current_function_decl,
  2569.         DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
  2570.         "%s `%s'", message, gen_declaration (rawdecl, errbuf), 0);
  2571. #endif
  2572.   fprintf (stderr, "%s:%d: ",
  2573.        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
  2574.   bzero (errbuf, BUFSIZE);
  2575.   fprintf (stderr, "%s `%s'\n", message, gen_declaration (rawdecl, errbuf));
  2576.   errorcount++;
  2577. }
  2578.  
  2579. #define USERTYPE(t)    (TREE_CODE (t) == RECORD_TYPE || \
  2580.              TREE_CODE (t) == UNION_TYPE ||  \
  2581.              TREE_CODE (t) == ENUMERAL_TYPE)
  2582.  
  2583. static void
  2584. check_ivars (inter, imp)
  2585.      tree inter;
  2586.      tree imp;
  2587. {
  2588.   tree intdecls = CLASS_IVARS (inter);
  2589.   tree impdecls = CLASS_IVARS (imp);
  2590.   tree rawintdecls = CLASS_RAW_IVARS (inter);
  2591.   tree rawimpdecls = CLASS_RAW_IVARS (imp);
  2592.  
  2593.   while (1)
  2594.     {
  2595.       tree t1, t2;
  2596.  
  2597.       if (intdecls == 0 && impdecls == 0)
  2598.     break;
  2599.       if (intdecls == 0 || impdecls == 0)
  2600.     {
  2601.       error ("inconsistent instance variable specification");
  2602.       break;
  2603.     }
  2604.       t1 = TREE_TYPE (intdecls); t2 = TREE_TYPE (impdecls);
  2605.  
  2606.       if (!comptypes (t1, t2))
  2607.     {
  2608.       if (DECL_NAME (intdecls) == DECL_NAME (impdecls))
  2609.         {
  2610.           error_with_ivar ("conflicting instance variable type",
  2611.                    impdecls, rawimpdecls);
  2612.           error_with_ivar ("previous declaration of",
  2613.                    intdecls, rawintdecls);
  2614.         }
  2615.       else            /* both the type and the name don't match */
  2616.         {
  2617.           error ("inconsistent instance variable specification");
  2618.           break;
  2619.         }
  2620.     }
  2621.       else if (DECL_NAME (intdecls) != DECL_NAME (impdecls))
  2622.     {
  2623.       error_with_ivar ("conflicting instance variable name",
  2624.                impdecls, rawimpdecls);
  2625.       error_with_ivar ("previous declaration of",
  2626.                intdecls, rawintdecls);
  2627.     }
  2628.       intdecls = TREE_CHAIN (intdecls);
  2629.       impdecls = TREE_CHAIN (impdecls);
  2630.       rawintdecls = TREE_CHAIN (rawintdecls);
  2631.       rawimpdecls = TREE_CHAIN (rawimpdecls);
  2632.     }
  2633. }
  2634.  
  2635. /*
  2636.  *     struct objc_super {
  2637.  *        id self;
  2638.  *        struct objc_class *class;
  2639.  *    };
  2640.  */
  2641. static tree
  2642. build_super_template ()
  2643. {
  2644.   tree record, decl_specs, field_decl, field_decl_chain;
  2645.  
  2646.   record = start_struct (RECORD_TYPE, get_identifier (_TAG_SUPER));
  2647.  
  2648.   /* struct objc_object *self; */
  2649.  
  2650.   decl_specs = build_tree_list (NULLT, objc_object_reference);
  2651.   field_decl = get_identifier ("self");
  2652.   field_decl = build1 (INDIRECT_REF, NULLT, field_decl);
  2653.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2654.   field_decl_chain = field_decl;
  2655.  
  2656.   /* struct objc_class *class; */
  2657.  
  2658.   decl_specs = get_identifier (_TAG_CLASS);
  2659.   decl_specs = build_tree_list (NULLT, xref_tag (RECORD_TYPE, decl_specs));
  2660.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("class"));
  2661.  
  2662.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2663.   chainon (field_decl_chain, field_decl);
  2664.  
  2665.   finish_struct (record, field_decl_chain);
  2666.  
  2667.   /* `struct objc_super *' */
  2668.   super_type = groktypename (build_tree_list (build_tree_list (NULLT, record),
  2669.                           build1 (INDIRECT_REF, NULLT, NULLT)));
  2670.   return record;
  2671. }
  2672.  
  2673. /*
  2674.  *     struct objc_ivar {
  2675.  *        char *ivar_name;
  2676.  *        char *ivar_type;
  2677.  *        int ivar_offset;
  2678.  *    };
  2679.  */
  2680. static tree
  2681. build_ivar_template ()
  2682. {
  2683.   tree objc_ivar_id, objc_ivar_record;
  2684.   tree decl_specs, field_decl, field_decl_chain;
  2685.  
  2686.   objc_ivar_id = get_identifier (_TAG_IVAR);
  2687.   objc_ivar_record = start_struct (RECORD_TYPE, objc_ivar_id);
  2688.  
  2689.   /* char *ivar_name; */
  2690.  
  2691.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_CHAR]);
  2692.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("ivar_name"));
  2693.  
  2694.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2695.   field_decl_chain = field_decl;
  2696.  
  2697.   /* char *ivar_type; */
  2698.  
  2699.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_CHAR]);
  2700.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("ivar_type"));
  2701.  
  2702.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2703.   chainon (field_decl_chain, field_decl);
  2704.  
  2705.   /* int ivar_offset; */
  2706.  
  2707.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_INT]);
  2708.   field_decl = get_identifier ("ivar_offset");
  2709.  
  2710.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2711.   chainon (field_decl_chain, field_decl);
  2712.  
  2713.   finish_struct (objc_ivar_record, field_decl_chain);
  2714.  
  2715.   return objc_ivar_record;
  2716. }
  2717.  
  2718. /*
  2719.  *     struct {
  2720.  *        int ivar_count;
  2721.  *        struct objc_ivar ivar_list[ivar_count];
  2722.  *    };
  2723.  */
  2724. static tree
  2725. build_ivar_list_template (list_type, size)
  2726.      tree list_type;
  2727.      int size;
  2728. {
  2729.   tree objc_ivar_list_id, objc_ivar_list_record;
  2730.   tree decl_specs, field_decl, field_decl_chain;
  2731.  
  2732.   objc_ivar_list_record = start_struct (RECORD_TYPE, NULLT);
  2733.  
  2734.   /* int ivar_count; */
  2735.  
  2736.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_INT]);
  2737.   field_decl = get_identifier ("ivar_count");
  2738.  
  2739.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2740.   field_decl_chain = field_decl;
  2741.  
  2742.   /* struct objc_ivar ivar_list[]; */
  2743.  
  2744.   decl_specs = build_tree_list (NULLT, list_type);
  2745.   field_decl = build_nt (ARRAY_REF, get_identifier ("ivar_list"),
  2746.              build_int_2 (size, 0));
  2747.  
  2748.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2749.   chainon (field_decl_chain, field_decl);
  2750.  
  2751.   finish_struct (objc_ivar_list_record, field_decl_chain);
  2752.  
  2753.   return objc_ivar_list_record;
  2754. }
  2755.  
  2756. /*
  2757.  *     struct {
  2758.  *        int method_next;
  2759.  *        int method_count;
  2760.  *        struct objc_method method_list[method_count];
  2761.  *    };
  2762.  */
  2763. static tree
  2764. build_method_list_template (list_type, size)
  2765.      tree list_type;
  2766.      int size;
  2767. {
  2768.   tree objc_ivar_list_id, objc_ivar_list_record;
  2769.   tree decl_specs, field_decl, field_decl_chain;
  2770.  
  2771.   objc_ivar_list_record = start_struct (RECORD_TYPE, NULLT);
  2772.  
  2773.   /* int method_next; */
  2774.  
  2775.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_INT]);
  2776.   field_decl = get_identifier ("method_next");
  2777.  
  2778.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2779.   field_decl_chain = field_decl;
  2780.  
  2781.   /* int method_count; */
  2782.  
  2783.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_INT]);
  2784.   field_decl = get_identifier ("method_count");
  2785.  
  2786.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2787.   chainon (field_decl_chain, field_decl);
  2788.  
  2789.   /* struct objc_method method_list[]; */
  2790.  
  2791.   decl_specs = build_tree_list (NULLT, list_type);
  2792.   field_decl = build_nt (ARRAY_REF, get_identifier ("method_list"),
  2793.              build_int_2 (size, 0));
  2794.  
  2795.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2796.   chainon (field_decl_chain, field_decl);
  2797.  
  2798.   finish_struct (objc_ivar_list_record, field_decl_chain);
  2799.  
  2800.   return objc_ivar_list_record;
  2801. }
  2802.  
  2803. static tree
  2804. build_ivar_list_initializer (field_decl, size)
  2805.      tree field_decl;
  2806.      int *size;
  2807. {
  2808.   tree initlist = NULLT;
  2809.  
  2810.   do
  2811.     {
  2812.     /* set name */
  2813.     if (DECL_NAME (field_decl))
  2814.       {
  2815.         initlist = tree_cons (NULLT, add_objc_string (DECL_NAME (field_decl), meth_var_names), initlist);
  2816.       }
  2817.     else
  2818.       {
  2819.         /* unnamed bit-field ivar (yuck). */
  2820.         initlist = tree_cons (NULLT, build_int_2 (0, 0), initlist);
  2821.       }
  2822.  
  2823.       /* set type */
  2824.       bzero (utlbuf, BUFSIZE);
  2825.       encode_field_decl (field_decl, utlbuf, utlbuf,
  2826.              OBJC_ENCODE_DONT_INLINE_DEFS);
  2827.  
  2828.       initlist = tree_cons (NULLT, add_objc_string (get_identifier (utlbuf), meth_var_types), initlist);
  2829.  
  2830.       /* set offset */
  2831.       initlist = tree_cons (NULLT,
  2832.                 build_int_2 (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field_decl)) / BITS_PER_UNIT, 0),
  2833.                 
  2834.                 initlist);
  2835.       (*size)++;
  2836.       field_decl = TREE_CHAIN (field_decl);
  2837.     }
  2838.   while (field_decl);
  2839.  
  2840.   return build_nt (CONSTRUCTOR, NULLT, nreverse (initlist));
  2841. }
  2842.  
  2843. static tree
  2844. generate_ivars_list (type, name, size, list)
  2845.      tree type;
  2846.      char *name;
  2847.      int size;
  2848.      tree list;
  2849. {
  2850.   tree sc_spec, decl_specs, decl, initlist;
  2851.  
  2852.   sc_spec = tree_cons (NULLT, ridpointers[(int) RID_STATIC], NULLT);
  2853.   decl_specs = tree_cons (NULLT, type, sc_spec);
  2854.  
  2855.   decl = start_decl (synth_id_with_class_suffix (name, implementation_context),
  2856.                  decl_specs, 1);
  2857.  
  2858.   initlist = build_tree_list (NULLT, build_int_2 (size, 0));
  2859.   initlist = tree_cons (NULLT, list, initlist);
  2860.  
  2861.   finish_decl (decl, build_nt (CONSTRUCTOR, NULLT, nreverse (initlist)), NULLT);
  2862.  
  2863.   return decl;
  2864. }
  2865.  
  2866. static void
  2867. generate_ivar_lists ()
  2868. {
  2869.   tree initlist, ivar_list_template, chain;
  2870.   tree cast, variable_length_type;
  2871.   int size;
  2872.  
  2873.   if (!objc_ivar_template)
  2874.     objc_ivar_template = build_ivar_template ();
  2875.  
  2876.   cast = build_tree_list (build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  2877.                                 get_identifier (_TAG_IVAR_LIST))), NULLT);
  2878.   variable_length_type = groktypename (cast);
  2879.  
  2880.   /* only generate class variables for the root of the inheritance
  2881.      hierarchy since these will be the same for every class */
  2882.  
  2883.   if (CLASS_SUPER_NAME (implementation_template) == NULLT
  2884.       && (chain = TYPE_FIELDS (objc_class_template)))
  2885.     {
  2886.       size = 0;
  2887.       initlist = build_ivar_list_initializer (chain, &size);
  2888.  
  2889.       ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
  2890.  
  2891.       _OBJC_CLASS_VARIABLES_decl =
  2892.     generate_ivars_list (ivar_list_template, "_OBJC_CLASS_VARIABLES",
  2893.                  size, initlist);
  2894.       /* cast! */
  2895.       TREE_TYPE (_OBJC_CLASS_VARIABLES_decl) = variable_length_type;
  2896.     }
  2897.   else
  2898.     _OBJC_CLASS_VARIABLES_decl = 0;
  2899.  
  2900.   chain = CLASS_IVARS (implementation_template);
  2901.   if (chain)
  2902.     {
  2903.       size = 0;
  2904.       initlist = build_ivar_list_initializer (chain, &size);
  2905.  
  2906.       ivar_list_template = build_ivar_list_template (objc_ivar_template, size);
  2907.  
  2908.       _OBJC_INSTANCE_VARIABLES_decl =
  2909.     generate_ivars_list (ivar_list_template, "_OBJC_INSTANCE_VARIABLES",
  2910.                  size, initlist);
  2911.       /* cast! */
  2912.       TREE_TYPE (_OBJC_INSTANCE_VARIABLES_decl) = variable_length_type;
  2913.     }
  2914.   else
  2915.     _OBJC_INSTANCE_VARIABLES_decl = 0;
  2916. }
  2917.  
  2918. static tree
  2919. build_dispatch_table_initializer (entries, size)
  2920.      tree entries;
  2921.      int *size;
  2922. {
  2923.   tree initlist = NULLT;
  2924.  
  2925.   do
  2926.     {
  2927.       initlist = tree_cons (NULLT, build_selector (METHOD_SEL_NAME (entries)), initlist);
  2928.  
  2929.       initlist = tree_cons (NULLT, add_objc_string (METHOD_ENCODING (entries), meth_var_types), initlist);
  2930.  
  2931.       initlist = tree_cons (NULLT, METHOD_DEFINITION (entries), initlist);
  2932.  
  2933.       (*size)++;
  2934.       entries = TREE_CHAIN (entries);
  2935.     }
  2936.   while (entries);
  2937.  
  2938.   return build_nt (CONSTRUCTOR, NULLT, nreverse (initlist));
  2939. }
  2940.  
  2941. /*
  2942.  * To accomplish method prototyping without generating all kinds of
  2943.  * inane warnings, the definition of the dispatch table entries were
  2944.  * changed from:
  2945.  *
  2946.  *     struct objc_method { SEL _cmd; id (*_imp)(); };
  2947.  * to:
  2948.  *     struct objc_method { SEL _cmd; void *_imp; };
  2949.  */
  2950. static tree
  2951. build_method_template ()
  2952. {
  2953.   tree _SLT_record;
  2954.   tree decl_specs, field_decl, field_decl_chain, parms;
  2955.  
  2956.   _SLT_record = start_struct (RECORD_TYPE, get_identifier (_TAG_METHOD));
  2957.  
  2958. #ifdef OBJC_SELS_R_INTS
  2959.   /* unsigned int _cmd; */
  2960.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_UNSIGNED], NULLT);
  2961.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_INT], decl_specs);
  2962.   field_decl = get_identifier ("_cmd");
  2963. #endif
  2964.  
  2965. #ifdef OBJC_SELS_R_STRUCT_PTRS
  2966.   /* struct objc_selector *_cmd; */
  2967.   decl_specs = tree_cons (NULLT,
  2968.               xref_tag (RECORD_TYPE,
  2969.                     get_identifier (TAG_SELECTOR)),
  2970.               NULLT);
  2971.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("_cmd"));
  2972. #endif
  2973.  
  2974.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2975.   field_decl_chain = field_decl;
  2976.  
  2977.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_CHAR], NULLT);
  2978.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("method_types"));
  2979.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2980.   chainon (field_decl_chain, field_decl);
  2981.  
  2982.   /* void *_imp; */
  2983.  
  2984.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_VOID], NULLT);
  2985.   field_decl = build1 (INDIRECT_REF, NULLT, get_identifier ("_imp"));
  2986.   field_decl = grokfield (input_filename, lineno, field_decl, decl_specs, NULLT);
  2987.   chainon (field_decl_chain, field_decl);
  2988.  
  2989.   finish_struct (_SLT_record, field_decl_chain);
  2990.  
  2991.   return _SLT_record;
  2992. }
  2993.  
  2994.  
  2995. static tree
  2996. generate_dispatch_table (type, name, size, list)
  2997.      tree type;
  2998.      char *name;
  2999.      int size;
  3000.      tree list;
  3001. {
  3002.   tree sc_spec, decl_specs, decl, initlist;
  3003.  
  3004.   sc_spec = tree_cons (NULLT, ridpointers[(int) RID_STATIC], NULLT);
  3005.   decl_specs = tree_cons (NULLT, type, sc_spec);
  3006.  
  3007.   decl = start_decl (synth_id_with_class_suffix (name, implementation_context), 
  3008.                 decl_specs, 1);
  3009.  
  3010.   initlist = build_tree_list (NULLT, build_int_2 (0, 0));
  3011.   initlist = tree_cons (NULLT, build_int_2 (size, 0), initlist);
  3012.   initlist = tree_cons (NULLT, list, initlist);
  3013.  
  3014.   finish_decl (decl, build_nt (CONSTRUCTOR, NULLT, nreverse (initlist)), NULLT);
  3015.  
  3016.   return decl;
  3017. }
  3018.  
  3019. static void
  3020. generate_dispatch_tables ()
  3021. {
  3022.   tree initlist, chain, method_list_template;
  3023.   tree cast, variable_length_type;
  3024.   int size;
  3025.  
  3026.   if (!objc_method_template)
  3027.     objc_method_template = build_method_template ();
  3028.  
  3029.   cast = build_tree_list (build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  3030.                                 get_identifier (_TAG_METHOD_LIST))), NULLT);
  3031.   variable_length_type = groktypename (cast);
  3032.  
  3033.   chain = CLASS_CLS_METHODS (implementation_context);
  3034.   if (chain)
  3035.     {
  3036.       size = 0;
  3037.       initlist = build_dispatch_table_initializer (chain, &size);
  3038.  
  3039.       method_list_template = build_method_list_template (objc_method_template,
  3040.                              size);
  3041.       if (TREE_CODE (implementation_context) == CLASS_IMPLEMENTATION)
  3042.     _OBJC_CLASS_METHODS_decl = 
  3043.         generate_dispatch_table (method_list_template,
  3044.                      "_OBJC_CLASS_METHODS", 
  3045.                      size, initlist);
  3046.       else
  3047.     /* we have a category */
  3048.     _OBJC_CLASS_METHODS_decl = 
  3049.         generate_dispatch_table (method_list_template,
  3050.                      "_OBJC_CATEGORY_CLASS_METHODS", 
  3051.                      size, initlist);
  3052.       /* cast! */
  3053.       TREE_TYPE (_OBJC_CLASS_METHODS_decl) = variable_length_type;
  3054.     }
  3055.   else
  3056.     _OBJC_CLASS_METHODS_decl = 0;
  3057.  
  3058.   chain = CLASS_NST_METHODS (implementation_context);
  3059.   if (chain)
  3060.     {
  3061.       size = 0;
  3062.       initlist = build_dispatch_table_initializer (chain, &size);
  3063.  
  3064.       method_list_template = build_method_list_template (objc_method_template,
  3065.                              size);
  3066.       if (TREE_CODE (implementation_context) == CLASS_IMPLEMENTATION)
  3067.     _OBJC_INSTANCE_METHODS_decl = 
  3068.         generate_dispatch_table (method_list_template,
  3069.                      "_OBJC_INSTANCE_METHODS", 
  3070.                      size, initlist);
  3071.       else
  3072.     /* we have a category */
  3073.     _OBJC_INSTANCE_METHODS_decl = 
  3074.         generate_dispatch_table (method_list_template,
  3075.                      "_OBJC_CATEGORY_INSTANCE_METHODS", 
  3076.                      size, initlist);
  3077.       /* cast! */
  3078.       TREE_TYPE (_OBJC_INSTANCE_METHODS_decl) = variable_length_type;
  3079.     }
  3080.   else
  3081.     _OBJC_INSTANCE_METHODS_decl = 0;
  3082. }
  3083.  
  3084. static tree
  3085. generate_protocol_list(i_or_p)
  3086.     tree i_or_p;
  3087. {
  3088.       static tree cast_type = 0;
  3089.       tree initlist, decl_specs, sc_spec;
  3090.       tree refs_decl, expr_decl, lproto, e, plist;
  3091.       int size = 0;
  3092.       
  3093.       if (TREE_CODE(i_or_p) == CLASS_INTERFACE
  3094.           || TREE_CODE(i_or_p) == CATEGORY_INTERFACE)
  3095.         plist = CLASS_PROTOCOL_LIST(i_or_p);
  3096.       else if (TREE_CODE(i_or_p) == PROTOCOL_INTERFACE)
  3097.         plist = PROTOCOL_LIST(i_or_p);
  3098.  
  3099.       if (!cast_type)
  3100.         cast_type = groktypename (build_tree_list (
  3101.             build_tree_list (NULLT,    
  3102.               xref_tag (RECORD_TYPE,
  3103.                   get_identifier (_TAG_PROTOCOL))), 
  3104.             build1 (INDIRECT_REF, NULLT, NULLT)));
  3105.  
  3106.       /* compute size */     
  3107.       for (lproto = plist; lproto; lproto = TREE_CHAIN(lproto))
  3108.     if ((TREE_CODE(TREE_VALUE(lproto)) == PROTOCOL_INTERFACE) &&
  3109.             PROTOCOL_FORWARD_DECL(TREE_VALUE(lproto))) {
  3110.           size++;
  3111.         }
  3112.  
  3113.       /* build initializer */
  3114.       initlist = tree_cons (NULLT, build_int_2 (0, 0), NULLT);
  3115.  
  3116.       e = build_int_2 (size, 0);      
  3117.       TREE_TYPE (e) = cast_type; 
  3118.       initlist = tree_cons (NULLT, e, initlist);
  3119.       
  3120.       for (lproto = plist; lproto; lproto = TREE_CHAIN(lproto))
  3121.         {
  3122.         int offset;
  3123.         tree pval = TREE_VALUE(lproto);
  3124.      
  3125.     if ((TREE_CODE(pval) == PROTOCOL_INTERFACE) && 
  3126.              PROTOCOL_FORWARD_DECL(pval))
  3127.             {
  3128.             e = build_unary_op (ADDR_EXPR, PROTOCOL_FORWARD_DECL(pval), 0);
  3129.             initlist = tree_cons (NULLT, e, initlist);
  3130.             }
  3131.         }
  3132.  
  3133.       /* static struct objc_protocol *refs[n]; */
  3134.  
  3135.       sc_spec = tree_cons (NULLT, ridpointers[(int) RID_STATIC], NULLT);
  3136.       decl_specs = tree_cons (NULLT, xref_tag (RECORD_TYPE,
  3137.                   get_identifier (_TAG_PROTOCOL)), sc_spec);
  3138.                 
  3139.       if (TREE_CODE(i_or_p) == PROTOCOL_INTERFACE)
  3140.         expr_decl = build_nt (ARRAY_REF, 
  3141.                   synth_id_with_class_suffix ("_OBJC_PROTOCOL_REFS", i_or_p),
  3142.                build_int_2 (size+2, 0));
  3143.       else if (TREE_CODE(i_or_p) == CLASS_INTERFACE)
  3144.         expr_decl = build_nt (ARRAY_REF, 
  3145.                   synth_id_with_class_suffix ("_OBJC_CLASS_PROTOCOLS", i_or_p),
  3146.                build_int_2 (size+2, 0));
  3147.       else if (TREE_CODE(i_or_p) == CATEGORY_INTERFACE)
  3148.         expr_decl = build_nt (ARRAY_REF, 
  3149.                   synth_id_with_class_suffix ("_OBJC_CATEGORY_PROTOCOLS", i_or_p),
  3150.                build_int_2 (size+2, 0));
  3151.             
  3152.       expr_decl = build1 (INDIRECT_REF, NULLT, expr_decl);
  3153.  
  3154.       refs_decl = start_decl (expr_decl, decl_specs, 1);
  3155.  
  3156.       finish_decl (refs_decl, build_nt (CONSTRUCTOR, NULLT, 
  3157.                       nreverse (initlist)), NULLT);
  3158.  
  3159.       return refs_decl;
  3160. }
  3161.  
  3162. static tree
  3163. build_category_initializer (cat_name, class_name,
  3164.                 instance_methods, class_methods, protocol_list)
  3165.      tree cat_name;
  3166.      tree class_name;
  3167.      tree instance_methods;
  3168.      tree class_methods;
  3169.      tree protocol_list;
  3170. {
  3171.   tree initlist = NULLT, expr;
  3172.  
  3173.   initlist = tree_cons (NULLT, cat_name, initlist);
  3174.   initlist = tree_cons (NULLT, class_name, initlist);
  3175.  
  3176.   if (!instance_methods)
  3177.     initlist = tree_cons (NULLT, build_int_2 (0, 0), initlist);
  3178.   else
  3179.     {
  3180.       expr = build_unary_op (ADDR_EXPR, instance_methods, 0);
  3181.       initlist = tree_cons (NULLT, expr, initlist);
  3182.     }
  3183.   if (!class_methods)
  3184.     initlist = tree_cons (NULLT, build_int_2 (0, 0), initlist);
  3185.   else
  3186.     {
  3187.       expr = build_unary_op (ADDR_EXPR, class_methods, 0);
  3188.       initlist = tree_cons (NULLT, expr, initlist);
  3189.     }
  3190.  
  3191.   /* protocol_list = */
  3192.   if (!protocol_list)
  3193.      initlist = tree_cons (NULLT, build_int_2 (0, 0), initlist);
  3194.   else
  3195.      {
  3196.     static tree cast_type2;
  3197.     
  3198.     if (!cast_type2)
  3199.       cast_type2 = groktypename (build_tree_list(
  3200.               build_tree_list (NULLT, xref_tag (RECORD_TYPE,
  3201.                         get_identifier (_TAG_PROTOCOL))), 
  3202.               build1 (INDIRECT_REF, NULLT, 
  3203.                 build1 (INDIRECT_REF, NULLT, NULLT))));
  3204.     
  3205.     expr = build_unary_op (ADDR_EXPR, protocol_list, 0);
  3206.     TREE_TYPE(expr) = cast_type2;
  3207.     initlist = tree_cons (NULLT, expr, initlist);
  3208.      }
  3209.  
  3210.   return build_nt (CONSTRUCTOR, NULLT, nreverse (initlist));
  3211. }
  3212.  
  3213. /*
  3214.  *  struct objc_class {
  3215.  *    struct objc_class *isa;
  3216.  *    struct objc_class *super_class;
  3217.  *    char *name;
  3218.  *    long version;
  3219.  *    long info;
  3220.  *    long instance_size;
  3221.  *    struct objc_ivar_list *ivars;
  3222.  *    struct objc_method_list *methods;
  3223.  *    struct objc_cache *cache;
  3224.  *    struct objc_protocol_list *protocols;
  3225.  *  };
  3226.  */
  3227. static tree
  3228. build_shared_structure_initializer (isa, super, name, size, status,
  3229.                     dispatch_table, ivar_list, protocol_list)
  3230.      tree isa;
  3231.      tree super;
  3232.      tree name;
  3233.      tree size;
  3234.      int status;
  3235.      tree dispatch_table;
  3236.      tree ivar_list;
  3237.      tree protocol_list;
  3238. {
  3239.   tree initlist = NULLT, expr;
  3240.  
  3241.   /* isa = */
  3242.   initlist = tree_cons (NULLT, isa, initlist);
  3243.  
  3244.   /* super_class = */
  3245.   initlist = tree_cons (NULLT, super, initlist);
  3246.  
  3247.   /* name = */
  3248.   initlist = tree_cons (NULLT, name, initlist);
  3249.  
  3250.   /* version = */
  3251.   initlist = tree_cons (NULLT, build_int_2 (0, 0), initlist);
  3252.  
  3253.   /* info = */
  3254.   initlist = tree_cons (NULLT, build_int_2 (status), initlist);
  3255.  
  3256.   /* instance_size = */
  3257.   initlist = tree_cons (NULLT, size, initlist);
  3258.  
  3259.   /* objc_ivar_list = */
  3260.   if (!ivar_list)
  3261.     initlist = tree_cons (NULLT, build_int_2 (0, 0), initlist);
  3262.   else
  3263.     {
  3264.       expr = build_unary_op (ADDR_EXPR, ivar_list, 0);
  3265.       initlist = tree_cons (NULLT, expr, initlist);
  3266.     }
  3267.  
  3268.   /* objc_method_list = */
  3269.   if (!dispatch_table)
  3270.     initlist = tree_cons (NULLT, build_int_2 (0, 0), initlist);
  3271.   else
  3272.     {
  3273.       expr = build_unary_op (ADDR_EXPR, dispatch_table, 0);
  3274.       initlist = tree_cons (NULLT, expr, initlist);
  3275.     }
  3276.  
  3277.   /* method_cache = */
  3278.   initlist = tree_cons (NULLT, build_int_2 (0, 0), initlist);
  3279.  
  3280.   /* protocol_list = */
  3281.   if (!protocol_list)
  3282.      initlist = tree_cons (NULLT, build_int_2 (0, 0), initlist);
  3283.   else
  3284.      {
  3285.      static tree cast_type2;
  3286.      
  3287.      if (!cast_type2)
  3288.         cast_type2 = groktypename (build_tree_list(
  3289.             build_tree_list (NULLT,    xref_tag (RECORD_TYPE,
  3290.                       get_identifier (_TAG_PROTOCOL))), 
  3291.             build1 (INDIRECT_REF, NULLT, 
  3292.               build1 (INDIRECT_REF, NULLT, NULLT))));
  3293.  
  3294.      expr = build_unary_op (ADDR_EXPR, protocol_list, 0);
  3295.      TREE_TYPE(expr) = cast_type2;
  3296.      initlist = tree_cons (NULLT, expr, initlist);
  3297.      }
  3298.  
  3299.   return build_nt (CONSTRUCTOR, NULLT, nreverse (initlist));
  3300. }
  3301.  
  3302. /*
  3303.  * static struct objc_category _OBJC_CATEGORY_<name> = { ... };
  3304.  */
  3305. static void
  3306. generate_category (cat)
  3307.      tree cat;
  3308. {
  3309.   tree sc_spec, decl_specs, decl;
  3310.   tree initlist, cat_name_expr, class_name_expr;
  3311.   tree protocol_decl, category;
  3312.  
  3313.   cat_name_expr = add_objc_string (CLASS_SUPER_NAME (cat), class_names);
  3314.  
  3315.   add_class_reference (CLASS_NAME (cat));
  3316.   class_name_expr = add_objc_string (CLASS_NAME (cat), class_names);
  3317.  
  3318.   category = CLASS_CATEGORY_LIST (implementation_template);
  3319.   
  3320.   /* find the category interface from the class it is associated with */
  3321.   while (category)
  3322.     {
  3323.       if (CLASS_SUPER_NAME (cat) == CLASS_SUPER_NAME (category))
  3324.     break;
  3325.       category = CLASS_CATEGORY_LIST (category);
  3326.     }
  3327.  
  3328.   if (category && CLASS_PROTOCOL_LIST (category))
  3329.     {
  3330.       generate_protocol_references (CLASS_PROTOCOL_LIST (category));
  3331.       protocol_decl = generate_protocol_list (category);
  3332.     }
  3333.   else
  3334.     protocol_decl = 0;
  3335.  
  3336.   sc_spec = tree_cons (NULLT, ridpointers[(int) RID_STATIC], NULLT);
  3337.   decl_specs = tree_cons (NULLT, objc_category_template, sc_spec);
  3338.  
  3339.   decl = start_decl (
  3340.               synth_id_with_class_suffix ("_OBJC_CATEGORY", implementation_context),
  3341.         decl_specs, 1);
  3342.  
  3343.   initlist = build_category_initializer (
  3344.                      cat_name_expr, class_name_expr,
  3345.                      _OBJC_INSTANCE_METHODS_decl,
  3346.                      _OBJC_CLASS_METHODS_decl,
  3347.                      protocol_decl);
  3348.  
  3349.   finish_decl (decl, initlist, NULLT);
  3350. }
  3351.  
  3352. /*
  3353.  * static struct objc_class _OBJC_METACLASS_Foo={ ... };
  3354.  * static struct objc_class _OBJC_CLASS_Foo={ ... };
  3355.  */
  3356. static void
  3357. generate_shared_structures ()
  3358. {
  3359.   tree sc_spec, decl_specs, expr_decl, decl;
  3360.   tree name_expr, super_expr, root_expr;
  3361.   tree my_root_id = NULLT, my_super_id = NULLT;
  3362.   tree cast_type, initlist, protocol_decl;
  3363.   int offset;
  3364.  
  3365.   my_super_id = CLASS_SUPER_NAME (implementation_template);
  3366.   if (my_super_id)
  3367.     {
  3368.       add_class_reference (my_super_id);
  3369.  
  3370.       /* compute "my_root_id" - this is required for code generation.
  3371.        * the "isa" for all meta class structures points to the root of
  3372.        * the inheritance hierarchy (e.g. "__Object")...
  3373.        */
  3374.       my_root_id = my_super_id;
  3375.       do
  3376.     {
  3377.       tree my_root_int = lookup_interface (my_root_id);
  3378.  
  3379.       if (my_root_int && CLASS_SUPER_NAME (my_root_int))
  3380.         my_root_id = CLASS_SUPER_NAME (my_root_int);
  3381.       else
  3382.         break;
  3383.     }
  3384.       while (1);
  3385.     }
  3386.   else                /* no super class */
  3387.     {
  3388.       my_root_id = CLASS_NAME (implementation_template);
  3389.     }
  3390.  
  3391.   cast_type = groktypename (build_tree_list (build_tree_list (NULLT,
  3392.                                   objc_class_template), build1 (INDIRECT_REF, NULLT, NULLT)));
  3393.  
  3394.   name_expr = add_objc_string (CLASS_NAME (implementation_template), class_names);
  3395.  
  3396.   /* install class `isa' and `super' pointers at runtime */
  3397.   if (my_super_id)
  3398.     {
  3399.       super_expr = add_objc_string (my_super_id, class_names);
  3400.       super_expr = build_c_cast (cast_type, super_expr); /* cast! */
  3401.     }
  3402.   else
  3403.     super_expr = build_int_2 (0, 0);
  3404.  
  3405.   root_expr = add_objc_string (my_root_id, class_names);
  3406.   root_expr = build_c_cast (cast_type, root_expr); /* cast! */
  3407.   
  3408.   if (CLASS_PROTOCOL_LIST(implementation_template)) {
  3409.     generate_protocol_references(CLASS_PROTOCOL_LIST(implementation_template));
  3410.     protocol_decl = generate_protocol_list (implementation_template);
  3411.   } else
  3412.     protocol_decl = 0;
  3413.     
  3414.   /* static struct objc_class _OBJC_METACLASS_Foo = { ... }; */
  3415.  
  3416.   sc_spec = build_tree_list (NULLT, ridpointers[(int) RID_STATIC]);
  3417. //  sc_spec = tree_cons (NULLT, ridpointers[(int) RID_CONST], sc_spec);
  3418.   decl_specs = tree_cons (NULLT, objc_class_template, sc_spec);
  3419.  
  3420.   decl = start_decl (DECL_NAME (_OBJC_METACLASS_decl), decl_specs, 1);
  3421.  
  3422.   initlist = build_shared_structure_initializer (root_expr, super_expr, name_expr,
  3423.                          build_int_2 (TREE_INT_CST_LOW (TYPE_SIZE (objc_class_template)) / BITS_PER_UNIT, 0),
  3424.                          2 /*CLS_META*/,
  3425.                          _OBJC_CLASS_METHODS_decl,
  3426.                          _OBJC_CLASS_VARIABLES_decl, 
  3427.                          protocol_decl);
  3428.   finish_decl (decl, initlist, NULLT);
  3429.  
  3430.   /* static struct objc_class _OBJC_CLASS_Foo={ ... }; */
  3431.  
  3432.   decl = start_decl (DECL_NAME (_OBJC_CLASS_decl), decl_specs, 1);
  3433.  
  3434.   initlist = build_shared_structure_initializer (
  3435.                          build_unary_op (ADDR_EXPR, _OBJC_METACLASS_decl, 0),
  3436.                          super_expr, name_expr,
  3437.                          build_int_2 (TREE_INT_CST_LOW (TYPE_SIZE (CLASS_STATIC_TEMPLATE (implementation_template))) / BITS_PER_UNIT, 0),
  3438.                          1 /*CLS_FACTORY*/,
  3439.                          _OBJC_INSTANCE_METHODS_decl,
  3440.                          _OBJC_INSTANCE_VARIABLES_decl,
  3441.                          protocol_decl);
  3442.  
  3443.   finish_decl (decl, initlist, NULLT);
  3444. }
  3445.  
  3446. static tree
  3447. synth_id_with_class_suffix (preamble, ctxt)
  3448.      char *preamble;
  3449.      tree ctxt;
  3450. {
  3451.   if (TREE_CODE (ctxt) == CLASS_IMPLEMENTATION ||
  3452.       TREE_CODE (ctxt) == CLASS_INTERFACE)
  3453.     sprintf (utlbuf, "%s_%s", preamble,
  3454.          IDENTIFIER_POINTER (CLASS_NAME (ctxt)));
  3455.  
  3456.   else if (TREE_CODE (ctxt) == CATEGORY_IMPLEMENTATION ||
  3457.        TREE_CODE (ctxt) == CATEGORY_INTERFACE) 
  3458.     sprintf (utlbuf, "%s_%s_%s", preamble,
  3459.          IDENTIFIER_POINTER (CLASS_NAME (ctxt)),
  3460.          IDENTIFIER_POINTER (CLASS_SUPER_NAME (ctxt)));
  3461.  
  3462.   else if (TREE_CODE (ctxt) == PROTOCOL_INTERFACE)
  3463.     sprintf (utlbuf, "%s_%s", preamble,
  3464.          IDENTIFIER_POINTER (PROTOCOL_NAME (ctxt)));
  3465.  
  3466.   return get_identifier (utlbuf);
  3467. }
  3468.  
  3469. static int
  3470. is_objc_type_qualifier (node)
  3471.      tree node;
  3472. {
  3473.   return (TREE_CODE (node) == IDENTIFIER_NODE
  3474.       && (node == ridpointers [(int) RID_CONST]
  3475.           || node == ridpointers [(int) RID_VOLATILE]
  3476.           || node == ridpointers [(int) RID_IN]
  3477.           || node == ridpointers [(int) RID_OUT]
  3478.           || node == ridpointers [(int) RID_INOUT]
  3479.           || node == ridpointers [(int) RID_BYCOPY]
  3480.           || node == ridpointers [(int) RID_ONEWAY]));
  3481. }
  3482.  
  3483. /* If type is empty or only type qualifiers are present, add default
  3484.    type of id (otherwise grokdeclarator will default to int).  */
  3485.  
  3486. static tree
  3487. adjust_type_for_id_default (type)
  3488.      tree type;
  3489. {
  3490.   tree declspecs, chain;
  3491.   int type_spec_seen = 0;
  3492.  
  3493.   if (!type)
  3494.     return build_tree_list (build_tree_list (NULLT, objc_object_reference),
  3495.                 build1 (INDIRECT_REF, NULLT, NULLT));
  3496.  
  3497.   declspecs = TREE_PURPOSE (type);
  3498.  
  3499.   /* Determine if a typespec is present.  */
  3500.   for (chain = declspecs;
  3501.        chain;
  3502.        chain = TREE_CHAIN (chain))
  3503.     {
  3504.       if (!is_objc_type_qualifier (TREE_VALUE (chain)))
  3505.     return type;
  3506.     }
  3507.  
  3508.   return build_tree_list (tree_cons (NULLT, objc_object_reference, declspecs),
  3509.               build1 (INDIRECT_REF, NULLT, NULLT));
  3510. }
  3511.  
  3512. /*
  3513.  *   usage:
  3514.  *        keyworddecl:
  3515.  *            selector ':' '(' typename ')' identifier
  3516.  *
  3517.  *   purpose:
  3518.  *        transform an Objective-C keyword argument into
  3519.  *        the C equivalent parameter declarator.
  3520.  *
  3521.  *   in:    key_name, an "identifier_node" (optional).
  3522.  *        arg_type, a  "tree_list" (optional).
  3523.  *        arg_name, an "identifier_node".
  3524.  *
  3525.  *   note:    it would be really nice to strongly type the preceding
  3526.  *        arguments in the function prototype; however, then i
  3527.  *        could not use the "accessor" macros defined in "tree.h".
  3528.  *
  3529.  *   out:    an instance of "keyword_decl".
  3530.  *
  3531.  */
  3532.  
  3533. tree
  3534. build_keyword_decl (key_name, arg_type, arg_name)
  3535.      tree key_name;
  3536.      tree arg_type;
  3537.      tree arg_name;
  3538. {
  3539.   tree keyword_decl;
  3540.  
  3541.   /* if no type is specified, default to "id" */
  3542.   arg_type = adjust_type_for_id_default (arg_type);
  3543.  
  3544.   keyword_decl = make_node (KEYWORD_DECL);
  3545.  
  3546.   TREE_TYPE (keyword_decl) = arg_type;
  3547.   KEYWORD_ARG_NAME (keyword_decl) = arg_name;
  3548.   KEYWORD_KEY_NAME (keyword_decl) = key_name;
  3549.  
  3550.   return keyword_decl;
  3551. }
  3552.  
  3553. /*
  3554.  *  given a chain of keyword_decl's, synthesize the full keyword selector.
  3555.  */
  3556. static tree
  3557. build_keyword_selector (selector)
  3558.      tree selector;
  3559. {
  3560.   int len = 0;
  3561.   tree key_chain, key_name;
  3562.   char *buf;
  3563.  
  3564.   for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
  3565.     {
  3566.       if (TREE_CODE (selector) == KEYWORD_DECL)
  3567.     key_name = KEYWORD_KEY_NAME (key_chain);
  3568.       else if (TREE_CODE (selector) == TREE_LIST)
  3569.     key_name = TREE_PURPOSE (key_chain);
  3570.  
  3571.       if (key_name)
  3572.     len += IDENTIFIER_LENGTH (key_name) + 1;
  3573.       else            /* just a ':' arg */
  3574.     len++;
  3575.     }
  3576.   buf = (char *)alloca (len + 1);
  3577.   bzero (buf, len + 1);
  3578.  
  3579.   for (key_chain = selector; key_chain; key_chain = TREE_CHAIN (key_chain))
  3580.     {
  3581.       if (TREE_CODE (selector) == KEYWORD_DECL)
  3582.     key_name = KEYWORD_KEY_NAME (key_chain);
  3583.       else if (TREE_CODE (selector) == TREE_LIST)
  3584.     key_name = TREE_PURPOSE (key_chain);
  3585.  
  3586.       if (key_name)
  3587.     strcat (buf, IDENTIFIER_POINTER (key_name));
  3588.       strcat (buf, ":");
  3589.     }
  3590.   return get_identifier (buf);
  3591. }
  3592.  
  3593. /* used for declarations and definitions */
  3594.  
  3595. tree
  3596. build_method_decl (code, ret_type, selector, add_args)
  3597.      enum tree_code code;
  3598.      tree ret_type;
  3599.      tree selector;
  3600.      tree add_args;
  3601. {
  3602.   tree method_decl;
  3603.  
  3604.   /* if no type is specified, default to "id" */
  3605.   ret_type = adjust_type_for_id_default (ret_type);
  3606.  
  3607.   method_decl = make_node (code);
  3608.   TREE_TYPE (method_decl) = ret_type;
  3609.  
  3610.   METHOD_FILENAME(method_decl) = input_filename;
  3611.   METHOD_LINENUM(method_decl) = lineno;
  3612.  
  3613.   /*
  3614.    *  if we have a keyword selector, create an identifier_node that
  3615.    *  represents the full selector name (`:' included)...
  3616.    */
  3617.   if (TREE_CODE (selector) == KEYWORD_DECL)
  3618.     {
  3619.       METHOD_SEL_NAME (method_decl) = build_keyword_selector (selector);
  3620.       METHOD_SEL_ARGS (method_decl) = selector;
  3621.       METHOD_ADD_ARGS (method_decl) = add_args;
  3622.     }
  3623.   else
  3624.     {
  3625.       METHOD_SEL_NAME (method_decl) = selector;
  3626.       METHOD_SEL_ARGS (method_decl) = NULLT;
  3627.       METHOD_ADD_ARGS (method_decl) = NULLT;
  3628.     }
  3629.  
  3630.   return method_decl;
  3631. }
  3632.  
  3633. #define METHOD_DEF 0
  3634. #define METHOD_REF 1
  3635. /*
  3636.  * used by `build_message_expr ()' and `comp_method_types ()'.
  3637.  *
  3638.  * add_args is a tree_list node the following info on a parameter list:
  3639.  *
  3640.  *    The TREE_PURPOSE is a chain of decls of those parms.
  3641.  *    The TREE_VALUE is a list of structure, union and enum tags defined.
  3642.  *    The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
  3643.  *    This tree_list node is later fed to `grokparms'.
  3644.  *
  3645.  *    VOID_AT_END nonzero means append `void' to the end of the type-list.
  3646.  *    Zero means the parmlist ended with an ellipsis so don't append `void'.
  3647.  */
  3648. static tree
  3649. get_arg_type_list (meth, context, superflag)
  3650.      tree meth;
  3651.      int context;
  3652.      int superflag;
  3653. {
  3654.   tree arglist, akey;
  3655.  
  3656.   /* receiver type */
  3657.   if (superflag)
  3658.     arglist = build_tree_list (NULLT, super_type);
  3659.   else
  3660.     {
  3661.       if (context == METHOD_DEF)
  3662.     arglist = build_tree_list (NULLT, TREE_TYPE (self_decl));
  3663.       else
  3664.     arglist = build_tree_list (NULLT, id_type);
  3665.     }
  3666.  
  3667.   /* selector type - will eventually change to `int' */
  3668.   chainon (arglist, build_tree_list (NULLT, _selector_type));
  3669.  
  3670.   /* build a list of argument types */
  3671.   for (akey = METHOD_SEL_ARGS (meth); akey; akey = TREE_CHAIN (akey))
  3672.     {
  3673.       tree arg_decl = groktypename_in_parm_context (TREE_TYPE (akey));
  3674.       chainon (arglist, build_tree_list (NULLT, TREE_TYPE (arg_decl)));
  3675.     }
  3676.  
  3677.   if (METHOD_ADD_ARGS (meth) == (tree)1)
  3678.     /*
  3679.      * we have a `, ...' immediately following the selector,
  3680.      * finalize the arglist...simulate get_parm_info (0)
  3681.      */
  3682.     ;
  3683.   else if (METHOD_ADD_ARGS (meth))
  3684.     {
  3685.       /* we have a variable length selector */
  3686.       tree add_arg_list = TREE_CHAIN (METHOD_ADD_ARGS (meth));
  3687.       chainon (arglist, add_arg_list);
  3688.     }
  3689.   else
  3690.     /* Finalize the arglist...simulate get_parm_info (1).  */
  3691.     chainon (arglist, build_tree_list (NULLT, void_type_node));
  3692.  
  3693.   return arglist;
  3694. }
  3695.  
  3696. static tree
  3697. check_duplicates (hsh)
  3698.      hash hsh;
  3699. {
  3700.   tree meth = NULLT;
  3701.  
  3702.   if (hsh)
  3703.     {
  3704.       meth = hsh->key;
  3705.  
  3706.       if (hsh->list)
  3707.         {
  3708.       /* we have two methods with the same name and different types */
  3709.       attr loop;
  3710.       char type;
  3711.  
  3712.       type = (TREE_CODE (meth) == INSTANCE_METHOD_DECL) ? '-' : '+';
  3713.  
  3714.       warning ("multiple declarations for method `%s'",
  3715.            IDENTIFIER_POINTER (METHOD_SEL_NAME (meth)));
  3716.  
  3717.       warn_with_method ("using", type, meth);
  3718.       for (loop = hsh->list; loop; loop = loop->next)
  3719.         warn_with_method ("also found", type, loop->value);
  3720.         }
  3721.     }
  3722.   return meth;
  3723. }
  3724.  
  3725. #ifdef OBJC_CLASS_REFS
  3726. static tree
  3727. receiver_is_class_object (arg)
  3728.       tree arg;
  3729. {
  3730.   if ((TREE_CODE (arg) == COMPONENT_REF) &&
  3731.       (arg = TREE_OPERAND (arg, 0)) &&
  3732.       (TREE_CODE (arg) == CONSTRUCTOR) &&
  3733.       (TREE_TYPE (arg) == objc_class_ref_template) &&
  3734.       (arg = TREE_OPERAND (arg, 1)) &&
  3735.       (TREE_CODE (arg) == TREE_LIST) &&
  3736.       (arg = TREE_VALUE (arg)) &&
  3737.       (TREE_CODE (arg) == NOP_EXPR) &&
  3738.       (arg = TREE_OPERAND (arg, 0)) &&
  3739.       (TREE_CODE (arg) == ADDR_EXPR) &&
  3740.       (arg = TREE_OPERAND (arg, 0)) &&
  3741.       (TREE_CODE (arg) == VAR_DECL))
  3742.     {
  3743.       tree chain = class_names_chain;
  3744.       
  3745.       /* Look up the identifier corresponding to this string decl. */
  3746.       while (chain)
  3747.     {
  3748.       if (TREE_PURPOSE (chain) == arg)
  3749.         return TREE_VALUE (chain);
  3750.       
  3751.       chain = TREE_CHAIN (chain);
  3752.     }
  3753.     }
  3754.   
  3755.   return 0;
  3756. }
  3757. #else /* OBJC_CLASS_REFS */
  3758. static tree
  3759. receiver_is_class_object (receiver)
  3760.       tree receiver;
  3761. {
  3762.   /* the receiver is a function call that returns an id...
  3763.    * ...check if it is a call to objc_getClass, if so, give it
  3764.    * special treatment.
  3765.    */
  3766.   tree exp = 0;
  3767.  
  3768.   if ((exp = TREE_OPERAND (receiver, 0)) && (TREE_CODE (exp) == ADDR_EXPR))
  3769.     {
  3770.       if ((exp = TREE_OPERAND (exp, 0)) && 
  3771.       (TREE_CODE (exp) == FUNCTION_DECL) && exp == objc_getClass_decl)
  3772.     {
  3773.       /* we have a call to objc_getClass! */
  3774.       tree arg = 0;
  3775.     
  3776.       if ((arg = TREE_OPERAND (receiver, 1)) &&
  3777.           (TREE_CODE (arg) == TREE_LIST) &&
  3778.           (arg = TREE_VALUE (arg)) &&
  3779.           (TREE_CODE (arg) == NOP_EXPR) &&
  3780.           (arg = TREE_OPERAND (arg, 0)) &&
  3781.           (TREE_CODE (arg) == ADDR_EXPR) &&
  3782.           (arg = TREE_OPERAND (arg, 0)) &&
  3783.           (TREE_CODE (arg) == STRING_CST))
  3784.         /* finally, we have the class name */
  3785.         return get_identifier (TREE_STRING_POINTER (arg));
  3786.     }
  3787.     }
  3788.   return 0;
  3789. }
  3790. #endif /* OBJC_CLASS_REFS */
  3791.  
  3792. /* If we are currently building a message expr, this holds
  3793.    the identifier of the selector of the message.  This is
  3794.    used when printing warnings about argument mismatches. */
  3795.  
  3796.  
  3797. static tree building_objc_message_expr = 0;
  3798.  
  3799.  
  3800. tree
  3801. maybe_building_objc_message_expr ()
  3802. {
  3803.   return building_objc_message_expr;
  3804. }
  3805.  
  3806.  
  3807. /*
  3808.  *    (*(<abstract_decl>(*)())_msg)(receiver, selTransTbl[n], ...);
  3809.  *    (*(<abstract_decl>(*)())_msgSuper)(receiver, selTransTbl[n], ...);
  3810.  */
  3811. tree
  3812. build_message_expr (mess)
  3813.      tree mess;
  3814. {
  3815.   tree receiver = TREE_PURPOSE (mess), rtype, sel_name;
  3816.   tree args = TREE_VALUE (mess);
  3817.   tree params = NULLT;
  3818.   tree meth = NULLT, message_decl;
  3819.   tree retval;
  3820.   int statically_typed = 0, statically_allocated = 0;
  3821.   tree class_ident = 0;
  3822.  
  3823.   if (!doing_objc_thang)
  3824.     objc_fatal ();
  3825.  
  3826.   if (TREE_CODE (receiver) == ERROR_MARK)
  3827.     return error_mark_node;
  3828.  
  3829.   /* determine receiver type */
  3830.   rtype = TREE_TYPE (receiver);
  3831.  
  3832.   if (rtype == super_type)
  3833.     message_decl = _msgSuper_decl;
  3834.   else
  3835.     {
  3836.       message_decl = _msg_decl;
  3837.  
  3838.       if (TREE_STATIC_TEMPLATE (rtype))
  3839.     statically_allocated = 1;
  3840.       else if (TREE_CODE (rtype) == POINTER_TYPE
  3841.            && TREE_STATIC_TEMPLATE (TREE_TYPE (rtype)))
  3842.     statically_typed = 1;
  3843.       /* classfix -smn */
  3844. #ifdef OBJC_CLASS_REFS
  3845.       else if (class_ident = receiver_is_class_object (receiver))
  3846.     ;
  3847. #else /* OBJC_CLASS_REFS */
  3848.       else if ((TREE_CODE (receiver) == CALL_EXPR) && (rtype == id_type) &&
  3849.         (class_ident = receiver_is_class_object (receiver)))
  3850.     ;
  3851. #endif /* OBJC_CLASS_REFS */
  3852.       else if ((TYPE_MAIN_VARIANT (rtype) != id_type)
  3853.            && (rtype != objc_class_type))
  3854.     {
  3855.       bzero (errbuf, BUFSIZE);
  3856.       error ("illegal receiver type `%s'",
  3857.          gen_declaration (rtype, errbuf));
  3858.       return error_mark_node;
  3859.     }
  3860.     }
  3861.  
  3862.   /* obtain the full selector name */
  3863.   if (TREE_CODE (args) == IDENTIFIER_NODE)
  3864.     /* a unary selector */
  3865.     sel_name = args;
  3866.   else if (TREE_CODE (args) == TREE_LIST)
  3867.     sel_name = build_keyword_selector (args);
  3868.  
  3869.   /* start building argument list - synthesizing the second argument */
  3870.   if (statically_allocated)
  3871.     params = build_tree_list (NULLT, build_unary_op (ADDR_EXPR, receiver, 0));
  3872.   else
  3873.     params = build_tree_list (NULLT, receiver);
  3874.  
  3875.   chainon (params, build_tree_list (NULLT,
  3876.        build_selector_reference (sel_name)));
  3877.   if (TREE_CODE (args) == TREE_LIST)
  3878.     {
  3879.       tree chain = args, prev = NULLT;
  3880.  
  3881.       /* we have a keyword selector - check for comma expressions */
  3882.       while (chain)
  3883.     {
  3884.       tree element = TREE_VALUE (chain);
  3885.  
  3886.       /* we have a comma expression, must collapse... */
  3887.       if (TREE_CODE (element) == TREE_LIST)
  3888.         {
  3889.           if (prev)
  3890.         TREE_CHAIN (prev) = element;
  3891.           else
  3892.         args = element;
  3893.         }
  3894.       prev = chain;
  3895.       chain = TREE_CHAIN (chain);
  3896.         }
  3897.       /* attach the rest of the argument list */
  3898.       chainon (params, args);
  3899.     }
  3900.  
  3901.   /* determine return type */
  3902.  
  3903.   if (rtype == super_type)
  3904.     {
  3905.       tree iface;
  3906.  
  3907.       if (CLASS_SUPER_NAME (implementation_template))
  3908.     {
  3909.       iface = lookup_interface (CLASS_SUPER_NAME (implementation_template));
  3910.     
  3911.       if (TREE_CODE (method_context) == INSTANCE_METHOD_DECL)
  3912.         meth = lookup_instance_method_static (iface, sel_name);
  3913.       else
  3914.         meth = lookup_class_method_static (iface, sel_name);
  3915.     
  3916.       if (iface && !meth)
  3917.         {
  3918.           warning ("`%s' does not respond to `%s'",
  3919.             IDENTIFIER_POINTER (CLASS_SUPER_NAME (implementation_template)),
  3920.             IDENTIFIER_POINTER (sel_name));
  3921.         }
  3922.     }
  3923.       else
  3924.     {
  3925.       error ("no super class declared in interface for `%s'",
  3926.          IDENTIFIER_POINTER (CLASS_NAME (implementation_template)));
  3927.       return error_mark_node;
  3928.     }
  3929.  
  3930.     }
  3931.   else if (statically_typed || statically_allocated)
  3932.     {
  3933.       tree ctype;
  3934.  
  3935.       if (statically_allocated)
  3936.         ctype = rtype;
  3937.       if (statically_typed)
  3938.         ctype = TREE_TYPE (rtype);
  3939.  
  3940.       /* `self' is now statically_typed...all methods should be visible
  3941.        * within the context of the implementation...
  3942.        */
  3943.       if ((implementation_context
  3944.        && CLASS_NAME (implementation_context) == TYPE_NAME (ctype)))
  3945.     {
  3946.       meth = lookup_instance_method_static (implementation_template, sel_name);
  3947.  
  3948. /* NEW!!! */
  3949.           if (!meth && TYPE_PROTOCOL_LIST (ctype))
  3950.         meth = lookup_method_in_protocol_list (
  3951.             TYPE_PROTOCOL_LIST (ctype), sel_name, 0);
  3952.  
  3953.       if (!meth && (implementation_template != implementation_context))
  3954.         /* the method is not published in the interface...check locally */
  3955.         meth = lookup_method (CLASS_NST_METHODS (implementation_context),
  3956.                   sel_name);
  3957.     }
  3958.       else
  3959.     {
  3960.       tree iface = lookup_interface (TYPE_NAME (ctype));
  3961.  
  3962.       if (iface)
  3963.         meth = lookup_instance_method_static (iface, sel_name);
  3964.       else
  3965.         error ("Cannot find interface declaration for `%s'",
  3966.            IDENTIFIER_POINTER (TYPE_NAME (ctype)));
  3967.  
  3968.           if (!meth && TYPE_PROTOCOL_LIST (ctype))
  3969.         meth = lookup_method_in_protocol_list (
  3970.             TYPE_PROTOCOL_LIST (ctype), sel_name, 0);
  3971.     }
  3972.  
  3973.       if (!meth)
  3974.         warning ("`%s' does not respond to `%s'",
  3975.          IDENTIFIER_POINTER (TYPE_NAME (ctype)),
  3976.          IDENTIFIER_POINTER (sel_name));
  3977.     }
  3978.   else if (class_ident)
  3979.     {
  3980.       if ((implementation_context && 
  3981.        CLASS_NAME (implementation_context) == class_ident))
  3982.     {
  3983.       meth = lookup_class_method_static (implementation_template,
  3984.                          sel_name);
  3985.     
  3986.       if (!meth && (implementation_template != implementation_context))
  3987.         /* the method is not published in the interface...check locally */
  3988.         meth = lookup_method (CLASS_CLS_METHODS (implementation_context),
  3989.                   sel_name);
  3990.     }
  3991.       else
  3992.     {
  3993.       tree iface;
  3994.     
  3995.       if (iface = lookup_interface (class_ident))
  3996.         meth = lookup_class_method_static (iface, sel_name);
  3997.     }
  3998.   
  3999.       if (!meth)
  4000.     {
  4001.       warning ("cannot find class (factory) method.");
  4002.       warning ("return type for `%s' defaults to id",
  4003.            IDENTIFIER_POINTER (sel_name));
  4004.     }
  4005.     }
  4006.   else if (rtype != id_type && TYPE_MAIN_VARIANT (rtype) == id_type)
  4007.     {
  4008.       /* An anonymous object that has been qualified with a protocol.  */
  4009.   
  4010.       tree protocol_list = TYPE_PROTOCOL_LIST (rtype);
  4011.       
  4012.       meth = lookup_method_in_protocol_list (protocol_list, sel_name, 0);
  4013.  
  4014.       if (!meth)
  4015.     {
  4016.           hash hsh;
  4017.  
  4018.       warning ("method `%s' not implemented by protocol.",
  4019.            IDENTIFIER_POINTER (sel_name));
  4020.  
  4021.           /* try and find the method signiture in the global pools! */
  4022.  
  4023.           if (!(hsh = hash_lookup (nst_method_hash_list, sel_name)))
  4024.         hsh = hash_lookup (cls_method_hash_list, sel_name);
  4025.  
  4026.           if (!(meth = check_duplicates (hsh)))
  4027.         warning ("return type defaults to id");
  4028.     }
  4029.     }
  4030.   else
  4031.     {
  4032.       hash hsh;
  4033.  
  4034.       /* we think we have an instance...loophole: extern id Object; */
  4035.       if (!(hsh = hash_lookup (nst_method_hash_list, sel_name)))
  4036.     {
  4037.       /* for various loopholes...like sending messages to self in a
  4038.        * factory context...
  4039.        */
  4040.       hsh = hash_lookup (cls_method_hash_list, sel_name);
  4041.     }
  4042.       if (!(meth = check_duplicates (hsh)))
  4043.     {
  4044.       warning ("cannot find method.");
  4045.       warning ("return type for `%s' defaults to id",
  4046.            IDENTIFIER_POINTER (sel_name));
  4047.     }
  4048.     }
  4049.  
  4050.   building_objc_message_expr = sel_name;
  4051.  
  4052.   if (!meth)
  4053.     {
  4054.       retval = build_function_call (message_decl, params);
  4055.     }
  4056.   else                /* we have a method prototype */
  4057.     {
  4058.       tree arglist = NULLT;
  4059.       tree savret, savarg;
  4060.  
  4061.       arglist = get_arg_type_list (meth, METHOD_REF, message_decl == _msgSuper_decl);
  4062.  
  4063.       /* install argument types - normally set by "build_function_type ()". */
  4064.       savarg = TYPE_ARG_TYPES (TREE_TYPE (message_decl));
  4065.       TYPE_ARG_TYPES (TREE_TYPE (message_decl)) = arglist;
  4066.  
  4067.       /* install return type - "message_decl" is a function returning ret_type. */
  4068.       savret = TREE_TYPE (TREE_TYPE (message_decl));
  4069.       TREE_TYPE (TREE_TYPE (message_decl)) = groktypename (TREE_TYPE (meth));
  4070.  
  4071.       /* check argments types supplied vs. ones in method prototype.
  4072.        * build_function_call () calls convert_arguments () to do the
  4073.        * type checking
  4074.        */
  4075.       retval = build_function_call (message_decl, params);
  4076.  
  4077.       /* restore previous return/argument types */
  4078.       TYPE_ARG_TYPES (TREE_TYPE (message_decl)) = savarg;
  4079.       TREE_TYPE (TREE_TYPE (message_decl)) = savret;
  4080.     }
  4081.  
  4082.   building_objc_message_expr = 0;
  4083.  
  4084.   return retval;
  4085. }
  4086.  
  4087. static void
  4088. build_protocol_reference (p)
  4089.      tree p;
  4090. {
  4091.   tree ref, decl, ident, ptype;
  4092.   struct obstack *save_current_obstack = current_obstack;
  4093.   struct obstack *save_rtl_obstack = rtl_obstack;
  4094.  
  4095.   rtl_obstack = current_obstack = &permanent_obstack;
  4096.   
  4097.   /* extern struct objc_protocol _OBJC_PROTOCOL_<mumble>; */
  4098.  
  4099.   ident = synth_id_with_class_suffix ("_OBJC_PROTOCOL", p);
  4100.   ptype = groktypename (build_tree_list(
  4101.                          build_tree_list(NULLT, objc_protocol_template), NULLT));
  4102.  
  4103.   if (IDENTIFIER_GLOBAL_VALUE (ident))
  4104.     decl = IDENTIFIER_GLOBAL_VALUE (ident); /* set by pushdecl() */
  4105.   else 
  4106.     {
  4107.       decl = build_decl (VAR_DECL, ident, ptype);
  4108.       TREE_EXTERNAL (decl) = 1;
  4109.       TREE_PUBLIC (decl) = 1;
  4110.       TREE_USED (decl) = 1;
  4111.  
  4112.       /* usually called from `rest_of_decl_compilation' */
  4113.       make_decl_rtl (decl, 0, 1); 
  4114.       /* our `extended/custom' pushdecl in c-decl.c */
  4115.       pushdecl_top_level (decl);  
  4116.     }
  4117.   current_obstack = save_current_obstack;
  4118.   rtl_obstack = save_rtl_obstack;
  4119.  
  4120.   PROTOCOL_FORWARD_DECL (p) = decl;
  4121. }
  4122.  
  4123. tree
  4124. build_protocol_expr (protoname)
  4125.      tree protoname;
  4126. {
  4127.   tree expr;
  4128.   tree p;
  4129.  
  4130.   if (!doing_objc_thang)
  4131.     objc_fatal ();
  4132.  
  4133.   p = lookup_protocol (protoname);
  4134.  
  4135.   if (!p) {
  4136.     error ("Cannot find protocol declaration for `%s'",
  4137.        IDENTIFIER_POINTER (protoname));
  4138.     return error_mark_node;
  4139.   }
  4140.  
  4141.   if (!PROTOCOL_FORWARD_DECL (p)) 
  4142.     build_protocol_reference(p);
  4143.  
  4144.   expr = build_unary_op (ADDR_EXPR, PROTOCOL_FORWARD_DECL (p), 0);
  4145.  
  4146.   TREE_TYPE(expr) = protocol_type;
  4147.  
  4148.   return expr;
  4149. }
  4150.  
  4151. tree
  4152. build_selector_expr (selnamelist)
  4153.      tree selnamelist;
  4154. {
  4155.   tree selname;
  4156.  
  4157.   if (!doing_objc_thang)
  4158.     objc_fatal ();
  4159.  
  4160.   /* obtain the full selector name */
  4161.   if (TREE_CODE (selnamelist) == IDENTIFIER_NODE)
  4162.     /* a unary selector */
  4163.     selname = selnamelist;
  4164.   else if (TREE_CODE (selnamelist) == TREE_LIST)
  4165.     selname = build_keyword_selector (selnamelist);
  4166.  
  4167.   return build_selector_reference (selname);
  4168. }
  4169.  
  4170. tree
  4171. build_encode_expr (type)
  4172.      tree type;
  4173. {
  4174.   if (!doing_objc_thang)
  4175.     objc_fatal ();
  4176.  
  4177.   if (!utlbuf)
  4178.     utlbuf = (char *)xmalloc (BUFSIZE);
  4179.   bzero (utlbuf, BUFSIZE);
  4180.  
  4181.   encode_type (type, utlbuf, utlbuf + strlen (utlbuf),
  4182.            OBJC_ENCODE_INLINE_DEFS);
  4183.  
  4184.   /* synthesize a string that represents the encoded struct/union */
  4185.   return my_build_string (strlen (utlbuf) + 1, utlbuf);
  4186. }
  4187.  
  4188. tree
  4189. build_ivar_reference (id)
  4190.      tree id;
  4191. {
  4192.   if (TREE_CODE (method_context) == CLASS_METHOD_DECL)
  4193.     TREE_TYPE (self_decl) = instance_type; /* cast */
  4194.  
  4195.   return build_component_ref (build_indirect_ref (self_decl, "->"), id);
  4196. }
  4197.  
  4198. #define HASH_ALLOC_LIST_SIZE    170
  4199. #define ATTR_ALLOC_LIST_SIZE    170
  4200. #define SIZEHASHTABLE         257
  4201. #define HASHFUNCTION(key)    ((int)key >> 2)     /* divide by 4 */
  4202.  
  4203. static void
  4204. hash_init ()
  4205. {
  4206.   nst_method_hash_list = (hash *)xmalloc (SIZEHASHTABLE * sizeof (hash));
  4207.   cls_method_hash_list = (hash *)xmalloc (SIZEHASHTABLE * sizeof (hash));
  4208.  
  4209.   if (!nst_method_hash_list || !cls_method_hash_list)
  4210.     perror ("unable to allocate space in objc-tree.c");
  4211.   else
  4212.     {
  4213.       int i;
  4214.  
  4215.       for (i = 0; i < SIZEHASHTABLE; i++)
  4216.     {
  4217.       nst_method_hash_list[i] = 0;
  4218.       cls_method_hash_list[i] = 0;
  4219.     }
  4220.     }
  4221. }
  4222.  
  4223. static void
  4224. hash_enter (hashlist, method)
  4225.      hash *hashlist;
  4226.      tree method;
  4227. {
  4228.   static hash     hash_alloc_list = 0;
  4229.   static int    hash_alloc_index = 0;
  4230.   hash obj;
  4231.   int slot = HASHFUNCTION (METHOD_SEL_NAME (method)) % SIZEHASHTABLE;
  4232.  
  4233.   if (!hash_alloc_list || hash_alloc_index >= HASH_ALLOC_LIST_SIZE)
  4234.     {
  4235.       hash_alloc_index = 0;
  4236.       hash_alloc_list = (hash)xmalloc (sizeof (struct hashedEntry) *
  4237.                       HASH_ALLOC_LIST_SIZE);
  4238.       if (!hash_alloc_list)
  4239.     perror ("unable to allocate in objc-tree.c");
  4240.     }
  4241.   obj = &hash_alloc_list[hash_alloc_index++];
  4242.   obj->list = 0;
  4243.   obj->next = hashlist[slot];
  4244.   obj->key = method;
  4245.  
  4246.   hashlist[slot] = obj;        /* append to front */
  4247. }
  4248.  
  4249. static hash
  4250. hash_lookup (hashlist, sel_name)
  4251.      hash *hashlist;
  4252.      tree sel_name;
  4253. {
  4254.   hash target;
  4255.  
  4256.   target = hashlist[HASHFUNCTION (sel_name) % SIZEHASHTABLE];
  4257.  
  4258.   while (target)
  4259.     {
  4260.       if (sel_name == METHOD_SEL_NAME (target->key))
  4261.     return target;
  4262.  
  4263.       target = target->next;
  4264.     }
  4265.   return 0;
  4266. }
  4267.  
  4268. static void
  4269. hash_add_attr (entry, value)
  4270.      hash entry;
  4271.      tree value;
  4272. {
  4273.   static attr     attr_alloc_list = 0;
  4274.   static int    attr_alloc_index = 0;
  4275.   attr obj;
  4276.  
  4277.   if (!attr_alloc_list || attr_alloc_index >= ATTR_ALLOC_LIST_SIZE)
  4278.     {
  4279.       attr_alloc_index = 0;
  4280.       attr_alloc_list = (attr)xmalloc (sizeof (struct hashedAttribute) *
  4281.                       ATTR_ALLOC_LIST_SIZE);
  4282.       if (!attr_alloc_list)
  4283.     perror ("unable to allocate in objc-tree.c");
  4284.     }
  4285.   obj = &attr_alloc_list[attr_alloc_index++];
  4286.   obj->next = entry->list;
  4287.   obj->value = value;
  4288.  
  4289.   entry->list = obj;        /* append to front */
  4290. }
  4291.  
  4292. static tree
  4293. lookup_method (mchain, method)
  4294.      tree mchain;
  4295.      tree method;
  4296. {
  4297.   tree key;
  4298.  
  4299.   if (TREE_CODE (method) == IDENTIFIER_NODE)
  4300.     key = method;
  4301.   else
  4302.     key = METHOD_SEL_NAME (method);
  4303.  
  4304.   while (mchain)
  4305.     {
  4306.       if (METHOD_SEL_NAME (mchain) == key)
  4307.     return mchain;
  4308.       mchain = TREE_CHAIN (mchain);
  4309.     }
  4310.   return NULLT;
  4311. }
  4312.  
  4313. static tree
  4314. lookup_instance_method_static (interface, ident)
  4315.      tree interface;
  4316.      tree ident;
  4317. {
  4318.   tree inter = interface;
  4319.   tree meth = NULLT;
  4320.  
  4321.   while (inter)
  4322.     {
  4323.       if (meth = lookup_method (CLASS_NST_METHODS (inter), ident))
  4324.     return meth;
  4325.  
  4326.       if (CLASS_CATEGORY_LIST (inter))
  4327.     {
  4328.       tree category = CLASS_CATEGORY_LIST (inter);
  4329.  
  4330.       while (category)
  4331.         {
  4332.           if (meth = lookup_method (CLASS_NST_METHODS (category), ident))
  4333.         return meth;
  4334.  
  4335. /* NEW!!! */
  4336.           /* Check for instance methods in protocols in categories.  */
  4337.           if (CLASS_PROTOCOL_LIST (category))
  4338.         {
  4339.           if (meth = lookup_method_in_protocol_list (
  4340.                 CLASS_PROTOCOL_LIST (category), ident, 0))
  4341.           return meth;
  4342.         }
  4343.       
  4344.           category = CLASS_CATEGORY_LIST (category);
  4345.         }
  4346.     }
  4347.  
  4348.       if (CLASS_PROTOCOL_LIST (inter))
  4349.         {
  4350.       if (meth = lookup_method_in_protocol_list (
  4351.             CLASS_PROTOCOL_LIST (inter), ident, 0))
  4352.           return meth;
  4353.         }
  4354.  
  4355.       inter = lookup_interface (CLASS_SUPER_NAME (inter));
  4356.     }
  4357.  
  4358.   return meth;
  4359. }
  4360.  
  4361. static tree
  4362. lookup_class_method_static (interface, ident)
  4363.      tree interface;
  4364.      tree ident;
  4365. {
  4366.   tree inter = interface;
  4367.   tree meth = NULLT;
  4368.   tree root_inter = NULLT;
  4369.  
  4370.   while (inter)
  4371.     {
  4372.       if (meth = lookup_method (CLASS_CLS_METHODS (inter), ident))
  4373.     return meth;
  4374.  
  4375.       if (CLASS_CATEGORY_LIST (inter))
  4376.     {
  4377.       tree category = CLASS_CATEGORY_LIST (inter);
  4378.     
  4379.       while (category)
  4380.         {
  4381.           if (meth = lookup_method (CLASS_CLS_METHODS (category), ident))
  4382.         return meth;
  4383.  
  4384. /* NEW!!! */
  4385.           /* Check for class methods in protocols in categories.  */
  4386.           if (CLASS_PROTOCOL_LIST (category))
  4387.         {
  4388.           if (meth = lookup_method_in_protocol_list (
  4389.                 CLASS_PROTOCOL_LIST (category), ident, 1))
  4390.           return meth;
  4391.         }
  4392.  
  4393.           category = CLASS_CATEGORY_LIST (category);
  4394.         }
  4395.     }
  4396.  
  4397. /* NEW!!! */
  4398.       /* Check for class methods in protocols.  */
  4399.       if (CLASS_PROTOCOL_LIST (inter))
  4400.         {
  4401.     if (meth = lookup_method_in_protocol_list (
  4402.             CLASS_PROTOCOL_LIST (inter), ident, 1))
  4403.           return meth;
  4404.         }
  4405.  
  4406.       root_inter = inter;
  4407.       inter = lookup_interface (CLASS_SUPER_NAME (inter));
  4408.     }
  4409.  
  4410. /* NEW!!! */
  4411.   /* Simulate wrap around.  */
  4412.   return lookup_instance_method_static (root_inter, ident);
  4413. }
  4414.  
  4415. tree
  4416. add_class_method (class, method)
  4417.      tree class;
  4418.      tree method;
  4419. {
  4420.   tree mth;
  4421.   hash hsh;
  4422.  
  4423.   if (!(mth = lookup_method (CLASS_CLS_METHODS (class), method)))
  4424.     {
  4425.       /* put method on list in reverse order */
  4426.       TREE_CHAIN (method) = CLASS_CLS_METHODS (class);
  4427.       CLASS_CLS_METHODS (class) = method;
  4428.     }
  4429.   else
  4430.     {
  4431.       if (TREE_CODE (class) == CLASS_IMPLEMENTATION)
  4432.     error ("duplicate definition of class method `%s'.",
  4433.            IDENTIFIER_POINTER (METHOD_SEL_NAME (mth)));
  4434.       else
  4435.         {
  4436.       /* check types, if different complain */
  4437.       if (!comp_proto_with_proto (method, mth))
  4438.         error ("duplicate declaration of class method `%s'.",
  4439.            IDENTIFIER_POINTER (METHOD_SEL_NAME (mth)));
  4440.         }
  4441.     }
  4442.  
  4443.   if (!(hsh = hash_lookup (cls_method_hash_list, METHOD_SEL_NAME (method))))
  4444.     {
  4445.       /* install on a global chain */
  4446.       hash_enter (cls_method_hash_list, method);
  4447.     }
  4448.   else
  4449.     {
  4450.       /* check types, if different add to a list */
  4451.       if (!comp_proto_with_proto (method, hsh->key))
  4452.         hash_add_attr (hsh, method);
  4453.     }
  4454.   return method;
  4455. }
  4456.  
  4457. tree
  4458. add_instance_method (class, method)
  4459.      tree class;
  4460.      tree method;
  4461. {
  4462.   tree mth;
  4463.   hash hsh;
  4464.  
  4465.   if (!(mth = lookup_method (CLASS_NST_METHODS (class), method)))
  4466.     {
  4467.       /* put method on list in reverse order */
  4468.       TREE_CHAIN (method) = CLASS_NST_METHODS (class);
  4469.       CLASS_NST_METHODS (class) = method;
  4470.     }
  4471.   else
  4472.     {
  4473.       if (TREE_CODE (class) == CLASS_IMPLEMENTATION)
  4474.     error ("duplicate definition of instance method `%s'.",
  4475.            IDENTIFIER_POINTER (METHOD_SEL_NAME (mth)));
  4476.       else
  4477.         {
  4478.       /* check types, if different complain */
  4479.       if (!comp_proto_with_proto (method, mth))
  4480.         error ("duplicate declaration of instance method `%s'.",
  4481.            IDENTIFIER_POINTER (METHOD_SEL_NAME (mth)));
  4482.         }
  4483.     }
  4484.  
  4485.   if (!(hsh = hash_lookup (nst_method_hash_list, METHOD_SEL_NAME (method))))
  4486.     {
  4487.       /* install on a global chain */
  4488.       hash_enter (nst_method_hash_list, method);
  4489.     }
  4490.   else
  4491.     {
  4492.       /* check types, if different add to a list */
  4493.       if (!comp_proto_with_proto (method, hsh->key))
  4494.         hash_add_attr (hsh, method);
  4495.     }
  4496.   return method;
  4497. }
  4498.  
  4499. static tree
  4500. add_class (class)
  4501.      tree class;
  4502. {
  4503.   /* put interfaces on list in reverse order */
  4504.   TREE_CHAIN (class) = interface_chain;
  4505.   interface_chain = class;
  4506.   return interface_chain;
  4507. }
  4508.  
  4509. static void
  4510. add_category (class, category)
  4511.       tree class;
  4512.       tree category;
  4513. {
  4514.   /* put categories on list in reverse order */
  4515.   CLASS_CATEGORY_LIST (category) = CLASS_CATEGORY_LIST (class);
  4516.   CLASS_CATEGORY_LIST (class) = category;
  4517. }
  4518.  
  4519. /* called after parsing each instance variable declaration. Necessary to
  4520.  * preserve typedefs and implement public/private...
  4521.  */
  4522.  
  4523. /* PUBLIC is 1 for public, 0 for protected, and 2 for private. */
  4524.  
  4525. tree
  4526. add_instance_variable (class, public, declarator, declspecs, width)
  4527.      tree class;
  4528.      int public;
  4529.      tree declarator;
  4530.      tree declspecs;
  4531.      tree width;
  4532. {
  4533.   tree field_decl, raw_decl;
  4534.  
  4535.   raw_decl = build_tree_list (declspecs    /*purpose*/, declarator/*value*/);
  4536.  
  4537.   if (CLASS_RAW_IVARS (class))
  4538.     chainon (CLASS_RAW_IVARS (class), raw_decl);
  4539.   else
  4540.     CLASS_RAW_IVARS (class) = raw_decl;
  4541.  
  4542.   field_decl = grokfield (input_filename, lineno,
  4543.               declarator, declspecs, width);
  4544.  
  4545.   /* overload the public attribute, it is not used for FIELD_DECL's */
  4546.   switch (public)
  4547.     {
  4548.     case 0:
  4549.       TREE_PUBLIC (field_decl) = 0;
  4550.       TREE_PRIVATE (field_decl) = 0;
  4551.       TREE_PROTECTED (field_decl) = 1;
  4552.       break;
  4553.       
  4554.     case 1:
  4555.       TREE_PUBLIC (field_decl) = 1;
  4556.       TREE_PRIVATE (field_decl) = 0;
  4557.       TREE_PROTECTED (field_decl) = 0;
  4558.       break;
  4559.       
  4560.     case 2:
  4561.       TREE_PUBLIC (field_decl) = 0;
  4562.       TREE_PRIVATE (field_decl) = 1;
  4563.       TREE_PROTECTED (field_decl) = 0;
  4564.       break;
  4565.     }
  4566.  
  4567.   if (CLASS_IVARS (class))
  4568.     chainon (CLASS_IVARS (class), field_decl);
  4569.   else
  4570.     CLASS_IVARS (class) = field_decl;
  4571.  
  4572.   return class;
  4573. }
  4574.  
  4575. tree
  4576. is_ivar (decl_chain, ident)
  4577.      tree decl_chain;
  4578.      tree ident;
  4579. {
  4580.   for ( ; decl_chain; decl_chain = TREE_CHAIN (decl_chain))
  4581.     if (DECL_NAME (decl_chain) == ident)
  4582.       return decl_chain;
  4583.   return NULL_TREE;
  4584. }
  4585.  
  4586.  
  4587. /* True if the ivar is private and we are not in its implementation.  */
  4588.  
  4589. int
  4590. is_private (decl)
  4591.      tree decl;
  4592. {
  4593.   if (TREE_PRIVATE (decl)
  4594.       && ! is_ivar (CLASS_IVARS (implementation_template), DECL_NAME (decl)))
  4595.     {
  4596.       error ("instance variable `%s' is declared private",
  4597.          IDENTIFIER_POINTER (DECL_NAME (decl)));
  4598.       return 1;
  4599.     }
  4600.   else
  4601.     return 0;
  4602. }
  4603.  
  4604.  
  4605. /* we have an instance variable reference, check to see if it is public...*/
  4606.  
  4607. int
  4608. is_public (expr, identifier)
  4609.      tree expr;
  4610.      tree identifier;
  4611. {
  4612.   tree basetype = TREE_TYPE (expr);
  4613.   enum tree_code code = TREE_CODE (basetype);
  4614.   tree decl;
  4615.  
  4616.   if (code == RECORD_TYPE)
  4617.     {
  4618.       if (TREE_STATIC_TEMPLATE (basetype))
  4619.     {
  4620.       if (!lookup_interface (TYPE_NAME (basetype)))
  4621.         {
  4622.           error ("Cannot find interface declaration for `%s'",
  4623.              IDENTIFIER_POINTER (TYPE_NAME (basetype)));
  4624.           return 0;
  4625.         }
  4626.  
  4627.       if (decl = is_ivar (TYPE_FIELDS (basetype), identifier))
  4628.         {
  4629.           if (TREE_PUBLIC (decl))
  4630.         return 1;
  4631.  
  4632.           /* important diffence between the Stepstone translator:
  4633.          
  4634.          all instance variables should be public within the context
  4635.          of the implementation...
  4636.          */
  4637.           if (implementation_context
  4638.           && (TREE_CODE (implementation_context) == CLASS_IMPLEMENTATION
  4639.               || TREE_CODE (implementation_context) == CATEGORY_IMPLEMENTATION)
  4640.           && CLASS_NAME (implementation_context) == TYPE_NAME (basetype))
  4641.         return (!is_private (decl));
  4642.  
  4643.           error ("instance variable `%s' is declared %s",
  4644.              IDENTIFIER_POINTER (identifier),
  4645.              TREE_PRIVATE (decl) ? "private" : "protected");
  4646.           return 0;
  4647.         }
  4648.     }
  4649.       else if (implementation_context && (basetype == objc_object_reference))
  4650.     {
  4651.       TREE_TYPE (expr) = _PRIVATE_record;
  4652.       warning ("static access to object of type `id'");
  4653.       warning ("please change to type `%s *'",
  4654.            IDENTIFIER_POINTER (CLASS_NAME (implementation_context)));
  4655.     }
  4656.     }
  4657.   return 1;
  4658. }
  4659.  
  4660. /* implement @defs (<classname>) within struct bodies. */
  4661.  
  4662. tree
  4663. get_class_ivars (interface)
  4664.      tree interface;
  4665. {
  4666.   if (!doing_objc_thang)
  4667.     objc_fatal ();
  4668.  
  4669.   /* Make sure we copy the leaf ivars in case @defs is used in a local
  4670.      context.  Otherwise finish_struct will overwrite the layout info
  4671.      using temporary storage.  */
  4672.   return build_ivar_chain (interface, 1);
  4673. }
  4674.  
  4675. tree
  4676. get_class_reference (ident)
  4677.      tree ident;
  4678. {
  4679.   tree params;
  4680.   tree class_name_expr, initlist, constructor;
  4681.  
  4682.   add_class_reference (ident);
  4683.  
  4684. #ifdef OBJC_CLASS_REFS
  4685.   /* = ((struct _objc_class_ref) {(id) "CLASSNAME"})  */
  4686.  
  4687.   class_name_expr = add_objc_string (ident, class_names);
  4688.   class_name_expr = build_c_cast (objc_class_type, class_name_expr);
  4689.  
  4690.   initlist = build_tree_list (NULLT, class_name_expr);
  4691.   constructor = build (CONSTRUCTOR, objc_class_ref_template, NULLT,
  4692.                nreverse (initlist));
  4693.   TREE_CONSTANT (constructor) = 1;
  4694.   TREE_STATIC (constructor) = 1;
  4695.  
  4696.   return build_component_ref (constructor, get_identifier ("class"));
  4697. #else /* OBJC_CLASS_REFS */
  4698.   params = build_tree_list (NULLT,
  4699.                 my_build_string (IDENTIFIER_LENGTH (ident) + 1,
  4700.                          IDENTIFIER_POINTER (ident)));
  4701.  
  4702.   return build_function_call (objc_getClass_decl, params);
  4703. #endif /* OBJC_CLASS_REFS */
  4704. }
  4705.  
  4706. /* make sure all entries in "chain" are also in "list" */
  4707.  
  4708. static int
  4709. check_methods (chain, list, mtype)
  4710.      tree chain;
  4711.      tree list;
  4712.      int mtype;
  4713. {
  4714.   int first = 1;
  4715.  
  4716.   while (chain)
  4717.     {
  4718.       if (!lookup_method (list, chain))
  4719.     {
  4720.       if (first)
  4721.         {
  4722.           if (TREE_CODE (implementation_context) == CLASS_IMPLEMENTATION)
  4723.         warning ("incomplete implementation of class `%s'",
  4724.              IDENTIFIER_POINTER (CLASS_NAME (implementation_context)));
  4725.           else if (TREE_CODE (implementation_context) == CATEGORY_IMPLEMENTATION)
  4726.         warning ("incomplete implementation of category `%s'",
  4727.              IDENTIFIER_POINTER (CLASS_SUPER_NAME (implementation_context)));
  4728.           first = 0;
  4729.         }
  4730.       warning ("method definition for `%c%s' not found",
  4731.            mtype, IDENTIFIER_POINTER (METHOD_SEL_NAME (chain)));
  4732.     }
  4733.       chain = TREE_CHAIN (chain);
  4734.     }
  4735.  
  4736.   return first;
  4737. }
  4738.  
  4739. static void
  4740. check_protocols (proto_list, type, name)
  4741.      tree proto_list;
  4742.      char *type;
  4743.      char *name;
  4744. {
  4745.   for ( ; proto_list; proto_list = TREE_CHAIN (proto_list))
  4746.     {
  4747.       tree p = TREE_VALUE (proto_list);
  4748.       
  4749.       if (TREE_CODE (p) == PROTOCOL_INTERFACE)
  4750.     {
  4751.       int f1, f2;
  4752.       
  4753.       /* Ensure that all protocols have bodies! */
  4754.       f1 = check_methods (PROTOCOL_CLS_METHODS (p),
  4755.             CLASS_CLS_METHODS (implementation_context),
  4756.             '+');
  4757.       f2 = check_methods (PROTOCOL_NST_METHODS (p),
  4758.             CLASS_NST_METHODS (implementation_context),
  4759.             '-');
  4760.             
  4761.       if (!f1 || !f2)
  4762.         warning ("%s `%s' does not fully implement the `%s' protocol",
  4763.              type, name, IDENTIFIER_POINTER (PROTOCOL_NAME (p)));
  4764.     
  4765.     }
  4766.       else 
  4767.     ; /* an identifier...if we could not find a protocol.  */
  4768.       
  4769.       /* Check protocols recursively. */
  4770.       if (PROTOCOL_LIST (p))
  4771.         check_protocols (PROTOCOL_LIST (p), type, name);
  4772.     }
  4773. }
  4774.  
  4775. tree
  4776. start_class (code, class_name, super_name, protocol_list)
  4777.      enum tree_code code;
  4778.      tree class_name;
  4779.      tree super_name;
  4780.      tree protocol_list;
  4781. {
  4782.   tree class, decl;
  4783.  
  4784.   if (!doing_objc_thang)
  4785.     objc_fatal ();
  4786.  
  4787.   class = make_node (code);
  4788.  
  4789.   CLASS_NAME (class) = class_name;
  4790.   CLASS_SUPER_NAME (class) = super_name;
  4791.   CLASS_CLS_METHODS (class) = NULL_TREE;
  4792.  
  4793.   if ((decl = lookup_name (class_name)) != 0)
  4794.     {
  4795.       error ("`%s' redeclared as different kind of symbol",
  4796.          IDENTIFIER_POINTER (class_name));
  4797.       error_with_decl (decl, "previous declaration of `%s'");
  4798.     }
  4799.  
  4800.   if (code == CLASS_IMPLEMENTATION)
  4801.     {
  4802.       /* pre-build the following entities - for speed/convenience. */
  4803.       if (!self_id)
  4804.         self_id = get_identifier ("self");
  4805.       if (!_cmd_id)
  4806.         _cmd_id = get_identifier ("_cmd");
  4807.  
  4808.       if (!objc_super_template)
  4809.     objc_super_template = build_super_template ();
  4810.  
  4811.       method_slot = 0;        /* reset for multiple classes per file */
  4812.  
  4813.       implementation_context = class;
  4814.  
  4815.       /* lookup the interface for this implementation. */
  4816.  
  4817.       if (!(implementation_template = lookup_interface (class_name)))
  4818.         {
  4819.       warning ("Cannot find interface declaration for `%s'",
  4820.            IDENTIFIER_POINTER (class_name));
  4821.       add_class (implementation_template = implementation_context);
  4822.         }
  4823.  
  4824.       /* if a super class has been specified in the implementation,
  4825.      insure it conforms to the one specified in the interface */
  4826.  
  4827.       if (super_name
  4828.       && (super_name != CLASS_SUPER_NAME (implementation_template)))
  4829.         {
  4830.       error ("conflicting super class name `%s'",
  4831.          IDENTIFIER_POINTER (super_name));
  4832.       error ("previous declaration of `%s'",
  4833.          IDENTIFIER_POINTER (CLASS_SUPER_NAME (implementation_template)));
  4834.         }
  4835.     }
  4836.   else if (code == CLASS_INTERFACE)
  4837.     {
  4838.       if (lookup_interface (class_name))
  4839.         warning ("duplicate interface declaration for class `%s'",
  4840.                  IDENTIFIER_POINTER (class_name));
  4841.       else
  4842.         add_class (class);
  4843.  
  4844.       if (protocol_list)
  4845.         {
  4846.       lookup_and_install_protocols (protocol_list);
  4847.       CLASS_PROTOCOL_LIST (class) = protocol_list;
  4848.     }
  4849.     }
  4850.   else if (code == CATEGORY_INTERFACE)
  4851.     {
  4852.       tree class_category_is_assoc_with;
  4853.  
  4854.       /* for a category, class_name is really the name of the class that
  4855.      the following set of methods will be associated with...we must
  4856.      find the interface so that can derive the objects template */
  4857.  
  4858.       if (!(class_category_is_assoc_with = lookup_interface (class_name)))
  4859.     {
  4860.       error ("Cannot find interface declaration for `%s'",
  4861.          IDENTIFIER_POINTER (class_name));
  4862.       exit (1);
  4863.     }
  4864.       else
  4865.         add_category (class_category_is_assoc_with, class);
  4866.  
  4867.       if (protocol_list)
  4868.         {
  4869.       lookup_and_install_protocols (protocol_list);
  4870.       CLASS_PROTOCOL_LIST (class) = protocol_list;
  4871.     }
  4872.     }
  4873.   else if (code == CATEGORY_IMPLEMENTATION)
  4874.     {
  4875.       /* pre-build the following entities - for speed/convenience. */
  4876.       if (!self_id)
  4877.         self_id = get_identifier ("self");
  4878.       if (!_cmd_id)
  4879.         _cmd_id = get_identifier ("_cmd");
  4880.  
  4881.       if (!objc_super_template)
  4882.     objc_super_template = build_super_template ();
  4883.  
  4884.       method_slot = 0;        /* reset for multiple classes per file */
  4885.  
  4886.       implementation_context = class;
  4887.  
  4888.       /* for a category, class_name is really the name of the class that
  4889.      the following set of methods will be associated with...we must
  4890.      find the interface so that can derive the objects template */
  4891.  
  4892.       if (!(implementation_template = lookup_interface (class_name)))
  4893.         {
  4894.       error ("Cannot find interface declaration for `%s'",
  4895.          IDENTIFIER_POINTER (class_name));
  4896.       exit (1);
  4897.         }
  4898.     }
  4899.   return class;
  4900. }
  4901.  
  4902. tree
  4903. continue_class (class)
  4904.      tree class;
  4905. {
  4906.   if (TREE_CODE (class) == CLASS_IMPLEMENTATION
  4907.       || TREE_CODE (class) == CATEGORY_IMPLEMENTATION)
  4908.     {
  4909.       struct imp_entry *impEntry;
  4910.       tree ivar_context;
  4911.  
  4912.       /* check consistency of the instance variables. */
  4913.  
  4914.       if (CLASS_IVARS (class))
  4915.     check_ivars (implementation_template, class);
  4916.  
  4917.       /* code generation */
  4918.  
  4919.       ivar_context = build_private_template (implementation_template);
  4920.  
  4921.       if (!objc_class_template)
  4922.     build_class_template ();
  4923.  
  4924.       if (!(impEntry = (struct imp_entry *)xmalloc (sizeof (struct imp_entry))))
  4925.     perror ("unable to allocate in objc-tree.c");
  4926.  
  4927.       impEntry->next = imp_list;
  4928.       impEntry->imp_context = class;
  4929.       impEntry->imp_template = implementation_template;
  4930.  
  4931.       synth_forward_declarations ();
  4932.       impEntry->class_decl = _OBJC_CLASS_decl;
  4933.       impEntry->meta_decl = _OBJC_METACLASS_decl;
  4934.  
  4935.       /* append to front and increment count */
  4936.       imp_list = impEntry;
  4937.       if (TREE_CODE (class) == CLASS_IMPLEMENTATION)
  4938.     imp_count++;
  4939.       else
  4940.     cat_count++;
  4941.  
  4942.       return ivar_context;
  4943.     }
  4944.   else if (TREE_CODE (class) == CLASS_INTERFACE)
  4945.     {
  4946.       tree record = xref_tag (RECORD_TYPE, CLASS_NAME (class));
  4947.  
  4948.       if (!TYPE_FIELDS (record))
  4949.     {
  4950.       finish_struct (record, build_ivar_chain (class, 0));
  4951.       CLASS_STATIC_TEMPLATE (class) = record;
  4952.  
  4953.       /* mark this record as a class template - for static typing */
  4954.       TREE_STATIC_TEMPLATE (record) = 1;
  4955.     }
  4956.       return NULLT;
  4957.     }
  4958. }
  4959.  
  4960. /*
  4961.  * this is called once we see the "@end" in an interface/implementation.
  4962.  */
  4963. tree
  4964. finish_class (class)
  4965.      tree class;
  4966. {
  4967.   if (TREE_CODE (class) == CLASS_IMPLEMENTATION)
  4968.     {
  4969.       /* all code generation is done in "finish_objc ()" */
  4970.  
  4971.       if (implementation_template != implementation_context)
  4972.     {
  4973.       /* Ensure that all methods listed in the interface have bodies!  */
  4974.       check_methods (CLASS_CLS_METHODS (implementation_template),
  4975.              CLASS_CLS_METHODS (implementation_context), '+');
  4976.       check_methods (CLASS_NST_METHODS (implementation_template),
  4977.              CLASS_NST_METHODS (implementation_context), '-');
  4978.  
  4979.       if (CLASS_PROTOCOL_LIST (implementation_template))
  4980.         check_protocols (CLASS_PROTOCOL_LIST (implementation_template),
  4981.                  "class",
  4982.                  IDENTIFIER_POINTER (CLASS_NAME (implementation_context)));
  4983.     }
  4984.     }
  4985.   else if (TREE_CODE (class) == CATEGORY_IMPLEMENTATION)
  4986.     {
  4987.       tree category = CLASS_CATEGORY_LIST (implementation_template);
  4988.   
  4989.       /* find the category interface from the class it is associated with */
  4990.       while (category)
  4991.     {
  4992.       if (CLASS_SUPER_NAME (class) == CLASS_SUPER_NAME (category))
  4993.         break;
  4994.       category = CLASS_CATEGORY_LIST (category);
  4995.     }
  4996.   
  4997.       if (category)
  4998.     {
  4999.       /* ensure that all method listed in the interface contain bodies! */
  5000.       check_methods (CLASS_CLS_METHODS (category),
  5001.              CLASS_CLS_METHODS (implementation_context), '+');
  5002.       check_methods (CLASS_NST_METHODS (category),
  5003.              CLASS_NST_METHODS (implementation_context), '-');
  5004.  
  5005. /* NEW!!! */
  5006.       if (CLASS_PROTOCOL_LIST (category))
  5007.         check_protocols (CLASS_PROTOCOL_LIST (category),
  5008.                  "category",
  5009.                  IDENTIFIER_POINTER (CLASS_SUPER_NAME (implementation_context)));
  5010.     }
  5011.     }
  5012.   else if (TREE_CODE (class) == CLASS_INTERFACE)
  5013.     {
  5014.       tree decl_specs;
  5015.  
  5016.       /* extern struct objc_object *_<my_name>; */
  5017.  
  5018.       sprintf (utlbuf, "_%s", IDENTIFIER_POINTER (CLASS_NAME (class)));
  5019.  
  5020.       decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_EXTERN]);
  5021.       decl_specs = tree_cons (NULLT, objc_object_reference, decl_specs);
  5022.       define_decl (build1 (INDIRECT_REF, NULLT, get_identifier (utlbuf)), decl_specs);
  5023.     }
  5024. }
  5025.  
  5026. static tree
  5027. add_protocol (protocol)
  5028.      tree protocol;
  5029. {
  5030.   /* put protocol on list in reverse order */
  5031.   TREE_CHAIN (protocol) = protocol_chain;
  5032.   protocol_chain = protocol;
  5033.   return protocol_chain;
  5034. }
  5035.  
  5036. tree
  5037. lookup_protocol (ident)
  5038.      tree ident;
  5039. {
  5040.   tree chain;
  5041.     
  5042.   for (chain = protocol_chain; chain; chain = TREE_CHAIN (chain))
  5043.     {
  5044.       if (ident == PROTOCOL_NAME (chain))
  5045.     return chain;
  5046.     }
  5047.   return NULLT;
  5048. }
  5049.  
  5050. tree
  5051. start_protocol (code, name, list)
  5052.      enum tree_code code;
  5053.      tree name;
  5054.      tree list;
  5055. {
  5056.   tree protocol;
  5057.  
  5058.   if (!doing_objc_thang)
  5059.     objc_fatal ();
  5060.  
  5061.   /* this is as good a place as any...need a "push_tag_toplevel()" */
  5062.   if (!objc_protocol_template)
  5063.     objc_protocol_template = build_protocol_template ();
  5064.  
  5065.   protocol = make_node (code);
  5066.  
  5067.   PROTOCOL_NAME (protocol) = name;
  5068.   PROTOCOL_LIST (protocol) = list;
  5069.  
  5070.   lookup_and_install_protocols (list);
  5071.  
  5072.   if (lookup_protocol (name))
  5073.     warning ("duplicate declaration for protocol `%s'",
  5074.          IDENTIFIER_POINTER (name));
  5075.   else
  5076.     add_protocol (protocol);
  5077.  
  5078.   PROTOCOL_FORWARD_DECL (protocol) = NULL_TREE;
  5079.  
  5080.   return protocol;
  5081. }
  5082.  
  5083. void
  5084. finish_protocol (protocol)
  5085.     tree protocol;
  5086. {
  5087. }
  5088.  
  5089. static void
  5090. encode_type_qualifiers (declspecs, utlbuf)
  5091.      tree declspecs;
  5092.      char *utlbuf;
  5093. {
  5094.   tree spec;
  5095.  
  5096.   for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
  5097.     {
  5098.       if (ridpointers[RID_CONST] == TREE_VALUE (spec))
  5099.     strcat (utlbuf, "r");
  5100.       else if (ridpointers[RID_IN] == TREE_VALUE (spec))
  5101.     strcat (utlbuf, "n");
  5102.       else if (ridpointers[RID_INOUT] == TREE_VALUE (spec))
  5103.     strcat (utlbuf, "N");
  5104.       else if (ridpointers[RID_OUT] == TREE_VALUE (spec))
  5105.     strcat (utlbuf, "o");
  5106.       else if (ridpointers[RID_BYCOPY] == TREE_VALUE (spec))
  5107.     strcat (utlbuf, "O");
  5108.       else if (ridpointers[RID_ONEWAY] == TREE_VALUE (spec))
  5109.     strcat (utlbuf, "V");
  5110.     }
  5111. }
  5112.  
  5113. static void
  5114. encode_pointer (type, str, curtype, format)
  5115.      tree type;
  5116.      char *str;
  5117.      char *curtype;
  5118.      int format;
  5119. {
  5120.   tree pointer_to = TREE_TYPE (type);
  5121.  
  5122.   if (TREE_CODE (pointer_to) == RECORD_TYPE)
  5123.     {
  5124.       if (TYPE_NAME (pointer_to)
  5125.       && TREE_CODE (TYPE_NAME (pointer_to)) == IDENTIFIER_NODE)
  5126.     {
  5127.       char *name = IDENTIFIER_POINTER (TYPE_NAME (pointer_to));
  5128.  
  5129.       if ((strcmp (name, TAG_OBJECT) == 0) || /* '@' */
  5130.           (TREE_STATIC_TEMPLATE (pointer_to)))
  5131.         {
  5132.           strcat (str, "@");
  5133.           return;
  5134.         }
  5135.       else if (strcmp (name, TAG_CLASS) == 0) /* '#' */
  5136.         {
  5137.           strcat (str, "#");
  5138.           return;
  5139.         }
  5140. #ifdef OBJC_SELS_R_STRUCT_PTRS
  5141.       else if (strcmp (name, TAG_SELECTOR) == 0) /* ':' */
  5142.         {
  5143.           strcat (str, ":");
  5144.           return;
  5145.         }
  5146. #endif
  5147.     }
  5148.     }
  5149.   else if (TREE_CODE (pointer_to) == INTEGER_TYPE
  5150.        && TYPE_MODE (pointer_to) == QImode)
  5151.     {
  5152.       strcat (str, "*");
  5153.       return;
  5154.     }
  5155.  
  5156.   /* we have a type that does not get special treatment... */
  5157.  
  5158.   /* NeXT extension */
  5159.   strcat (str, "^");
  5160.   encode_type (pointer_to, str, curtype, format);
  5161. }
  5162.  
  5163. static void
  5164. encode_array (type, str, curtype, format)
  5165.      tree type;
  5166.      char *str;
  5167.      char *curtype;
  5168.      int format;
  5169. {
  5170.   tree anIntCst = TYPE_SIZE (type);
  5171.   tree array_of = TREE_TYPE (type);
  5172.  
  5173.   /* An incomplete array is treated like a pointer.  */
  5174.   if (anIntCst == NULL)
  5175.     {
  5176.       /* split for obvious reasons.  North-Keys 30 Mar 1991 */
  5177.       encode_pointer (type, str, curtype, format);
  5178.       return;
  5179.     }
  5180.   
  5181.   sprintf (str + strlen (str), "[%d",
  5182.        TREE_INT_CST_LOW (anIntCst)
  5183.        / TREE_INT_CST_LOW (TYPE_SIZE (array_of)));
  5184.   encode_type (array_of, str, curtype, format);
  5185.   strcat (str, "]");
  5186.   return;
  5187. }
  5188.  
  5189. static void
  5190. encode_aggregate (type, str, curtype, format)
  5191.      tree type;
  5192.      char *str;
  5193.      char *curtype;
  5194.      int format;
  5195. {
  5196.   enum tree_code code = TREE_CODE (type);
  5197.  
  5198.   switch (code)
  5199.     {
  5200.     case RECORD_TYPE:
  5201.       {
  5202.     if (strlen (curtype) && curtype[strlen (curtype) - 1] == '^')
  5203.       {
  5204.         tree name = TYPE_NAME (type);
  5205.  
  5206.         /* we have a reference - this is a NeXT extension */
  5207.  
  5208.             if ((strlen (curtype) == 1)
  5209.             && (format == OBJC_ENCODE_INLINE_DEFS))
  5210.           {
  5211.         /* output format of struct for first level only! */
  5212.         
  5213.         tree fields = TYPE_FIELDS (type);
  5214.         
  5215.                 if (name && (TREE_CODE (name) == IDENTIFIER_NODE))
  5216.               sprintf (str + strlen (str), "{%s=",
  5217.                       IDENTIFIER_POINTER (name));
  5218.         else
  5219.           strcat (str, "{?=");
  5220.           
  5221.         for ( ; fields; fields = TREE_CHAIN (fields))
  5222.           encode_field_decl (fields, str, curtype, format);
  5223.         strcat (str, "}");
  5224.           }
  5225.             else if (name && (TREE_CODE (name) == IDENTIFIER_NODE))
  5226.           sprintf (str + strlen (str), "{%s}",
  5227.                IDENTIFIER_POINTER (name));
  5228.         else    /* we have an untagged structure or a typedef */
  5229.           sprintf (str + strlen (str), "{?}");
  5230.       }
  5231.     else
  5232.       {
  5233.         tree name = TYPE_NAME (type);
  5234.         tree fields = TYPE_FIELDS (type);
  5235.  
  5236.             if (format == OBJC_ENCODE_INLINE_DEFS)
  5237.               {
  5238.                 if (name && (TREE_CODE (name) == IDENTIFIER_NODE))
  5239.               sprintf (str + strlen (str), "{%s=",
  5240.                       IDENTIFIER_POINTER (name));
  5241.         else
  5242.           strcat (str, "{?=");
  5243.           
  5244.         for ( ; fields; fields = TREE_CHAIN (fields))
  5245.           encode_field_decl (fields, str, curtype, format);
  5246.         strcat (str, "}");
  5247.               }
  5248.             else
  5249.               {
  5250.                 if (name && (TREE_CODE (name) == IDENTIFIER_NODE))
  5251.           sprintf (str + strlen (str), "{%s}",
  5252.                IDENTIFIER_POINTER (name));
  5253.         else    /* we have an untagged structure or a typedef */
  5254.           sprintf (str + strlen (str), "{?}");
  5255.               }
  5256.       }
  5257.     break;
  5258.       }
  5259.     case UNION_TYPE:
  5260.       {
  5261.     if (str[strlen (str)-1] == '^')
  5262.       {
  5263.             if (TYPE_NAME (type)
  5264.         && (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE))
  5265.           /* we have a reference - this is a NeXT extension */
  5266.           sprintf (str + strlen (str), "(%s)",
  5267.                IDENTIFIER_POINTER (TYPE_NAME (type)));
  5268.         else        /* we have an untagged structure */
  5269.           sprintf (str + strlen (str), "(?)");
  5270.       }
  5271.     else
  5272.       {
  5273.         tree fields = TYPE_FIELDS (type);
  5274.  
  5275.             if (format == OBJC_ENCODE_INLINE_DEFS)
  5276.               {
  5277.         strcat (str, "(");
  5278.         for ( ; fields; fields = TREE_CHAIN (fields))
  5279.           encode_field_decl (fields, str, curtype, format);
  5280.         strcat (str, ")");
  5281.               }
  5282.             else
  5283.               {
  5284.         if (TYPE_NAME (type) &&
  5285.             (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE))
  5286.           /* we have a reference - this is a NeXT extension */
  5287.           sprintf (str + strlen (str), "(%s)",
  5288.                IDENTIFIER_POINTER (TYPE_NAME (type)));
  5289.         else        /* we have an untagged structure */
  5290.           sprintf (str + strlen (str), "(?)");
  5291.               }
  5292.       }
  5293.     break;
  5294.       }
  5295.     case ENUMERAL_TYPE:
  5296.       strcat (str, "i");
  5297.       break;
  5298.     }
  5299. }
  5300.  
  5301. /*
  5302.  *  support bitfields, the current version of Objective-C does not support
  5303.  *  them. the string will consist of one or more "b:n"'s where n is an
  5304.  *  integer describing the width of the bitfield. Currently, classes in
  5305.  *  the kit implement a method "-(char *)describeBitfieldStruct:" that
  5306.  *  simulates this...if they do not implement this method, the archiver
  5307.  *  assumes the bitfield is 16 bits wide (padded if necessary) and packed
  5308.  *  according to the GNU compiler. After looking at the "kit", it appears
  5309.  *  that all classes currently rely on this default behavior, rather than
  5310.  *  hand generating this string (which is tedious).
  5311.  */
  5312. static void
  5313. encode_bitfield (width, str, format)
  5314.      int width;
  5315.      char *str;
  5316.      int format;
  5317. {
  5318.   sprintf (str + strlen (str), "b%d", width);
  5319. }
  5320.  
  5321. /*
  5322.  *    format will be:
  5323.  *
  5324.  *    OBJC_ENCODE_INLINE_DEFS or OBJC_ENCODE_DONT_INLINE_DEFS
  5325.  */
  5326. static void
  5327. encode_type (type, str, curtype, format)
  5328.      tree type;
  5329.      char *str;
  5330.      char *curtype;
  5331.      int format;
  5332. {
  5333.   enum tree_code code = TREE_CODE (type);
  5334.  
  5335.   if (code == INTEGER_TYPE)
  5336.     {
  5337.       if (TREE_INT_CST_LOW (TYPE_MIN_VALUE (type)) == 0)
  5338.     {
  5339.       /* unsigned integer types */
  5340.  
  5341.       if (TYPE_MODE (type) == QImode) /* 'C' */
  5342.         strcat (str, "C");
  5343.       else if (TYPE_MODE (type) == HImode) /* 'S' */
  5344.         strcat (str, "S");
  5345.       else if (TYPE_MODE (type) == SImode)
  5346.         {
  5347.           if (type == long_unsigned_type_node)
  5348.         strcat (str, "L"); /* 'L' */
  5349.           else
  5350.         strcat (str, "I"); /* 'I' */
  5351.         }
  5352.     }
  5353.       else            /* signed integer types */
  5354.     {
  5355.       if (TYPE_MODE (type) == QImode) /* 'c' */
  5356.         strcat (str, "c");
  5357.       else if (TYPE_MODE (type) == HImode) /* 's' */
  5358.         strcat (str, "s");
  5359.       else if (TYPE_MODE (type) == SImode) /* 'i' */
  5360.         {
  5361.           if (type == long_integer_type_node)
  5362.         strcat (str, "l"); /* 'l' */
  5363.           else
  5364.         strcat (str, "i"); /* 'i' */
  5365.         }
  5366.     }
  5367.     }
  5368.   else if (code == REAL_TYPE)
  5369.     {
  5370.       /* floating point types */
  5371.  
  5372.       if (TYPE_MODE (type) == SFmode) /* 'f' */
  5373.     strcat (str, "f");
  5374.       else if (TYPE_MODE (type) == DFmode
  5375.            || TYPE_MODE (type) == TFmode) /* 'd' */
  5376.     strcat (str, "d");
  5377.     }
  5378.  
  5379.   else if (code == VOID_TYPE)    /* 'v' */
  5380.     strcat (str, "v");
  5381.  
  5382.   else if (code == ARRAY_TYPE)
  5383.     encode_array (type, str, curtype, format);
  5384.  
  5385.   else if (code == POINTER_TYPE)
  5386.     encode_pointer (type, str, curtype, format);
  5387.  
  5388.   else if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
  5389.     encode_aggregate (type, str, curtype, format);
  5390.  
  5391.   else if (code == FUNCTION_TYPE) /* '?' */
  5392.     strcat (str, "?");
  5393. }
  5394.  
  5395. static void
  5396. encode_field_decl (field_decl, str, curtype, format)
  5397.      tree field_decl;
  5398.      char *str;
  5399.      char *curtype;
  5400.      int format;
  5401. {
  5402.   if (DECL_BIT_FIELD (field_decl))
  5403.     encode_bitfield (DECL_FRAME_SIZE (field_decl), str, format);
  5404.   else
  5405.     encode_type (TREE_TYPE (field_decl), str, curtype, format);
  5406. }
  5407.  
  5408. static tree
  5409. expr_last (complex_expr)
  5410.      tree complex_expr;
  5411. {
  5412.   tree next;
  5413.  
  5414.   if (complex_expr)
  5415.     while (next = TREE_OPERAND (complex_expr, 0))
  5416.       complex_expr = next;
  5417.   return complex_expr;
  5418. }
  5419.  
  5420.  
  5421. /* The selector of the current method,
  5422.    or NULL if we aren't compiling a method.  */
  5423.  
  5424. tree
  5425. maybe_objc_method_name (decl)
  5426.       tree decl;
  5427. {
  5428.   if (method_context)
  5429.     return METHOD_SEL_NAME (method_context);
  5430.   else
  5431.     return 0;
  5432. }
  5433.  
  5434. /*
  5435.  *  Transform a method definition into a function definition as follows:
  5436.  *
  5437.  *  - synthesize the first two arguments, "self" and "_cmd".
  5438.  */
  5439.  
  5440. void
  5441. start_method_def (method)
  5442.      tree method;
  5443. {
  5444.   tree decl_specs;
  5445.  
  5446.   /* required to implement _msgSuper () */
  5447.   method_context = method;
  5448.   _OBJC_SUPER_decl = NULLT;
  5449.  
  5450.   pushlevel (0);         /* must be called BEFORE "start_function ()" */
  5451.  
  5452.   /* generate prototype declarations for arguments..."new-style" */
  5453.  
  5454.   if (TREE_CODE (method_context) == INSTANCE_METHOD_DECL)
  5455.     decl_specs = build_tree_list (NULLT, _PRIVATE_record);
  5456.   else
  5457.     /* really a `struct objc_class *'...however we allow people to
  5458.        assign to self...which changes its type midstream.
  5459.        */
  5460.     decl_specs = build_tree_list (NULLT, objc_object_reference);
  5461.  
  5462.   push_parm_decl (build_tree_list (decl_specs,
  5463.                    build1 (INDIRECT_REF, NULLT, self_id)));
  5464.  
  5465. #ifdef OBJC_SELS_R_INTS
  5466.   decl_specs = build_tree_list (NULLT, ridpointers[(int) RID_UNSIGNED]);
  5467.   decl_specs = tree_cons (NULLT, ridpointers[(int) RID_INT], decl_specs);
  5468.   push_parm_decl (build_tree_list (decl_specs, _cmd_id));
  5469. #endif
  5470.  
  5471. #ifdef OBJC_SELS_R_STRUCT_PTRS
  5472.   decl_specs = build_tree_list (NULLT,
  5473.                 xref_tag (RECORD_TYPE,
  5474.                       get_identifier (TAG_SELECTOR)));
  5475.   push_parm_decl (build_tree_list (decl_specs, 
  5476.                    build1 (INDIRECT_REF, NULLT, _cmd_id)));
  5477. #endif
  5478.  
  5479.   /* generate argument delclarations if a keyword_decl */
  5480.   if (METHOD_SEL_ARGS (method))
  5481.     {
  5482.       tree arglist = METHOD_SEL_ARGS (method);
  5483.       do
  5484.     {
  5485.       tree arg_spec = TREE_PURPOSE (TREE_TYPE (arglist));
  5486.       tree arg_decl = TREE_VALUE (TREE_TYPE (arglist));
  5487.  
  5488.       if (arg_decl)
  5489.         {
  5490.           tree last_expr = expr_last (arg_decl);
  5491.  
  5492.           /* unite the abstract decl with its name */
  5493.           TREE_OPERAND (last_expr, 0) = KEYWORD_ARG_NAME (arglist);
  5494.           push_parm_decl (build_tree_list (arg_spec, arg_decl));
  5495.           /* unhook...restore the abstract declarator */
  5496.           TREE_OPERAND (last_expr, 0) = NULLT;
  5497.         }
  5498.       else
  5499.         push_parm_decl (build_tree_list (arg_spec, KEYWORD_ARG_NAME (arglist)));
  5500.  
  5501.       arglist = TREE_CHAIN (arglist);
  5502.     }
  5503.       while (arglist);
  5504.     }
  5505.  
  5506.   if (METHOD_ADD_ARGS (method) > (tree)1)
  5507.     {
  5508.       /* we have a variable length selector - in "prototype" format */
  5509.       tree akey = TREE_PURPOSE (METHOD_ADD_ARGS (method));
  5510.       while (akey)
  5511.     {
  5512.       /* this must be done prior to calling pushdecl (). pushdecl () is
  5513.        * going to change our chain on us...
  5514.        */
  5515.       tree nextkey = TREE_CHAIN (akey);
  5516.       pushdecl (akey);
  5517.       akey = nextkey;
  5518.     }
  5519.     }
  5520. }
  5521.  
  5522. static void
  5523. error_with_method (message, mtype, method)
  5524.      char *message;
  5525.      char mtype;
  5526.      tree method;
  5527. {
  5528.   extern int errorcount;
  5529.  
  5530. #ifdef REPORT_EVENT
  5531.   bzero (errbuf, BUFSIZE);
  5532.   report_event (0, current_function_decl,
  5533.         METHOD_FILENAME (method), METHOD_LINENUM (method),
  5534.         "%s `%c%s'", message, mtype,
  5535.         gen_method_decl (method, errbuf));
  5536. #endif
  5537.   fprintf (stderr, "%s:%d: ",
  5538.        METHOD_FILENAME (method), METHOD_LINENUM (method));
  5539.   bzero (errbuf, BUFSIZE);
  5540.   fprintf (stderr, "%s `%c%s'\n", message, mtype, gen_method_decl (method, errbuf));
  5541.   errorcount++;
  5542. }
  5543.  
  5544. static void
  5545. warn_with_method (message, mtype, method)
  5546.      char *message;
  5547.      char mtype;
  5548.      tree method;
  5549. {
  5550. #ifdef REPORT_EVENT
  5551.   bzero (errbuf, BUFSIZE);
  5552.   report_event (1, current_function_decl,
  5553.         METHOD_FILENAME (method), METHOD_LINENUM (method),
  5554.         "%s `%c%s'", message, mtype,
  5555.         gen_method_decl (method, errbuf));
  5556. #endif
  5557.   fprintf (stderr, "%s:%d: ",
  5558.        METHOD_FILENAME (method), METHOD_LINENUM (method));
  5559.   bzero (errbuf, BUFSIZE);
  5560.   fprintf (stderr, "%s `%c%s'\n", message, mtype, gen_method_decl (method, errbuf));
  5561. }
  5562.  
  5563. /* return 1 if `method' is consistent with `proto' */
  5564.  
  5565. static int
  5566. comp_method_with_proto (method, proto)
  5567.      tree method, proto;
  5568. {
  5569.   static tree function_type = 0;
  5570.  
  5571.   /* create a function_type node once */
  5572.   if (!function_type)
  5573.     {
  5574.       struct obstack *ambient_obstack = current_obstack;
  5575.       
  5576.       current_obstack = &permanent_obstack;
  5577.       function_type = make_node (FUNCTION_TYPE);
  5578.       current_obstack = ambient_obstack;
  5579.     }
  5580.  
  5581.   /* install argument types - normally set by "build_function_type ()". */
  5582.   TYPE_ARG_TYPES (function_type) = get_arg_type_list (proto, METHOD_DEF, 0);
  5583.  
  5584.   /* install return type */
  5585.   TREE_TYPE (function_type) = groktypename (TREE_TYPE (proto));
  5586.  
  5587.   return comptypes (TREE_TYPE (METHOD_DEFINITION (method)), function_type);
  5588. }
  5589.  
  5590. /* return 1 if `proto1' is consistent with `proto2' */
  5591.  
  5592. static int
  5593. comp_proto_with_proto (proto1, proto2)
  5594.      tree proto1, proto2;
  5595. {
  5596.   static tree function_type1 = 0, function_type2 = 0;
  5597.  
  5598.   /* create a couple function_type node's once */
  5599.   if (!function_type1)
  5600.     {
  5601.       struct obstack *ambient_obstack = current_obstack;
  5602.       
  5603.       current_obstack = &permanent_obstack;
  5604.       function_type1 = make_node (FUNCTION_TYPE);
  5605.       function_type2 = make_node (FUNCTION_TYPE);
  5606.       current_obstack = ambient_obstack;
  5607.     }
  5608.  
  5609.   /* install argument types - normally set by "build_function_type ()". */
  5610.   TYPE_ARG_TYPES (function_type1) = get_arg_type_list (proto1, METHOD_REF, 0);
  5611.   TYPE_ARG_TYPES (function_type2) = get_arg_type_list (proto2, METHOD_REF, 0);
  5612.  
  5613.   /* install return type */
  5614.   TREE_TYPE (function_type1) = groktypename (TREE_TYPE (proto1));
  5615.   TREE_TYPE (function_type2) = groktypename (TREE_TYPE (proto2));
  5616.  
  5617.   return comptypes (function_type1, function_type2);
  5618. }
  5619.  
  5620. /*
  5621.  *  - generate an identifier for the function. the format is "_n_cls",
  5622.  *    where 1 <= n <= nMethods, and cls is the name the implementation we
  5623.  *    are processing.
  5624.  *  - install the return type from the method declaration.
  5625.  *  - if we have a prototype, check for type consistency.
  5626.  */
  5627. static void
  5628. really_start_method (method, parmlist)
  5629.      tree method, parmlist;
  5630. {
  5631.   tree sc_spec, ret_spec, ret_decl, decl_specs;
  5632.   tree method_decl, method_id;
  5633.   char buf[256];
  5634.  
  5635.   /* synth the storage class & assemble the return type */
  5636.   sc_spec = tree_cons (NULLT, ridpointers[(int) RID_STATIC], NULLT);
  5637.   ret_spec = TREE_PURPOSE (TREE_TYPE (method));
  5638.   decl_specs = chainon (sc_spec, ret_spec);
  5639.  
  5640.   if (TREE_CODE (implementation_context) == CLASS_IMPLEMENTATION)
  5641. #ifdef OBJC_GEN_METHOD_LABEL
  5642.     OBJC_GEN_METHOD_LABEL (buf,
  5643.       TREE_CODE (method) == INSTANCE_METHOD_DECL,
  5644.       IDENTIFIER_POINTER (CLASS_NAME (implementation_context)),
  5645.       NULL,
  5646.       IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
  5647. #else
  5648.     sprintf (buf, "_%d_%s", ++method_slot,
  5649.          IDENTIFIER_POINTER (CLASS_NAME (implementation_context)));
  5650. #endif
  5651.   else                /* we have a category */
  5652. #ifdef OBJC_GEN_METHOD_LABEL
  5653.     OBJC_GEN_METHOD_LABEL (buf,
  5654.       TREE_CODE (method) == INSTANCE_METHOD_DECL,
  5655.       IDENTIFIER_POINTER (CLASS_NAME (implementation_context)),
  5656.       IDENTIFIER_POINTER (CLASS_SUPER_NAME (implementation_context)),
  5657.       IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
  5658. #else
  5659.     sprintf (buf, "_%d_%s_%s", ++method_slot,
  5660.          IDENTIFIER_POINTER (CLASS_NAME (implementation_context)),
  5661.          IDENTIFIER_POINTER (CLASS_SUPER_NAME (implementation_context)));
  5662. #endif
  5663.  
  5664.   method_id = get_identifier (buf);
  5665.  
  5666.   method_decl = build_nt (CALL_EXPR, method_id, parmlist, NULLT);
  5667.  
  5668.   /* check the delclarator portion of the return type for the method */
  5669.   if (ret_decl = TREE_VALUE (TREE_TYPE (method)))
  5670.     {
  5671.       /*
  5672.        * unite the complex decl (specified in the abstract decl) with the
  5673.        * function decl just synthesized...(int *), (int (*)()), (int (*)[]).
  5674.        */
  5675.       tree save_expr = expr_last (ret_decl);
  5676.  
  5677.       TREE_OPERAND (save_expr, 0) = method_decl;
  5678.       method_decl = ret_decl;
  5679.       /* fool the parser into thinking it is starting a function */
  5680.       start_function (decl_specs, method_decl, 0);
  5681.       /* unhook...this has the effect of restoring the abstract declarator */
  5682.       TREE_OPERAND (save_expr, 0) = NULLT;
  5683.     }
  5684.   else
  5685.     {
  5686.       TREE_VALUE (TREE_TYPE (method)) = method_decl;
  5687.       /* fool the parser into thinking it is starting a function */
  5688.       start_function (decl_specs, method_decl, 0);
  5689.       /* unhook...this has the effect of restoring the abstract declarator */
  5690.       TREE_VALUE (TREE_TYPE (method)) = NULLT;
  5691.     }
  5692.  
  5693.   METHOD_DEFINITION (method) = current_function_decl;
  5694.  
  5695.   /* check consistency...start_function (), pushdecl (), duplicate_decls (). */
  5696.  
  5697.   if (implementation_template != implementation_context)
  5698.     {
  5699.       tree chain, proto;
  5700.  
  5701.       if (TREE_CODE (method) == INSTANCE_METHOD_DECL)
  5702.     proto = lookup_instance_method_static (implementation_template,
  5703.                            METHOD_SEL_NAME (method));
  5704.       else
  5705.     proto = lookup_class_method_static (implementation_template,
  5706.                         METHOD_SEL_NAME (method));
  5707.  
  5708.       if (proto)
  5709.     {
  5710.       if (!comp_method_with_proto (method, proto))
  5711.         {
  5712.           fprintf (stderr, "%s: In method `%s'\n", input_filename,
  5713.                IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
  5714.           if (TREE_CODE (method) == INSTANCE_METHOD_DECL)
  5715.         {
  5716.           warn_with_method ("conflicting types for", '-', method);
  5717.           warn_with_method ("previous declaration of", '-', proto);
  5718.         }
  5719.           else
  5720.         {
  5721.           warn_with_method ("conflicting types for", '+', method);
  5722.           warn_with_method ("previous declaration of", '+', proto);
  5723.         }
  5724.         }
  5725.     }
  5726.     }
  5727. }
  5728.  
  5729. /*
  5730.  * the following routine is always called...this "architecture" is to
  5731.  * accommodate "old-style" variable length selectors.
  5732.  *
  5733.  *    - a:a b:b // prototype  ; id c; id d; // old-style
  5734.  */
  5735. void
  5736. continue_method_def ()
  5737. {
  5738.   tree parmlist;
  5739.  
  5740.   if (METHOD_ADD_ARGS (method_context) == (tree)1)
  5741.     /*
  5742.      * we have a `, ...' immediately following the selector.
  5743.      */
  5744.     parmlist = get_parm_info (0);
  5745.   else
  5746.     parmlist = get_parm_info (1); /* place a `void_at_end' */
  5747.  
  5748.   /* set self_decl from the first argument...this global is used by
  5749.    * build_ivar_reference ().build_indirect_ref ().
  5750.    */
  5751.   self_decl = TREE_PURPOSE (parmlist);
  5752.  
  5753.   poplevel (0, 0, 0);        /* must be called BEFORE "start_function ()" */
  5754.  
  5755.   really_start_method (method_context, parmlist);
  5756.  
  5757.   store_parm_decls ();        /* must be called AFTER "start_function ()" */
  5758. }
  5759.  
  5760. /* Called by the parser, from the `pushlevel' production.  */
  5761.  
  5762. void
  5763. add_objc_decls ()
  5764. {
  5765.   if (!_OBJC_SUPER_decl)
  5766.     {
  5767.       _OBJC_SUPER_decl = start_decl (get_identifier (_TAG_SUPER),
  5768.                      build_tree_list (NULLT,
  5769.                               objc_super_template),
  5770.                      0);
  5771.  
  5772.       finish_decl (_OBJC_SUPER_decl, NULLT, NULLT);
  5773.  
  5774.       /* this prevents `unused variable' warnings. */
  5775.       TREE_USED (_OBJC_SUPER_decl) = 1;
  5776.     }
  5777. }
  5778.  
  5779. /*
  5780.  *    _n_Method (id self, SEL sel, ...)
  5781.  *    {
  5782.  *        struct objc_super _S;
  5783.  *
  5784.  *        _msgSuper ((_S.self = self, _S.class = _cls, &_S), ...);
  5785.  *    }
  5786.  */
  5787. tree
  5788. get_super_receiver ()
  5789. {
  5790.   if (method_context)
  5791.     {
  5792.       tree super_expr, super_expr_list;
  5793.  
  5794.       /* set receiver to self */
  5795.       super_expr = build_component_ref (_OBJC_SUPER_decl, self_id);
  5796.       super_expr = build_modify_expr (super_expr, NOP_EXPR, self_decl);
  5797.       super_expr_list = build_tree_list (NULLT, super_expr);
  5798.  
  5799.       /* set class to begin searching */
  5800.       super_expr = build_component_ref (_OBJC_SUPER_decl, get_identifier ("class"));
  5801.  
  5802.       if (TREE_CODE (implementation_context) == CLASS_IMPLEMENTATION)
  5803.     {
  5804.       /* [_cls, __cls]Super are "pre-built" in synth_foward_declarations () */
  5805.  
  5806.       if (TREE_CODE (method_context) == INSTANCE_METHOD_DECL)
  5807.         super_expr = build_modify_expr (super_expr, NOP_EXPR, _clsSuper_ref);
  5808.       else
  5809.         super_expr = build_modify_expr (super_expr, NOP_EXPR, __clsSuper_ref);
  5810.     }
  5811.       else            /* we have a category... */
  5812.     {
  5813.       tree params, super_name = CLASS_SUPER_NAME (implementation_template);
  5814.       tree funcCall;
  5815.       tree super_class;
  5816.  
  5817.       if (!super_name)  /* Barf if super used in a category of Object. */
  5818.         {
  5819.           error("no super class declared in interface for `%s'",
  5820.             IDENTIFIER_POINTER (CLASS_NAME (implementation_template)));
  5821.           return error_mark_node;
  5822.         }
  5823.  
  5824. #ifdef OBJC_CLASS_REFS
  5825.       super_class = get_class_reference (super_name);
  5826.       if (TREE_CODE (method_context) == CLASS_METHOD_DECL)
  5827.         super_class
  5828.           = build_component_ref (build_indirect_ref (super_class, "->"),
  5829.                      get_identifier ("isa"));
  5830.       TREE_TYPE (super_class) = TREE_TYPE (_clsSuper_ref);
  5831.       super_expr = build_modify_expr (super_expr, NOP_EXPR, super_class);
  5832. #else /* OBJC_CLASS_REFS */
  5833.       add_class_reference (super_name);
  5834.  
  5835.       params = build_tree_list (NULLT,
  5836.                     my_build_string (IDENTIFIER_LENGTH (super_name) + 1,
  5837.                              IDENTIFIER_POINTER (super_name)));
  5838.  
  5839.       if (TREE_CODE (method_context) == INSTANCE_METHOD_DECL)
  5840.         funcCall = build_function_call (objc_getClass_decl, params);
  5841.       else
  5842.         funcCall = build_function_call (objc_getMetaClass_decl, params);
  5843.  
  5844.       /* cast! */
  5845.       TREE_TYPE (funcCall) = TREE_TYPE (_clsSuper_ref);
  5846.       super_expr = build_modify_expr (super_expr, NOP_EXPR, funcCall);
  5847. #endif /* OBJC_CLASS_REFS */
  5848.     }
  5849.       chainon (super_expr_list, build_tree_list (NULL_TREE, super_expr));
  5850.  
  5851.       super_expr = build_unary_op (ADDR_EXPR, _OBJC_SUPER_decl, 0);
  5852.       chainon (super_expr_list, build_tree_list (NULL_TREE, super_expr));
  5853.  
  5854.       return build_compound_expr (super_expr_list);
  5855.     }
  5856.   else
  5857.     {
  5858.       error ("[super ...] must appear in a method context");
  5859.       return error_mark_node;
  5860.     }
  5861. }
  5862.  
  5863. static tree
  5864. encode_method_def (func_decl)
  5865.       tree func_decl;
  5866. {
  5867.   tree parms;
  5868.   int stack_size = 0;
  5869.  
  5870.   bzero (utlbuf, BUFSIZE);
  5871.  
  5872.   /* return type */
  5873.   encode_type (TREE_TYPE (TREE_TYPE (func_decl)), utlbuf,
  5874.            utlbuf + strlen (utlbuf), OBJC_ENCODE_INLINE_DEFS);
  5875.  
  5876.   /* stack size */
  5877.   for (parms = DECL_ARGUMENTS (func_decl); parms;
  5878.        parms = TREE_CHAIN (parms))
  5879.     stack_size += TREE_INT_CST_LOW (TYPE_SIZE (DECL_ARG_TYPE (parms)))
  5880.           / BITS_PER_UNIT;
  5881.  
  5882.   sprintf (&utlbuf[strlen (utlbuf)], "%d", stack_size);
  5883.  
  5884.   /* argument types */
  5885.   for (parms = DECL_ARGUMENTS (func_decl); parms;
  5886.        parms = TREE_CHAIN (parms))
  5887.     {
  5888.       int offset_in_bytes;
  5889.   
  5890.       /* type */ 
  5891.       encode_type (TREE_TYPE (parms), utlbuf, utlbuf + strlen (utlbuf),
  5892.            OBJC_ENCODE_INLINE_DEFS);
  5893.   
  5894.       /* compute offset */
  5895.       if (GET_CODE (DECL_INCOMING_RTL (parms)) == MEM)
  5896.         {
  5897.       rtx addr = XEXP (DECL_INCOMING_RTL (parms), 0);
  5898.       
  5899.       /* ??? Here we assume that the parm address is indexed
  5900.           off the frame pointer or arg pointer.
  5901.           If that is not true, we produce meaningless results,
  5902.           but do not crash.  */
  5903.       if (GET_CODE (addr) == PLUS
  5904.           && GET_CODE (XEXP (addr, 1)) == CONST_INT)
  5905.         offset_in_bytes = INTVAL (XEXP (addr, 1));
  5906.       else
  5907.         offset_in_bytes = 0;
  5908.       
  5909.       /* This is the case where the parm is passed as an int or double
  5910.           and it is converted to a char, short or float and stored back
  5911.           in the parmlist.  In this case, describe the parm
  5912.           with the variable's declared type, and adjust the address
  5913.           if the least significant bytes (which we are using) are not
  5914.           the first ones.  */
  5915. #if BYTES_BIG_ENDIAN
  5916.       if (TREE_TYPE (parms) != DECL_ARG_TYPE (parms))
  5917.         offset_in_bytes += (GET_MODE_SIZE (TYPE_MODE (DECL_ARG_TYPE (parms)))
  5918.                 - GET_MODE_SIZE (GET_MODE (DECL_RTL (parms))));
  5919. #endif
  5920.     }
  5921.       else
  5922.         offset_in_bytes = 0;
  5923.       
  5924.       /* The "+ 4" is a total hack to account for the return pc and
  5925.          saved fp on the 68k.  We should redefine this format! */
  5926.       sprintf (&utlbuf[strlen (utlbuf)], "%d", offset_in_bytes + 8);
  5927.     }
  5928.  
  5929.   return get_identifier (utlbuf);
  5930. }
  5931.  
  5932. void
  5933. finish_method_def ()
  5934. {
  5935.   METHOD_ENCODING (method_context) =
  5936.     encode_method_def (current_function_decl);
  5937.  
  5938.   finish_function (0);
  5939.  
  5940.   /* this must be done AFTER finish_function, since the optimizer may
  5941.      find "may be used before set" errors.  */
  5942.   method_context = NULLT;    /* required to implement _msgSuper () */
  5943. }
  5944.  
  5945. int
  5946. lang_report_error_function (decl)
  5947.       tree decl;
  5948. {
  5949.   if (method_context)
  5950.     {
  5951.       fprintf (stderr, "In method `%s'\n",
  5952.            IDENTIFIER_POINTER (METHOD_SEL_NAME (method_context)));
  5953.       return 1;
  5954.     }
  5955.   else
  5956.     return 0;
  5957. }
  5958.  
  5959. static int
  5960. is_complex_decl (type)
  5961.      tree type;
  5962. {
  5963.   return (TREE_CODE (type) == ARRAY_TYPE
  5964.       || TREE_CODE (type) == FUNCTION_TYPE
  5965.       || (TREE_CODE (type) == POINTER_TYPE
  5966.           && TYPE_MAIN_VARIANT (type) != id_type));
  5967. }
  5968.  
  5969.  
  5970. /* Code to convert a decl node into text for a declaration in C.  */
  5971.  
  5972. static char tmpbuf[256];
  5973.  
  5974. static void
  5975. adorn_decl (decl, str)
  5976.      tree decl;
  5977.      char *str;
  5978. {
  5979.   enum tree_code code = TREE_CODE (decl);
  5980.  
  5981.   if (code == ARRAY_REF)
  5982.     {
  5983.       tree anIntCst = TREE_OPERAND (decl, 1);
  5984.  
  5985.       if (anIntCst && TREE_CODE (anIntCst) == INTEGER_CST)
  5986.     sprintf (str + strlen (str), "[%d]", TREE_INT_CST_LOW (anIntCst));
  5987.       else
  5988.         strcat (str, "[]");
  5989.     }
  5990.   else if (code == ARRAY_TYPE)
  5991.     {
  5992.       tree anIntCst = TYPE_SIZE (decl);
  5993.       tree array_of = TREE_TYPE (decl);
  5994.  
  5995.       if (anIntCst && TREE_CODE (anIntCst) == INTEGER_CST)
  5996.     sprintf (str + strlen (str), "[%d]",
  5997.            TREE_INT_CST_LOW (anIntCst)/TREE_INT_CST_LOW (TYPE_SIZE (array_of)));
  5998.       else
  5999.         strcat (str, "[]");
  6000.     }
  6001.   else if (code == CALL_EXPR)
  6002.     {
  6003.       tree chain = TREE_PURPOSE (TREE_OPERAND (decl, 1));
  6004.  
  6005.       strcat (str, "(");
  6006.       while (chain)
  6007.     {
  6008.       gen_declaration (chain, str);
  6009.       chain = TREE_CHAIN (chain);
  6010.       if (chain)
  6011.         strcat (str, ", ");
  6012.     }
  6013.       strcat (str, ")");
  6014.     }
  6015.   else if (code == FUNCTION_TYPE)
  6016.     {
  6017.       tree chain  = TYPE_ARG_TYPES (decl); /* a list of types */
  6018.  
  6019.       strcat (str, "(");
  6020.       while (chain && TREE_VALUE (chain) != void_type_node)
  6021.     {
  6022.       gen_declaration (TREE_VALUE (chain), str);
  6023.       chain = TREE_CHAIN (chain);
  6024.       if (chain && TREE_VALUE (chain) != void_type_node)
  6025.         strcat (str, ", ");
  6026.     }
  6027.       strcat (str, ")");
  6028.     }
  6029.   else if (code == INDIRECT_REF)
  6030.     {
  6031.       strcpy (tmpbuf, "*");
  6032.       if (TREE_TYPE (decl) && TREE_CODE (TREE_TYPE (decl)) == TREE_LIST)
  6033.         {
  6034.       tree chain;
  6035.       
  6036.       for (chain = nreverse (copy_list (TREE_TYPE (decl)));
  6037.            chain;
  6038.            chain = TREE_CHAIN (chain))
  6039.         {
  6040.           if (TREE_CODE (TREE_VALUE (chain)) == IDENTIFIER_NODE)
  6041.         {
  6042.           strcat (tmpbuf, " ");
  6043.           strcat (tmpbuf, IDENTIFIER_POINTER (TREE_VALUE (chain)));
  6044.         }
  6045.         }
  6046.       if (str[0])
  6047.         strcat (tmpbuf, " ");
  6048.     }
  6049.       strcat (tmpbuf, str);
  6050.       strcpy (str, tmpbuf);
  6051.     }
  6052.   else if (code == POINTER_TYPE)
  6053.     {
  6054.       strcpy (tmpbuf, "*");
  6055.       if (TREE_READONLY (decl) || TYPE_VOLATILE (decl))
  6056.         {
  6057.       if (TREE_READONLY (decl))
  6058.         strcat (tmpbuf, " const");
  6059.       if (TYPE_VOLATILE (decl))
  6060.         strcat (tmpbuf, " volatile");
  6061.       if (str[0])
  6062.         strcat (tmpbuf, " ");
  6063.     }
  6064.       strcat (tmpbuf, str);
  6065.       strcpy (str, tmpbuf);
  6066.     }
  6067. }
  6068.  
  6069. static char *
  6070. gen_declarator (decl, buf, name)
  6071.      tree decl;
  6072.      char *buf;
  6073.      char *name;
  6074. {
  6075.   if (decl)
  6076.     {
  6077.       enum tree_code code = TREE_CODE (decl);
  6078.       char *str;
  6079.       tree op;
  6080.       int wrap = 0;
  6081.  
  6082.       switch (code)
  6083.     {
  6084.     case ARRAY_REF: case INDIRECT_REF: case CALL_EXPR:
  6085.       {
  6086.         op = TREE_OPERAND (decl, 0);
  6087.  
  6088.         /* we have a pointer to a function or array...(*)(), (*)[] */
  6089.         if ((code == ARRAY_REF || code == CALL_EXPR) &&
  6090.         (op && TREE_CODE (op) == INDIRECT_REF))
  6091.           wrap = 1;
  6092.  
  6093.         str = gen_declarator (op, buf, name);
  6094.  
  6095.         if (wrap)
  6096.           {
  6097.         strcpy (tmpbuf, "("); strcat (tmpbuf, str); strcat (tmpbuf, ")");
  6098.         strcpy (str, tmpbuf);
  6099.           }
  6100.  
  6101.         adorn_decl (decl, str);
  6102.         break;
  6103.       }
  6104.     case ARRAY_TYPE: case FUNCTION_TYPE: case POINTER_TYPE:
  6105.       {
  6106.         str = strcpy (buf, name);
  6107.  
  6108.         /* this clause is done iteratively...rather than recursively */
  6109.         do
  6110.           {
  6111.         op = is_complex_decl (TREE_TYPE (decl))
  6112.           ? TREE_TYPE (decl)
  6113.             : NULLT;
  6114.  
  6115.         adorn_decl (decl, str);
  6116.  
  6117.         /* we have a pointer to a function or array...(*)(), (*)[] */
  6118.         if ((code == POINTER_TYPE) &&
  6119.             (op && (TREE_CODE (op) == FUNCTION_TYPE
  6120.                 || TREE_CODE (op) == ARRAY_TYPE)))
  6121.           {
  6122.             strcpy (tmpbuf, "("); strcat (tmpbuf, str); strcat (tmpbuf, ")");
  6123.             strcpy (str, tmpbuf);
  6124.           }
  6125.  
  6126.         decl = is_complex_decl (TREE_TYPE (decl))
  6127.           ? TREE_TYPE (decl)
  6128.             : NULLT;
  6129.           }
  6130.         while (decl && (code = TREE_CODE (decl)));
  6131.  
  6132.         break;
  6133.       }
  6134.     case IDENTIFIER_NODE:
  6135.       /* will only happen if we are processing a "raw" expr-decl. */
  6136.       return strcpy (buf, IDENTIFIER_POINTER (decl));
  6137.     }
  6138.  
  6139.       return str;
  6140.     }
  6141.   else                /* we have an abstract declarator or a _DECL node */
  6142.     {
  6143.       return strcpy (buf, name);
  6144.     }
  6145. }
  6146.  
  6147. static void
  6148. gen_declspecs (declspecs, buf, raw)
  6149.      tree declspecs;
  6150.      char *buf;
  6151.      int raw;
  6152. {
  6153.   if (raw)
  6154.     {
  6155.       tree chain;
  6156.  
  6157.       for (chain = nreverse (copy_list (declspecs)); chain; chain = TREE_CHAIN (chain))
  6158.     {
  6159.       tree aspec = TREE_VALUE (chain);
  6160.  
  6161.       if (TREE_CODE (aspec) == IDENTIFIER_NODE)
  6162.         strcat (buf, IDENTIFIER_POINTER (aspec));
  6163.       else if (TREE_CODE (aspec) == RECORD_TYPE)
  6164.         {
  6165.           if (TYPE_NAME (aspec))
  6166.         {
  6167.           tree protocol_list = TYPE_PROTOCOL_LIST (aspec);
  6168.           
  6169.           if (!TREE_STATIC_TEMPLATE (aspec))
  6170.             strcat (buf, "struct ");
  6171.           strcat (buf, IDENTIFIER_POINTER (TYPE_NAME (aspec)));
  6172. /* NEW!!! */
  6173.           if (protocol_list)
  6174.             {
  6175.               tree chain = protocol_list;
  6176.               
  6177.               strcat (buf, " <");
  6178.               while (chain)
  6179.             {
  6180.               strcat (buf, IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (chain))));
  6181.               chain = TREE_CHAIN (chain);
  6182.               if (chain)
  6183.                 strcat (buf, ", ");
  6184.             }
  6185.               strcat (buf, ">");
  6186.             }
  6187.         }
  6188.           else
  6189.         strcat (buf, "untagged struct");
  6190.         }
  6191.       else if (TREE_CODE (aspec) == UNION_TYPE)
  6192.         {
  6193.           if (TYPE_NAME (aspec))
  6194.         {
  6195.           strcat (buf, "union ");
  6196.           strcat (buf, IDENTIFIER_POINTER (TYPE_NAME (aspec)));
  6197.         }
  6198.           else
  6199.         strcat (buf, "untagged union");
  6200.         }
  6201.       else if (TREE_CODE (aspec) == ENUMERAL_TYPE)
  6202.         {
  6203.           if (TYPE_NAME (aspec))
  6204.         {
  6205.           strcat (buf, "enum ");
  6206.           strcat (buf, IDENTIFIER_POINTER (TYPE_NAME (aspec)));
  6207.         }
  6208.           else
  6209.         strcat (buf, "untagged enum");
  6210.         }
  6211.       else if (TREE_CODE (aspec) == TYPE_DECL && DECL_NAME (aspec))
  6212.         {
  6213.           strcat (buf, IDENTIFIER_POINTER (DECL_NAME (aspec)));
  6214.         }
  6215. /* NEW!!! */
  6216.       else if (TREE_CODE (aspec) == POINTER_TYPE
  6217.            && TYPE_MAIN_VARIANT (aspec) == id_type)
  6218.         {
  6219.           tree protocol_list = TYPE_PROTOCOL_LIST (aspec);
  6220.           
  6221.           strcat (buf, "id");
  6222.           if (protocol_list)
  6223.         {
  6224.           tree chain = protocol_list;
  6225.           
  6226.           strcat (buf, " <");
  6227.           while (chain)
  6228.             {
  6229.               strcat (buf, IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (chain))));
  6230.               chain = TREE_CHAIN (chain);
  6231.               if (chain)
  6232.             strcat (buf, ", ");
  6233.             }
  6234.           strcat (buf, ">");
  6235.         }
  6236.         }
  6237.       if (TREE_CHAIN (chain))
  6238.         strcat (buf, " ");
  6239.     }
  6240.     }
  6241.   else
  6242.     {
  6243.     /* type qualifiers */
  6244.  
  6245.     if (TREE_READONLY (declspecs))
  6246.       strcat (buf, "const ");
  6247.     if (TYPE_VOLATILE (declspecs))
  6248.       strcat (buf, "volatile ");
  6249.  
  6250.     switch (TREE_CODE (declspecs))
  6251.       {
  6252.     /* type specifiers */
  6253.  
  6254.       case INTEGER_TYPE:    /* signed integer types */
  6255.  
  6256.     declspecs = TYPE_MAIN_VARIANT (declspecs);
  6257.  
  6258.         if (declspecs == short_integer_type_node) /* 's' */
  6259.           strcat (buf, "short");
  6260.         else if (declspecs == integer_type_node) /* 'i' */
  6261.           strcat (buf, "int");
  6262.         else if (declspecs == long_integer_type_node) /* 'l' */
  6263.           strcat (buf, "long");
  6264.         else if (declspecs == long_long_integer_type_node)
  6265.           strcat (buf, "long long");
  6266.         else if (declspecs == signed_char_type_node || /* 'c' */
  6267.                declspecs == char_type_node)
  6268.           strcat (buf, "char");
  6269.  
  6270.         /* unsigned integer types */
  6271.  
  6272.         else if (declspecs == short_unsigned_type_node)    /* 'S' */
  6273.           strcat (buf, "unsigned short");
  6274.         else if (declspecs == unsigned_type_node) /* 'I' */
  6275.           strcat (buf, "unsigned int");
  6276.         else if (declspecs == long_unsigned_type_node) /* 'L' */
  6277.           strcat (buf, "unsigned long");
  6278.         else if (declspecs == long_long_unsigned_type_node) /* 'L' */
  6279.           strcat (buf, "unsigned long long");
  6280.         else if (declspecs == unsigned_char_type_node) /* 'C' */
  6281.           strcat (buf, "unsigned char");
  6282.     break;
  6283.  
  6284.       case REAL_TYPE:        /* floating point types */
  6285.  
  6286.     declspecs = TYPE_MAIN_VARIANT (declspecs);
  6287.  
  6288.         if (declspecs == float_type_node) /* 'f' */
  6289.           strcat (buf, "float");
  6290.         else if (declspecs == double_type_node)    /* 'd' */
  6291.           strcat (buf, "double");
  6292.     else if (declspecs == long_double_type_node) /* 'd' */
  6293.           strcat (buf, "long double");
  6294.     break;
  6295.  
  6296.       case RECORD_TYPE:
  6297.     if (TYPE_NAME (declspecs) &&
  6298.         (TREE_CODE (TYPE_NAME (declspecs)) == IDENTIFIER_NODE))
  6299.       {
  6300.         tree protocol_list = TYPE_PROTOCOL_LIST (declspecs);
  6301.  
  6302.         if (!TREE_STATIC_TEMPLATE (declspecs))
  6303.           strcat (buf, "struct ");
  6304.         strcat (buf, IDENTIFIER_POINTER (TYPE_NAME (declspecs)));
  6305. /* NEW!!! */
  6306.         if (protocol_list)
  6307.           {
  6308.         tree chain = protocol_list;
  6309.         
  6310.         strcat (buf, " <");
  6311.         while (chain)
  6312.           {
  6313.             strcat (buf, IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (chain))));
  6314.             chain = TREE_CHAIN (chain);
  6315.             if (chain)
  6316.               strcat (buf, ", ");
  6317.           }
  6318.         strcat (buf, ">");
  6319.           }
  6320.       }
  6321.     else
  6322.       strcat (buf, "untagged struct");
  6323.     break;
  6324.       case UNION_TYPE:
  6325.     if (TYPE_NAME (declspecs) &&
  6326.         (TREE_CODE (TYPE_NAME (declspecs)) == IDENTIFIER_NODE))
  6327.       {
  6328.         strcat (buf, "union ");
  6329.         strcat (buf, IDENTIFIER_POINTER (TYPE_NAME (declspecs)));
  6330.       }
  6331.     else
  6332.       strcat (buf, "untagged union");
  6333.     break;
  6334.       case ENUMERAL_TYPE:
  6335.     if (TYPE_NAME (declspecs) &&
  6336.         (TREE_CODE (TYPE_NAME (declspecs)) == IDENTIFIER_NODE))
  6337.       {
  6338.         strcat (buf, "enum ");
  6339.         strcat (buf, IDENTIFIER_POINTER (TYPE_NAME (declspecs)));
  6340.       }
  6341.     else
  6342.       strcat (buf, "untagged enum");
  6343.     break;
  6344.       case VOID_TYPE:
  6345.     strcat (buf, "void");
  6346.     break;
  6347. /* NEW!!! */
  6348.       case POINTER_TYPE:
  6349.     {
  6350.       tree protocol_list = TYPE_PROTOCOL_LIST (declspecs);
  6351.       
  6352.       strcat (buf, "id");
  6353.       if (protocol_list)
  6354.         {
  6355.           tree chain = protocol_list;
  6356.           
  6357.           strcat (buf, " <");
  6358.           while (chain)
  6359.         {
  6360.           strcat (buf, IDENTIFIER_POINTER (PROTOCOL_NAME (TREE_VALUE (chain))));
  6361.           chain = TREE_CHAIN (chain);
  6362.           if (chain)
  6363.             strcat (buf, ", ");
  6364.         }
  6365.           strcat (buf, ">");
  6366.         }
  6367.     }
  6368.       }
  6369.     }
  6370. }
  6371.  
  6372. static char *
  6373. gen_declaration (atype_or_adecl, buf)
  6374.      tree atype_or_adecl;
  6375.      char *buf;
  6376. {
  6377.   char declbuf[256];
  6378.  
  6379.   if (TREE_CODE (atype_or_adecl) == TREE_LIST)
  6380.     {
  6381.       tree declspecs;        /* "identifier_node", "record_type" */
  6382.       tree declarator;        /* "array_ref", "indirect_ref", "call_expr"... */
  6383.  
  6384.       /* we have a "raw", abstract delclarator (typename) */
  6385.       declarator = TREE_VALUE (atype_or_adecl);
  6386.       declspecs  = TREE_PURPOSE (atype_or_adecl);
  6387.  
  6388.       gen_declspecs (declspecs, buf, 1);
  6389.       if (declarator)
  6390.         {
  6391.       strcat (buf, " ");
  6392.       strcat (buf, gen_declarator (declarator, declbuf, ""));
  6393.     }
  6394.     }
  6395.   else
  6396.     {
  6397.       tree atype;
  6398.       tree declspecs;    /* "integer_type", "real_type", "record_type"... */
  6399.       tree declarator;    /* "array_type", "function_type", "pointer_type". */
  6400.  
  6401.       if (TREE_CODE (atype_or_adecl) == FIELD_DECL
  6402.       || TREE_CODE (atype_or_adecl) == PARM_DECL
  6403.       || TREE_CODE (atype_or_adecl) == FUNCTION_DECL)
  6404.     atype = TREE_TYPE (atype_or_adecl);
  6405.       else
  6406.     atype = atype_or_adecl;    /* assume we have a *_type node */
  6407.  
  6408.       if (is_complex_decl (atype))
  6409.     {
  6410.       tree chain;
  6411.  
  6412.       /* get the declaration specifier...it is at the end of the list */
  6413.       declarator = chain = atype;
  6414.       do
  6415.         chain = TREE_TYPE (chain); /* not TREE_CHAIN (chain); */
  6416.       while (is_complex_decl (chain));
  6417.       declspecs = chain;
  6418.     }
  6419.       else
  6420.     {
  6421.       declspecs = atype;
  6422.       declarator = NULLT;
  6423.     }
  6424.  
  6425.       gen_declspecs (declspecs, buf, 0);
  6426.  
  6427.       if (TREE_CODE (atype_or_adecl) == FIELD_DECL
  6428.       || TREE_CODE (atype_or_adecl) == PARM_DECL
  6429.       || TREE_CODE (atype_or_adecl) == FUNCTION_DECL)
  6430.     {
  6431.       const char *decl_name;
  6432.       
  6433.       if (DECL_NAME (atype_or_adecl))
  6434.         decl_name = IDENTIFIER_POINTER (DECL_NAME (atype_or_adecl));
  6435.       else
  6436.         decl_name = "";
  6437.  
  6438.       if (declarator)
  6439.         {
  6440.           strcat (buf, " ");
  6441.           strcat (buf, gen_declarator (declarator, declbuf, decl_name));
  6442.         }
  6443.       else if (decl_name[0])
  6444.         {
  6445.           strcat (buf, " ");
  6446.           strcat (buf, decl_name);
  6447.         }
  6448.     }
  6449.       else if (declarator)
  6450.         {
  6451.       strcat (buf, " ");
  6452.       strcat (buf, gen_declarator (declarator, declbuf, ""));
  6453.     }
  6454.     }
  6455.   return buf;
  6456. }
  6457.  
  6458. #define RAW_TYPESPEC(meth) (TREE_VALUE (TREE_PURPOSE (TREE_TYPE (meth))))
  6459.  
  6460. static char *
  6461. gen_method_decl (method, buf)
  6462.      tree method;
  6463.      char *buf;
  6464. {
  6465.   tree chain;
  6466.  
  6467.   if (RAW_TYPESPEC (method) != objc_object_reference)
  6468.     {
  6469.       strcpy (buf, "(");
  6470.       gen_declaration (TREE_TYPE (method), buf);
  6471.       strcat (buf, ")");
  6472.     }
  6473.  
  6474.   chain = METHOD_SEL_ARGS (method);
  6475.   if (chain)
  6476.     {                /* we have a chain of keyword_decls */
  6477.       do
  6478.         {
  6479.       if (KEYWORD_KEY_NAME (chain))
  6480.         strcat (buf, IDENTIFIER_POINTER (KEYWORD_KEY_NAME (chain)));
  6481.  
  6482.       strcat (buf, ":");
  6483.       if (RAW_TYPESPEC (chain) != objc_object_reference)
  6484.         {
  6485.           strcat (buf, "(");
  6486.           gen_declaration (TREE_TYPE (chain), buf);
  6487.           strcat (buf, ")");
  6488.         }
  6489.       strcat (buf, IDENTIFIER_POINTER (KEYWORD_ARG_NAME (chain)));
  6490.       if (chain = TREE_CHAIN (chain))
  6491.         strcat (buf, " ");
  6492.         }
  6493.       while (chain);
  6494.  
  6495.       if (METHOD_ADD_ARGS (method) == (tree)1)
  6496.         strcat (buf, ", ...");
  6497.       else if (METHOD_ADD_ARGS (method))
  6498.         { 
  6499.       /* We have a tree list node as generate by get_parm_info ().  */
  6500.       chain  = TREE_PURPOSE (METHOD_ADD_ARGS (method));
  6501.           /* know we have a chain of parm_decls */
  6502.           while (chain)
  6503.             {
  6504.           strcat (buf, ", ");
  6505.           gen_declaration (chain, buf);
  6506.           chain = TREE_CHAIN (chain);
  6507.             }
  6508.     }
  6509.     }
  6510.   else                /* we have a unary selector */
  6511.     {
  6512.       strcat (buf, IDENTIFIER_POINTER (METHOD_SEL_NAME (method)));
  6513.     }
  6514.  
  6515.   return buf;
  6516. }
  6517.  
  6518. void
  6519. gen_prototype (fp, decl)
  6520.      FILE *fp;
  6521.      tree decl;
  6522. {
  6523.   /* we have a function definition - generate prototype */
  6524.   bzero (errbuf, BUFSIZE);
  6525.   gen_declaration (decl, errbuf);
  6526.   fprintf (fp, "%s;\n", errbuf);
  6527. }
  6528. /*
  6529.  *  debug info...
  6530.  */
  6531. static void
  6532. dump_interface (fp, chain)
  6533.      FILE *fp;
  6534.      tree chain;
  6535. {
  6536.   char *buf = (char *)xmalloc (256);
  6537.   char *my_name = IDENTIFIER_POINTER (CLASS_NAME (chain));
  6538.   tree ivar_decls = CLASS_RAW_IVARS (chain);
  6539.   tree nst_methods = CLASS_NST_METHODS (chain);
  6540.   tree cls_methods = CLASS_CLS_METHODS (chain);
  6541.  
  6542.   fprintf (fp, "\n@interface %s", my_name);
  6543.  
  6544.   if (CLASS_SUPER_NAME (chain))
  6545.     {
  6546.       char *super_name = IDENTIFIER_POINTER (CLASS_SUPER_NAME (chain));
  6547.       fprintf (fp, " : %s\n", super_name);
  6548.     }
  6549.   else
  6550.     fprintf (fp, "\n");
  6551.  
  6552.   if (ivar_decls)
  6553.     {
  6554.       fprintf (fp, "{\n");
  6555.       do
  6556.     {
  6557.       bzero (buf, 256);
  6558.       fprintf (fp, "\t%s;\n", gen_declaration (ivar_decls, buf));
  6559.       ivar_decls = TREE_CHAIN (ivar_decls);
  6560.     }
  6561.       while (ivar_decls);
  6562.       fprintf (fp, "}\n");
  6563.     }
  6564.  
  6565.   while (nst_methods)
  6566.     {
  6567.       bzero (buf, 256);
  6568.       fprintf (fp, "- %s;\n", gen_method_decl (nst_methods, buf));
  6569.       nst_methods = TREE_CHAIN (nst_methods);
  6570.     }
  6571.  
  6572.   while (cls_methods)
  6573.     {
  6574.       bzero (buf, 256);
  6575.       fprintf (fp, "+ %s;\n", gen_method_decl (cls_methods, buf));
  6576.       cls_methods = TREE_CHAIN (cls_methods);
  6577.     }
  6578.   fprintf (fp, "\n@end");
  6579. }
  6580.  
  6581. void
  6582. init_objc ()
  6583. {
  6584.   /* Add the special tree codes of Objective C to the tables.  */
  6585.  
  6586.   tree_code_type
  6587.     = (char **) realloc (tree_code_type,
  6588.              sizeof (char *) * LAST_OBJC_TREE_CODE);
  6589.   tree_code_length
  6590.     = (int *) realloc (tree_code_length,
  6591.                sizeof (int) * LAST_OBJC_TREE_CODE);
  6592.   tree_code_name
  6593.     = (char **) realloc (tree_code_name,
  6594.              sizeof (char *) * LAST_OBJC_TREE_CODE);
  6595.   bcopy (objc_tree_code_type,
  6596.      tree_code_type + (int) FIRST_OBJC_TREE_CODE,
  6597.      (((int) LAST_OBJC_TREE_CODE - (int) FIRST_OBJC_TREE_CODE)
  6598.       * sizeof (char *)));
  6599.   bcopy (objc_tree_code_length,
  6600.      tree_code_length + (int) FIRST_OBJC_TREE_CODE,
  6601.      (((int) LAST_OBJC_TREE_CODE - (int) FIRST_OBJC_TREE_CODE)
  6602.       * sizeof (int)));
  6603.   bcopy (objc_tree_code_name,
  6604.      tree_code_name + (int) FIRST_OBJC_TREE_CODE,
  6605.      (((int) LAST_OBJC_TREE_CODE - (int) FIRST_OBJC_TREE_CODE)
  6606.       * sizeof (char *)));
  6607.  
  6608.   errbuf = (char *)xmalloc (BUFSIZE);
  6609.   utlbuf = (char *)xmalloc (BUFSIZE);
  6610.   hash_init ();
  6611.   synth_module_prologue ();
  6612. }
  6613.  
  6614. void
  6615. finish_objc ()
  6616. {
  6617.   struct imp_entry *impent;
  6618.   tree chain;
  6619.  
  6620.   if (implementation_context || class_names_chain ||
  6621.       meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
  6622.     generate_objc_symtab_decl ();
  6623.  
  6624.   for (impent = imp_list; impent; impent = impent->next)
  6625.     {
  6626.       implementation_context = impent->imp_context;
  6627.       implementation_template = impent->imp_template;
  6628.  
  6629.       _OBJC_CLASS_decl = impent->class_decl;
  6630.       _OBJC_METACLASS_decl = impent->meta_decl;
  6631.  
  6632.       if (TREE_CODE (implementation_context) == CLASS_IMPLEMENTATION)
  6633.     {
  6634.       /* all of the following reference the string pool...  */
  6635.       generate_ivar_lists ();
  6636.       generate_dispatch_tables ();
  6637.       generate_shared_structures ();
  6638.     }
  6639.       else
  6640.     {
  6641.       generate_dispatch_tables ();
  6642.       generate_category (implementation_context);
  6643.     }
  6644.     }
  6645.  
  6646.   if (sel_ref_chain)
  6647.     build_selector_translation_table ();
  6648.  
  6649.   if (protocol_chain) 
  6650.     generate_protocols ();
  6651.  
  6652.   if (implementation_context || class_names_chain ||
  6653.       meth_var_names_chain || meth_var_types_chain || sel_ref_chain)
  6654.     {
  6655.       build_module_descriptor ();
  6656.     }
  6657.  
  6658.   /* dump the string table last */
  6659.  
  6660.   generate_strings ();
  6661.  
  6662.   /* dump the class references...this forces the appropriate classes
  6663.      to be linked into the executable image, preserving unix archive
  6664.      semantics...this can be removed when we move to a more dynamically
  6665.      linked environment
  6666.      */
  6667.   for (chain = cls_ref_chain; chain; chain = TREE_CHAIN (chain))
  6668.     {
  6669.       sprintf (utlbuf, ".reference .objc_class_name_%s",
  6670.            IDENTIFIER_POINTER (TREE_VALUE (chain)));
  6671.       assemble_asm (my_build_string (strlen (utlbuf) + 1, utlbuf));
  6672.     }
  6673.   for (impent = imp_list; impent; impent = impent->next)
  6674.     {
  6675.       implementation_context = impent->imp_context;
  6676.       implementation_template = impent->imp_template;
  6677.  
  6678.       if (TREE_CODE (impent->imp_context) == CLASS_IMPLEMENTATION)
  6679.     {
  6680.       sprintf (utlbuf, ".objc_class_name_%s=0",
  6681.            IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context)));
  6682.       assemble_asm (my_build_string (strlen (utlbuf) + 1, utlbuf));
  6683.  
  6684.       sprintf (utlbuf, ".globl .objc_class_name_%s",
  6685.            IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context)));
  6686.       assemble_asm (my_build_string (strlen (utlbuf) + 1, utlbuf));
  6687.     }
  6688.       else if (TREE_CODE (impent->imp_context) == CATEGORY_IMPLEMENTATION)
  6689.     {
  6690.       /* Do the same for categories.  Even though no references to these
  6691.           symbols are generated automatically by the compiler, it gives
  6692.           you a handle to pull them into an archive by hand. */
  6693.       sprintf (utlbuf, ".objc_category_name_%s_%s=0",
  6694.            IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context)),
  6695.            IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent->imp_context)));
  6696.       assemble_asm (my_build_string (strlen (utlbuf) + 1, utlbuf));
  6697.  
  6698.       sprintf (utlbuf, ".globl .objc_category_name_%s_%s",
  6699.            IDENTIFIER_POINTER (CLASS_NAME (impent->imp_context)),
  6700.            IDENTIFIER_POINTER (CLASS_SUPER_NAME (impent->imp_context)));
  6701.       assemble_asm (my_build_string (strlen (utlbuf) + 1, utlbuf));
  6702.     }
  6703.     }
  6704.   /*** this fixes a gross bug in the assembler...it `expects' #APP to have
  6705.    *** a matching #NO_APP, or it crashes (sometimes). app_disable () will
  6706.    *** insure this is the case. 5/19/89, s.naroff.
  6707.    ***/
  6708.   if (cls_ref_chain || imp_list)
  6709.     app_disable ();
  6710.  
  6711.   if (flag_gen_declaration)
  6712.     {
  6713.       add_class (implementation_context);
  6714.       dump_interface (gen_declaration_file, implementation_context);
  6715.     }
  6716.   if (warn_selector)
  6717.     {
  6718.       int slot;
  6719.       
  6720.       /* Run through the selector hash tables and print a warning for any
  6721.          selector which has multiple methods. */
  6722.       
  6723.       for (slot = 0; slot < SIZEHASHTABLE; slot++)
  6724.         {
  6725.       hash hsh;
  6726.       
  6727.       for (hsh = cls_method_hash_list[slot]; hsh; hsh = hsh->next)
  6728.         {
  6729.           if (hsh->list)
  6730.             {
  6731.               tree meth = hsh->key;
  6732.               char type = (TREE_CODE (meth) == INSTANCE_METHOD_DECL)
  6733.                   ? '-' : '+';
  6734.               attr loop;
  6735.           
  6736.           warning ("potential selector conflict for method `%s'",
  6737.                IDENTIFIER_POINTER (METHOD_SEL_NAME (meth)));
  6738.               warn_with_method ("found", type, meth);
  6739.               for (loop = hsh->list; loop; loop = loop->next)
  6740.                 warn_with_method ("found", type, loop->value);
  6741.             }
  6742.         }
  6743.     }
  6744.     
  6745.       for (slot = 0; slot < SIZEHASHTABLE; slot++)
  6746.         {
  6747.       hash hsh;
  6748.       
  6749.       for (hsh = nst_method_hash_list[slot]; hsh; hsh = hsh->next)
  6750.         {
  6751.           if (hsh->list)
  6752.             {
  6753.               tree meth = hsh->key;
  6754.               char type = (TREE_CODE (meth) == INSTANCE_METHOD_DECL)
  6755.                   ? '-' : '+';
  6756.               attr loop;
  6757.           
  6758.           warning ("potential selector conflict for method `%s'",
  6759.                IDENTIFIER_POINTER (METHOD_SEL_NAME (meth)));
  6760.               warn_with_method ("found", type, meth);
  6761.               for (loop = hsh->list; loop; loop = loop->next)
  6762.                 warn_with_method ("found", type, loop->value);
  6763.             }
  6764.         }
  6765.     }
  6766.     }
  6767. }
  6768.  
  6769. #ifdef DEBUG
  6770.  
  6771. static void
  6772. objc_debug (fp)
  6773.      FILE *fp;
  6774. {
  6775.   char *buf = (char *)xmalloc (256);
  6776.  
  6777.   {                /* dump function prototypes */
  6778.     tree loop = _OBJC_MODULES_decl;
  6779.  
  6780.     fprintf (fp, "\n\nfunction prototypes:\n");
  6781.     while (loop)
  6782.       {
  6783.     if (TREE_CODE (loop) == FUNCTION_DECL && DECL_INITIAL (loop))
  6784.       {
  6785.         /* we have a function definition - generate prototype */
  6786.             bzero (errbuf, BUFSIZE);
  6787.         gen_declaration (loop, errbuf);
  6788.         fprintf (fp, "%s;\n", errbuf);
  6789.       }
  6790.     loop = TREE_CHAIN (loop);
  6791.       }
  6792.   }
  6793.   {                /* dump global chains */
  6794.     tree loop;
  6795.     int i, index = 0, offset = 0;
  6796.     hash hashlist;
  6797.  
  6798.     for (i = 0; i < SIZEHASHTABLE; i++)
  6799.       {
  6800.     if (hashlist = nst_method_hash_list[i])
  6801.       {
  6802.         fprintf (fp, "\n\nnst_method_hash_list[%d]:\n", i);
  6803.         do
  6804.           {
  6805.         bzero (buf, 256);
  6806.         fprintf (fp, "-%s;\n", gen_method_decl (hashlist->key, buf));
  6807.         hashlist = hashlist->next;
  6808.           }
  6809.         while (hashlist);
  6810.       }
  6811.       }
  6812.     for (i = 0; i < SIZEHASHTABLE; i++)
  6813.       {
  6814.     if (hashlist = cls_method_hash_list[i])
  6815.       {
  6816.         fprintf (fp, "\n\ncls_method_hash_list[%d]:\n", i);
  6817.         do
  6818.           {
  6819.         bzero (buf, 256);
  6820.         fprintf (fp, "-%s;\n", gen_method_decl (hashlist->key, buf));
  6821.         hashlist = hashlist->next;
  6822.           }
  6823.         while (hashlist);
  6824.       }
  6825.       }
  6826.     fprintf (fp, "\nsel_refdef_chain:\n");
  6827.     for (loop = sel_refdef_chain; loop; loop = TREE_CHAIN (loop))
  6828.       {
  6829.     fprintf (fp, "(index: %4d offset: %4d) %s\n", index, offset,
  6830.          IDENTIFIER_POINTER (TREE_VALUE (loop)));
  6831.     index++;
  6832.     /* add one for the '\0' character */
  6833.     offset += IDENTIFIER_LENGTH (TREE_VALUE (loop)) + 1;
  6834.       }
  6835.     fprintf (fp, "\n (max_selector_index: %4d.\n", max_selector_index);
  6836.   }
  6837. }
  6838. #endif
  6839.  
  6840. void
  6841. print_lang_statistics ()
  6842. {
  6843. }
  6844.