Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DSymbol: Hackerpilot: Fix several problems with symbol protection #620 #679

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dsymbol/src/dsymbol/builtin/symbols.d
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,21 @@ private HashSet!(DSymbol*) symbolsMadeHere;

private DSymbol* makeSymbol(string s, CompletionKind kind, DSymbol* type = null)
{
import dparse.lexer : tok;

auto sym = rba.make!DSymbol(istring(s), kind, type);
sym.ownType = false;
sym.protection = tok!"public";
symbolsMadeHere.insert(sym);
return sym;
}
private DSymbol* makeSymbol(istring s, CompletionKind kind, DSymbol* type = null)
{
import dparse.lexer : tok;

auto sym = rba.make!DSymbol(s, kind, type);
sym.ownType = false;
sym.protection = tok!"public";
symbolsMadeHere.insert(sym);
return sym;
}
37 changes: 24 additions & 13 deletions dsymbol/src/dsymbol/conversion/first.d
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ final class FirstPass : ASTVisitor
{
assert(dec);
pushSymbol(dec.name.text, CompletionKind.functionName, symbolFile,
dec.name.index, dec.returnType);
dec.name.index, dec.returnType, protection.current);
scope (exit) popSymbol();
currentSymbol.acSymbol.protection = protection.current;
currentSymbol.acSymbol.doc = makeDocumentation(dec.comment);

istring lastComment = this.lastComment;
Expand Down Expand Up @@ -374,6 +373,7 @@ final class FirstPass : ASTVisitor
auto objectImport = allocateSemanticSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, objectLocation);
objectImport.acSymbol.skipOver = true;
objectImport.acSymbol.protection = protection.currentForImport;
currentSymbol.addChild(objectImport, true);
currentScope.addSymbol(objectImport.acSymbol, false);
}
Expand Down Expand Up @@ -440,6 +440,7 @@ final class FirstPass : ASTVisitor
thisSymbol.symbolFile = symbolFile;
thisSymbol.type = currentSymbol.acSymbol;
thisSymbol.ownType = false;
thisSymbol.protection = tok!"private";
currentScope.addSymbol(thisSymbol, false);

foreach (dec; structBody.declarations)
Expand Down Expand Up @@ -471,6 +472,7 @@ final class FirstPass : ASTVisitor
SemanticSymbol* importSymbol = allocateSemanticSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, modulePath);
importSymbol.acSymbol.skipOver = protection.currentForImport != tok!"public";
importSymbol.acSymbol.protection = protection.currentForImport;
if (single.rename == tok!"")
{
size_t i = 0;
Expand All @@ -488,6 +490,8 @@ final class FirstPass : ASTVisitor
if (s.length == 0)
{
currentImportSymbol = GCAllocator.instance.make!DSymbol(ip, kind);
currentImportSymbol.protection = protection.currentForImport;
currentImportSymbol.skipOver = protection.currentForImport != tok!"public";
currentScope.addSymbol(currentImportSymbol, true);
if (last)
{
Expand All @@ -505,6 +509,8 @@ final class FirstPass : ASTVisitor
if (s.length == 0)
{
auto sym = GCAllocator.instance.make!DSymbol(ip, kind);
sym.protection = protection.currentForImport;
sym.skipOver = protection.currentForImport != tok!"public";
currentImportSymbol.addChild(sym, true);
currentImportSymbol = sym;
if (last)
Expand All @@ -527,6 +533,7 @@ final class FirstPass : ASTVisitor
SemanticSymbol* renameSymbol = allocateSemanticSymbol(
internString(single.rename.text), CompletionKind.aliasName,
modulePath);
renameSymbol.acSymbol.protection = protection.currentForImport;
renameSymbol.acSymbol.skipOver = protection.currentForImport != tok!"public";
renameSymbol.acSymbol.type = importSymbol.acSymbol;
renameSymbol.acSymbol.ownType = true;
Expand All @@ -544,7 +551,7 @@ final class FirstPass : ASTVisitor
istring modulePath = cache.resolveImportLocation(chain);
if (modulePath is null)
{
warning("Could not resolve location of module '", chain, "'");
warning("Could not resolve location of module '", chain.data, "'");
return;
}

Expand Down Expand Up @@ -572,6 +579,7 @@ final class FirstPass : ASTVisitor
importSymbol.acSymbol.qualifier = SymbolQualifier.selectiveImport;
importSymbol.typeLookups.insert(lookup);
importSymbol.acSymbol.skipOver = protection.currentForImport != tok!"public";
importSymbol.acSymbol.protection = protection.currentForImport;
currentSymbol.addChild(importSymbol, true);
currentScope.addSymbol(importSymbol.acSymbol, false);
}
Expand Down Expand Up @@ -831,10 +839,11 @@ private:
}

void pushSymbol(string name, CompletionKind kind, istring symbolFile,
size_t location = 0, const Type type = null)
size_t location = 0, const Type type = null,
const IdType protection = tok!"public")
{
SemanticSymbol* symbol = allocateSemanticSymbol(name, kind, symbolFile,
location);
location, protection);
if (type !is null)
addTypeToLookups(symbol.typeLookups, type);
symbol.parent = currentSymbol;
Expand Down Expand Up @@ -867,14 +876,13 @@ private:
dec.accept(this);
return;
}
pushSymbol(dec.name.text, kind, symbolFile, dec.name.index);
pushSymbol(dec.name.text, kind, symbolFile, dec.name.index, null, protection.current);
scope(exit) popSymbol();

