Skip to content

Commit

Permalink
nixd/Sema: lowering for OpHasAttrs
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Sep 23, 2023
1 parent e51e85c commit a9bb703
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
28 changes: 28 additions & 0 deletions nixd/lib/Sema/Lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,24 @@ void ExprAttrsBuilder::addInherited(const syntax::InheritedAttribute &IA) {
}
}

nix::AttrPath Lowering::lowerAttrPath(const syntax::AttrPath &Path) {
nix::AttrPath Ret;
for (Node *Name : Path.Names) {
switch (Name->getKind()) {
case Node::NK_Identifier: {
nix::Symbol Sym = dynamic_cast<syntax::Identifier *>(Name)->Symbol;
Ret.emplace_back(Sym);
break;
}
default: {
nix::Expr *E = lower(Name);
Ret.emplace_back(E);
}
}
}
return Ret;
}

nix::Expr *Lowering::lowerOp(const syntax::Node *Op) {
if (!Op)
return nullptr;
Expand Down Expand Up @@ -471,6 +489,15 @@ nix::Expr *Lowering::lowerOp(const syntax::Node *Op) {
return lowerLegalOp<nix::ExprOpUpdate, syntax::OpUpdate>(Op);
case Node::NK_OpConcatLists:
return lowerLegalOp<nix::ExprOpConcatLists, syntax::OpConcatLists>(Op);

case Node::NK_OpHasAttr: {
const auto *OpHasAttr = dynamic_cast<const syntax::OpHasAttr *>(Op);
assert(OpHasAttr->Path && "OpHasAttr->Path must not be nullptr! (parser?)");
nix::AttrPath Path = lowerAttrPath(*OpHasAttr->Path);
nix::Expr *Body = lower(OpHasAttr->Operand);
auto *NixOpHasAttr = new nix::ExprOpHasAttr(Body, std::move(Path));
return Ctx.Pool.record(NixOpHasAttr);
}
default:
llvm_unreachable("do not know how to lower this op!");
}
Expand Down Expand Up @@ -545,6 +572,7 @@ nix::Expr *Lowering::lower(const syntax::Node *Root) {
// operators
case Node::NK_OpNot:
case Node::NK_OpNegate:
case Node::NK_OpHasAttr:
#define BIN_OP(NAME, _) case Node::NK_##NAME:
#include "nixd/Syntax/BinaryOps.inc"
#undef BIN_OP
Expand Down
4 changes: 4 additions & 0 deletions nixd/tools/nixd-lint/test/lowering-ops.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@

# CHECK: ExprOpConcatLists: (1 ++ 2)
Concat = 1 ++ 2;

# TODO: check that we can deal with dynamic attrpath
# CHECK: ExprOpHasAttr: ((1) ? a.b.c)
HasAttr = 1 ? a.b.c;
}

0 comments on commit a9bb703

Please sign in to comment.