blob: bf9f7a0bb3f55e5fc5d7ec82997e1aff98b91265 [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"
Chris Lattnerd3e98952006-10-06 05:22:26 +000017#include "clang/Lex/Preprocessor.h"
Chris Lattnerd6647d32007-05-16 17:56:50 +000018#include "clang/Basic/Diagnostic.h"
Steve Naroff6d40db02007-10-31 18:42:27 +000019#include "clang/Parse/Scope.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000020
Chris Lattnerc11438c2006-08-18 05:17:52 +000021using namespace clang;
22
Ted Kremenek1b0ea822008-01-07 19:49:32 +000023bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroff6d40db02007-10-31 18:42:27 +000024 const char *typeName = TD->getIdentifier()->getName();
25 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +000026 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroff6d40db02007-10-31 18:42:27 +000027}
28
Steve Naroffc62adb62007-10-09 22:01:59 +000029void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
30 TUScope = S;
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000031 CurContext = Context.getTranslationUnitDecl();
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000032 if (!PP.getLangOptions().ObjC1) return;
33
Steve Narofff0ed31a2008-02-24 16:25:02 +000034 // Synthesize "typedef struct objc_selector *SEL;"
Chris Lattnerca1e8482008-07-21 07:06:49 +000035 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct,
36 CurContext,
Chris Lattner96c460d2008-03-15 21:32:50 +000037 SourceLocation(),
Chris Lattnera7b32872008-03-15 06:12:44 +000038 &Context.Idents.get("objc_selector"),
Chris Lattner96c460d2008-03-15 21:32:50 +000039 0);
Argyrios Kyrtzidisb8a49202008-04-12 00:47:19 +000040 PushOnScopeChains(SelTag, TUScope);
Steve Narofff0ed31a2008-02-24 16:25:02 +000041
42 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattnerc5ffed42008-04-04 06:12:32 +000043 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
44 SourceLocation(),
Chris Lattnera7b32872008-03-15 06:12:44 +000045 &Context.Idents.get("SEL"),
Chris Lattner96c460d2008-03-15 21:32:50 +000046 SelT, 0);
Argyrios Kyrtzidisb8a49202008-04-12 00:47:19 +000047 PushOnScopeChains(SelTypedef, TUScope);
Steve Narofff0ed31a2008-02-24 16:25:02 +000048 Context.setObjCSelType(SelTypedef);
Chris Lattner5a92bab2008-06-21 20:20:39 +000049
Chris Lattner7fa27582008-06-21 22:44:51 +000050 // FIXME: Make sure these don't leak!
Chris Lattner5a92bab2008-06-21 20:20:39 +000051 RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
52 CurContext,
53 SourceLocation(),
54 &Context.Idents.get("objc_class"),
55 0);
56 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
57 TypedefDecl *ClassTypedef =
58 TypedefDecl::Create(Context, CurContext, SourceLocation(),
59 &Context.Idents.get("Class"), ClassT, 0);
60 PushOnScopeChains(ClassTag, TUScope);
61 PushOnScopeChains(ClassTypedef, TUScope);
62 Context.setObjCClassType(ClassTypedef);
63 // Synthesize "@class Protocol;
64 ObjCInterfaceDecl *ProtocolDecl =
Chris Lattnerca1e8482008-07-21 07:06:49 +000065 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner5a92bab2008-06-21 20:20:39 +000066 &Context.Idents.get("Protocol"),
67 SourceLocation(), true);
68 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
69 PushOnScopeChains(ProtocolDecl, TUScope);
70
71 // Synthesize "typedef struct objc_object { Class isa; } *id;"
72 RecordDecl *ObjectTag =
73 RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
74 SourceLocation(),
75 &Context.Idents.get("objc_object"), 0);
76 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
77 PushOnScopeChains(ObjectTag, TUScope);
78 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
79 SourceLocation(),
80 &Context.Idents.get("id"),
81 ObjT, 0);
82 PushOnScopeChains(IdTypedef, TUScope);
83 Context.setObjCIdType(IdTypedef);
Steve Naroff7f549f12007-10-10 21:53:07 +000084}
85
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000086Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argyrios Kyrtzidis853fbea2008-06-28 06:07:14 +000087 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) {
Chris Lattnerb87b1b32007-08-10 20:18:51 +000088
89 // Get IdentifierInfo objects for known functions for which we
90 // do extra checking.
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000091 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattnerb87b1b32007-08-10 20:18:51 +000092
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000093 KnownFunctionIDs[id_printf] = &IT.get("printf");
94 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
95 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
96 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
97 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Ted Kremenek34f664d2008-06-16 18:00:42 +000098 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000099 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_vprintf] = &IT.get("vprintf");
Steve Naroff6d40db02007-10-31 18:42:27 +0000104
Steve Naroff7f549f12007-10-10 21:53:07 +0000105 TUScope = 0;
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000106 if (getLangOptions().CPlusPlus)
107 FieldCollector.reset(new CXXFieldCollector());
Steve Naroff38d31b42007-02-28 01:22:02 +0000108}
Chris Lattnercb6a3822006-11-10 06:20:45 +0000109
Chris Lattnera65e1f32008-01-16 19:17:22 +0000110/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
111/// If there is already an implicit cast, merge into the existing one.
112void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattner574dee62008-07-26 22:17:49 +0000113 if (Context.getCanonicalType(Expr->getType()) ==
114 Context.getCanonicalType(Type)) return;
Chris Lattnera65e1f32008-01-16 19:17:22 +0000115
116 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
117 ImpCast->setType(Type);
118 else
119 Expr = new ImplicitCastExpr(Type, Expr);
120}
121
122
123
Chris Lattner57c523f2007-08-31 04:53:24 +0000124void Sema::DeleteExpr(ExprTy *E) {
125 delete static_cast<Expr*>(E);
126}
127void Sema::DeleteStmt(StmtTy *S) {
128 delete static_cast<Stmt*>(S);
129}
130
Chris Lattnerc11438c2006-08-18 05:17:52 +0000131//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +0000132// Helper functions.
133//===----------------------------------------------------------------------===//
134
Chris Lattnerd6647d32007-05-16 17:56:50 +0000135bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000136 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Chris Lattnerd6647d32007-05-16 17:56:50 +0000137 return true;
138}
139
Steve Narofff1e53692007-03-23 22:27:02 +0000140bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000141 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattnerd6647d32007-05-16 17:56:50 +0000142 return true;
143}
144
145bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
146 const std::string &Msg2) {
147 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000148 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000149 return true;
Chris Lattnereaafe1222006-11-10 05:17:58 +0000150}
151
Steve Naroff71ce2e02007-05-18 22:53:50 +0000152bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000153 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Steve Naroffbc2f0992007-03-30 20:09:34 +0000154 return true;
155}
156
Steve Naroff71ce2e02007-05-18 22:53:50 +0000157bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
158 SourceRange Range) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000159 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000160 return true;
161}
162
163bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
164 const std::string &Msg2, SourceRange Range) {
165 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000166 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000167 return true;
168}
169
Chris Lattner816dea22008-01-03 23:38:43 +0000170bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
171 const std::string &Msg2, const std::string &Msg3,
172 SourceRange R1) {
173 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
174 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
175 return true;
176}
177
Steve Naroff71ce2e02007-05-18 22:53:50 +0000178bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
179 SourceRange R1, SourceRange R2) {
180 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000181 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000182 return true;
183}
184
185bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
186 SourceRange R1, SourceRange R2) {
187 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000188 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000189 return true;
190}
191
192bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
193 const std::string &Msg2, SourceRange R1, SourceRange R2) {
194 std::string MsgArr[] = { Msg1, Msg2 };
195 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000196 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000197 return true;
Steve Naroff8160ea22007-03-06 01:09:46 +0000198}
199
Chris Lattnerac18be92006-11-20 06:49:47 +0000200const LangOptions &Sema::getLangOptions() const {
Steve Naroff38d31b42007-02-28 01:22:02 +0000201 return PP.getLangOptions();
Chris Lattnerac18be92006-11-20 06:49:47 +0000202}