Skip to content

Commit

Permalink
Merge pull request #5616 from 9rnsr/ismember
Browse files Browse the repository at this point in the history
Remove identical but different named functions
  • Loading branch information
WalterBright committed Apr 12, 2016
2 parents 8373123 + 7363d55 commit 36a2929
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 65 deletions.
1 change: 0 additions & 1 deletion src/declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ class FuncDeclaration : public Declaration
MATCH leastAsSpecialized(FuncDeclaration *g);
LabelDsymbol *searchLabel(Identifier *ident);
AggregateDeclaration *isThis();
AggregateDeclaration *isMember2();
int getLevel(Loc loc, Scope *sc, FuncDeclaration *fd); // lexical nesting level difference
const char *toPrettyChars(bool QualifyTypes = false);
const char *toFullSignature(); // for diagnostics, e.g. 'int foo(int x, int y) pure'
Expand Down
2 changes: 1 addition & 1 deletion src/dscope.d
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ struct Scope
FuncDeclaration f = func;
if (fes)
f = fes.func;
AggregateDeclaration ad = f.isAggregateMember2();
auto ad = f.isMember2();
assert(ad);
for (size_t i = 0; i < ad.fields.dim; i++)
{
Expand Down
87 changes: 52 additions & 35 deletions src/dsymbol.d
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,42 @@ public:
return s;
}

/**********************************
* `parent` field returns a lexically enclosing scope symbol this is a member of.
*
* `toParent()` returns a logically enclosing scope symbol this is a member of.
* It skips over TemplateMixin's.
*
* `toParent2()` returns an enclosing scope symbol this is living at runtime.
* It skips over both TemplateInstance's and TemplateMixin's.
* It's used when looking for the 'this' pointer of the enclosing function/class.
*
* Examples:
* module mod;
* template Foo(alias a) { mixin Bar!(); }
* mixin template Bar() {
* public { // ProtDeclaration
* void baz() { a = 2; }
* }
* }
* void test() {
* int v = 1;
* alias foo = Foo!(v);
* foo.baz();
* assert(v == 2);
* }
*
* // s == FuncDeclaration('mod.test.Foo!().Bar!().baz()')
* // s.parent == TemplateMixin('mod.test.Foo!().Bar!()')
* // s.toParent() == TemplateInstance('mod.test.Foo!()')
* // s.toParent2() == FuncDeclaration('mod.test')
*/
final Dsymbol toParent()
{
return parent ? parent.pastMixin() : null;
}

/**********************************
* Use this instead of toParent() when looking for the
* 'this' pointer of the enclosing function/class.
* This skips over both TemplateInstance's and TemplateMixin's.
*/
/// ditto
final Dsymbol toParent2()
{
Dsymbol s = parent;
Expand Down Expand Up @@ -772,31 +798,6 @@ public:
return null;
}

// are we a member of an aggregate?
final AggregateDeclaration isAggregateMember()
{
Dsymbol parent = toParent();
if (parent && parent.isAggregateDeclaration())
return cast(AggregateDeclaration)parent;
return null;
}

// are we a member of an aggregate?
final AggregateDeclaration isAggregateMember2()
{
Dsymbol parent = toParent2();
if (parent && parent.isAggregateDeclaration())
return cast(AggregateDeclaration)parent;
return null;
}

// are we a member of a class?
final ClassDeclaration isClassMember()
{
AggregateDeclaration ad = isAggregateMember();
return ad ? ad.isClassDeclaration() : null;
}

// is Dsymbol exported?
bool isExport()
{
Expand Down Expand Up @@ -826,13 +827,29 @@ public:
return null;
}

// is this a member of an AggregateDeclaration?
AggregateDeclaration isMember()
/// Returns an AggregateDeclaration when toParent() is that.
final AggregateDeclaration isMember()
{
//printf("Dsymbol::isMember() %s\n", toChars());
Dsymbol parent = toParent();
//printf("parent is %s %s\n", parent->kind(), parent->toChars());
return parent ? parent.isAggregateDeclaration() : null;
auto p = toParent();
//printf("parent is %s %s\n", p.kind(), p.toChars());
return p ? p.isAggregateDeclaration() : null;
}

