This commit is contained in:
Jan Jurzitza 2025-03-01 02:12:59 +01:00 committed by GitHub
commit 38ee93ef72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 36 additions and 13 deletions

View File

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

View File

@ -127,9 +127,8 @@ final class FirstPass : ASTVisitor
{ {
assert(dec); assert(dec);
pushSymbol(dec.name.text, CompletionKind.functionName, symbolFile, pushSymbol(dec.name.text, CompletionKind.functionName, symbolFile,
dec.name.index, dec.returnType); dec.name.index, dec.returnType, protection.current);
scope (exit) popSymbol(); scope (exit) popSymbol();
currentSymbol.acSymbol.protection = protection.current;
currentSymbol.acSymbol.doc = makeDocumentation(dec.comment); currentSymbol.acSymbol.doc = makeDocumentation(dec.comment);
currentSymbol.acSymbol.qualifier = SymbolQualifier.func; currentSymbol.acSymbol.qualifier = SymbolQualifier.func;
@ -381,6 +380,7 @@ final class FirstPass : ASTVisitor
auto objectImport = allocateSemanticSymbol(IMPORT_SYMBOL_NAME, auto objectImport = allocateSemanticSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, objectLocation); CompletionKind.importSymbol, objectLocation);
objectImport.acSymbol.skipOver = true; objectImport.acSymbol.skipOver = true;
objectImport.acSymbol.protection = protection.currentForImport;
currentSymbol.addChild(objectImport, true); currentSymbol.addChild(objectImport, true);
currentScope.addSymbol(objectImport.acSymbol, false); currentScope.addSymbol(objectImport.acSymbol, false);
} }
@ -447,6 +447,7 @@ final class FirstPass : ASTVisitor
thisSymbol.symbolFile = symbolFile; thisSymbol.symbolFile = symbolFile;
thisSymbol.type = currentSymbol.acSymbol; thisSymbol.type = currentSymbol.acSymbol;
thisSymbol.ownType = false; thisSymbol.ownType = false;
thisSymbol.protection = tok!"private";
currentScope.addSymbol(thisSymbol, false); currentScope.addSymbol(thisSymbol, false);
foreach (dec; structBody.declarations) foreach (dec; structBody.declarations)
@ -478,6 +479,7 @@ final class FirstPass : ASTVisitor
SemanticSymbol* importSymbol = allocateSemanticSymbol(IMPORT_SYMBOL_NAME, SemanticSymbol* importSymbol = allocateSemanticSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, modulePath); CompletionKind.importSymbol, modulePath);
importSymbol.acSymbol.skipOver = protection.currentForImport != tok!"public"; importSymbol.acSymbol.skipOver = protection.currentForImport != tok!"public";
importSymbol.acSymbol.protection = protection.currentForImport;
if (single.rename == tok!"") if (single.rename == tok!"")
{ {
size_t i = 0; size_t i = 0;
@ -495,6 +497,8 @@ final class FirstPass : ASTVisitor
if (s.length == 0) if (s.length == 0)
{ {
currentImportSymbol = GCAllocator.instance.make!DSymbol(ip, kind); currentImportSymbol = GCAllocator.instance.make!DSymbol(ip, kind);
currentImportSymbol.protection = protection.currentForImport;
currentImportSymbol.skipOver = protection.currentForImport != tok!"public";
currentScope.addSymbol(currentImportSymbol, true); currentScope.addSymbol(currentImportSymbol, true);
if (last) if (last)
{ {
@ -512,6 +516,8 @@ final class FirstPass : ASTVisitor
if (s.length == 0) if (s.length == 0)
{ {
auto sym = GCAllocator.instance.make!DSymbol(ip, kind); auto sym = GCAllocator.instance.make!DSymbol(ip, kind);
sym.protection = protection.currentForImport;
sym.skipOver = protection.currentForImport != tok!"public";
currentImportSymbol.addChild(sym, true); currentImportSymbol.addChild(sym, true);
currentImportSymbol = sym; currentImportSymbol = sym;
if (last) if (last)
@ -534,6 +540,7 @@ final class FirstPass : ASTVisitor
SemanticSymbol* renameSymbol = allocateSemanticSymbol( SemanticSymbol* renameSymbol = allocateSemanticSymbol(
internString(single.rename.text), CompletionKind.aliasName, internString(single.rename.text), CompletionKind.aliasName,
modulePath); modulePath);
renameSymbol.acSymbol.protection = protection.currentForImport;
renameSymbol.acSymbol.skipOver = protection.currentForImport != tok!"public"; renameSymbol.acSymbol.skipOver = protection.currentForImport != tok!"public";
renameSymbol.acSymbol.type = importSymbol.acSymbol; renameSymbol.acSymbol.type = importSymbol.acSymbol;
renameSymbol.acSymbol.ownType = true; renameSymbol.acSymbol.ownType = true;
@ -551,7 +558,7 @@ final class FirstPass : ASTVisitor
istring modulePath = cache.resolveImportLocation(chain); istring modulePath = cache.resolveImportLocation(chain);
if (modulePath is null) if (modulePath is null)
{ {
warning("Could not resolve location of module '", chain, "'"); warning("Could not resolve location of module '", chain.data, "'");
return; return;
} }
@ -579,6 +586,7 @@ final class FirstPass : ASTVisitor
importSymbol.acSymbol.qualifier = SymbolQualifier.selectiveImport; importSymbol.acSymbol.qualifier = SymbolQualifier.selectiveImport;
importSymbol.typeLookups.insert(lookup); importSymbol.typeLookups.insert(lookup);
importSymbol.acSymbol.skipOver = protection.currentForImport != tok!"public"; importSymbol.acSymbol.skipOver = protection.currentForImport != tok!"public";
importSymbol.acSymbol.protection = protection.currentForImport;
currentSymbol.addChild(importSymbol, true); currentSymbol.addChild(importSymbol, true);
currentScope.addSymbol(importSymbol.acSymbol, false); currentScope.addSymbol(importSymbol.acSymbol, false);
} }
@ -838,10 +846,11 @@ private:
} }
void pushSymbol(string name, CompletionKind kind, istring symbolFile, 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, SemanticSymbol* symbol = allocateSemanticSymbol(name, kind, symbolFile,
location); location, protection);
if (type !is null) if (type !is null)
addTypeToLookups(symbol.typeLookups, type); addTypeToLookups(symbol.typeLookups, type);
symbol.parent = currentSymbol; symbol.parent = currentSymbol;
@ -874,14 +883,13 @@ private:
dec.accept(this); dec.accept(this);
return; 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(); scope(exit) popSymbol();
if (kind == CompletionKind.className) if (kind == CompletionKind.className)
currentSymbol.acSymbol.addChildren(classSymbols[], false); currentSymbol.acSymbol.addChildren(classSymbols[], false);
else else
currentSymbol.acSymbol.addChildren(aggregateSymbols[], false); currentSymbol.acSymbol.addChildren(aggregateSymbols[], false);
currentSymbol.acSymbol.protection = protection.current;
currentSymbol.acSymbol.doc = makeDocumentation(dec.comment); currentSymbol.acSymbol.doc = makeDocumentation(dec.comment);
istring lastComment = this.lastComment; istring lastComment = this.lastComment;
@ -1148,11 +1156,12 @@ private:
} }
SemanticSymbol* allocateSemanticSymbol(string name, CompletionKind kind, 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); DSymbol* acSymbol = GCAllocator.instance.make!DSymbol(istring(name), kind);
acSymbol.location = location; acSymbol.location = location;
acSymbol.symbolFile = symbolFile; acSymbol.symbolFile = symbolFile;
acSymbol.protection = protection;
symbolsAllocated++; symbolsAllocated++;
return GCAllocator.instance.make!SemanticSymbol(acSymbol); return GCAllocator.instance.make!SemanticSymbol(acSymbol);
} }
@ -1264,17 +1273,19 @@ struct ProtectionStack
IdType currentForImport() const IdType currentForImport() const
{ {
return stack.empty ? tok!"default" : current(); // Imports are private unless specified otherwise.
return stack.empty ? tok!"private" : current();
} }
IdType current() const IdType current() const
out(t; isProtection(t), str(t))
do
{ {
import std.algorithm.iteration : filter; import std.algorithm.iteration : filter;
import std.range : choose, only; import std.range : choose, only;
IdType retVal; IdType retVal = tok!"public";
foreach (t; choose(stack.empty, only(tok!"public"), stack[]).filter!( foreach (t; stack[].filter!(a => a != tok!"{" && a != tok!":"))
a => a != tok!"{" && a != tok!":"))
retVal = cast(IdType) t; retVal = cast(IdType) t;
return retVal; return retVal;
} }
@ -1303,7 +1314,7 @@ struct ProtectionStack
void beginLocal(const IdType t) void beginLocal(const IdType t)
{ {
assert (t != tok!"", "DERP!"); assert(isProtection(t), str(t));
stack.insertBack(t); stack.insertBack(t);
} }

