home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / cplus-except.c < prev    next >
C/C++ Source or Header  |  1991-06-03  |  33KB  |  1,056 lines

  1. /* Handle exceptional things in C++.
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@cygnus.com)
  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. /* High-level class interface. */
  23.  
  24. #define NULL 0
  25. #define EXCEPTION_NAME_PREFIX "__exception_"
  26.  
  27. #include "config.h"
  28. #include "tree.h"
  29. #include "cplus-tree.h"
  30. #include "flags.h"
  31. #include "assert.h"
  32. /* On Suns this can get you to the right definition if you
  33.    set the right value for TARGET.  */
  34. #include <setjmp.h>
  35. #ifdef sequent
  36. /* Can you believe they forgot this?  */
  37. #define _JBLEN 11
  38. #endif
  39.  
  40. #ifndef _JBLEN
  41. #define _JBLEN (sizeof(jmp_buf)/sizeof(int))
  42. #endif
  43.  
  44. void init_exception_processing ();
  45. void init_exception_processing_1 ();
  46.  
  47. /* If non-zero, a VAR_DECL whose cleanup will cause a throw to the
  48.    next exception handler.  Its value says whether to throw or not.
  49.    In the case of functions which do not issue a RAISE, it should be
  50.    possible to optimize away this VAR_DECL (and overhead associated
  51.    with it).  */
  52. tree exception_throw_decl;
  53. /* Use this to know that we did not set `exception_throw_decl',
  54.    until GCC optimizer is smart enough to figure it out for itself.  */
  55. int sets_exception_throw_decl;
  56.  
  57. /* The exception `type' currently in scope, or NULL_TREE if none.  */
  58. tree current_exception_type;
  59.  
  60. /* The exception handler object for the given scope.  */
  61. tree current_exception_decl;
  62.  
  63. /* The ``object'' view of the current exception parameters.
  64.    We cast up from the `parms' field to `current_exception_type'.  */
  65. tree current_exception_object;
  66.  
  67. /* Cache `setjmp', `longjmp', `raise_exception', and `unhandled_exception'
  68.    after default conversion.  Maybe later they will get built-in.  */
  69. static tree BISJ, BILJ, BIR, BIUE;
  70.  
  71. /* Local variables which give the appearance that exception
  72.    handling is part of the language and the execution model.  */
  73.  
  74. /* The type of the exception handler stack.  */
  75. static tree EHS_type;
  76.  
  77. /* The global handler stack.  */
  78. tree EHS_decl;
  79.  
  80. /* Cached component refs to fields of `EHS_decl'.  */
  81. static tree EHS_prev, EHS_handler, EHS_parms, EHS_name;
  82.  
  83. /* The parameter names of this exception type.  */
  84.  
  85. static tree last_exception_fields;
  86. static tree last_exception_field_types;
  87.  
  88. /* When ID is VOID_TYPE_NODE, it means ``raise all''.
  89.    Cannot be inline, since it uses `alloca', and that
  90.    breaks code which pushes the result of this function
  91.    on the stack.  */
  92. static tree
  93. exception_object_name (prefix, id)
  94.      tree prefix;
  95.      tree id;
  96. {
  97.   /* First, cons up the `name' of this exception.  */
  98.   char *name;
  99.   int length = (id == void_type_node ? 3 : IDENTIFIER_LENGTH (id)) + EXCEPTION_NAME_LENGTH;
  100.  
  101.   if (prefix)
  102.     length += IDENTIFIER_LENGTH (prefix) + 2;
  103.  
  104.   name = (char *)alloca (length);
  105.   strcpy (name, EXCEPTION_NAME_PREFIX);
  106.   length = EXCEPTION_NAME_LENGTH;
  107.   if (prefix)
  108.     {
  109.       strcpy (name + length, IDENTIFIER_POINTER (prefix));
  110.       name[length + IDENTIFIER_LENGTH (prefix)] = JOINER;
  111.       length += IDENTIFIER_LENGTH (prefix) + 1;
  112.     }
  113.   if (id == void_type_node)
  114.     strcpy (name + length, "all");
  115.   else
  116.     strcpy (name + length, IDENTIFIER_POINTER (id));
  117.   return get_identifier (name);
  118. }
  119.  
  120. tree
  121. lookup_exception_cname (ctype, cname, raise_id)
  122.      tree ctype, cname;
  123.      tree raise_id;
  124. {
  125.   tree this_cname = TREE_PURPOSE (raise_id);
  126.   if (this_cname == NULL_TREE)
  127.     {
  128.       if (cname)
  129.     {
  130.       tree name = TREE_VALUE (raise_id);
  131.       if (purpose_member (name, CLASSTYPE_TAGS (ctype)))
  132.         this_cname = cname;
  133.     }
  134.     }
  135.   else if (this_cname == void_type_node)
  136.     this_cname = NULL_TREE;
  137.   else if (TREE_CODE (this_cname) != IDENTIFIER_NODE)
  138.     {
  139.       sorry ("multiple scope refs in `cplus_expand_raise_stmt'");
  140.       this_cname = error_mark_node;
  141.     }
  142.   return this_cname;
  143. }
  144.  
  145. tree
  146. lookup_exception_tname (oname)
  147.      tree oname;
  148. {
  149.   return get_identifier (IDENTIFIER_POINTER (oname) + EXCEPTION_NAME_LENGTH);
  150. }
  151.  
  152. tree
  153. lookup_exception_object (cname, name, complain)
  154.      tree cname, name;
  155.      int complain;
  156. {
  157.   tree oname;
  158.   tree decl;
  159.  
  160.   if (cname == void_type_node)
  161.     cname = NULL_TREE;
  162.   else if (cname && TREE_CODE (cname) != IDENTIFIER_NODE)
  163.     {
  164.       sorry ("multiple scope refs in `lookup_exception_object'");
  165.       cname = NULL_TREE;
  166.     }
  167.   oname = exception_object_name (cname, name);
  168.   decl = IDENTIFIER_GLOBAL_VALUE (oname);
  169.   if (decl == NULL_TREE || TREE_CODE (decl) != VAR_DECL)
  170.     {
  171.       if (complain)
  172.     {
  173.       int temp = allocation_temporary_p ();
  174.       if (cname)
  175.         error ("no exception name object for name `%s::%s'",
  176.            IDENTIFIER_POINTER (cname),
  177.            IDENTIFIER_POINTER (name));
  178.       else
  179.         error ("no exception name object for name `%s'",
  180.            IDENTIFIER_POINTER (name));
  181.       if (temp)
  182.         end_temporary_allocation ();
  183.       /* Avoid further error messages.  */
  184.       pushdecl_top_level (build_lang_field_decl (VAR_DECL,
  185.                              exception_object_name (cname, name),
  186.                              error_mark_node));
  187.       if (temp)
  188.         resume_temporary_allocation ();
  189.     }
  190.       return NULL_TREE;
  191.     }
  192.   return decl;
  193. }
  194.  
  195. tree
  196. lookup_exception_type (ctype, cname, raise_id)
  197.      tree ctype, cname;
  198.      tree raise_id;
  199. {
  200.   tree name = TREE_VALUE (raise_id);
  201.   tree purpose = TREE_PURPOSE (raise_id);
  202.  
  203.   if (cname && purpose == NULL_TREE)
  204.     purpose = cname;
  205.  
  206.   if (purpose && purpose != void_type_node)
  207.     {
  208.       tree assoc = NULL_TREE;
  209.  
  210.       if (TREE_CODE (purpose) != IDENTIFIER_NODE)
  211.     {
  212.       sorry ("multiple scope refs in `lookup_exception_type'");
  213.       TREE_PURPOSE (raise_id) = NULL_TREE;
  214.       return NULL_TREE;
  215.     }
  216.       if (! is_aggr_typedef (purpose, 1))
  217.     return NULL_TREE;
  218.       ctype = IDENTIFIER_TYPE_VALUE (purpose);
  219.       assoc = purpose_member (name, CLASSTYPE_TAGS (ctype));
  220.       if (assoc)
  221.     return TREE_VALUE (assoc);
  222.     }
  223.  
  224.   ctype = lookup_name (name);
  225.   if (ctype && TREE_CODE (ctype) == TYPE_DECL)
  226.     ctype = TREE_TYPE (ctype);
  227.   if (ctype && TREE_CODE (ctype) == RECORD_TYPE
  228.       && CLASSTYPE_DECLARED_EXCEPTION (ctype))
  229.     return ctype;
  230.   return NULL_TREE;
  231. }
  232.  
  233. tree
  234. finish_exception (e, list_of_fieldlists)
  235.      tree e;
  236.      tree list_of_fieldlists;
  237. {
  238.   tree parmtypes = NULL_TREE, name_field;
  239.   tree cname = TYPE_NAME (e);
  240.  
  241.   if (TREE_CODE (cname) == TYPE_DECL)
  242.     cname = DECL_NAME (cname);
  243.  
  244.   if (last_exception_fields)
  245.     error ("cannot declare exceptions within exceptions");
  246.   if (list_of_fieldlists && ! ANON_AGGRNAME_P (cname))
  247.     error_with_aggr_type (e, "exception name `%s' must follow body declaration");
  248.   if (list_of_fieldlists)
  249.     {
  250.       tree prev, field;
  251.  
  252.       /* Note: no public, private, or protected allowed.  */
  253.       if (TREE_CHAIN (list_of_fieldlists))
  254.     error ("visibility declarations invalid in exception declaration");
  255.       else if (TREE_PURPOSE (list_of_fieldlists) != (tree)visibility_default)
  256.     error ("visibility declarations invalid in exception declaration");
  257.       TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_default;
  258.  
  259.       /* Note also: no member function declarations allowed.  */
  260.       for (prev = 0, field = TREE_VALUE (list_of_fieldlists);
  261.        field; prev = field, field = TREE_CHAIN (field))
  262.     {
  263.       switch (TREE_CODE (field))
  264.         {
  265.         case FIELD_DECL:
  266.           /* ok.  */
  267.           parmtypes = tree_cons (NULL_TREE, TREE_TYPE (field), parmtypes);
  268.           continue;
  269.         case FUNCTION_DECL:
  270.           error_with_decl (field, "declaration of function `%s' in exception invalid");
  271.           break;
  272.         case VAR_DECL:
  273.           if (TREE_STATIC (field))
  274.         error_with_decl (field, "declaration of static variable `%s' in exception invalid");
  275.           else
  276.         error_with_decl (field, "declaration of constant field `%s' in exception invalid");
  277.           break;
  278.         case CONST_DECL:
  279.           error_with_decl (field, "declaration of enum value `%s' in exception invalid");
  280.           break;
  281.         case SCOPE_REF:
  282.           error ("use of `::' in exception context invalid");
  283.           break;
  284.         }
  285.       if (prev)
  286.         TREE_CHAIN (prev) = TREE_CHAIN (field);
  287.       else
  288.         TREE_VALUE (list_of_fieldlists) = TREE_CHAIN (field);
  289.     }
  290.     }
  291.  
  292.   /* Now that we've cleaned up the fields, add a name identifier at front.  */
  293.   name_field = build_lang_field_decl (FIELD_DECL, get_identifier ("__name"),
  294.                       ptr_type_node);
  295.   if (list_of_fieldlists)
  296.     {
  297.       TREE_CHAIN (name_field) = TREE_VALUE (list_of_fieldlists);
  298.       TREE_VALUE (list_of_fieldlists) = name_field;
  299.     }
  300.   else
  301.     list_of_fieldlists = build_tree_list (NULL_TREE, name_field);
  302.  
  303.   last_exception_fields = TREE_VALUE (list_of_fieldlists);
  304.   if (parmtypes)
  305.     {
  306.       last_exception_field_types = nreverse (parmtypes);
  307.       /* Set the TREE_CHAIN of what is now at the end of the
  308.      list to `void_list_node'.  */
  309.       TREE_CHAIN (parmtypes) = void_list_node;
  310.     }
  311.   else
  312.     last_exception_field_types = void_list_node;
  313.  
  314.   popclass (0);
  315.  
  316. #if 0
  317.   /* Remove aggregate types from the list of tags,
  318.      since these appear at global scope.  */
  319.   while (x && IS_AGGR_TYPE (TREE_VALUE (x)))
  320.     x = TREE_CHAIN (x);
  321.   CLASSTYPE_TAGS (t) = x;
  322.   y = x;
  323.   while (x)
  324.     {
  325.       if (IS_AGGR_TYPE (TREE_VALUE (x)))
  326.     TREE_CHAIN (y) = TREE_CHAIN (x);
  327.       x = TREE_CHAIN (x);
  328.     }
  329. #endif
  330.  
  331.   if (flag_cadillac)
  332.     cadillac_finish_exception (e);
  333.  
  334.   return e;
  335. }
  336.  
  337. void
  338. finish_exception_decl (cname, decl)
  339.      tree cname, decl;
  340. {
  341.   /* In cplus-decl.h.  */
  342.   extern tree last_function_parms;
  343.  
  344.   /* An exception declaration.  */
  345.   tree t, ctor;
  346.   tree parmdecls = NULL_TREE, fields;
  347.   tree list_of_fieldlists = temp_tree_cons (NULL_TREE,
  348.                         copy_list (last_exception_fields),
  349.                         NULL_TREE);
  350.   tree edecl = build_lang_field_decl (VAR_DECL,
  351.                       exception_object_name (cname, DECL_NAME (decl)),
  352.                       ptr_type_node);
  353.  
  354.   DECL_LANGUAGE (edecl) = lang_c;
  355.   TREE_STATIC (edecl) = 1;
  356.   TREE_PUBLIC (edecl) = 1;
  357.   finish_decl (pushdecl (edecl), 0, 0);
  358.  
  359.   /* Now instantiate the exception decl.  */
  360.   t = xref_tag (exception_type_node, DECL_NAME (decl), NULL_TREE);
  361.  
  362.   /* finish_struct will pop this.  */
  363.   pushclass (t, 0);
  364.  
  365.   /* Now add a constructor which takes as parameters all the types we
  366.      just defined.  */
  367.   ctor = build_lang_decl (FUNCTION_DECL, DECL_NAME (decl),
  368.               build_cplus_method_type (t, TYPE_POINTER_TO (t),
  369.                            last_exception_field_types));
  370.   /* Don't take `name'.  The constructor handles that.  */
  371.   fields = TREE_CHAIN (TREE_VALUE (list_of_fieldlists));
  372.   while (fields)
  373.     {
  374.       tree parm = build_decl (PARM_DECL, DECL_NAME (fields), TREE_TYPE (fields));
  375.       /* Since there is a prototype, args are passed in their own types.  */
  376.       DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
  377. #ifdef PROMOTE_PROTOTYPES
  378.       if (TREE_CODE (TREE_TYPE (fields)) == INTEGER_TYPE
  379.       && TYPE_PRECISION (TREE_TYPE (fields)) < TYPE_PRECISION (integer_type_node))
  380.     DECL_ARG_TYPE (parm) = integer_type_node;
  381. #endif
  382.       TREE_CHAIN (parm) = parmdecls;
  383.       parmdecls = parm;
  384.       fields = TREE_CHAIN (fields);
  385.     }
  386.   fields = TREE_VALUE (list_of_fieldlists);
  387.   last_function_parms = nreverse (parmdecls);
  388.  
  389.   DECL_CONSTRUCTOR_P (ctor) = 1;
  390.   TYPE_HAS_CONSTRUCTOR (t) = 1;
  391.   grokclassfn (t, DECL_NAME (decl), ctor, NO_SPECIAL, 0, NULL_TREE);
  392.   TREE_EXTERNAL (ctor) = 1;
  393.   TREE_STATIC (ctor) = 1;
  394.   TREE_PUBLIC (ctor) = 0;
  395.   TREE_INLINE (ctor) = 1;
  396.   make_decl_rtl (ctor, 0, 1);
  397.   finish_decl (ctor, NULL_TREE, 0);
  398.   TREE_CHAIN (ctor) = TREE_VALUE (list_of_fieldlists);
  399.   TREE_VALUE (list_of_fieldlists) = ctor;
  400.  
  401.   finish_struct (t, list_of_fieldlists, 0, 0);
  402.  
  403.   if (current_function_decl)
  404.     error ("cannot define exception inside function scope");
  405.   else
  406.     {
  407.       enum debugger old_write_symbols = write_symbols;
  408.       write_symbols = NO_DEBUG;
  409.  
  410.       /* Now build the constructor for this exception.  */
  411.       parmdecls = DECL_ARGUMENTS (ctor);
  412.       start_function (NULL_TREE, ctor, 0, 1);
  413.       store_parm_decls ();
  414.       pushlevel (0);
  415.       clear_last_expr ();
  416.       push_momentary ();
  417.       expand_start_bindings (0);
  418.  
  419.       /* Move all the parameters to the fields, skipping `this'.  */
  420.       parmdecls = TREE_CHAIN (parmdecls);
  421.       /* Install `name' of this exception handler.  */
  422.       DECL_INITIAL (fields) = build_unary_op (ADDR_EXPR, edecl, 0);
  423.       fields = TREE_CHAIN (fields);
  424.       /* Install all the values.  */
  425.       while (fields)
  426.     {
  427.       /* Set up the initialization for this field.  */
  428.       DECL_INITIAL (fields) = parmdecls;
  429.       fields = TREE_CHAIN (fields);
  430.       parmdecls = TREE_CHAIN (parmdecls);
  431.     }
  432.       emit_base_init (t, 0);
  433.  
  434.       finish_function (DECL_SOURCE_LINE (ctor), 1);
  435.       write_symbols = old_write_symbols;
  436.     }
  437. }
  438.  
  439. void
  440. end_exception_decls ()
  441. {
  442.   last_exception_field_types = NULL_TREE;
  443.   last_exception_fields = NULL_TREE;
  444. }
  445.  
  446. /* Statement-level exception semantics.  */
  447.  
  448. void
  449. cplus_expand_start_try (implicit)
  450.      int implicit;
  451. {
  452.   tree call_to_setjmp;
  453.   tree handler, ref;
  454.  
  455.   /* Start a new block enclosing the whole handler.  */
  456.   if (implicit)
  457.     {
  458.       pushlevel_temporary (1);
  459.     }
  460.   else
  461.     {
  462.       pushlevel (0);
  463.       clear_last_expr ();
  464.       push_momentary ();
  465.  
  466.       /* Encompass whole exception handler in one big binding contour.
  467.      If RAISE should throw out of the whole TRY/EXCEPT block, call
  468.      `expand_start_bindings' with argument of 1.  */
  469.       expand_start_bindings (0);
  470.     }
  471.  
  472.   /* Allocate handler in that block.  It's real name will come later.
  473.      Note that it will be the first name in this binding contour.  */
  474.   handler = get_temp_name (EHS_type, 0);
  475.   DECL_INITIAL (handler) = error_mark_node;
  476.   finish_decl (handler, NULL_TREE, 0);
  477.  
  478.   /* Must come after call to `finish_decl', else the cleanup for the temp
  479.      for the handler will cause the contour we just created to be popped.  */
  480.   if (implicit)
  481.     declare_implicit_exception ();
  482.  
  483.   /* Catch via `setjmp'.  */
  484.   ref = build_component_ref (handler, get_identifier ("handler"), NULL_TREE, 0);
  485.   call_to_setjmp = build_function_call (BISJ, build_tree_list (NULL_TREE, ref));
  486.  
  487.   /* RAISE throws to EXCEPT part.  */
  488.   expand_start_try (build_binary_op (EQ_EXPR, call_to_setjmp, integer_zero_node), 0, 1);
  489. }
  490.  
  491. /* If KEEP is 1, then declarations in the TRY statement are worth keeping.
  492.    If KEEP is 2, then the TRY statement was generated by the compiler.
  493.    If KEEP is 0, the declarations in the TRY statement contain errors.  */
  494.  
  495. tree
  496. cplus_expand_end_try (keep)
  497.      int keep;
  498. {
  499.   tree decls, decl, block;
  500.  
  501.   if (keep < 2)
  502.     pop_implicit_try_blocks (NULL_TREE);
  503.  
  504.   decls = getdecls ();
  505.  
  506.   /* Emit code to avoid falling through into a default
  507.      handler that might come later.  */
  508.   expand_end_try ();
  509.  
  510.   /* Pops binding contour local to TRY, and get the exception handler
  511.      object built by `...start_try'.  */
  512.   switch (keep)
  513.     {
  514.     case 0:
  515.       expand_end_bindings (decls, 0, 1);
  516.       block = poplevel (0, 0, 0);
  517.       pop_momentary (); 
  518.       decl = getdecls ();
  519.       break;
  520.  
  521.     case 1:
  522.       expand_end_bindings (decls, 1, 1);
  523.       block = poplevel (1, 1, 0);
  524.       pop_momentary ();
  525.       decl = getdecls ();
  526.       break;
  527.  
  528.     default:
  529.       decl = tree_last (decls);
  530.       block = NULL_TREE;
  531.       break;
  532.     }
  533.  
  534.   assert (TREE_CODE (decl) == VAR_DECL && TREE_TYPE (decl) == EHS_type);
  535.   if (block)
  536.     {
  537.       TREE_LANG_FLAG_1 (block) = 1;
  538.       TREE_USED (block) = 1;
  539.     }
  540.  
  541.   /* Pass it back so that its rtl can be bound to its name
  542.      (or vice versa).  */
  543.   return decl;
  544. }
  545.  
  546. void
  547. cplus_expand_start_except (name, decl)
  548.      tree name, decl;
  549. {
  550.   int yes;
  551.   tree tmp;
  552.  
  553.   expand_start_except (0, 1);
  554.  
  555.   /* This is internal `eh'.  */
  556.   current_exception_decl = decl;
  557.   /* Get the exception object into scope (user declared `ex').  */
  558.   tmp = pushdecl (build_decl (VAR_DECL, name, ptr_type_node));
  559.   DECL_INITIAL (tmp) = error_mark_node;
  560.   finish_decl (tmp, build (COMPONENT_REF, ptr_type_node, decl, TREE_OPERAND (EHS_parms, 1)), 0);
  561.   current_exception_type = NULL_TREE;
  562.   yes = suspend_momentary ();
  563.   /* From now on, send the user to our faked-up object.  */
  564.   current_exception_object = build1 (INDIRECT_REF, void_type_node, tmp);
  565.   IDENTIFIER_LOCAL_VALUE (name) = current_exception_object;
  566.   resume_momentary (yes);
  567.  
  568.   /* Pop exception handler stack.  */
  569.   expand_assignment (EHS_decl, EHS_prev, 0, 0);
  570. }
  571.  
  572. /* Generate the call to `unhandled_exception' that is appropriate
  573.    for this particular unhandled exception.  */
  574. static tree
  575. call_to_unhandled_exception ()
  576. {
  577.   extern int lineno;
  578.   tree parms = tree_cons (NULL_TREE,
  579.               combine_strings (build_string (strlen (input_filename + 1), input_filename)),
  580.               build_tree_list (NULL_TREE, build_int_2 (lineno, 0)));
  581.   return build_function_call (BIUE, parms);
  582. }
  583.  
  584. /* Note that this must be mirror image of `...start_try'.
  585.    DFAULT is the default clause, if there was one.
  586.    DFAULT is ERROR_MARK_NODE when this ends an implicit handler.  */
  587. void
  588. cplus_expand_end_except (dfault)
  589.      tree dfault;
  590. {
  591.   extern tree expand_end_except (); /* stmt.c.  */
  592.   tree decls, raised;
  593.  
  594.   if (dfault == NULL_TREE)
  595.     {
  596.       /* Uncaught exception at outermost level.  If raised locally,
  597.      reraise the exception.  Otherwise, generate code to call `abort'.  */
  598.       if (in_try_block (1) == 0)
  599.     {
  600.       expand_start_cond (build (EQ_EXPR, integer_type_node,
  601.                     exception_throw_decl, integer_zero_node), 0);
  602.       expand_expr (call_to_unhandled_exception (), 0, VOIDmode, 0);
  603.       expand_end_cond ();
  604.     }
  605.       /* Try the next handler.  */
  606.       if (! expand_escape_except ())
  607.     compiler_error ("except nesting botch");
  608.     }
  609.  
  610.   raised = expand_end_except ();
  611.  
  612.   decls = getdecls ();
  613.   expand_end_bindings (decls, decls != 0, 1);
  614.   poplevel (decls != 0, 1, 0);
  615.  
  616.   /* Implicit handlers do not use the momentary obstack.  */
  617.   if (dfault != error_mark_node)
  618.     pop_momentary ();
  619.  
  620.   if (! in_try_block (1))
  621.     {
  622.       /* Check that this function is not raising exceptions
  623.      it is not supposed to.  */
  624.       while (raised)
  625.     {
  626.       error_with_decl (TREE_VALUE (raised), "exception `%s' raised but not declared raisable");
  627.       raised = TREE_CHAIN (raised);
  628.     }
  629.     }
  630.   else if (dfault == NULL_TREE || dfault == error_mark_node)
  631.     {
  632.       expand_start_cond (build (NE_EXPR, integer_type_node,
  633.                 exception_throw_decl,
  634.                 integer_zero_node), 0);
  635.       /* We fell off the end of this try block.  Try going to the next.
  636.      The escape_label will be the beginning of the next try block.  */
  637.       if (! expand_escape_except ())
  638.     compiler_error ("except nesting botch");
  639.       expand_end_cond ();
  640.     }
  641. }
  642.  
  643. /* Generate code to raise exception RAISE_ID.
  644.    If EXP is NULL_TREE, then PARMS is the list of parameters to use
  645.    for constructing this exception.
  646.    If EXP is non-NULL, then it is an already constructed object
  647.    of the kind that we want.
  648.  
  649.    FOR_RERAISE is non-zero if this raise is called by reraise.  In
  650.    this case we do not need to emit extra gotos to avoid warning messages;
  651.    the caller will do that once after all the exceptions it reraises
  652.    are handled and raised.  */
  653. void
  654. cplus_expand_raise (raise_id, parms, exp, for_reraise)
  655.      tree raise_id;
  656.      tree parms;
  657.      tree exp;
  658.      int for_reraise;
  659. {
  660.   /* Allocate new exception of appropriate type, passing
  661.      PARMS to its constructor.  */
  662.   tree cname, name;
  663.   tree decl;
  664.   tree xexp = exp;
  665.  
  666.   cname = lookup_exception_cname (current_class_type, current_class_name, raise_id);
  667.   if (cname == error_mark_node)
  668.     return;
  669.   name = TREE_VALUE (raise_id);
  670.  
  671.   decl = lookup_exception_object (cname, name, 1);
  672.   if (decl == NULL_TREE)
  673.     return;
  674.  
  675.   if (exp == NULL_TREE)
  676.     {
  677.       exp = build_method_call (NULL_TREE, name, parms, NULL_TREE, LOOKUP_COMPLAIN);
  678.       if (exp == error_mark_node)
  679.     return;
  680.     }
  681.  
  682.   if (in_try_block (1))
  683.     {
  684.       expand_raise (decl);
  685.     }
  686.   else if (! current_function_decl)
  687.     {
  688.       if (xexp == NULL_TREE)
  689.     error_with_decl (decl, "invalid raise of `%s' outside of functions");
  690.       else
  691.     error_with_decl (decl, "invalid reraise of `%s' outside of functions");
  692.     }
  693.   else
  694.     {
  695.       /* Test this raise against what this function permits.  */
  696.       tree names = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl));
  697.       while (names)
  698.     {
  699.       if (decl == TREE_TYPE (names))
  700.         break;
  701.       names = TREE_CHAIN (names);
  702.     }
  703.       if (names == NULL_TREE)
  704.     {
  705.       error ("current function not declared to raise exception `%s'",
  706.          IDENTIFIER_POINTER (name));
  707.       return;
  708.     }
  709.     }
  710.  
  711.   expand_assignment (EHS_parms, exp, 0, 0);
  712.  
  713.   /* Set the global exception handler stack's NAME field
  714.      to the `name' of this exception.  The global exception
  715.      handler stack is the container for the exception object
  716.      we just built.
  717.  
  718.      We go through a function call to make life easier when debugging.  */
  719. #if 0
  720.   expand_assignment (EHS_name, build_unary_op (ADDR_EXPR, decl, 0), 0, 0);
  721. #else
  722.   parms = tree_cons (NULL_TREE, build_unary_op (ADDR_EXPR, EHS_name, 0),
  723.              build_tree_list (NULL_TREE,
  724.                       build_unary_op (ADDR_EXPR, decl, 0)));
  725.   expand_expr (build_function_call (BIR, parms));
  726. #endif
  727.  
  728.   /* Activate thrower.  If we are inside a TRY statement,
  729.      we can cheat and not do this, saving a longjmp.  */
  730.   if (in_try_block (1) == 0)
  731.     {
  732.       sets_exception_throw_decl = 1;
  733.       expand_assignment (exception_throw_decl, integer_one_node, 0, 0);
  734.     }
  735.  
  736.   if (xexp == NULL_TREE)
  737.     {    
  738.       /* Invoke destructors for current procedure or handler.  */
  739.       if (! expand_escape_except ())
  740.     compiler_error ("except nesting botch");
  741.       /* Throw via `longjmp'... Done as side-effect of goto.  */
  742.     }
  743.   /* To avoid spurious warning messages, we add a goto to the end
  744.      of the function.  This code is dead, and the compiler should
  745.      know how to delete it, but for now, we are stuck with it.  */
  746.   if (! for_reraise && DECL_RESULT (current_function_decl))
  747.     expand_null_return ();
  748. }
  749.  
  750. tree
  751. cplus_expand_start_catch (raise_id)
  752.      tree raise_id;
  753. {
  754.   tree cname = lookup_exception_cname (current_class_type, current_class_name, raise_id);
  755.   tree decl;
  756.   tree ref, cond;
  757.  
  758.   if (cname == error_mark_node)
  759.     {
  760.       decl = error_mark_node;
  761.       cond = error_mark_node;
  762.     }
  763.   else
  764.     {
  765.       decl = lookup_exception_object (cname, TREE_VALUE (raise_id), 1);
  766.       if (decl == NULL_TREE)
  767.     {
  768.       cond = error_mark_node;
  769.     }
  770.       else
  771.     {
  772.       ref = build (COMPONENT_REF, ptr_type_node,
  773.                current_exception_decl, TREE_OPERAND (EHS_name, 1));
  774.       cond = build_binary_op (EQ_EXPR, build_unary_op (ADDR_EXPR, decl, 0), ref);
  775.     }
  776.     }
  777.   expand_start_cond (cond, 0);
  778.  
  779.   /* Does nothing right now.  */
  780.   expand_catch (decl);
  781.   if (current_exception_type
  782.       && TYPE_NEEDS_DESTRUCTOR (current_exception_type))
  783.     {
  784.       /* Make a cleanup for the name-specific exception object now in scope.  */
  785.       tree cleanup = maybe_build_cleanup (current_exception_object);
  786.       expand_start_bindings (0);
  787.       expand_decl_cleanup (NULL_TREE, cleanup);
  788.     }
  789.   return decl;
  790. }
  791.  
  792. void
  793. cplus_expand_end_catch (for_reraise)
  794.      int for_reraise;
  795. {
  796.   if (current_exception_type
  797.       && TYPE_NEEDS_DESTRUCTOR (current_exception_type))
  798.     {
  799.       /* Destroy the specific exception object now in scope.  */
  800.       expand_end_bindings (getdecls (), 0, 1);
  801.     }
  802.   if (for_reraise)
  803.     {
  804.       if (! expand_escape_except ())
  805.     abort ();
  806.     }
  807.   else
  808.     {
  809.       if (! expand_end_catch ())
  810.     abort ();
  811.     }
  812.   expand_end_cond ();
  813. }
  814.  
  815. /* Reraise an exception.
  816.    If EXCEPTIONS is NULL_TREE, it means reraise whatever exception was caught.
  817.    If EXCEPTIONS is an IDENTIFIER_NODE, it means reraise the exception
  818.    object named by EXCEPTIONS.  This must be a variable declared in
  819.    an `except' clause.
  820.    If EXCEPTIONS is a TREE_LIST, it is the list of exceptions we are
  821.    willing to reraise.  */
  822.  
  823. void
  824. cplus_expand_reraise (exceptions)
  825.      tree exceptions;
  826. {
  827.   tree ex_ptr;
  828.   tree ex_object = current_exception_object;
  829.  
  830.   if (exceptions && TREE_CODE (exceptions) == IDENTIFIER_NODE)
  831.     {
  832.       /* Don't get tripped up if its TREE_TYPE is `error_mark_node'.  */
  833.       ex_object = IDENTIFIER_LOCAL_VALUE (exceptions);
  834.       if (ex_object == NULL_TREE || TREE_CODE (ex_object) != INDIRECT_REF)
  835.     {
  836.       error ("`%s' is not an exception decl", IDENTIFIER_POINTER (exceptions));
  837.       return;
  838.     }
  839.       assert (TREE_CODE (TREE_OPERAND (ex_object, 0)) == VAR_DECL);
  840.       exceptions = NULL_TREE;
  841.     }
  842.  
  843.   /* reraise ALL, used by compiler.  */
  844.   if (exceptions == NULL_TREE)
  845.     {
  846.       /* Now treat reraise like catch/raise.  */
  847.       expand_catch (error_mark_node);
  848.       expand_raise (error_mark_node);
  849.       expand_assignment (EHS_name,
  850.              build (COMPONENT_REF, ptr_type_node,
  851.                 current_exception_decl, TREE_OPERAND (EHS_name, 1)), 0, 0);
  852.       expand_assignment (EHS_parms,
  853.              build (COMPONENT_REF, ptr_type_node,
  854.                 current_exception_decl, TREE_OPERAND (EHS_parms, 1)), 0, 0);
  855.       if (in_try_block (1) == 0)
  856.     {
  857.       sets_exception_throw_decl = 1;
  858.       expand_assignment (exception_throw_decl, integer_one_node, 0, 0);
  859.     }
  860.       /* Set to zero so that destructor will not be called.  */
  861.       expand_assignment (build1 (NOP_EXPR, ptr_type_node, TREE_OPERAND (ex_object, 0)),
  862.              integer_zero_node, 0, 0);
  863.       if (! expand_escape_except ())
  864.     abort ();
  865.  
  866.       /* To avoid spurious warning messages, we add a goto to the end
  867.      of the function.  This code is dead, and the compiler should
  868.      know how to delete it, but for now, we are stuck with it.  */
  869.       if (DECL_RESULT (current_function_decl))
  870.     expand_null_return ();
  871.  
  872.       return;
  873.     }
  874.  
  875.   ex_ptr = build1 (NOP_EXPR, NULL_TREE, TREE_OPERAND (ex_object, 0));
  876.  
  877.   /* reraise from a list of exceptions.  */
  878.   while (exceptions)
  879.     {
  880.       tree type = lookup_exception_type (current_class_type, current_class_name,
  881.                      exceptions);
  882.       if (type == NULL_TREE)
  883.     {
  884.       error ("`%s' is not an exception type",
  885.          IDENTIFIER_POINTER (TREE_VALUE (exceptions)));
  886.       current_exception_type = NULL_TREE;
  887.       TREE_TYPE (ex_object) = error_mark_node;
  888.       TREE_TYPE (ex_ptr) = error_mark_node;
  889.     }
  890.       else
  891.     {
  892.       current_exception_type = type;
  893.       /* In-place union.  */
  894.       TREE_TYPE (ex_object) = type;
  895.       TREE_TYPE (ex_ptr) = TYPE_POINTER_TO (type);
  896.     }
  897.  
  898.       /* Now treat reraise like catch/raise.  */
  899.       cplus_expand_start_catch (exceptions);
  900.       cplus_expand_raise (exceptions, NULL_TREE, ex_ptr, 1);
  901.       /* Set to zero so that destructor will not be called.  */
  902.       if (TREE_TYPE (ex_ptr) != error_mark_node)
  903.     expand_assignment (ex_ptr, integer_zero_node, 0, 0);
  904.       cplus_expand_end_catch (1);
  905.       exceptions = TREE_CHAIN (exceptions);
  906.     }
  907.   /* Don't propagate any unhandled exceptions.  */
  908.   expand_expr (call_to_unhandled_exception (), 0, VOIDmode, 0);
  909.  
  910.   /* To avoid spurious warning messages, we add a goto to the end
  911.      of the function.  This code is dead, and the compiler should
  912.      know how to delete it, but for now, we are stuck with it.  */
  913.   if (DECL_RESULT (current_function_decl))
  914.     expand_null_return ();
  915. }
  916.  
  917. void
  918. setup_exception_throw_decl ()
  919. {
  920.   tree call_to_longjmp, parms;
  921.  
  922.   int old = suspend_momentary ();
  923.  
  924.   exception_throw_decl = build_decl (VAR_DECL, get_identifier (THROW_NAME), integer_type_node);
  925.   pushdecl (exception_throw_decl);
  926.   parms = tree_cons (NULL_TREE, EHS_handler,
  927.              build_tree_list (0, integer_one_node));
  928.   call_to_longjmp = build_function_call (BILJ, parms);
  929.  
  930.   expand_decl (exception_throw_decl);
  931.   expand_decl_cleanup (exception_throw_decl,
  932.                build (COND_EXPR, void_type_node,
  933.                   exception_throw_decl,
  934.                   call_to_longjmp, integer_zero_node));
  935.   DECL_INITIAL (exception_throw_decl) = integer_zero_node;
  936.   sets_exception_throw_decl = 0;
  937.   resume_momentary (old);
  938. }
  939.  
  940. void
  941. init_exception_processing ()
  942. {
  943.   extern tree unhandled_exception_fndecl;
  944.   tree cname = get_identifier ("ExceptionHandler");
  945.   tree field, chain;
  946.   tree ctor, dtor;
  947.   tree jmp_buf_type = build_array_type (integer_type_node,
  948.                     build_index_type (build_int_2 (_JBLEN-1, 0)));
  949.   tree jmp_buf_arg_type = build_pointer_type (integer_type_node);
  950.  
  951.   tree parmtypes = hash_tree_chain (jmp_buf_arg_type, void_list_node);
  952.   tree setjmp_fndecl, longjmp_fndecl, raise_fndecl;
  953.  
  954.   EHS_type = xref_tag (record_type_node, cname, NULL_TREE);
  955.  
  956.   push_lang_context (lang_name_c);
  957.   setjmp_fndecl = define_function ("setjmp",
  958.                    build_function_type (integer_type_node,
  959.                             parmtypes),
  960.                    NOT_BUILT_IN, pushdecl, 0);
  961.   BISJ = default_conversion (setjmp_fndecl);
  962.   parmtypes = hash_tree_chain (jmp_buf_arg_type,
  963.                    hash_tree_chain (integer_type_node, void_list_node));
  964.   longjmp_fndecl = define_function ("longjmp",
  965.                     build_function_type (integer_type_node, parmtypes),
  966.                     NOT_BUILT_IN, pushdecl, 0);
  967.   raise_fndecl = define_function ("__raise_exception",
  968.                   build_function_type (void_type_node,
  969.                                hash_tree_chain (ptr_type_node,
  970.                                     hash_tree_chain (build_pointer_type (ptr_type_node), void_list_node))),
  971.                   NOT_BUILT_IN, pushdecl, 0);
  972.   BILJ = default_conversion (longjmp_fndecl);
  973.   BIR = default_conversion (raise_fndecl);
  974.   BIUE = default_conversion (unhandled_exception_fndecl);
  975.  
  976.   pop_lang_context ();
  977.  
  978.   /* finish_struct will pop this.  */
  979.   pushclass (EHS_type, 0);
  980.   field = build_lang_field_decl (FIELD_DECL, get_identifier ("parms"), ptr_type_node);
  981.   chain = field;
  982.   field = build_lang_field_decl (FIELD_DECL, get_identifier ("name"),
  983.                  build_pointer_type (default_function_type));
  984.   TREE_CHAIN (field) = chain;
  985.   chain = field;
  986.   field = build_lang_field_decl (FIELD_DECL, get_identifier ("handler"), jmp_buf_type);
  987.   TREE_CHAIN (field) = chain;
  988.   chain = field;
  989.   field = build_lang_field_decl (FIELD_DECL, get_identifier ("prev"),
  990.                  TYPE_POINTER_TO (EHS_type));
  991.   TREE_CHAIN (field) = chain;
  992.   chain = field;
  993.  
  994.   ctor = build_lang_decl (FUNCTION_DECL, cname,
  995.               build_cplus_method_type (EHS_type, TYPE_POINTER_TO (EHS_type), void_list_node));
  996.   DECL_CONSTRUCTOR_P (ctor) = 1;
  997.   TREE_STATIC (ctor) = 1;
  998.   TREE_PUBLIC (ctor) = 1;
  999.   grokclassfn (EHS_type, cname, ctor, NO_SPECIAL, 0, 0);
  1000.   grok_ctor_properties (EHS_type, ctor);
  1001.   finish_decl (pushdecl (ctor), 0, 0);
  1002.   /* Must copy the node here because the FUNCTION_DECL
  1003.      used inside the struct ain't the same as the
  1004.      FUNCTION_DECL we stick into the global binding
  1005.      contour.  */
  1006.   ctor = copy_node (ctor);
  1007.   TREE_CHAIN (ctor) = chain;
  1008.   chain = ctor;
  1009.   dtor = build_lang_decl (FUNCTION_DECL, cname,
  1010.               build_cplus_method_type (EHS_type, TYPE_POINTER_TO (EHS_type), void_list_node));
  1011.   TREE_STATIC (dtor) = 1;
  1012.   TREE_PUBLIC (dtor) = 1;
  1013.   grokclassfn (EHS_type, cname, dtor, DTOR_FLAG, 0, 0);
  1014.   finish_decl (pushdecl (dtor), 0, 0);
  1015.   /* Copy for the same reason as copying ctor.  */
  1016.   dtor = copy_node (dtor);
  1017.   TREE_CHAIN (dtor) = chain;
  1018.   chain = dtor;
  1019.   TYPE_HAS_CONSTRUCTOR (EHS_type) = 1;
  1020.   TYPE_HAS_DESTRUCTOR (EHS_type) = 1;
  1021.   finish_struct (EHS_type, temp_tree_cons (NULL_TREE, chain, NULL_TREE), 0, 0);
  1022. }
  1023.  
  1024. void
  1025. init_exception_processing_1 ()
  1026. {
  1027.   register tree EHS_id = get_identifier ("exceptionHandlerStack");
  1028.  
  1029.   EHS_decl = IDENTIFIER_GLOBAL_VALUE (EHS_id);
  1030.  
  1031.   /* If we have no other definition, default to library implementation.  */
  1032.   if (EHS_decl == NULL_TREE)
  1033.     {
  1034.       EHS_decl = build_decl (VAR_DECL, EHS_id, TYPE_POINTER_TO (EHS_type));
  1035.       /* If we don't push this, its definition, should it be encountered,
  1036.      will not be seen.  */
  1037.       EHS_decl = pushdecl (EHS_decl);
  1038.       TREE_EXTERNAL (EHS_decl) = 1;
  1039.       TREE_STATIC (EHS_decl) = 1;
  1040.       TREE_PUBLIC (EHS_decl) = 1;
  1041.       finish_decl (EHS_decl, 0, 0);
  1042.     }
  1043.   else if (TREE_CODE (EHS_decl) != VAR_DECL
  1044.        || TREE_TYPE (EHS_decl) != TYPE_POINTER_TO (EHS_type))
  1045.     fatal ("exception handling declarations conflict with compiler's internal model");
  1046.  
  1047.   if (EHS_prev == NULL_TREE)
  1048.     {
  1049.       register tree EHS_DECL = build1 (INDIRECT_REF, EHS_type, EHS_decl);
  1050.       EHS_prev = build_component_ref (EHS_DECL, get_identifier ("prev"), 0, 0);
  1051.       EHS_handler = build_component_ref (EHS_DECL, get_identifier ("handler"), 0, 0);
  1052.       EHS_parms = build_component_ref (EHS_DECL, get_identifier ("parms"), 0, 0);
  1053.       EHS_name = build_component_ref (EHS_DECL, get_identifier ("name"), 0, 0);
  1054.     }
  1055. }
  1056.