if (kind == CompletionKind.className)
currentSymbol.acSymbol.addChildren(classSymbols[], false);
else
currentSymbol.acSymbol.addChildren(aggregateSymbols[], false);
currentSymbol.acSymbol.protection = protection.current;
currentSymbol.acSymbol.doc = makeDocumentation(dec.comment);

istring lastComment = this.lastComment;
Expand Down Expand Up @@ -1092,11 +1100,12 @@ private:
}

SemanticSymbol* allocateSemanticSymbol(string name, CompletionKind kind,
istring symbolFile, size_t location = 0)
istring symbolFile, size_t location = 0, IdType protection = tok!"public")
{
DSymbol* acSymbol = GCAllocator.instance.make!DSymbol(istring(name), kind);
acSymbol.location = location;
acSymbol.symbolFile = symbolFile;
acSymbol.protection = protection;
symbolsAllocated++;
return GCAllocator.instance.make!SemanticSymbol(acSymbol);
}
Expand Down Expand Up @@ -1213,17 +1222,19 @@ struct ProtectionStack

IdType currentForImport() const
{
return stack.empty ? tok!"default" : current();
// Imports are private unless specified otherwise.
return stack.empty ? tok!"private" : current();
}

IdType current() const
out(t; isProtection(t), str(t))
do
{
import std.algorithm.iteration : filter;
import std.range : choose, only;

IdType retVal;
foreach (t; choose(stack.empty, only(tok!"public"), stack[]).filter!(
a => a != tok!"{" && a != tok!":"))
IdType retVal = tok!"public";
foreach (t; stack[].filter!(a => a != tok!"{" && a != tok!":"))
retVal = cast(IdType) t;
return retVal;
}
Expand Down Expand Up @@ -1252,7 +1263,7 @@ struct ProtectionStack

void beginLocal(const IdType t)
{
assert (t != tok!"", "DERP!");
assert(isProtection(t), str(t));
stack.insertBack(t);
}

Expand Down
3 changes: 3 additions & 0 deletions dsymbol/src/dsymbol/conversion/second.d
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ void resolveInheritance(DSymbol* symbol, ref TypeLookups typeLookups,

DSymbol* imp = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, baseClass);
imp.protection = tok!"public";
symbol.addChild(imp, true);
symbolScope.addSymbol(imp, false);
if (baseClass.kind == CompletionKind.className)
Expand All @@ -359,6 +360,7 @@ void resolveAliasThis(DSymbol* symbol,
continue;
DSymbol* s = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, parts[0].type);
s.protection = tok!"public";
symbol.addChild(s, true);
auto symbolScope = moduleScope.getScopeByCursor(s.location);
if (symbolScope !is null)
Expand Down Expand Up @@ -396,6 +398,7 @@ void resolveMixinTemplates(DSymbol* symbol,
auto i = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, currentSymbol);
i.ownType = false;
i.protection = tok!"public";
symbol.addChild(i, true);
}
}
Expand Down
3 changes: 3 additions & 0 deletions dsymbol/src/dsymbol/symbol.d
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ struct DSymbol
void addChild(DSymbol* symbol, bool owns)
{
assert(symbol !is null);
assert(isProtection(symbol.protection));
parts.insert(SymbolOwnership(symbol, owns));
}

Expand All @@ -307,6 +308,7 @@ struct DSymbol
foreach (symbol; symbols)
{
assert(symbol !is null);
assert(isProtection(symbol.protection));
parts.insert(SymbolOwnership(symbol, owns));
}
}
Expand All @@ -316,6 +318,7 @@ struct DSymbol
foreach (symbol; symbols)
{
assert(symbol !is null);
assert(isProtection(symbol.protection));
parts.insert(SymbolOwnership(symbol, owns));
}
}
Expand Down