llvm.org GIT mirror llvm / 215b61f
[Kaleidoscope] Rename Error -> LogError in Chapters 2-5. This keeps the naming consistent with Chapters 6-8, where Error was renamed to LogError in r264426 to avoid clashes with the new Error class in libSupport. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@264427 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 3 years ago
7 changed file(s) with 92 addition(s) and 92 deletion(s). Raw diff Collapse all Expand all
175175 .. code-block:: c++
176176
177177
178 /// Error* - These are little helper functions for error handling.
179 std::unique_ptr Error(const char *Str) {
180 fprintf(stderr, "Error: %s\n", Str);
178 /// LogError* - These are little helper functions for error handling.
179 std::unique_ptr LogError(const char *Str) {
180 fprintf(stderr, "LogError: %s\n", Str);
181181 return nullptr;
182182 }
183 std::unique_ptr ErrorP(const char *Str) {
184 Error(Str);
183 std::unique_ptr LogErrorP(const char *Str) {
184 LogError(Str);
185185 return nullptr;
186186 }
187187
188 The ``Error`` routines are simple helper routines that our parser will
188 The ``LogError`` routines are simple helper routines that our parser will
189189 use to handle errors. The error recovery in our parser will not be the
190190 best and is not particular user-friendly, but it will be enough for our
191191 tutorial. These routines make it easier to handle errors in routines
232232 return nullptr;
233233
234234 if (CurTok != ')')
235 return Error("expected ')'");
235 return LogError("expected ')'");
236236 getNextToken(); // eat ).
237237 return V;
238238 }
240240 This function illustrates a number of interesting things about the
241241 parser:
242242
243 1) It shows how we use the Error routines. When called, this function
243 1) It shows how we use the LogError routines. When called, this function
244244 expects that the current token is a '(' token, but after parsing the
245245 subexpression, it is possible that there is no ')' waiting. For example,
246246 if the user types in "(4 x" instead of "(4)", the parser should emit an
287287 break;
288288
289289 if (CurTok != ',')
290 return Error("Expected ')' or ',' in argument list");
290 return LogError("Expected ')' or ',' in argument list");
291291 getNextToken();
292292 }
293293 }
323323 static std::unique_ptr ParsePrimary() {
324324 switch (CurTok) {
325325 default:
326 return Error("unknown token when expecting an expression");
326 return LogError("unknown token when expecting an expression");
327327 case tok_identifier:
328328 return ParseIdentifierExpr();
329329 case tok_number:
570570 /// ::= id '(' id* ')'
571571 static std::unique_ptr ParsePrototype() {
572572 if (CurTok != tok_identifier)
573 return ErrorP("Expected function name in prototype");
573 return LogErrorP("Expected function name in prototype");
574574
575575 std::string FnName = IdentifierStr;
576576 getNextToken();
577577
578578 if (CurTok != '(')
579 return ErrorP("Expected '(' in prototype");
579 return LogErrorP("Expected '(' in prototype");
580580
581581 // Read the list of argument names.
582582 std::vector ArgNames;
583583 while (getNextToken() == tok_identifier)
584584 ArgNames.push_back(IdentifierStr);
585585 if (CurTok != ')')
586 return ErrorP("Expected ')' in prototype");
586 return LogErrorP("Expected ')' in prototype");
587587
588588 // success.
589589 getNextToken(); // eat ')'.
6666 engineering practices: for our purposes, adding a virtual method is
6767 simplest.
6868
69 The second thing we want is an "Error" method like we used for the
69 The second thing we want is an "LogError" method like we used for the
7070 parser, which will be used to report errors found during code generation
7171 (for example, use of an undeclared parameter):
7272
7676 static IRBuilder<> Builder(getGlobalContext());
7777 static std::map NamedValues;
7878
79 Value *ErrorV(const char *Str) {
80 Error(Str);
79 Value *LogErrorV(const char *Str) {
80 LogError(Str);
8181 return nullptr;
8282 }
8383
132132 // Look this variable up in the function.
133133 Value *V = NamedValues[Name];
134134 if (!V)
135 ErrorV("Unknown variable name");
135 LogErrorV("Unknown variable name");
136136 return V;
137137 }
138138
167167 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
168168 "booltmp");
169169 default:
170 return ErrorV("invalid binary operator");
170 return LogErrorV("invalid binary operator");
171171 }
172172 }
173173
213213 // Look up the name in the global module table.
214214 Function *CalleeF = TheModule->getFunction(Callee);
215215 if (!CalleeF)
216 return ErrorV("Unknown function referenced");
216 return LogErrorV("Unknown function referenced");
217217
218218 // If argument mismatch error.
219219 if (CalleeF->arg_size() != Args.size())
220 return ErrorV("Incorrect # arguments passed");
220 return LogErrorV("Incorrect # arguments passed");
221221
222222 std::vector ArgsV;
223223 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
327327 return nullptr;
328328
329329 if (!TheFunction->empty())
330 return (Function*)ErrorV("Function cannot be redefined.");
330 return (Function*)LogErrorV("Function cannot be redefined.");
331331
332332
333333 For function definitions, we start by searching TheModule's symbol table for an
126126 return nullptr;
127127
128128 if (CurTok != tok_then)
129 return Error("expected then");
129 return LogError("expected then");
130130 getNextToken(); // eat the then
131131
132132 auto Then = ParseExpression();
134134 return nullptr;
135135
136136 if (CurTok != tok_else)
137 return Error("expected else");
137 return LogError("expected else");
138138
139139 getNextToken();
140140
153153 static std::unique_ptr ParsePrimary() {
154154 switch (CurTok) {
155155 default:
156 return Error("unknown token when expecting an expression");
156 return LogError("unknown token when expecting an expression");
157157 case tok_identifier:
158158 return ParseIdentifierExpr();
159159 case tok_number:
517517 getNextToken(); // eat the for.
518518
519519 if (CurTok != tok_identifier)
520 return Error("expected identifier after for");
520 return LogError("expected identifier after for");
521521
522522 std::string IdName = IdentifierStr;
523523 getNextToken(); // eat identifier.
524524
525525 if (CurTok != '=')
526 return Error("expected '=' after for");
526 return LogError("expected '=' after for");
527527 getNextToken(); // eat '='.
528528
529529
531531 if (!Start)
532532 return nullptr;
533533 if (CurTok != ',')
534 return Error("expected ',' after for start value");
534 return LogError("expected ',' after for start value");
535535 getNextToken();
536536
537537 auto End = ParseExpression();
548548 }
549549
550550 if (CurTok != tok_in)
551 return Error("expected 'in' after for");
551 return LogError("expected 'in' after for");
552552 getNextToken(); // eat 'in'.
553553
554554 auto Body = ParseExpression();
186186 return TokPrec;
187187 }
188188
189 /// Error* - These are little helper functions for error handling.
190 std::unique_ptr Error(const char *Str) {
189 /// LogError* - These are little helper functions for error handling.
190 std::unique_ptr LogError(const char *Str) {
191191 fprintf(stderr, "Error: %s\n", Str);
192192 return nullptr;
193193 }
194 std::unique_ptr ErrorP(const char *Str) {
195 Error(Str);
194 std::unique_ptr LogErrorP(const char *Str) {
195 LogError(Str);
196196 return nullptr;
197197 }
198198
213213 return nullptr;
214214
215215 if (CurTok != ')')
216 return Error("expected ')'");
216 return LogError("expected ')'");
217217 getNextToken(); // eat ).
218218 return V;
219219 }
243243 break;
244244
245245 if (CurTok != ',')
246 return Error("Expected ')' or ',' in argument list");
246 return LogError("Expected ')' or ',' in argument list");
247247 getNextToken();
248248 }
249249 }
261261 static std::unique_ptr ParsePrimary() {
262262 switch (CurTok) {
263263 default:
264 return Error("unknown token when expecting an expression");
264 return LogError("unknown token when expecting an expression");
265265 case tok_identifier:
266266 return ParseIdentifierExpr();
267267 case tok_number:
323323 /// ::= id '(' id* ')'
324324 static std::unique_ptr ParsePrototype() {
325325 if (CurTok != tok_identifier)
326 return ErrorP("Expected function name in prototype");
326 return LogErrorP("Expected function name in prototype");
327327
328328 std::string FnName = IdentifierStr;
329329 getNextToken();
330330
331331 if (CurTok != '(')
332 return ErrorP("Expected '(' in prototype");
332 return LogErrorP("Expected '(' in prototype");
333333
334334 std::vector ArgNames;
335335 while (getNextToken() == tok_identifier)
336336 ArgNames.push_back(IdentifierStr);
337337 if (CurTok != ')')
338 return ErrorP("Expected ')' in prototype");
338 return LogErrorP("Expected ')' in prototype");
339339
340340 // success.
341341 getNextToken(); // eat ')'.
188188 return TokPrec;
189189 }
190190
191 /// Error* - These are little helper functions for error handling.
192 std::unique_ptr Error(const char *Str) {
191 /// LogError* - These are little helper functions for error handling.
192 std::unique_ptr LogError(const char *Str) {
193193 fprintf(stderr, "Error: %s\n", Str);
194194 return nullptr;
195195 }
196196
197 std::unique_ptr ErrorP(const char *Str) {
198 Error(Str);
197 std::unique_ptr LogErrorP(const char *Str) {
198 LogError(Str);
199199 return nullptr;
200200 }
201201
216216 return nullptr;
217217
218218 if (CurTok != ')')
219 return Error("expected ')'");
219 return LogError("expected ')'");
220220 getNextToken(); // eat ).
221221 return V;
222222 }
246246 break;
247247
248248 if (CurTok != ',')
249 return Error("Expected ')' or ',' in argument list");
249 return LogError("Expected ')' or ',' in argument list");
250250 getNextToken();
251251 }
252252 }
264264 static std::unique_ptr ParsePrimary() {
265265 switch (CurTok) {
266266 default:
267 return Error("unknown token when expecting an expression");
267 return LogError("unknown token when expecting an expression");
268268 case tok_identifier:
269269 return ParseIdentifierExpr();
270270 case tok_number:
326326 /// ::= id '(' id* ')'
327327 static std::unique_ptr ParsePrototype() {
328328 if (CurTok != tok_identifier)
329 return ErrorP("Expected function name in prototype");
329 return LogErrorP("Expected function name in prototype");
330330
331331 std::string FnName = IdentifierStr;
332332 getNextToken();
333333
334334 if (CurTok != '(')
335 return ErrorP("Expected '(' in prototype");
335 return LogErrorP("Expected '(' in prototype");
336336
337337 std::vector ArgNames;
338338 while (getNextToken() == tok_identifier)
339339 ArgNames.push_back(IdentifierStr);
340340 if (CurTok != ')')
341 return ErrorP("Expected ')' in prototype");
341 return LogErrorP("Expected ')' in prototype");
342342
343343 // success.
344344 getNextToken(); // eat ')'.
383383 static IRBuilder<> Builder(getGlobalContext());
384384 static std::map NamedValues;
385385
386 Value *ErrorV(const char *Str) {
387 Error(Str);
386 Value *LogErrorV(const char *Str) {
387 LogError(Str);
388388 return nullptr;
389389 }
390390
396396 // Look this variable up in the function.
397397 Value *V = NamedValues[Name];
398398 if (!V)
399 return ErrorV("Unknown variable name");
399 return LogErrorV("Unknown variable name");
400400 return V;
401401 }
402402
419419 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
420420 "booltmp");
421421 default:
422 return ErrorV("invalid binary operator");
422 return LogErrorV("invalid binary operator");
423423 }
424424 }
425425
427427 // Look up the name in the global module table.
428428 Function *CalleeF = TheModule->getFunction(Callee);
429429 if (!CalleeF)
430 return ErrorV("Unknown function referenced");
430 return LogErrorV("Unknown function referenced");
431431
432432 // If argument mismatch error.
433433 if (CalleeF->arg_size() != Args.size())
434 return ErrorV("Incorrect # arguments passed");
434 return LogErrorV("Incorrect # arguments passed");
435435
436436 std::vector ArgsV;
437437 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
195195 return TokPrec;
196196 }
197197
198 /// Error* - These are little helper functions for error handling.
199 std::unique_ptr Error(const char *Str) {
198 /// LogError* - These are little helper functions for error handling.
199 std::unique_ptr LogError(const char *Str) {
200200 fprintf(stderr, "Error: %s\n", Str);
201201 return nullptr;
202202 }
203203
204 std::unique_ptr ErrorP(const char *Str) {
205 Error(Str);
204 std::unique_ptr LogErrorP(const char *Str) {
205 LogError(Str);
206206 return nullptr;
207207 }
208208
223223 return nullptr;
224224
225225 if (CurTok != ')')
226 return Error("expected ')'");
226 return LogError("expected ')'");
227227 getNextToken(); // eat ).
228228 return V;
229229 }
253253 break;
254254
255255 if (CurTok != ',')
256 return Error("Expected ')' or ',' in argument list");
256 return LogError("Expected ')' or ',' in argument list");
257257 getNextToken();
258258 }
259259 }
271271 static std::unique_ptr ParsePrimary() {
272272 switch (CurTok) {
273273 default:
274 return Error("unknown token when expecting an expression");
274 return LogError("unknown token when expecting an expression");
275275 case tok_identifier:
276276 return ParseIdentifierExpr();
277277 case tok_number:
333333 /// ::= id '(' id* ')'
334334 static std::unique_ptr ParsePrototype() {
335335 if (CurTok != tok_identifier)
336 return ErrorP("Expected function name in prototype");
336 return LogErrorP("Expected function name in prototype");
337337
338338 std::string FnName = IdentifierStr;
339339 getNextToken();
340340
341341 if (CurTok != '(')
342 return ErrorP("Expected '(' in prototype");
342 return LogErrorP("Expected '(' in prototype");
343343
344344 std::vector ArgNames;
345345 while (getNextToken() == tok_identifier)
346346 ArgNames.push_back(IdentifierStr);
347347 if (CurTok != ')')
348 return ErrorP("Expected ')' in prototype");
348 return LogErrorP("Expected ')' in prototype");
349349
350350 // success.
351351 getNextToken(); // eat ')'.
393393 static std::unique_ptr TheJIT;
394394 static std::map> FunctionProtos;
395395
396 Value *ErrorV(const char *Str) {
397 Error(Str);
396 Value *LogErrorV(const char *Str) {
397 LogError(Str);
398398 return nullptr;
399399 }
400400
421421 // Look this variable up in the function.
422422 Value *V = NamedValues[Name];
423423 if (!V)
424 return ErrorV("Unknown variable name");
424 return LogErrorV("Unknown variable name");
425425 return V;
426426 }
427427
444444 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
445445 "booltmp");
446446 default:
447 return ErrorV("invalid binary operator");
447 return LogErrorV("invalid binary operator");
448448 }
449449 }
450450
452452 // Look up the name in the global module table.
453453 Function *CalleeF = getFunction(Callee);
454454 if (!CalleeF)
455 return ErrorV("Unknown function referenced");
455 return LogErrorV("Unknown function referenced");
456456
457457 // If argument mismatch error.
458458 if (CalleeF->arg_size() != Args.size())
459 return ErrorV("Incorrect # arguments passed");
459 return LogErrorV("Incorrect # arguments passed");
460460
461461 std::vector ArgsV;
462462 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
237237 return TokPrec;
238238 }
239239
240 /// Error* - These are little helper functions for error handling.
241 std::unique_ptr Error(const char *Str) {
240 /// LogError* - These are little helper functions for error handling.
241 std::unique_ptr LogError(const char *Str) {
242242 fprintf(stderr, "Error: %s\n", Str);
243243 return nullptr;
244244 }
245245
246 std::unique_ptr ErrorP(const char *Str) {
247 Error(Str);
246 std::unique_ptr LogErrorP(const char *Str) {
247 LogError(Str);
248248 return nullptr;
249249 }
250250
265265 return nullptr;
266266
267267 if (CurTok != ')')
268 return Error("expected ')'");
268 return LogError("expected ')'");
269269 getNextToken(); // eat ).
270270 return V;
271271 }
295295 break;
296296
297297 if (CurTok != ',')
298 return Error("Expected ')' or ',' in argument list");
298 return LogError("Expected ')' or ',' in argument list");
299299 getNextToken();
300300 }
301301 }
316316 return nullptr;
317317
318318 if (CurTok != tok_then)
319 return Error("expected then");
319 return LogError("expected then");
320320 getNextToken(); // eat the then
321321
322322 auto Then = ParseExpression();
324324 return nullptr;
325325
326326 if (CurTok != tok_else)
327 return Error("expected else");
327 return LogError("expected else");
328328
329329 getNextToken();
330330
341341 getNextToken(); // eat the for.
342342
343343 if (CurTok != tok_identifier)
344 return Error("expected identifier after for");
344 return LogError("expected identifier after for");
345345
346346 std::string IdName = IdentifierStr;
347347 getNextToken(); // eat identifier.
348348
349349 if (CurTok != '=')
350 return Error("expected '=' after for");
350 return LogError("expected '=' after for");
351351 getNextToken(); // eat '='.
352352
353353 auto Start = ParseExpression();
354354 if (!Start)
355355 return nullptr;
356356 if (CurTok != ',')
357 return Error("expected ',' after for start value");
357 return LogError("expected ',' after for start value");
358358 getNextToken();
359359
360360 auto End = ParseExpression();
371371 }
372372
373373 if (CurTok != tok_in)
374 return Error("expected 'in' after for");
374 return LogError("expected 'in' after for");
375375 getNextToken(); // eat 'in'.
376376
377377 auto Body = ParseExpression();
391391 static std::unique_ptr ParsePrimary() {
392392 switch (CurTok) {
393393 default:
394 return Error("unknown token when expecting an expression");
394 return LogError("unknown token when expecting an expression");
395395 case tok_identifier:
396396 return ParseIdentifierExpr();
397397 case tok_number:
457457 /// ::= id '(' id* ')'
458458 static std::unique_ptr ParsePrototype() {
459459 if (CurTok != tok_identifier)
460 return ErrorP("Expected function name in prototype");
460 return LogErrorP("Expected function name in prototype");
461461
462462 std::string FnName = IdentifierStr;
463463 getNextToken();
464464
465465 if (CurTok != '(')
466 return ErrorP("Expected '(' in prototype");
466 return LogErrorP("Expected '(' in prototype");
467467
468468 std::vector ArgNames;
469469 while (getNextToken() == tok_identifier)
470470 ArgNames.push_back(IdentifierStr);
471471 if (CurTok != ')')
472 return ErrorP("Expected ')' in prototype");
472 return LogErrorP("Expected ')' in prototype");
473473
474474 // success.
475475 getNextToken(); // eat ')'.
517517 static std::unique_ptr TheJIT;
518518 static std::map> FunctionProtos;
519519
520 Value *ErrorV(const char *Str) {
521 Error(Str);
520 Value *LogErrorV(const char *Str) {
521 LogError(Str);
522522 return nullptr;
523523 }
524524
545545 // Look this variable up in the function.
546546 Value *V = NamedValues[Name];
547547 if (!V)
548 return ErrorV("Unknown variable name");
548 return LogErrorV("Unknown variable name");
549549 return V;
550550 }
551551
568568 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
569569 "booltmp");
570570 default:
571 return ErrorV("invalid binary operator");
571 return LogErrorV("invalid binary operator");
572572 }
573573 }
574574
576576 // Look up the name in the global module table.
577577 Function *CalleeF = getFunction(Callee);
578578 if (!CalleeF)
579 return ErrorV("Unknown function referenced");
579 return LogErrorV("Unknown function referenced");
580580
581581 // If argument mismatch error.
582582 if (CalleeF->arg_size() != Args.size())
583 return ErrorV("Incorrect # arguments passed");
583 return LogErrorV("Incorrect # arguments passed");
584584
585585 std::vector ArgsV;
586586 for (unsigned i = 0, e = Args.size(); i != e; ++i) {