blob: 2be198de43be7b9d4f8880dbe222c129e1b159c9 [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 Naroff7aeeda02007-10-10 23:24:43 +000026/// GetObjcIdType - The following method assumes that "id" is imported
27/// via <objc/objc.h>. This is the way GCC worked for almost 20 years.
28/// In GCC 4.0, "id" is now a built-in type. Unfortunately, typedefs *cannot* be
29/// redefined (even if they are identical). To allow a built-in types to coexist
30/// with <objc/objc.h>, GCC has a special hack on decls (DECL_IN_SYSTEM_HEADER).
31/// For now, we will *not* install id as a built-in. FIXME: reconsider this.
32QualType Sema::GetObjcIdType(SourceLocation Loc) {
Steve Naroffee1de132007-10-10 21:53:07 +000033 assert(TUScope && "GetObjcIdType(): Top-level scope is null");
Chris Lattner77470dc2007-10-30 20:57:56 +000034 if (!Context.getObjcIdType().isNull())
35 return Context.getObjcIdType();
36
37 IdentifierInfo *IdIdent = &Context.Idents.get("id");
38 ScopedDecl *IdDecl = LookupScopedDecl(IdIdent, Decl::IDNS_Ordinary,
39 SourceLocation(), TUScope);
40 TypedefDecl *ObjcIdTypedef = dyn_cast_or_null<TypedefDecl>(IdDecl);
41 if (!ObjcIdTypedef) {
42 Diag(Loc, diag::err_missing_id_definition);
43 return QualType();
Steve Naroffee1de132007-10-10 21:53:07 +000044 }
Chris Lattner77470dc2007-10-30 20:57:56 +000045 Context.setObjcIdType(ObjcIdTypedef);
Steve Naroff9d12c902007-10-15 14:41:52 +000046 return Context.getObjcIdType();
Steve Naroffee1de132007-10-10 21:53:07 +000047}
48
Fariborz Jahanianf807c202007-10-16 20:40:23 +000049/// GetObjcSelType - See comments for Sema::GetObjcIdType above; replace "id"
50/// with "SEL".
51QualType Sema::GetObjcSelType(SourceLocation Loc) {
52 assert(TUScope && "GetObjcSelType(): Top-level scope is null");
53 if (Context.getObjcSelType().isNull()) {
54 IdentifierInfo *SelIdent = &Context.Idents.get("SEL");
55 ScopedDecl *SelDecl = LookupScopedDecl(SelIdent, Decl::IDNS_Ordinary,
56 SourceLocation(), TUScope);
57 TypedefDecl *ObjcSelTypedef = dyn_cast_or_null<TypedefDecl>(SelDecl);
58 if (!ObjcSelTypedef) {
59 Diag(Loc, diag::err_missing_sel_definition);
60 return QualType();
61 }
62 Context.setObjcSelType(ObjcSelTypedef);
63 }
64 return Context.getObjcSelType();
65}
Steve Naroffee1de132007-10-10 21:53:07 +000066
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +000067/// GetObjcProtoType - See comments for Sema::GetObjcIdType above; replace "id"
68/// with "Protocol".
69QualType Sema::GetObjcProtoType(SourceLocation Loc) {
70 assert(TUScope && "GetObjcProtoType(): Top-level scope is null");
71 if (Context.getObjcProtoType().isNull()) {
72 IdentifierInfo *ProtoIdent = &Context.Idents.get("Protocol");
73 ScopedDecl *ProtoDecl = LookupScopedDecl(ProtoIdent, Decl::IDNS_Ordinary,
74 SourceLocation(), TUScope);
75 TypedefDecl *ObjcProtoTypedef = dyn_cast_or_null<TypedefDecl>(ProtoDecl);
76 if (!ObjcProtoTypedef) {
77 Diag(Loc, diag::err_missing_proto_definition);
78 return QualType();
79 }
80 Context.setObjcProtoType(ObjcProtoTypedef);
81 }
82 return Context.getObjcProtoType();
83}
84
Chris Lattner4b009652007-07-25 00:24:17 +000085Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
86 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattner2e64c072007-08-10 20:18:51 +000087
88 // Get IdentifierInfo objects for known functions for which we
89 // do extra checking.
90 IdentifierTable& IT = PP.getIdentifierTable();
91
92 KnownFunctionIDs[ id_printf ] = &IT.get("printf");
93 KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
94 KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
95 KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +000096 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremenek2d7e9532007-08-10 21:13:51 +000097 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +000098 KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
99 KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
100 KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
101 KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
Steve Naroffee1de132007-10-10 21:53:07 +0000102
103 TUScope = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000104}
105
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000106void Sema::DeleteExpr(ExprTy *E) {
107 delete static_cast<Expr*>(E);
108}
109void Sema::DeleteStmt(StmtTy *S) {
110 delete static_cast<Stmt*>(S);
111}
112
Chris Lattner4b009652007-07-25 00:24:17 +0000113//===----------------------------------------------------------------------===//
114// Helper functions.
115//===----------------------------------------------------------------------===//
116
117bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
118 PP.getDiagnostics().Report(Loc, DiagID);
119 return true;
120}
121
122bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
123 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
124 return true;
125}
126
127bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
128 const std::string &Msg2) {
129 std::string MsgArr[] = { Msg1, Msg2 };
130 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
131 return true;
132}
133
134bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
135 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
136 return true;
137}
138
139bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
140 SourceRange Range) {
141 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
142 return true;
143}
144
145bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
146 const std::string &Msg2, SourceRange Range) {
147 std::string MsgArr[] = { Msg1, Msg2 };
148 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
149 return true;
150}
151
152bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
153 SourceRange R1, SourceRange R2) {
154 SourceRange RangeArr[] = { R1, R2 };
155 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
156 return true;
157}
158
159bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
160 SourceRange R1, SourceRange R2) {
161 SourceRange RangeArr[] = { R1, R2 };
162 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
163 return true;
164}
165
166bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
167 const std::string &Msg2, SourceRange R1, SourceRange R2) {
168 std::string MsgArr[] = { Msg1, Msg2 };
169 SourceRange RangeArr[] = { R1, R2 };
170 PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
171 return true;
172}
173
174const LangOptions &Sema::getLangOptions() const {
175 return PP.getLangOptions();
176}