blob: 2edf08ec4256778840ede150bff0b44639e35e2b [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 Dunbar6e8aa532008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000019#include "clang/Lex/Preprocessor.h"
Chris Lattnerd6647d32007-05-16 17:56:50 +000020#include "clang/Basic/Diagnostic.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000021using namespace clang;
22
Chris Lattner8488c822008-11-18 07:04:44 +000023static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonfbcd8512008-08-23 22:20:38 +000024 if (C.getLangOptions().CPlusPlus)
25 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
26 C.getTranslationUnitDecl(),
Ted Kremenek47923c72008-09-05 01:34:33 +000027 SourceLocation(), &C.Idents.get(Name));
Chris Lattner3b054132008-11-19 05:08:23 +000028
29 return RecordDecl::Create(C, TagDecl::TK_struct,
30 C.getTranslationUnitDecl(),
31 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonfbcd8512008-08-23 22:20:38 +000032}
33
Steve Naroffc62adb62007-10-09 22:01:59 +000034void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
35 TUScope = S;
Argyrios Kyrtzidis16ac9be2008-11-08 17:17:31 +000036 PushDeclContext(Context.getTranslationUnitDecl());
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000037 if (!PP.getLangOptions().ObjC1) return;
38
Steve Narofff0ed31a2008-02-24 16:25:02 +000039 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlssonfbcd8512008-08-23 22:20:38 +000040 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
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!
Anders Carlssonfbcd8512008-08-23 22:20:38 +000052 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner5a92bab2008-06-21 20:20:39 +000053 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
54 TypedefDecl *ClassTypedef =
55 TypedefDecl::Create(Context, CurContext, SourceLocation(),
56 &Context.Idents.get("Class"), ClassT, 0);
57 PushOnScopeChains(ClassTag, TUScope);
58 PushOnScopeChains(ClassTypedef, TUScope);
59 Context.setObjCClassType(ClassTypedef);
60 // Synthesize "@class Protocol;
61 ObjCInterfaceDecl *ProtocolDecl =
Chris Lattnerca1e8482008-07-21 07:06:49 +000062 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner5a92bab2008-06-21 20:20:39 +000063 &Context.Idents.get("Protocol"),
64 SourceLocation(), true);
65 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
66 PushOnScopeChains(ProtocolDecl, TUScope);
67
68 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Anders Carlssonfbcd8512008-08-23 22:20:38 +000069 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
70
Chris Lattner5a92bab2008-06-21 20:20:39 +000071 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
72 PushOnScopeChains(ObjectTag, TUScope);
73 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
74 SourceLocation(),
75 &Context.Idents.get("id"),
76 ObjT, 0);
77 PushOnScopeChains(IdTypedef, TUScope);
78 Context.setObjCIdType(IdTypedef);
Steve Naroff7f549f12007-10-10 21:53:07 +000079}
80
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000081Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Chris Lattnerbc495d72008-11-22 08:28:49 +000082 : PP(pp), Context(ctxt), Consumer(consumer), Diags(PP.getDiagnostics()),
83 SourceMgr(PP.getSourceManager()), CurContext(0), PreDeclaratorDC(0),
Argyrios Kyrtzidis16ac9be2008-11-08 17:17:31 +000084 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()) {
Chris Lattnerb87b1b32007-08-10 20:18:51 +000085
86 // Get IdentifierInfo objects for known functions for which we
87 // do extra checking.
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000088 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattnerb87b1b32007-08-10 20:18:51 +000089
Daniel Dunbardd9b2d12008-10-02 18:44:07 +000090 KnownFunctionIDs[id_printf] = &IT.get("printf");
91 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
92 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
93 KnownFunctionIDs[id_sprintf_chk] = &IT.get("__builtin___sprintf_chk");
94 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
95 KnownFunctionIDs[id_snprintf_chk] = &IT.get("__builtin___snprintf_chk");
96 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
97 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
98 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
99 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
100 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
101 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
102 KnownFunctionIDs[id_vsprintf_chk] = &IT.get("__builtin___vsprintf_chk");
103 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
104 KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk");
105 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff6d40db02007-10-31 18:42:27 +0000106
Sebastian Redlc4704762008-11-11 11:37:55 +0000107 StdNamespace = 0;
Steve Naroff7f549f12007-10-10 21:53:07 +0000108 TUScope = 0;
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000109 if (getLangOptions().CPlusPlus)
110 FieldCollector.reset(new CXXFieldCollector());
Steve Naroff38d31b42007-02-28 01:22:02 +0000111}
Chris Lattnercb6a3822006-11-10 06:20:45 +0000112
Chris Lattnera65e1f32008-01-16 19:17:22 +0000113/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
114/// If there is already an implicit cast, merge into the existing one.
Douglas Gregora11693b2008-11-12 17:17:38 +0000115 /// If isLvalue, the result of the cast is an lvalue.
116void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
Mon P Wang74b32072008-09-04 08:38:01 +0000117 QualType ExprTy = Context.getCanonicalType(Expr->getType());
118 QualType TypeTy = Context.getCanonicalType(Ty);
119
120 if (ExprTy == TypeTy)
121 return;
122
123 if (Expr->getType().getTypePtr()->isPointerType() &&
124 Ty.getTypePtr()->isPointerType()) {
125 QualType ExprBaseType =
126 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
127 QualType BaseType =
128 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
129 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerf490e152008-11-19 05:27:50 +0000130 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
131 << Expr->getSourceRange();
Mon P Wang74b32072008-09-04 08:38:01 +0000132 }
133 }
Chris Lattnera65e1f32008-01-16 19:17:22 +0000134
Douglas Gregora11693b2008-11-12 17:17:38 +0000135 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wang74b32072008-09-04 08:38:01 +0000136 ImpCast->setType(Ty);
Douglas Gregora11693b2008-11-12 17:17:38 +0000137 ImpCast->setLvalueCast(isLvalue);
138 } else
139 Expr = new ImplicitCastExpr(Ty, Expr, isLvalue);
Chris Lattnera65e1f32008-01-16 19:17:22 +0000140}
141
Chris Lattner57c523f2007-08-31 04:53:24 +0000142void Sema::DeleteExpr(ExprTy *E) {
143 delete static_cast<Expr*>(E);
144}
145void Sema::DeleteStmt(StmtTy *S) {
146 delete static_cast<Stmt*>(S);
147}
148
Chris Lattnerf4404402008-08-23 03:19:52 +0000149/// ActOnEndOfTranslationUnit - This is called at the very end of the
150/// translation unit when EOF is reached and all but the top-level scope is
151/// popped.
152void Sema::ActOnEndOfTranslationUnit() {
153
154}
155
156
Chris Lattnerc11438c2006-08-18 05:17:52 +0000157//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +0000158// Helper functions.
159//===----------------------------------------------------------------------===//
160
Chris Lattnerac18be92006-11-20 06:49:47 +0000161const LangOptions &Sema::getLangOptions() const {
Steve Naroff38d31b42007-02-28 01:22:02 +0000162 return PP.getLangOptions();
Chris Lattnerac18be92006-11-20 06:49:47 +0000163}
Daniel Dunbar6e8aa532008-08-11 05:35:13 +0000164
165ObjCMethodDecl *Sema::getCurMethodDecl() {
Steve Naroffecf2bb82008-11-17 16:28:52 +0000166 DeclContext *DC = CurContext;
167 while (isa<BlockDecl>(DC))
168 DC = DC->getParent();
169 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbar6e8aa532008-08-11 05:35:13 +0000170}