From patchwork Tue Jan 15 23:03:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1025535 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-494107-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gdcproject.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="LW8GK+US"; 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 43fQqw4N1nz9sBZ for ; Wed, 16 Jan 2019 10:03:38 +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 :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=MEeBDNiDELr7NOeM+wx4TMLN/bgVcs23m8bGXhc/SUu/FV wBUjkCqax1F7tikz6/1oWpG31l67pc0Ts1exlcZD46a3EOPOQpXkMccXChjvgzKI /jpTZojmRcroHwDbO9L3PiAXxYYIjBRXMhbp4J1X6qKh2g++c2cUNqyLmlnQk= 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 :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=z4XLxbp8iv1ViJrXtwkGQ0gLxoU=; b=LW8GK+USb2md+1/NXbqa jKUF847riyThQoKR62o2ZOpqWBemH1nGlbqnSum8zWXmPHZMztYFX0Rv/TLbtD87 2RG3pFqBZICFlmMjJJld8hgOXQcTTFWR6DF2DiWZLQv609WAwVpCKIjrvYNRsNbR lG0XFj2xktoY0lfWavHX6X8= Received: (qmail 22493 invoked by alias); 15 Jan 2019 23:03:31 -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 22480 invoked by uid 89); 15 Jan 2019 23:03:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=20190116, retrieve, interior, visit X-HELO: mail-qt1-f175.google.com Received: from mail-qt1-f175.google.com (HELO mail-qt1-f175.google.com) (209.85.160.175) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 15 Jan 2019 23:03:27 +0000 Received: by mail-qt1-f175.google.com with SMTP id y20so4964171qtm.13 for ; Tue, 15 Jan 2019 15:03:27 -0800 (PST) MIME-Version: 1.0 From: Iain Buclaw Date: Wed, 16 Jan 2019 00:03:13 +0100 Message-ID: Subject: [PATCH, d] Committed refactor building typeof(null) value. To: gcc-patches X-IsSubscribed: yes Hi, This patch moves into one function building the typeof(null) values in the D front-end. Bootstrapped and regression tested on x86_64-linux-gnu. Committed to trunk as r267955. diff --git a/gcc/d/d-codegen.cc b/gcc/d/d-codegen.cc index e62d1f9d3b3..7ca0acffcc4 100644 --- a/gcc/d/d-codegen.cc +++ b/gcc/d/d-codegen.cc @@ -448,6 +448,42 @@ extract_from_method_call (tree t, tree& callee, tree& object) callee = CONSTRUCTOR_ELT (t, 1)->value; } +/* Build a typeof(null) constant of type TYPE. Handles certain special case + conversions, where the underlying type is an aggregate with a nullable + interior pointer. */ + +tree +build_typeof_null_value (Type *type) +{ + Type *tb = type->toBasetype (); + tree value; + + /* For dynamic arrays, set length and pointer fields to zero. */ + if (tb->ty == Tarray) + value = d_array_value (build_ctype (type), size_int (0), null_pointer_node); + + /* For associative arrays, set the pointer field to null. */ + else if (tb->ty == Taarray) + { + tree ctype = build_ctype (type); + gcc_assert (TYPE_ASSOCIATIVE_ARRAY (ctype)); + + value = build_constructor_single (ctype, TYPE_FIELDS (ctype), + null_pointer_node); + } + + /* For delegates, set the frame and function pointer fields to null. */ + else if (tb->ty == Tdelegate) + value = build_delegate_cst (null_pointer_node, null_pointer_node, type); + + /* Simple zero constant for all other types. */ + else + value = build_zero_cst (build_ctype (type)); + + TREE_CONSTANT (value) = 1; + return value; +} + /* Build a dereference into the virtual table for OBJECT to retrieve a function pointer of type FNTYPE at position INDEX. */ diff --git a/gcc/d/d-convert.cc b/gcc/d/d-convert.cc index 0f3cb7ad0c5..e9aa457d852 100644 --- a/gcc/d/d-convert.cc +++ b/gcc/d/d-convert.cc @@ -560,18 +560,12 @@ convert_expr (tree exp, Type *etype, Type *totype) case Tnull: /* Casting from typeof(null) is represented as all zeros. */ - if (tbtype->ty == Tarray) - { - tree ptrtype = build_ctype (tbtype->nextOf ()->pointerTo ()); - return d_array_value (build_ctype (totype), size_int (0), - build_nop (ptrtype, exp)); - } - else if (tbtype->ty == Taarray) - return build_constructor (build_ctype (totype), NULL); - else if (tbtype->ty == Tdelegate) - return build_delegate_cst (exp, null_pointer_node, totype); + result = build_typeof_null_value (totype); - return build_zero_cst (build_ctype (totype)); + /* Make sure the expression is still evaluated if necessary. */ + if (TREE_SIDE_EFFECTS (exp)) + result = compound_expr (exp, result); + break; case Tvector: if (tbtype->ty == Tsarray) diff --git a/gcc/d/d-tree.h b/gcc/d/d-tree.h index adab1d1f892..6ffb0f32a1f 100644 --- a/gcc/d/d-tree.h +++ b/gcc/d/d-tree.h @@ -511,6 +511,7 @@ extern tree delegate_object (tree); extern tree build_delegate_cst (tree, tree, Type *); extern tree build_method_call (tree, tree, Type *); extern void extract_from_method_call (tree, tree &, tree &); +extern tree build_typeof_null_value (Type *); extern tree build_vindex_ref (tree, tree, size_t); extern tree d_save_expr (tree); extern tree stabilize_expr (tree *); diff --git a/gcc/d/expr.cc b/gcc/d/expr.cc index fecdffde020..15754a1dc2e 100644 --- a/gcc/d/expr.cc +++ b/gcc/d/expr.cc @@ -2941,33 +2941,7 @@ public: void visit (NullExp *e) { - Type *tb = e->type->toBasetype (); - tree value; - - /* Handle certain special case conversions, where the underlying type is an - aggregate with a nullable interior pointer. */ - if (tb->ty == Tarray) - { - /* For dynamic arrays, set length and pointer fields to zero. */ - value = d_array_value (build_ctype (e->type), size_int (0), - null_pointer_node); - } - else if (tb->ty == Taarray) - { - /* For associative arrays, set the pointer field to null. */ - value = build_constructor (build_ctype (e->type), NULL); - } - else if (tb->ty == Tdelegate) - { - /* For delegates, set the frame and function pointer to null. */ - value = build_delegate_cst (null_pointer_node, - null_pointer_node, e->type); - } - else - value = d_convert (build_ctype (e->type), integer_zero_node); - - TREE_CONSTANT (value) = 1; - this->result_ = value; + this->result_ = build_typeof_null_value (e->type); } /* Build a vector literal. */