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