/// Returns an AggregateDeclaration when toParent2() is that.
final AggregateDeclaration isMember2()
{
//printf("Dsymbol::isMember2() '%s'\n", toChars());
auto p = toParent2();
//printf("parent is %s %s\n", p.kind(), p.toChars());
return p ? p.isAggregateDeclaration() : null;
}

// is this a member of a ClassDeclaration?
final ClassDeclaration isClassMember()
{
auto ad = isMember();
return ad ? ad.isClassDeclaration() : null;
}

// is this a type?
Expand Down
7 changes: 3 additions & 4 deletions src/dsymbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,14 @@ class Dsymbol : public RootObject
virtual unsigned size(Loc loc);
virtual bool isforwardRef();
virtual AggregateDeclaration *isThis(); // is a 'this' required to access the member
AggregateDeclaration *isAggregateMember(); // are we a member of an aggregate?
AggregateDeclaration *isAggregateMember2(); // are we a member of an aggregate?
ClassDeclaration *isClassMember(); // are we a member of a class?
virtual bool isExport(); // is Dsymbol exported?
virtual bool isImportedSymbol(); // is Dsymbol imported?
virtual bool isDeprecated(); // is Dsymbol deprecated?
virtual bool isOverloadable();
virtual LabelDsymbol *isLabel(); // is this a LabelDsymbol?
virtual AggregateDeclaration *isMember(); // is this symbol a member of an AggregateDeclaration?
AggregateDeclaration *isMember(); // is this a member of an AggregateDeclaration?
AggregateDeclaration *isMember2(); // is this a member of an AggregateDeclaration?
ClassDeclaration *isClassMember(); // is this a member of a ClassDeclaration?
virtual Type *getType(); // is this a type?
virtual bool needThis(); // need a 'this' pointer?
virtual Prot prot();
Expand Down
2 changes: 1 addition & 1 deletion src/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -2068,7 +2068,7 @@ extern (C++) int modifyFieldVar(Loc loc, Scope* sc, VarDeclaration var, Expressi
assert(e1);
bool mustInit = (var.storage_class & STCnodefaultctor || var.type.needsNested());
size_t dim = sc.fieldinit_dim;
AggregateDeclaration ad = fd.isAggregateMember2();
auto ad = fd.isMember2();
assert(ad);
size_t i;
for (i = 0; i < dim; i++) // same as findFieldIndexByName in ctfeexp.c ?
Expand Down
23 changes: 1 addition & 22 deletions src/func.d
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ public:
auto sym = new ScopeDsymbol();
sym.parent = sc2.scopesym;
sc2 = sc2.push(sym);
AggregateDeclaration ad2 = isAggregateMember2();
auto ad2 = isMember2();
/* If this is a class constructor
*/
if (ad2 && isCtorDeclaration())
Expand Down Expand Up @@ -2889,27 +2889,6 @@ public:
return ad;
}

final AggregateDeclaration isMember2()
{
//printf("+FuncDeclaration::isMember2() '%s'\n", toChars());
AggregateDeclaration ad = null;
for (Dsymbol s = this; s; s = s.parent)
{
//printf("\ts = '%s', parent = '%s', kind = %s\n", s->toChars(), s->parent->toChars(), s->parent->kind());
ad = s.isMember();
if (ad)
{
break;
}
if (!s.parent || (!s.parent.isTemplateInstance()))
{
break;
}
}
//printf("-FuncDeclaration::isMember2() %p\n", ad);
return ad;
}

/*****************************************
* Determine lexical level difference from 'this' to nested function 'fd'.
* Error if this cannot call fd.
Expand Down
2 changes: 1 addition & 1 deletion src/statement.d
Original file line number Diff line number Diff line change
Expand Up @@ -4544,7 +4544,7 @@ public:
sc.callSuper |= CSXreturn;
if (sc.fieldinit)
{
AggregateDeclaration ad = fd.isAggregateMember2();
auto ad = fd.isMember2();
assert(ad);
size_t dim = sc.fieldinit_dim;
foreach (i; 0 .. dim)
Expand Down

0 comments on commit 36a2929

Please sign in to comment.