1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use sway_ast::{
attribute::{Attribute, AttributeArg, CFG_ATTRIBUTE_NAME, DOC_COMMENT_ATTRIBUTE_NAME},
brackets::SquareBrackets,
keywords::{HashBangToken, HashToken, Token},
AttributeDecl, Literal, Parens, Punctuated,
};
use sway_types::{Ident, Span, Spanned};
use crate::assert_insert_span;
use super::{Modifier, New};
#[allow(dead_code)]
impl Modifier<'_, Attribute> {
pub(crate) fn set_name<S: AsRef<str> + ?Sized>(&mut self, name: &S) -> &mut Self {
// We preserve the current span of the name.
let insert_span = self.element.name.span();
self.element.name = Ident::new_with_override(name.as_ref().into(), insert_span);
self
}
}
#[allow(dead_code)]
impl New {
/// Creates an [Attribute] with a single [AttributeArg]. E.g. `attribute_name(arg_name = value)` or `attribute_name(arg_name)`.
pub(crate) fn attribute_with_arg<S: AsRef<str> + ?Sized>(
insert_span: Span,
attribute_name: &S,
arg_name: &S,
value: Option<Literal>,
) -> Attribute {
assert_insert_span!(insert_span);
Attribute {
name: Ident::new_with_override(attribute_name.as_ref().into(), insert_span.clone()),
args: Some(Parens {
inner: Punctuated {
value_separator_pairs: vec![],
final_value_opt: Some(Box::new(AttributeArg {
name: Ident::new_with_override(
arg_name.as_ref().into(),
insert_span.clone(),
),
value,
})),
},
span: insert_span,
}),
}
}
/// Creates an [AttributeDecl] with a single [Attribute] that has a single [AttributeArg]. E.g. `#[attribute_name(arg_name = value)]` or `#[attribute_name(arg_name)]`.
pub(crate) fn attribute_decl_with_arg<S: AsRef<str> + ?Sized>(
insert_span: Span,
attribute_name: &S,
arg_name: &S,
value: Option<Literal>,
) -> AttributeDecl {
assert_insert_span!(insert_span);
AttributeDecl {
hash_kind: sway_ast::attribute::AttributeHashKind::Outer(HashToken::new(
insert_span.clone(),
)),
attribute: SquareBrackets {
inner: Punctuated {
value_separator_pairs: vec![],
final_value_opt: Some(Box::new(New::attribute_with_arg(
insert_span.clone(),
attribute_name,
arg_name,
value,
))),
},
span: insert_span,
},
}
}
/// Creates an [AttributeDecl] representing a single `cfg` experimental attribute. E.g. `#[cfg(experimental_flag = value)]`.
pub(crate) fn cfg_experimental_attribute_decl(
insert_span: Span,
feature_name: &str,
value: bool,
) -> AttributeDecl {
assert_insert_span!(insert_span);
AttributeDecl {
hash_kind: sway_ast::attribute::AttributeHashKind::Outer(HashToken::new(
insert_span.clone(),
)),
attribute: SquareBrackets {
inner: Punctuated {
value_separator_pairs: vec![],
final_value_opt: Some(Box::new(New::attribute_with_arg(
insert_span.clone(),
CFG_ATTRIBUTE_NAME,
&format!("experimental_{feature_name}"),
Some(New::literal_bool(insert_span.clone(), value)),
))),
},
span: insert_span,
},
}
}
/// Creates a `doc-comment` [AttributeDecl] that defines a single doc-comment line.
/// It automatically inserts the leading space.
///
/// E.g., `comment` "This is a comment." will create an [AttributeDecl] that corresponds to:
/// ```ignore
/// //! This is a comment.
/// ```
pub(crate) fn doc_comment_attribute_decl<S: AsRef<str> + ?Sized>(
insert_span: Span,
comment: &S,
) -> AttributeDecl {
assert_insert_span!(insert_span);
AttributeDecl {
hash_kind: sway_ast::attribute::AttributeHashKind::Inner(HashBangToken::new(
insert_span.clone(),
)),
attribute: SquareBrackets {
inner: Punctuated {
value_separator_pairs: vec![],
final_value_opt: Some(Box::new(New::attribute_with_arg(
insert_span.clone(),
DOC_COMMENT_ATTRIBUTE_NAME,
&format!(" {}", comment.as_ref()),
None,
))),
},
span: insert_span,
},
}
}
/// Creates `doc-comment` [AttributeDecl]s that define multiple doc-comment lines.
/// It automatically inserts the leading space into each line.
///
/// E.g., `comments` \["This is a comment.", "This is the second line."\] will create
/// [AttributeDecl]s that corresponds to:
/// ```ignore
/// //! This is a comment.
/// //! This is the second line.
/// ```
pub(crate) fn doc_comments_attribute_decls<S: AsRef<str> + ?Sized>(
insert_span: Span,
comments: &[&S],
) -> Vec<AttributeDecl> {
comments
.iter()
.map(|comment| New::doc_comment_attribute_decl(insert_span.clone(), comment))
.collect()
}
}