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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use super::{type_analysis::TypeAnalysis, type_system::TypeSystem};
use asciifile::{MaybeSpanned, Span, Spanned};
use compiler_shared::context::Context;
use parser::{ast, visitor::NodeKind};
use strtab::{self, Symbol};

use failure::Fail;
use std::collections::HashMap;

#[derive(Debug, Fail)]
pub enum SemanticError {
    #[fail(display = "redefinition of {} '{}'", kind, name)]
    RedefinitionError {
        kind: String, // "class", "parameter", ...
        name: String, // name of the parameter class...
    },

    #[fail(display = "Usage of the parameter '{}' of the main function", name)]
    MainMethodParamUsed { name: String },

    #[fail(display = "Only the 'main' method can be static")]
    StaticMethodNotMain,

    #[fail(display = "No 'main' method found")]
    NoMainMethod,

    #[fail(
        display = "{}. definition of a static method. Only one is allowed",
        amount
    )]
    MultipleStaticMethods { amount: u64 },

    #[fail(display = "non-static variable 'this' cannot be referenced from a static context")]
    ThisInStaticMethod,

    #[fail(display = "method '{}' might not return", method_name)]
    MightNotReturn { method_name: String },

    #[fail(
        display = "non-static method '{}' cannot be referenced from a static context",
        method_name
    )]
    ThisMethodInvocationInStaticMethod { method_name: String },

    #[fail(display = "cannot call static method '{}'", method_name)]
    CannotCallStaticMethod { method_name: String },

    #[fail(display = "condition must be boolean")]
    ConditionMustBeBoolean,

    #[fail(display = "cannot find var or field '{}'", name)]
    CannotLookupVarOrField { name: String },

    #[fail(
        display = "Cannot find var or field '{}'. Did you mean {}?",
        name, did_you_mean
    )]
    CannotLookupVarOrFieldDidYouMean { name: String, did_you_mean: String },

    #[fail(
        display = "cannot access non static field '{}' in static method",
        field_name
    )]
    CannotAccessNonStaticFieldInStaticMethod { field_name: String },

    #[fail(display = "method cannot return a value")]
    VoidMethodCannotReturnValue,

    #[fail(display = "type 'void' is not allowed here")]
    VoidNotAllowed,

    #[fail(display = "method must return a value of type '{}'", ty)]
    MethodMustReturnSomething { ty: String },

    #[fail(
        display = "invalid type: Expected expression of type '{}', but was of type '{}'",
        ty_expected, ty_expr
    )]
    InvalidType {
        ty_expected: String,
        ty_expr: String,
    },

    #[fail(display = "cannot reference class '{}' here", class_name)]
    InvalidReferenceToClass { class_name: String },

    #[fail(display = "class '{}' does not exist", class_name)]
    ClassDoesNotExist { class_name: String },

    #[fail(display = "cannot index non-array type '{}'", ty)]
    CannotIndexNonArrayType { ty: String },

    #[fail(display = "method '{}' does not exist on type '{}'", method_name, ty)]
    MethodDoesNotExistOnType { method_name: String, ty: String },

    #[fail(display = "field '{}' does not exist on type '{}'", field_name, ty)]
    FieldDoesNotExistOnType { field_name: String, ty: String },

    #[fail(
        display = "method argument count does not match: Expected {} arguments, but found {}",
        expected_args, actual_args
    )]
    MethodArgCountDoesNotMatch {
        expected_args: usize,
        actual_args: usize,
    },

    #[fail(
        display = "cannot compare values of type '{}' with values of type '{}'",
        ty1, ty2
    )]
    CannotCompareValuesOfType1WithType2 { ty1: String, ty2: String },

    #[fail(display = "not a statement")]
    NotAStatement,

    #[fail(display = "invalid assignment - can only assign to \
                      local variables, parameters, field and array fields")]
    InvalidAssignment,

    #[fail(display = "Cannot write to read-only field '{}'", field_name)]
    CannotWriteToReadOnlyField { field_name: String },

    #[fail(display = "integer number too large: {}", int)]
    IntTooLarge { int: String },
}

/// `check` returns an `Err` iff at least one errors was emitted through
/// `context`.
pub fn check<'a, 'f>(
    strtab: &mut strtab::StringTable<'f>,
    ast: &'a ast::AST<'f>,
    context: &Context<'f>,
) -> Result<(TypeSystem<'f, 'a>, TypeAnalysis<'f, 'a>), ()> {
    let mut first_pass_visitor = ClassesAndMembersVisitor::new(context);
    first_pass_visitor.do_visit(&NodeKind::from(ast));

    // Check if a static method was found. If multiple static methods were found or
    // the static method is not called `main` the error is already emitted in
    // the visitor
    if first_pass_visitor.static_method_found == 0 {
        context
            .diagnostics
            .error(&MaybeSpanned::WithoutSpan(SemanticError::NoMainMethod));
    }

    if context.diagnostics.errored() {
        return Err(());
    }
    let res = super::check(strtab, &ast, &context);
    if context.diagnostics.errored() {
        return Err(());
    }
    Ok(res)
}

