blob: 8be821290bba68408610d648977aad2a6df81a4d [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 Dunbar221fa942008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000018#include "clang/Lex/Preprocessor.h"
Chris Lattnerd6647d32007-05-16 17:56:50 +000019#include "clang/Basic/Diagnostic.h"
Steve Naroff6d40db02007-10-31 18:42:27 +000020#include "clang/Parse/Scope.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000021
Chris Lattnerc11438c2006-08-18 05:17:52 +000022using namespace clang;
23
Ted Kremenek1b0ea822008-01-07 19:49:32 +000024bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroff6d40db02007-10-31 18:42:27 +000025 const char *typeName = TD->getIdentifier()->getName();
26 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +000027 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroff6d40db02007-10-31 18:42:27 +000028}
29
Steve Naroffc62adb62007-10-09 22:01:59 +000030void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
31 TUScope = S;
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000032 CurContext = Context.getTranslationUnitDecl();
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000033 if (!PP.getLangOptions().ObjC1) return;
34
Steve Narofff0ed31a2008-02-24 16:25:02 +000035 // Synthesize "typedef struct objc_selector *SEL;"
Chris Lattnerca1e8482008-07-21 07:06:49 +000036 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct,
37 CurContext,
Chris Lattner96c460d2008-03-15 21:32:50 +000038 SourceLocation(),
Chris Lattnera7b32872008-03-15 06:12:44 +000039 &Context.Idents.get("objc_selector"),
Chris Lattner96c460d2008-03-15 21:32:50 +000040 0);
Argyrios Kyrtzidisb8a49202008-04-12 00:47:19 +000041 PushOnScopeChains(SelTag, TUScope);
Steve Narofff0ed31a2008-02-24 16:25:02 +000042
43 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattnerc5ffed42008-04-04 06:12:32 +000044 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
45 SourceLocation(),
Chris Lattnera7b32872008-03-15 06:12:44 +000046 &Context.Idents.get("SEL"),
Chris Lattner96c460d2008-03-15 21:32:50 +000047 SelT, 0);
Argyrios Kyrtzidisb8a49202008-04-12 00:47:19 +000048 PushOnScopeChains(SelTypedef, TUScope);
Steve Narofff0ed31a2008-02-24 16:25:02 +000049 Context.setObjCSelType(SelTypedef);
Chris Lattner5a92bab2008-06-21 20:20:39 +000050
Chris Lattner7fa27582008-06-21 22:44:51 +000051 // FIXME: Make sure these don't leak!
Chris Lattner5a92bab2008-06-21 20:20:39 +000052 RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
53 CurContext,
54 SourceLocation(),
55 &Context.Idents.get("objc_class"),
56 0);
57 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
58 TypedefDecl *ClassTypedef =
59 TypedefDecl::Create(Context, CurContext, SourceLocation(),
60 &Context.Idents.get("Class"), ClassT, 0);
61 PushOnScopeChains(ClassTag, TUScope);
62 PushOnScopeChains(ClassTypedef, TUScope);
63 Context.setObjCClassType(ClassTypedef);
64 // Synthesize "@class Protocol;
65 ObjCInterfaceDecl *ProtocolDecl =
Chris Lattnerca1e8482008-07-21 07:06:49 +000066 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner5a92bab2008-06-21 20:20:39 +000067 &Context.Idents.get("Protocol"),
68 SourceLocation(), true);
69 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
70 PushOnScopeChains(ProtocolDecl, TUScope);
71
72 // Synthesize "typedef struct objc_object { Class isa; } *id;"
73 RecordDecl *ObjectTag =
74 RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
75 SourceLocation(),
76 &Context.Idents.get("objc_object"), 0);
77 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
78 PushOnScopeChains(ObjectTag, TUScope);
79 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
80 SourceLocation(),
81 &Context.Idents.get("id"),
82 ObjT, 0);
83 PushOnScopeChains(IdTypedef, TUScope);
84 Context.setObjCIdType(IdTypedef);
Steve Naroff7f549f12007-10-10 21:53:07 +000085}
86
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000087Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argyrios Kyrtzidis853fbea2008-06-28 06:07:14 +000088 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) {
Chris Lattnerb87b1b32007-08-10 20:18:51 +000089
90 // Get IdentifierInfo objects for known functions for which we
91 // do extra checking.
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000092 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattnerb87b1b32007-08-10 20:18:51 +000093
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000094 KnownFunctionIDs[id_printf] = &IT.get("printf");
95 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
96 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
97 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
98 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Ted Kremenek34f664d2008-06-16 18:00:42 +000099 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattnerfe0e0af2008-02-06 00:46:58 +0000100 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
101 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
102 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
103 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
104 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff6d40db02007-10-31 18:42:27 +0000105
Steve Naroff7f549f12007-10-10 21:53:07 +0000106 TUScope = 0;
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000107 if (getLangOptions().CPlusPlus)
108 FieldCollector.reset(new CXXFieldCollector());
Steve Naroff38d31b42007-02-28 01:22:02 +0000109}
Chris Lattnercb6a3822006-11-10 06:20:45 +0000110
Chris Lattnera65e1f32008-01-16 19:17:22 +0000111/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
112/// If there is already an implicit cast, merge into the existing one.
113void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattner574dee62008-07-26 22:17:49 +0000114 if (Context.getCanonicalType(Expr->getType()) ==
115 Context.getCanonicalType(Type)) return;
Chris Lattnera65e1f32008-01-16 19:17:22 +0000116
117 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
118 ImpCast->setType(Type);
119 else
120 Expr = new ImplicitCastExpr(Type, Expr);
121}
122
123
124
Chris Lattner57c523f2007-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 Lattnerc11438c2006-08-18 05:17:52 +0000132//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +0000133// Helper functions.
134//===----------------------------------------------------------------------===//
135
Chris Lattnerd6647d32007-05-16 17:56:50 +0000136bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000137 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Chris Lattnerd6647d32007-05-16 17:56:50 +0000138 return true;
139}
140
Steve Narofff1e53692007-03-23 22:27:02 +0000141bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000142 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattnerd6647d32007-05-16 17:56:50 +0000143 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 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000149 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000150 return true;
Chris Lattnereaafe1222006-11-10 05:17:58 +0000151}
152
Steve Naroff71ce2e02007-05-18 22:53:50 +0000153bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000154 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Steve Naroffbc2f0992007-03-30 20:09:34 +0000155 return true;
156}
157
Steve Naroff71ce2e02007-05-18 22:53:50 +0000158bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
159 SourceRange Range) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000160 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000161 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 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000167 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000168 return true;
169}
170
Chris Lattner816dea22008-01-03 23:38:43 +0000171bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
172 const std::string &Msg2, const std::string &Msg3,
173 SourceRange R1) {
174 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
175 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
176 return true;
177}
178
Steve Naroff71ce2e02007-05-18 22:53:50 +0000179bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
180 SourceRange R1, SourceRange R2) {
181 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000182 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000183 return true;
184}
185
186bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
187 SourceRange R1, SourceRange R2) {
188 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000189 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000190 return true;
191}
192
193bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
194 const std::string &Msg2, SourceRange R1, SourceRange R2) {
195 std::string MsgArr[] = { Msg1, Msg2 };
196 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000197 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000198 return true;
Steve Naroff8160ea22007-03-06 01:09:46 +0000199}
200
Chris Lattnerac18be92006-11-20 06:49:47 +0000201const LangOptions &Sema::getLangOptions() const {
Steve Naroff38d31b42007-02-28 01:22:02 +0000202 return PP.getLangOptions();
Chris Lattnerac18be92006-11-20 06:49:47 +0000203}