blob: d43eadd57441ca1d289368e351f97714e1b623d4 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +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 Dunbar64789f82008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "clang/Lex/Preprocessor.h"
20#include "clang/Basic/Diagnostic.h"
21using namespace clang;
22
Chris Lattnerda5c0872008-11-23 09:13:29 +000023/// ConvertQualTypeToStringFn - This function is used to pretty print the
24/// specified QualType as a string in diagnostics.
25static void ConvertQualTypeToStringFn(intptr_t QT,
26 const char *Modifier, unsigned ML,
27 const char *Argument, unsigned ArgLen,
28 llvm::SmallVectorImpl<char> &Output) {
29 assert(ML == 0 && ArgLen == 0 && "Invalid modifier for QualType argument");
30
31 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(QT)));
32
33 // FIXME: Playing with std::string is really slow.
34 std::string S = Ty.getAsString();
35 Output.append(S.begin(), S.end());
36}
37
38
Chris Lattner6948ae62008-11-18 07:04:44 +000039static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlsson8930fb52008-08-23 22:20:38 +000040 if (C.getLangOptions().CPlusPlus)
41 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
42 C.getTranslationUnitDecl(),
Ted Kremenek2c984042008-09-05 01:34:33 +000043 SourceLocation(), &C.Idents.get(Name));
Chris Lattner8ba580c2008-11-19 05:08:23 +000044
45 return RecordDecl::Create(C, TagDecl::TK_struct,
46 C.getTranslationUnitDecl(),
47 SourceLocation(), &C.Idents.get(Name));
Anders Carlsson8930fb52008-08-23 22:20:38 +000048}
49
Steve Naroff9637a9b2007-10-09 22:01:59 +000050void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
51 TUScope = S;
Argiris Kirtzidis054a2632008-11-08 17:17:31 +000052 PushDeclContext(Context.getTranslationUnitDecl());
Chris Lattnera8c2d592008-02-06 00:46:58 +000053 if (!PP.getLangOptions().ObjC1) return;
54
Steve Naroff8f635e02008-02-24 16:25:02 +000055 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlsson8930fb52008-08-23 22:20:38 +000056 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
Argiris Kirtzidis951f25b2008-04-12 00:47:19 +000057 PushOnScopeChains(SelTag, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +000058
59 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattnereee57c02008-04-04 06:12:32 +000060 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
61 SourceLocation(),
Chris Lattnere4650482008-03-15 06:12:44 +000062 &Context.Idents.get("SEL"),
Chris Lattner58114f02008-03-15 21:32:50 +000063 SelT, 0);
Argiris Kirtzidis951f25b2008-04-12 00:47:19 +000064 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +000065 Context.setObjCSelType(SelTypedef);
Chris Lattner4e9553a2008-06-21 20:20:39 +000066
Chris Lattner1062d3a2008-06-21 22:44:51 +000067 // FIXME: Make sure these don't leak!
Anders Carlsson8930fb52008-08-23 22:20:38 +000068 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner4e9553a2008-06-21 20:20:39 +000069 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
70 TypedefDecl *ClassTypedef =
71 TypedefDecl::Create(Context, CurContext, SourceLocation(),
72 &Context.Idents.get("Class"), ClassT, 0);
73 PushOnScopeChains(ClassTag, TUScope);
74 PushOnScopeChains(ClassTypedef, TUScope);
75 Context.setObjCClassType(ClassTypedef);
76 // Synthesize "@class Protocol;
77 ObjCInterfaceDecl *ProtocolDecl =
Chris Lattner5cece462008-07-21 07:06:49 +000078 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner4e9553a2008-06-21 20:20:39 +000079 &Context.Idents.get("Protocol"),
80 SourceLocation(), true);
81 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
82 PushOnScopeChains(ProtocolDecl, TUScope);
83
84 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Anders Carlsson8930fb52008-08-23 22:20:38 +000085 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
86
Chris Lattner4e9553a2008-06-21 20:20:39 +000087 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
88 PushOnScopeChains(ObjectTag, TUScope);
89 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
90 SourceLocation(),
91 &Context.Idents.get("id"),
92 ObjT, 0);
93 PushOnScopeChains(IdTypedef, TUScope);
94 Context.setObjCIdType(IdTypedef);
Steve Naroffee1de132007-10-10 21:53:07 +000095}
96
Chris Lattnera8c2d592008-02-06 00:46:58 +000097Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Chris Lattnerf9ea6a42008-11-22 08:28:49 +000098 : PP(pp), Context(ctxt), Consumer(consumer), Diags(PP.getDiagnostics()),
99 SourceMgr(PP.getSourceManager()), CurContext(0), PreDeclaratorDC(0),
Argiris Kirtzidis054a2632008-11-08 17:17:31 +0000100 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()) {
Chris Lattner2e64c072007-08-10 20:18:51 +0000101
102 // Get IdentifierInfo objects for known functions for which we
103 // do extra checking.
Chris Lattnera8c2d592008-02-06 00:46:58 +0000104 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner2e64c072007-08-10 20:18:51 +0000105
Daniel Dunbar0ab03e62008-10-02 18:44:07 +0000106 KnownFunctionIDs[id_printf] = &IT.get("printf");
107 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
108 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
109 KnownFunctionIDs[id_sprintf_chk] = &IT.get("__builtin___sprintf_chk");
110 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
111 KnownFunctionIDs[id_snprintf_chk] = &IT.get("__builtin___snprintf_chk");
112 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
113 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
114 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
115 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
116 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
117 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
118 KnownFunctionIDs[id_vsprintf_chk] = &IT.get("__builtin___vsprintf_chk");
119 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
120 KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk");
121 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroffae84af82007-10-31 18:42:27 +0000122
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000123 StdNamespace = 0;
Steve Naroffee1de132007-10-10 21:53:07 +0000124 TUScope = 0;
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000125 if (getLangOptions().CPlusPlus)
126 FieldCollector.reset(new CXXFieldCollector());
Chris Lattnerda5c0872008-11-23 09:13:29 +0000127
128 // Tell diagnostics how to render things from the AST library.
129 PP.getDiagnostics().SetQualTypeToStringFn(ConvertQualTypeToStringFn);
Chris Lattner4b009652007-07-25 00:24:17 +0000130}
131
Chris Lattnere992d6c2008-01-16 19:17:22 +0000132/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
133/// If there is already an implicit cast, merge into the existing one.
Douglas Gregor70d26122008-11-12 17:17:38 +0000134 /// If isLvalue, the result of the cast is an lvalue.
135void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
Mon P Wangc6c92742008-09-04 08:38:01 +0000136 QualType ExprTy = Context.getCanonicalType(Expr->getType());
137 QualType TypeTy = Context.getCanonicalType(Ty);
138
139 if (ExprTy == TypeTy)
140 return;
141
142 if (Expr->getType().getTypePtr()->isPointerType() &&
143 Ty.getTypePtr()->isPointerType()) {
144 QualType ExprBaseType =
145 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
146 QualType BaseType =
147 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
148 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000149 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
150 << Expr->getSourceRange();
Mon P Wangc6c92742008-09-04 08:38:01 +0000151 }
152 }
Chris Lattnere992d6c2008-01-16 19:17:22 +0000153
Douglas Gregor70d26122008-11-12 17:17:38 +0000154 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wangc6c92742008-09-04 08:38:01 +0000155 ImpCast->setType(Ty);
Douglas Gregor70d26122008-11-12 17:17:38 +0000156 ImpCast->setLvalueCast(isLvalue);
157 } else
158 Expr = new ImplicitCastExpr(Ty, Expr, isLvalue);
Chris Lattnere992d6c2008-01-16 19:17:22 +0000159}
160
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000161void Sema::DeleteExpr(ExprTy *E) {
162 delete static_cast<Expr*>(E);
163}
164void Sema::DeleteStmt(StmtTy *S) {
165 delete static_cast<Stmt*>(S);
166}
167
Chris Lattnerc1aea812008-08-23 03:19:52 +0000168/// ActOnEndOfTranslationUnit - This is called at the very end of the
169/// translation unit when EOF is reached and all but the top-level scope is
170/// popped.
171void Sema::ActOnEndOfTranslationUnit() {
172
173}
174
175
Chris Lattner4b009652007-07-25 00:24:17 +0000176//===----------------------------------------------------------------------===//
177// Helper functions.
178//===----------------------------------------------------------------------===//
179
Chris Lattner4b009652007-07-25 00:24:17 +0000180const LangOptions &Sema::getLangOptions() const {
181 return PP.getLangOptions();
182}
Daniel Dunbar64789f82008-08-11 05:35:13 +0000183
184ObjCMethodDecl *Sema::getCurMethodDecl() {
Steve Naroff55debea2008-11-17 16:28:52 +0000185 DeclContext *DC = CurContext;
186 while (isa<BlockDecl>(DC))
187 DC = DC->getParent();
188 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbar64789f82008-08-11 05:35:13 +0000189}