diff mbox series

[committed] d: Use new isXxxxExp helpers where possible

Message ID 20200616215933.3858164-8-ibuclaw@gdcproject.org
State New
Headers show
Series [committed] d: Use new isXxxxExp helpers where possible | expand

Commit Message

Iain Buclaw June 16, 2020, 9:59 p.m. UTC
Hi,

This patch replaces uses of casting to Expression nodes with the newly
introduced isXxxxExp functions.

Bootstrapped and regression tested on x86_64-linux-gnu, and committed to
master.

Regards
Iain


gcc/d/ChangeLog:

	* d-attribs.cc (build_attributes): Use isXxxxExp helpers instead of
	explicit casts.
	* d-codegen.cc (d_build_call): Likewise.
	* d-compiler.cc (Compiler::paintAsType): Likewise.
	* decl.cc (ExprVisitor::visit): Likewise.
	(layout_class_initializer): Likewise.
	* expr.cc (ExprVisitor::lvalue_p): Likewise
	(ExprVisitor::visit): Likewise.
	* types.cc (layout_aggregate_members): Likewise.
---
 gcc/d/d-attribs.cc  |  6 ++---
 gcc/d/d-codegen.cc  |  2 +-
 gcc/d/d-compiler.cc |  4 +--
 gcc/d/decl.cc       | 11 ++++----
 gcc/d/expr.cc       | 62 +++++++++++++++++++++++++--------------------
 gcc/d/types.cc      |  2 +-
 6 files changed, 46 insertions(+), 41 deletions(-)
diff mbox series

Patch

diff --git a/gcc/d/d-attribs.cc b/gcc/d/d-attribs.cc
index b83db95e2a7..7e55b42fd03 100644
--- a/gcc/d/d-attribs.cc
+++ b/gcc/d/d-attribs.cc
@@ -268,7 +268,7 @@  build_attributes (Expressions *eattrs)
       /* Should now have a struct `Attribute("attrib", "value", ...)'
 	 initializer list.  */
       gcc_assert (attr->op == TOKstructliteral);
-      Expressions *elems = ((StructLiteralExp*) attr)->elements;
+      Expressions *elems = attr->isStructLiteralExp ()->elements;
       Expression *e0 = (*elems)[0];
 
       if (e0->op != TOKstring)
