blob: 92d17e1e99bd5f953d2f65f850c47fe7881441e3 [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
Anders Carlsson7f23e3d2007-10-31 02:53:19 +000085/// GetObjcClassType - See comments for Sema::GetObjcIdType above; replace "id"
86/// with "Protocol".
87QualType Sema::GetObjcClassType(SourceLocation Loc) {
88 assert(TUScope && "GetObjcClassType(): Top-level scope is null");
89 if (!Context.getObjcClassType().isNull())
90 return Context.getObjcClassType();
91
92 IdentifierInfo *ClassIdent = &Context.Idents.get("Class");
93 ScopedDecl *ClassDecl = LookupScopedDecl(ClassIdent, Decl::IDNS_Ordinary,
94 SourceLocation(), TUScope);
95 TypedefDecl *ObjcClassTypedef = dyn_cast_or_null<TypedefDecl>(ClassDecl);
96 if (!ObjcClassTypedef) {
97 Diag(Loc, diag::err_missing_class_definition);
98 return QualType();
99 }
100 Context.setObjcClassType(ObjcClassTypedef);
101 return Context.getObjcClassType();
102}
103
Chris Lattner4b009652007-07-25 00:24:17 +0000104Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
105 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattner2e64c072007-08-10 20:18:51 +0000106
107 // Get IdentifierInfo objects for known functions for which we
108 // do extra checking.
109 IdentifierTable& IT = PP.getIdentifierTable();
110
111 KnownFunctionIDs[ id_printf ] = &IT.get("printf");
112 KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
113 KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
114 KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +0000115 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremenek2d7e9532007-08-10 21:13:51 +0000116 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +0000117 KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
118 KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
119 KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
120 KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
Steve Naroffee1de132007-10-10 21:53:07 +0000121
122 TUScope = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000123}
124
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000125void Sema::DeleteExpr(ExprTy *E) {
126 delete static_cast<Expr*>(E);
127}
128void Sema::DeleteStmt(StmtTy *S) {
129 delete static_cast<Stmt*>(S);
130}
131
Chris Lattner4b009652007-07-25 00:24:17 +0000132//===----------------------------------------------------------------------===//
133// Helper functions.
134//===----------------------------------------------------------------------===//
135
136bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
137 PP.getDiagnostics().Report(Loc, DiagID);
138 return true;
139}
140
141bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
142 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
143 return true;
144}
145
146bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
147 const std::string &Msg2) {
148 std::string MsgArr[] = { Msg1, Msg2 };
149 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
150 return true;
151}
152
153bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
154 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
155 return true;
156}
157
158bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
159 SourceRange Range) {
160 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
161 return true;
162}
163
164bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
165 const std::string &Msg2, SourceRange Range) {
166 std::string MsgArr[] = { Msg1, Msg2 };
167 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
168 return true;
169}
170
171bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
172 SourceRange R1, SourceRange R2) {
173 SourceRange RangeArr[] = { R1, R2 };
174 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
175 return true;
176}
177
178bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
179 SourceRange R1, SourceRange R2) {
180 SourceRange RangeArr[] = { R1, R2 };
181 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
182 return true;
183}
184
185bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
186 const std::string &Msg2, SourceRange R1, SourceRange R2) {
187 std::string MsgArr[] = { Msg1, Msg2 };
188 SourceRange RangeArr[] = { R1, R2 };
189 PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
190 return true;
191}
192
193const LangOptions &Sema::getLangOptions() const {
194 return PP.getLangOptions();
195}