blob: 5bd5df5539e9628516e74ad4fbc4b180f6b2c801 [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");
Steve Naroff9d12c902007-10-15 14:41:52 +000034 if (Context.getObjcIdType().isNull()) {
Steve Naroffee1de132007-10-10 21:53:07 +000035 IdentifierInfo *IdIdent = &Context.Idents.get("id");
36 ScopedDecl *IdDecl = LookupScopedDecl(IdIdent, Decl::IDNS_Ordinary,
37 SourceLocation(), TUScope);
Steve Naroff9d12c902007-10-15 14:41:52 +000038 TypedefDecl *ObjcIdTypedef = dyn_cast_or_null<TypedefDecl>(IdDecl);
Steve Naroff7aeeda02007-10-10 23:24:43 +000039 if (!ObjcIdTypedef) {
40 Diag(Loc, diag::err_missing_id_definition);
41 return QualType();
42 }
Steve Naroff9d12c902007-10-15 14:41:52 +000043 Context.setObjcIdType(ObjcIdTypedef);
Steve Naroffee1de132007-10-10 21:53:07 +000044 }
Steve Naroff9d12c902007-10-15 14:41:52 +000045 return Context.getObjcIdType();
Steve Naroffee1de132007-10-10 21:53:07 +000046}
47
Fariborz Jahanianf807c202007-10-16 20:40:23 +000048/// GetObjcSelType - See comments for Sema::GetObjcIdType above; replace "id"
49/// with "SEL".
50QualType Sema::GetObjcSelType(SourceLocation Loc) {
51 assert(TUScope && "GetObjcSelType(): Top-level scope is null");
52 if (Context.getObjcSelType().isNull()) {
53 IdentifierInfo *SelIdent = &Context.Idents.get("SEL");
54 ScopedDecl *SelDecl = LookupScopedDecl(SelIdent, Decl::IDNS_Ordinary,
55 SourceLocation(), TUScope);
56 TypedefDecl *ObjcSelTypedef = dyn_cast_or_null<TypedefDecl>(SelDecl);
57 if (!ObjcSelTypedef) {
58 Diag(Loc, diag::err_missing_sel_definition);
59 return QualType();
60 }
61 Context.setObjcSelType(ObjcSelTypedef);
62 }
63 return Context.getObjcSelType();
64}
Steve Naroffee1de132007-10-10 21:53:07 +000065
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +000066/// GetObjcProtoType - See comments for Sema::GetObjcIdType above; replace "id"
67/// with "Protocol".
68QualType Sema::GetObjcProtoType(SourceLocation Loc) {
69 assert(TUScope && "GetObjcProtoType(): Top-level scope is null");
70 if (Context.getObjcProtoType().isNull()) {
71 IdentifierInfo *ProtoIdent = &Context.Idents.get("Protocol");
72 ScopedDecl *ProtoDecl = LookupScopedDecl(ProtoIdent, Decl::IDNS_Ordinary,
73 SourceLocation(), TUScope);
74 TypedefDecl *ObjcProtoTypedef = dyn_cast_or_null<TypedefDecl>(ProtoDecl);
75 if (!ObjcProtoTypedef) {
76 Diag(Loc, diag::err_missing_proto_definition);
77 return QualType();
78 }
79 Context.setObjcProtoType(ObjcProtoTypedef);
80 }
81 return Context.getObjcProtoType();
82}
83
Chris Lattner4b009652007-07-25 00:24:17 +000084Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
85 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattner2e64c072007-08-10 20:18:51 +000086
87 // Get IdentifierInfo objects for known functions for which we
88 // do extra checking.
89 IdentifierTable& IT = PP.getIdentifierTable();
90
91 KnownFunctionIDs[ id_printf ] = &IT.get("printf");
92 KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
93 KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
94 KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +000095 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremenek2d7e9532007-08-10 21:13:51 +000096 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +000097 KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
98 KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
99 KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
100 KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
Steve Naroffee1de132007-10-10 21:53:07 +0000101
102 TUScope = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000103}
104
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000105void Sema::DeleteExpr(ExprTy *E) {
106 delete static_cast<Expr*>(E);
107}
108void Sema::DeleteStmt(StmtTy *S) {
109 delete static_cast<Stmt*>(S);
110}
111
Chris Lattner4b009652007-07-25 00:24:17 +0000112//===----------------------------------------------------------------------===//
113// Helper functions.
114//===----------------------------------------------------------------------===//
115
116bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
117 PP.getDiagnostics().Report(Loc, DiagID);
118 return true;
119}
120
121bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
122 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
123 return true;
124}
125
126bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
127 const std::string &Msg2) {
128 std::string MsgArr[] = { Msg1, Msg2 };
129 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
130 return true;
131}
132
133bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
134 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
135 return true;
136}
137
138bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
139 SourceRange Range) {
140 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
141 return true;
142}
143
144bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
145 const std::string &Msg2, SourceRange Range) {
146 std::string MsgArr[] = { Msg1, Msg2 };
147 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
148 return true;
149}
150
151bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
152 SourceRange R1, SourceRange R2) {
153 SourceRange RangeArr[] = { R1, R2 };
154 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
155 return true;
156}
157
158bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
159 SourceRange R1, SourceRange R2) {
160 SourceRange RangeArr[] = { R1, R2 };
161 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
162 return true;
163}
164
165bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
166 const std::string &Msg2, SourceRange R1, SourceRange R2) {
167 std::string MsgArr[] = { Msg1, Msg2 };
168 SourceRange RangeArr[] = { R1, R2 };
169 PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
170 return true;
171}
172
173const LangOptions &Sema::getLangOptions() const {
174 return PP.getLangOptions();
175}