@@ -76,6 +76,11 @@ impl Decl {
7676 Self :: new ( DeclKind :: Import ( import) , span)
7777 }
7878
79+ pub fn extern_block ( extern_block : ExternBlock ) -> Self {
80+ let span = extern_block. span ;
81+ Self :: new ( DeclKind :: ExternBlock ( extern_block) , span)
82+ }
83+
7984 /// 创建模块声明
8085 pub fn module ( module : Module ) -> Self {
8186 let span = module. span ;
@@ -95,6 +100,7 @@ impl Decl {
95100 DeclKind :: Const ( c) => Some ( & c. name ) ,
96101 DeclKind :: Static ( s) => Some ( & s. name ) ,
97102 DeclKind :: Import ( _) => None ,
103+ DeclKind :: ExternBlock ( _) => None ,
98104 DeclKind :: Module ( m) => Some ( & m. name ) ,
99105 }
100106 }
@@ -112,6 +118,7 @@ impl Decl {
112118 DeclKind :: Const ( c) => c. vis . is_public ( ) ,
113119 DeclKind :: Static ( s) => s. vis . is_public ( ) ,
114120 DeclKind :: Import ( _) => true ,
121+ DeclKind :: ExternBlock ( _) => true ,
115122 DeclKind :: Module ( m) => m. vis . is_public ( ) ,
116123 }
117124 }
@@ -156,6 +163,9 @@ pub enum DeclKind {
156163 /// 导入 `import ...`
157164 Import ( Import ) ,
158165
166+ /// extern block `extern "C" { ... }`
167+ ExternBlock ( ExternBlock ) ,
168+
159169 /// 模块 `mod name { ... }`
160170 Module ( Module ) ,
161171}
@@ -169,8 +179,14 @@ pub struct Function {
169179 pub params : Vec < Param > ,
170180 pub self_param : Option < SelfParam > ,
171181 pub return_type : Option < Type > ,
182+ pub precondition : Option < Box < super :: Expr > > ,
183+ pub postcondition : Option < Box < super :: Expr > > ,
172184 pub body : Block ,
173185 pub is_async : bool ,
186+ pub abi : Option < String > ,
187+ pub is_unsafe : bool ,
188+ pub no_mangle : bool ,
189+ pub export_name : Option < String > ,
174190 pub span : Span ,
175191}
176192
@@ -183,8 +199,14 @@ impl Function {
183199 params : Vec :: new ( ) ,
184200 self_param : None ,
185201 return_type : None ,
202+ precondition : None ,
203+ postcondition : None ,
186204 body,
187205 is_async : false ,
206+ abi : None ,
207+ is_unsafe : false ,
208+ no_mangle : false ,
209+ export_name : None ,
188210 span,
189211 }
190212 }
@@ -226,6 +248,72 @@ impl Node for Function {
226248 }
227249}
228250
251+ /// extern 声明块
252+ #[ derive( Debug , Clone , PartialEq ) ]
253+ pub struct ExternBlock {
254+ pub abi : String ,
255+ pub link_name : Option < String > ,
256+ pub items : Vec < ExternItem > ,
257+ pub span : Span ,
258+ }
259+
260+ impl ExternBlock {
261+ pub fn new ( abi : impl Into < String > , span : Span ) -> Self {
262+ Self {
263+ abi : abi. into ( ) ,
264+ link_name : None ,
265+ items : Vec :: new ( ) ,
266+ span,
267+ }
268+ }
269+ }
270+
271+ impl Node for ExternBlock {
272+ fn span ( & self ) -> Span {
273+ self . span
274+ }
275+ }
276+
277+ /// extern 块条目
278+ #[ derive( Debug , Clone , PartialEq ) ]
279+ pub enum ExternItem {
280+ Function ( ExternFunction ) ,
281+ Static ( ExternStatic ) ,
282+ }
283+
284+ /// extern 函数声明
285+ #[ derive( Debug , Clone , PartialEq ) ]
286+ pub struct ExternFunction {
287+ pub vis : Visibility ,
288+ pub name : Ident ,
289+ pub params : Vec < Param > ,
290+ pub return_type : Option < Type > ,
291+ pub is_unsafe : bool ,
292+ pub span : Span ,
293+ }
294+
295+ impl Node for ExternFunction {
296+ fn span ( & self ) -> Span {
297+ self . span
298+ }
299+ }
300+
301+ /// extern 静态变量声明
302+ #[ derive( Debug , Clone , PartialEq ) ]
303+ pub struct ExternStatic {
304+ pub vis : Visibility ,
305+ pub is_mut : bool ,
306+ pub name : Ident ,
307+ pub ty : Type ,
308+ pub span : Span ,
309+ }
310+
311+ impl Node for ExternStatic {
312+ fn span ( & self ) -> Span {
313+ self . span
314+ }
315+ }
316+
229317/// 结构体声明
230318#[ derive( Debug , Clone , PartialEq ) ]
231319pub struct Struct {
0 commit comments