/
error.cpp
525 lines (445 loc) · 14.3 KB
/
error.cpp
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include "clay.hpp"
#include "evaluator.hpp"
#include "codegen.hpp"
#include "matchinvoke.hpp"
#include "invoketables.hpp"
#include "error.hpp"
namespace clay {
bool shouldPrintFullMatchErrors;
set<pair<string,string> > logMatchSymbols;
//
// invoke stack - a compilation call stack
//
static vector<CompileContextEntry> contextStack;
static const unsigned RECURSION_WARNING_LEVEL = 1000;
void pushCompileContext(ObjectPtr obj) {
if (contextStack.size() >= RECURSION_WARNING_LEVEL)
warning("potential runaway recursion");
if (!contextStack.empty())
contextStack.back().location = topLocation();
contextStack.push_back(CompileContextEntry(obj));
}
void pushCompileContext(ObjectPtr obj, llvm::ArrayRef<ObjectPtr> params) {
if (contextStack.size() >= RECURSION_WARNING_LEVEL)
warning("potential runaway recursion");
if (!contextStack.empty())
contextStack.back().location = topLocation();
contextStack.push_back(CompileContextEntry(obj, params));
}
void pushCompileContext(ObjectPtr obj, llvm::ArrayRef<ObjectPtr> params, llvm::ArrayRef<unsigned> dispatchIndices) {
if (contextStack.size() >= RECURSION_WARNING_LEVEL)
warning("potential runaway recursion");
if (!contextStack.empty())
contextStack.back().location = topLocation();
contextStack.push_back(CompileContextEntry(obj, params, dispatchIndices));
}
void popCompileContext() {
contextStack.pop_back();
}
vector<CompileContextEntry> getCompileContext() {
return contextStack;
}
void setCompileContext(llvm::ArrayRef<CompileContextEntry> x) {
contextStack = x;
}
//
// source location of the current item being processed
//
static vector<Location> errorLocations;
void pushLocation(Location const& location) {
errorLocations.push_back(location);
}
void popLocation() {
errorLocations.pop_back();
}
Location topLocation() {
vector<Location>::iterator i, begin;
i = errorLocations.end();
begin = errorLocations.begin();
while (i != begin) {
--i;
if (i->ok()) return *i;
}
return Location();
}
//
// DebugPrinter
//
static vector<ObjectPtr> debugStack;
int DebugPrinter::indent = 0;
DebugPrinter::DebugPrinter(ObjectPtr obj)
: obj(obj)
{
for (int i = 0; i < indent; ++i)
llvm::outs() << ' ';
llvm::outs() << "BEGIN - " << obj << '\n';
++indent;
debugStack.push_back(obj);
}
DebugPrinter::~DebugPrinter()
{
debugStack.pop_back();
--indent;
for (int i = 0; i < indent; ++i)
llvm::outs() << ' ';
llvm::outs() << "DONE - " << obj << '\n';
}
//
// report error
//
static void computeLineCol(Location const &location, unsigned &line, unsigned &column, unsigned &tabColumn) {
const char *p = location.source->data();
const char *end = p + location.offset;
line = column = tabColumn = 0;
for (; p != end; ++p) {
++column;
++tabColumn;
if (*p == '\n') {
++line;
column = 0;
tabColumn = 0;
}
else if (*p == '\t') {
tabColumn += 7;
}
}
}
llvm::DIFile getDebugLineCol(Location const &location, unsigned &line, unsigned &column) {
if (!location.ok()) {
line = 0;
column = 0;
return llvm::DIFile(NULL);
}
unsigned tabColumn;
computeLineCol(location, line, column, tabColumn);
line += 1;
column += 1;
return location.source->getDebugInfo();
}
void getLineCol(Location const &location, unsigned &line, unsigned &column, unsigned &tabColumn) {
if (!location.ok()) {
line = 0;
column = 0;
tabColumn = 0;
return;
}
computeLineCol(location, line, column, tabColumn);
}
static void splitLines(SourcePtr source, vector<string> &lines) {
lines.push_back(string());
const char *p = source->data();
const char *end = source->endData();
for (; p != end; ++p) {
lines.back().push_back(*p);
if (*p == '\n')
lines.push_back(string());
}
}
static bool endsWithNewline(llvm::StringRef s) {
if (s.size() == 0) return false;
return s[s.size()-1] == '\n';
}
static void displayLocation(Location const &location, unsigned &line, unsigned &column) {
unsigned tabColumn;
getLineCol(location, line, column, tabColumn);
vector<string> lines;
splitLines(location.source, lines);
llvm::errs() << "###############################\n";
unsigned i = (line < 2) ? 0 : line-2;
for (; i <= line+2; ++i) {
if (i >= lines.size())
continue;
llvm::errs() << lines[i];
if (!endsWithNewline(lines[i]))
llvm::errs() << "\n";
if (i == line) {
for (unsigned j = 0; j < tabColumn; ++j)
llvm::errs() << "-";
llvm::errs() << "^\n";
}
}
llvm::errs() << "###############################\n";
}
// This has to use stdio because it needs to be usable from the debugger
// and cerr or errs may be destroyed if there's a bug in global dtors
extern "C" void displayCompileContext() {
if (contextStack.empty())
return;
fprintf(stderr, "\ncompilation context: \n");
string buf;
llvm::raw_string_ostream errs(buf);
for (size_t i = contextStack.size(); i > 0; --i) {
ObjectPtr obj = contextStack[i-1].callable;
llvm::ArrayRef<ObjectPtr> params = contextStack[i-1].params;
if (i < contextStack.size() && contextStack[i-1].location.ok()) {
errs << " ";
printFileLineCol(errs, contextStack[i-1].location);
errs << ":\n";
}
errs << " ";
if (obj->objKind == GLOBAL_VARIABLE) {
errs << "global ";
printName(errs, obj);
if (!params.empty()) {
errs << "[";
printNameList(errs, params);
errs << "]";
}
}
else {
printName(errs, obj);
if (contextStack[i-1].hasParams) {
errs << "(";
printNameList(errs, params, contextStack[i-1].dispatchIndices);
errs << ")";
}
}
errs << "\n";
}
fprintf(stderr, "%s", errs.str().c_str());
fflush(stderr);
}
static void displayDebugStack() {
if (debugStack.empty())
return;
llvm::errs() << "\ndebug stack:\n";
for (size_t i = debugStack.size(); i > 0; --i) {
llvm::errs() << " " << debugStack[i-1] << "\n";
}
}
void displayError(llvm::Twine const &msg, llvm::StringRef kind) {
string msgString = msg.str();
if (msgString.empty() || msgString[msgString.length() - 1] != '\n')
msgString += '\n';
Location location = topLocation();
if (location.ok()) {
unsigned line, column;
displayLocation(location, line, column);
llvm::errs() << location.source->fileName
<< '(' << line+1 << ',' << column << "): " << kind << ": " << msgString;
llvm::errs().flush();
displayCompileContext();
displayDebugStack();
}
else {
llvm::errs() << kind << ": " << msgString;
}
}
void warning(llvm::Twine const &msg) {
displayError(msg, "warning");
}
void note(llvm::Twine const &msg) {
displayError(msg, "note");
}
void error(llvm::Twine const &msg) {
displayError(msg, "error");
throw CompilerError();
}
void error(Location const &location, llvm::Twine const &msg) {
if (location.ok())
pushLocation(location);
error(msg);
}
void fmtError(const char *fmt, ...) {
va_list ap;
char s[256];
va_start(ap, fmt);
vsnprintf(s, sizeof(s)-1, fmt, ap);
va_end(ap);
error(s);
}
void argumentError(size_t index, llvm::StringRef msg) {
string buf;
llvm::raw_string_ostream sout(buf);
sout << "argument " << (index+1) << ": " << msg;
error(sout.str());
}
void ambiguousMatchError(MatchSuccessPtr match, MatchSuccessPtr match2) {
if (match->overload->location.ok()) {
unsigned line, column;
displayLocation(match->overload->location, line, column);
llvm::errs() << match->overload->location.source->fileName
<< '(' << line+1 << ',' << column << "): " << "found matching overload\n";
llvm::errs().flush();
}
if (match2->overload->location.ok()) {
unsigned line, column;
displayLocation(match2->overload->location, line, column);
llvm::errs() << match2->overload->location.source->fileName
<< '(' << line+1 << ',' << column << "): " << "error: ambiguous overload\n";
llvm::errs().flush();
}
string buf;
llvm::raw_string_ostream err(buf);
err << "ambiguous overloads for call-site\n";
error(err.str());
}
static const char *valuesStr(size_t n) {
return (n == 1) ? "value" : "values";
}
void arityError(size_t expected, size_t received) {
string buf;
llvm::raw_string_ostream sout(buf);
sout << "expected " << expected << " " << valuesStr(expected);
sout << ", but received " << received << " " << valuesStr(received);
error(sout.str());
}
void arityError2(size_t minExpected, size_t received) {
string buf;
llvm::raw_string_ostream sout(buf);
sout << "expected at least " << minExpected
<< " " << valuesStr(minExpected);
sout << ", but received " << received << " " << valuesStr(received);
error(sout.str());
}
void ensureArity(MultiStaticPtr args, size_t size) {
if (args->size() != size)
arityError(size, args->size());
}
void ensureArity(MultiEValuePtr args, size_t size) {
if (args->size() != size)
arityError(size, args->size());
}
void ensureArity(MultiPValuePtr args, size_t size) {
if (args->size() != size)
arityError(size, args->size());
}
void ensureArity(MultiCValuePtr args, size_t size) {
if (args->size() != size)
arityError(size, args->size());
}
void arityMismatchError(size_t leftArity, size_t rightArity, bool hasVarArg) {
string buf;
llvm::raw_string_ostream sout(buf);
if (hasVarArg)
sout << "left side takes " << leftArity << " or more " << valuesStr(leftArity);
else
sout << "left side has " << leftArity << " " << valuesStr(leftArity);
sout << ", but right side has " << rightArity
<< " " << valuesStr(rightArity);
error(sout.str());
}
static string typeErrorMessage(llvm::StringRef expected,
TypePtr receivedType) {
string buf;
llvm::raw_string_ostream sout(buf);
sout << "expected " << expected << ", "
<< "but received " << receivedType << " type";
return sout.str();
}
static string typeErrorMessage(TypePtr expectedType,
TypePtr receivedType) {
string buf;
llvm::raw_string_ostream sout(buf);
sout << expectedType << " type";
return typeErrorMessage(sout.str(), receivedType);
}
void typeError(llvm::StringRef expected, TypePtr receivedType) {
error(typeErrorMessage(expected, receivedType));
}
void typeError(TypePtr expectedType, TypePtr receivedType) {
error(typeErrorMessage(expectedType, receivedType));
}
void argumentTypeError(unsigned index,
llvm::StringRef expected,
TypePtr receivedType) {
argumentError(index, typeErrorMessage(expected, receivedType));
}
void argumentTypeError(unsigned index,
TypePtr expectedType,
TypePtr receivedType) {
argumentError(index, typeErrorMessage(expectedType, receivedType));
}
void indexRangeError(llvm::StringRef kind,
size_t value,
size_t maxValue)
{
string buf;
llvm::raw_string_ostream sout(buf);
sout << kind << " " << value << " is out of range. ";
sout << "it should be less than " << maxValue;
error(sout.str());
}
void argumentIndexRangeError(unsigned index,
llvm::StringRef kind,
size_t value,
size_t maxValue)
{
string buf;
llvm::raw_string_ostream sout(buf);
sout << kind << " " << value << " is out of range. ";
sout << "it should be less than " << maxValue;
argumentError(index, sout.str());
}
void invalidStaticObjectError(ObjectPtr obj)
{
string buf;
llvm::raw_string_ostream sout(buf);
sout << "invalid static object: " << obj;
error(sout.str());
}
void argumentInvalidStaticObjectError(unsigned index, ObjectPtr obj)
{
string buf;
llvm::raw_string_ostream sout(buf);
sout << "invalid static object: " << obj;
argumentError(index, sout.str());
}
void matchBindingError(MatchResultPtr const &result)
{
string buf;
llvm::raw_string_ostream sout(buf);
printMatchError(sout, result);
error(sout.str());
}
static void matchFailureMessage(MatchFailureError const &err, string &outBuf)
{
llvm::raw_string_ostream sout(outBuf);
int hiddenPatternOverloads = 0;
for (MatchFailureVector::const_iterator i = err.failures.begin();
i != err.failures.end();
++i)
{
OverloadPtr overload = i->first;
if (!shouldPrintFullMatchErrors && overload->nameIsPattern) {
++hiddenPatternOverloads;
continue;
}
sout << "\n ";
Location location = overload->location;
unsigned line, column, tabColumn;
getLineCol(location, line, column, tabColumn);
sout << location.source->fileName.c_str()
<< "(" << line+1 << "," << column << ")"
<< "\n ";
printMatchError(sout, i->second);
}
if (hiddenPatternOverloads > 0)
sout << "\n " << hiddenPatternOverloads << " universal overloads not shown (show with -full-match-errors option)";
sout.flush();
}
void matchFailureError(MatchFailureError const &err)
{
string buf;
if (err.failedInterface)
buf = "call does not conform to function interface";
else
buf = "no matching overload found";
matchFailureMessage(err, buf);
error(buf);
}
void matchFailureLog(MatchFailureError const &err)
{
if (err.failures.empty())
return;
string buf = "matched";
matchFailureMessage(err, buf);
note(buf);
}
void printFileLineCol(llvm::raw_ostream &out, Location const &location)
{
unsigned line, column, tabColumn;
getLineCol(location, line, column, tabColumn);
out << location.source->fileName << "(" << line+1 << "," << column << ")";
}
}