Check source function annotations and unify managed function identity
This commit is contained in:
@@ -4,6 +4,6 @@
|
||||
| --- | --- |
|
||||
| [lib.rs](lib.rs) | CompileOptions、公开分析/编译入口、AST 原文范围检查与带来源诊断 |
|
||||
| [analysis.rs](analysis.rs) | 模块/函数/类方法/表达式/单次交互作用域、局部槽、全局名称、逐引用绑定和初始化检查 |
|
||||
| [codegen.rs](codegen.rs) | AST 到函数与类创建指令、嵌套代码模板、EndorCode 验证、显式 VM 装载与临时根释放 |
|
||||
| [codegen.rs](codegen.rs) | AST 到函数与类创建指令、嵌套代码模板与源码签名保留、EndorCode 验证、显式 VM 装载与临时根释放 |
|
||||
|
||||
节点定义在 frontend/Endor.asdl;这里匹配生成的 AST,不复制节点类型。源码和调用方提供的 AST 都先分析后生成,不执行 AST。修改名称语义、代码布局或所有权时同步 [编译契约](../../../docs/runtime/compiler.md)、[检查](../tests/README.md) 与 [示例](../examples/README.md)。返回 [compiler](../README.md)。
|
||||
|
||||
@@ -25,6 +25,9 @@ impl CompiledCode {
|
||||
pub fn name(&self) -> &str {
|
||||
self.code.name()
|
||||
}
|
||||
pub fn source_signature(&self) -> Option<&endor_core::SourceSignature> {
|
||||
self.code.source_signature()
|
||||
}
|
||||
pub fn filename(&self) -> &str {
|
||||
self.code.filename()
|
||||
}
|
||||
@@ -47,7 +50,7 @@ impl CompiledCode {
|
||||
roots.push(reference);
|
||||
constants[*index] = Value::Object(reference);
|
||||
}
|
||||
let code = EndorCode::with_layout(
|
||||
let mut code = EndorCode::with_layout(
|
||||
self.code.bytecode().to_vec(),
|
||||
constants,
|
||||
self.code.layout().clone(),
|
||||
@@ -57,6 +60,9 @@ impl CompiledCode {
|
||||
self.code.filename(),
|
||||
self.code.first_line(),
|
||||
);
|
||||
if let Some(signature) = self.code.source_signature() {
|
||||
code = code.with_source_signature(signature.clone())?;
|
||||
}
|
||||
vm.new_code(code)
|
||||
})();
|
||||
for reference in roots {
|
||||
@@ -152,7 +158,7 @@ impl<'a> Emitter<'a> {
|
||||
fn declaration(&mut self, declaration: &Decl, symbols: &SymbolTable) -> Result<(), Diagnostic> {
|
||||
match &declaration.kind {
|
||||
DeclKind::Function(function) => {
|
||||
self.function_value(function, symbols)?;
|
||||
self.function_value(function, symbols, None)?;
|
||||
self.emit(Opcode::StoreGlobal, self.name_index(&function.name.text))
|
||||
}
|
||||
DeclKind::Class(class) => {
|
||||
@@ -169,7 +175,7 @@ impl<'a> Emitter<'a> {
|
||||
self.emit(Opcode::BuildMap, class.fields.len())?;
|
||||
for method in &class.methods {
|
||||
self.constant(Value::Text(method.name.text.clone()))?;
|
||||
self.function_value(method, symbols)?;
|
||||
self.function_value(method, symbols, Some(&class.name.text))?;
|
||||
}
|
||||
self.emit(Opcode::BuildMap, class.methods.len())?;
|
||||
self.simple(Opcode::BuildClass);
|
||||
@@ -182,6 +188,7 @@ impl<'a> Emitter<'a> {
|
||||
&mut self,
|
||||
function: &frontend::ast::Function,
|
||||
symbols: &SymbolTable,
|
||||
owner: Option<&str>,
|
||||
) -> Result<(), Diagnostic> {
|
||||
let scope = symbols
|
||||
.functions
|
||||
@@ -203,7 +210,19 @@ impl<'a> Emitter<'a> {
|
||||
body.constant(Value::Nil)?;
|
||||
body.simple(Opcode::ReturnValue);
|
||||
}
|
||||
let child = body.finish()?;
|
||||
let mut child = body.finish()?;
|
||||
child.code = child
|
||||
.code
|
||||
.with_source_signature(endor_core::SourceSignature {
|
||||
parameters: function
|
||||
.parameters
|
||||
.iter()
|
||||
.map(|p| p.type_name.text.clone())
|
||||
.collect(),
|
||||
return_type: function.return_type.text.clone(),
|
||||
method_owner: owner.map(str::to_owned),
|
||||
})
|
||||
.map_err(|failure| error(function.span, failure.to_string()))?;
|
||||
let index = self.constants.len();
|
||||
self.children.push((index, child));
|
||||
self.constant(Value::Nil)?; // Replaced by a managed code object during load().
|
||||
|
||||
Reference in New Issue
Block a user