From patchwork Thu Feb 24 03:01:29 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 84313 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 176B2B717A for ; Thu, 24 Feb 2011 14:01:45 +1100 (EST) Received: (qmail 7161 invoked by alias); 24 Feb 2011 03:01:43 -0000 Received: (qmail 7153 invoked by uid 22791); 24 Feb 2011 03:01:42 -0000 X-SWARE-Spam-Status: No, hits=-2.0 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, SPF_HELO_PASS, T_RP_MATCHES_RCVD, T_TVD_MIME_NO_HEADERS X-Spam-Check-By: sourceware.org Received: from smtp-out.google.com (HELO smtp-out.google.com) (74.125.121.67) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 24 Feb 2011 03:01:38 +0000 Received: from wpaz33.hot.corp.google.com (wpaz33.hot.corp.google.com [172.24.198.97]) by smtp-out.google.com with ESMTP id p1O31Zkm020951 for ; Wed, 23 Feb 2011 19:01:35 -0800 Received: from vws6 (vws6.prod.google.com [10.241.21.134]) by wpaz33.hot.corp.google.com with ESMTP id p1O31XtB015984 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NOT) for ; Wed, 23 Feb 2011 19:01:33 -0800 Received: by vws6 with SMTP id 6so137586vws.17 for ; Wed, 23 Feb 2011 19:01:33 -0800 (PST) Received: by 10.52.158.100 with SMTP id wt4mr425609vdb.144.1298516493492; Wed, 23 Feb 2011 19:01:33 -0800 (PST) Received: from coign.google.com ([72.14.228.1]) by mx.google.com with ESMTPS id i1sm5976534vby.1.2011.02.23.19.01.31 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 23 Feb 2011 19:01:33 -0800 (PST) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: Go patch committed: Don't crash on map index outside function Date: Wed, 23 Feb 2011 19:01:29 -0800 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 X-System-Of-Record: true 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 This patch to the Go frontend avoids a crash if a map index is used outside of a function. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline. Ian diff -r 8f38753c87d1 go/expressions.cc --- a/go/expressions.cc Wed Feb 23 18:49:55 2011 -0800 +++ b/go/expressions.cc Wed Feb 23 18:57:29 2011 -0800 @@ -9888,12 +9888,39 @@ // We need to pass in a pointer to the key, so stuff it into a // variable. - tree tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree)); - DECL_IGNORED_P(tmp) = 0; - DECL_INITIAL(tmp) = index_tree; - tree make_tmp = build1(DECL_EXPR, void_type_node, tmp); - tree tmpref = fold_convert(const_ptr_type_node, build_fold_addr_expr(tmp)); - TREE_ADDRESSABLE(tmp) = 1; + tree tmp; + tree make_tmp; + if (current_function_decl != NULL) + { + tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree)); + DECL_IGNORED_P(tmp) = 0; + DECL_INITIAL(tmp) = index_tree; + make_tmp = build1(DECL_EXPR, void_type_node, tmp); + TREE_ADDRESSABLE(tmp) = 1; + } + else + { + tmp = build_decl(this->location(), VAR_DECL, create_tmp_var_name("M"), + TREE_TYPE(index_tree)); + DECL_EXTERNAL(tmp) = 0; + TREE_PUBLIC(tmp) = 0; + TREE_STATIC(tmp) = 1; + DECL_ARTIFICIAL(tmp) = 1; + if (!TREE_CONSTANT(index_tree)) + make_tmp = fold_build2_loc(this->location(), INIT_EXPR, void_type_node, + tmp, index_tree); + else + { + TREE_READONLY(tmp) = 1; + TREE_CONSTANT(tmp) = 1; + DECL_INITIAL(tmp) = index_tree; + make_tmp = NULL_TREE; + } + rest_of_decl_compilation(tmp, 1, 0); + } + tree tmpref = fold_convert_loc(this->location(), const_ptr_type_node, + build_fold_addr_expr_loc(this->location(), + tmp)); static tree map_index_fndecl; tree call = Gogo::call_builtin(&map_index_fndecl, @@ -9920,9 +9947,10 @@ return error_mark_node; tree ptr_val_type_tree = build_pointer_type(val_type_tree); - return build2(COMPOUND_EXPR, ptr_val_type_tree, - make_tmp, - fold_convert(ptr_val_type_tree, call)); + tree ret = fold_convert_loc(this->location(), ptr_val_type_tree, call); + if (make_tmp != NULL_TREE) + ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret); + return ret; } // Make a map index expression.