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" |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/APFloat.h" |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Preprocessor.h" |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 27 | /// Determines whether we should have an a.k.a. clause when |
Chris Lattner | 0a026af | 2009-10-20 05:36:05 +0000 | [diff] [blame] | 28 | /// pretty-printing a type. There are three main criteria: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 29 | /// |
| 30 | /// 1) Some types provide very minimal sugar that doesn't impede the |
| 31 | /// user's understanding --- for example, elaborated type |
| 32 | /// specifiers. If this is all the sugar we see, we don't want an |
| 33 | /// a.k.a. clause. |
| 34 | /// 2) Some types are technically sugared but are much more familiar |
| 35 | /// when seen in their sugared form --- for example, va_list, |
| 36 | /// vector types, and the magic Objective C types. We don't |
| 37 | /// want to desugar these, even if we do produce an a.k.a. clause. |
Chris Lattner | 0a026af | 2009-10-20 05:36:05 +0000 | [diff] [blame] | 38 | /// 3) Some types may have already been desugared previously in this diagnostic. |
| 39 | /// if this is the case, doing another "aka" would just be clutter. |
| 40 | /// |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 41 | static bool ShouldAKA(ASTContext &Context, QualType QT, |
Chris Lattner | 0a026af | 2009-10-20 05:36:05 +0000 | [diff] [blame] | 42 | const Diagnostic::ArgumentValue *PrevArgs, |
| 43 | unsigned NumPrevArgs, |
| 44 | QualType &DesugaredQT) { |
| 45 | QualType InputTy = QT; |
| 46 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 47 | bool AKA = false; |
| 48 | QualifierCollector Qc; |
| 49 | |
| 50 | while (true) { |
| 51 | const Type *Ty = Qc.strip(QT); |
| 52 | |
| 53 | // Don't aka just because we saw an elaborated type... |
| 54 | if (isa<ElaboratedType>(Ty)) { |
| 55 | QT = cast<ElaboratedType>(Ty)->desugar(); |
| 56 | continue; |
| 57 | } |
| 58 | |
| 59 | // ...or a qualified name type... |
| 60 | if (isa<QualifiedNameType>(Ty)) { |
| 61 | QT = cast<QualifiedNameType>(Ty)->desugar(); |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | // ...or a substituted template type parameter. |
| 66 | if (isa<SubstTemplateTypeParmType>(Ty)) { |
| 67 | QT = cast<SubstTemplateTypeParmType>(Ty)->desugar(); |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | // Don't desugar template specializations. |
| 72 | if (isa<TemplateSpecializationType>(Ty)) |
| 73 | break; |
| 74 | |
| 75 | // Don't desugar magic Objective-C types. |
| 76 | if (QualType(Ty,0) == Context.getObjCIdType() || |
| 77 | QualType(Ty,0) == Context.getObjCClassType() || |
| 78 | QualType(Ty,0) == Context.getObjCSelType() || |
| 79 | QualType(Ty,0) == Context.getObjCProtoType()) |
| 80 | break; |
| 81 | |
| 82 | // Don't desugar va_list. |
| 83 | if (QualType(Ty,0) == Context.getBuiltinVaListType()) |
| 84 | break; |
| 85 | |
| 86 | // Otherwise, do a single-step desugar. |
| 87 | QualType Underlying; |
| 88 | bool IsSugar = false; |
| 89 | switch (Ty->getTypeClass()) { |
| 90 | #define ABSTRACT_TYPE(Class, Base) |
| 91 | #define TYPE(Class, Base) \ |
| 92 | case Type::Class: { \ |
| 93 | const Class##Type *CTy = cast<Class##Type>(Ty); \ |
| 94 | if (CTy->isSugared()) { \ |
| 95 | IsSugar = true; \ |
| 96 | Underlying = CTy->desugar(); \ |
| 97 | } \ |
| 98 | break; \ |
| 99 | } |
| 100 | #include "clang/AST/TypeNodes.def" |
| 101 | } |
| 102 | |
| 103 | // If it wasn't sugared, we're done. |
| 104 | if (!IsSugar) |
| 105 | break; |
| 106 | |
| 107 | // If the desugared type is a vector type, we don't want to expand |
| 108 | // it, it will turn into an attribute mess. People want their "vec4". |
| 109 | if (isa<VectorType>(Underlying)) |
| 110 | break; |
| 111 | |
| 112 | // Otherwise, we're tearing through something opaque; note that |
| 113 | // we'll eventually need an a.k.a. clause and keep going. |
| 114 | AKA = true; |
| 115 | QT = Underlying; |
| 116 | continue; |
| 117 | } |
| 118 | |
Chris Lattner | 0a026af | 2009-10-20 05:36:05 +0000 | [diff] [blame] | 119 | // If we never tore through opaque sugar, don't print aka. |
| 120 | if (!AKA) return false; |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 0a026af | 2009-10-20 05:36:05 +0000 | [diff] [blame] | 122 | // If we did, check to see if we already desugared this type in this |
| 123 | // diagnostic. If so, don't do it again. |
| 124 | for (unsigned i = 0; i != NumPrevArgs; ++i) { |
| 125 | // TODO: Handle ak_declcontext case. |
| 126 | if (PrevArgs[i].first == Diagnostic::ak_qualtype) { |
| 127 | void *Ptr = (void*)PrevArgs[i].second; |
| 128 | QualType PrevTy(QualType::getFromOpaquePtr(Ptr)); |
| 129 | if (PrevTy == InputTy) |
| 130 | return false; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | DesugaredQT = Qc.apply(QT); |
| 135 | return true; |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 138 | /// \brief Convert the given type to a string suitable for printing as part of |
| 139 | /// a diagnostic. |
| 140 | /// |
| 141 | /// \param Context the context in which the type was allocated |
| 142 | /// \param Ty the type to print |
Chris Lattner | 0a026af | 2009-10-20 05:36:05 +0000 | [diff] [blame] | 143 | static std::string |
| 144 | ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty, |
| 145 | const Diagnostic::ArgumentValue *PrevArgs, |
| 146 | unsigned NumPrevArgs) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 147 | // FIXME: Playing with std::string is really slow. |
| 148 | std::string S = Ty.getAsString(Context.PrintingPolicy); |
| 149 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 150 | // Consider producing an a.k.a. clause if removing all the direct |
| 151 | // sugar gives us something "significantly different". |
| 152 | |
| 153 | QualType DesugaredTy; |
Chris Lattner | 0a026af | 2009-10-20 05:36:05 +0000 | [diff] [blame] | 154 | if (ShouldAKA(Context, Ty, PrevArgs, NumPrevArgs, DesugaredTy)) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 155 | S = "'"+S+"' (aka '"; |
| 156 | S += DesugaredTy.getAsString(Context.PrintingPolicy); |
| 157 | S += "')"; |
| 158 | return S; |
| 159 | } |
| 160 | |
| 161 | S = "'" + S + "'"; |
| 162 | return S; |
| 163 | } |
| 164 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | /// ConvertQualTypeToStringFn - This function is used to pretty print the |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 166 | /// specified QualType as a string in diagnostics. |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 167 | static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val, |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 168 | const char *Modifier, unsigned ModLen, |
| 169 | const char *Argument, unsigned ArgLen, |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 170 | const Diagnostic::ArgumentValue *PrevArgs, |
| 171 | unsigned NumPrevArgs, |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 172 | llvm::SmallVectorImpl<char> &Output, |
| 173 | void *Cookie) { |
| 174 | ASTContext &Context = *static_cast<ASTContext*>(Cookie); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 176 | std::string S; |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 177 | bool NeedQuotes = true; |
Chris Lattner | 9cf9f86 | 2009-10-20 05:12:36 +0000 | [diff] [blame] | 178 | |
| 179 | switch (Kind) { |
| 180 | default: assert(0 && "unknown ArgumentKind"); |
| 181 | case Diagnostic::ak_qualtype: { |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 182 | assert(ModLen == 0 && ArgLen == 0 && |
| 183 | "Invalid modifier for QualType argument"); |
| 184 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 185 | QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val))); |
Chris Lattner | 0a026af | 2009-10-20 05:36:05 +0000 | [diff] [blame] | 186 | S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs); |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 187 | NeedQuotes = false; |
Chris Lattner | 9cf9f86 | 2009-10-20 05:12:36 +0000 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | case Diagnostic::ak_declarationname: { |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 191 | DeclarationName N = DeclarationName::getFromOpaqueInteger(Val); |
| 192 | S = N.getAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 194 | if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0) |
| 195 | S = '+' + S; |
| 196 | else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0) |
| 197 | S = '-' + S; |
| 198 | else |
| 199 | assert(ModLen == 0 && ArgLen == 0 && |
| 200 | "Invalid modifier for DeclarationName argument"); |
Chris Lattner | 9cf9f86 | 2009-10-20 05:12:36 +0000 | [diff] [blame] | 201 | break; |
| 202 | } |
| 203 | case Diagnostic::ak_nameddecl: { |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 204 | bool Qualified; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 205 | if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0) |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 206 | Qualified = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | else { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 208 | assert(ModLen == 0 && ArgLen == 0 && |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 209 | "Invalid modifier for NamedDecl* argument"); |
John McCall | 136a698 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 210 | Qualified = false; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 211 | } |
Chris Lattner | 9cf9f86 | 2009-10-20 05:12:36 +0000 | [diff] [blame] | 212 | reinterpret_cast<NamedDecl*>(Val)-> |
| 213 | getNameForDiagnostic(S, Context.PrintingPolicy, Qualified); |
| 214 | break; |
| 215 | } |
| 216 | case Diagnostic::ak_nestednamespec: { |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 217 | llvm::raw_string_ostream OS(S); |
Chris Lattner | 9cf9f86 | 2009-10-20 05:12:36 +0000 | [diff] [blame] | 218 | reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS, |
| 219 | Context.PrintingPolicy); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 220 | NeedQuotes = false; |
Chris Lattner | 9cf9f86 | 2009-10-20 05:12:36 +0000 | [diff] [blame] | 221 | break; |
| 222 | } |
| 223 | case Diagnostic::ak_declcontext: { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 224 | DeclContext *DC = reinterpret_cast<DeclContext *> (Val); |
Chris Lattner | 9cf9f86 | 2009-10-20 05:12:36 +0000 | [diff] [blame] | 225 | assert(DC && "Should never have a null declaration context"); |
| 226 | |
| 227 | if (DC->isTranslationUnit()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 228 | // FIXME: Get these strings from some localized place |
| 229 | if (Context.getLangOptions().CPlusPlus) |
| 230 | S = "the global namespace"; |
| 231 | else |
| 232 | S = "the global scope"; |
| 233 | } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) { |
Chris Lattner | 0a026af | 2009-10-20 05:36:05 +0000 | [diff] [blame] | 234 | S = ConvertTypeToDiagnosticString(Context, Context.getTypeDeclType(Type), |
| 235 | PrevArgs, NumPrevArgs); |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 236 | } else { |
| 237 | // FIXME: Get these strings from some localized place |
| 238 | NamedDecl *ND = cast<NamedDecl>(DC); |
| 239 | if (isa<NamespaceDecl>(ND)) |
| 240 | S += "namespace "; |
| 241 | else if (isa<ObjCMethodDecl>(ND)) |
| 242 | S += "method "; |
| 243 | else if (isa<FunctionDecl>(ND)) |
| 244 | S += "function "; |
| 245 | |
| 246 | S += "'"; |
| 247 | ND->getNameForDiagnostic(S, Context.PrintingPolicy, true); |
| 248 | S += "'"; |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 249 | } |
Chris Lattner | 9cf9f86 | 2009-10-20 05:12:36 +0000 | [diff] [blame] | 250 | NeedQuotes = false; |
| 251 | break; |
| 252 | } |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 253 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 255 | if (NeedQuotes) |
| 256 | Output.push_back('\''); |
| 257 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 258 | Output.append(S.begin(), S.end()); |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 259 | |
| 260 | if (NeedQuotes) |
| 261 | Output.push_back('\''); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 265 | static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) { |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 266 | if (C.getLangOptions().CPlusPlus) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | return CXXRecordDecl::Create(C, TagDecl::TK_struct, |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 268 | C.getTranslationUnitDecl(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 269 | SourceLocation(), &C.Idents.get(Name)); |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 270 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | return RecordDecl::Create(C, TagDecl::TK_struct, |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 272 | C.getTranslationUnitDecl(), |
| 273 | SourceLocation(), &C.Idents.get(Name)); |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 276 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 277 | TUScope = S; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 278 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 280 | if (PP.getTargetInfo().getPointerWidth(0) >= 64) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 281 | DeclaratorInfo *DInfo; |
| 282 | |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 283 | // Install [u]int128_t for 64-bit targets. |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 284 | DInfo = Context.getTrivialDeclaratorInfo(Context.Int128Ty); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 285 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 286 | SourceLocation(), |
| 287 | &Context.Idents.get("__int128_t"), |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 288 | DInfo), TUScope); |
| 289 | |
| 290 | DInfo = Context.getTrivialDeclaratorInfo(Context.UnsignedInt128Ty); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 291 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 292 | SourceLocation(), |
| 293 | &Context.Idents.get("__uint128_t"), |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 294 | DInfo), TUScope); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 295 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
| 297 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 298 | if (!PP.getLangOptions().ObjC1) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | |
Steve Naroff | cb83c53 | 2009-06-16 00:20:10 +0000 | [diff] [blame] | 300 | // 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] | 301 | if (Context.getObjCSelType().isNull()) { |
| 302 | // Synthesize "typedef struct objc_selector *SEL;" |
| 303 | RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector"); |
| 304 | PushOnScopeChains(SelTag, TUScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 306 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 307 | DeclaratorInfo *SelInfo = Context.getTrivialDeclaratorInfo(SelT); |
| 308 | TypedefDecl *SelTypedef |
| 309 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 310 | &Context.Idents.get("SEL"), SelInfo); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 311 | PushOnScopeChains(SelTypedef, TUScope); |
| 312 | Context.setObjCSelType(Context.getTypeDeclType(SelTypedef)); |
| 313 | } |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 315 | // Synthesize "@class Protocol; |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 316 | if (Context.getObjCProtoType().isNull()) { |
| 317 | ObjCInterfaceDecl *ProtocolDecl = |
| 318 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | &Context.Idents.get("Protocol"), |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 320 | SourceLocation(), true); |
| 321 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
| 322 | PushOnScopeChains(ProtocolDecl, TUScope); |
| 323 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 324 | // Create the built-in typedef for 'id'. |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 325 | if (Context.getObjCIdType().isNull()) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 326 | QualType IdT = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy); |
| 327 | DeclaratorInfo *IdInfo = Context.getTrivialDeclaratorInfo(IdT); |
| 328 | TypedefDecl *IdTypedef |
| 329 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 330 | &Context.Idents.get("id"), IdInfo); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 331 | PushOnScopeChains(IdTypedef, TUScope); |
| 332 | Context.setObjCIdType(Context.getTypeDeclType(IdTypedef)); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 333 | Context.ObjCIdRedefinitionType = Context.getObjCIdType(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 334 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 335 | // Create the built-in typedef for 'Class'. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 336 | if (Context.getObjCClassType().isNull()) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 337 | QualType ClassType |
| 338 | = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy); |
| 339 | DeclaratorInfo *ClassInfo = Context.getTrivialDeclaratorInfo(ClassType); |
| 340 | TypedefDecl *ClassTypedef |
| 341 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 342 | &Context.Idents.get("Class"), ClassInfo); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 343 | PushOnScopeChains(ClassTypedef, TUScope); |
| 344 | Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef)); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 345 | Context.ObjCClassRedefinitionType = Context.getObjCClassType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 346 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 349 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, |
| 350 | bool CompleteTranslationUnit) |
Chris Lattner | 53ebff3 | 2009-01-22 19:21:44 +0000 | [diff] [blame] | 351 | : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()), |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 353 | ExternalSource(0), CodeCompleter(0), CurContext(0), |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 354 | PreDeclaratorDC(0), CurBlock(0), PackContext(0), ParsingDeclDepth(0), |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 355 | IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0), |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 356 | GlobalNewDeleteDeclared(false), ExprEvalContext(PotentiallyEvaluated), |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 357 | CompleteTranslationUnit(CompleteTranslationUnit), |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 358 | NumSFINAEErrors(0), CurrentInstantiationScope(0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 360 | TUScope = 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 361 | if (getLangOptions().CPlusPlus) |
| 362 | FieldCollector.reset(new CXXFieldCollector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 364 | // Tell diagnostics how to render things from the AST library. |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 365 | PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | } |
| 367 | |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 368 | /// Retrieves the width and signedness of the given integer type, |
| 369 | /// or returns false if it is not an integer type. |
John McCall | e8babd1 | 2009-11-07 08:15:46 +0000 | [diff] [blame^] | 370 | /// |
| 371 | /// \param T must be canonical |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 372 | static bool getIntProperties(ASTContext &C, const Type *T, |
| 373 | unsigned &BitWidth, bool &Signed) { |
John McCall | e8babd1 | 2009-11-07 08:15:46 +0000 | [diff] [blame^] | 374 | assert(T->isCanonicalUnqualified()); |
| 375 | |
| 376 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 377 | T = VT->getElementType().getTypePtr(); |
| 378 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 379 | T = CT->getElementType().getTypePtr(); |
| 380 | |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 381 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) { |
| 382 | if (!BT->isInteger()) return false; |
| 383 | |
| 384 | BitWidth = C.getIntWidth(QualType(T, 0)); |
| 385 | Signed = BT->isSignedInteger(); |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | if (const FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) { |
| 390 | BitWidth = FWIT->getWidth(); |
| 391 | Signed = FWIT->isSigned(); |
| 392 | return true; |
| 393 | } |
| 394 | |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | /// Checks whether the given value will have the same value if it it |
| 399 | /// is truncated to the given width, then extended back to the |
| 400 | /// original width. |
| 401 | static bool IsSameIntAfterCast(const llvm::APSInt &value, |
John McCall | e8babd1 | 2009-11-07 08:15:46 +0000 | [diff] [blame^] | 402 | unsigned TargetWidth) { |
| 403 | unsigned SourceWidth = value.getBitWidth(); |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 404 | llvm::APSInt truncated = value; |
| 405 | truncated.trunc(TargetWidth); |
| 406 | truncated.extend(SourceWidth); |
| 407 | return (truncated == value); |
| 408 | } |
| 409 | |
| 410 | /// Checks whether the given value will have the same value if it |
| 411 | /// is truncated to the given width, then extended back to the original |
| 412 | /// width. |
| 413 | /// |
| 414 | /// The value might be a vector or a complex. |
John McCall | e8babd1 | 2009-11-07 08:15:46 +0000 | [diff] [blame^] | 415 | static bool IsSameIntAfterCast(const APValue &value, unsigned TargetWidth) { |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 416 | if (value.isInt()) |
John McCall | e8babd1 | 2009-11-07 08:15:46 +0000 | [diff] [blame^] | 417 | return IsSameIntAfterCast(value.getInt(), TargetWidth); |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 418 | |
| 419 | if (value.isVector()) { |
| 420 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
John McCall | e8babd1 | 2009-11-07 08:15:46 +0000 | [diff] [blame^] | 421 | if (!IsSameIntAfterCast(value.getVectorElt(i), TargetWidth)) |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 422 | return false; |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | assert(value.isComplexInt()); |
John McCall | e8babd1 | 2009-11-07 08:15:46 +0000 | [diff] [blame^] | 427 | return IsSameIntAfterCast(value.getComplexIntReal(), TargetWidth) && |
| 428 | IsSameIntAfterCast(value.getComplexIntImag(), TargetWidth); |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | |
| 432 | /// Checks whether the given value, which currently has the given |
| 433 | /// source semantics, has the same value when coerced through the |
| 434 | /// target semantics. |
| 435 | static bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 436 | const llvm::fltSemantics &Src, |
| 437 | const llvm::fltSemantics &Tgt) { |
| 438 | llvm::APFloat truncated = value; |
| 439 | |
| 440 | bool ignored; |
| 441 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 442 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 443 | |
| 444 | return truncated.bitwiseIsEqual(value); |
| 445 | } |
| 446 | |
| 447 | /// Checks whether the given value, which currently has the given |
| 448 | /// source semantics, has the same value when coerced through the |
| 449 | /// target semantics. |
| 450 | /// |
| 451 | /// The value might be a vector of floats (or a complex number). |
| 452 | static bool IsSameFloatAfterCast(const APValue &value, |
| 453 | const llvm::fltSemantics &Src, |
| 454 | const llvm::fltSemantics &Tgt) { |
| 455 | if (value.isFloat()) |
| 456 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 457 | |
| 458 | if (value.isVector()) { |
| 459 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 460 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 461 | return false; |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | assert(value.isComplexFloat()); |
| 466 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 467 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 468 | } |
| 469 | |
John McCall | e8babd1 | 2009-11-07 08:15:46 +0000 | [diff] [blame^] | 470 | /// Determines if it's reasonable for the given expression to be truncated |
| 471 | /// down to the given integer width. |
| 472 | /// * Boolean expressions are automatically white-listed. |
| 473 | /// * Arithmetic operations on implicitly-promoted operands of the |
| 474 | /// target width or less are okay --- not because the results are |
| 475 | /// actually guaranteed to fit within the width, but because the |
| 476 | /// user is effectively pretending that the operations are closed |
| 477 | /// within the implicitly-promoted type. |
| 478 | static bool IsExprValueWithinWidth(ASTContext &C, Expr *E, unsigned Width) { |
| 479 | E = E->IgnoreParens(); |
| 480 | |
| 481 | #ifndef NDEBUG |
| 482 | { |
| 483 | const Type *ETy = E->getType()->getCanonicalTypeInternal().getTypePtr(); |
| 484 | unsigned EWidth; |
| 485 | bool ESigned; |
| 486 | |
| 487 | if (!getIntProperties(C, ETy, EWidth, ESigned)) |
| 488 | assert(0 && "expression not of integer type"); |
| 489 | |
| 490 | // The caller should never let this happen. |
| 491 | assert(EWidth > Width && "called on expr whose type is too small"); |
| 492 | } |
| 493 | #endif |
| 494 | |
| 495 | // Strip implicit casts off. |
| 496 | while (isa<ImplicitCastExpr>(E)) { |
| 497 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 498 | |
| 499 | const Type *ETy = E->getType()->getCanonicalTypeInternal().getTypePtr(); |
| 500 | |
| 501 | unsigned EWidth; |
| 502 | bool ESigned; |
| 503 | if (!getIntProperties(C, ETy, EWidth, ESigned)) |
| 504 | return false; |
| 505 | |
| 506 | if (EWidth <= Width) |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 511 | switch (BO->getOpcode()) { |
| 512 | |
| 513 | // Boolean-valued operations are white-listed. |
| 514 | case BinaryOperator::LAnd: |
| 515 | case BinaryOperator::LOr: |
| 516 | case BinaryOperator::LT: |
| 517 | case BinaryOperator::GT: |
| 518 | case BinaryOperator::LE: |
| 519 | case BinaryOperator::GE: |
| 520 | case BinaryOperator::EQ: |
| 521 | case BinaryOperator::NE: |
| 522 | return true; |
| 523 | |
| 524 | // Operations with opaque sources are black-listed. |
| 525 | case BinaryOperator::PtrMemD: |
| 526 | case BinaryOperator::PtrMemI: |
| 527 | return false; |
| 528 | |
| 529 | // Left shift gets black-listed based on a judgement call. |
| 530 | case BinaryOperator::Shl: |
| 531 | return false; |
| 532 | |
| 533 | // Various special cases. |
| 534 | case BinaryOperator::Shr: |
| 535 | return IsExprValueWithinWidth(C, BO->getLHS(), Width); |
| 536 | case BinaryOperator::Comma: |
| 537 | return IsExprValueWithinWidth(C, BO->getRHS(), Width); |
| 538 | case BinaryOperator::Sub: |
| 539 | if (BO->getLHS()->getType()->isPointerType()) |
| 540 | return false; |
| 541 | // fallthrough |
| 542 | |
| 543 | // Any other operator is okay if the operands are |
| 544 | // promoted from expressions of appropriate size. |
| 545 | default: |
| 546 | return IsExprValueWithinWidth(C, BO->getLHS(), Width) && |
| 547 | IsExprValueWithinWidth(C, BO->getRHS(), Width); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 552 | switch (UO->getOpcode()) { |
| 553 | // Boolean-valued operations are white-listed. |
| 554 | case UnaryOperator::LNot: |
| 555 | return true; |
| 556 | |
| 557 | // Operations with opaque sources are black-listed. |
| 558 | case UnaryOperator::Deref: |
| 559 | case UnaryOperator::AddrOf: // should be impossible |
| 560 | return false; |
| 561 | |
| 562 | case UnaryOperator::OffsetOf: |
| 563 | return false; |
| 564 | |
| 565 | default: |
| 566 | return IsExprValueWithinWidth(C, UO->getSubExpr(), Width); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | // Don't diagnose if the expression is an integer constant |
| 571 | // whose value in the target type is the same as it was |
| 572 | // in the original type. |
| 573 | Expr::EvalResult result; |
| 574 | if (E->Evaluate(result, C)) |
| 575 | if (IsSameIntAfterCast(result.Val, Width)) |
| 576 | return true; |
| 577 | |
| 578 | return false; |
| 579 | } |
| 580 | |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 581 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
| 582 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, unsigned diag) { |
| 583 | S.Diag(E->getExprLoc(), diag) << E->getType() << T << E->getSourceRange(); |
| 584 | } |
| 585 | |
| 586 | /// Implements -Wconversion. |
| 587 | static void CheckImplicitConversion(Sema &S, Expr *E, QualType T) { |
| 588 | // Don't diagnose in unevaluated contexts. |
| 589 | if (S.ExprEvalContext == Sema::Unevaluated) |
| 590 | return; |
| 591 | |
| 592 | // Don't diagnose for value-dependent expressions. |
| 593 | if (E->isValueDependent()) |
| 594 | return; |
| 595 | |
| 596 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
| 597 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
| 598 | |
| 599 | // Never diagnose implicit casts to bool. |
| 600 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) |
| 601 | return; |
| 602 | |
| 603 | // Strip vector types. |
| 604 | if (isa<VectorType>(Source)) { |
| 605 | if (!isa<VectorType>(Target)) |
| 606 | return DiagnoseImpCast(S, E, T, diag::warn_impcast_vector_scalar); |
| 607 | |
| 608 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 609 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 610 | } |
| 611 | |
| 612 | // Strip complex types. |
| 613 | if (isa<ComplexType>(Source)) { |
| 614 | if (!isa<ComplexType>(Target)) |
| 615 | return DiagnoseImpCast(S, E, T, diag::warn_impcast_complex_scalar); |
| 616 | |
| 617 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 618 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 619 | } |
| 620 | |
| 621 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 622 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 623 | |
| 624 | // If the source is floating point... |
| 625 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 626 | // ...and the target is floating point... |
| 627 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 628 | // ...then warn if we're dropping FP rank. |
| 629 | |
| 630 | // Builtin FP kinds are ordered by increasing FP rank. |
| 631 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 632 | // Don't warn about float constants that are precisely |
| 633 | // representable in the target type. |
| 634 | Expr::EvalResult result; |
| 635 | if (E->Evaluate(result, S.Context)) { |
| 636 | // Value might be a float, a float vector, or a float complex. |
| 637 | if (IsSameFloatAfterCast(result.Val, |
| 638 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 639 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | DiagnoseImpCast(S, E, T, diag::warn_impcast_float_precision); |
| 644 | } |
| 645 | return; |
| 646 | } |
| 647 | |
| 648 | // If the target is integral, always warn. |
| 649 | if ((TargetBT && TargetBT->isInteger()) || |
| 650 | isa<FixedWidthIntType>(Target)) |
| 651 | // TODO: don't warn for integer values? |
| 652 | return DiagnoseImpCast(S, E, T, diag::warn_impcast_float_integer); |
| 653 | |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | unsigned SourceWidth, TargetWidth; |
| 658 | bool SourceSigned, TargetSigned; |
| 659 | |
| 660 | if (!getIntProperties(S.Context, Source, SourceWidth, SourceSigned) || |
| 661 | !getIntProperties(S.Context, Target, TargetWidth, TargetSigned)) |
| 662 | return; |
| 663 | |
| 664 | if (SourceWidth > TargetWidth) { |
John McCall | e8babd1 | 2009-11-07 08:15:46 +0000 | [diff] [blame^] | 665 | if (IsExprValueWithinWidth(S.Context, E, TargetWidth)) |
| 666 | return; |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 667 | |
| 668 | return DiagnoseImpCast(S, E, T, diag::warn_impcast_integer_precision); |
| 669 | } |
| 670 | |
| 671 | return; |
| 672 | } |
| 673 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 674 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 675 | /// If there is already an implicit cast, merge into the existing one. |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 676 | /// If isLvalue, the result of the cast is an lvalue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 677 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, |
Anders Carlsson | c0a2fd8 | 2009-09-15 05:13:45 +0000 | [diff] [blame] | 678 | CastExpr::CastKind Kind, bool isLvalue) { |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 679 | QualType ExprTy = Context.getCanonicalType(Expr->getType()); |
| 680 | QualType TypeTy = Context.getCanonicalType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 682 | if (ExprTy == TypeTy) |
| 683 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 685 | if (Expr->getType()->isPointerType() && Ty->isPointerType()) { |
| 686 | QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType(); |
| 687 | QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType(); |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 688 | if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 689 | Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast) |
| 690 | << Expr->getSourceRange(); |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 691 | } |
| 692 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 694 | CheckImplicitConversion(*this, Expr, Ty); |
| 695 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 696 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
Anders Carlsson | 4c5fad3 | 2009-09-15 05:28:24 +0000 | [diff] [blame] | 697 | if (ImpCast->getCastKind() == Kind) { |
| 698 | ImpCast->setType(Ty); |
| 699 | ImpCast->setLvalueCast(isLvalue); |
| 700 | return; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 707 | void Sema::DeleteExpr(ExprTy *E) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 708 | if (E) static_cast<Expr*>(E)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 709 | } |
| 710 | void Sema::DeleteStmt(StmtTy *S) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 711 | if (S) static_cast<Stmt*>(S)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 714 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 715 | /// translation unit when EOF is reached and all but the top-level scope is |
| 716 | /// popped. |
| 717 | void Sema::ActOnEndOfTranslationUnit() { |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 718 | // C++: Perform implicit template instantiations. |
| 719 | // |
| 720 | // FIXME: When we perform these implicit instantiations, we do not carefully |
| 721 | // keep track of the point of instantiation (C++ [temp.point]). This means |
| 722 | // that name lookup that occurs within the template instantiation will |
| 723 | // 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] | 724 | // some names that should not be found. Although this is common behavior |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 725 | // for C++ compilers, it is technically wrong. In the future, we either need |
| 726 | // to be able to filter the results of name lookup or we need to perform |
| 727 | // template instantiations earlier. |
| 728 | PerformPendingImplicitInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 730 | // Check for #pragma weak identifiers that were never declared |
| 731 | // FIXME: This will cause diagnostics to be emitted in a non-determinstic |
| 732 | // order! Iterating over a densemap like this is bad. |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 733 | for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 734 | I = WeakUndeclaredIdentifiers.begin(), |
| 735 | E = WeakUndeclaredIdentifiers.end(); I != E; ++I) { |
| 736 | if (I->second.getUsed()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 738 | Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared) |
| 739 | << I->first; |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 742 | if (!CompleteTranslationUnit) |
| 743 | return; |
| 744 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 745 | // C99 6.9.2p2: |
| 746 | // A declaration of an identifier for an object that has file |
| 747 | // scope without an initializer, and without a storage-class |
| 748 | // specifier or with the storage-class specifier static, |
| 749 | // constitutes a tentative definition. If a translation unit |
| 750 | // contains one or more tentative definitions for an identifier, |
| 751 | // and the translation unit contains no external definition for |
| 752 | // that identifier, then the behavior is exactly as if the |
| 753 | // translation unit contains a file scope declaration of that |
| 754 | // identifier, with the composite type as of the end of the |
| 755 | // translation unit, with an initializer equal to 0. |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 756 | for (unsigned i = 0, e = TentativeDefinitionList.size(); i != e; ++i) { |
| 757 | VarDecl *VD = TentativeDefinitions.lookup(TentativeDefinitionList[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 759 | // If the tentative definition was completed, it will be in the list, but |
| 760 | // not the map. |
| 761 | if (VD == 0 || VD->isInvalidDecl() || !VD->isTentativeDefinition(Context)) |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 762 | continue; |
| 763 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | if (const IncompleteArrayType *ArrayT |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 765 | = Context.getAsIncompleteArrayType(VD->getType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | if (RequireCompleteType(VD->getLocation(), |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 767 | ArrayT->getElementType(), |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 768 | diag::err_tentative_def_incomplete_type_arr)) { |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 769 | VD->setInvalidDecl(); |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 770 | continue; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 771 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 773 | // Set the length of the array to 1 (C99 6.9.2p5). |
| 774 | Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); |
| 775 | llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 776 | QualType T = Context.getConstantArrayType(ArrayT->getElementType(), |
| 777 | One, ArrayType::Normal, 0); |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 778 | VD->setType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 779 | } else if (RequireCompleteType(VD->getLocation(), VD->getType(), |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 780 | diag::err_tentative_def_incomplete_type)) |
| 781 | VD->setInvalidDecl(); |
| 782 | |
| 783 | // Notify the consumer that we've completed a tentative definition. |
| 784 | if (!VD->isInvalidDecl()) |
| 785 | Consumer.CompleteTentativeDefinition(VD); |
| 786 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 787 | } |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 791 | //===----------------------------------------------------------------------===// |
| 792 | // Helper functions. |
| 793 | //===----------------------------------------------------------------------===// |
| 794 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 795 | DeclContext *Sema::getFunctionLevelDeclContext() { |
Anders Carlsson | fb7ef75 | 2009-08-08 17:48:49 +0000 | [diff] [blame] | 796 | DeclContext *DC = PreDeclaratorDC ? PreDeclaratorDC : CurContext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 798 | while (isa<BlockDecl>(DC)) |
| 799 | DC = DC->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 801 | return DC; |
| 802 | } |
| 803 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 804 | /// getCurFunctionDecl - If inside of a function body, this returns a pointer |
| 805 | /// to the function decl for the function being parsed. If we're currently |
| 806 | /// in a 'block', this returns the containing context. |
| 807 | FunctionDecl *Sema::getCurFunctionDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 808 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 809 | return dyn_cast<FunctionDecl>(DC); |
| 810 | } |
| 811 | |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 812 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 813 | DeclContext *DC = getFunctionLevelDeclContext(); |
Steve Naroff | d7612e1 | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 814 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 815 | } |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 816 | |
| 817 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 818 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 819 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 820 | return cast<NamedDecl>(DC); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 824 | Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 825 | if (!this->Emit()) |
| 826 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 828 | // If this is not a note, and we're in a template instantiation |
| 829 | // that is different from the last template instantiation where |
| 830 | // we emitted an error, print a template instantiation |
| 831 | // backtrace. |
| 832 | if (!SemaRef.Diags.isBuiltinNote(DiagID) && |
| 833 | !SemaRef.ActiveTemplateInstantiations.empty() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | SemaRef.ActiveTemplateInstantiations.back() |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 835 | != SemaRef.LastTemplateInstantiationErrorContext) { |
| 836 | SemaRef.PrintInstantiationStack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | SemaRef.LastTemplateInstantiationErrorContext |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 838 | = SemaRef.ActiveTemplateInstantiations.back(); |
| 839 | } |
| 840 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 841 | |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 842 | Sema::SemaDiagnosticBuilder |
| 843 | Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) { |
| 844 | SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID())); |
| 845 | PD.Emit(Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 846 | |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 847 | return Builder; |
| 848 | } |
| 849 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 850 | void Sema::ActOnComment(SourceRange Comment) { |
| 851 | Context.Comments.push_back(Comment); |
| 852 | } |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 853 | |