Skip to content

Commit 1835fb9

Browse files
HackerpilotWebFreak001
authored andcommitted
Fix several problems with symbol protection #620
1 parent 5c529f3 commit 1835fb9

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

dsymbol/src/dsymbol/builtin/symbols.d

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,21 @@ private HashSet!(DSymbol*) symbolsMadeHere;
296296

297297
private DSymbol* makeSymbol(string s, CompletionKind kind, DSymbol* type = null)
298298
{
299+
import dparse.lexer : tok;
300+
299301
auto sym = rba.make!DSymbol(istring(s), kind, type);
300302
sym.ownType = false;
303+
sym.protection = tok!"public";
301304
symbolsMadeHere.insert(sym);
302305
return sym;
303306
}
304307
private DSymbol* makeSymbol(istring s, CompletionKind kind, DSymbol* type = null)
305308
{
309+
import dparse.lexer : tok;
310+
306311
auto sym = rba.make!DSymbol(s, kind, type);
307312
sym.ownType = false;
313+
sym.protection = tok!"public";
308314
symbolsMadeHere.insert(sym);
309315
return sym;
310316
}

dsymbol/src/dsymbol/conversion/first.d

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@ final class FirstPass : ASTVisitor
125125
{
126126
assert(dec);
127127
pushSymbol(dec.name.text, CompletionKind.functionName, symbolFile,
128-
dec.name.index, dec.returnType);
128+
dec.name.index, dec.returnType, protection.current);
129129
scope (exit) popSymbol();
130-
currentSymbol.acSymbol.protection = protection.current;
131130
currentSymbol.acSymbol.doc = makeDocumentation(dec.comment);
132131

133132
istring lastComment = this.lastComment;
@@ -374,6 +373,7 @@ final class FirstPass : ASTVisitor
374373
auto objectImport = allocateSemanticSymbol(IMPORT_SYMBOL_NAME,
375374
CompletionKind.importSymbol, objectLocation);
376375
objectImport.acSymbol.skipOver = true;
376+
objectImport.acSymbol.protection = protection.currentForImport;
377377
currentSymbol.addChild(objectImport, true);
378378
currentScope.addSymbol(objectImport.acSymbol, false);
379379
}
@@ -440,6 +440,7 @@ final class FirstPass : ASTVisitor
440440
thisSymbol.symbolFile = symbolFile;
441441
thisSymbol.type = currentSymbol.acSymbol;
442442
thisSymbol.ownType = false;
443+
thisSymbol.protection = tok!"private";
443444
currentScope.addSymbol(thisSymbol, false);
444445

445446
foreach (dec; structBody.declarations)
@@ -471,6 +472,7 @@ final class FirstPass : ASTVisitor
471472
SemanticSymbol* importSymbol = allocateSemanticSymbol(IMPORT_SYMBOL_NAME,
472473
CompletionKind.importSymbol, modulePath);
473474
importSymbol.acSymbol.skipOver = protection.currentForImport != tok!"public";
475+
importSymbol.acSymbol.protection = protection.currentForImport;
474476
if (single.rename == tok!"")
475477
{
476478
size_t i = 0;
@@ -488,6 +490,8 @@ final class FirstPass : ASTVisitor
488490
if (s.length == 0)
489491
{
490492
currentImportSymbol = GCAllocator.instance.make!DSymbol(ip, kind);
493+
currentImportSymbol.protection = protection.currentForImport;
494+
currentImportSymbol.skipOver = protection.currentForImport != tok!"public";
491495
currentScope.addSymbol(currentImportSymbol, true);
492496
if (last)
493497
{
@@ -505,6 +509,8 @@ final class FirstPass : ASTVisitor
505509
if (s.length == 0)
506510
{
507511
auto sym = GCAllocator.instance.make!DSymbol(ip, kind);
512+
sym.protection = protection.currentForImport;
513+
sym.skipOver = protection.currentForImport != tok!"public";
508514
currentImportSymbol.addChild(sym, true);
509515
currentImportSymbol = sym;
510516
if (last)
@@ -527,6 +533,7 @@ final class FirstPass : ASTVisitor
527533
SemanticSymbol* renameSymbol = allocateSemanticSymbol(
528534
internString(single.rename.text), CompletionKind.aliasName,
529535
modulePath);
536+
renameSymbol.acSymbol.protection = protection.currentForImport;
530537
renameSymbol.acSymbol.skipOver = protection.currentForImport != tok!"public";
531538
renameSymbol.acSymbol.type = importSymbol.acSymbol;
532539
renameSymbol.acSymbol.ownType = true;
@@ -544,7 +551,7 @@ final class FirstPass : ASTVisitor
544551
istring modulePath = cache.resolveImportLocation(chain);
545552
if (modulePath is null)
546553
{
547-
warning("Could not resolve location of module '", chain, "'");
554+
warning("Could not resolve location of module '", chain.data, "'");
548555
return;
549556
}
550557

@@ -572,6 +579,7 @@ final class FirstPass : ASTVisitor
572579
importSymbol.acSymbol.qualifier = SymbolQualifier.selectiveImport;
573580
importSymbol.typeLookups.insert(lookup);
574581
importSymbol.acSymbol.skipOver = protection.currentForImport != tok!"public";
582+
importSymbol.acSymbol.protection = protection.currentForImport;
575583
currentSymbol.addChild(importSymbol, true);
576584
currentScope.addSymbol(importSymbol.acSymbol, false);
577585
}
@@ -831,10 +839,11 @@ private:
831839
}
832840

833841
void pushSymbol(string name, CompletionKind kind, istring symbolFile,
834-
size_t location = 0, const Type type = null)
842+
size_t location = 0, const Type type = null,
843+
const IdType protection = tok!"public")
835844
{
836845
SemanticSymbol* symbol = allocateSemanticSymbol(name, kind, symbolFile,
837-
location);
846+
location, protection);
838847
if (type !is null)
839848
addTypeToLookups(symbol.typeLookups, type);
840849
symbol.parent = currentSymbol;
@@ -867,14 +876,13 @@ private:
867876
dec.accept(this);
868877
return;
869878
}
870-
pushSymbol(dec.name.text, kind, symbolFile, dec.name.index);
879+
pushSymbol(dec.name.text, kind, symbolFile, dec.name.index, null, protection.current);
871880
scope(exit) popSymbol();
872881

