diff --git a/src/analyze/expr.rs b/src/analyze/expr.rs index 1a3d7039..e6e9008c 100644 --- a/src/analyze/expr.rs +++ b/src/analyze/expr.rs @@ -5,7 +5,7 @@ use crate::intern::InternedStr; use crate::parse::Lexer; impl Analyzer { - pub fn parse_expr(&mut self, expr: ast::Expr) -> Expr { + pub fn expr(&mut self, expr: ast::Expr) -> Expr { use ast::ExprType::*; let _guard = self.recursion_check(); @@ -42,20 +42,20 @@ impl Analyzer { Div(left, right) => self.binary_helper(left, right, BinaryOp::Div, Self::mul), Mod(left, right) => self.binary_helper(left, right, BinaryOp::Mod, Self::mul), Assign(lval, rval, token) => { - let lval = self.parse_expr(*lval); - let rval = self.parse_expr(*rval); + let lval = self.expr(*lval); + let rval = self.expr(*rval); self.assignment_expr(lval, rval, token, expr.location) } Add(left, right) => self.binary_helper(left, right, BinaryOp::Add, Self::add), Sub(left, right) => self.binary_helper(left, right, BinaryOp::Sub, Self::add), FuncCall(func, args) => self.func_call(*func, args), Member(struct_, id) => { - let struct_ = self.parse_expr(*struct_); + let struct_ = self.expr(*struct_); self.struct_member(struct_, id, expr.location) } // s->p desguars to (*s).p DerefMember(inner, id) => { - let inner = self.parse_expr(*inner); + let inner = self.expr(*inner); let struct_type = match &inner.ctype { Type::Pointer(ctype, _) => match &**ctype { Type::Union(_) | Type::Struct(_) => (**ctype).clone(), @@ -77,7 +77,7 @@ impl Analyzer { } // `*p` or `a[i]` Deref(inner) => { - let inner = self.parse_expr(*inner); + let inner = self.expr(*inner); match &inner.ctype { Type::Array(t, _) | Type::Pointer(t, _) => { let ctype = (**t).clone(); @@ -95,7 +95,7 @@ impl Analyzer { // &x // 6.5.3.2 Address and indirection operators AddressOf(inner) => { - let inner = self.parse_expr(*inner); + let inner = self.expr(*inner); match inner.expr { // parse &*x as x // footnote 102: &*E is equivalent to E (even if E is a null pointer) @@ -141,7 +141,7 @@ impl Analyzer { self.align(ctype, expr.location) } AlignofExpr(inner) => { - let inner = self.parse_expr(*inner); + let inner = self.expr(*inner); self.align(inner.ctype, expr.location) } SizeofType(type_name) => { @@ -149,7 +149,7 @@ impl Analyzer { self.sizeof(ctype, expr.location) } SizeofExpr(inner) => { - let inner = self.parse_expr(*inner); + let inner = self.expr(*inner); self.sizeof(inner.ctype, expr.location) } BitwiseNot(inner) => self.bitwise_not(*inner), @@ -171,8 +171,8 @@ impl Analyzer { // `for(j = i, k = 0; k < n; j++, k++);` // see also https://stackoverflow.com/a/43561555/7669110 Comma(left, right) => { - let left = self.parse_expr(*left); - let right = self.parse_expr(*right).rval(); + let left = self.expr(*left); + let right = self.expr(*right).rval(); Expr { ctype: right.ctype.clone(), lval: false, @@ -185,7 +185,7 @@ impl Analyzer { } } } - // only meant for use with `parse_expr` + // only meant for use with `expr` // TODO: change ast::Expr to use `ExprType::Binary` as well, which would make this unnecessary // TODO: these functions should have the locations of the parent expression, not the children #[allow(clippy::boxed_local)] @@ -199,8 +199,8 @@ impl Analyzer { where F: FnOnce(&mut Self, Expr, Expr, BinaryOp) -> Expr, { - let left = self.parse_expr(*left); - let right = self.parse_expr(*right); + let left = self.expr(*left); + let right = self.expr(*right); expr_checker(self, left, right, op) } // left OP right, where OP is an operation that requires integral types @@ -273,8 +273,8 @@ impl Analyzer { token: ComparisonToken, ) -> Expr { let location = left.location.merge(right.location); - let mut left = self.parse_expr(left); - let mut right = self.parse_expr(right); + let mut left = self.expr(left); + let mut right = self.expr(right); // i == i if left.ctype.is_arithmetic() && right.ctype.is_arithmetic() { @@ -401,7 +401,7 @@ impl Analyzer { // 6.5.4 Cast operators fn explicit_cast(&mut self, expr: ast::Expr, ctype: Type) -> Expr { let location = expr.location; - let expr = self.parse_expr(expr).rval(); + let expr = self.expr(expr).rval(); // (void)0; if ctype == Type::Void { // casting anything to void is allowed @@ -487,7 +487,7 @@ impl Analyzer { // `func(args)` // 6.5.2.2 Function calls fn func_call(&mut self, func: ast::Expr, args: Vec) -> Expr { - let mut func = self.parse_expr(func); + let mut func = self.expr(func); // if fp is a function pointer, fp() desugars to (*fp)() match &func.ctype { Type::Pointer(pointee, _) if pointee.is_function() => { @@ -525,7 +525,7 @@ impl Analyzer { } let mut promoted_args = vec![]; for (i, arg) in args.into_iter().enumerate() { - let arg = self.parse_expr(arg); + let arg = self.expr(arg); let promoted = match functype.params.get(i) { // int f(int); f(1) Some(expected) => arg @@ -595,7 +595,7 @@ impl Analyzer { ) -> Expr { use crate::data::lex::AssignmentToken; - let expr = self.parse_expr(expr); + let expr = self.expr(expr); if let Err(err) = expr.modifiable_lval() { self.err(err, location); } else if !(expr.ctype.is_arithmetic() || expr.ctype.is_pointer()) { @@ -635,8 +635,8 @@ impl Analyzer { // a[i] desugars to *(a + i) // 6.5.2.1 Array subscripting fn index(&mut self, left: ast::Expr, right: ast::Expr, location: Location) -> Expr { - let left = self.parse_expr(left).rval(); - let right = self.parse_expr(right).rval(); + let left = self.expr(left).rval(); + let right = self.expr(right).rval(); let (target_type, array, index) = match (&left.ctype, &right.ctype) { // p[i] @@ -674,7 +674,7 @@ impl Analyzer { // ~expr // 6.5.3.3 Unary arithmetic operators fn bitwise_not(&mut self, expr: ast::Expr) -> Expr { - let expr = self.parse_expr(expr); + let expr = self.expr(expr); if !expr.ctype.is_integral() { self.err( SemanticError::NonIntegralExpr(expr.ctype.clone()), @@ -694,7 +694,7 @@ impl Analyzer { // -x and +x // 6.5.3.3 Unary arithmetic operators fn unary_add(&mut self, expr: ast::Expr, add: bool, location: Location) -> Expr { - let expr = self.parse_expr(expr); + let expr = self.expr(expr); if !expr.ctype.is_arithmetic() { self.err(SemanticError::NotArithmetic(expr.ctype.clone()), location); return expr; @@ -719,7 +719,7 @@ impl Analyzer { // 6.5.3.3 Unary arithmetic operators // > The expression !E is equivalent to (0==E). fn logical_not(&mut self, expr: ast::Expr) -> Expr { - let expr = self.parse_expr(expr); + let expr = self.expr(expr); let boolean = expr.truthy(&mut self.error_handler); debug_assert_eq!(boolean.ctype, Type::Bool); let zero = Expr::zero(boolean.location).implicit_cast(&Type::Bool, &mut self.error_handler); @@ -757,9 +757,9 @@ impl Analyzer { otherwise: ast::Expr, location: Location, ) -> Expr { - let condition = self.parse_expr(condition).truthy(&mut self.error_handler); - let mut then = self.parse_expr(then).rval(); - let mut otherwise = self.parse_expr(otherwise).rval(); + let condition = self.expr(condition).truthy(&mut self.error_handler); + let mut then = self.expr(then).rval(); + let mut otherwise = self.expr(otherwise).rval(); if then.ctype.is_arithmetic() && otherwise.ctype.is_arithmetic() { let (tmp1, tmp2) = Expr::binary_promote(then, otherwise, &mut self.error_handler); @@ -1330,8 +1330,8 @@ mod test { use super::*; use crate::analyze::test::analyze; use crate::analyze::*; - pub(crate) fn parse_expr(input: &str) -> CompileResult { - analyze(input, Parser::expr, Analyzer::parse_expr) + pub(crate) fn expr(input: &str) -> CompileResult { + analyze(input, Parser::expr, Analyzer::expr) } fn get_location(r: &CompileResult) -> Location { match r { @@ -1340,21 +1340,21 @@ mod test { } } fn assert_literal(token: Literal) { - let parsed = parse_expr(&token.to_string()); + let parsed = expr(&token.to_string()); let location = get_location(&parsed); assert_eq!(parsed.unwrap(), literal(token, location)); } - fn parse_expr_with_scope<'a>(input: &'a str, variables: &[Symbol]) -> CompileResult { + fn expr_with_scope<'a>(input: &'a str, variables: &[Symbol]) -> CompileResult { analyze(input, Parser::expr, |a, expr| { for &meta in variables { let id = meta.get().id; a.scope.insert(id, meta); } - a.parse_expr(expr) + a.expr(expr) }) } fn assert_type(input: &str, ctype: Type) { - match parse_expr(input) { + match expr(input) { Ok(expr) => assert_eq!(expr.ctype, ctype), Err(err) => panic!("error: {}", err.data), }; @@ -1362,7 +1362,7 @@ mod test { #[test] fn test_primaries() { assert_literal(Literal::Int(141)); - let parsed = parse_expr("\"hi there\""); + let parsed = expr("\"hi there\""); assert_eq!( parsed, @@ -1372,7 +1372,7 @@ mod test { )), ); assert_literal(Literal::Float(1.5)); - let parsed = parse_expr("(1)"); + let parsed = expr("(1)"); assert_eq!(parsed, Ok(literal(Literal::Int(1), get_location(&parsed)))); let x = Variable { ctype: Type::Int(true), @@ -1381,7 +1381,7 @@ mod test { storage_class: Default::default(), } .insert(); - let parsed = parse_expr_with_scope("x", &[x]); + let parsed = expr_with_scope("x", &[x]); assert_eq!( parsed, Ok(Expr { @@ -1417,8 +1417,8 @@ mod test { }), } .insert(); - assert!(parse_expr_with_scope("f(1,2,3)", &[f]).is_err()); - let parsed = parse_expr_with_scope("f()", &[f]); + assert!(expr_with_scope("f(1,2,3)", &[f]).is_err()); + let parsed = expr_with_scope("f()", &[f]); assert!(match parsed { Ok(Expr { expr: ExprType::FuncCall(_, _), @@ -1429,7 +1429,7 @@ mod test { } #[test] fn test_type_errors() { - assert!(parse_expr("1 % 2.0").is_err()); + assert!(expr("1 % 2.0").is_err()); } #[test] @@ -1438,7 +1438,7 @@ mod test { assert_type("(unsigned int)4.2", Type::Int(false)); assert_type("(float)4.2", Type::Float); assert_type("(double)4.2", Type::Double); - assert!(parse_expr("(int*)4.2").is_err()); + assert!(expr("(int*)4.2").is_err()); assert_type( "(int*)(int)4.2", Type::Pointer(Box::new(Type::Int(true)), Qualifiers::default()), diff --git a/src/analyze/init.rs b/src/analyze/init.rs index 1faa2f2a..74088407 100644 --- a/src/analyze/init.rs +++ b/src/analyze/init.rs @@ -15,7 +15,7 @@ impl Analyzer { // initializer_list let mut expr = match init { Aggregate(list) => return self.check_aggregate_overflow(list, ctype, location), - Scalar(expr) => self.parse_expr(*expr), + Scalar(expr) => self.expr(*expr), }; // The only time (that I know of) that an expression will initialize a non-scalar // is for character literals. @@ -110,7 +110,7 @@ impl Analyzer { } else { let expr = match list.next() { Some(Scalar(expr)) => self - .parse_expr(*expr) + .expr(*expr) .rval() .implicit_cast(&inner, &mut self.error_handler), _ => unreachable!(), diff --git a/src/analyze/mod.rs b/src/analyze/mod.rs index 9326a71b..4cb56926 100644 --- a/src/analyze/mod.rs +++ b/src/analyze/mod.rs @@ -594,7 +594,7 @@ impl Analyzer { }; // struct s { int i: 5 }; if let Some(bitfield) = bitfield { - let bit_size = match Self::const_uint(self.parse_expr(bitfield)) { + let bit_size = match Self::const_uint(self.expr(bitfield)) { Ok(e) => e, Err(err) => { self.error_handler.push_back(err); @@ -701,7 +701,7 @@ impl Analyzer { for (name, maybe_value) in ast_members { // enum E { A = 5 }; if let Some(value) = maybe_value { - discriminant = Self::const_sint(self.parse_expr(value)).unwrap_or_else(|err| { + discriminant = Self::const_sint(self.expr(value)).unwrap_or_else(|err| { self.error_handler.push_back(err); std::i64::MIN }); @@ -850,7 +850,7 @@ impl Analyzer { Array { of, size } => { // int a[5] let size = if let Some(expr) = size { - let size = Self::const_uint(self.parse_expr(*expr)).unwrap_or_else(|err| { + let size = Self::const_uint(self.expr(*expr)).unwrap_or_else(|err| { self.error_handler.push_back(err); 1 }); @@ -1380,7 +1380,7 @@ pub(crate) mod test { } pub(crate) fn analyze_expr(s: &str) -> CompileResult { - analyze(s, Parser::expr, Analyzer::parse_expr) + analyze(s, Parser::expr, Analyzer::expr) } pub(crate) fn assert_decl_display(left: &str, right: &str) { diff --git a/src/analyze/stmt.rs b/src/analyze/stmt.rs index d5369d1d..a527197a 100644 --- a/src/analyze/stmt.rs +++ b/src/analyze/stmt.rs @@ -4,8 +4,8 @@ use crate::parse::Lexer; impl FunctionAnalyzer<'_, T> { #[inline(always)] - fn parse_expr(&mut self, expr: ast::Expr) -> Expr { - self.analyzer.parse_expr(expr) + fn expr(&mut self, expr: ast::Expr) -> Expr { + self.analyzer.expr(expr) } pub(crate) fn parse_stmt(&mut self, stmt: ast::Stmt) -> Stmt { use ast::StmtType::*; @@ -24,11 +24,11 @@ impl FunctionAnalyzer<'_, T> { S::Compound(parsed) } // 6.8.3 Expression and null statements - Expr(expr) => S::Expr(self.parse_expr(expr)), + Expr(expr) => S::Expr(self.expr(expr)), // 6.8.4.1 The if statement If(condition, then, otherwise) => { let condition = self - .parse_expr(condition) + .expr(condition) .truthy(&mut self.analyzer.error_handler); let then = self.parse_stmt(*then); let otherwise = otherwise.map(|s| Box::new(self.parse_stmt(*s))); @@ -36,7 +36,7 @@ impl FunctionAnalyzer<'_, T> { } // 6.8.4.2 The switch statement Switch(value, body) => { - let value = self.parse_expr(value).rval(); + let value = self.expr(value).rval(); if !value.ctype.is_integral() { self.err( SemanticError::NonIntegralSwitch(value.ctype.clone()), @@ -50,14 +50,14 @@ impl FunctionAnalyzer<'_, T> { Do(body, condition) => { let body = self.parse_stmt(*body); let condition = self - .parse_expr(condition) + .expr(condition) .truthy(&mut self.analyzer.error_handler); S::Do(Box::new(body), condition) } // 6.8.5.1 The while statement While(condition, body) => { let condition = self - .parse_expr(condition) + .expr(condition) .truthy(&mut self.analyzer.error_handler); let body = self.parse_stmt(*body); S::While(condition, Box::new(body)) @@ -73,10 +73,9 @@ impl FunctionAnalyzer<'_, T> { // Or encode that in the type somehow? self.enter_scope(); let initializer = self.parse_stmt(*initializer); - let condition = condition.map(|e| { - Box::new(self.parse_expr(*e).truthy(&mut self.analyzer.error_handler)) - }); - let post_loop = post_loop.map(|e| Box::new(self.parse_expr(*e))); + let condition = condition + .map(|e| Box::new(self.expr(*e).truthy(&mut self.analyzer.error_handler))); + let post_loop = post_loop.map(|e| Box::new(self.expr(*e))); let body = self.parse_stmt(*body); self.leave_scope(stmt.location); S::For(Box::new(initializer), condition, post_loop, Box::new(body)) @@ -120,7 +119,7 @@ impl FunctionAnalyzer<'_, T> { use super::expr::literal; use crate::data::lex::Literal; - let expr = match self.parse_expr(expr).const_fold() { + let expr = match self.expr(expr).const_fold() { Ok(e) => e, Err(err) => { self.analyzer.error_handler.push_back(err); @@ -149,7 +148,7 @@ impl FunctionAnalyzer<'_, T> { fn return_statement(&mut self, expr: Option, location: Location) -> StmtType { use crate::data::Type; - let expr = expr.map(|e| self.parse_expr(e)); + let expr = expr.map(|e| self.expr(e)); let ret_type = &self.metadata.return_type; match (expr, *ret_type != Type::Void) { // void f() { return ;} diff --git a/src/lex/cpp.rs b/src/lex/cpp.rs index 615daa4a..8510952b 100644 --- a/src/lex/cpp.rs +++ b/src/lex/cpp.rs @@ -940,7 +940,7 @@ impl<'a> PreProcessor<'a> { // TODO: catch expressions that aren't allowed // (see https://github.com/jyn514/rcc/issues/5#issuecomment-575339427) // TODO: can semantic errors happen here? should we check? - Ok(Analyzer::new(parser, false).parse_expr(expr)) + Ok(Analyzer::new(parser, false).expr(expr)) } /// We saw an `#if`, `#ifdef`, or `#ifndef` token at the start of the line /// and want to either take the branch or ignore the tokens within the directive.