mirror of
https://github.com/RustPython/Parser.git
synced 2025-09-04 09:31:31 +00:00
usize::to_u32 for compiler internal conversion
This commit is contained in:
parent
461ef3d71d
commit
0cd04ca2de
1 changed files with 33 additions and 21 deletions
|
@ -5,6 +5,8 @@
|
||||||
//! <https://github.com/python/cpython/blob/main/Python/compile.c>
|
//! <https://github.com/python/cpython/blob/main/Python/compile.c>
|
||||||
//! <https://github.com/micropython/micropython/blob/master/py/compile.c>
|
//! <https://github.com/micropython/micropython/blob/master/py/compile.c>
|
||||||
|
|
||||||
|
#![deny(clippy::cast_possible_truncation)]
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
error::{CodegenError, CodegenErrorType},
|
error::{CodegenError, CodegenErrorType},
|
||||||
ir,
|
ir,
|
||||||
|
@ -332,7 +334,8 @@ impl Compiler {
|
||||||
let cache = cache(self.current_codeinfo());
|
let cache = cache(self.current_codeinfo());
|
||||||
cache
|
cache
|
||||||
.get_index_of(name.as_ref())
|
.get_index_of(name.as_ref())
|
||||||
.unwrap_or_else(|| cache.insert_full(name.into_owned()).0) as u32
|
.unwrap_or_else(|| cache.insert_full(name.into_owned()).0)
|
||||||
|
.to_u32()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile_program(
|
fn compile_program(
|
||||||
|
@ -900,7 +903,7 @@ impl Compiler {
|
||||||
let have_defaults = !args.defaults.is_empty();
|
let have_defaults = !args.defaults.is_empty();
|
||||||
if have_defaults {
|
if have_defaults {
|
||||||
// Construct a tuple:
|
// Construct a tuple:
|
||||||
let size = args.defaults.len() as u32;
|
let size = args.defaults.len().to_u32();
|
||||||
for element in &args.defaults {
|
for element in &args.defaults {
|
||||||
self.compile_expression(element)?;
|
self.compile_expression(element)?;
|
||||||
}
|
}
|
||||||
|
@ -921,7 +924,7 @@ impl Compiler {
|
||||||
emit!(
|
emit!(
|
||||||
self,
|
self,
|
||||||
Instruction::BuildMap {
|
Instruction::BuildMap {
|
||||||
size: args.kw_defaults.len() as u32,
|
size: args.kw_defaults.len().to_u32(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -936,11 +939,9 @@ impl Compiler {
|
||||||
|
|
||||||
self.push_output(
|
self.push_output(
|
||||||
bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED,
|
bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED,
|
||||||
args.posonlyargs.len().try_into().unwrap(),
|
args.posonlyargs.len().to_u32(),
|
||||||
(args.posonlyargs.len() + args.args.len())
|
(args.posonlyargs.len() + args.args.len()).to_u32(),
|
||||||
.try_into()
|
args.kwonlyargs.len().to_u32(),
|
||||||
.unwrap(),
|
|
||||||
args.kwonlyargs.len().try_into().unwrap(),
|
|
||||||
name.to_owned(),
|
name.to_owned(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1269,12 +1270,12 @@ impl Compiler {
|
||||||
if let SymbolScope::Free = symbol.scope {
|
if let SymbolScope::Free = symbol.scope {
|
||||||
idx += parent_code.cellvar_cache.len();
|
idx += parent_code.cellvar_cache.len();
|
||||||
}
|
}
|
||||||
emit!(self, Instruction::LoadClosure(idx as u32))
|
emit!(self, Instruction::LoadClosure(idx.to_u32()))
|
||||||
}
|
}
|
||||||
emit!(
|
emit!(
|
||||||
self,
|
self,
|
||||||
Instruction::BuildTuple {
|
Instruction::BuildTuple {
|
||||||
size: code.freevars.len() as u32,
|
size: code.freevars.len().to_u32(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
true
|
true
|
||||||
|
@ -1370,7 +1371,7 @@ impl Compiler {
|
||||||
.position(|var| *var == "__class__");
|
.position(|var| *var == "__class__");
|
||||||
|
|
||||||
if let Some(classcell_idx) = classcell_idx {
|
if let Some(classcell_idx) = classcell_idx {
|
||||||
emit!(self, Instruction::LoadClosure(classcell_idx as u32));
|
emit!(self, Instruction::LoadClosure(classcell_idx.to_u32()));
|
||||||
emit!(self, Instruction::Duplicate);
|
emit!(self, Instruction::Duplicate);
|
||||||
let classcell = self.name("__classcell__");
|
let classcell = self.name("__classcell__");
|
||||||
emit!(self, Instruction::StoreLocal(classcell));
|
emit!(self, Instruction::StoreLocal(classcell));
|
||||||
|
@ -1775,7 +1776,7 @@ impl Compiler {
|
||||||
emit!(
|
emit!(
|
||||||
self,
|
self,
|
||||||
Instruction::UnpackSequence {
|
Instruction::UnpackSequence {
|
||||||
size: elts.len() as u32,
|
size: elts.len().to_u32(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -2175,7 +2176,7 @@ impl Compiler {
|
||||||
emit!(
|
emit!(
|
||||||
self,
|
self,
|
||||||
Instruction::BuildString {
|
Instruction::BuildString {
|
||||||
size: values.len() as u32,
|
size: values.len().to_u32(),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -2244,7 +2245,7 @@ impl Compiler {
|
||||||
emit!(
|
emit!(
|
||||||
compiler,
|
compiler,
|
||||||
Instruction::ListAppend {
|
Instruction::ListAppend {
|
||||||
i: generators.len() as u32,
|
i: generators.len().to_u32(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -2263,7 +2264,7 @@ impl Compiler {
|
||||||
emit!(
|
emit!(
|
||||||
compiler,
|
compiler,
|
||||||
Instruction::SetAdd {
|
Instruction::SetAdd {
|
||||||
i: generators.len() as u32,
|
i: generators.len().to_u32(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -2289,7 +2290,7 @@ impl Compiler {
|
||||||
emit!(
|
emit!(
|
||||||
compiler,
|
compiler,
|
||||||
Instruction::MapAdd {
|
Instruction::MapAdd {
|
||||||
i: generators.len() as u32,
|
i: generators.len().to_u32(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -2420,7 +2421,7 @@ impl Compiler {
|
||||||
args: &[ast::Expr],
|
args: &[ast::Expr],
|
||||||
keywords: &[ast::Keyword],
|
keywords: &[ast::Keyword],
|
||||||
) -> CompileResult<CallType> {
|
) -> CompileResult<CallType> {
|
||||||
let count = (args.len() + keywords.len()) as u32 + additional_positional;
|
let count = (args.len() + keywords.len()).to_u32() + additional_positional;
|
||||||
|
|
||||||
// Normal arguments:
|
// Normal arguments:
|
||||||
let (size, unpack) = self.gather_elements(additional_positional, args)?;
|
let (size, unpack) = self.gather_elements(additional_positional, args)?;
|
||||||
|
@ -2521,7 +2522,7 @@ impl Compiler {
|
||||||
for element in elements {
|
for element in elements {
|
||||||
self.compile_expression(element)?;
|
self.compile_expression(element)?;
|
||||||
}
|
}
|
||||||
before + elements.len() as u32
|
before + elements.len().to_u32()
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok((size, has_stars))
|
Ok((size, has_stars))
|
||||||
|
@ -2710,7 +2711,7 @@ impl Compiler {
|
||||||
|
|
||||||
fn emit_constant(&mut self, constant: ConstantData) {
|
fn emit_constant(&mut self, constant: ConstantData) {
|
||||||
let info = self.current_codeinfo();
|
let info = self.current_codeinfo();
|
||||||
let idx = info.constants.insert_full(constant).0 as u32;
|
let idx = info.constants.insert_full(constant).0.to_u32();
|
||||||
self.emit_arg(idx, |idx| Instruction::LoadConst { idx })
|
self.emit_arg(idx, |idx| Instruction::LoadConst { idx })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2725,7 +2726,7 @@ impl Compiler {
|
||||||
|
|
||||||
fn new_block(&mut self) -> ir::BlockIdx {
|
fn new_block(&mut self) -> ir::BlockIdx {
|
||||||
let code = self.current_codeinfo();
|
let code = self.current_codeinfo();
|
||||||
let idx = ir::BlockIdx(code.blocks.len() as u32);
|
let idx = ir::BlockIdx(code.blocks.len().to_u32());
|
||||||
code.blocks.push(ir::Block::default());
|
code.blocks.push(ir::Block::default());
|
||||||
idx
|
idx
|
||||||
}
|
}
|
||||||
|
@ -2753,7 +2754,7 @@ impl Compiler {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_source_line_number(&self) -> u32 {
|
fn get_source_line_number(&self) -> u32 {
|
||||||
self.current_source_location.row() as u32
|
self.current_source_location.row().to_u32()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push_qualified_path(&mut self, name: &str) {
|
fn push_qualified_path(&mut self, name: &str) {
|
||||||
|
@ -2849,6 +2850,17 @@ fn compile_constant(value: &ast::Constant) -> ConstantData {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Note: Not a good practice in general. Keep this trait private only for compiler
|
||||||
|
trait ToU32 {
|
||||||
|
fn to_u32(self) -> u32;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToU32 for usize {
|
||||||
|
fn to_u32(self) -> u32 {
|
||||||
|
self.try_into().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue