From patchwork Thu Oct 17 12:03:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 1178490 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-511213-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=acm.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="in+e3hUm"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="vJK8Tvvx"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46v7B43ckQz9sNw for ; Thu, 17 Oct 2019 23:04:10 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=JUG6kspm9XyjkwoQLnBOTPWVXR8qSMI1qUHpiuzBioHlUjJKdh UDqdqlT7e8k2H/Nx3OYAxhaCRa8Fo6HgSkqF1kA+kayftL+jW08iLjfwSzLI6DiJ oZVZ72WLDH6Whj3Pv4CATAcoDNWZps0r7VR0JzibQAVMMe8SbQMb8e+Yw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=7yu6BokodwbM1FBCBuG495P87CI=; b=in+e3hUmyt/6v6i7roS8 pLI9W7e9R8SuqIuq22X0Zzb+7scFPqnmqMO7sxRrmd+pZ/rBNHoFeEO6YWeGHJhV PPbmXJitegXZxNcTe3MbrWQRhg+krkwaWorAfvueZRB2evfYDG6M3uAKa2Q5xxIN pcE0+egkXY4GWsG692nqQ2U= Received: (qmail 45960 invoked by alias); 17 Oct 2019 12:04:04 -0000 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 Received: (qmail 45952 invoked by uid 89); 17 Oct 2019 12:04:03 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=nathan@acm.org, nathanacmorg, D*acm.org, H*Ad:U*nathan X-HELO: mail-qk1-f173.google.com Received: from mail-qk1-f173.google.com (HELO mail-qk1-f173.google.com) (209.85.222.173) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 17 Oct 2019 12:04:02 +0000 Received: by mail-qk1-f173.google.com with SMTP id p10so1573153qkg.8 for ; Thu, 17 Oct 2019 05:04:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=YZPOpmR2lHZZVnPf7Uchs6az2ry00nEPYRp8mgIy36Q=; b=vJK8TvvxFzpp91I7IhiEw3y2Iz2BveFAKoMWjj4dT+hTz5rJ4ttJiZ+/5Inet1Y7SV ZswtuyV2m5UEtgO7K4NjDT51KfBIlvjZfRzg3FMstSPjQZ9pu4KVhsPbn2Ds+PrV3Nu1 99AylcWXk8FIY9FnMtkhr6V7F4MEaR2kgjK3Mi9Vqcu55jxAB6fvcosFNf4xLLtxw+pi 4wxe88mR+DRZa0uycTYybf3eybODv6ypAbMGMPWJ1x2TzO5ufCNuSsK+bTRsvEdI94Km XwHdL39+rJzPJYUncvKKc6wEa983Z9Tm3YnIJk1Um8B0don2Tr2zuMzCRuKSpkL40OBS JEcw== Received: from ?IPv6:2620:10d:c0a3:1407:f4be:64c8:1512:3eb6? ([2620:10d:c091:500::3:391a]) by smtp.googlemail.com with ESMTPSA id w45sm1245013qtb.47.2019.10.17.05.03.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Oct 2019 05:03:59 -0700 (PDT) To: GCC Patches From: Nathan Sidwell Subject: [C++ PATCH] builtin fn creation Message-ID: <9ed2e759-e361-df6c-62d8-616e744dc551@acm.org> Date: Thu, 17 Oct 2019 08:03:58 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.1.1 MIME-Version: 1.0 We're rather lax about setting DECL_CONTEXT correctly on several classes of builtins. This is affecting modules. But in cleaning it up I noticed cxx_builtin_function & cxx_builtin_function_ext_scope * are doing very similar work (they're identical except for a boolean value). * through a worker that repeats work (builtin_function_1 repeats its analysis on the ::std version). * and the latter incorrectly pushes things intended for ::std into ::, (because it uses pushdecl_top_level (decl)) The bug I encountered was that the version pushed to :: had a NULL context (rather than FROB_CONTEXT (global_namespace), which is the translation unit decl). This patch * implements cxx_builtin_function_ext_scope in terms of cxx_builtin_function, by wrapping that call in a push_nested_namespace (global_namespace). * folds builtin_function_1 into cxx_builtin_function * applies the transforms to the incoming decl before cloning it * uses copy_decl, a c++ decl-specific copy_node, which correctly duplicates the lang-specific data * uses IDENTIFIER_LENGTH rather than strlen * set DECL_CONTEXT to FROB_CONTEXT (current_namespace). applying to trunk. nathan 2019-10-17 Nathan Sidwell * decl.c (builtin_function_1): Merge into ... (cxx_builtin_function): ... here. Nadger the decl before maybe copying it. Set the context. (cxx_builtin_function_ext_scope): Push to top level, then call cxx_builtin_function. Index: decl.c =================================================================== --- decl.c (revision 277098) +++ decl.c (working copy) @@ -72,5 +72,4 @@ static tree grokvardecl (tree, tree, tre static void check_static_variable_definition (tree, tree); static void record_unknown_type (tree, const char *); -static tree builtin_function_1 (tree, tree, bool); static int member_function_or_else (tree, tree, enum overload_flags); static tree local_variable_p_walkfn (tree *, int *, void *); @@ -4630,10 +4629,11 @@ cp_make_fname_decl (location_t loc, tree } -static tree -builtin_function_1 (tree decl, tree context, bool is_global) -{ - tree id = DECL_NAME (decl); - const char *name = IDENTIFIER_POINTER (id); +/* Install DECL as a builtin function at current (global) scope. + Return the new decl (if we found an existing version). Also + installs it into ::std, if it's not '_*'. */ +tree +cxx_builtin_function (tree decl) +{ retrofit_lang_decl (decl); @@ -4645,45 +4645,33 @@ builtin_function_1 (tree decl, tree cont DECL_VISIBILITY_SPECIFIED (decl) = 1; - DECL_CONTEXT (decl) = context; - - /* A function in the user's namespace should have an explicit - declaration before it is used. Mark the built-in function as - anticipated but not actually declared. */ + tree id = DECL_NAME (decl); + const char *name = IDENTIFIER_POINTER (id); if (name[0] != '_' || name[1] != '_') + /* In the user's namespace, it must be declared before use. */ + DECL_ANTICIPATED (decl) = 1; + else if (IDENTIFIER_LENGTH (id) > strlen ("___chk") + && 0 != strncmp (name + 2, "builtin_", strlen ("builtin_")) + && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"), + "_chk", strlen ("_chk") + 1)) + /* Treat __*_chk fortification functions as anticipated as well, + unless they are __builtin_*_chk. */ DECL_ANTICIPATED (decl) = 1; - else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0) - { - size_t len = strlen (name); - - /* Treat __*_chk fortification functions as anticipated as well, - unless they are __builtin_*. */ - if (len > strlen ("___chk") - && memcmp (name + len - strlen ("_chk"), - "_chk", strlen ("_chk") + 1) == 0) - DECL_ANTICIPATED (decl) = 1; - } - - if (is_global) - return pushdecl_top_level (decl); - else - return pushdecl (decl); -} -tree -cxx_builtin_function (tree decl) -{ - tree id = DECL_NAME (decl); - const char *name = IDENTIFIER_POINTER (id); /* All builtins that don't begin with an '_' should additionally go in the 'std' namespace. */ if (name[0] != '_') { - tree decl2 = copy_node(decl); + tree std_decl = copy_decl (decl); + push_namespace (std_identifier); - builtin_function_1 (decl2, std_node, false); + DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node); + pushdecl (std_decl); pop_namespace (); } - return builtin_function_1 (decl, NULL_TREE, false); + DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); + decl = pushdecl (decl); + + return decl; } @@ -4697,18 +4685,9 @@ tree cxx_builtin_function_ext_scope (tree decl) { + push_nested_namespace (global_namespace); + decl = cxx_builtin_function (decl); + pop_nested_namespace (global_namespace); - tree id = DECL_NAME (decl); - const char *name = IDENTIFIER_POINTER (id); - /* All builtins that don't begin with an '_' should additionally - go in the 'std' namespace. */ - if (name[0] != '_') - { - tree decl2 = copy_node(decl); - push_namespace (std_identifier); - builtin_function_1 (decl2, std_node, true); - pop_namespace (); - } - - return builtin_function_1 (decl, NULL_TREE, true); + return decl; }