blob: b78336b6cd2d6b75aa8c946f4ea3b5d31fbf7784 [file] [log] [blame]
Chris Lattnerddd6fc82006-11-10 04:58:55 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerddd6fc82006-11-10 04:58:55 +000010// This file implements the actions class which performs semantic analysis and
11// builds an AST out of a parse stream.
Chris Lattner3e7bd4e2006-08-17 05:51:27 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerddd6fc82006-11-10 04:58:55 +000015#include "Sema.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000019#include "clang/Lex/Preprocessor.h"
Chris Lattnerd6647d32007-05-16 17:56:50 +000020#include "clang/Basic/Diagnostic.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000021
Chris Lattnerc11438c2006-08-18 05:17:52 +000022using namespace clang;
23
Anders Carlssonfbcd8512008-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 Kremenek47923c72008-09-05 01:34:33 +000029 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonfbcd8512008-08-23 22:20:38 +000030 else
31 return RecordDecl::Create(C, TagDecl::TK_struct,
32 C.getTranslationUnitDecl(),
Ted Kremenek47923c72008-09-05 01:34:33 +000033 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonfbcd8512008-08-23 22:20:38 +000034}
35
Steve Naroffc62adb62007-10-09 22:01:59 +000036void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
37 TUScope = S;
Argyrios Kyrtzidis16ac9be2008-11-08 17:17:31 +000038 PushDeclContext(Context.getTranslationUnitDecl());
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000039 if (!PP.getLangOptions().ObjC1) return;
40
Steve Narofff0ed31a2008-02-24 16:25:02 +000041 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlssonfbcd8512008-08-23 22:20:38 +000042 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
Argyrios Kyrtzidisb8a49202008-04-12 00:47:19 +000043 PushOnScopeChains(SelTag, TUScope);
Steve Narofff0ed31a2008-02-24 16:25:02 +000044
45 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattnerc5ffed42008-04-04 06:12:32 +000046 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
47 SourceLocation(),
Chris Lattnera7b32872008-03-15 06:12:44 +000048 &Context.Idents.get("SEL"),
Chris Lattner96c460d2008-03-15 21:32:50 +000049 SelT, 0);
Argyrios Kyrtzidisb8a49202008-04-12 00:47:19 +000050 PushOnScopeChains(SelTypedef, TUScope);
Steve Narofff0ed31a2008-02-24 16:25:02 +000051 Context.setObjCSelType(SelTypedef);
Chris Lattner5a92bab2008-06-21 20:20:39 +000052
Chris Lattner7fa27582008-06-21 22:44:51 +000053 // FIXME: Make sure these don't leak!
Anders Carlssonfbcd8512008-08-23 22:20:38 +000054 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner5a92bab2008-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 Lattnerca1e8482008-07-21 07:06:49 +000064 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner5a92bab2008-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 Carlssonfbcd8512008-08-23 22:20:38 +000071 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
72
Chris Lattner5a92bab2008-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 Naroff7f549f12007-10-10 21:53:07 +000081}
82
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000083Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argyrios Kyrtzidis16ac9be2008-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 Lattnerb87b1b32007-08-10 20:18:51 +000086
87 // Get IdentifierInfo objects for known functions for which we
88 // do extra checking.
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000089 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattnerb87b1b32007-08-10 20:18:51 +000090
Daniel Dunbardd9b2d12008-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 Naroff6d40db02007-10-31 18:42:27 +0000107
Daniel Dunbar12c9ddc2008-08-14 22:04:54 +0000108 SuperID = &IT.get("super");
109
Steve Naroff44cfcb62008-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 Redlc4704762008-11-11 11:37:55 +0000116 Ident_StdNs = &IT.get("std");
117 Ident_TypeInfo = 0;
118 StdNamespace = 0;
119
Steve Naroff7f549f12007-10-10 21:53:07 +0000120 TUScope = 0;
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000121 if (getLangOptions().CPlusPlus)
122 FieldCollector.reset(new CXXFieldCollector());
Steve Naroff38d31b42007-02-28 01:22:02 +0000123}
Chris Lattnercb6a3822006-11-10 06:20:45 +0000124
Chris Lattnera65e1f32008-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.
Douglas Gregora11693b2008-11-12 17:17:38 +0000127 /// If isLvalue, the result of the cast is an lvalue.
128void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
Mon P Wang74b32072008-09-04 08:38:01 +0000129 QualType ExprTy = Context.getCanonicalType(Expr->getType());
130 QualType TypeTy = Context.getCanonicalType(Ty);
131
132 if (ExprTy == TypeTy)
133 return;
134
135 if (Expr->getType().getTypePtr()->isPointerType() &&
136 Ty.getTypePtr()->isPointerType()) {
137 QualType ExprBaseType =
138 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
139 QualType BaseType =
140 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
141 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
142 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast,
143 Expr->getSourceRange());
144 }
145 }
Chris Lattnera65e1f32008-01-16 19:17:22 +0000146
Douglas Gregora11693b2008-11-12 17:17:38 +0000147 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wang74b32072008-09-04 08:38:01 +0000148 ImpCast->setType(Ty);
Douglas Gregora11693b2008-11-12 17:17:38 +0000149 ImpCast->setLvalueCast(isLvalue);
150 } else
151 Expr = new ImplicitCastExpr(Ty, Expr, isLvalue);
Chris Lattnera65e1f32008-01-16 19:17:22 +0000152}
153
Chris Lattner57c523f2007-08-31 04:53:24 +0000154void Sema::DeleteExpr(ExprTy *E) {
155 delete static_cast<Expr*>(E);
156}
157void Sema::DeleteStmt(StmtTy *S) {
158 delete static_cast<Stmt*>(S);
159}
160
Chris Lattnerf4404402008-08-23 03:19:52 +0000161/// ActOnEndOfTranslationUnit - This is called at the very end of the
162/// translation unit when EOF is reached and all but the top-level scope is
163/// popped.
164void Sema::ActOnEndOfTranslationUnit() {
165
166}
167
168
Chris Lattnerc11438c2006-08-18 05:17:52 +0000169//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +0000170// Helper functions.
171//===----------------------------------------------------------------------===//
172
Chris Lattnerd6647d32007-05-16 17:56:50 +0000173bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000174 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Chris Lattnerd6647d32007-05-16 17:56:50 +0000175 return true;
176}
177
Steve Narofff1e53692007-03-23 22:27:02 +0000178bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000179 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattnerd6647d32007-05-16 17:56:50 +0000180 return true;
181}
182
183bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
184 const std::string &Msg2) {
185 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000186 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000187 return true;
Chris Lattnereaafe1222006-11-10 05:17:58 +0000188}
189
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000190bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const SourceRange& Range) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000191 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Steve Naroffbc2f0992007-03-30 20:09:34 +0000192 return true;
193}
194
Steve Naroff71ce2e02007-05-18 22:53:50 +0000195bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000196 const SourceRange& Range) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000197 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000198 return true;
199}
200
201bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000202 const std::string &Msg2, const SourceRange& Range) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000203 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000204 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000205 return true;
206}
207
Chris Lattner816dea22008-01-03 23:38:43 +0000208bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
209 const std::string &Msg2, const std::string &Msg3,
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000210 const SourceRange& R1) {
Chris Lattner816dea22008-01-03 23:38:43 +0000211 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
212 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
213 return true;
214}
215
Steve Naroff71ce2e02007-05-18 22:53:50 +0000216bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000217 const SourceRange& R1, const SourceRange& R2) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000218 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000219 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000220 return true;
221}
222
223bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000224 const SourceRange& R1, const SourceRange& R2) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000225 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000226 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000227 return true;
228}
229
230bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000231 const std::string &Msg2, const SourceRange& R1,
232 const SourceRange& R2) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000233 std::string MsgArr[] = { Msg1, Msg2 };
234 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000235 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000236 return true;
Steve Naroff8160ea22007-03-06 01:09:46 +0000237}
238
Chris Lattnerac18be92006-11-20 06:49:47 +0000239const LangOptions &Sema::getLangOptions() const {
Steve Naroff38d31b42007-02-28 01:22:02 +0000240 return PP.getLangOptions();
Chris Lattnerac18be92006-11-20 06:49:47 +0000241}
Daniel Dunbar6e8aa532008-08-11 05:35:13 +0000242
243ObjCMethodDecl *Sema::getCurMethodDecl() {
244 return dyn_cast<ObjCMethodDecl>(CurContext);
245}