Adding return type name to symbol

This commit is contained in:
davu 2023-02-26 21:28:46 +01:00
parent 92f2d73731
commit 5bbc335034
5 changed files with 67 additions and 27 deletions

View File

@ -37,6 +37,7 @@ import std.experimental.allocator;
import std.experimental.allocator.gc_allocator : GCAllocator; import std.experimental.allocator.gc_allocator : GCAllocator;
import std.experimental.logger; import std.experimental.logger;
import std.typecons : Rebindable; import std.typecons : Rebindable;
import std.array : appender;
/** /**
* First Pass handles the following: * First Pass handles the following:
@ -148,6 +149,11 @@ final class FirstPass : ASTVisitor
processParameters(currentSymbol, dec.returnType, processParameters(currentSymbol, dec.returnType,
currentSymbol.acSymbol.name, dec.parameters, dec.templateParameters); currentSymbol.acSymbol.name, dec.parameters, dec.templateParameters);
} }
auto app = appender!string();
app.formatNode(dec.returnType);
currentSymbol.typeLookups.insert(TypeLookupsAllocator.instance.make!TypeLookup(
istring(app.data), TypeLookupKind.returnType));
} }
override void visit(const FunctionLiteralExpression exp) override void visit(const FunctionLiteralExpression exp)
@ -778,7 +784,6 @@ private:
void createConstructor() void createConstructor()
{ {
import std.array : appender;
import std.range : zip; import std.range : zip;
auto app = appender!string(); auto app = appender!string();
@ -1064,7 +1069,6 @@ private:
istring formatCallTip(const Type returnType, string name, istring formatCallTip(const Type returnType, string name,
const Parameters parameters, const TemplateParameters templateParameters) const Parameters parameters, const TemplateParameters templateParameters)
{ {
import std.array : appender;
auto app = appender!string(); auto app = appender!string();
if (returnType !is null) if (returnType !is null)
@ -1136,7 +1140,6 @@ private:
lookup.breadcrumbs.insert(POINTER_SYMBOL_NAME); lookup.breadcrumbs.insert(POINTER_SYMBOL_NAME);
else if (suffix.delegateOrFunction != tok!"") else if (suffix.delegateOrFunction != tok!"")
{ {
import std.array : appender;
auto app = appender!string(); auto app = appender!string();
formatNode(app, type); formatNode(app, type);
istring callTip = istring(app.data); istring callTip = istring(app.data);
@ -1341,7 +1344,6 @@ void writeIotcTo(T)(const IdentifierOrTemplateChain iotc, ref T output) nothrow
static istring convertChainToImportPath(const IdentifierChain ic) static istring convertChainToImportPath(const IdentifierChain ic)
{ {
import std.path : dirSeparator; import std.path : dirSeparator;
import std.array : appender;
auto app = appender!string(); auto app = appender!string();
foreach (i, ident; ic.identifiers) foreach (i, ident; ic.identifiers)
{ {

View File

@ -33,6 +33,8 @@ import std.experimental.allocator.gc_allocator : GCAllocator;
import std.experimental.logger; import std.experimental.logger;
import dparse.ast; import dparse.ast;
import dparse.lexer; import dparse.lexer;
import std.algorithm : filter;
import std.range;
void secondPass(SemanticSymbol* currentSymbol, Scope* moduleScope, ref ModuleCache cache) void secondPass(SemanticSymbol* currentSymbol, Scope* moduleScope, ref ModuleCache cache)
{ {
@ -309,11 +311,28 @@ do
private: private:
void resolveReturnType(DSymbol* symbol, ref TypeLookups typeLookups,
Scope* moduleScope, ref ModuleCache cache)
{
foreach (returnType; typeLookups[].filter!(a => a.kind == TypeLookupKind.returnType))
{
assert(returnType.breadcrumbs.length > 0);
auto parts = symbol.getPartsByName(returnType.breadcrumbs.front);
if (parts.empty){
// If nothing found try to lookup within the global scope
auto found = moduleScope.getSymbolsAtGlobalScope(returnType.breadcrumbs.front);
if (!found.empty) {
symbol.returnType = found.front;
}
}
}
}
void resolveInheritance(DSymbol* symbol, ref TypeLookups typeLookups, void resolveInheritance(DSymbol* symbol, ref TypeLookups typeLookups,
Scope* moduleScope, ref ModuleCache cache) Scope* moduleScope, ref ModuleCache cache)
{ {
import std.algorithm : filter;
outer: foreach (TypeLookup* lookup; typeLookups[]) outer: foreach (TypeLookup* lookup; typeLookups[])
{ {
if (lookup.kind != TypeLookupKind.inherit) if (lookup.kind != TypeLookupKind.inherit)
@ -355,9 +374,6 @@ void resolveInheritance(DSymbol* symbol, ref TypeLookups typeLookups,
void resolveAliasThis(DSymbol* symbol, void resolveAliasThis(DSymbol* symbol,
ref TypeLookups typeLookups, Scope* moduleScope, ref ModuleCache cache) ref TypeLookups typeLookups, Scope* moduleScope, ref ModuleCache cache)
{ {
import std.algorithm : filter;
import std.array : array;
foreach (aliasThis; typeLookups[].filter!(a => a.kind == TypeLookupKind.aliasThis)) foreach (aliasThis; typeLookups[].filter!(a => a.kind == TypeLookupKind.aliasThis))
{ {
assert(aliasThis.breadcrumbs.length > 0); assert(aliasThis.breadcrumbs.length > 0);
@ -379,8 +395,6 @@ void resolveAliasThis(DSymbol* symbol,
void resolveMixinTemplates(DSymbol* symbol, void resolveMixinTemplates(DSymbol* symbol,
ref TypeLookups typeLookups, Scope* moduleScope, ref ModuleCache cache) ref TypeLookups typeLookups, Scope* moduleScope, ref ModuleCache cache)
{ {
import std.algorithm : filter;
foreach (mix; typeLookups[].filter!(a => a.kind == TypeLookupKind.mixinTemplate)) foreach (mix; typeLookups[].filter!(a => a.kind == TypeLookupKind.mixinTemplate))
{ {
assert(mix.breadcrumbs.length > 0); assert(mix.breadcrumbs.length > 0);
@ -414,22 +428,21 @@ void resolveMixinTemplates(DSymbol* symbol,
void resolveType(DSymbol* symbol, ref TypeLookups typeLookups, void resolveType(DSymbol* symbol, ref TypeLookups typeLookups,
Scope* moduleScope, ref ModuleCache cache) Scope* moduleScope, ref ModuleCache cache)
{ {
// going through the lookups
import std.conv; foreach(lookup; typeLookups) {
if (lookup.kind == TypeLookupKind.varOrFunType)
if (typeLookups.length == 0) resolveTypeFromType(symbol, lookup, moduleScope, cache, null);
return; else if (lookup.kind == TypeLookupKind.initializer)
assert(typeLookups.length == 1); resolveTypeFromInitializer(symbol, lookup, moduleScope, cache);
auto lookup = typeLookups.front; // issue 94
if (lookup.kind == TypeLookupKind.varOrFunType) else if (lookup.kind == TypeLookupKind.inherit)
resolveTypeFromType(symbol, lookup, moduleScope, cache, null); resolveInheritance(symbol, typeLookups, moduleScope, cache);
else if (lookup.kind == TypeLookupKind.initializer) else if (lookup.kind == TypeLookupKind.returnType){
resolveTypeFromInitializer(symbol, lookup, moduleScope, cache); resolveReturnType(symbol, typeLookups, moduleScope, cache);
// issue 94 }
else if (lookup.kind == TypeLookupKind.inherit) else
resolveInheritance(symbol, typeLookups, moduleScope, cache); assert(false, "How did this happen?");
else }
assert(false, "How did this happen?");
} }
void resolveTypeFromInitializer(DSymbol* symbol, TypeLookup* lookup, void resolveTypeFromInitializer(DSymbol* symbol, TypeLookup* lookup,

View File

@ -375,6 +375,9 @@ struct DSymbol
// TODO: assert that the type is not a function // TODO: assert that the type is not a function
DSymbol* type; DSymbol* type;
// Return type symbol, if symbol currently is of function kind
DSymbol* returnType;
// Is alias this symbols // Is alias this symbols
DSymbol*[] aliasThisSymbols; DSymbol*[] aliasThisSymbols;
/** /**

View File

@ -126,6 +126,27 @@ unittest
} }
} }
unittest
{
ModuleCache cache;
writeln("Get return type name");
auto source = q{ int meaningOfLife() { return 42; } };
auto pair = generateAutocompleteTrees(source, cache);
auto meaningOfLife = pair.symbol.getFirstPartNamed(istring("meaningOfLife"));
assert(meaningOfLife.returnType.name == "int");
}
unittest
{
ModuleCache cache;
writeln("Get return type name from class method");
auto source = q{ class Life { uint meaningOfLife() { return 42; } }};
auto pair = generateAutocompleteTrees(source, cache);
auto lifeClass = pair.symbol.getFirstPartNamed(istring("Life"));
auto meaningOfLife = lifeClass.getFirstPartNamed(istring("meaningOfLife"));
assert(meaningOfLife.returnType.name == "uint");
}
unittest unittest
{ {
ModuleCache cache; ModuleCache cache;

View File

@ -14,6 +14,7 @@ enum TypeLookupKind : ubyte
mixinTemplate, mixinTemplate,
varOrFunType, varOrFunType,
selectiveImport, selectiveImport,
returnType,
} }
/** /**