Skip to content

Commit

Permalink
[Refactoring] Tweak code style
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Aug 28, 2014
1 parent 725c097 commit c4a0d80
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 65 deletions.
33 changes: 16 additions & 17 deletions src/attrib.c
Expand Up @@ -541,11 +541,11 @@ char *LinkDeclaration::toChars()
/********************************* ProtDeclaration ****************************/

/**
* Params:
* loc = source location of attribute token
* p = protection attribute data
* decl = declarations which are affected by this protection attribute
*/
* Params:
* loc = source location of attribute token
* p = protection attribute data
* decl = declarations which are affected by this protection attribute
*/
ProtDeclaration::ProtDeclaration(Loc loc, Prot p, Dsymbols *decl)
: AttribDeclaration(decl)
{
Expand All @@ -556,11 +556,11 @@ ProtDeclaration::ProtDeclaration(Loc loc, Prot p, Dsymbols *decl)
}

/**
* Params:
* loc = source location of attribute token
* pkg_identifiers = list of identifiers for a qualified package name
* decl = declarations which are affected by this protection attribute
*/
* Params:
* loc = source location of attribute token
* pkg_identifiers = list of identifiers for a qualified package name
* decl = declarations which are affected by this protection attribute
*/
ProtDeclaration::ProtDeclaration(Loc loc, Identifiers* pkg_identifiers, Dsymbols *decl)
: AttribDeclaration(decl)
{
Expand All @@ -574,9 +574,9 @@ Dsymbol *ProtDeclaration::syntaxCopy(Dsymbol *s)
{
assert(!s);
if (protection.kind == PROTpackage)
return new ProtDeclaration(this->loc, pkg_identifiers, Dsymbol::arraySyntaxCopy(decl));
return new ProtDeclaration(this->loc, pkg_identifiers, Dsymbol::arraySyntaxCopy(decl));
else
return new ProtDeclaration(this->loc, protection, Dsymbol::arraySyntaxCopy(decl));
return new ProtDeclaration(this->loc, protection, Dsymbol::arraySyntaxCopy(decl));
}

Scope *ProtDeclaration::newScope(Scope *sc)
Expand All @@ -598,31 +598,30 @@ void ProtDeclaration::semantic(Scope* sc)

AttribDeclaration::semantic(sc);

if ((protection.kind == PROTpackage) && (protection.pkg != NULL) && sc->module)
if (protection.kind == PROTpackage && protection.pkg && sc->module)
{
Module *m = sc->module;
Package* pkg = m->parent ? m->parent->isPackage() : NULL;
Package* pkg = m->parent ? m->parent->isPackage() : NULL;
if (!pkg || !protection.pkg->isAncestorPackageOf(pkg))
error("does not bind to one of ancestor packages of module '%s'",
m->toPrettyChars(true));
}
}


const char *ProtDeclaration::kind()
{
return "protection attribute";
}

const char *ProtDeclaration::toPrettyChars(bool unused)
const char *ProtDeclaration::toPrettyChars(bool)
{
assert(protection.kind > PROTundefined);

const char* kind = protectionToChars(this->protection);

OutBuffer buffer;

if ((protection.kind == PROTpackage) && protection.pkg)
if (protection.kind == PROTpackage && protection.pkg)
{
// 'package(name)'
const char* name = protection.pkg->toPrettyChars(true);
Expand Down
21 changes: 18 additions & 3 deletions src/dsymbol.c
Expand Up @@ -1028,10 +1028,9 @@ OverloadSet *ScopeDsymbol::mergeOverloadSet(OverloadSet *os, Dsymbol *s)
Dsymbol *s2 = os->a[j];
if (s->toAlias() == s2->toAlias())
{

if (s2->isDeprecated() ||
(s2->prot().isMoreRestrictiveThan(s->prot()) &&
s->prot().kind != PROTnone))
s->prot().kind != PROTnone))
{
os->a[j] = s;
}
Expand Down Expand Up @@ -1555,12 +1554,18 @@ Prot::Prot(PROTKIND kind)
this->pkg = NULL;
}


/**
* Checks if `this` is superset of `other` restrictions.
* For example, "protected" is more restrictive than "public".
*/
bool Prot::isMoreRestrictiveThan(Prot other)
{
return this->kind < other.kind;
}

/**
* Checks if `this` is absolutely identical protection attribute to `other`
*/
bool Prot::operator==(Prot other)
{
if (this->kind == other.kind)
Expand All @@ -1572,6 +1577,16 @@ bool Prot::operator==(Prot other)
return false;
}

