blob: e2986a87a54dac2370b3d9d9474dfdef44ab8a82 [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"
17#include "clang/Lex/Preprocessor.h"
18#include "clang/Basic/Diagnostic.h"
Steve Naroff8ee529b2007-10-31 18:42:27 +000019#include "clang/Parse/Scope.h"
Chris Lattner59907c42007-08-10 20:18:51 +000020
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Ted Kremeneka526c5c2008-01-07 19:49:32 +000023bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroff8ee529b2007-10-31 18:42:27 +000024 const char *typeName = TD->getIdentifier()->getName();
25 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +000026 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroff8ee529b2007-10-31 18:42:27 +000027}
28
Steve Naroffb216c882007-10-09 22:01:59 +000029void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
30 TUScope = S;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000031 CurContext = Context.getTranslationUnitDecl();
Chris Lattner2ae34ed2008-02-06 00:46:58 +000032 if (!PP.getLangOptions().ObjC1) return;
33
Steve Naroff69d63752008-02-24 16:25:02 +000034 // Synthesize "typedef struct objc_selector *SEL;"
Chris Lattnerb752f282008-07-21 07:06:49 +000035 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct,
36 CurContext,
Chris Lattnerc63e6602008-03-15 21:32:50 +000037 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000038 &Context.Idents.get("objc_selector"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000039 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000040 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000041
42 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000043 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
44 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000045 &Context.Idents.get("SEL"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000046 SelT, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000047 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000048 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000049
Chris Lattner27933c12008-06-21 22:44:51 +000050 // FIXME: Make sure these don't leak!
Chris Lattner6ee1f9c2008-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 Lattnerb752f282008-07-21 07:06:49 +000065 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner6ee1f9c2008-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 Naroff3b950172007-10-10 21:53:07 +000084}
85
Chris Lattner2ae34ed2008-02-06 00:46:58 +000086Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000087 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) {
Chris Lattner59907c42007-08-10 20:18:51 +000088
89 // Get IdentifierInfo objects for known functions for which we
90 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +000091 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +000092
Chris Lattner2ae34ed2008-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 Kremenek7ff22b22008-06-16 18:00:42 +000098 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattner2ae34ed2008-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 Naroff8ee529b2007-10-31 18:42:27 +0000104
Steve Naroff3b950172007-10-10 21:53:07 +0000105 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000106 if (getLangOptions().CPlusPlus)
107 FieldCollector.reset(new CXXFieldCollector());
Reid Spencer5f016e22007-07-11 17:01:13 +0000108}
109
Chris Lattner1e0a3902008-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 Lattner438757c2008-02-13 18:01:07 +0000113 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000114
115 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
116 ImpCast->setType(Type);
117 else
118 Expr = new ImplicitCastExpr(Type, Expr);
119}
120
121
122
Chris Lattner394a3fd2007-08-31 04:53:24 +0000123void Sema::DeleteExpr(ExprTy *E) {
124 delete static_cast<Expr*>(E);
125}
126void Sema::DeleteStmt(StmtTy *S) {
127 delete static_cast<Stmt*>(S);
128}
129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130//===----------------------------------------------------------------------===//
131// Helper functions.
132//===----------------------------------------------------------------------===//
133
134bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000135 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 return true;
137}
138
139bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000140 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 return true;
142}
143
144bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
145 const std::string &Msg2) {
146 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000147 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 return true;
149}
150
151bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000152 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 return true;
154}
155
156bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
157 SourceRange Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000158 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 return true;
160}
161
162bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
163 const std::string &Msg2, SourceRange Range) {
164 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000165 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 return true;
167}
168
Chris Lattner4667ac32008-01-03 23:38:43 +0000169bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
170 const std::string &Msg2, const std::string &Msg3,
171 SourceRange R1) {
172 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
173 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
174 return true;
175}
176
Reid Spencer5f016e22007-07-11 17:01:13 +0000177bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
178 SourceRange R1, SourceRange R2) {
179 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000180 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 return true;
182}
183
184bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
185 SourceRange R1, SourceRange R2) {
186 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000187 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 return true;
189}
190
191bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
192 const std::string &Msg2, SourceRange R1, SourceRange R2) {
193 std::string MsgArr[] = { Msg1, Msg2 };
194 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000195 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 return true;
197}
198
199const LangOptions &Sema::getLangOptions() const {
200 return PP.getLangOptions();
201}