Skip to content
This repository has been archived by the owner on Jun 20, 2019. It is now read-only.

Commit

Permalink
Merge pull request #247 from ibuclaw/bug242
Browse files Browse the repository at this point in the history
Bug 242: Do constant propagation of enums when frontend doesn't.
  • Loading branch information
ibuclaw authored Oct 3, 2016
2 parents ef3141d + bb7c50d commit 1cca265
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
7 changes: 7 additions & 0 deletions gcc/d/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2016-10-03 Iain Buclaw <ibuclaw@gdcproject.org>

* d-objfile.cc (VarDeclaration::toObjFile): Always generate
DECL_INITIAL for all kinds of var decls.
* d-codegen.cc (build_address): Use the DECL_INITIAL directly if
taking the address of a CONST_DECL.

2016-09-26 Johannes Pfau <johannespfau@gmail.com>

* d-objfile.cc (d_finish_function): Handle template mixins (issue 231).
Expand Down
6 changes: 5 additions & 1 deletion gcc/d/d-codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1734,9 +1734,13 @@ build_address(tree exp)
else
ptrtype = build_pointer_type(type);

// Maybe rewrite: (e1, e2) => (e1, &e2)
// Maybe rewrite: &(e1, e2) => (e1, &e2)
tree init = stabilize_expr(&exp);

// Can't take the address of a manifest constant, instead use its value.
if (TREE_CODE (exp) == CONST_DECL)
exp = DECL_INITIAL (exp);

d_mark_addressable(exp);
exp = build_fold_addr_expr_with_type_loc(input_location, exp, ptrtype);

Expand Down
22 changes: 13 additions & 9 deletions gcc/d/d-objfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -826,19 +826,23 @@ VarDeclaration::toObjFile()
if (isInstantiated())
return;

// CONST_DECL was initially intended for enumerals and may
// be used for scalars in general but not for aggregates.
if (!type->isscalar())
return;

tree decl = toSymbol()->Stree;
gcc_assert (init && !init->isVoidInitializer());

Expression *ie = init->toExpression();
DECL_INITIAL (decl) = build_expr(ie, true);

d_pushdecl (decl);
rest_of_decl_compilation (decl, 1, 0);
// CONST_DECL was initially intended for enumerals and may be used for
// scalars in general, but not for aggregates. Here a non-constant value
// is generated anyway so as the CONST_DECL only serves as a placeholder
// for the value, however the DECL itself should never be referenced in
// any generated code, or passed to the backend.
if (!type->isscalar())
DECL_INITIAL (decl) = build_expr(ie, false);
else
{
DECL_INITIAL (decl) = build_expr(ie, true);
d_pushdecl (decl);
rest_of_decl_compilation (decl, 1, 0);
}
}
else if (isDataseg() && !(storage_class & STCextern))
{
Expand Down
23 changes: 23 additions & 0 deletions gcc/testsuite/gdc.test/compilable/gdc242.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// PERMUTE_ARGS:

struct S242a
{
enum M = S242a();
void iter() { }
}

void test242a()
{
return S242a.M.iter;
}

struct S242b
{
enum M = S242b();
void iter() { }
}

void test242b()
{
S242b.M.iter;
}
26 changes: 26 additions & 0 deletions gcc/testsuite/gdc.test/runnable/gdc.d
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,32 @@ void test210()

/******************************************/

// Bug 242

struct S242
{
enum M = S242();
int a = 42;

auto iter()
{
this.a = 24;
return this;
}
}

S242 test242a()
{
return S242.M.iter;
}

void test242()
{
assert(test242a() == S242(24));
}

/******************************************/

void main()
{
test2();
Expand Down

0 comments on commit 1cca265

Please sign in to comment.