blob: f34aa6362fcbf133c6ec1ff7c721d0c1c7b3a673 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2//
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//
10// This file implements the actions class which performs semantic analysis and
11// builds an AST out of a parse stream.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Sema.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/Lex/Preprocessor.h"
18#include "clang/Basic/Diagnostic.h"
Chris Lattner2e64c072007-08-10 20:18:51 +000019
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Steve Naroff9637a9b2007-10-09 22:01:59 +000022void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
23 TUScope = S;
24}
25
Steve Naroffee1de132007-10-10 21:53:07 +000026QualType Sema::GetObjcIdType() {
27 assert(TUScope && "GetObjcIdType(): Top-level scope is null");
28 if (ObjcIdType.isNull()) {
29 IdentifierInfo *IdIdent = &Context.Idents.get("id");
30 ScopedDecl *IdDecl = LookupScopedDecl(IdIdent, Decl::IDNS_Ordinary,
31 SourceLocation(), TUScope);
32 TypedefDecl *IdTypedef = dyn_cast_or_null<TypedefDecl>(IdDecl);
33 assert(IdTypedef && "GetObjcIdType(): Couldn't find 'id' type");
34 ObjcIdType = Context.getTypedefType(IdTypedef);
35 }
36 return ObjcIdType;
37}
38
39
Chris Lattner4b009652007-07-25 00:24:17 +000040Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
41 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattner2e64c072007-08-10 20:18:51 +000042
43 // Get IdentifierInfo objects for known functions for which we
44 // do extra checking.
45 IdentifierTable& IT = PP.getIdentifierTable();
46
47 KnownFunctionIDs[ id_printf ] = &IT.get("printf");
48 KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
49 KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
50 KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +000051 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremenek2d7e9532007-08-10 21:13:51 +000052 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +000053 KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
54 KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
55 KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
56 KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
Steve Naroffee1de132007-10-10 21:53:07 +000057
58 TUScope = 0;
59 ObjcIdType = QualType();
Chris Lattner4b009652007-07-25 00:24:17 +000060}
61
Chris Lattnerb26b7ad2007-08-31 04:53:24 +000062void Sema::DeleteExpr(ExprTy *E) {
63 delete static_cast<Expr*>(E);
64}
65void Sema::DeleteStmt(StmtTy *S) {
66 delete static_cast<Stmt*>(S);
67}
68
Chris Lattner4b009652007-07-25 00:24:17 +000069//===----------------------------------------------------------------------===//
70// Helper functions.
71//===----------------------------------------------------------------------===//
72
73bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
74 PP.getDiagnostics().Report(Loc, DiagID);
75 return true;
76}
77
78bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
79 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
80 return true;
81}
82
83bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
84 const std::string &Msg2) {
85 std::string MsgArr[] = { Msg1, Msg2 };
86 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
87 return true;
88}
89
90bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
91 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
92 return true;
93}
94
95bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
96 SourceRange Range) {
97 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
98 return true;
99}
100
101bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
102 const std::string &Msg2, SourceRange Range) {
103 std::string MsgArr[] = { Msg1, Msg2 };
104 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
105 return true;
106}
107
108bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
109 SourceRange R1, SourceRange R2) {
110 SourceRange RangeArr[] = { R1, R2 };
111 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
112 return true;
113}
114
115bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
116 SourceRange R1, SourceRange R2) {
117 SourceRange RangeArr[] = { R1, R2 };
118 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
119 return true;
120}
121
122bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
123 const std::string &Msg2, SourceRange R1, SourceRange R2) {
124 std::string MsgArr[] = { Msg1, Msg2 };
125 SourceRange RangeArr[] = { R1, R2 };
126 PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
127 return true;
128}
129
130const LangOptions &Sema::getLangOptions() const {
131 return PP.getLangOptions();
132}