struct ClassesAndMembersVisitor<'f, 'cx> {
    context: &'cx Context<'cx>,
    static_method_found: u64,
    class_member_to_its_span: HashMap<*const ast::ClassMember<'f>, Span<'f>>,
}

impl<'f, 'cx> ClassesAndMembersVisitor<'f, 'cx> {
    pub fn new(context: &'cx Context<'_>) -> Self {
        Self {
            context,
            static_method_found: 0,
            class_member_to_its_span: HashMap::new(),
        }
    }

    fn do_visit(&mut self, node: &NodeKind<'_, 'f>) {
        use self::{ast, NodeKind::*};
        node.for_each_child(&mut |child| {
            match child {
                ClassDeclaration(decl) => {
                    let decl_node = NodeKind::from(decl);
                    decl_node.for_each_child(&mut |member_node| {
                        let member_decl: &Spanned<'_, ast::ClassMember<'_>> = match member_node {
                            NodeKind::ClassMember(m) => m,
                            _ => panic!("class children are expected to be class members"),
                        };
                        self.class_member_to_its_span
                            .insert(&member_decl.data as *const _, member_decl.span);
                    });
                }

                ClassMember(member) => {
                    if let ast::ClassMemberKind::MainMethod(params, _) = &member.kind {
                        debug_assert!(params.len() == 1);
                        self.static_method_found += 1;
                        if &member.name != "main" {
                            self.context.diagnostics.error(&Spanned {
                                span: member.span,
                                data: SemanticError::StaticMethodNotMain,
                            });
                        }
                        if self.static_method_found > 1 {
                            self.context.diagnostics.error(&Spanned {
                                span: member.span,
                                data: SemanticError::MultipleStaticMethods {
                                    amount: self.static_method_found,
                                },
                            })
                        }
                    }

                    match &member.kind {
                        ast::ClassMemberKind::Method(ty, pl, block)
                            if ty.basic.data != ast::BasicType::Void =>
                        {
                            let ptr = (&member.data) as *const _;
                            let member_decl = self
                                .class_member_to_its_span
                                .get(&ptr)
                                .expect("must have current_member_decl while visiting ClassMember");
                            let highlight_span = Span::from_positions(&[
                                member_decl.start_position(),
                                pl.span.end_position(),
                            ])
                            .unwrap();
                            self.check_method_always_returns(&member.name, highlight_span, block)
                        }
                        _ => (),
                    }
                }

                Stmt(stmt) => {
                    use ast::Expr::*;
                    if let ast::Stmt::Expression(expr) = &stmt.data {
                        match &expr.data {
                            Binary(ast::BinaryOp::Assign, _, _)
                            | MethodInvocation(..)
                            | ThisMethodInvocation(..) => (),
                            _ => {
                                //Err
                                self.context.diagnostics.error(&Spanned {
                                    span: stmt.span,
                                    data: SemanticError::NotAStatement,
                                });
                            }
                        }
                    }
                }

                Expr(expr) => match &expr.data {
                    ast::Expr::NegInt(int) if int.data != "2147483648" => {
                        self.check_int(int.data, int.span)
                    }
                    ast::Expr::Int(int) => self.check_int(int.data, int.span),
                    _ => (),
                },

                _ => (),
            };

            self.do_visit(&child)
        });
    }

    fn check_int(&self, int: &str, span: Span<'_>) {
        if int.parse::<i32>().is_err() {
            self.context.diagnostics.error(&Spanned {
                span,
                data: SemanticError::IntTooLarge {
                    int: int.to_string(),
                },
            });
        }
    }

    fn check_method_always_returns(
        &self,
        method_name: &Symbol<'_>,
        hightlight_span: Span<'_>,
        method_body: &Spanned<'_, ast::Block<'_>>,
    ) {
        fn always_returns<'t>(stmt: &Spanned<'t, ast::Stmt<'t>>) -> bool {
            match &stmt.data {
                // An if-else stmt always returns iff both arms always return
                ast::Stmt::If(_, then_arm, else_arm) => {
                    let then_arm_always_returns = always_returns(&*then_arm);
                    let else_arm_always_returns = else_arm
                        .as_ref()
                        .map_or(false, |else_arm| always_returns(&*else_arm));

                    then_arm_always_returns && else_arm_always_returns
                }

                // An empty block does not return
                ast::Stmt::Block(block) if block.statements.is_empty() => false,
                // A non-empty block always returns iff any of its top-level statements
                // always returns
                ast::Stmt::Block(block) => block.statements.iter().any(always_returns),

                // A return stmt always returns
                ast::Stmt::Return(_) => true,

                // All other stmts do not always return
                _ => false,
            }
        }

