-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.h
133 lines (98 loc) · 4.51 KB
/
errors.h
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
/* File: errors.h
* --------------
* This file defines an error-reporting class with a set of already
* implemented static methods for reporting the standard Decaf errors.
* You should report all errors via this class so that your error
* messages will have the same wording/spelling as ours and thus
* diff can easily compare the two. If needed, you can add new
* methods if you have some fancy error-reporting, but for the most
* part, you will just use the class as given.
*/
#ifndef _H_errors
#define _H_errors
#include <map>
#include <string>
using std::multimap;
using std::string;
#include "location.h"
class Type;
class Identifier;
class Expr;
class BreakStmt;
class ReturnStmt;
class This;
class Decl;
class Operator;
/* General notes on using this class
* ----------------------------------
* Each of the methods in thie class matches one of the standard Decaf
* errors and reports a specific problem such as an unterminated string,
* type mismatch, declaration conflict, etc. You will call these methods
* to report problems encountered during the analysis phases. All methods
* on this class are static, thus you can invoke methods directly via
* the class name, e.g.
*
* if (missingEnd) ReportError::UntermString(&yylloc, str);
*
* For some methods, the first argument is the pointer to the location
* structure that identifies where the problem is (usually this is the
* location of the offending token). You can pass NULL for the argument
* if there is no appropriate position to point out. For other methods,
* location is accessed by messaging the node in error which is passed
* as an argument. You cannot pass NULL for these arguments.
*/
typedef enum {LookingForType, LookingForClass, LookingForInterface, LookingForVariable, LookingForFunction} reasonT;
class ReportError
{
public:
// Errors used by preprocessor
static void UntermComment();
static void InvalidDirective(int linenum);
// Errors used by scanner
static void LongIdentifier(yyltype *loc, const char *ident);
static void UntermString(yyltype *loc, const char *str);
static void UnrecogChar(yyltype *loc, char ch);
// Errors used by semantic analyzer for declarations
static void DeclConflict(Decl *newDecl, Decl *prevDecl);
static void OverrideMismatch(Decl *fnDecl);
static void InterfaceNotImplemented(Decl *classDecl, Type *intfType);
// Errors used by semantic analyzer for identifiers
static void IdentifierNotDeclared(Identifier *ident, reasonT whyNeeded);
// Errors used by semantic analyzer for expressions
static void IncompatibleOperand(Operator *op, Type *rhs); // unary
static void IncompatibleOperands(Operator *op, Type *lhs, Type *rhs); // binary
static void ThisOutsideClassScope(This *th);
// Errors used by semantic analyzer for array acesss & NewArray
static void BracketsOnNonArray(Expr *baseExpr);
static void SubscriptNotInteger(Expr *subscriptExpr);
static void NewArraySizeNotInteger(Expr *sizeExpr);
// Errors used by semantic analyzer for function/method calls
static void NumArgsMismatch(Identifier *fnIdentifier, int numExpected, int numGiven);
static void ArgMismatch(Expr *arg, int argIndex, Type *given, Type *expected);
static void PrintArgMismatch(Expr *arg, int argIndex, Type *given);
// Errors used by semantic analyzer for field access
static void FieldNotFoundInBase(Identifier *field, Type *base);
static void InaccessibleField(Identifier *field, Type *base);
// Errors used by semantic analyzer for control structures
static void TestNotBoolean(Expr *testExpr);
static void ReturnMismatch(ReturnStmt *rStmt, Type *given, Type *expected);
static void BreakOutsideLoop(BreakStmt *bStmt);
// Errors used by code-generator/linker
static void NoMainFound();
// Generic method to report a printf-style error message
static void Formatted(yyltype *loc, const char *format, ...);
// Returns number of error messages printed
static int NumErrors() { return numErrors; }
// Print out all error messages in lexical order
static void PrintErrors();
private:
static void UnderlineErrorInLine(const char *line, const yyltype *pos);
static void EmitError(yyltype *loc, string msg);
static void OutputError(const yyltype *loc, string msg);
static int numErrors;
static multimap<yyltype,string> errors;
};
// Wording to use for runtime error messages
static const char *err_arr_out_of_bounds = "Decaf runtime error: Array subscript out of bounds\\n";
static const char *err_arr_bad_size = "Decaf runtime error: Array size is <= 0\\n";
#endif