873882
if (kind == CompletionKind.className)
874883
currentSymbol.acSymbol.addChildren(classSymbols[], false);
875884
else
876885
currentSymbol.acSymbol.addChildren(aggregateSymbols[], false);
877-
currentSymbol.acSymbol.protection = protection.current;
878886
currentSymbol.acSymbol.doc = makeDocumentation(dec.comment);
879887

880888
istring lastComment = this.lastComment;
@@ -1092,11 +1100,12 @@ private:
10921100
}
10931101

10941102
SemanticSymbol* allocateSemanticSymbol(string name, CompletionKind kind,
1095-
istring symbolFile, size_t location = 0)
1103+
istring symbolFile, size_t location = 0, IdType protection = tok!"public")
10961104
{
10971105
DSymbol* acSymbol = GCAllocator.instance.make!DSymbol(istring(name), kind);
10981106
acSymbol.location = location;
10991107
acSymbol.symbolFile = symbolFile;
1108+
acSymbol.protection = protection;
11001109
symbolsAllocated++;
11011110
return GCAllocator.instance.make!SemanticSymbol(acSymbol);
11021111
}
@@ -1213,17 +1222,19 @@ struct ProtectionStack
12131222

12141223
IdType currentForImport() const
12151224
{
1216-
return stack.empty ? tok!"default" : current();
1225+
// Imports are private unless specified otherwise.
1226+
return stack.empty ? tok!"private" : current();
12171227
}
12181228

12191229
IdType current() const
1230+
out(t; isProtection(t), str(t))
1231+
do
12201232
{
12211233
import std.algorithm.iteration : filter;
12221234
import std.range : choose, only;
12231235

1224-
IdType retVal;
1225-
foreach (t; choose(stack.empty, only(tok!"public"), stack[]).filter!(
1226-
a => a != tok!"{" && a != tok!":"))
1236+
IdType retVal = tok!"public";
1237+
foreach (t; stack[].filter!(a => a != tok!"{" && a != tok!":"))
12271238
retVal = cast(IdType) t;
12281239
return retVal;
12291240
}
@@ -1252,7 +1263,7 @@ struct ProtectionStack
12521263

12531264
void beginLocal(const IdType t)
12541265
{
1255-
assert (t != tok!"", "DERP!");
1266+
assert(isProtection(t), str(t));
12561267
stack.insertBack(t);
12571268
}
12581269

dsymbol/src/dsymbol/conversion/second.d

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ void resolveInheritance(DSymbol* symbol, ref TypeLookups typeLookups,
335335

336336
DSymbol* imp = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
337337
CompletionKind.importSymbol, baseClass);
338+
imp.protection = tok!"public";
338339
symbol.addChild(imp, true);
339340
symbolScope.addSymbol(imp, false);
340341
if (baseClass.kind == CompletionKind.className)
@@ -359,6 +360,7 @@ void resolveAliasThis(DSymbol* symbol,
359360
continue;
360361
DSymbol* s = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
361362
CompletionKind.importSymbol, parts[0].type);
363+
s.protection = tok!"public";
362364
symbol.addChild(s, true);
363365
auto symbolScope = moduleScope.getScopeByCursor(s.location);
364366
if (symbolScope !is null)
@@ -396,6 +398,7 @@ void resolveMixinTemplates(DSymbol* symbol,
396398
auto i = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
397399
CompletionKind.importSymbol, currentSymbol);
398400
i.ownType = false;
401+
i.protection = tok!"public";
399402
symbol.addChild(i, true);
400403
}
401404
}

dsymbol/src/dsymbol/symbol.d

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ struct DSymbol
299299
void addChild(DSymbol* symbol, bool owns)
300300
{
301301
assert(symbol !is null);
302+
assert(isProtection(symbol.protection));
302303
parts.insert(SymbolOwnership(symbol, owns));
303304
}
304305

@@ -307,6 +308,7 @@ struct DSymbol
307308
foreach (symbol; symbols)
308309
{
309310
assert(symbol !is null);
311+
assert(isProtection(symbol.protection));
310312
parts.insert(SymbolOwnership(symbol, owns));
311313
}
312314
}
@@ -316,6 +318,7 @@ struct DSymbol
316318
foreach (symbol; symbols)
317319
{
318320
assert(symbol !is null);
321+
assert(isProtection(symbol.protection));
319322
parts.insert(SymbolOwnership(symbol, owns));
320323
}
321324
}

0 commit comments

Comments
 (0)