/**
* Checks if parent defines different access restrictions than this one.
*
* Params:
* parent = protection attribute for scope that hosts this one
*
* Returns:
* 'true' if parent is already more restrictive than this one and thus
* no differentiation is needed.
*/
bool Prot::isSubsetOf(Prot parent)
{
if (this->kind != parent.kind)
Expand Down
21 changes: 2 additions & 19 deletions src/dsymbol.h
Expand Up @@ -106,29 +106,12 @@ enum PROTKIND
struct Prot
{
PROTKIND kind;
Package* pkg;
Package *pkg;

Prot(PROTKIND kind = PROTundefined);

/**
* Checks if `this` is superset of `other` restrictions.
* For example, "protected" is more restrictive than "public".
*/
bool isMoreRestrictiveThan(Prot other);
/**
* Checks if `this` is absolutely identical protection attribute to `other`
*/
bool operator==(Prot other);
/**
* Checks if parent defines different access restrictions than this one.
*
* Params:
* parent = protection attribute for scope that hosts this one
*
* Returns:
* 'true' if parent is already more restrictive than this one and thus
* no differentiation is needed.
*/
bool isSubsetOf(Prot other);
};

Expand Down Expand Up @@ -171,7 +154,7 @@ class Dsymbol : public RootObject
Dsymbol *parent;
Symbol *csym; // symbol for code generator
Symbol *isym; // import version of csym
const utf8_t *comment; // documentation comment for this Dsymbol
const utf8_t *comment; // documentation comment for this Dsymbol
Loc loc; // where defined
Scope *scope; // !=NULL means context to use for semantic()
bool errors; // this symbol failed to pass semantic()
Expand Down
4 changes: 2 additions & 2 deletions src/hdrgen.c
Expand Up @@ -3009,9 +3009,9 @@ void protectionToBuffer(OutBuffer *buf, Prot prot)
if (p)
buf->writestring(p);

if ((prot.kind == PROTpackage) && (prot.pkg))
if (prot.kind == PROTpackage && prot.pkg)
{
Package* ppkg = prot.pkg;
Package *ppkg = prot.pkg;

if (ppkg)
{
Expand Down
18 changes: 9 additions & 9 deletions src/module.c
Expand Up @@ -1077,18 +1077,18 @@ Module *Package::isPackageMod()
*/
bool Package::isAncestorPackageOf(Package* pkg)
{
while (pkg)
{
if (this == pkg)
return true;
while (pkg)
{
if (this == pkg)
return true;

if (!pkg->parent)
break;
if (!pkg->parent)
break;

pkg = pkg->parent->isPackage();
}
pkg = pkg->parent->isPackage();
}

return false;
return false;
}

/****************************************************
Expand Down
2 changes: 1 addition & 1 deletion src/module.h
Expand Up @@ -53,7 +53,7 @@ class Package : public ScopeDsymbol

Package *isPackage() { return this; }

bool isAncestorPackageOf(Package* pkg);
bool isAncestorPackageOf(Package *pkg);

void semantic(Scope *sc) { }
Dsymbol *search(Loc loc, Identifier *ident, int flags = IgnoreNone);
Expand Down
19 changes: 7 additions & 12 deletions src/parse.c
Expand Up @@ -242,7 +242,7 @@ Dsymbols *Parser::parseDeclDefs(int once, Dsymbol **pLastDecl, PrefixAttributes
pAttrs->comment = token.blockComment;
}
PROTKIND prot;
Identifiers* pkg_prot_idents = NULL;
Identifiers *pkg_prot_idents = NULL;
StorageClass stc;
Condition *condition;

Expand Down Expand Up @@ -690,12 +690,12 @@ Dsymbols *Parser::parseDeclDefs(int once, Dsymbol **pLastDecl, PrefixAttributes
{
// optional qualified package identifier to bind
// protection to
if ((pAttrs->protection.kind == PROTpackage) && (token.value == TOKlparen))
if (pAttrs->protection.kind == PROTpackage && token.value == TOKlparen)
{
pkg_prot_idents = parseQualifiedIdentifier("protection package");

if (pkg_prot_idents)
check(TOKrparen);
check(TOKrparen);
else
{
while (token.value != TOKsemicolon && token.value != TOKeof)
Expand All @@ -710,7 +710,7 @@ Dsymbols *Parser::parseDeclDefs(int once, Dsymbol **pLastDecl, PrefixAttributes
a = parseBlock(pLastDecl, pAttrs);
if (pAttrs->protection.kind != PROTundefined)
{
if ((pAttrs->protection.kind == PROTpackage) && pkg_prot_idents)
if (pAttrs->protection.kind == PROTpackage && pkg_prot_idents)
s = new ProtDeclaration(attrloc, pkg_prot_idents, a);
else
s = new ProtDeclaration(attrloc, pAttrs->protection, a);
Expand Down Expand Up @@ -1263,22 +1263,17 @@ LINK Parser::parseLinkage(Identifiers **pidents)
* Returns:
* array of identifiers with actual qualified one stored last
*/
Identifiers* Parser::parseQualifiedIdentifier(const char* entity)
Identifiers *Parser::parseQualifiedIdentifier(const char *entity)
{
Identifiers *qualified = new Identifiers();

do
{
nextToken();

if (token.value != TOKidentifier)
{
error(
"%s expected as dot-separated identifiers, got '%s'",
entity,
token.toChars()
);

error("%s expected as dot-separated identifiers, got '%s'",
entity, token.toChars());
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion src/parse.h
Expand Up @@ -93,7 +93,7 @@ class Parser : public Lexer
TypeQualified *parseTypeof();
Type *parseVector();
LINK parseLinkage(Identifiers **);
Identifiers* parseQualifiedIdentifier(const char* entity);
Identifiers *parseQualifiedIdentifier(const char *entity);
Condition *parseDebugCondition();
Condition *parseVersionCondition();
Condition *parseStaticIfCondition();
Expand Down
2 changes: 1 addition & 1 deletion src/template.c
Expand Up @@ -2141,7 +2141,7 @@ void functionResolve(Match *m, Dsymbol *dstart, Loc loc, Scope *sc,
if (tf->equals(m->lastf->type) &&
fd->storage_class == m->lastf->storage_class &&
fd->parent == m->lastf->parent &&
(fd->protection == m->lastf->protection) &&
fd->protection == m->lastf->protection &&
fd->linkage == m->lastf->linkage)
{
if ( fd->fbody && !m->lastf->fbody) goto LfIsBetter;
Expand Down

0 comments on commit c4a0d80

Please sign in to comment.