nixd
Loading...
Searching...
No Matches
SemaActions.h
Go to the documentation of this file.
1/// \file
2/// \brief Semantic Actions while building the AST
3///
4/// Non grammatical errors (e.g. duplicating) are detected here.
5
10#include "nixf/Basic/Range.h"
11
12#include <map>
13
14namespace nixf {
15
16class Sema {
17 std::string_view Src;
18 std::vector<Diagnostic> &Diags;
19
20public:
21 Sema(std::string_view Src, std::vector<Diagnostic> &Diags)
22 : Src(Src), Diags(Diags) {}
23
25
26 /// \brief Make text edits to remove a formal
27 static void removeFormal(Fix &F, const FormalVector::const_iterator &Rm,
28 const FormalVector &FV);
29
30 /// \brief Check if there is a seperator "," between formals
31 void checkFormalSep(const FormalVector &FV);
32
33 /// \brief Check if ellipsis "..."
34 void checkFormalEllipsis(const FormalVector &FV);
35
36 /// \brief Diagnose empty formal i.e. single comma
37 //
38 // e.g. `{ , } : 1`
39 void checkFormalEmpty(const FormalVector &FV);
40
41 /// \brief Deduplicate formals.
42 void dedupFormal(std::map<std::string, const Formal *> &Dedup,
43 const FormalVector &FV);
44
45 std::shared_ptr<Formals> onFormals(LexerCursorRange Range, FormalVector FV);
46
47 /// \brief Desugar inherit (expr) a, inherit a, into select, or variable.
48 static std::pair<std::shared_ptr<Expr>, Attribute::AttributeKind>
49 desugarInheritExpr(std::shared_ptr<AttrName> Name, std::shared_ptr<Expr> E);
50
51 void dupAttr(std::string Name, LexerCursorRange Range, LexerCursorRange Prev);
52
53 /// \brief Check if these two attrsets has the same "recursive" modifier.
54 ///
55 /// Official nix implementation implicitly discards the second modifier, this
56 /// is somehow error-prone, let's detect it.
57 void checkAttrRecursiveForMerge(const ExprAttrs &XAttrs,
58 const ExprAttrs &YAttrs);
59
60 /// \brief Perform attrsets merging while duplicated fields are both attrsets.
61 ///
62 /// e.g.
63 /// \code{nix}
64 /// {
65 /// a = { x = 1; };
66 /// a = { y = 1; };
67 /// }
68 /// \endcode
69 /// We may want to merge both "a = " attrsets into a single one, instead of
70 /// report duplicating attrs.
71 void mergeAttrSets(SemaAttrs &XAttrs, const SemaAttrs &YAttrs);
72
73 /// \note Name must not be null
74 void insertAttr(SemaAttrs &SA, std::shared_ptr<AttrName> Name,
75 std::shared_ptr<Expr> E, Attribute::AttributeKind Kind);
76
77 /// Select into \p Attr the attribute specified by \p Path, or create one if
78 /// not exists, until reached the inner-most attr. Similar to `mkdir -p`.
79 ///
80 /// \return The selected or created attribute.
82 const std::vector<std::shared_ptr<AttrName>> &Path);
83
84 /// Insert the binding: `AttrPath = E;` into \p Attr
85 void addAttr(SemaAttrs &Attr, const AttrPath &Path, std::shared_ptr<Expr> E);
86
87 void lowerInheritName(SemaAttrs &SA, std::shared_ptr<AttrName> Name,
88 std::shared_ptr<Expr> E,
89 Attribute::AttributeKind InheritKind);
90
91 void lowerInherit(SemaAttrs &Attr, const Inherit &Inherit);
92
93 void lowerBinds(SemaAttrs &SA, const Binds &B);
94
95 std::shared_ptr<ExprAttrs> onExprAttrs(LexerCursorRange Range,
96 std::shared_ptr<Binds> Binds,
97 std::shared_ptr<Misc> Rec);
98
99 std::shared_ptr<LambdaArg> onLambdaArg(LexerCursorRange Range,
100 std::shared_ptr<Identifier> ID,
101 std::shared_ptr<Formals> F);
102};
103
104} // namespace nixf
std::vector< std::shared_ptr< Formal > > FormalVector
Definition Lambda.h:65
Attribute set after deduplication.
Definition Attrs.h:231
SemaAttrs * selectOrCreate(SemaAttrs &SA, const std::vector< std::shared_ptr< AttrName > > &Path)
void insertAttr(SemaAttrs &SA, std::shared_ptr< AttrName > Name, std::shared_ptr< Expr > E, Attribute::AttributeKind Kind)
void lowerInherit(SemaAttrs &Attr, const Inherit &Inherit)
std::shared_ptr< ExprAttrs > onExprAttrs(LexerCursorRange Range, std::shared_ptr< Binds > Binds, std::shared_ptr< Misc > Rec)
std::shared_ptr< LambdaArg > onLambdaArg(LexerCursorRange Range, std::shared_ptr< Identifier > ID, std::shared_ptr< Formals > F)
void dedupFormal(std::map< std::string, const Formal * > &Dedup, const FormalVector &FV)
Deduplicate formals.
void lowerInheritName(SemaAttrs &SA, std::shared_ptr< AttrName > Name, std::shared_ptr< Expr > E, Attribute::AttributeKind InheritKind)
void mergeAttrSets(SemaAttrs &XAttrs, const SemaAttrs &YAttrs)
Perform attrsets merging while duplicated fields are both attrsets.
void checkFormalEllipsis(const FormalVector &FV)
Check if ellipsis "...".
void lowerBinds(SemaAttrs &SA, const Binds &B)
void addAttr(SemaAttrs &Attr, const AttrPath &Path, std::shared_ptr< Expr > E)
Insert the binding: AttrPath = E; into Attr.
void checkFormalSep(const FormalVector &FV)
Check if there is a seperator "," between formals.
void dupAttr(std::string Name, LexerCursorRange Range, LexerCursorRange Prev)
Formals::FormalVector FormalVector
Definition SemaActions.h:24
void checkAttrRecursiveForMerge(const ExprAttrs &XAttrs, const ExprAttrs &YAttrs)
Check if these two attrsets has the same "recursive" modifier.
void checkFormalEmpty(const FormalVector &FV)
Diagnose empty formal i.e. single comma.
std::shared_ptr< Formals > onFormals(LexerCursorRange Range, FormalVector FV)
static void removeFormal(Fix &F, const FormalVector::const_iterator &Rm, const FormalVector &FV)
Make text edits to remove a formal.
Sema(std::string_view Src, std::vector< Diagnostic > &Diags)
Definition SemaActions.h:21
static std::pair< std::shared_ptr< Expr >, Attribute::AttributeKind > desugarInheritExpr(std::shared_ptr< AttrName > Name, std::shared_ptr< Expr > E)
Desugar inherit (expr) a, inherit a, into select, or variable.