blob: 125b0cb63fd97194a403b234b05f68373add0b9c [file] [log] [blame]
Chris Lattnerddd6fc82006-11-10 04:58:55 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerddd6fc82006-11-10 04:58:55 +000010// This file implements the actions class which performs semantic analysis and
11// builds an AST out of a parse stream.
Chris Lattner3e7bd4e2006-08-17 05:51:27 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerddd6fc82006-11-10 04:58:55 +000015#include "Sema.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000016#include "clang/AST/ASTContext.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000017#include "clang/Lex/Preprocessor.h"
Chris Lattnerd6647d32007-05-16 17:56:50 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000019
Chris Lattnerc11438c2006-08-18 05:17:52 +000020using namespace clang;
21
Steve Naroffc62adb62007-10-09 22:01:59 +000022void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
23 TUScope = S;
24}
25
Steve Naroff2c055d22007-02-28 19:32:13 +000026Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
27 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattnerb87b1b32007-08-10 20:18:51 +000028
29 // Get IdentifierInfo objects for known functions for which we
30 // do extra checking.
31 IdentifierTable& IT = PP.getIdentifierTable();
32
33 KnownFunctionIDs[ id_printf ] = &IT.get("printf");
34 KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
35 KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
36 KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
Chris Lattnerb87b1b32007-08-10 20:18:51 +000037 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremenekcfc94192007-08-10 21:13:51 +000038 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattnerb87b1b32007-08-10 20:18:51 +000039 KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
40 KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
41 KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
42 KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
Steve Naroff38d31b42007-02-28 01:22:02 +000043}
Chris Lattnercb6a3822006-11-10 06:20:45 +000044
Chris Lattner57c523f2007-08-31 04:53:24 +000045void Sema::DeleteExpr(ExprTy *E) {
46 delete static_cast<Expr*>(E);
47}
48void Sema::DeleteStmt(StmtTy *S) {
49 delete static_cast<Stmt*>(S);
50}
51
Chris Lattnerc11438c2006-08-18 05:17:52 +000052//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +000053// Helper functions.
54//===----------------------------------------------------------------------===//
55
Chris Lattnerd6647d32007-05-16 17:56:50 +000056bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
57 PP.getDiagnostics().Report(Loc, DiagID);
58 return true;
59}
60
Steve Narofff1e53692007-03-23 22:27:02 +000061bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Chris Lattnerd6647d32007-05-16 17:56:50 +000062 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
63 return true;
64}
65
66bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
67 const std::string &Msg2) {
68 std::string MsgArr[] = { Msg1, Msg2 };
69 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +000070 return true;
Chris Lattnereaafe1222006-11-10 05:17:58 +000071}
72
Steve Naroff71ce2e02007-05-18 22:53:50 +000073bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
74 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
Steve Naroffbc2f0992007-03-30 20:09:34 +000075 return true;
76}
77
Steve Naroff71ce2e02007-05-18 22:53:50 +000078bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
79 SourceRange Range) {
80 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
81 return true;
82}
83
84bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
85 const std::string &Msg2, SourceRange Range) {
86 std::string MsgArr[] = { Msg1, Msg2 };
87 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
88 return true;
89}
90
91bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
92 SourceRange R1, SourceRange R2) {
93 SourceRange RangeArr[] = { R1, R2 };
94 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
95 return true;
96}
97
98bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
99 SourceRange R1, SourceRange R2) {
100 SourceRange RangeArr[] = { R1, R2 };
101 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
102 return true;
103}
104
105bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
106 const std::string &Msg2, SourceRange R1, SourceRange R2) {
107 std::string MsgArr[] = { Msg1, Msg2 };
108 SourceRange RangeArr[] = { R1, R2 };
109 PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000110 return true;
Steve Naroff8160ea22007-03-06 01:09:46 +0000111}
112
Chris Lattnerac18be92006-11-20 06:49:47 +0000113const LangOptions &Sema::getLangOptions() const {
Steve Naroff38d31b42007-02-28 01:22:02 +0000114 return PP.getLangOptions();
Chris Lattnerac18be92006-11-20 06:49:47 +0000115}