        // FIXME de-duplicate empty block logic from always_returns
        if method_body.statements.is_empty() || !method_body.statements.iter().any(always_returns) {
            self.context.diagnostics.error(&Spanned {
                span: hightlight_span,
                data: SemanticError::MightNotReturn {
                    method_name: format!("{}", method_name),
                },
            });
        }
    }
}

#[cfg(test)]
#[allow(clippy::print_stdout)]
mod tests {
    use super::*;
    use asciifile::AsciiFile;
    use lexer::{Lexer, TokenKind};
    use mjtest::SemanticTestCase;
    use mjtest_macros::gen_semantic_tests;
    use parser::Parser;
    use strtab::StringTable;

    macro_rules! gen_check_code {
        ($check_res:ident = $input:expr) => {
            let ascii_file = AsciiFile::new($input).unwrap();
            let context = Context::dummy(&ascii_file);
            let mut strtab = StringTable::new();
            let lexer = Lexer::new(&mut strtab, &context);
            let unforgiving_lexer = lexer.filter_map(|result| match result {
                Ok(token) => match token.data {
                    TokenKind::Whitespace | TokenKind::Comment(_) => None,
                    _ => Some(token),
                },
                Err(lexical_error) => panic!("{}", lexical_error),
            });
            let ast = Parser::new(unforgiving_lexer).parse().unwrap();
            let $check_res = super::check(&mut strtab, &ast, &context);
        };
    }

    fn do_mjtest_semantic_test(tc: &SemanticTestCase) {
        println!("file name: {:?}", tc.file_name());
        let input = std::fs::read_to_string(tc.path()).unwrap().into_bytes();
        gen_check_code!(check_res = &input);
        match (tc, check_res) {
            (SemanticTestCase::Valid(_), Ok(_)) | (SemanticTestCase::Invalid(_), Err(_)) => (),
            (tc, res) => {
                println!("test case: {:?}", tc);
                println!("result:    {:?}", res);
                panic!();
            }
        }
    }
    gen_semantic_tests!((do_mjtest_semantic_test, []));

    #[derive(Debug, PartialEq, Eq)]
    enum BinopCheckResult {
        Accept,
        Error,
    }

    macro_rules! binop_test {
        (internal $mjcode:expr, $expected:ident, $testname:ident)  => {
            #[test]
            fn $testname() {
                let prog = $mjcode;
                let input = prog.into_bytes();
                gen_check_code!(check_res = &input);
                let exp = BinopCheckResult::$expected;
                match (exp, check_res) {
                    (BinopCheckResult::Accept, Ok(_)) => (),
                    (BinopCheckResult::Error, Err(_)) => (),
                    (exp, act) => {
                        println!("expected: {:?}", exp);
                        println!("actual:   {:?}", act);
                        panic!();
                    }
                }
            }
        };
        ($t1:expr, $val1:expr, $t2:expr, $val2:expr, $t3:expr, $op:expr, $exp:ident, $n:ident) => {
            binop_test!(internal format!(r"
                class C {{}}
                class D {{}}
                class BinopCheck {{
                    public static void main(String[] args) {{
                        {} v1 = {};
                        {} v2 = {};
                        {} v3 = v1 {} v2;
                    }}
                }}",
                $t1, $val1, $t2, $val2, $t3, $op), $exp, $n
            );
        };
        ($lit1:expr, $op:expr, $lit2:expr, $expected:ident, $testname:ident) => {
            binop_test!(internal format!(r"
                class C {{}}
                class D {{}}
                class BinopCheck {{
                    public static void main(String[] args) {{
                        if ({} {} {}) {{
                            System.out.println(23);
                        }}
                    }}
                }}",
                $lit1, $op, $lit2), $expected, $testname
            );
        };
    }

    #[rustfmt::skip]
    mod binop {
        use super::*;
        binop_test!("int", "1", "boolean", "true", "int", "==",    Error,  eq_int_bool_eq);
        binop_test!("C", "null", "C", "null", "boolean", "==",     Accept, eq_same_class_null);
        binop_test!("C", "null", "int", "23", "boolean", "==",     Error,  eq_class_int);
        binop_test!("C", "null", "boolean", "23", "boolean", "==", Error,  eq_class_bool);
        binop_test!("C", "null", "D", "null", "boolean", "==",     Error,  eq_class_class_eq);
        binop_test!("23", "==", "42",                              Accept, eq_ints);
        binop_test!("23", "==", "null",                            Error,  eq_integer_null);
        binop_test!("0", "==", "null",                             Error,  eq_integer0_null);
        binop_test!("true", "==", "null",                          Error,  eq_true_null);
        binop_test!("false", "==", "null",                         Error,  eq_false_null );
        binop_test!("0", "==", "false",                            Error,  eq_zero_false);
        binop_test!("1", "==", "true",                             Error,  eq_one_true);
        binop_test!("(new C())", "==", "null",                     Accept, eq_new_null);
        binop_test!("(new C())", "==", "(new D())",                Error,  eq_new_new_2types);
    }
}