blob: 9c8d0c8bcb414ed348bd86e166102fe7de6c800b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Lex/Preprocessor.h"
20#include "clang/Basic/Diagnostic.h"
Chris Lattner59907c42007-08-10 20:18:51 +000021
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Anders Carlssonc3036062008-08-23 22:20:38 +000024static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name)
25{
26 if (C.getLangOptions().CPlusPlus)
27 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
28 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +000029 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +000030 else
31 return RecordDecl::Create(C, TagDecl::TK_struct,
32 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +000033 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +000034}
35
Steve Naroffb216c882007-10-09 22:01:59 +000036void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
37 TUScope = S;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000038 PushDeclContext(Context.getTranslationUnitDecl());
Chris Lattner2ae34ed2008-02-06 00:46:58 +000039 if (!PP.getLangOptions().ObjC1) return;
40
Steve Naroff69d63752008-02-24 16:25:02 +000041 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlssonc3036062008-08-23 22:20:38 +000042 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000043 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000044
45 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000046 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
47 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000048 &Context.Idents.get("SEL"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000049 SelT, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000050 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000051 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000052
Chris Lattner27933c12008-06-21 22:44:51 +000053 // FIXME: Make sure these don't leak!
Anders Carlssonc3036062008-08-23 22:20:38 +000054 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000055 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
56 TypedefDecl *ClassTypedef =
57 TypedefDecl::Create(Context, CurContext, SourceLocation(),
58 &Context.Idents.get("Class"), ClassT, 0);
59 PushOnScopeChains(ClassTag, TUScope);
60 PushOnScopeChains(ClassTypedef, TUScope);
61 Context.setObjCClassType(ClassTypedef);
62 // Synthesize "@class Protocol;
63 ObjCInterfaceDecl *ProtocolDecl =
Chris Lattnerb752f282008-07-21 07:06:49 +000064 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000065 &Context.Idents.get("Protocol"),
66 SourceLocation(), true);
67 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
68 PushOnScopeChains(ProtocolDecl, TUScope);
69
70 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Anders Carlssonc3036062008-08-23 22:20:38 +000071 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
72
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000073 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
74 PushOnScopeChains(ObjectTag, TUScope);
75 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
76 SourceLocation(),
77 &Context.Idents.get("id"),
78 ObjT, 0);
79 PushOnScopeChains(IdTypedef, TUScope);
80 Context.setObjCIdType(IdTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +000081}
82
Chris Lattner2ae34ed2008-02-06 00:46:58 +000083Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000084 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0),PreDeclaratorDC(0),
85 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()) {
Chris Lattner59907c42007-08-10 20:18:51 +000086
87 // Get IdentifierInfo objects for known functions for which we
88 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +000089 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +000090
Daniel Dunbarde454282008-10-02 18:44:07 +000091 KnownFunctionIDs[id_printf] = &IT.get("printf");
92 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
93 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
94 KnownFunctionIDs[id_sprintf_chk] = &IT.get("__builtin___sprintf_chk");
95 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
96 KnownFunctionIDs[id_snprintf_chk] = &IT.get("__builtin___snprintf_chk");
97 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
98 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
99 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
100 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
101 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
102 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
103 KnownFunctionIDs[id_vsprintf_chk] = &IT.get("__builtin___vsprintf_chk");
104 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
105 KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk");
106 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000107
Daniel Dunbar662e8b52008-08-14 22:04:54 +0000108 SuperID = &IT.get("super");
109
Steve Naroff2b255c42008-09-09 14:32:20 +0000110 // ObjC builtin typedef names.
111 Ident_id = &IT.get("id");
112 Ident_Class = &IT.get("Class");
113 Ident_SEL = &IT.get("SEL");
114 Ident_Protocol = &IT.get("Protocol");
115
Sebastian Redlc42e1182008-11-11 11:37:55 +0000116 Ident_StdNs = &IT.get("std");
117 Ident_TypeInfo = 0;
118 StdNamespace = 0;
119
Steve Naroff3b950172007-10-10 21:53:07 +0000120 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000121 if (getLangOptions().CPlusPlus)
122 FieldCollector.reset(new CXXFieldCollector());
Reid Spencer5f016e22007-07-11 17:01:13 +0000123}
124
Chris Lattner1e0a3902008-01-16 19:17:22 +0000125/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
126/// If there is already an implicit cast, merge into the existing one.
Mon P Wang3a2c7442008-09-04 08:38:01 +0000127void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty) {
128 QualType ExprTy = Context.getCanonicalType(Expr->getType());
129 QualType TypeTy = Context.getCanonicalType(Ty);
130
131 if (ExprTy == TypeTy)
132 return;
133
134 if (Expr->getType().getTypePtr()->isPointerType() &&
135 Ty.getTypePtr()->isPointerType()) {
136 QualType ExprBaseType =
137 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
138 QualType BaseType =
139 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
140 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
141 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast,
142 Expr->getSourceRange());
143 }
144 }
Chris Lattner1e0a3902008-01-16 19:17:22 +0000145
146 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
Mon P Wang3a2c7442008-09-04 08:38:01 +0000147 ImpCast->setType(Ty);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000148 else
Mon P Wang3a2c7442008-09-04 08:38:01 +0000149 Expr = new ImplicitCastExpr(Ty, Expr);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000150}
151
Chris Lattner394a3fd2007-08-31 04:53:24 +0000152void Sema::DeleteExpr(ExprTy *E) {
153 delete static_cast<Expr*>(E);
154}
155void Sema::DeleteStmt(StmtTy *S) {
156 delete static_cast<Stmt*>(S);
157}
158
Chris Lattner9299f3f2008-08-23 03:19:52 +0000159/// ActOnEndOfTranslationUnit - This is called at the very end of the
160/// translation unit when EOF is reached and all but the top-level scope is
161/// popped.
162void Sema::ActOnEndOfTranslationUnit() {
163
164}
165
166
Reid Spencer5f016e22007-07-11 17:01:13 +0000167//===----------------------------------------------------------------------===//
168// Helper functions.
169//===----------------------------------------------------------------------===//
170
171bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000172 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 return true;
174}
175
176bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000177 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 return true;
179}
180
181bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
182 const std::string &Msg2) {
183 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000184 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 return true;
186}
187
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000188bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const SourceRange& Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000189 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 return true;
191}
192
193bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000194 const SourceRange& Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000195 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 return true;
197}
198
199bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000200 const std::string &Msg2, const SourceRange& Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000202 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 return true;
204}
205
Chris Lattner4667ac32008-01-03 23:38:43 +0000206bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
207 const std::string &Msg2, const std::string &Msg3,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000208 const SourceRange& R1) {
Chris Lattner4667ac32008-01-03 23:38:43 +0000209 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
210 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
211 return true;
212}
213
Reid Spencer5f016e22007-07-11 17:01:13 +0000214bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000215 const SourceRange& R1, const SourceRange& R2) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000217 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 return true;
219}
220
221bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000222 const SourceRange& R1, const SourceRange& R2) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000224 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 return true;
226}
227
228bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000229 const std::string &Msg2, const SourceRange& R1,
230 const SourceRange& R2) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 std::string MsgArr[] = { Msg1, Msg2 };
232 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000233 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 return true;
235}
236
237const LangOptions &Sema::getLangOptions() const {
238 return PP.getLangOptions();
239}
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000240
241ObjCMethodDecl *Sema::getCurMethodDecl() {
242 return dyn_cast<ObjCMethodDecl>(CurContext);
243}