blob: 46c9e994ce15110c9b7812450a25a59fa5166429 [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
Ted Kremeneka526c5c2008-01-07 19:49:32 +000024bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroff8ee529b2007-10-31 18:42:27 +000025 const char *typeName = TD->getIdentifier()->getName();
26 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +000027 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroff8ee529b2007-10-31 18:42:27 +000028}
29
Steve Naroffb216c882007-10-09 22:01:59 +000030void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
31 TUScope = S;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000032 CurContext = Context.getTranslationUnitDecl();
Chris Lattner2ae34ed2008-02-06 00:46:58 +000033 if (!PP.getLangOptions().ObjC1) return;
34
Steve Naroff69d63752008-02-24 16:25:02 +000035 // Synthesize "typedef struct objc_selector *SEL;"
Chris Lattnerb752f282008-07-21 07:06:49 +000036 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct,
37 CurContext,
Chris Lattnerc63e6602008-03-15 21:32:50 +000038 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000039 &Context.Idents.get("objc_selector"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000040 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000041 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000042
43 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000044 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
45 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000046 &Context.Idents.get("SEL"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000047 SelT, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000048 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000049 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000050
Chris Lattner27933c12008-06-21 22:44:51 +000051 // FIXME: Make sure these don't leak!
Chris Lattner6ee1f9c2008-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 Lattnerb752f282008-07-21 07:06:49 +000066 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner6ee1f9c2008-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 Naroff3b950172007-10-10 21:53:07 +000085}
86
Chris Lattner2ae34ed2008-02-06 00:46:58 +000087Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000088 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) {
Chris Lattner59907c42007-08-10 20:18:51 +000089
90 // Get IdentifierInfo objects for known functions for which we
91 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +000092 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +000093
Chris Lattner2ae34ed2008-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 Kremenek7ff22b22008-06-16 18:00:42 +000099 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattner2ae34ed2008-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 Naroff8ee529b2007-10-31 18:42:27 +0000105
Steve Naroff3b950172007-10-10 21:53:07 +0000106 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000107 if (getLangOptions().CPlusPlus)
108 FieldCollector.reset(new CXXFieldCollector());
Reid Spencer5f016e22007-07-11 17:01:13 +0000109}
110
Chris Lattner1e0a3902008-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 Lattnerb77792e2008-07-26 22:17:49 +0000114 if (Context.getCanonicalType(Expr->getType()) ==
115 Context.getCanonicalType(Type)) return;
Chris Lattner1e0a3902008-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 Lattner394a3fd2007-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000132//===----------------------------------------------------------------------===//
133// Helper functions.
134//===----------------------------------------------------------------------===//
135
136bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000137 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 return true;
139}
140
141bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000142 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek9c728dc2007-12-12 22:39:36 +0000149 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 return true;
151}
152
153bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000154 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 return true;
156}
157
158bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
159 SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000160 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek9c728dc2007-12-12 22:39:36 +0000167 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 return true;
169}
170
Chris Lattner4667ac32008-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000179bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
180 SourceRange R1, SourceRange R2) {
181 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000182 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek9c728dc2007-12-12 22:39:36 +0000189 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek9c728dc2007-12-12 22:39:36 +0000197 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 return true;
199}
200
201const LangOptions &Sema::getLangOptions() const {
202 return PP.getLangOptions();
203}
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000204
205ObjCMethodDecl *Sema::getCurMethodDecl() {
206 return dyn_cast<ObjCMethodDecl>(CurContext);
207}