Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 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" |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 26 | /// \brief Convert the given type to a string suitable for printing as part of |
| 27 | /// a diagnostic. |
| 28 | /// |
| 29 | /// \param Context the context in which the type was allocated |
| 30 | /// \param Ty the type to print |
| 31 | static std::string ConvertTypeToDiagnosticString(ASTContext &Context, |
| 32 | QualType Ty) { |
| 33 | // FIXME: Playing with std::string is really slow. |
| 34 | std::string S = Ty.getAsString(Context.PrintingPolicy); |
| 35 | |
| 36 | // If this is a sugared type (like a typedef, typeof, etc), then unwrap one |
| 37 | // level of the sugar so that the type is more obvious to the user. |
| 38 | QualType DesugaredTy = Ty.getDesugaredType(); |
| 39 | |
| 40 | if (Ty != DesugaredTy && |
| 41 | // If the desugared type is a vector type, we don't want to expand it, |
| 42 | // it will turn into an attribute mess. People want their "vec4". |
| 43 | !isa<VectorType>(DesugaredTy) && |
| 44 | |
| 45 | // Don't aka just because we saw an elaborated type... |
| 46 | (!isa<ElaboratedType>(Ty) || |
| 47 | cast<ElaboratedType>(Ty)->desugar() != DesugaredTy) && |
| 48 | |
| 49 | // ...or a qualified name type... |
| 50 | (!isa<QualifiedNameType>(Ty) || |
| 51 | cast<QualifiedNameType>(Ty)->desugar() != DesugaredTy) && |
| 52 | |
| 53 | // ...or a non-dependent template specialization. |
| 54 | (!isa<TemplateSpecializationType>(Ty) || Ty->isDependentType()) && |
| 55 | |
| 56 | // Don't desugar magic Objective-C types. |
| 57 | Ty.getUnqualifiedType() != Context.getObjCIdType() && |
| 58 | Ty.getUnqualifiedType() != Context.getObjCClassType() && |
| 59 | Ty.getUnqualifiedType() != Context.getObjCSelType() && |
| 60 | Ty.getUnqualifiedType() != Context.getObjCProtoType() && |
| 61 | |
| 62 | // Not va_list. |
| 63 | Ty.getUnqualifiedType() != Context.getBuiltinVaListType()) { |
| 64 | S = "'"+S+"' (aka '"; |
| 65 | S += DesugaredTy.getAsString(Context.PrintingPolicy); |
| 66 | S += "')"; |
| 67 | return S; |
| 68 | } |
| 69 | |
| 70 | S = "'" + S + "'"; |
| 71 | return S; |
| 72 | } |
| 73 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | /// ConvertQualTypeToStringFn - This function is used to pretty print the |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 75 | /// specified QualType as a string in diagnostics. |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 76 | static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val, |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 77 | const char *Modifier, unsigned ModLen, |
| 78 | const char *Argument, unsigned ArgLen, |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 79 | llvm::SmallVectorImpl<char> &Output, |
| 80 | void *Cookie) { |
| 81 | ASTContext &Context = *static_cast<ASTContext*>(Cookie); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 83 | std::string S; |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 84 | bool NeedQuotes = true; |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 85 | if (Kind == Diagnostic::ak_qualtype) { |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 86 | assert(ModLen == 0 && ArgLen == 0 && |
| 87 | "Invalid modifier for QualType argument"); |
| 88 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 89 | QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val))); |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 90 | S = ConvertTypeToDiagnosticString(Context, Ty); |
| 91 | NeedQuotes = false; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 92 | } else if (Kind == Diagnostic::ak_declarationname) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 94 | DeclarationName N = DeclarationName::getFromOpaqueInteger(Val); |
| 95 | S = N.getAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 97 | if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0) |
| 98 | S = '+' + S; |
| 99 | else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0) |
| 100 | S = '-' + S; |
| 101 | else |
| 102 | assert(ModLen == 0 && ArgLen == 0 && |
| 103 | "Invalid modifier for DeclarationName argument"); |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 104 | } else if (Kind == Diagnostic::ak_nameddecl) { |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 105 | bool Qualified; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 106 | if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0) |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 107 | Qualified = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | else { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 109 | assert(ModLen == 0 && ArgLen == 0 && |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 110 | "Invalid modifier for NamedDecl* argument"); |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 111 | Qualified = false; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 112 | } |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 113 | reinterpret_cast<NamedDecl*>(Val)->getNameForDiagnostic(S, |
| 114 | Context.PrintingPolicy, |
| 115 | Qualified); |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 116 | } else if (Kind == Diagnostic::ak_nestednamespec) { |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 117 | llvm::raw_string_ostream OS(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | reinterpret_cast<NestedNameSpecifier*> (Val)->print(OS, |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 119 | Context.PrintingPolicy); |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 120 | } else { |
| 121 | assert(Kind == Diagnostic::ak_declcontext); |
| 122 | DeclContext *DC = reinterpret_cast<DeclContext *> (Val); |
| 123 | NeedQuotes = false; |
| 124 | if (!DC) { |
| 125 | assert(false && "Should never have a null declaration context"); |
| 126 | S = "unknown context"; |
| 127 | } else if (DC->isTranslationUnit()) { |
| 128 | // FIXME: Get these strings from some localized place |
| 129 | if (Context.getLangOptions().CPlusPlus) |
| 130 | S = "the global namespace"; |
| 131 | else |
| 132 | S = "the global scope"; |
| 133 | } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) { |
| 134 | S = ConvertTypeToDiagnosticString(Context, Context.getTypeDeclType(Type)); |
| 135 | NeedQuotes = false; |
| 136 | } else { |
| 137 | // FIXME: Get these strings from some localized place |
| 138 | NamedDecl *ND = cast<NamedDecl>(DC); |
| 139 | if (isa<NamespaceDecl>(ND)) |
| 140 | S += "namespace "; |
| 141 | else if (isa<ObjCMethodDecl>(ND)) |
| 142 | S += "method "; |
| 143 | else if (isa<FunctionDecl>(ND)) |
| 144 | S += "function "; |
| 145 | |
| 146 | S += "'"; |
| 147 | ND->getNameForDiagnostic(S, Context.PrintingPolicy, true); |
| 148 | S += "'"; |
| 149 | NeedQuotes = false; |
| 150 | } |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 153 | if (NeedQuotes) |
| 154 | Output.push_back('\''); |
| 155 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 156 | Output.append(S.begin(), S.end()); |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 157 | |
| 158 | if (NeedQuotes) |
| 159 | Output.push_back('\''); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 163 | static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) { |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 164 | if (C.getLangOptions().CPlusPlus) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | return CXXRecordDecl::Create(C, TagDecl::TK_struct, |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 166 | C.getTranslationUnitDecl(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 167 | SourceLocation(), &C.Idents.get(Name)); |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 168 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | return RecordDecl::Create(C, TagDecl::TK_struct, |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 170 | C.getTranslationUnitDecl(), |
| 171 | SourceLocation(), &C.Idents.get(Name)); |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 174 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 175 | TUScope = S; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 176 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 178 | if (PP.getTargetInfo().getPointerWidth(0) >= 64) { |
| 179 | // Install [u]int128_t for 64-bit targets. |
| 180 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 181 | SourceLocation(), |
| 182 | &Context.Idents.get("__int128_t"), |
| 183 | Context.Int128Ty), TUScope); |
| 184 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 185 | SourceLocation(), |
| 186 | &Context.Idents.get("__uint128_t"), |
| 187 | Context.UnsignedInt128Ty), TUScope); |
| 188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | |
| 190 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 191 | if (!PP.getLangOptions().ObjC1) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Steve Naroff | cb83c53 | 2009-06-16 00:20:10 +0000 | [diff] [blame] | 193 | // Built-in ObjC types may already be set by PCHReader (hence isNull checks). |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 194 | if (Context.getObjCSelType().isNull()) { |
| 195 | // Synthesize "typedef struct objc_selector *SEL;" |
| 196 | RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector"); |
| 197 | PushOnScopeChains(SelTag, TUScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 199 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
| 200 | TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, |
| 201 | SourceLocation(), |
| 202 | &Context.Idents.get("SEL"), |
| 203 | SelT); |
| 204 | PushOnScopeChains(SelTypedef, TUScope); |
| 205 | Context.setObjCSelType(Context.getTypeDeclType(SelTypedef)); |
| 206 | } |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 208 | // Synthesize "@class Protocol; |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 209 | if (Context.getObjCProtoType().isNull()) { |
| 210 | ObjCInterfaceDecl *ProtocolDecl = |
| 211 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | &Context.Idents.get("Protocol"), |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 213 | SourceLocation(), true); |
| 214 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
| 215 | PushOnScopeChains(ProtocolDecl, TUScope); |
| 216 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 217 | // Create the built-in typedef for 'id'. |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 218 | if (Context.getObjCIdType().isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | TypedefDecl *IdTypedef = |
| 220 | TypedefDecl::Create( |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 221 | Context, CurContext, SourceLocation(), &Context.Idents.get("id"), |
| 222 | Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy) |
| 223 | ); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 224 | PushOnScopeChains(IdTypedef, TUScope); |
| 225 | Context.setObjCIdType(Context.getTypeDeclType(IdTypedef)); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 226 | Context.ObjCIdRedefinitionType = Context.getObjCIdType(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 227 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 228 | // Create the built-in typedef for 'Class'. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 229 | if (Context.getObjCClassType().isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | TypedefDecl *ClassTypedef = |
| 231 | TypedefDecl::Create( |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 232 | Context, CurContext, SourceLocation(), &Context.Idents.get("Class"), |
| 233 | Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy) |
| 234 | ); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 235 | PushOnScopeChains(ClassTypedef, TUScope); |
| 236 | Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef)); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 237 | Context.ObjCClassRedefinitionType = Context.getObjCClassType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 238 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 241 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, |
| 242 | bool CompleteTranslationUnit) |
Chris Lattner | 53ebff3 | 2009-01-22 19:21:44 +0000 | [diff] [blame] | 243 | : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()), |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 245 | ExternalSource(0), CodeCompleter(0), CurContext(0), |
| 246 | PreDeclaratorDC(0), CurBlock(0), PackContext(0), |
| 247 | IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0), |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 248 | GlobalNewDeleteDeclared(false), ExprEvalContext(PotentiallyEvaluated), |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 249 | CompleteTranslationUnit(CompleteTranslationUnit), |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 250 | NumSFINAEErrors(0), CurrentInstantiationScope(0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 252 | TUScope = 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 253 | if (getLangOptions().CPlusPlus) |
| 254 | FieldCollector.reset(new CXXFieldCollector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 256 | // Tell diagnostics how to render things from the AST library. |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 257 | PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 261 | /// If there is already an implicit cast, merge into the existing one. |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 262 | /// If isLvalue, the result of the cast is an lvalue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, |
Anders Carlsson | c0a2fd8 | 2009-09-15 05:13:45 +0000 | [diff] [blame] | 264 | CastExpr::CastKind Kind, bool isLvalue) { |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 265 | QualType ExprTy = Context.getCanonicalType(Expr->getType()); |
| 266 | QualType TypeTy = Context.getCanonicalType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 268 | if (ExprTy == TypeTy) |
| 269 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 271 | if (Expr->getType().getTypePtr()->isPointerType() && |
| 272 | Ty.getTypePtr()->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | QualType ExprBaseType = |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 274 | cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType(); |
| 275 | QualType BaseType = |
| 276 | cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType(); |
| 277 | if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 278 | Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast) |
| 279 | << Expr->getSourceRange(); |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 283 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
Anders Carlsson | 4c5fad3 | 2009-09-15 05:28:24 +0000 | [diff] [blame] | 284 | if (ImpCast->getCastKind() == Kind) { |
| 285 | ImpCast->setType(Ty); |
| 286 | ImpCast->setLvalueCast(isLvalue); |
| 287 | return; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 294 | void Sema::DeleteExpr(ExprTy *E) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 295 | if (E) static_cast<Expr*>(E)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 296 | } |
| 297 | void Sema::DeleteStmt(StmtTy *S) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 298 | if (S) static_cast<Stmt*>(S)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 301 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 302 | /// translation unit when EOF is reached and all but the top-level scope is |
| 303 | /// popped. |
| 304 | void Sema::ActOnEndOfTranslationUnit() { |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 305 | // C++: Perform implicit template instantiations. |
| 306 | // |
| 307 | // FIXME: When we perform these implicit instantiations, we do not carefully |
| 308 | // keep track of the point of instantiation (C++ [temp.point]). This means |
| 309 | // that name lookup that occurs within the template instantiation will |
| 310 | // always happen at the end of the translation unit, so it will find |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | // some names that should not be found. Although this is common behavior |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 312 | // for C++ compilers, it is technically wrong. In the future, we either need |
| 313 | // to be able to filter the results of name lookup or we need to perform |
| 314 | // template instantiations earlier. |
| 315 | PerformPendingImplicitInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 317 | // Check for #pragma weak identifiers that were never declared |
| 318 | // FIXME: This will cause diagnostics to be emitted in a non-determinstic |
| 319 | // order! Iterating over a densemap like this is bad. |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 320 | for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 321 | I = WeakUndeclaredIdentifiers.begin(), |
| 322 | E = WeakUndeclaredIdentifiers.end(); I != E; ++I) { |
| 323 | if (I->second.getUsed()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 325 | Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared) |
| 326 | << I->first; |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 329 | if (!CompleteTranslationUnit) |
| 330 | return; |
| 331 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 332 | // C99 6.9.2p2: |
| 333 | // A declaration of an identifier for an object that has file |
| 334 | // scope without an initializer, and without a storage-class |
| 335 | // specifier or with the storage-class specifier static, |
| 336 | // constitutes a tentative definition. If a translation unit |
| 337 | // contains one or more tentative definitions for an identifier, |
| 338 | // and the translation unit contains no external definition for |
| 339 | // that identifier, then the behavior is exactly as if the |
| 340 | // translation unit contains a file scope declaration of that |
| 341 | // identifier, with the composite type as of the end of the |
| 342 | // translation unit, with an initializer equal to 0. |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 343 | for (unsigned i = 0, e = TentativeDefinitionList.size(); i != e; ++i) { |
| 344 | VarDecl *VD = TentativeDefinitions.lookup(TentativeDefinitionList[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 346 | // If the tentative definition was completed, it will be in the list, but |
| 347 | // not the map. |
| 348 | if (VD == 0 || VD->isInvalidDecl() || !VD->isTentativeDefinition(Context)) |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 349 | continue; |
| 350 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | if (const IncompleteArrayType *ArrayT |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 352 | = Context.getAsIncompleteArrayType(VD->getType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | if (RequireCompleteType(VD->getLocation(), |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 354 | ArrayT->getElementType(), |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 355 | diag::err_tentative_def_incomplete_type_arr)) { |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 356 | VD->setInvalidDecl(); |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 357 | continue; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 358 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 360 | // Set the length of the array to 1 (C99 6.9.2p5). |
| 361 | Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); |
| 362 | llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | QualType T |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 364 | = Context.getConstantArrayWithoutExprType(ArrayT->getElementType(), |
| 365 | One, ArrayType::Normal, 0); |
| 366 | VD->setType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | } else if (RequireCompleteType(VD->getLocation(), VD->getType(), |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 368 | diag::err_tentative_def_incomplete_type)) |
| 369 | VD->setInvalidDecl(); |
| 370 | |
| 371 | // Notify the consumer that we've completed a tentative definition. |
| 372 | if (!VD->isInvalidDecl()) |
| 373 | Consumer.CompleteTentativeDefinition(VD); |
| 374 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 375 | } |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 379 | //===----------------------------------------------------------------------===// |
| 380 | // Helper functions. |
| 381 | //===----------------------------------------------------------------------===// |
| 382 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 383 | DeclContext *Sema::getFunctionLevelDeclContext() { |
Anders Carlsson | fb7ef75 | 2009-08-08 17:48:49 +0000 | [diff] [blame] | 384 | DeclContext *DC = PreDeclaratorDC ? PreDeclaratorDC : CurContext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 386 | while (isa<BlockDecl>(DC)) |
| 387 | DC = DC->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 389 | return DC; |
| 390 | } |
| 391 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 392 | /// getCurFunctionDecl - If inside of a function body, this returns a pointer |
| 393 | /// to the function decl for the function being parsed. If we're currently |
| 394 | /// in a 'block', this returns the containing context. |
| 395 | FunctionDecl *Sema::getCurFunctionDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 396 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 397 | return dyn_cast<FunctionDecl>(DC); |
| 398 | } |
| 399 | |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 400 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 401 | DeclContext *DC = getFunctionLevelDeclContext(); |
Steve Naroff | d7612e1 | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 402 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 403 | } |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 404 | |
| 405 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 406 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 407 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 408 | return cast<NamedDecl>(DC); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 412 | Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 413 | if (!this->Emit()) |
| 414 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 416 | // If this is not a note, and we're in a template instantiation |
| 417 | // that is different from the last template instantiation where |
| 418 | // we emitted an error, print a template instantiation |
| 419 | // backtrace. |
| 420 | if (!SemaRef.Diags.isBuiltinNote(DiagID) && |
| 421 | !SemaRef.ActiveTemplateInstantiations.empty() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | SemaRef.ActiveTemplateInstantiations.back() |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 423 | != SemaRef.LastTemplateInstantiationErrorContext) { |
| 424 | SemaRef.PrintInstantiationStack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | SemaRef.LastTemplateInstantiationErrorContext |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 426 | = SemaRef.ActiveTemplateInstantiations.back(); |
| 427 | } |
| 428 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 429 | |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 430 | Sema::SemaDiagnosticBuilder |
| 431 | Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) { |
| 432 | SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID())); |
| 433 | PD.Emit(Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 435 | return Builder; |
| 436 | } |
| 437 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 438 | void Sema::ActOnComment(SourceRange Comment) { |
| 439 | Context.Comments.push_back(Comment); |
| 440 | } |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 441 | |