blob: be21c36073c1e4559e1dffe41150a676056402ac [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"
21using namespace clang;
22
Chris Lattner22caddc2008-11-23 09:13:29 +000023/// ConvertQualTypeToStringFn - This function is used to pretty print the
24/// specified QualType as a string in diagnostics.
Chris Lattner011bb4e2008-11-23 20:28:15 +000025static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattner22caddc2008-11-23 09:13:29 +000026 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");
Chris Lattner3fdf4b02008-11-23 09:21:17 +000030
Chris Lattner011bb4e2008-11-23 20:28:15 +000031 std::string S;
32 if (Kind == Diagnostic::ak_qualtype) {
33 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattner22caddc2008-11-23 09:13:29 +000034
Chris Lattner011bb4e2008-11-23 20:28:15 +000035 // FIXME: Playing with std::string is really slow.
36 S = Ty.getAsString();
37 } else {
38 assert(Kind == Diagnostic::ak_declarationname);
39
40 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
41 S = N.getAsString();
42 }
Chris Lattner22caddc2008-11-23 09:13:29 +000043 Output.append(S.begin(), S.end());
44}
45
46
Chris Lattner0a14eee2008-11-18 07:04:44 +000047static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +000048 if (C.getLangOptions().CPlusPlus)
49 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
50 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +000051 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +000052
53 return RecordDecl::Create(C, TagDecl::TK_struct,
54 C.getTranslationUnitDecl(),
55 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +000056}
57
Steve Naroffb216c882007-10-09 22:01:59 +000058void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
59 TUScope = S;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000060 PushDeclContext(Context.getTranslationUnitDecl());
Chris Lattner2ae34ed2008-02-06 00:46:58 +000061 if (!PP.getLangOptions().ObjC1) return;
62
Steve Naroff69d63752008-02-24 16:25:02 +000063 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlssonc3036062008-08-23 22:20:38 +000064 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000065 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000066
67 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000068 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
69 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000070 &Context.Idents.get("SEL"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000071 SelT, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000072 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000073 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000074
Chris Lattner27933c12008-06-21 22:44:51 +000075 // FIXME: Make sure these don't leak!
Anders Carlssonc3036062008-08-23 22:20:38 +000076 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000077 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
78 TypedefDecl *ClassTypedef =
79 TypedefDecl::Create(Context, CurContext, SourceLocation(),
80 &Context.Idents.get("Class"), ClassT, 0);
81 PushOnScopeChains(ClassTag, TUScope);
82 PushOnScopeChains(ClassTypedef, TUScope);
83 Context.setObjCClassType(ClassTypedef);
84 // Synthesize "@class Protocol;
85 ObjCInterfaceDecl *ProtocolDecl =
Chris Lattnerb752f282008-07-21 07:06:49 +000086 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000087 &Context.Idents.get("Protocol"),
88 SourceLocation(), true);
89 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
90 PushOnScopeChains(ProtocolDecl, TUScope);
91
92 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Anders Carlssonc3036062008-08-23 22:20:38 +000093 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
94
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000095 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
96 PushOnScopeChains(ObjectTag, TUScope);
97 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
98 SourceLocation(),
99 &Context.Idents.get("id"),
100 ObjT, 0);
101 PushOnScopeChains(IdTypedef, TUScope);
102 Context.setObjCIdType(IdTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +0000103}
104
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000105Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Chris Lattner3cfa9282008-11-22 08:28:49 +0000106 : PP(pp), Context(ctxt), Consumer(consumer), Diags(PP.getDiagnostics()),
107 SourceMgr(PP.getSourceManager()), CurContext(0), PreDeclaratorDC(0),
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000108 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()) {
Chris Lattner59907c42007-08-10 20:18:51 +0000109
110 // Get IdentifierInfo objects for known functions for which we
111 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000112 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +0000113
Daniel Dunbarde454282008-10-02 18:44:07 +0000114 KnownFunctionIDs[id_printf] = &IT.get("printf");
115 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
116 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
117 KnownFunctionIDs[id_sprintf_chk] = &IT.get("__builtin___sprintf_chk");
118 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
119 KnownFunctionIDs[id_snprintf_chk] = &IT.get("__builtin___snprintf_chk");
120 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
121 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
122 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
123 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
124 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
125 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
126 KnownFunctionIDs[id_vsprintf_chk] = &IT.get("__builtin___vsprintf_chk");
127 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
128 KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk");
129 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000130
Sebastian Redlc42e1182008-11-11 11:37:55 +0000131 StdNamespace = 0;
Steve Naroff3b950172007-10-10 21:53:07 +0000132 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000133 if (getLangOptions().CPlusPlus)
134 FieldCollector.reset(new CXXFieldCollector());
Chris Lattner22caddc2008-11-23 09:13:29 +0000135
136 // Tell diagnostics how to render things from the AST library.
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000137 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn);
Reid Spencer5f016e22007-07-11 17:01:13 +0000138}
139
Chris Lattner1e0a3902008-01-16 19:17:22 +0000140/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
141/// If there is already an implicit cast, merge into the existing one.
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000142 /// If isLvalue, the result of the cast is an lvalue.
143void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000144 QualType ExprTy = Context.getCanonicalType(Expr->getType());
145 QualType TypeTy = Context.getCanonicalType(Ty);
146
147 if (ExprTy == TypeTy)
148 return;
149
150 if (Expr->getType().getTypePtr()->isPointerType() &&
151 Ty.getTypePtr()->isPointerType()) {
152 QualType ExprBaseType =
153 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
154 QualType BaseType =
155 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
156 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000157 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
158 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000159 }
160 }
Chris Lattner1e0a3902008-01-16 19:17:22 +0000161
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000162 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000163 ImpCast->setType(Ty);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000164 ImpCast->setLvalueCast(isLvalue);
165 } else
166 Expr = new ImplicitCastExpr(Ty, Expr, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000167}
168
Chris Lattner394a3fd2007-08-31 04:53:24 +0000169void Sema::DeleteExpr(ExprTy *E) {
170 delete static_cast<Expr*>(E);
171}
172void Sema::DeleteStmt(StmtTy *S) {
173 delete static_cast<Stmt*>(S);
174}
175
Chris Lattner9299f3f2008-08-23 03:19:52 +0000176/// ActOnEndOfTranslationUnit - This is called at the very end of the
177/// translation unit when EOF is reached and all but the top-level scope is
178/// popped.
179void Sema::ActOnEndOfTranslationUnit() {
180
181}
182
183
Reid Spencer5f016e22007-07-11 17:01:13 +0000184//===----------------------------------------------------------------------===//
185// Helper functions.
186//===----------------------------------------------------------------------===//
187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188const LangOptions &Sema::getLangOptions() const {
189 return PP.getLangOptions();
190}
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000191
192ObjCMethodDecl *Sema::getCurMethodDecl() {
Steve Naroffd7612e12008-11-17 16:28:52 +0000193 DeclContext *DC = CurContext;
194 while (isa<BlockDecl>(DC))
195 DC = DC->getParent();
196 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000197}