From patchwork Fri Oct 29 13:44:17 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Sandoe X-Patchwork-Id: 69588 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id DD218B70A7 for ; Sat, 30 Oct 2010 00:44:51 +1100 (EST) Received: (qmail 2950 invoked by alias); 29 Oct 2010 13:44:48 -0000 Received: (qmail 2933 invoked by uid 22791); 29 Oct 2010 13:44:46 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, TW_BJ, TW_JC, TW_TM X-Spam-Check-By: sourceware.org Received: from c2beaomr06.btconnect.com (HELO mail.btconnect.com) (213.123.26.184) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 29 Oct 2010 13:44:22 +0000 Received: from host81-138-1-83.in-addr.btopenworld.com (EHLO thor.office) ([81.138.1.83]) by c2beaomr06.btconnect.com with ESMTP id ASG99466; Fri, 29 Oct 2010 14:44:19 +0100 (BST) Message-Id: <5AEA56D9-052C-4FE7-B8E0-D78BDFCD3C57@sandoe-acoustics.co.uk> From: IainS To: GCC Patches Mime-Version: 1.0 (Apple Message framework v936) Subject: [Patch, RFC, c*, ObjC* ] make the translation unit decl avail. via a lang hook Date: Fri, 29 Oct 2010 14:44:17 +0100 Cc: Richard Guenther , Mike Stump , Nicola Pero X-Mirapoint-IP-Reputation: reputation=Fair-1, source=Queried, refid=tid=0001.0A0B0301.4CCACFB2.01B0, actions=TAG X-Junkmail-Signature-Raw: score=unknown, refid=str=0001.0A0B020A.4CCACFB4.0012, ss=1, fgs=0, ip=0.0.0.0, so=2010-07-22 22:03:31, dmn=2009-09-10 00:05:08, mode=single engine X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org given that my current understanding is: (a) we should make vars file-scope by setting DECL_CONTEXT = translation unit decl (b) there should only be one translation unit decl per TU (c) at present none of the FEs export the t_u_d where other parts of the machinery can see it. would the following patch be appropriate? (it can be extended to fortran and java, I believe, fairly easily). --- (With this applied I can use DECL_CONTEXT (decl) = (*lang_hooks.decls.translation_unit_context) (); in each place that it is currently set to NULL in objc-act.c and also replace the two instances of pushdecl_top_level () with the same. and I get no new test-suite fails for ObjC* ) comments? Iain Index: gcc/c-family/c-common.h =================================================================== --- gcc/c-family/c-common.h (revision 166059) +++ gcc/c-family/c-common.h (working copy) @@ -514,6 +514,7 @@ extern tree add_stmt (tree); extern void push_cleanup (tree, tree, bool); extern tree pushdecl_top_level (tree); extern tree pushdecl (tree); +extern tree c_translation_unit_decl (void); extern tree build_modify_expr (location_t, tree, tree, enum tree_code, location_t, tree, tree); extern tree build_indirect_ref (location_t, tree, ref_operator); Index: gcc/cp/decl.c =================================================================== --- gcc/cp/decl.c (revision 166059) +++ gcc/cp/decl.c (working copy) @@ -3427,6 +3427,19 @@ initialize_predefined_identifiers (void) } } +/* The current translation unit. */ +static GTY(()) tree current_translation_unit = NULL_TREE; + +/* The function used to return the current t_u_d for the + translation_unit_context langhook. */ + +tree +cp_translation_unit_decl (void) +{ + gcc_assert (current_translation_unit); + return current_translation_unit; +} + /* Create the predefined scalar types of C, and some nodes representing standard constants (0, 1, (void *)0). Initialize the global binding level. @@ -3452,7 +3465,8 @@ cxx_init_decl_processing (void) gcc_assert (global_namespace == NULL_TREE); global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name, void_type_node); - DECL_CONTEXT (global_namespace) = build_translation_unit_decl (NULL_TREE); + current_translation_unit = build_translation_unit_decl (NULL_TREE); + DECL_CONTEXT (global_namespace) = current_translation_unit; TREE_PUBLIC (global_namespace) = 1; begin_scope (sk_namespace, global_namespace); Index: gcc/cp/cp-objcp-common.h =================================================================== --- gcc/cp/cp-objcp-common.h (revision 166059) +++ gcc/cp/cp-objcp-common.h (working copy) @@ -132,6 +132,8 @@ extern bool cp_function_decl_explicit_p (tree decl #define LANG_HOOKS_GIMPLIFY_EXPR cp_gimplify_expr #undef LANG_HOOKS_FUNCTION_DECL_EXPLICIT_P #define LANG_HOOKS_FUNCTION_DECL_EXPLICIT_P cp_function_decl_explicit_p +#undef LANG_HOOKS_TUD +#define LANG_HOOKS_TUD cp_translation_unit_decl #undef LANG_HOOKS_OMP_PREDETERMINED_SHARING #define LANG_HOOKS_OMP_PREDETERMINED_SHARING cxx_omp_predetermined_sharing #undef LANG_HOOKS_OMP_CLAUSE_DEFAULT_CTOR Index: gcc/cp/cp-tree.h =================================================================== --- gcc/cp/cp-tree.h (revision 166059) +++ gcc/cp/cp-tree.h (working copy) @@ -4764,6 +4764,7 @@ extern tree current_decl_namespace (void); /* decl.c */ extern tree poplevel (int, int, int); +extern tree cp_translation_unit_decl (void); extern void cxx_init_decl_processing (void); enum cp_tree_node_structure_enum cp_tree_node_structure (union lang_tree_node *); Index: gcc/c-objc-common.h =================================================================== --- gcc/c-objc-common.h (revision 166059) +++ gcc/c-objc-common.h (working copy) @@ -85,6 +85,8 @@ along with GCC; see the file COPYING3. If not see #define LANG_HOOKS_TO_TARGET_CHARSET c_common_to_target_charset #undef LANG_HOOKS_EXPR_TO_DECL #define LANG_HOOKS_EXPR_TO_DECL c_expr_to_decl +#undef LANG_HOOKS_TUD +#define LANG_HOOKS_TUD c_translation_unit_decl /* The C front end's scoping structure is very different from that expected by the language-independent code; it is best Index: gcc/c-decl.c =================================================================== --- gcc/c-decl.c (revision 166059) +++ gcc/c-decl.c (working copy) @@ -454,6 +454,8 @@ static GTY((deletable)) struct c_binding *binding_ t_->to##_last = f_->from##_last; \ } while (0) +static GTY(()) tree current_translation_unit = NULL_TREE; + /* A c_inline_static structure stores details of a static identifier referenced in a definition of a function that may be an inline definition if no subsequent declaration of that function uses @@ -528,6 +530,14 @@ static tree grokdeclarator (const struct c_declara static tree grokparms (struct c_arg_info *, bool); static void layout_array_type (tree); +/* Return the translation unit decl for the current TU. */ +tree +c_translation_unit_decl (void) +{ + gcc_assert (current_translation_unit); + return current_translation_unit; +} + /* T is a statement. Add it to the statement-tree. This is the C/ObjC version--C++ has a slightly different version of this function. */ @@ -1066,8 +1076,7 @@ pop_scope (void) context = current_function_decl; else if (scope == file_scope) { - tree file_decl = build_translation_unit_decl (NULL_TREE); - context = file_decl; + context = (*lang_hooks.decls.translation_unit_context) (); } else context = block; @@ -3461,6 +3470,9 @@ c_init_decl_processing (void) c_common_nodes_and_builtins (); + /* The current translation unit. */ + current_translation_unit = build_translation_unit_decl (NULL_TREE); + /* In C, comparisons and TRUTH_* expressions have type int. */ truthvalue_type_node = integer_type_node; truthvalue_true_node = integer_one_node; Index: gcc/langhooks.h =================================================================== --- gcc/langhooks.h (revision 166059) +++ gcc/langhooks.h (working copy) @@ -152,6 +152,9 @@ struct lang_hooks_for_decls symbol is already declared, may return a different decl for that name. */ tree (*pushdecl) (tree); + + /* To add a decl to 'file scope' set its DECL_CONTEXT to this. */ + tree (*translation_unit_context) (void); /* Returns the chain of decls so far in the current scope level. */ tree (*getdecls) (void); Index: gcc/langhooks-def.h =================================================================== --- gcc/langhooks-def.h (revision 166059) +++ gcc/langhooks-def.h (working copy) @@ -200,6 +200,7 @@ extern tree lhd_make_node (enum tree_code); /* Declaration hooks. */ #define LANG_HOOKS_GLOBAL_BINDINGS_P global_bindings_p #define LANG_HOOKS_PUSHDECL pushdecl +#define LANG_HOOKS_TUD lhd_return_null_tree_v #define LANG_HOOKS_GETDECLS getdecls #define LANG_HOOKS_FUNCTION_DECL_EXPLICIT_P hook_bool_tree_false #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL lhd_warn_unused_global_decl @@ -220,6 +221,7 @@ extern tree lhd_make_node (enum tree_code); #define LANG_HOOKS_DECLS { \ LANG_HOOKS_GLOBAL_BINDINGS_P, \ LANG_HOOKS_PUSHDECL, \ + LANG_HOOKS_TUD, \ LANG_HOOKS_GETDECLS, \ LANG_HOOKS_FUNCTION_DECL_EXPLICIT_P, \ LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P, \