Skip to content

Commit

Permalink
Merge pull request #5389 from WalterBright/cd-interfaces
Browse files Browse the repository at this point in the history
refactor: make interfaces a proper D array
  • Loading branch information
yebblies committed Feb 1, 2016
2 parents 0011b71 + 2a4ee35 commit 7df77f7
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 34 deletions.
3 changes: 1 addition & 2 deletions src/aggregate.h
Expand Up @@ -262,8 +262,7 @@ class ClassDeclaration : public AggregateDeclaration
BaseClasses *baseclasses; // Array of BaseClass's; first is super,
// rest are Interface's

size_t interfaces_dim;
BaseClass **interfaces; // interfaces[interfaces_dim] for this class
DArray<BaseClass*> interfaces; // interfaces[interfaces_dim] for this class
// (does not include baseClass)

BaseClasses *vtblInterfaces; // array of base interfaces that have
Expand Down
34 changes: 13 additions & 21 deletions src/dclass.d
Expand Up @@ -128,8 +128,8 @@ struct BaseClass
//printf("+copyBaseInterfaces(), %s\n", sym->toChars());
// if (baseInterfaces_dim)
// return;
auto bc = cast(BaseClass*)mem.xcalloc(sym.interfaces_dim, BaseClass.sizeof);
baseInterfaces = bc[0 .. sym.interfaces_dim];
auto bc = cast(BaseClass*)mem.xcalloc(sym.interfaces.length, BaseClass.sizeof);
baseInterfaces = bc[0 .. sym.interfaces.length];
//printf("%s.copyBaseInterfaces()\n", sym->toChars());
for (size_t i = 0; i < baseInterfaces.length; i++)
{
Expand Down Expand Up @@ -199,9 +199,9 @@ public:
// Array of BaseClass's; first is super, rest are Interface's
BaseClasses* baseclasses;

// interfaces[interfaces_dim] for this class (does not include baseClass)
size_t interfaces_dim;
BaseClass** interfaces;
/* Slice of baseclasses[] that does not include baseClass
*/
BaseClass*[] interfaces;

// array of base interfaces that have their own vtbl[]
BaseClasses* vtblInterfaces;
Expand Down Expand Up @@ -679,11 +679,9 @@ public:
storage_class |= baseClass.storage_class & STC_TYPECTOR;
}

