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