16 void addBuiltin(std::string Name) {
21 [[nodiscard(
"Record ToDef Map!")]] std::shared_ptr<Definition>
22 add(std::string Name,
const Node *Entry,
24 assert(!Def.contains(Name));
25 auto NewDef = std::make_shared<Definition>(Entry, Source);
26 Def.insert({std::move(Name), NewDef});
36 for (
const auto &[_, D] : Defs) {
37 if (!D->uses().empty())
43void VariableLookupAnalysis::emitEnvLivenessWarning(
44 const std::shared_ptr<EnvNode> &NewEnv) {
45 for (
const auto &[Name, Def] : NewEnv->defs()) {
54 if (Def->uses().empty()) {
55 Diagnostic::DiagnosticKind Kind = [&]() {
56 switch (Def->source()) {
58 return Diagnostic::DK_UnusedDefLet;
60 return Diagnostic::DK_UnusedDefLambdaNoArg_Formal;
62 return Diagnostic::DK_UnusedDefLambdaWithArg_Formal;
64 return Diagnostic::DK_UnusedDefLambdaWithArg_Arg;
66 assert(
false &&
"liveness diagnostic encountered an unknown source!");
67 __builtin_unreachable();
70 Diagnostic &D = Diags.emplace_back(Kind, Def->syntax()->range());
77void VariableLookupAnalysis::lookupVar(
const ExprVar &Var,
78 const std::shared_ptr<EnvNode> &Env) {
79 const auto &Name = Var.
id().
name();
80 const auto *CurEnv = Env.get();
81 std::shared_ptr<Definition> Def;
82 std::vector<const EnvNode *> WithEnvs;
83 for (; CurEnv; CurEnv = CurEnv->parent()) {
84 if (CurEnv->defs().contains(Name)) {
85 Def = CurEnv->defs().at(Name);
96 if (CurEnv->isWith()) {
97 WithEnvs.emplace_back(CurEnv);
104 }
else if (!WithEnvs.empty()) {
105 for (
const auto *WithEnv : WithEnvs) {
106 Def = WithDefs.at(WithEnv->syntax());
114 Diags.emplace_back(Diagnostic::DK_UndefinedVariable, Var.
range());
119void VariableLookupAnalysis::dfs(
const ExprLambda &Lambda,
120 const std::shared_ptr<EnvNode> &Env) {
127 assert(Lambda.
arg());
134 ToDef.insert_or_assign(Arg.
id(), DBuilder.add(Arg.
id()->
name(), Arg.
id(),
139 ToDef.insert_or_assign(Arg.
id(),
140 DBuilder.add(Arg.
id()->
name(), Arg.
id(),
161 ToDef.insert_or_assign(
Formal->
id(),
162 DBuilder.add(Name,
Formal->
id(), Source));
166 auto NewEnv = std::make_shared<EnvNode>(Env, DBuilder.finish(), &Lambda);
176 dfs(*Lambda.
body(), NewEnv);
178 emitEnvLivenessWarning(NewEnv);
181void VariableLookupAnalysis::dfsDynamicAttrs(
182 const std::vector<Attribute> &DynamicAttrs,
183 const std::shared_ptr<EnvNode> &Env) {
184 for (
const auto &Attr : DynamicAttrs) {
187 dfs(Attr.key(), Env);
188 dfs(*Attr.value(), Env);
192std::shared_ptr<EnvNode> VariableLookupAnalysis::dfsAttrs(
193 const SemaAttrs &SA,
const std::shared_ptr<EnvNode> &Env,
200 ToDef.insert_or_assign(&Attr.key(), DB.add(Name, &Attr.key(), Source));
202 auto NewEnv = std::make_shared<EnvNode>(Env, DB.finish(), Syntax);
209 dfs(*Attr.value(), NewEnv);
212 dfs(*Attr.value(), Env);
224 dfs(*Attr.value(), Env);
231void VariableLookupAnalysis::dfs(
const ExprAttrs &Attrs,
232 const std::shared_ptr<EnvNode> &Env) {
234 std::shared_ptr<EnvNode> NewEnv =
238 "NewEnv must be created for recursive attrset");
239 if (!NewEnv->isLive()) {
240 Diagnostic &D = Diags.emplace_back(Diagnostic::DK_ExtraRecursive,
242 D.
fix(
"remove `rec` keyword")
249void VariableLookupAnalysis::dfs(
const ExprLet &Let,
250 const std::shared_ptr<EnvNode> &Env) {
253 auto GetLetEnv = [&Env, &Let,
this]() -> std::shared_ptr<EnvNode> {
262 assert(SA.
isRecursive() &&
"let ... in ... attrset must be recursive");
266 auto LetEnv = GetLetEnv();
269 dfs(*Let.
expr(), LetEnv);
270 emitEnvLivenessWarning(LetEnv);
273void VariableLookupAnalysis::trivialDispatch(
274 const Node &Root,
const std::shared_ptr<EnvNode> &Env) {
282void VariableLookupAnalysis::dfs(
const ExprWith &With,
283 const std::shared_ptr<EnvNode> &Env) {
284 auto NewEnv = std::make_shared<EnvNode>(Env,
EnvNode::DefMap{}, &With);
285 if (!WithDefs.contains(&With)) {
288 ToDef.insert_or_assign(&With.
kwWith(), NewDef);
289 WithDefs.insert_or_assign(&With, NewDef);
293 dfs(*With.
with(), Env);
296 dfs(*With.
expr(), NewEnv);
298 if (WithDefs.at(&With)->uses().empty()) {
300 Diags.emplace_back(Diagnostic::DK_ExtraWith, With.
kwWith().
range());
301 Fix &F = D.
fix(
"remove `with` expression")
310void VariableLookupAnalysis::dfs(
const Node &Root,
311 const std::shared_ptr<EnvNode> &Env) {
312 Envs.insert({&Root, Env});
313 switch (Root.
kind()) {
314 case Node::NK_ExprVar: {
315 const auto &Var =
static_cast<const ExprVar &
>(Root);
319 case Node::NK_ExprLambda: {
320 const auto &Lambda =
static_cast<const ExprLambda &
>(Root);
324 case Node::NK_ExprAttrs: {
325 const auto &Attrs =
static_cast<const ExprAttrs &
>(Root);
329 case Node::NK_ExprLet: {
330 const auto &Let =
static_cast<const ExprLet &
>(Root);
334 case Node::NK_ExprWith: {
335 const auto &With =
static_cast<const ExprWith &
>(Root);
340 trivialDispatch(Root, Env);
347 std::vector<std::string> Builtins{
353 "__addDrvOutputDependencies",
369 "__flakeRefToString",
428 "__concatStringsSep",
441 "__unsafeDiscardOutputDependency",
446 "__unsafeDiscardStringContext",
451 "__unsafeGetAttrPos",
467 for (
const auto &Builtin : Builtins)
468 DB.addBuiltin(Builtin);
470 auto Env = std::make_shared<EnvNode>(
nullptr, DB.finish(),
nullptr);
479 if (!Envs.contains(N))
481 return Envs.at(N).get();
Lookup variable names, from it's parent scope.
@ InheritFrom
inherit (expr) a b c
DefinitionSource
"Source" information so we can know where the def comes from.
@ DS_Rec
From recursive attribute set. e.g. rec { }.
@ DS_LambdaArg
From ambda arg e.g. a: a + 1.
@ DS_LambdaNoArg_Formal
From lambda (noarg) formal, e.g. { a }: a + 1.
@ DS_Builtin
Builtin names.
@ DS_LambdaWithArg_Arg
From lambda (with @arg) arg, e.g. a in { foo }@a: foo + 1
@ DS_With
From with <expr>;.
@ DS_LambdaWithArg_Formal
From lambda (with @arg) formal, e.g. foo in { foo }@a: foo + 1
@ DS_Let
From let ... in ...
Fix & fix(std::string Message)
A set of variable definitions, which may inherit parent environment.
std::map< std::string, std::shared_ptr< Definition > > DefMap
const SemaAttrs & sema() const
const ExprAttrs * attrs() const
const Expr * expr() const
const Identifier & id() const
const Misc & kwWith() const
const Misc * tokSemi() const
Fix & edit(TextEdit Edit)
const std::string & name() const
Formals * formals() const
LexerCursorRange range() const
virtual ChildVector children() const =0
void tag(DiagnosticTag Tag)
Attribute set after deduplication.
bool isRecursive() const
If the attribute set is rec.
const std::vector< Attribute > & dynamicAttrs() const
Dynamic attributes, require evaluation to get the key.
const std::map< std::string, Attribute > & staticAttrs() const
Static attributes, do not require evaluation to get the key.
static TextEdit mkRemoval(LexerCursorRange RemovingRange)
const EnvNode * env(const Node *N) const
void runOnAST(const Node &Root)
Perform variable lookup analysis (def-use) on AST.
VariableLookupAnalysis(std::vector< Diagnostic > &Diags)