14std::set<std::string> Constants{
15 "true",
"false",
"null",
16 "__currentTime",
"__currentSystem",
"__nixVersion",
17 "__storeDir",
"__langVersion",
"__importNative",
18 "__traceVerbose",
"__nixPath",
"derivation",
25 std::vector<Diagnostic> &Diags;
27 std::shared_ptr<Definition> addSimple(std::string Name,
const Node *Entry,
29 assert(!Def.contains(Name));
30 auto NewDef = std::make_shared<Definition>(Entry, Source);
31 Def.insert({std::move(Name), NewDef});
36 DefBuilder(std::vector<Diagnostic> &Diags) : Diags(Diags) {}
38 void addBuiltin(std::string Name) {
43 [[nodiscard(
"Record ToDef Map!")]] std::shared_ptr<Definition>
45 bool IsInheritFromBuiltin) {
47 if (PrimOpLookup == PrimopLookupResult::Found && !IsInheritFromBuiltin) {
50 Diags.emplace_back(Diagnostic::DK_PrimOpOverridden, Entry->
range());
55 if (Constants.contains(Name)) {
57 Diags.emplace_back(Diagnostic::DK_ConstantOverridden, Entry->
range());
61 return addSimple(std::move(Name), Entry, Source);
72bool checkInheritedFromBuiltin(
const Attribute &Attr) {
76 assert(Attr.
value() &&
77 "select expr desugared from inherit should not be null");
78 assert(Attr.
value()->
kind() == Node::NK_ExprSelect &&
79 "desugared inherited from should be a select expr");
81 if (Select.expr().kind() == Node::NK_ExprVar) {
82 const auto &Var =
static_cast<const ExprVar &
>(Select.expr());
83 return Var.
id().
name() ==
"builtins";
88bool isBuiltinConstant(
const std::string &Name) {
89 if (Name.starts_with(
"_"))
91 return Constants.contains(Name) || Constants.contains(
"__" + Name);
97 for (
const auto &[_, D] : Defs) {
98 if (!D->uses().empty())
104void VariableLookupAnalysis::emitEnvLivenessWarning(
105 const std::shared_ptr<EnvNode> &NewEnv) {
106 for (
const auto &[Name, Def] : NewEnv->defs()) {
115 if (Def->uses().empty()) {
116 Diagnostic::DiagnosticKind Kind = [&]() {
117 switch (Def->source()) {
119 return Diagnostic::DK_UnusedDefLet;
121 return Diagnostic::DK_UnusedDefLambdaNoArg_Formal;
123 return Diagnostic::DK_UnusedDefLambdaWithArg_Formal;
125 return Diagnostic::DK_UnusedDefLambdaWithArg_Arg;
127 assert(
false &&
"liveness diagnostic encountered an unknown source!");
128 __builtin_unreachable();
131 Diagnostic &D = Diags.emplace_back(Kind, Def->syntax()->range());
137 const Node *LetNode = NewEnv->syntax();
138 if (LetNode && LetNode->
kind() == Node::NK_ExprLet) {
139 const auto &Let =
static_cast<const ExprLet &
>(*LetNode);
141 for (
const auto &BindNode : Let.binds()->bindings()) {
143 if (BindNode->kind() != Node::NK_Binding)
146 const auto &Bind =
static_cast<const Binding &
>(*BindNode);
147 const auto &PathNames = Bind.path().names();
148 if (PathNames.empty())
151 const auto &FirstName = PathNames[0];
157 if (D.
range().
lCur() == FirstName->range().lCur() &&
158 D.
range().
rCur() == FirstName->range().rCur()) {
159 D.
fix(
"remove unused binding")
171void VariableLookupAnalysis::lookupVar(
const ExprVar &Var,
172 const std::shared_ptr<EnvNode> &Env) {
173 const auto &Name = Var.
id().
name();
174 const auto *CurEnv = Env.get();
175 std::shared_ptr<Definition> Def;
176 std::vector<const EnvNode *> WithEnvs;
177 for (; CurEnv; CurEnv = CurEnv->parent()) {
178 if (CurEnv->defs().contains(Name)) {
179 Def = CurEnv->defs().at(Name);
190 if (CurEnv->isWith()) {
191 WithEnvs.emplace_back(CurEnv);
198 }
else if (!WithEnvs.empty()) {
202 std::vector<const ExprWith *> WithScopes;
203 for (
const auto *WithEnv : WithEnvs) {
204 Def = WithDefs.at(WithEnv->syntax());
206 WithScopes.push_back(
static_cast<const ExprWith *
>(WithEnv->syntax()));
208 VarWithScopes.insert({&Var, std::move(WithScopes)});
214 assert(
false &&
"primop name should be defined");
218 Diags.emplace_back(Diagnostic::DK_PrimOpNeedsPrefix, Var.
range());
219 D.
fix(
"use `builtins.` prefix")
230 Diags.emplace_back(Diagnostic::DK_UndefinedVariable, Var.
range());
237void VariableLookupAnalysis::dfs(
const ExprLambda &Lambda,
238 const std::shared_ptr<EnvNode> &Env) {
244 DefBuilder DBuilder(Diags);
245 assert(Lambda.
arg());
246 const LambdaArg &Arg = *Lambda.
arg();
252 ToDef.insert_or_assign(Arg.
id(),
253 DBuilder.add(Arg.
id()->
name(), Arg.
id(),
259 ToDef.insert_or_assign(Arg.
id(),
260 DBuilder.add(Arg.
id()->
name(), Arg.
id(),
278 for (
const auto &[Name, Formal] : Arg.
formals()->
dedup()) {
282 ToDef.insert_or_assign(Formal->id(),
283 DBuilder.add(Name, Formal->id(), Source,
288 auto NewEnv = std::make_shared<EnvNode>(Env, DBuilder.finish(), &Lambda);
292 if (
const Expr *Def = Formal->defaultExpr()) {
298 dfs(*Lambda.
body(), NewEnv);
300 emitEnvLivenessWarning(NewEnv);
303void VariableLookupAnalysis::dfsDynamicAttrs(
304 const std::vector<Attribute> &DynamicAttrs,
305 const std::shared_ptr<EnvNode> &Env) {
306 for (
const auto &Attr : DynamicAttrs) {
309 dfs(Attr.
key(), Env);
310 dfs(*Attr.
value(), Env);
314std::shared_ptr<EnvNode> VariableLookupAnalysis::dfsAttrs(
315 const SemaAttrs &SA,
const std::shared_ptr<EnvNode> &Env,
319 DefBuilder DB(Diags);
321 for (
const auto &[Name, Attr] : SA.
staticAttrs()) {
322 ToDef.insert_or_assign(
324 DB.add(Name, &Attr.
key(), Source, checkInheritedFromBuiltin(Attr)));
327 auto NewEnv = std::make_shared<EnvNode>(Env, DB.finish(), Syntax);
334 dfs(*Attr.
value(), NewEnv);
337 dfs(*Attr.
value(), Env);
349 dfs(*Attr.
value(), Env);
356void VariableLookupAnalysis::dfs(
const ExprAttrs &Attrs,
357 const std::shared_ptr<EnvNode> &Env) {
358 const SemaAttrs &SA = Attrs.
sema();
359 std::shared_ptr<EnvNode> NewEnv =
363 "NewEnv must be created for recursive attrset");
364 if (!NewEnv->isLive()) {
365 Diagnostic &D = Diags.emplace_back(Diagnostic::DK_ExtraRecursive,
367 D.
fix(
"remove `rec` keyword")
374void VariableLookupAnalysis::dfs(
const ExprLet &Let,
375 const std::shared_ptr<EnvNode> &Env) {
378 auto GetLetEnv = [&Env, &Let,
this]() -> std::shared_ptr<EnvNode> {
387 const SemaAttrs &SA = Let.
attrs()->
sema();
388 assert(SA.
isRecursive() &&
"let ... in ... attrset must be recursive");
389 checkLetInheritBuiltins(SA);
393 auto LetEnv = GetLetEnv();
396 dfs(*Let.
expr(), LetEnv);
397 emitEnvLivenessWarning(LetEnv);
400void VariableLookupAnalysis::trivialDispatch(
401 const Node &Root,
const std::shared_ptr<EnvNode> &Env) {
402 for (
const Node *Ch : Root.
children()) {
409void VariableLookupAnalysis::dfs(
const ExprWith &With,
410 const std::shared_ptr<EnvNode> &Env) {
411 auto NewEnv = std::make_shared<EnvNode>(Env,
EnvNode::DefMap{}, &With);
412 if (!WithDefs.contains(&With)) {
415 ToDef.insert_or_assign(&With.
kwWith(), NewDef);
416 WithDefs.insert_or_assign(&With, NewDef);
420 dfs(*With.
with(), Env);
423 dfs(*With.
expr(), NewEnv);
425 if (WithDefs.at(&With)->uses().empty()) {
427 Diags.emplace_back(Diagnostic::DK_ExtraWith, With.
kwWith().
range());
428 Fix &F = D.
fix(
"remove `with` expression")
437void VariableLookupAnalysis::checkBuiltins(
const ExprSelect &Sel) {
445 if (Sel.
expr().
kind() != Node::NK_ExprVar)
448 const auto &Builtins =
static_cast<const ExprVar &
>(Sel.
expr());
449 if (Builtins.id().name() !=
"builtins")
452 const auto &AP = *Sel.
path();
454 if (AP.names().size() != 1)
457 AttrName &First = *AP.
names()[0];
465 Diagnostic &D = Diags.emplace_back(Diagnostic::DK_PrimOpRemovablePrefix,
468 D.
fix(
"remove `builtins.` prefix")
480 if (!isBuiltinConstant(Name)) {
481 Diagnostic &D = Diags.emplace_back(Diagnostic::DK_PrimOpUnknown,
482 AP.names()[0]->range());
489void VariableLookupAnalysis::checkLetInheritBuiltins(
const SemaAttrs &SA) {
490 for (
const auto &[Name, Attr] : SA.
staticAttrs()) {
491 if (!checkInheritedFromBuiltin(Attr))
496 Diagnostic &D = Diags.emplace_back(Diagnostic::DK_PrimOpRemovablePrefix,
498 D.
fix(
"remove unnecessary inherit")
504void VariableLookupAnalysis::dfs(
const Node &Root,
505 const std::shared_ptr<EnvNode> &Env) {
506 Envs.insert({&Root, Env});
507 switch (Root.
kind()) {
508 case Node::NK_ExprVar: {
509 const auto &Var =
static_cast<const ExprVar &
>(Root);
513 case Node::NK_ExprLambda: {
514 const auto &Lambda =
static_cast<const ExprLambda &
>(Root);
518 case Node::NK_ExprAttrs: {
519 const auto &Attrs =
static_cast<const ExprAttrs &
>(Root);
523 case Node::NK_ExprLet: {
524 const auto &Let =
static_cast<const ExprLet &
>(Root);
528 case Node::NK_ExprWith: {
529 const auto &With =
static_cast<const ExprWith &
>(Root);
533 case Node::NK_ExprSelect: {
534 trivialDispatch(Root, Env);
535 const auto &Sel =
static_cast<const ExprSelect &
>(Root);
540 trivialDispatch(Root, Env);
546 DefBuilder DB(Diags);
549 if (!Info.Internal) {
555 for (
const auto &Builtin : Constants)
556 DB.addBuiltin(Builtin);
558 DB.addBuiltin(
"builtins");
560 DB.addBuiltin(std::string(
"__curPos"));
562 auto Env = std::make_shared<EnvNode>(
nullptr, DB.finish(),
nullptr);
571 if (!Envs.contains(N))
573 return Envs.at(N).get();
Lookup variable names, from it's parent scope.
const std::string & staticName() const
const std::vector< std::shared_ptr< AttrName > > & names() const
AttributeKind kind() const
@ 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 Expr * desugaredFrom() 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)
LexerCursorRange range() const
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)
static TextEdit mkInsertion(LexerCursor P, std::string NewText)
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)
PrimopLookupResult lookupGlobalPrimOpInfo(const std::string &Name)
Look up information about a global primop by name.
std::map< std::string, nixf::PrimOpInfo > PrimOpsInfo
@ PrefixedFound
The primop was found, but needs "builtin." prefix.
@ NotFound
The primop was not found.
@ Found
The primop was found with an exact match.