@@ -300,10 +300,10 @@  build_attributes (Expressions *eattrs)
       for (size_t j = 1; j < elems->length; j++)
 	{
 	  Expression *e = (*elems)[j];
+	  StringExp *s = e->isStringExp ();
 	  tree t;
-	  if (e->op == TOKstring && ((StringExp *) e)->sz == 1)
+	  if (s != NULL && s->sz == 1)
 	    {
-	      StringExp *s = (StringExp *) e;
 	      const char *string = (const char *)(s->len ? s->string : "");
 	      t = build_string (s->len, string);
 	    }
diff --git a/gcc/d/d-codegen.cc b/gcc/d/d-codegen.cc
index 1bf74e1f223..d31638e2c1d 100644
--- a/gcc/d/d-codegen.cc
+++ b/gcc/d/d-codegen.cc
@@ -1874,7 +1874,7 @@  d_build_call (TypeFunction *tf, tree callable, tree object,
 
 	  if (arg->op == TOKcomma)
 	    {
-	      CommaExp *ce = (CommaExp *) arg;
+	      CommaExp *ce = arg->isCommaExp ();
 	      tree tce = build_expr (ce->e1);
 	      saved_args = compound_expr (saved_args, tce);
 	      (*arguments)[i] = ce->e2;
diff --git a/gcc/d/d-compiler.cc b/gcc/d/d-compiler.cc
index 75ca15737d9..41b3b53207a 100644
--- a/gcc/d/d-compiler.cc
+++ b/gcc/d/d-compiler.cc
@@ -88,7 +88,7 @@  Compiler::paintAsType (UnionExp *, Expression *expr, Type *type)
   else if (expr->op == TOKarrayliteral)
     {
       /* Build array as VECTOR_CST, assumes EXPR is constant.  */
-      Expressions *elements = ((ArrayLiteralExp *) expr)->elements;
+      Expressions *elements = expr->isArrayLiteralExp ()->elements;
       vec<constructor_elt, va_gc> *elms = NULL;
 
       vec_safe_reserve (elms, elements->length);
@@ -136,7 +136,7 @@  Compiler::paintAsType (UnionExp *, Expression *expr, Type *type)
       Expression *e = d_eval_constant_expression (cst);
       gcc_assert (e != NULL && e->op == TOKvector);
 
-      return ((VectorExp *) e)->e1;
+      return e->isVectorExp ()->e1;
     }
   else
     {
diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc
index a6144f7c269..28d1d6d5f31 100644
--- a/gcc/d/decl.cc
+++ b/gcc/d/decl.cc
@@ -218,12 +218,11 @@  public:
     for (size_t i = 0; i < d->objects->length; i++)
       {
 	RootObject *o = (*d->objects)[i];
-	if ((o->dyncast () == DYNCAST_EXPRESSION)
-	    && ((Expression *) o)->op == TOKdsymbol)
+	if (o->dyncast () == DYNCAST_EXPRESSION)
 	  {
-	    Declaration *d = ((DsymbolExp *) o)->s->isDeclaration ();
-	    if (d)
-	      this->build_dsymbol (d);
+	    DsymbolExp *de = ((Expression *) o)->isDsymbolExp ();
+	    if (de != NULL && de->s->isDeclaration ())
+	      this->build_dsymbol (de->s);
 	  }
       }
   }
@@ -2239,7 +2238,7 @@  layout_class_initializer (ClassDeclaration *cd)
   Expression *e = ne->ctfeInterpret ();
   gcc_assert (e->op == TOKclassreference);
 
-  return build_class_instance ((ClassReferenceExp *) e);
+  return build_class_instance (e->isClassReferenceExp ());
 }
 
 tree
diff --git a/gcc/d/expr.cc b/gcc/d/expr.cc
index 9c80db15552..06cd83a737d 100644
--- a/gcc/d/expr.cc
+++ b/gcc/d/expr.cc
@@ -90,9 +90,15 @@  class ExprVisitor : public Visitor
 
   bool lvalue_p (Expression *e)
   {
-    return ((e->op != TOKslice && e->isLvalue ())
-	    || (e->op == TOKslice && ((UnaExp *) e)->e1->isLvalue ())
-	    || (e->op == TOKcast && ((UnaExp *) e)->e1->isLvalue ()));
+    SliceExp *se = e->isSliceExp ();
+    if (se != NULL && se->e1->isLvalue ())
+      return true;
+
+    CastExp *ce = e->isCastExp ();
+    if (ce != NULL && ce->e1->isLvalue ())
+      return true;
+
+    return (e->op != TOKslice && e->isLvalue ());
   }
 
   /* Build an expression of code CODE, data type TYPE, and operands ARG0 and
@@ -174,7 +180,7 @@  class ExprVisitor : public Visitor
     Expression *e1b = e1;
     while (e1b->op == TOKcast)
       {
-	CastExp *ce = (CastExp *) e1b;
+	CastExp *ce = e1b->isCastExp ();
 	gcc_assert (same_type_p (ce->type, ce->to));
 	e1b = ce->e1;
       }
@@ -679,7 +685,7 @@  public:
 	  {
 	    if (ex->op == TOKcat)
 	      {
-		ex = ((CatExp *) ex)->e1;
+		ex = ex->isCatExp ()->e1;
 		ndims++;
 	      }
 	  }
@@ -696,7 +702,7 @@  public:
 	for (Expression *oe = ce->e2; oe != NULL;
 	     (ce->e1->op != TOKcat
 	      ? (oe = ce->e1)
-	      : (ce = (CatExp *)ce->e1, oe = ce->e2)))
+	      : (ce = ce->e1->isCatExp (), oe = ce->e2)))
 	  {
 	    tree arg = d_array_convert (etype, oe);
 	    tree index = size_int (dim);
@@ -790,7 +796,7 @@  public:
 	   Strip off casts just incase anyway.  */
 	while (e1b->op == TOKcast)
 	  {
-	    CastExp *ce = (CastExp *) e1b;
+	    CastExp *ce = e1b->isCastExp ();
 	    gcc_assert (same_type_p (ce->type, ce->to));
 	    e1b = ce->e1;
 	  }
@@ -883,7 +889,7 @@  public:
     if (e->e1->op == TOKarraylength)
       {
 	/* Assignment to an array's length property; resize the array.  */
-	ArrayLengthExp *ale = (ArrayLengthExp *) e->e1;
+	ArrayLengthExp *ale = e->e1->isArrayLengthExp ();
 	tree newlength = convert_expr (build_expr (e->e2), e->e2->type,
 				       Type::tsize_t);
 	tree ptr = build_address (build_expr (ale->e1));
@@ -904,7 +910,7 @@  public:
     /* Look for array[] = n;  */
     if (e->e1->op == TOKslice)
       {
-	SliceExp *se = (SliceExp *) e->e1;
+	SliceExp *se = e->e1->isSliceExp ();
 	Type *stype = se->e1->type->toBasetype ();
 	Type *etype = stype->nextOf ()->toBasetype ();
 
@@ -998,7 +1004,7 @@  public:
 	gcc_assert (e->op == TOKconstruct || e->op == TOKblit);
 	gcc_assert (e->e1->op == TOKvar);
 
-	Declaration *decl = ((VarExp *) e->e1)->var;
+	Declaration *decl = e->e1->isVarExp ()->var;
 	if (decl->storage_class & (STCout | STCref))
 	  {
 	    tree t2 = convert_for_assignment (build_expr (e->e2),
@@ -1443,7 +1449,7 @@  public:
 
 	if (e->e1->op == TOKvar)
 	  {
-	    VarDeclaration *v = ((VarExp *) e->e1)->var->isVarDeclaration ();
+	    VarDeclaration *v = e->e1->isVarExp ()->var->isVarDeclaration ();
 	    if (v && v->onstack)
 	      {
 		libcall = tb1->isClassHandle ()->isInterfaceDeclaration ()
@@ -1588,19 +1594,19 @@  public:
 
     if (e->e1->op == TOKadd)
       {
-	BinExp *be = (BinExp *) e->e1;
-	if (be->e1->op == TOKaddress
-	    && be->e2->isConst () && be->e2->type->isintegral ())
+	AddExp *ae = e->e1->isAddExp ();
+	if (ae->e1->op == TOKaddress
+	    && ae->e2->isConst () && ae->e2->type->isintegral ())
 	  {
-	    Expression *ae = ((AddrExp *) be->e1)->e1;
-	    tnext = ae->type->toBasetype ();
-	    result = build_expr (ae);
-	    offset = be->e2->toUInteger ();
+	    Expression *ex = ae->e1->isAddrExp ()->e1;
+	    tnext = ex->type->toBasetype ();
+	    result = build_expr (ex);
+	    offset = ae->e2->toUInteger ();
 	  }
       }
     else if (e->e1->op == TOKsymoff)
       {
-	SymOffExp *se = (SymOffExp *) e->e1;
+	SymOffExp *se = e->e1->isSymOffExp ();
 	if (!declaration_reference_p (se->var))
 	  {
 	    tnext = se->var->type->toBasetype ();
@@ -1652,7 +1658,7 @@  public:
        Taking the address of a struct literal is otherwise illegal.  */
     if (e->e1->op == TOKstructliteral)
       {
-	StructLiteralExp *sle = ((StructLiteralExp *) e->e1)->origin;
+	StructLiteralExp *sle = e->e1->isStructLiteralExp ()->origin;
 	gcc_assert (sle != NULL);
 
 	/* Build the reference symbol, the decl is built first as the
@@ -1690,21 +1696,21 @@  public:
     /* Calls to delegates can sometimes look like this.  */
     if (e1b->op == TOKcomma)
       {
-	e1b = ((CommaExp *) e1b)->e2;
+	e1b = e1b->isCommaExp ()->e2;
 	gcc_assert (e1b->op == TOKvar);
 
-	Declaration *var = ((VarExp *) e1b)->var;
+	Declaration *var = e1b->isVarExp ()->var;
 	gcc_assert (var->isFuncDeclaration () && !var->needThis ());
       }
 
     if (e1b->op == TOKdotvar && tb->ty != Tdelegate)
       {
-	DotVarExp *dve = (DotVarExp *) e1b;
+	DotVarExp *dve = e1b->isDotVarExp ();
 
 	/* Don't modify the static initializer for struct literals.  */
 	if (dve->e1->op == TOKstructliteral)
 	  {
-	    StructLiteralExp *sle = (StructLiteralExp *) dve->e1;
+	    StructLiteralExp *sle = dve->e1->isStructLiteralExp ();
 	    sle->useStaticInit = false;
 	  }
 
@@ -1767,7 +1773,7 @@  public:
 	    /* This gets the true function type, getting the function type
 	       from e1->type can sometimes be incorrect, such as when calling
 	       a 'ref' return function.  */
-	    tf = get_function_type (((DotVarExp *) e1b)->var->type);
+	    tf = get_function_type (e1b->isDotVarExp ()->var->type);
 	  }
 	else
 	  tf = get_function_type (tb);
@@ -1784,7 +1790,7 @@  public:
       }
     else if (e1b->op == TOKvar)
       {
-	FuncDeclaration *fd = ((VarExp *) e1b)->var->isFuncDeclaration ();
+	FuncDeclaration *fd = e1b->isVarExp ()->var->isFuncDeclaration ();
 	gcc_assert (fd != NULL);
 	tf = get_function_type (fd->type);
 
@@ -2145,7 +2151,7 @@  public:
   void visit (SymOffExp *e)
   {
     /* Build the address and offset of the symbol.  */
-    size_t soffset = ((SymOffExp *) e)->offset;
+    size_t soffset = e->isSymOffExp ()->offset;
     tree result = get_decl_tree (e->var);
     TREE_USED (result) = 1;
 
@@ -2956,7 +2962,7 @@  public:
     /* First handle array literal expressions.  */
     if (e->e1->op == TOKarrayliteral)
       {
-	ArrayLiteralExp *ale = ((ArrayLiteralExp *) e->e1);
+	ArrayLiteralExp *ale = e->e1->isArrayLiteralExp ();
 	vec<constructor_elt, va_gc> *elms = NULL;
 	bool constant_p = true;
 
diff --git a/gcc/d/types.cc b/gcc/d/types.cc
index cb9a6dec7e5..ba7e29a12e1 100644
--- a/gcc/d/types.cc
+++ b/gcc/d/types.cc
@@ -292,7 +292,7 @@  layout_aggregate_members (Dsymbols *members, tree context, bool inherited_p)
 		  gcc_assert (ro->dyncast () == DYNCAST_EXPRESSION);
 		  Expression *e = (Expression *) ro;
 		  gcc_assert (e->op == TOKdsymbol);
-		  DsymbolExp *se = (DsymbolExp *) e;
+		  DsymbolExp *se = e->isDsymbolExp ();
 
 		  tmembers.push (se->s);
 		}