-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.cc
195 lines (159 loc) · 6.16 KB
/
errors.cc
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
/**
* File: errors.cc
* ---------------
* Implementation for error-reporting class.
*/
#include "errors.h"
#include <iostream>
#include <sstream>
#include <stdarg.h>
#include <stdio.h>
using namespace std;
#include "scanner.h" // for GetLineNumbered
#include "ast_type.h"
#include "ast_expr.h"
#include "ast_stmt.h"
#include "ast_decl.h"
int ReportError::numErrors = 0;
void ReportError::UnderlineErrorInLine(const char *line, yyltype *pos) {
if (!line) return;
cerr << line << endl;
for (int i = 1; i <= pos->last_column; i++)
cerr << (i >= pos->first_column ? '^' : ' ');
cerr << endl;
}
void ReportError::OutputError(yyltype *loc, string msg) {
numErrors++;
fflush(stdout); // make sure any buffered text has been output
if (loc) {
cerr << endl << "*** Error line " << loc->first_line << "." << endl;
UnderlineErrorInLine(GetLineNumbered(loc->first_line), loc);
} else
cerr << endl << "*** Error." << endl;
cerr << "*** " << msg << endl << endl;
}
void ReportError::Formatted(yyltype *loc, const char *format, ...) {
va_list args;
char errbuf[2048];
va_start(args, format);
vsprintf(errbuf,format, args);
va_end(args);
OutputError(loc, errbuf);
}
void ReportError::UntermComment() {
OutputError(NULL, "Input ends with unterminated comment");
}
void ReportError::LongIdentifier(yyltype *loc, const char *ident) {
ostringstream s;
s << "Identifier too long: \"" << ident << "\"";
OutputError(loc, s.str());
}
void ReportError::UntermString(yyltype *loc, const char *str) {
ostringstream s;
s << "Unterminated string constant: " << str;
OutputError(loc, s.str());
}
void ReportError::UnrecogChar(yyltype *loc, char ch) {
ostringstream s;
s << "Unrecognized char: '" << ch << "'";
OutputError(loc, s.str());
}
void ReportError::DeclConflict(Decl *decl, Decl *prevDecl) {
ostringstream s;
s << "Declaration of '" << decl << "' here conflicts with declaration on line "
<< prevDecl->GetLocation()->first_line;
OutputError(decl->GetLocation(), s.str());
}
void ReportError::OverrideMismatch(Decl *fnDecl) {
ostringstream s;
s << "Method '" << fnDecl << "' must match inherited type signature";
OutputError(fnDecl->GetLocation(), s.str());
}
void ReportError::InterfaceNotImplemented(Decl *cd, Type *interfaceType) {
ostringstream s;
s << "Class '" << cd << "' does not implement entire interface '" << interfaceType << "'";
OutputError(interfaceType->GetLocation(), s.str());
}
void ReportError::IdentifierNotDeclared(Identifier *ident, reasonT whyNeeded) {
ostringstream s;
static const char *names[] = {"type", "class", "interface", "variable", "function"};
Assert(whyNeeded >= 0 && whyNeeded <= sizeof(names)/sizeof(names[0]));
s << "No declaration found for "<< names[whyNeeded] << " '" << ident << "'";
OutputError(ident->GetLocation(), s.str());
}
void ReportError::IncompatibleOperands(Operator *op, Type *lhs, Type *rhs) {
ostringstream s;
s << "Incompatible operands: " << lhs << " " << op << " " << rhs;
OutputError(op->GetLocation(), s.str());
}
void ReportError::IncompatibleOperand(Operator *op, Type *rhs) {
ostringstream s;
s << "Incompatible operand: " << op << " " << rhs;
OutputError(op->GetLocation(), s.str());
}
void ReportError::ThisOutsideClassScope(This *th) {
OutputError(th->GetLocation(), "'this' is only valid within class scope");
}
void ReportError::BracketsOnNonArray(Expr *baseExpr) {
OutputError(baseExpr->GetLocation(), "[] can only be applied to arrays");
}
void ReportError::SubscriptNotInteger(Expr *subscriptExpr) {
OutputError(subscriptExpr->GetLocation(), "Array subscript must be an integer");
}
void ReportError::NewArraySizeNotInteger(Expr *sizeExpr) {
OutputError(sizeExpr->GetLocation(), "Size for NewArray must be an integer");
}
void ReportError::NumArgsMismatch(Identifier *fnIdent, int numExpected, int numGiven) {
ostringstream s;
s << "Function '"<< fnIdent << "' expects " << numExpected << " argument" << (numExpected==1?"":"s")
<< " but " << numGiven << " given";
OutputError(fnIdent->GetLocation(), s.str());
}
void ReportError::ArgMismatch(Expr *arg, int argIndex, Type *given, Type *expected) {
ostringstream s;
s << "Incompatible argument " << argIndex << ": " << given << " given, " << expected << " expected";
OutputError(arg->GetLocation(), s.str());
}
void ReportError::ReturnMismatch(ReturnStmt *rStmt, Type *given, Type *expected) {
ostringstream s;
s << "Incompatible return: " << given << " given, " << expected << " expected";
OutputError(rStmt->GetLocation(), s.str());
}
void ReportError::FieldNotFoundInBase(Identifier *field, Type *base) {
ostringstream s;
s << base << " has no such field '" << field <<"'";
OutputError(field->GetLocation(), s.str());
}
void ReportError::InaccessibleField(Identifier *field, Type *base) {
ostringstream s;
s << base << " field '" << field << "' only accessible within class scope";
OutputError(field->GetLocation(), s.str());
}
void ReportError::PrintArgMismatch(Expr *arg, int argIndex, Type *given) {
ostringstream s;
s << "Incompatible argument " << argIndex << ": " << given
<< " given, int/bool/string expected";
OutputError(arg->GetLocation(), s.str());
}
void ReportError::TestNotBoolean(Expr *expr) {
OutputError(expr->GetLocation(), "Test expression must have boolean type");
}
void ReportError::BreakOutsideLoop(BreakStmt *bStmt) {
OutputError(bStmt->GetLocation(), "break is only allowed inside a loop");
}
void ReportError::NoMainFound() {
OutputError(NULL, "Linker: function 'main' not defined");
}
/**
* Function: yyerror()
* -------------------
* Standard error-reporting function expected by yacc. Our version merely
* just calls into the error reporter above, passing the location of
* the last token read. If you want to suppress the ordinary "parse error"
* message from yacc, you can implement yyerror to do nothing and
* then call ReportError::Formatted yourself with a more descriptive
* message.
*/
void yyerror(const char *msg) {
ReportError::Formatted(&yylloc, "%s", msg);
}