interfaces_dim = baseclasses.dim - (baseClass ? 1 : 0);
interfaces = baseclasses.tdata() + (baseClass ? 1 : 0);
for (size_t i = 0; i < interfaces_dim; i++)
interfaces = baseclasses.tdata()[(baseClass ? 1 : 0) .. baseclasses.dim];
foreach (b; interfaces)
{
BaseClass* b = interfaces[i];
// If this is an interface, and it derives from a COM interface,
// then this is a COM interface too.
if (b.sym.isCOMinterface())
Expand Down Expand Up @@ -1324,10 +1322,9 @@ public:
final void interfaceSemantic(Scope* sc)
{
vtblInterfaces = new BaseClasses();
vtblInterfaces.reserve(interfaces_dim);
for (size_t i = 0; i < interfaces_dim; i++)
vtblInterfaces.reserve(interfaces.length);
foreach (b; interfaces)
{
BaseClass* b = interfaces[i];
vtblInterfaces.push(b);
b.copyBaseInterfaces(vtblInterfaces);
}
Expand Down Expand Up @@ -1636,11 +1633,9 @@ public:
}
baseok = BASEOKdone;

interfaces_dim = baseclasses.dim;
interfaces = baseclasses.tdata();
for (size_t i = 0; i < interfaces_dim; i++)
interfaces = baseclasses.tdata()[0 .. baseclasses.dim];
foreach (b; interfaces)
{
BaseClass* b = interfaces[i];
// If this is an interface, and it derives from a COM interface,
// then this is a COM interface too.
if (b.sym.isCOMinterface())
Expand Down Expand Up @@ -1688,10 +1683,8 @@ public:
vtbl.push(this); // leave room at vtbl[0] for classinfo

// Cat together the vtbl[]'s from base interfaces
for (size_t i = 0; i < interfaces_dim; i++)
foreach (i, b; interfaces)
{
BaseClass* b = interfaces[i];

// Skip if b has already appeared
for (size_t k = 0; k < i; k++)
{
Expand Down Expand Up @@ -1811,9 +1804,8 @@ public:
{
//printf("%s.InterfaceDeclaration::isBaseOf(cd = '%s')\n", toChars(), cd->toChars());
assert(!baseClass);
for (size_t j = 0; j < cd.interfaces_dim; j++)
foreach (j, b; cd.interfaces)
{
BaseClass* b = cd.interfaces[j];
//printf("\tX base %s\n", b->sym->toChars());
if (this == b.sym)
{
Expand Down
3 changes: 1 addition & 2 deletions src/dtemplate.d
Expand Up @@ -3995,9 +3995,8 @@ extern (C++) MATCH deduceType(RootObject o, Scope* sc, Type tparam, TemplatePara
// Test the base class
deduceBaseClassParameters(*(*s.baseclasses)[0], sc, tparam, parameters, dedtypes, best, numBaseClassMatches);
// Test the interfaces inherited by the base class
for (size_t i = 0; i < s.interfaces_dim; ++i)
foreach (b; s.interfaces)
{
BaseClass* b = s.interfaces[i];
deduceBaseClassParameters(*b, sc, tparam, parameters, dedtypes, best, numBaseClassMatches);
}
s = (*s.baseclasses)[0].sym;
Expand Down
9 changes: 3 additions & 6 deletions src/func.d
Expand Up @@ -1041,9 +1041,8 @@ public:
* functions, set the tintro.
*/
Linterfaces:
for (size_t i = 0; i < cd.interfaces_dim; i++)
foreach (b; cd.interfaces)
{
BaseClass* b = cd.interfaces[i];
vi = findVtblIndex(cast(Dsymbols*)&b.sym.vtbl, cast(int)b.sym.vtbl.dim);
switch (vi)
{
Expand Down Expand Up @@ -1123,9 +1122,8 @@ public:
/* Go through all the interface bases.
* Disallow overriding any final functions in the interface(s).
*/
for (size_t i = 0; i < cd.interfaces_dim; i++)
foreach (b; cd.interfaces)
{
BaseClass* b = cd.interfaces[i];
if (b.sym)
{
Dsymbol s = search_function(b.sym, ident);
Expand Down Expand Up @@ -2488,9 +2486,8 @@ public:
final BaseClass* overrideInterface()
{
ClassDeclaration cd = parent.isClassDeclaration();
for (size_t i = 0; i < cd.interfaces_dim; i++)
foreach (b; cd.interfaces)
{
BaseClass* b = cd.interfaces[i];
auto v = findVtblIndex(cast(Dsymbols*)&b.sym.vtbl, cast(int)b.sym.vtbl.dim);
if (v >= 0)
return b;
Expand Down
8 changes: 8 additions & 0 deletions src/globals.h
Expand Up @@ -243,6 +243,14 @@ typedef longdouble d_float80;

typedef longdouble real_t;

// Represents a D [ ] array
template<typename T>
struct DArray
{
size_t length;
T *ptr;
};

// file location
struct Loc
{
Expand Down
5 changes: 2 additions & 3 deletions src/json.d
Expand Up @@ -586,13 +586,12 @@ public:
{
property("base", cd.baseClass.toPrettyChars(true));
}
if (cd.interfaces_dim)
if (cd.interfaces.length)
{
propertyStart("interfaces");
arrayStart();
for (size_t i = 0; i < cd.interfaces_dim; i++)
foreach (b; cd.interfaces)
{
BaseClass* b = cd.interfaces[i];
item(b.sym.toPrettyChars(true));
}
arrayEnd();
Expand Down

0 comments on commit 7df77f7

Please sign in to comment.