View File

@ -470,6 +470,7 @@ void resolveInheritance(DSymbol* symbol, ref TypeLookups typeLookups,
DSymbol* imp = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME, DSymbol* imp = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, baseClass); CompletionKind.importSymbol, baseClass);
imp.protection = tok!"public";
symbol.addChild(imp, true); symbol.addChild(imp, true);
symbolScope.addSymbol(imp, false); symbolScope.addSymbol(imp, false);
if (baseClass.kind == CompletionKind.className) if (baseClass.kind == CompletionKind.className)
@ -495,6 +496,7 @@ void resolveAliasThis(DSymbol* symbol,
DSymbol* s = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME, DSymbol* s = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, parts[0].type); CompletionKind.importSymbol, parts[0].type);
s.protection = tok!"public";
symbol.addChild(s, true); symbol.addChild(s, true);
auto symbolScope = moduleScope.getScopeByCursor(s.location); auto symbolScope = moduleScope.getScopeByCursor(s.location);
if (symbolScope !is null) if (symbolScope !is null)
@ -530,6 +532,7 @@ void resolveMixinTemplates(DSymbol* symbol,
auto i = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME, auto i = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, currentSymbol); CompletionKind.importSymbol, currentSymbol);
i.ownType = false; i.ownType = false;
i.protection = tok!"public";
symbol.addChild(i, true); symbol.addChild(i, true);
} }
} }

View File

@ -311,6 +311,7 @@ struct DSymbol
void addChild(DSymbol* symbol, bool owns) void addChild(DSymbol* symbol, bool owns)
{ {
assert(symbol !is null); assert(symbol !is null);
assert(isProtection(symbol.protection));
parts.insert(SymbolOwnership(symbol, owns)); parts.insert(SymbolOwnership(symbol, owns));
} }
@ -319,6 +320,7 @@ struct DSymbol
foreach (symbol; symbols) foreach (symbol; symbols)
{ {
assert(symbol !is null); assert(symbol !is null);
assert(isProtection(symbol.protection));
parts.insert(SymbolOwnership(symbol, owns)); parts.insert(SymbolOwnership(symbol, owns));
} }
} }
@ -328,6 +330,7 @@ struct DSymbol
foreach (symbol; symbols) foreach (symbol; symbols)
{ {
assert(symbol !is null); assert(symbol !is null);
assert(isProtection(symbol.protection));
parts.insert(SymbolOwnership(symbol, owns)); parts.insert(SymbolOwnership(symbol, owns));
} }
} }