Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
| 16 | #include "clang/Sema/Lookup.h" |
| 17 | #include "clang/Sema/AnalysisBasedWarnings.h" |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Sebastian Redl | 2ac2c72 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 23 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 021ca18 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 27 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 29 | #include "clang/Basic/PartialDiagnostic.h" |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 30 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 32 | #include "clang/Lex/LiteralSupport.h" |
| 33 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 34 | #include "clang/Sema/DeclSpec.h" |
| 35 | #include "clang/Sema/Designator.h" |
| 36 | #include "clang/Sema/Scope.h" |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 37 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 38 | #include "clang/Sema/ParsedTemplate.h" |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 39 | #include "clang/Sema/SemaFixItUtils.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 40 | #include "clang/Sema/Template.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 41 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 42 | using namespace sema; |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 43 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 44 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 45 | /// \brief Determine whether the use of this declaration is valid, and |
| 46 | /// emit any corresponding diagnostics. |
| 47 | /// |
| 48 | /// This routine diagnoses various problems with referencing |
| 49 | /// declarations that can occur when using a declaration. For example, |
| 50 | /// it might warn if a deprecated or unavailable declaration is being |
| 51 | /// used, or produce an error (and return true) if a C++0x deleted |
| 52 | /// function is being used. |
| 53 | /// |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 54 | /// If IgnoreDeprecated is set to true, this should not warn about deprecated |
Chris Lattner | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 55 | /// decls. |
| 56 | /// |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 57 | /// \returns true if there was an error (this declaration cannot be |
| 58 | /// referenced), false otherwise. |
Chris Lattner | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 59 | /// |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 60 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Fariborz Jahanian | dbbdd2f | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 61 | const ObjCInterfaceDecl *UnknownObjCClass) { |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 62 | if (getLangOptions().CPlusPlus && isa<FunctionDecl>(D)) { |
| 63 | // If there were any diagnostics suppressed by template argument deduction, |
| 64 | // emit them now. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 65 | llvm::DenseMap<Decl *, SmallVector<PartialDiagnosticAt, 1> >::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 66 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 67 | if (Pos != SuppressedDiagnostics.end()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 68 | SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 69 | for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) |
| 70 | Diag(Suppressed[I].first, Suppressed[I].second); |
| 71 | |
| 72 | // Clear out the list of suppressed diagnostics, so that we don't emit |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 73 | // them again for this specialization. However, we don't obsolete this |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 74 | // entry from the table, because we want to avoid ever emitting these |
| 75 | // diagnostics again. |
| 76 | Suppressed.clear(); |
| 77 | } |
| 78 | } |
| 79 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 80 | // See if this is an auto-typed variable whose initializer we are parsing. |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 81 | if (ParsingInitForAutoVars.count(D)) { |
| 82 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 83 | << D->getDeclName(); |
| 84 | return true; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 87 | // See if this is a deleted function. |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 88 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 89 | if (FD->isDeleted()) { |
| 90 | Diag(Loc, diag::err_deleted_function_use); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 91 | Diag(D->getLocation(), diag::note_unavailable_here) << 1 << true; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 92 | return true; |
| 93 | } |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 94 | } |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 95 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 96 | // See if this declaration is unavailable or deprecated. |
| 97 | std::string Message; |
| 98 | switch (D->getAvailability(&Message)) { |
| 99 | case AR_Available: |
| 100 | case AR_NotYetIntroduced: |
| 101 | break; |
| 102 | |
| 103 | case AR_Deprecated: |
| 104 | EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass); |
| 105 | break; |
| 106 | |
| 107 | case AR_Unavailable: |
Argyrios Kyrtzidis | c30661f | 2011-06-17 17:28:30 +0000 | [diff] [blame] | 108 | if (cast<Decl>(CurContext)->getAvailability() != AR_Unavailable) { |
| 109 | if (Message.empty()) { |
| 110 | if (!UnknownObjCClass) |
| 111 | Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
| 112 | else |
| 113 | Diag(Loc, diag::warn_unavailable_fwdclass_message) |
| 114 | << D->getDeclName(); |
| 115 | } |
| 116 | else |
| 117 | Diag(Loc, diag::err_unavailable_message) |
| 118 | << D->getDeclName() << Message; |
| 119 | Diag(D->getLocation(), diag::note_unavailable_here) |
| 120 | << isa<FunctionDecl>(D) << false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 121 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | |
Anders Carlsson | 73067a0 | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 125 | // Warn if this is used but marked unused. |
| 126 | if (D->hasAttr<UnusedAttr>()) |
| 127 | Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
| 128 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 129 | return false; |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 132 | /// \brief Retrieve the message suffix that should be added to a |
| 133 | /// diagnostic complaining about the given function being deleted or |
| 134 | /// unavailable. |
| 135 | std::string Sema::getDeletedOrUnavailableSuffix(const FunctionDecl *FD) { |
| 136 | // FIXME: C++0x implicitly-deleted special member functions could be |
| 137 | // detected here so that we could improve diagnostics to say, e.g., |
| 138 | // "base class 'A' had a deleted copy constructor". |
| 139 | if (FD->isDeleted()) |
| 140 | return std::string(); |
| 141 | |
| 142 | std::string Message; |
| 143 | if (FD->getAvailability(&Message)) |
| 144 | return ": " + Message; |
| 145 | |
| 146 | return std::string(); |
| 147 | } |
| 148 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 149 | /// DiagnoseSentinelCalls - This routine checks whether a call or |
| 150 | /// message-send is to a declaration with the sentinel attribute, and |
| 151 | /// if so, it checks that the requirements of the sentinel are |
| 152 | /// satisfied. |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 153 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 154 | Expr **args, unsigned numArgs) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 155 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | if (!attr) |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 157 | return; |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 158 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 159 | // The number of formal parameters of the declaration. |
| 160 | unsigned numFormalParams; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 162 | // The kind of declaration. This is also an index into a %select in |
| 163 | // the diagnostic. |
| 164 | enum CalleeType { CT_Function, CT_Method, CT_Block } calleeType; |
| 165 | |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 166 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 167 | numFormalParams = MD->param_size(); |
| 168 | calleeType = CT_Method; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 169 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 170 | numFormalParams = FD->param_size(); |
| 171 | calleeType = CT_Function; |
| 172 | } else if (isa<VarDecl>(D)) { |
| 173 | QualType type = cast<ValueDecl>(D)->getType(); |
| 174 | const FunctionType *fn = 0; |
| 175 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 176 | fn = ptr->getPointeeType()->getAs<FunctionType>(); |
| 177 | if (!fn) return; |
| 178 | calleeType = CT_Function; |
| 179 | } else if (const BlockPointerType *ptr = type->getAs<BlockPointerType>()) { |
| 180 | fn = ptr->getPointeeType()->castAs<FunctionType>(); |
| 181 | calleeType = CT_Block; |
| 182 | } else { |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 183 | return; |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 184 | } |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 185 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 186 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fn)) { |
| 187 | numFormalParams = proto->getNumArgs(); |
| 188 | } else { |
| 189 | numFormalParams = 0; |
| 190 | } |
| 191 | } else { |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 192 | return; |
| 193 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 194 | |
| 195 | // "nullPos" is the number of formal parameters at the end which |
| 196 | // effectively count as part of the variadic arguments. This is |
| 197 | // useful if you would prefer to not have *any* formal parameters, |
| 198 | // but the language forces you to have at least one. |
| 199 | unsigned nullPos = attr->getNullPos(); |
| 200 | assert((nullPos == 0 || nullPos == 1) && "invalid null position on sentinel"); |
| 201 | numFormalParams = (nullPos > numFormalParams ? 0 : numFormalParams - nullPos); |
| 202 | |
| 203 | // The number of arguments which should follow the sentinel. |
| 204 | unsigned numArgsAfterSentinel = attr->getSentinel(); |
| 205 | |
| 206 | // If there aren't enough arguments for all the formal parameters, |
| 207 | // the sentinel, and the args after the sentinel, complain. |
| 208 | if (numArgs < numFormalParams + numArgsAfterSentinel + 1) { |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 209 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 210 | Diag(D->getLocation(), diag::note_sentinel_here) << calleeType; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 211 | return; |
| 212 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 213 | |
| 214 | // Otherwise, find the sentinel expression. |
| 215 | Expr *sentinelExpr = args[numArgs - numArgsAfterSentinel - 1]; |
John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 216 | if (!sentinelExpr) return; |
John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 217 | if (sentinelExpr->isValueDependent()) return; |
Anders Carlsson | e981a8c | 2010-11-05 15:21:33 +0000 | [diff] [blame] | 218 | |
| 219 | // nullptr_t is always treated as null. |
| 220 | if (sentinelExpr->getType()->isNullPtrType()) return; |
| 221 | |
Fariborz Jahanian | c0b0ced | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 222 | if (sentinelExpr->getType()->isAnyPointerType() && |
John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 223 | sentinelExpr->IgnoreParenCasts()->isNullPointerConstant(Context, |
| 224 | Expr::NPC_ValueDependentIsNull)) |
| 225 | return; |
| 226 | |
| 227 | // Unfortunately, __null has type 'int'. |
| 228 | if (isa<GNUNullExpr>(sentinelExpr)) return; |
| 229 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 230 | // Pick a reasonable string to insert. Optimistically use 'nil' or |
| 231 | // 'NULL' if those are actually defined in the context. Only use |
| 232 | // 'nil' for ObjC methods, where it's much more likely that the |
| 233 | // variadic arguments form a list of object pointers. |
| 234 | SourceLocation MissingNilLoc |
Douglas Gregor | 5ff4e98 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 235 | = PP.getLocForEndOfToken(sentinelExpr->getLocEnd()); |
| 236 | std::string NullValue; |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 237 | if (calleeType == CT_Method && |
| 238 | PP.getIdentifierInfo("nil")->hasMacroDefinition()) |
Douglas Gregor | 5ff4e98 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 239 | NullValue = "nil"; |
| 240 | else if (PP.getIdentifierInfo("NULL")->hasMacroDefinition()) |
| 241 | NullValue = "NULL"; |
Douglas Gregor | 5ff4e98 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 242 | else |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 243 | NullValue = "(void*) 0"; |
Douglas Gregor | 5ff4e98 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 244 | |
| 245 | Diag(MissingNilLoc, diag::warn_missing_sentinel) |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 246 | << calleeType |
Douglas Gregor | 5ff4e98 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 247 | << FixItHint::CreateInsertion(MissingNilLoc, ", " + NullValue); |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 248 | Diag(D->getLocation(), diag::note_sentinel_here) << calleeType; |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 251 | SourceRange Sema::getExprRange(Expr *E) const { |
| 252 | return E ? E->getSourceRange() : SourceRange(); |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 255 | //===----------------------------------------------------------------------===// |
| 256 | // Standard Promotions and Conversions |
| 257 | //===----------------------------------------------------------------------===// |
| 258 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 259 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 260 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 261 | QualType Ty = E->getType(); |
| 262 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 263 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 264 | if (Ty->isFunctionType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 265 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 266 | CK_FunctionToPointerDecay).take(); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 267 | else if (Ty->isArrayType()) { |
| 268 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 269 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 270 | // type 'array of type' is converted to an expression that has type 'pointer |
| 271 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 272 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 273 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | 9321c74 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 274 | // |
| 275 | // C++ 4.2p1: |
| 276 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 277 | // T" can be converted to an rvalue of type "pointer to T". |
| 278 | // |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 279 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || E->isLValue()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 280 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 281 | CK_ArrayToPointerDecay).take(); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 282 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 283 | return Owned(E); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Argyrios Kyrtzidis | a9b630e | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 286 | static void CheckForNullPointerDereference(Sema &S, Expr *E) { |
| 287 | // Check to see if we are dereferencing a null pointer. If so, |
| 288 | // and if not volatile-qualified, this is undefined behavior that the |
| 289 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 290 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 291 | // only handles the pattern "*null", which is a very syntactic check. |
| 292 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 293 | if (UO->getOpcode() == UO_Deref && |
| 294 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 295 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) && |
| 296 | !UO->getType().isVolatileQualified()) { |
| 297 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 298 | S.PDiag(diag::warn_indirection_through_null) |
| 299 | << UO->getSubExpr()->getSourceRange()); |
| 300 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 301 | S.PDiag(diag::note_indirection_through_null)); |
| 302 | } |
| 303 | } |
| 304 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 305 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 306 | // C++ [conv.lval]p1: |
| 307 | // A glvalue of a non-function, non-array type T can be |
| 308 | // converted to a prvalue. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 309 | if (!E->isGLValue()) return Owned(E); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 310 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 311 | QualType T = E->getType(); |
| 312 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 313 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 314 | // Create a load out of an ObjCProperty l-value, if necessary. |
| 315 | if (E->getObjectKind() == OK_ObjCProperty) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 316 | ExprResult Res = ConvertPropertyForRValue(E); |
| 317 | if (Res.isInvalid()) |
| 318 | return Owned(E); |
| 319 | E = Res.take(); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 320 | if (!E->isGLValue()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 321 | return Owned(E); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 322 | } |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 323 | |
| 324 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 325 | // expressions of certain types in C++. |
| 326 | if (getLangOptions().CPlusPlus && |
| 327 | (E->getType() == Context.OverloadTy || |
| 328 | T->isDependentType() || |
| 329 | T->isRecordType())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 330 | return Owned(E); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 331 | |
| 332 | // The C standard is actually really unclear on this point, and |
| 333 | // DR106 tells us what the result should be but not why. It's |
| 334 | // generally best to say that void types just doesn't undergo |
| 335 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 336 | // 'void' type are never l-values, but qualified void can be. |
| 337 | if (T->isVoidType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 338 | return Owned(E); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 339 | |
Argyrios Kyrtzidis | a9b630e | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 340 | CheckForNullPointerDereference(*this, E); |
| 341 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 342 | // C++ [conv.lval]p1: |
| 343 | // [...] If T is a non-class type, the type of the prvalue is the |
| 344 | // cv-unqualified version of T. Otherwise, the type of the |
| 345 | // rvalue is T. |
| 346 | // |
| 347 | // C99 6.3.2.1p2: |
| 348 | // If the lvalue has qualified type, the value has the unqualified |
| 349 | // version of the type of the lvalue; otherwise, the value has the |
| 350 | // type of the lvalue. |
| 351 | if (T.hasQualifiers()) |
| 352 | T = T.getUnqualifiedType(); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 353 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 354 | return Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 355 | E, 0, VK_RValue)); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 356 | } |
| 357 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 358 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 359 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 360 | if (Res.isInvalid()) |
| 361 | return ExprError(); |
| 362 | Res = DefaultLvalueConversion(Res.take()); |
| 363 | if (Res.isInvalid()) |
| 364 | return ExprError(); |
| 365 | return move(Res); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 369 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 371 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 372 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 373 | /// In these instances, this routine should *not* be called. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 374 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 375 | // First, convert to an r-value. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 376 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 377 | if (Res.isInvalid()) |
| 378 | return Owned(E); |
| 379 | E = Res.take(); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 380 | |
| 381 | QualType Ty = E->getType(); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 382 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 383 | |
| 384 | // Try to perform integral promotions if the object has a theoretically |
| 385 | // promotable type. |
| 386 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 387 | // C99 6.3.1.1p2: |
| 388 | // |
| 389 | // The following may be used in an expression wherever an int or |
| 390 | // unsigned int may be used: |
| 391 | // - an object or expression with an integer type whose integer |
| 392 | // conversion rank is less than or equal to the rank of int |
| 393 | // and unsigned int. |
| 394 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 395 | // |
| 396 | // If an int can represent all values of the original type, the |
| 397 | // value is converted to an int; otherwise, it is converted to an |
| 398 | // unsigned int. These are called the integer promotions. All |
| 399 | // other types are unchanged by the integer promotions. |
| 400 | |
| 401 | QualType PTy = Context.isPromotableBitField(E); |
| 402 | if (!PTy.isNull()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 403 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 404 | return Owned(E); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 405 | } |
| 406 | if (Ty->isPromotableIntegerType()) { |
| 407 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 408 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 409 | return Owned(E); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 410 | } |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 411 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 412 | return Owned(E); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 415 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | /// do not have a prototype. Arguments that have type float are promoted to |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 417 | /// double. All other argument types are converted by UsualUnaryConversions(). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 418 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 419 | QualType Ty = E->getType(); |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 420 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 422 | ExprResult Res = UsualUnaryConversions(E); |
| 423 | if (Res.isInvalid()) |
| 424 | return Owned(E); |
| 425 | E = Res.take(); |
John McCall | 9bc2677 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 427 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 428 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 429 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 430 | |
John McCall | 4bb057d | 2011-08-27 22:06:17 +0000 | [diff] [blame] | 431 | // C++ performs lvalue-to-rvalue conversion as a default argument |
John McCall | 0562caa | 2011-08-29 23:55:37 +0000 | [diff] [blame] | 432 | // promotion, even on class types, but note: |
| 433 | // C++11 [conv.lval]p2: |
| 434 | // When an lvalue-to-rvalue conversion occurs in an unevaluated |
| 435 | // operand or a subexpression thereof the value contained in the |
| 436 | // referenced object is not accessed. Otherwise, if the glvalue |
| 437 | // has a class type, the conversion copy-initializes a temporary |
| 438 | // of type T from the glvalue and the result of the conversion |
| 439 | // is a prvalue for the temporary. |
| 440 | // FIXME: add some way to gate this entire thing for correctness in |
| 441 | // potentially potentially evaluated contexts. |
John McCall | 4bb057d | 2011-08-27 22:06:17 +0000 | [diff] [blame] | 442 | if (getLangOptions().CPlusPlus && E->isGLValue() && |
| 443 | ExprEvalContexts.back().Context != Unevaluated) { |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 444 | ExprResult Temp = PerformCopyInitialization( |
| 445 | InitializedEntity::InitializeTemporary(E->getType()), |
| 446 | E->getExprLoc(), |
| 447 | Owned(E)); |
| 448 | if (Temp.isInvalid()) |
| 449 | return ExprError(); |
| 450 | E = Temp.get(); |
| 451 | } |
| 452 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 453 | return Owned(E); |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 456 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 457 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 458 | /// interfaces passed by value. |
| 459 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 460 | FunctionDecl *FDecl) { |
Douglas Gregor | cbd446d | 2011-06-17 00:15:10 +0000 | [diff] [blame] | 461 | ExprResult ExprRes = CheckPlaceholderExpr(E); |
| 462 | if (ExprRes.isInvalid()) |
| 463 | return ExprError(); |
| 464 | |
| 465 | ExprRes = DefaultArgumentPromotion(E); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 466 | if (ExprRes.isInvalid()) |
| 467 | return ExprError(); |
| 468 | E = ExprRes.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 470 | // Don't allow one to pass an Objective-C interface to a vararg. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 471 | if (E->getType()->isObjCObjectType() && |
Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 472 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 473 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 474 | << E->getType() << CT)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 475 | return ExprError(); |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 476 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 477 | if (!E->getType().isPODType(Context)) { |
Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 478 | // C++0x [expr.call]p7: |
| 479 | // Passing a potentially-evaluated argument of class type (Clause 9) |
| 480 | // having a non-trivial copy constructor, a non-trivial move constructor, |
| 481 | // or a non-trivial destructor, with no corresponding parameter, |
| 482 | // is conditionally-supported with implementation-defined semantics. |
| 483 | bool TrivialEnough = false; |
| 484 | if (getLangOptions().CPlusPlus0x && !E->getType()->isDependentType()) { |
| 485 | if (CXXRecordDecl *Record = E->getType()->getAsCXXRecordDecl()) { |
| 486 | if (Record->hasTrivialCopyConstructor() && |
| 487 | Record->hasTrivialMoveConstructor() && |
| 488 | Record->hasTrivialDestructor()) |
| 489 | TrivialEnough = true; |
| 490 | } |
| 491 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 492 | |
| 493 | if (!TrivialEnough && |
| 494 | getLangOptions().ObjCAutoRefCount && |
| 495 | E->getType()->isObjCLifetimeType()) |
| 496 | TrivialEnough = true; |
Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 497 | |
| 498 | if (TrivialEnough) { |
| 499 | // Nothing to diagnose. This is okay. |
| 500 | } else if (DiagRuntimeBehavior(E->getLocStart(), 0, |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 501 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 502 | << getLangOptions().CPlusPlus0x << E->getType() |
Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 503 | << CT)) { |
| 504 | // Turn this into a trap. |
| 505 | CXXScopeSpec SS; |
| 506 | UnqualifiedId Name; |
| 507 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
| 508 | E->getLocStart()); |
| 509 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, Name, true, false); |
| 510 | if (TrapFn.isInvalid()) |
| 511 | return ExprError(); |
| 512 | |
| 513 | ExprResult Call = ActOnCallExpr(TUScope, TrapFn.get(), E->getLocStart(), |
| 514 | MultiExprArg(), E->getLocEnd()); |
| 515 | if (Call.isInvalid()) |
| 516 | return ExprError(); |
| 517 | |
| 518 | ExprResult Comma = ActOnBinOp(TUScope, E->getLocStart(), tok::comma, |
| 519 | Call.get(), E); |
| 520 | if (Comma.isInvalid()) |
John McCall | 1cd60a2 | 2011-08-26 18:41:18 +0000 | [diff] [blame] | 521 | return ExprError(); |
Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 522 | E = Comma.get(); |
| 523 | } |
Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 524 | } |
| 525 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 526 | return Owned(E); |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 529 | /// \brief Converts an integer to complex float type. Helper function of |
| 530 | /// UsualArithmeticConversions() |
| 531 | /// |
| 532 | /// \return false if the integer expression is an integer type and is |
| 533 | /// successfully converted to the complex type. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 534 | static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr, |
| 535 | ExprResult &ComplexExpr, |
| 536 | QualType IntTy, |
| 537 | QualType ComplexTy, |
| 538 | bool SkipCast) { |
| 539 | if (IntTy->isComplexType() || IntTy->isRealFloatingType()) return true; |
| 540 | if (SkipCast) return false; |
| 541 | if (IntTy->isIntegerType()) { |
| 542 | QualType fpTy = cast<ComplexType>(ComplexTy)->getElementType(); |
| 543 | IntExpr = S.ImpCastExprToType(IntExpr.take(), fpTy, CK_IntegralToFloating); |
| 544 | IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 545 | CK_FloatingRealToComplex); |
| 546 | } else { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 547 | assert(IntTy->isComplexIntegerType()); |
| 548 | IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 549 | CK_IntegralComplexToFloatingComplex); |
| 550 | } |
| 551 | return false; |
| 552 | } |
| 553 | |
| 554 | /// \brief Takes two complex float types and converts them to the same type. |
| 555 | /// Helper function of UsualArithmeticConversions() |
| 556 | static QualType |
Richard Trieu | 5065cdd | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 557 | handleComplexFloatToComplexFloatConverstion(Sema &S, ExprResult &LHS, |
| 558 | ExprResult &RHS, QualType LHSType, |
| 559 | QualType RHSType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 560 | bool IsCompAssign) { |
Richard Trieu | 5065cdd | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 561 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 562 | |
| 563 | if (order < 0) { |
| 564 | // _Complex float -> _Complex double |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 565 | if (!IsCompAssign) |
Richard Trieu | 5065cdd | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 566 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingComplexCast); |
| 567 | return RHSType; |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 568 | } |
| 569 | if (order > 0) |
| 570 | // _Complex float -> _Complex double |
Richard Trieu | 5065cdd | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 571 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingComplexCast); |
| 572 | return LHSType; |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /// \brief Converts otherExpr to complex float and promotes complexExpr if |
| 576 | /// necessary. Helper function of UsualArithmeticConversions() |
| 577 | static QualType handleOtherComplexFloatConversion(Sema &S, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 578 | ExprResult &ComplexExpr, |
| 579 | ExprResult &OtherExpr, |
| 580 | QualType ComplexTy, |
| 581 | QualType OtherTy, |
| 582 | bool ConvertComplexExpr, |
| 583 | bool ConvertOtherExpr) { |
| 584 | int order = S.Context.getFloatingTypeOrder(ComplexTy, OtherTy); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 585 | |
| 586 | // If just the complexExpr is complex, the otherExpr needs to be converted, |
| 587 | // and the complexExpr might need to be promoted. |
| 588 | if (order > 0) { // complexExpr is wider |
| 589 | // float -> _Complex double |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 590 | if (ConvertOtherExpr) { |
| 591 | QualType fp = cast<ComplexType>(ComplexTy)->getElementType(); |
| 592 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), fp, CK_FloatingCast); |
| 593 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), ComplexTy, |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 594 | CK_FloatingRealToComplex); |
| 595 | } |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 596 | return ComplexTy; |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // otherTy is at least as wide. Find its corresponding complex type. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 600 | QualType result = (order == 0 ? ComplexTy : |
| 601 | S.Context.getComplexType(OtherTy)); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 602 | |
| 603 | // double -> _Complex double |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 604 | if (ConvertOtherExpr) |
| 605 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), result, |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 606 | CK_FloatingRealToComplex); |
| 607 | |
| 608 | // _Complex float -> _Complex double |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 609 | if (ConvertComplexExpr && order < 0) |
| 610 | ComplexExpr = S.ImpCastExprToType(ComplexExpr.take(), result, |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 611 | CK_FloatingComplexCast); |
| 612 | |
| 613 | return result; |
| 614 | } |
| 615 | |
| 616 | /// \brief Handle arithmetic conversion with complex types. Helper function of |
| 617 | /// UsualArithmeticConversions() |
Richard Trieu | 5065cdd | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 618 | static QualType handleComplexFloatConversion(Sema &S, ExprResult &LHS, |
| 619 | ExprResult &RHS, QualType LHSType, |
| 620 | QualType RHSType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 621 | bool IsCompAssign) { |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 622 | // if we have an integer operand, the result is the complex type. |
Richard Trieu | 5065cdd | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 623 | if (!handleIntegerToComplexFloatConversion(S, RHS, LHS, RHSType, LHSType, |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 624 | /*skipCast*/false)) |
Richard Trieu | 5065cdd | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 625 | return LHSType; |
| 626 | if (!handleIntegerToComplexFloatConversion(S, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 627 | /*skipCast*/IsCompAssign)) |
Richard Trieu | 5065cdd | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 628 | return RHSType; |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 629 | |
| 630 | // This handles complex/complex, complex/float, or float/complex. |
| 631 | // When both operands are complex, the shorter operand is converted to the |
| 632 | // type of the longer, and that is the type of the result. This corresponds |
| 633 | // to what is done when combining two real floating-point operands. |
| 634 | // The fun begins when size promotion occur across type domains. |
| 635 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 636 | // floating-point type, the less precise type is converted, within it's |
| 637 | // real or complex domain, to the precision of the other type. For example, |
| 638 | // when combining a "long double" with a "double _Complex", the |
| 639 | // "double _Complex" is promoted to "long double _Complex". |
| 640 | |
Richard Trieu | 5065cdd | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 641 | bool LHSComplexFloat = LHSType->isComplexType(); |
| 642 | bool RHSComplexFloat = RHSType->isComplexType(); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 643 | |
| 644 | // If both are complex, just cast to the more precise type. |
| 645 | if (LHSComplexFloat && RHSComplexFloat) |
Richard Trieu | 5065cdd | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 646 | return handleComplexFloatToComplexFloatConverstion(S, LHS, RHS, |
| 647 | LHSType, RHSType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 648 | IsCompAssign); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 649 | |
| 650 | // If only one operand is complex, promote it if necessary and convert the |
| 651 | // other operand to complex. |
| 652 | if (LHSComplexFloat) |
| 653 | return handleOtherComplexFloatConversion( |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 654 | S, LHS, RHS, LHSType, RHSType, /*convertComplexExpr*/!IsCompAssign, |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 655 | /*convertOtherExpr*/ true); |
| 656 | |
| 657 | assert(RHSComplexFloat); |
| 658 | return handleOtherComplexFloatConversion( |
Richard Trieu | 5065cdd | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 659 | S, RHS, LHS, RHSType, LHSType, /*convertComplexExpr*/true, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 660 | /*convertOtherExpr*/ !IsCompAssign); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /// \brief Hande arithmetic conversion from integer to float. Helper function |
| 664 | /// of UsualArithmeticConversions() |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 665 | static QualType handleIntToFloatConversion(Sema &S, ExprResult &FloatExpr, |
| 666 | ExprResult &IntExpr, |
| 667 | QualType FloatTy, QualType IntTy, |
| 668 | bool ConvertFloat, bool ConvertInt) { |
| 669 | if (IntTy->isIntegerType()) { |
| 670 | if (ConvertInt) |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 671 | // Convert intExpr to the lhs floating point type. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 672 | IntExpr = S.ImpCastExprToType(IntExpr.take(), FloatTy, |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 673 | CK_IntegralToFloating); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 674 | return FloatTy; |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | // Convert both sides to the appropriate complex float. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 678 | assert(IntTy->isComplexIntegerType()); |
| 679 | QualType result = S.Context.getComplexType(FloatTy); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 680 | |
| 681 | // _Complex int -> _Complex float |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 682 | if (ConvertInt) |
| 683 | IntExpr = S.ImpCastExprToType(IntExpr.take(), result, |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 684 | CK_IntegralComplexToFloatingComplex); |
| 685 | |
| 686 | // float -> _Complex float |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 687 | if (ConvertFloat) |
| 688 | FloatExpr = S.ImpCastExprToType(FloatExpr.take(), result, |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 689 | CK_FloatingRealToComplex); |
| 690 | |
| 691 | return result; |
| 692 | } |
| 693 | |
| 694 | /// \brief Handle arithmethic conversion with floating point types. Helper |
| 695 | /// function of UsualArithmeticConversions() |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 696 | static QualType handleFloatConversion(Sema &S, ExprResult &LHS, |
| 697 | ExprResult &RHS, QualType LHSType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 698 | QualType RHSType, bool IsCompAssign) { |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 699 | bool LHSFloat = LHSType->isRealFloatingType(); |
| 700 | bool RHSFloat = RHSType->isRealFloatingType(); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 701 | |
| 702 | // If we have two real floating types, convert the smaller operand |
| 703 | // to the bigger result. |
| 704 | if (LHSFloat && RHSFloat) { |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 705 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 706 | if (order > 0) { |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 707 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingCast); |
| 708 | return LHSType; |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | assert(order < 0 && "illegal float comparison"); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 712 | if (!IsCompAssign) |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 713 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingCast); |
| 714 | return RHSType; |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | if (LHSFloat) |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 718 | return handleIntToFloatConversion(S, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 719 | /*convertFloat=*/!IsCompAssign, |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 720 | /*convertInt=*/ true); |
| 721 | assert(RHSFloat); |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 722 | return handleIntToFloatConversion(S, RHS, LHS, RHSType, LHSType, |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 723 | /*convertInt=*/ true, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 724 | /*convertFloat=*/!IsCompAssign); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /// \brief Handle conversions with GCC complex int extension. Helper function |
Benjamin Kramer | 499c68b | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 728 | /// of UsualArithmeticConversions() |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 729 | // FIXME: if the operands are (int, _Complex long), we currently |
| 730 | // don't promote the complex. Also, signedness? |
Benjamin Kramer | 499c68b | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 731 | static QualType handleComplexIntConversion(Sema &S, ExprResult &LHS, |
| 732 | ExprResult &RHS, QualType LHSType, |
| 733 | QualType RHSType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 734 | bool IsCompAssign) { |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 735 | const ComplexType *LHSComplexInt = LHSType->getAsComplexIntegerType(); |
| 736 | const ComplexType *RHSComplexInt = RHSType->getAsComplexIntegerType(); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 737 | |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 738 | if (LHSComplexInt && RHSComplexInt) { |
| 739 | int order = S.Context.getIntegerTypeOrder(LHSComplexInt->getElementType(), |
| 740 | RHSComplexInt->getElementType()); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 741 | assert(order && "inequal types with equal element ordering"); |
| 742 | if (order > 0) { |
| 743 | // _Complex int -> _Complex long |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 744 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralComplexCast); |
| 745 | return LHSType; |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 748 | if (!IsCompAssign) |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 749 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralComplexCast); |
| 750 | return RHSType; |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 753 | if (LHSComplexInt) { |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 754 | // int -> _Complex int |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 755 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralRealToComplex); |
| 756 | return LHSType; |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 759 | assert(RHSComplexInt); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 760 | // int -> _Complex int |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 761 | if (!IsCompAssign) |
Richard Trieu | cfe3f21 | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 762 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralRealToComplex); |
| 763 | return RHSType; |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /// \brief Handle integer arithmetic conversions. Helper function of |
| 767 | /// UsualArithmeticConversions() |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 768 | static QualType handleIntegerConversion(Sema &S, ExprResult &LHS, |
| 769 | ExprResult &RHS, QualType LHSType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 770 | QualType RHSType, bool IsCompAssign) { |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 771 | // The rules for this case are in C99 6.3.1.8 |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 772 | int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); |
| 773 | bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); |
| 774 | bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); |
| 775 | if (LHSSigned == RHSSigned) { |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 776 | // Same signedness; use the higher-ranked type |
| 777 | if (order >= 0) { |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 778 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 779 | return LHSType; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 780 | } else if (!IsCompAssign) |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 781 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 782 | return RHSType; |
| 783 | } else if (order != (LHSSigned ? 1 : -1)) { |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 784 | // The unsigned type has greater than or equal rank to the |
| 785 | // signed type, so use the unsigned type |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 786 | if (RHSSigned) { |
| 787 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 788 | return LHSType; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 789 | } else if (!IsCompAssign) |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 790 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 791 | return RHSType; |
| 792 | } else if (S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)) { |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 793 | // The two types are different widths; if we are here, that |
| 794 | // means the signed type is larger than the unsigned type, so |
| 795 | // use the signed type. |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 796 | if (LHSSigned) { |
| 797 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 798 | return LHSType; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 799 | } else if (!IsCompAssign) |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 800 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 801 | return RHSType; |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 802 | } else { |
| 803 | // The signed type is higher-ranked than the unsigned type, |
| 804 | // but isn't actually any bigger (like unsigned int and long |
| 805 | // on most 32-bit systems). Use the unsigned type corresponding |
| 806 | // to the signed type. |
| 807 | QualType result = |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 808 | S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType : RHSType); |
| 809 | RHS = S.ImpCastExprToType(RHS.take(), result, CK_IntegralCast); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 810 | if (!IsCompAssign) |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 811 | LHS = S.ImpCastExprToType(LHS.take(), result, CK_IntegralCast); |
Richard Trieu | 7aa58f1 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 812 | return result; |
| 813 | } |
| 814 | } |
| 815 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 816 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 817 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 818 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 819 | /// responsible for emitting appropriate error diagnostics. |
| 820 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 821 | /// GCC. |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 822 | QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 823 | bool IsCompAssign) { |
| 824 | if (!IsCompAssign) { |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 825 | LHS = UsualUnaryConversions(LHS.take()); |
| 826 | if (LHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 827 | return QualType(); |
| 828 | } |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 829 | |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 830 | RHS = UsualUnaryConversions(RHS.take()); |
| 831 | if (RHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 832 | return QualType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 833 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 835 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 836 | QualType LHSType = |
| 837 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 838 | QualType RHSType = |
| 839 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 840 | |
| 841 | // If both types are identical, no conversion is needed. |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 842 | if (LHSType == RHSType) |
| 843 | return LHSType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 844 | |
| 845 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 846 | // The caller can deal with this (e.g. pointer + int). |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 847 | if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType()) |
| 848 | return LHSType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 849 | |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 850 | // Apply unary and bitfield promotions to the LHS's type. |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 851 | QualType LHSUnpromotedType = LHSType; |
| 852 | if (LHSType->isPromotableIntegerType()) |
| 853 | LHSType = Context.getPromotedIntegerType(LHSType); |
| 854 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(LHS.get()); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 855 | if (!LHSBitfieldPromoteTy.isNull()) |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 856 | LHSType = LHSBitfieldPromoteTy; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 857 | if (LHSType != LHSUnpromotedType && !IsCompAssign) |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 858 | LHS = ImpCastExprToType(LHS.take(), LHSType, CK_IntegralCast); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 859 | |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 860 | // If both types are identical, no conversion is needed. |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 861 | if (LHSType == RHSType) |
| 862 | return LHSType; |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 863 | |
| 864 | // At this point, we have two different arithmetic types. |
| 865 | |
| 866 | // Handle complex types first (C99 6.3.1.8p1). |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 867 | if (LHSType->isComplexType() || RHSType->isComplexType()) |
| 868 | return handleComplexFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 869 | IsCompAssign); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 870 | |
| 871 | // Now handle "real" floating types (i.e. float, double, long double). |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 872 | if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) |
| 873 | return handleFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 874 | IsCompAssign); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 875 | |
| 876 | // Handle GCC complex int extension. |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 877 | if (LHSType->isComplexIntegerType() || RHSType->isComplexIntegerType()) |
Benjamin Kramer | 499c68b | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 878 | return handleComplexIntConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 879 | IsCompAssign); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 880 | |
| 881 | // Finally, we have two differing integer types. |
Richard Trieu | 9a52fbb | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 882 | return handleIntegerConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 883 | IsCompAssign); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 886 | //===----------------------------------------------------------------------===// |
| 887 | // Semantic Analysis for various Expression Types |
| 888 | //===----------------------------------------------------------------------===// |
| 889 | |
| 890 | |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 891 | ExprResult |
| 892 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 893 | SourceLocation DefaultLoc, |
| 894 | SourceLocation RParenLoc, |
| 895 | Expr *ControllingExpr, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 896 | MultiTypeArg ArgTypes, |
| 897 | MultiExprArg ArgExprs) { |
| 898 | unsigned NumAssocs = ArgTypes.size(); |
| 899 | assert(NumAssocs == ArgExprs.size()); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 900 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 901 | ParsedType *ParsedTypes = ArgTypes.release(); |
| 902 | Expr **Exprs = ArgExprs.release(); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 903 | |
| 904 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 905 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 906 | if (ParsedTypes[i]) |
| 907 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 908 | else |
| 909 | Types[i] = 0; |
| 910 | } |
| 911 | |
| 912 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 913 | ControllingExpr, Types, Exprs, |
| 914 | NumAssocs); |
Benjamin Kramer | 3462376 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 915 | delete [] Types; |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 916 | return ER; |
| 917 | } |
| 918 | |
| 919 | ExprResult |
| 920 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 921 | SourceLocation DefaultLoc, |
| 922 | SourceLocation RParenLoc, |
| 923 | Expr *ControllingExpr, |
| 924 | TypeSourceInfo **Types, |
| 925 | Expr **Exprs, |
| 926 | unsigned NumAssocs) { |
| 927 | bool TypeErrorFound = false, |
| 928 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 929 | ContainsUnexpandedParameterPack |
| 930 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 931 | |
| 932 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 933 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 934 | ContainsUnexpandedParameterPack = true; |
| 935 | |
| 936 | if (Types[i]) { |
| 937 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 938 | ContainsUnexpandedParameterPack = true; |
| 939 | |
| 940 | if (Types[i]->getType()->isDependentType()) { |
| 941 | IsResultDependent = true; |
| 942 | } else { |
| 943 | // C1X 6.5.1.1p2 "The type name in a generic association shall specify a |
| 944 | // complete object type other than a variably modified type." |
| 945 | unsigned D = 0; |
| 946 | if (Types[i]->getType()->isIncompleteType()) |
| 947 | D = diag::err_assoc_type_incomplete; |
| 948 | else if (!Types[i]->getType()->isObjectType()) |
| 949 | D = diag::err_assoc_type_nonobject; |
| 950 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 951 | D = diag::err_assoc_type_variably_modified; |
| 952 | |
| 953 | if (D != 0) { |
| 954 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 955 | << Types[i]->getTypeLoc().getSourceRange() |
| 956 | << Types[i]->getType(); |
| 957 | TypeErrorFound = true; |
| 958 | } |
| 959 | |
| 960 | // C1X 6.5.1.1p2 "No two generic associations in the same generic |
| 961 | // selection shall specify compatible types." |
| 962 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 963 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 964 | Context.typesAreCompatible(Types[i]->getType(), |
| 965 | Types[j]->getType())) { |
| 966 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 967 | diag::err_assoc_compatible_types) |
| 968 | << Types[j]->getTypeLoc().getSourceRange() |
| 969 | << Types[j]->getType() |
| 970 | << Types[i]->getType(); |
| 971 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 972 | diag::note_compat_assoc) |
| 973 | << Types[i]->getTypeLoc().getSourceRange() |
| 974 | << Types[i]->getType(); |
| 975 | TypeErrorFound = true; |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | if (TypeErrorFound) |
| 981 | return ExprError(); |
| 982 | |
| 983 | // If we determined that the generic selection is result-dependent, don't |
| 984 | // try to compute the result expression. |
| 985 | if (IsResultDependent) |
| 986 | return Owned(new (Context) GenericSelectionExpr( |
| 987 | Context, KeyLoc, ControllingExpr, |
| 988 | Types, Exprs, NumAssocs, DefaultLoc, |
| 989 | RParenLoc, ContainsUnexpandedParameterPack)); |
| 990 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 991 | SmallVector<unsigned, 1> CompatIndices; |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 992 | unsigned DefaultIndex = -1U; |
| 993 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 994 | if (!Types[i]) |
| 995 | DefaultIndex = i; |
| 996 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 997 | Types[i]->getType())) |
| 998 | CompatIndices.push_back(i); |
| 999 | } |
| 1000 | |
| 1001 | // C1X 6.5.1.1p2 "The controlling expression of a generic selection shall have |
| 1002 | // type compatible with at most one of the types named in its generic |
| 1003 | // association list." |
| 1004 | if (CompatIndices.size() > 1) { |
| 1005 | // We strip parens here because the controlling expression is typically |
| 1006 | // parenthesized in macro definitions. |
| 1007 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1008 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 1009 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 1010 | << (unsigned) CompatIndices.size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1011 | for (SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1012 | E = CompatIndices.end(); I != E; ++I) { |
| 1013 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 1014 | diag::note_compat_assoc) |
| 1015 | << Types[*I]->getTypeLoc().getSourceRange() |
| 1016 | << Types[*I]->getType(); |
| 1017 | } |
| 1018 | return ExprError(); |
| 1019 | } |
| 1020 | |
| 1021 | // C1X 6.5.1.1p2 "If a generic selection has no default generic association, |
| 1022 | // its controlling expression shall have type compatible with exactly one of |
| 1023 | // the types named in its generic association list." |
| 1024 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 1025 | // We strip parens here because the controlling expression is typically |
| 1026 | // parenthesized in macro definitions. |
| 1027 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1028 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 1029 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 1030 | return ExprError(); |
| 1031 | } |
| 1032 | |
| 1033 | // C1X 6.5.1.1p3 "If a generic selection has a generic association with a |
| 1034 | // type name that is compatible with the type of the controlling expression, |
| 1035 | // then the result expression of the generic selection is the expression |
| 1036 | // in that generic association. Otherwise, the result expression of the |
| 1037 | // generic selection is the expression in the default generic association." |
| 1038 | unsigned ResultIndex = |
| 1039 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 1040 | |
| 1041 | return Owned(new (Context) GenericSelectionExpr( |
| 1042 | Context, KeyLoc, ControllingExpr, |
| 1043 | Types, Exprs, NumAssocs, DefaultLoc, |
| 1044 | RParenLoc, ContainsUnexpandedParameterPack, |
| 1045 | ResultIndex)); |
| 1046 | } |
| 1047 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1048 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1049 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 1050 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 1051 | /// multiple tokens. However, the common case is that StringToks points to one |
| 1052 | /// string. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1053 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1054 | ExprResult |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1055 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1056 | assert(NumStringToks && "Must have at least one string!"); |
| 1057 | |
Chris Lattner | 8a24e58 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1058 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1059 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1060 | return ExprError(); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1061 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1062 | SmallVector<SourceLocation, 4> StringTokLocs; |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1063 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 1064 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1065 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1066 | QualType StrTy = Context.CharTy; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1067 | if (Literal.isWide()) |
Anders Carlsson | 6b06e18 | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1068 | StrTy = Context.getWCharType(); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1069 | else if (Literal.isUTF16()) |
| 1070 | StrTy = Context.Char16Ty; |
| 1071 | else if (Literal.isUTF32()) |
| 1072 | StrTy = Context.Char32Ty; |
Anders Carlsson | 6b06e18 | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1073 | else if (Literal.Pascal) |
| 1074 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1075 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1076 | StringLiteral::StringKind Kind = StringLiteral::Ascii; |
| 1077 | if (Literal.isWide()) |
| 1078 | Kind = StringLiteral::Wide; |
| 1079 | else if (Literal.isUTF8()) |
| 1080 | Kind = StringLiteral::UTF8; |
| 1081 | else if (Literal.isUTF16()) |
| 1082 | Kind = StringLiteral::UTF16; |
| 1083 | else if (Literal.isUTF32()) |
| 1084 | Kind = StringLiteral::UTF32; |
| 1085 | |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1086 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
Chris Lattner | a8687ae | 2010-06-15 18:05:34 +0000 | [diff] [blame] | 1087 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1088 | StrTy.addConst(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1089 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1090 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 1091 | // the nul terminator character as well as the string length for pascal |
| 1092 | // strings. |
| 1093 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | d42c29f | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1094 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1095 | ArrayType::Normal, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1097 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1098 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1099 | Kind, Literal.Pascal, StrTy, |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1100 | &StringTokLocs[0], |
| 1101 | StringTokLocs.size())); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1104 | enum CaptureResult { |
| 1105 | /// No capture is required. |
| 1106 | CR_NoCapture, |
| 1107 | |
| 1108 | /// A capture is required. |
| 1109 | CR_Capture, |
| 1110 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1111 | /// A by-ref capture is required. |
| 1112 | CR_CaptureByRef, |
| 1113 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1114 | /// An error occurred when trying to capture the given variable. |
| 1115 | CR_Error |
| 1116 | }; |
| 1117 | |
| 1118 | /// Diagnose an uncapturable value reference. |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1119 | /// |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1120 | /// \param var - the variable referenced |
| 1121 | /// \param DC - the context which we couldn't capture through |
| 1122 | static CaptureResult |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1123 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1124 | VarDecl *var, DeclContext *DC) { |
| 1125 | switch (S.ExprEvalContexts.back().Context) { |
| 1126 | case Sema::Unevaluated: |
| 1127 | // The argument will never be evaluated, so don't complain. |
| 1128 | return CR_NoCapture; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1130 | case Sema::PotentiallyEvaluated: |
| 1131 | case Sema::PotentiallyEvaluatedIfUsed: |
| 1132 | break; |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1133 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1134 | case Sema::PotentiallyPotentiallyEvaluated: |
| 1135 | // FIXME: delay these! |
| 1136 | break; |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 1137 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1139 | // Don't diagnose about capture if we're not actually in code right |
| 1140 | // now; in general, there are more appropriate places that will |
| 1141 | // diagnose this. |
| 1142 | if (!S.CurContext->isFunctionOrMethod()) return CR_NoCapture; |
| 1143 | |
John McCall | 92d627e | 2011-03-22 23:15:50 +0000 | [diff] [blame] | 1144 | // Certain madnesses can happen with parameter declarations, which |
| 1145 | // we want to ignore. |
| 1146 | if (isa<ParmVarDecl>(var)) { |
| 1147 | // - If the parameter still belongs to the translation unit, then |
| 1148 | // we're actually just using one parameter in the declaration of |
| 1149 | // the next. This is useful in e.g. VLAs. |
| 1150 | if (isa<TranslationUnitDecl>(var->getDeclContext())) |
| 1151 | return CR_NoCapture; |
| 1152 | |
| 1153 | // - This particular madness can happen in ill-formed default |
| 1154 | // arguments; claim it's okay and let downstream code handle it. |
| 1155 | if (S.CurContext == var->getDeclContext()->getParent()) |
| 1156 | return CR_NoCapture; |
| 1157 | } |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1158 | |
| 1159 | DeclarationName functionName; |
| 1160 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(var->getDeclContext())) |
| 1161 | functionName = fn->getDeclName(); |
| 1162 | // FIXME: variable from enclosing block that we couldn't capture from! |
| 1163 | |
| 1164 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 1165 | << var->getIdentifier() << functionName; |
| 1166 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 1167 | << var->getIdentifier(); |
| 1168 | |
| 1169 | return CR_Error; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1172 | /// There is a well-formed capture at a particular scope level; |
| 1173 | /// propagate it through all the nested blocks. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1174 | static CaptureResult propagateCapture(Sema &S, unsigned ValidScopeIndex, |
| 1175 | const BlockDecl::Capture &Capture) { |
| 1176 | VarDecl *var = Capture.getVariable(); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1177 | |
| 1178 | // Update all the inner blocks with the capture information. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1179 | for (unsigned i = ValidScopeIndex + 1, e = S.FunctionScopes.size(); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1180 | i != e; ++i) { |
| 1181 | BlockScopeInfo *innerBlock = cast<BlockScopeInfo>(S.FunctionScopes[i]); |
| 1182 | innerBlock->Captures.push_back( |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1183 | BlockDecl::Capture(Capture.getVariable(), Capture.isByRef(), |
| 1184 | /*nested*/ true, Capture.getCopyExpr())); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1185 | innerBlock->CaptureMap[var] = innerBlock->Captures.size(); // +1 |
| 1186 | } |
| 1187 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1188 | return Capture.isByRef() ? CR_CaptureByRef : CR_Capture; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | /// shouldCaptureValueReference - Determine if a reference to the |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1192 | /// given value in the current context requires a variable capture. |
| 1193 | /// |
| 1194 | /// This also keeps the captures set in the BlockScopeInfo records |
| 1195 | /// up-to-date. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1196 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1197 | ValueDecl *Value) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1198 | // Only variables ever require capture. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1199 | VarDecl *var = dyn_cast<VarDecl>(Value); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1200 | if (!var) return CR_NoCapture; |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1201 | |
| 1202 | // Fast path: variables from the current context never require capture. |
| 1203 | DeclContext *DC = S.CurContext; |
| 1204 | if (var->getDeclContext() == DC) return CR_NoCapture; |
| 1205 | |
| 1206 | // Only variables with local storage require capture. |
| 1207 | // FIXME: What about 'const' variables in C++? |
| 1208 | if (!var->hasLocalStorage()) return CR_NoCapture; |
| 1209 | |
| 1210 | // Otherwise, we need to capture. |
| 1211 | |
| 1212 | unsigned functionScopesIndex = S.FunctionScopes.size() - 1; |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1213 | do { |
| 1214 | // Only blocks (and eventually C++0x closures) can capture; other |
| 1215 | // scopes don't work. |
| 1216 | if (!isa<BlockDecl>(DC)) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1217 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1218 | |
| 1219 | BlockScopeInfo *blockScope = |
| 1220 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1221 | assert(blockScope->TheDecl == static_cast<BlockDecl*>(DC)); |
| 1222 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1223 | // Check whether we've already captured it in this block. If so, |
| 1224 | // we're done. |
| 1225 | if (unsigned indexPlus1 = blockScope->CaptureMap[var]) |
| 1226 | return propagateCapture(S, functionScopesIndex, |
| 1227 | blockScope->Captures[indexPlus1 - 1]); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1228 | |
| 1229 | functionScopesIndex--; |
| 1230 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 1231 | } while (var->getDeclContext() != DC); |
| 1232 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1233 | // Okay, we descended all the way to the block that defines the variable. |
| 1234 | // Actually try to capture it. |
| 1235 | QualType type = var->getType(); |
| 1236 | |
| 1237 | // Prohibit variably-modified types. |
| 1238 | if (type->isVariablyModifiedType()) { |
| 1239 | S.Diag(loc, diag::err_ref_vm_type); |
| 1240 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1241 | return CR_Error; |
| 1242 | } |
| 1243 | |
| 1244 | // Prohibit arrays, even in __block variables, but not references to |
| 1245 | // them. |
| 1246 | if (type->isArrayType()) { |
| 1247 | S.Diag(loc, diag::err_ref_array_type); |
| 1248 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1249 | return CR_Error; |
| 1250 | } |
| 1251 | |
| 1252 | S.MarkDeclarationReferenced(loc, var); |
| 1253 | |
| 1254 | // The BlocksAttr indicates the variable is bound by-reference. |
| 1255 | bool byRef = var->hasAttr<BlocksAttr>(); |
| 1256 | |
| 1257 | // Build a copy expression. |
| 1258 | Expr *copyExpr = 0; |
John McCall | a85af56 | 2011-04-28 02:15:35 +0000 | [diff] [blame] | 1259 | const RecordType *rtype; |
| 1260 | if (!byRef && S.getLangOptions().CPlusPlus && !type->isDependentType() && |
| 1261 | (rtype = type->getAs<RecordType>())) { |
| 1262 | |
| 1263 | // The capture logic needs the destructor, so make sure we mark it. |
| 1264 | // Usually this is unnecessary because most local variables have |
| 1265 | // their destructors marked at declaration time, but parameters are |
| 1266 | // an exception because it's technically only the call site that |
| 1267 | // actually requires the destructor. |
| 1268 | if (isa<ParmVarDecl>(var)) |
| 1269 | S.FinalizeVarWithDestructor(var, rtype); |
| 1270 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1271 | // According to the blocks spec, the capture of a variable from |
| 1272 | // the stack requires a const copy constructor. This is not true |
| 1273 | // of the copy/move done to move a __block variable to the heap. |
| 1274 | type.addConst(); |
| 1275 | |
| 1276 | Expr *declRef = new (S.Context) DeclRefExpr(var, type, VK_LValue, loc); |
| 1277 | ExprResult result = |
| 1278 | S.PerformCopyInitialization( |
| 1279 | InitializedEntity::InitializeBlock(var->getLocation(), |
| 1280 | type, false), |
| 1281 | loc, S.Owned(declRef)); |
| 1282 | |
| 1283 | // Build a full-expression copy expression if initialization |
| 1284 | // succeeded and used a non-trivial constructor. Recover from |
| 1285 | // errors by pretending that the copy isn't necessary. |
| 1286 | if (!result.isInvalid() && |
| 1287 | !cast<CXXConstructExpr>(result.get())->getConstructor()->isTrivial()) { |
| 1288 | result = S.MaybeCreateExprWithCleanups(result); |
| 1289 | copyExpr = result.take(); |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | // We're currently at the declarer; go back to the closure. |
| 1294 | functionScopesIndex++; |
| 1295 | BlockScopeInfo *blockScope = |
| 1296 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1297 | |
| 1298 | // Build a valid capture in this scope. |
| 1299 | blockScope->Captures.push_back( |
| 1300 | BlockDecl::Capture(var, byRef, /*nested*/ false, copyExpr)); |
| 1301 | blockScope->CaptureMap[var] = blockScope->Captures.size(); // +1 |
| 1302 | |
| 1303 | // Propagate that to inner captures if necessary. |
| 1304 | return propagateCapture(S, functionScopesIndex, |
| 1305 | blockScope->Captures.back()); |
| 1306 | } |
| 1307 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1308 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *VD, |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1309 | const DeclarationNameInfo &NameInfo, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1310 | bool ByRef) { |
| 1311 | assert(isa<VarDecl>(VD) && "capturing non-variable"); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1312 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1313 | VarDecl *var = cast<VarDecl>(VD); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1314 | assert(var->hasLocalStorage() && "capturing non-local"); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1315 | assert(ByRef == var->hasAttr<BlocksAttr>() && "byref set wrong"); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1316 | |
| 1317 | QualType exprType = var->getType().getNonReferenceType(); |
| 1318 | |
| 1319 | BlockDeclRefExpr *BDRE; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1320 | if (!ByRef) { |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1321 | // The variable will be bound by copy; make it const within the |
| 1322 | // closure, but record that this was done in the expression. |
| 1323 | bool constAdded = !exprType.isConstQualified(); |
| 1324 | exprType.addConst(); |
| 1325 | |
| 1326 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1327 | NameInfo.getLoc(), false, |
| 1328 | constAdded); |
| 1329 | } else { |
| 1330 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1331 | NameInfo.getLoc(), true); |
| 1332 | } |
| 1333 | |
| 1334 | return S.Owned(BDRE); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1335 | } |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1336 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1337 | ExprResult |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1338 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1339 | SourceLocation Loc, |
| 1340 | const CXXScopeSpec *SS) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1341 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1342 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1345 | /// BuildDeclRefExpr - Build an expression that references a |
| 1346 | /// declaration that does not require a closure capture. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1347 | ExprResult |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1348 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1349 | const DeclarationNameInfo &NameInfo, |
| 1350 | const CXXScopeSpec *SS) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1351 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1353 | Expr *E = DeclRefExpr::Create(Context, |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1354 | SS? SS->getWithLocInContext(Context) |
| 1355 | : NestedNameSpecifierLoc(), |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1356 | D, NameInfo, Ty, VK); |
| 1357 | |
| 1358 | // Just in case we're building an illegal pointer-to-member. |
| 1359 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 1360 | E->setObjectKind(OK_BitField); |
| 1361 | |
| 1362 | return Owned(E); |
Douglas Gregor | c7acfdf | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1365 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1366 | /// possibly a list of template arguments. |
| 1367 | /// |
| 1368 | /// If this produces template arguments, it is permitted to call |
| 1369 | /// DecomposeTemplateName. |
| 1370 | /// |
| 1371 | /// This actually loses a lot of source location information for |
| 1372 | /// non-standard name kinds; we should consider preserving that in |
| 1373 | /// some way. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1374 | void |
| 1375 | Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, |
| 1376 | TemplateArgumentListInfo &Buffer, |
| 1377 | DeclarationNameInfo &NameInfo, |
| 1378 | const TemplateArgumentListInfo *&TemplateArgs) { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1379 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1380 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1381 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1382 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1383 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1384 | Id.TemplateId->getTemplateArgs(), |
| 1385 | Id.TemplateId->NumArgs); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1386 | translateTemplateArguments(TemplateArgsPtr, Buffer); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1387 | TemplateArgsPtr.release(); |
| 1388 | |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1389 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1390 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1391 | NameInfo = Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1392 | TemplateArgs = &Buffer; |
| 1393 | } else { |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1394 | NameInfo = GetNameFromUnqualifiedId(Id); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1395 | TemplateArgs = 0; |
| 1396 | } |
| 1397 | } |
| 1398 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1399 | /// Diagnose an empty lookup. |
| 1400 | /// |
| 1401 | /// \return false if new lookup candidates were found |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1402 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
Kaelyn Uhrain | 4283092 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1403 | CorrectTypoContext CTC, |
| 1404 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 1405 | Expr **Args, unsigned NumArgs) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1406 | DeclarationName Name = R.getLookupName(); |
| 1407 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1408 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1409 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1410 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1411 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1412 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1413 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1414 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1415 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1416 | |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1417 | // If the original lookup was an unqualified lookup, fake an |
| 1418 | // unqualified lookup. This is useful when (for example) the |
| 1419 | // original lookup would not have found something because it was a |
| 1420 | // dependent name. |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1421 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1422 | DC; DC = DC->getParent()) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1423 | if (isa<CXXRecordDecl>(DC)) { |
| 1424 | LookupQualifiedName(R, DC); |
| 1425 | |
| 1426 | if (!R.empty()) { |
| 1427 | // Don't give errors about ambiguities in this lookup. |
| 1428 | R.suppressDiagnostics(); |
| 1429 | |
| 1430 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1431 | bool isInstance = CurMethod && |
| 1432 | CurMethod->isInstance() && |
| 1433 | DC == CurMethod->getParent(); |
| 1434 | |
| 1435 | // Give a code modification hint to insert 'this->'. |
| 1436 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1437 | // Actually quite difficult! |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1438 | if (isInstance) { |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1439 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1440 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1441 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1442 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1443 | if (DepMethod) { |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 1444 | if (getLangOptions().MicrosoftExt) |
Francois Pichet | bcf6471 | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 1445 | diagnostic = diag::warn_found_via_dependent_bases_lookup; |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1446 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1447 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1448 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1449 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1450 | R.getNameLoc(), DepThisType, false); |
| 1451 | TemplateArgumentListInfo TList; |
| 1452 | if (ULE->hasExplicitTemplateArgs()) |
| 1453 | ULE->copyTemplateArgumentsInto(TList); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1454 | |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1455 | CXXScopeSpec SS; |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1456 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1457 | CXXDependentScopeMemberExpr *DepExpr = |
| 1458 | CXXDependentScopeMemberExpr::Create( |
| 1459 | Context, DepThis, DepThisType, true, SourceLocation(), |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1460 | SS.getWithLocInContext(Context), NULL, |
Francois Pichet | 4391c75 | 2011-09-04 23:00:48 +0000 | [diff] [blame] | 1461 | R.getLookupNameInfo(), |
| 1462 | ULE->hasExplicitTemplateArgs() ? &TList : 0); |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1463 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1464 | } else { |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1465 | // FIXME: we should be able to handle this case too. It is correct |
| 1466 | // to add this-> here. This is a workaround for PR7947. |
| 1467 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1468 | } |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1469 | } else { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1470 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1471 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1472 | |
| 1473 | // Do we really want to note all of these? |
| 1474 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1475 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1476 | |
| 1477 | // Tell the callee to try to recover. |
| 1478 | return false; |
| 1479 | } |
Douglas Gregor | 86b8d9f | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1480 | |
| 1481 | R.clear(); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1485 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1486 | TypoCorrection Corrected; |
| 1487 | if (S && (Corrected = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), |
| 1488 | S, &SS, NULL, false, CTC))) { |
| 1489 | std::string CorrectedStr(Corrected.getAsString(getLangOptions())); |
| 1490 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
| 1491 | R.setLookupName(Corrected.getCorrection()); |
| 1492 | |
Hans Wennborg | 38198de | 2011-07-12 08:45:31 +0000 | [diff] [blame] | 1493 | if (NamedDecl *ND = Corrected.getCorrectionDecl()) { |
Kaelyn Uhrain | acbdc57 | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1494 | if (Corrected.isOverloaded()) { |
| 1495 | OverloadCandidateSet OCS(R.getNameLoc()); |
| 1496 | OverloadCandidateSet::iterator Best; |
| 1497 | for (TypoCorrection::decl_iterator CD = Corrected.begin(), |
| 1498 | CDEnd = Corrected.end(); |
| 1499 | CD != CDEnd; ++CD) { |
Kaelyn Uhrain | 6242220 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1500 | if (FunctionTemplateDecl *FTD = |
Kaelyn Uhrain | 4283092 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1501 | dyn_cast<FunctionTemplateDecl>(*CD)) |
| 1502 | AddTemplateOverloadCandidate( |
| 1503 | FTD, DeclAccessPair::make(FTD, AS_none), ExplicitTemplateArgs, |
| 1504 | Args, NumArgs, OCS); |
Kaelyn Uhrain | 6242220 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1505 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*CD)) |
| 1506 | if (!ExplicitTemplateArgs || ExplicitTemplateArgs->size() == 0) |
| 1507 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), |
| 1508 | Args, NumArgs, OCS); |
Kaelyn Uhrain | acbdc57 | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1509 | } |
| 1510 | switch (OCS.BestViableFunction(*this, R.getNameLoc(), Best)) { |
| 1511 | case OR_Success: |
| 1512 | ND = Best->Function; |
| 1513 | break; |
| 1514 | default: |
Kaelyn Uhrain | ea35018 | 2011-08-04 23:30:54 +0000 | [diff] [blame] | 1515 | break; |
Kaelyn Uhrain | acbdc57 | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1516 | } |
| 1517 | } |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1518 | R.addDecl(ND); |
| 1519 | if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1520 | if (SS.isEmpty()) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1521 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr |
| 1522 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1523 | else |
| 1524 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1525 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1526 | << SS.getRange() |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1527 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
| 1528 | if (ND) |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1529 | Diag(ND->getLocation(), diag::note_previous_decl) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1530 | << CorrectedQuotedStr; |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1531 | |
| 1532 | // Tell the callee to try to recover. |
| 1533 | return false; |
| 1534 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1535 | |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1536 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1537 | // FIXME: If we ended up with a typo for a type name or |
| 1538 | // Objective-C class name, we're in trouble because the parser |
| 1539 | // is in the wrong place to recover. Suggest the typo |
| 1540 | // correction, but don't make it a fix-it since we're not going |
| 1541 | // to recover well anyway. |
| 1542 | if (SS.isEmpty()) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1543 | Diag(R.getNameLoc(), diagnostic_suggest) |
| 1544 | << Name << CorrectedQuotedStr; |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1545 | else |
| 1546 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1547 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1548 | << SS.getRange(); |
| 1549 | |
| 1550 | // Don't try to recover; it won't work. |
| 1551 | return true; |
| 1552 | } |
| 1553 | } else { |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1554 | // FIXME: We found a keyword. Suggest it, but don't provide a fix-it |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1555 | // because we aren't able to recover. |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1556 | if (SS.isEmpty()) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1557 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1558 | else |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1559 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1560 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1561 | << SS.getRange(); |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1562 | return true; |
| 1563 | } |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1564 | } |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1565 | R.clear(); |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1566 | |
| 1567 | // Emit a special diagnostic for failed member lookups. |
| 1568 | // FIXME: computing the declaration context might fail here (?) |
| 1569 | if (!SS.isEmpty()) { |
| 1570 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1571 | << Name << computeDeclContext(SS, false) |
| 1572 | << SS.getRange(); |
| 1573 | return true; |
| 1574 | } |
| 1575 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1576 | // Give up, we can't recover. |
| 1577 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1578 | return true; |
| 1579 | } |
| 1580 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1581 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1582 | CXXScopeSpec &SS, |
| 1583 | UnqualifiedId &Id, |
| 1584 | bool HasTrailingLParen, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1585 | bool IsAddressOfOperand) { |
| 1586 | assert(!(IsAddressOfOperand && HasTrailingLParen) && |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1587 | "cannot be direct & operand and have a trailing lparen"); |
| 1588 | |
| 1589 | if (SS.isInvalid()) |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1590 | return ExprError(); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1591 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1592 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1593 | |
| 1594 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1595 | DeclarationNameInfo NameInfo; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1596 | const TemplateArgumentListInfo *TemplateArgs; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1597 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1598 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1599 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1600 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1601 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1602 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1603 | // C++ [temp.dep.expr]p3: |
| 1604 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1605 | // -- an identifier that was declared with a dependent type, |
| 1606 | // (note: handled after lookup) |
| 1607 | // -- a template-id that is dependent, |
| 1608 | // (note: handled in BuildTemplateIdExpr) |
| 1609 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1610 | // -- a nested-name-specifier that contains a class-name that |
| 1611 | // names a dependent type. |
| 1612 | // Determine whether this is a member of an unknown specialization; |
| 1613 | // we need to handle these differently. |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1614 | bool DependentID = false; |
| 1615 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1616 | Name.getCXXNameType()->isDependentType()) { |
| 1617 | DependentID = true; |
| 1618 | } else if (SS.isSet()) { |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1619 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1620 | if (RequireCompleteDeclContext(SS, DC)) |
| 1621 | return ExprError(); |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1622 | } else { |
| 1623 | DependentID = true; |
| 1624 | } |
| 1625 | } |
| 1626 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1627 | if (DependentID) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1628 | return ActOnDependentIdExpression(SS, NameInfo, IsAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1629 | TemplateArgs); |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1630 | |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1631 | bool IvarLookupFollowUp = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1632 | // Perform the required lookup. |
Fariborz Jahanian | 7f4427f | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1633 | LookupResult R(*this, NameInfo, |
| 1634 | (Id.getKind() == UnqualifiedId::IK_ImplicitSelfParam) |
| 1635 | ? LookupObjCImplicitSelfParam : LookupOrdinaryName); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1636 | if (TemplateArgs) { |
Douglas Gregor | 3e51e17 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1637 | // Lookup the template name again to correctly establish the context in |
| 1638 | // which it was found. This is really unfortunate as we already did the |
| 1639 | // lookup to determine that it was a template name in the first place. If |
| 1640 | // this becomes a performance hit, we can work harder to preserve those |
| 1641 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1642 | bool MemberOfUnknownSpecialization; |
| 1643 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1644 | MemberOfUnknownSpecialization); |
Douglas Gregor | a522693 | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1645 | |
| 1646 | if (MemberOfUnknownSpecialization || |
| 1647 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1648 | return ActOnDependentIdExpression(SS, NameInfo, IsAddressOfOperand, |
Douglas Gregor | a522693 | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1649 | TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1650 | } else { |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1651 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1652 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | |
Douglas Gregor | a522693 | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1654 | // If the result might be in a dependent base class, this is a dependent |
| 1655 | // id-expression. |
| 1656 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1657 | return ActOnDependentIdExpression(SS, NameInfo, IsAddressOfOperand, |
Douglas Gregor | a522693 | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1658 | TemplateArgs); |
| 1659 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1660 | // If this reference is in an Objective-C method, then we need to do |
| 1661 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1662 | if (IvarLookupFollowUp) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1663 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1664 | if (E.isInvalid()) |
| 1665 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1667 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1668 | return Owned(Ex); |
| 1669 | |
Fariborz Jahanian | 18d90a9 | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1670 | // for further use, this must be set to false if in class method. |
| 1671 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | ebf4cb4 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1672 | } |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1673 | } |
Douglas Gregor | f15f5d3 | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1674 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1675 | if (R.isAmbiguous()) |
| 1676 | return ExprError(); |
| 1677 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1678 | // Determine whether this name might be a candidate for |
| 1679 | // argument-dependent lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1680 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1681 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1682 | if (R.empty() && !ADL) { |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 1683 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1684 | // in C90, extension in C99, forbidden in C++). |
| 1685 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1686 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1687 | if (D) R.addDecl(D); |
| 1688 | } |
| 1689 | |
| 1690 | // If this name wasn't predeclared and if this is not a function |
| 1691 | // call, diagnose the problem. |
| 1692 | if (R.empty()) { |
Douglas Gregor | 5fd04d4 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1693 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1694 | return ExprError(); |
| 1695 | |
| 1696 | assert(!R.empty() && |
| 1697 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1698 | |
| 1699 | // If we found an Objective-C instance variable, let |
| 1700 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1701 | // reference the ivar. |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1702 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1703 | R.clear(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1704 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1705 | assert(E.isInvalid() || E.get()); |
| 1706 | return move(E); |
| 1707 | } |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 1708 | } |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 1709 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1711 | // This is guaranteed from this point on. |
| 1712 | assert(!R.empty() || ADL); |
| 1713 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1714 | // Check whether this might be a C++ implicit instance member access. |
John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1715 | // C++ [class.mfct.non-static]p3: |
| 1716 | // When an id-expression that is not part of a class member access |
| 1717 | // syntax and not used to form a pointer to member is used in the |
| 1718 | // body of a non-static member function of class X, if name lookup |
| 1719 | // resolves the name in the id-expression to a non-static non-type |
| 1720 | // member of some class C, the id-expression is transformed into a |
| 1721 | // class member access expression using (*this) as the |
| 1722 | // postfix-expression to the left of the . operator. |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1723 | // |
| 1724 | // But we don't actually need to do this for '&' operands if R |
| 1725 | // resolved to a function or overloaded function set, because the |
| 1726 | // expression is ill-formed if it actually works out to be a |
| 1727 | // non-static member function: |
| 1728 | // |
| 1729 | // C++ [expr.ref]p4: |
| 1730 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1731 | // [t]he expression can be used only as the left-hand operand of a |
| 1732 | // member function call. |
| 1733 | // |
| 1734 | // There are other safeguards against such uses, but it's important |
| 1735 | // to get this right here so that we don't end up making a |
| 1736 | // spuriously dependent expression if we're inside a dependent |
| 1737 | // instance method. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1738 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1739 | bool MightBeImplicitMember; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1740 | if (!IsAddressOfOperand) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1741 | MightBeImplicitMember = true; |
| 1742 | else if (!SS.isEmpty()) |
| 1743 | MightBeImplicitMember = false; |
| 1744 | else if (R.isOverloadedResult()) |
| 1745 | MightBeImplicitMember = false; |
Douglas Gregor | 1262b06 | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1746 | else if (R.isUnresolvableResult()) |
| 1747 | MightBeImplicitMember = true; |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1748 | else |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1749 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1750 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1751 | |
| 1752 | if (MightBeImplicitMember) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1753 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1756 | if (TemplateArgs) |
| 1757 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1758 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1759 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1760 | } |
| 1761 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1762 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1763 | /// declaration name, generally during template instantiation. |
| 1764 | /// There's a large number of things which don't need to be done along |
| 1765 | /// this path. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1766 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1767 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1768 | const DeclarationNameInfo &NameInfo) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1769 | DeclContext *DC; |
Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1770 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1771 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1772 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1773 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1774 | return ExprError(); |
| 1775 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1776 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1777 | LookupQualifiedName(R, DC); |
| 1778 | |
| 1779 | if (R.isAmbiguous()) |
| 1780 | return ExprError(); |
| 1781 | |
| 1782 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1783 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1784 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1785 | return ExprError(); |
| 1786 | } |
| 1787 | |
| 1788 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1789 | } |
| 1790 | |
| 1791 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1792 | /// detected that we're currently inside an ObjC method. Perform some |
| 1793 | /// additional lookup. |
| 1794 | /// |
| 1795 | /// Ideally, most of this would be done by lookup, but there's |
| 1796 | /// actually quite a lot of extra work involved. |
| 1797 | /// |
| 1798 | /// Returns a null sentinel to indicate trivial success. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1799 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1800 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1801 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1802 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1803 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1804 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1805 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1806 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1807 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1808 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1809 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1810 | |
| 1811 | // If we're in a class method, we don't normally want to look for |
| 1812 | // ivars. But if we don't find anything else, and there's an |
| 1813 | // ivar, that's an error. |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1814 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1815 | |
| 1816 | bool LookForIvars; |
| 1817 | if (Lookup.empty()) |
| 1818 | LookForIvars = true; |
| 1819 | else if (IsClassMethod) |
| 1820 | LookForIvars = false; |
| 1821 | else |
| 1822 | LookForIvars = (Lookup.isSingleResult() && |
| 1823 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1824 | ObjCInterfaceDecl *IFace = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1825 | if (LookForIvars) { |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1826 | IFace = CurMethod->getClassInterface(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1827 | ObjCInterfaceDecl *ClassDeclared; |
| 1828 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1829 | // Diagnose using an ivar in a class method. |
| 1830 | if (IsClassMethod) |
| 1831 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1832 | << IV->getDeclName()); |
| 1833 | |
| 1834 | // If we're referencing an invalid decl, just return this as a silent |
| 1835 | // error node. The error diagnostic was already emitted on the decl. |
| 1836 | if (IV->isInvalidDecl()) |
| 1837 | return ExprError(); |
| 1838 | |
| 1839 | // Check if referencing a field with __attribute__((deprecated)). |
| 1840 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1841 | return ExprError(); |
| 1842 | |
| 1843 | // Diagnose the use of an ivar outside of the declaring class. |
| 1844 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1845 | ClassDeclared != IFace) |
| 1846 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1847 | |
| 1848 | // FIXME: This should use a new expr for a direct reference, don't |
| 1849 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1850 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1851 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1852 | SelfName.setIdentifier(&II, SourceLocation()); |
Fariborz Jahanian | 7f4427f | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1853 | SelfName.setKind(UnqualifiedId::IK_ImplicitSelfParam); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1854 | CXXScopeSpec SelfScopeSpec; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1855 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | a1ed39b | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1856 | SelfName, false, false); |
| 1857 | if (SelfExpr.isInvalid()) |
| 1858 | return ExprError(); |
| 1859 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1860 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 1861 | if (SelfExpr.isInvalid()) |
| 1862 | return ExprError(); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1863 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1864 | MarkDeclarationReferenced(Loc, IV); |
| 1865 | return Owned(new (Context) |
| 1866 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1867 | SelfExpr.take(), true, true)); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1868 | } |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1869 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1870 | // We should warn if a local variable hides an ivar. |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1871 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1872 | ObjCInterfaceDecl *ClassDeclared; |
| 1873 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1874 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1875 | IFace == ClassDeclared) |
| 1876 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1877 | } |
| 1878 | } |
| 1879 | |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1880 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1881 | // FIXME. Consolidate this with similar code in LookupName. |
| 1882 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1883 | if (!(getLangOptions().CPlusPlus && |
| 1884 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1885 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1886 | S, Lookup.isForRedeclaration(), |
| 1887 | Lookup.getNameLoc()); |
| 1888 | if (D) Lookup.addDecl(D); |
| 1889 | } |
| 1890 | } |
| 1891 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1892 | // Sentinel value saying that we didn't do anything special. |
| 1893 | return Owned((Expr*) 0); |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1894 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1895 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1896 | /// \brief Cast a base object to a member's actual type. |
| 1897 | /// |
| 1898 | /// Logically this happens in three phases: |
| 1899 | /// |
| 1900 | /// * First we cast from the base type to the naming class. |
| 1901 | /// The naming class is the class into which we were looking |
| 1902 | /// when we found the member; it's the qualifier type if a |
| 1903 | /// qualifier was provided, and otherwise it's the base type. |
| 1904 | /// |
| 1905 | /// * Next we cast from the naming class to the declaring class. |
| 1906 | /// If the member we found was brought into a class's scope by |
| 1907 | /// a using declaration, this is that class; otherwise it's |
| 1908 | /// the class declaring the member. |
| 1909 | /// |
| 1910 | /// * Finally we cast from the declaring class to the "true" |
| 1911 | /// declaring class of the member. This conversion does not |
| 1912 | /// obey access control. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1913 | ExprResult |
| 1914 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1915 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1916 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1917 | NamedDecl *Member) { |
| 1918 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 1919 | if (!RD) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1920 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1921 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1922 | QualType DestRecordType; |
| 1923 | QualType DestType; |
| 1924 | QualType FromRecordType; |
| 1925 | QualType FromType = From->getType(); |
| 1926 | bool PointerConversions = false; |
| 1927 | if (isa<FieldDecl>(Member)) { |
| 1928 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1929 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1930 | if (FromType->getAs<PointerType>()) { |
| 1931 | DestType = Context.getPointerType(DestRecordType); |
| 1932 | FromRecordType = FromType->getPointeeType(); |
| 1933 | PointerConversions = true; |
| 1934 | } else { |
| 1935 | DestType = DestRecordType; |
| 1936 | FromRecordType = FromType; |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1937 | } |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1938 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 1939 | if (Method->isStatic()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1940 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1942 | DestType = Method->getThisType(Context); |
| 1943 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1945 | if (FromType->getAs<PointerType>()) { |
| 1946 | FromRecordType = FromType->getPointeeType(); |
| 1947 | PointerConversions = true; |
| 1948 | } else { |
| 1949 | FromRecordType = FromType; |
| 1950 | DestType = DestRecordType; |
| 1951 | } |
| 1952 | } else { |
| 1953 | // No conversion necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1954 | return Owned(From); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1955 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1956 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1957 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1958 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1959 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1960 | // If the unqualified types are the same, no conversion is necessary. |
| 1961 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1962 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1963 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1964 | SourceRange FromRange = From->getSourceRange(); |
| 1965 | SourceLocation FromLoc = FromRange.getBegin(); |
| 1966 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1967 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1968 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1969 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1970 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1971 | // class name. |
| 1972 | // |
| 1973 | // If the member was a qualified name and the qualified referred to a |
| 1974 | // specific base subobject type, we'll cast to that intermediate type |
| 1975 | // first and then to the object in which the member is declared. That allows |
| 1976 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 1977 | // |
| 1978 | // class Base { public: int x; }; |
| 1979 | // class Derived1 : public Base { }; |
| 1980 | // class Derived2 : public Base { }; |
| 1981 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 1982 | // |
| 1983 | // void VeryDerived::f() { |
| 1984 | // x = 17; // error: ambiguous base subobjects |
| 1985 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 1986 | // } |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1987 | if (Qualifier) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1988 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 1989 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 1990 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 1991 | |
| 1992 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 1993 | |
| 1994 | // In C++98, the qualifier type doesn't actually have to be a base |
| 1995 | // type of the object type, in which case we just ignore it. |
| 1996 | // Otherwise build the appropriate casts. |
| 1997 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1998 | CXXCastPath BasePath; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1999 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2000 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2001 | return ExprError(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2002 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2003 | if (PointerConversions) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2004 | QType = Context.getPointerType(QType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2005 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2006 | VK, &BasePath).take(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2007 | |
| 2008 | FromType = QType; |
| 2009 | FromRecordType = QRecordType; |
| 2010 | |
| 2011 | // If the qualifier type was the same as the destination type, |
| 2012 | // we're done. |
| 2013 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2014 | return Owned(From); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2015 | } |
| 2016 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2017 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2018 | bool IgnoreAccess = false; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2019 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2020 | // If we actually found the member through a using declaration, cast |
| 2021 | // down to the using declaration's type. |
| 2022 | // |
| 2023 | // Pointer equality is fine here because only one declaration of a |
| 2024 | // class ever has member declarations. |
| 2025 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2026 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2027 | QualType URecordType = Context.getTypeDeclType( |
| 2028 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2029 | |
| 2030 | // We only need to do this if the naming-class to declaring-class |
| 2031 | // conversion is non-trivial. |
| 2032 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2033 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2034 | CXXCastPath BasePath; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2035 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2036 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2037 | return ExprError(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2038 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2039 | QualType UType = URecordType; |
| 2040 | if (PointerConversions) |
| 2041 | UType = Context.getPointerType(UType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2042 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2043 | VK, &BasePath).take(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2044 | FromType = UType; |
| 2045 | FromRecordType = URecordType; |
| 2046 | } |
| 2047 | |
| 2048 | // We don't do access control for the conversion from the |
| 2049 | // declaring class to the true declaring class. |
| 2050 | IgnoreAccess = true; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2051 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2052 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2053 | CXXCastPath BasePath; |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2054 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2055 | FromLoc, FromRange, &BasePath, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2056 | IgnoreAccess)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2057 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2058 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2059 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2060 | VK, &BasePath); |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2061 | } |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2062 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2063 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2064 | const LookupResult &R, |
| 2065 | bool HasTrailingLParen) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2066 | // Only when used directly as the postfix-expression of a call. |
| 2067 | if (!HasTrailingLParen) |
| 2068 | return false; |
| 2069 | |
| 2070 | // Never if a scope specifier was provided. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2071 | if (SS.isSet()) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2072 | return false; |
| 2073 | |
| 2074 | // Only in C++ or ObjC++. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2075 | if (!getLangOptions().CPlusPlus) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2076 | return false; |
| 2077 | |
| 2078 | // Turn off ADL when we find certain kinds of declarations during |
| 2079 | // normal lookup: |
| 2080 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2081 | NamedDecl *D = *I; |
| 2082 | |
| 2083 | // C++0x [basic.lookup.argdep]p3: |
| 2084 | // -- a declaration of a class member |
| 2085 | // Since using decls preserve this property, we check this on the |
| 2086 | // original decl. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2087 | if (D->isCXXClassMember()) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2088 | return false; |
| 2089 | |
| 2090 | // C++0x [basic.lookup.argdep]p3: |
| 2091 | // -- a block-scope function declaration that is not a |
| 2092 | // using-declaration |
| 2093 | // NOTE: we also trigger this for function templates (in fact, we |
| 2094 | // don't check the decl type at all, since all other decl types |
| 2095 | // turn off ADL anyway). |
| 2096 | if (isa<UsingShadowDecl>(D)) |
| 2097 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2098 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2099 | return false; |
| 2100 | |
| 2101 | // C++0x [basic.lookup.argdep]p3: |
| 2102 | // -- a declaration that is neither a function or a function |
| 2103 | // template |
| 2104 | // And also for builtin functions. |
| 2105 | if (isa<FunctionDecl>(D)) { |
| 2106 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2107 | |
| 2108 | // But also builtin functions. |
| 2109 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2110 | return false; |
| 2111 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2112 | return false; |
| 2113 | } |
| 2114 | |
| 2115 | return true; |
| 2116 | } |
| 2117 | |
| 2118 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2119 | /// Diagnoses obvious problems with the use of the given declaration |
| 2120 | /// as an expression. This is only actually called for lookups that |
| 2121 | /// were not overloaded, and it doesn't promise that the declaration |
| 2122 | /// will in fact be used. |
| 2123 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2124 | if (isa<TypedefNameDecl>(D)) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2125 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2126 | return true; |
| 2127 | } |
| 2128 | |
| 2129 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2130 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2131 | return true; |
| 2132 | } |
| 2133 | |
| 2134 | if (isa<NamespaceDecl>(D)) { |
| 2135 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2136 | return true; |
| 2137 | } |
| 2138 | |
| 2139 | return false; |
| 2140 | } |
| 2141 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2142 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2143 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2144 | LookupResult &R, |
| 2145 | bool NeedsADL) { |
John McCall | 3a60c87 | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2146 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2147 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 4b4844f | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2148 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2149 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2150 | R.getFoundDecl()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2151 | |
| 2152 | // We only need to check the declaration if there's exactly one |
| 2153 | // result, because in the overloaded case the results can only be |
| 2154 | // functions and function templates. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2155 | if (R.isSingleResult() && |
| 2156 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2157 | return ExprError(); |
| 2158 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2159 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2160 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2161 | // we've picked a target. |
| 2162 | R.suppressDiagnostics(); |
| 2163 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2164 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2165 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2166 | SS.getWithLocInContext(Context), |
| 2167 | R.getLookupNameInfo(), |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2168 | NeedsADL, R.isOverloadedResult(), |
| 2169 | R.begin(), R.end()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2170 | |
| 2171 | return Owned(ULE); |
| 2172 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2173 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2174 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2175 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2176 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2177 | const DeclarationNameInfo &NameInfo, |
| 2178 | NamedDecl *D) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2179 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2180 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2181 | "Cannot refer unambiguously to a function template"); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2182 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2183 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2184 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2185 | return ExprError(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 2186 | |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2187 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2188 | // Specifically diagnose references to class templates that are missing |
| 2189 | // a template argument list. |
| 2190 | Diag(Loc, diag::err_template_decl_ref) |
| 2191 | << Template << SS.getRange(); |
| 2192 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2193 | return ExprError(); |
| 2194 | } |
| 2195 | |
| 2196 | // Make sure that we're referring to a value. |
| 2197 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2198 | if (!VD) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2199 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2200 | << D << SS.getRange(); |
John McCall | b48971d | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2201 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2202 | return ExprError(); |
| 2203 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2204 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2205 | // Check whether this declaration can be used. Note that we suppress |
| 2206 | // this check when we're going to perform argument-dependent lookup |
| 2207 | // on this function name, because this might not be the function |
| 2208 | // that overload resolution actually selects. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2209 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2210 | return ExprError(); |
| 2211 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2212 | // Only create DeclRefExpr's for valid Decl's. |
| 2213 | if (VD->isInvalidDecl()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2214 | return ExprError(); |
| 2215 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2216 | // Handle members of anonymous structs and unions. If we got here, |
| 2217 | // and the reference is to a class member indirect field, then this |
| 2218 | // must be the subject of a pointer-to-member expression. |
| 2219 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2220 | if (!indirectField->isCXXClassMember()) |
| 2221 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2222 | indirectField); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2223 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2224 | // If the identifier reference is inside a block, and it refers to a value |
| 2225 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2226 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2227 | // the block is formed. |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2228 | // |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2229 | // We do not do this for things like enum constants, global variables, etc, |
| 2230 | // as they do not get snapshotted. |
| 2231 | // |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2232 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2233 | case CR_Error: |
| 2234 | return ExprError(); |
Mike Stump | 7dafa0d | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2235 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2236 | case CR_Capture: |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2237 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2238 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2239 | |
| 2240 | case CR_CaptureByRef: |
| 2241 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2242 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2243 | |
| 2244 | case CR_NoCapture: { |
| 2245 | // If this reference is not in a block or if the referenced |
| 2246 | // variable is within the block, create a normal DeclRefExpr. |
| 2247 | |
| 2248 | QualType type = VD->getType(); |
Daniel Dunbar | 7c2dc36 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2249 | ExprValueKind valueKind = VK_RValue; |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2250 | |
| 2251 | switch (D->getKind()) { |
| 2252 | // Ignore all the non-ValueDecl kinds. |
| 2253 | #define ABSTRACT_DECL(kind) |
| 2254 | #define VALUE(type, base) |
| 2255 | #define DECL(type, base) \ |
| 2256 | case Decl::type: |
| 2257 | #include "clang/AST/DeclNodes.inc" |
| 2258 | llvm_unreachable("invalid value decl kind"); |
| 2259 | return ExprError(); |
| 2260 | |
| 2261 | // These shouldn't make it here. |
| 2262 | case Decl::ObjCAtDefsField: |
| 2263 | case Decl::ObjCIvar: |
| 2264 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2265 | return ExprError(); |
| 2266 | |
| 2267 | // Enum constants are always r-values and never references. |
| 2268 | // Unresolved using declarations are dependent. |
| 2269 | case Decl::EnumConstant: |
| 2270 | case Decl::UnresolvedUsingValue: |
| 2271 | valueKind = VK_RValue; |
| 2272 | break; |
| 2273 | |
| 2274 | // Fields and indirect fields that got here must be for |
| 2275 | // pointer-to-member expressions; we just call them l-values for |
| 2276 | // internal consistency, because this subexpression doesn't really |
| 2277 | // exist in the high-level semantics. |
| 2278 | case Decl::Field: |
| 2279 | case Decl::IndirectField: |
| 2280 | assert(getLangOptions().CPlusPlus && |
| 2281 | "building reference to field in C?"); |
| 2282 | |
| 2283 | // These can't have reference type in well-formed programs, but |
| 2284 | // for internal consistency we do this anyway. |
| 2285 | type = type.getNonReferenceType(); |
| 2286 | valueKind = VK_LValue; |
| 2287 | break; |
| 2288 | |
| 2289 | // Non-type template parameters are either l-values or r-values |
| 2290 | // depending on the type. |
| 2291 | case Decl::NonTypeTemplateParm: { |
| 2292 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2293 | type = reftype->getPointeeType(); |
| 2294 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2295 | break; |
| 2296 | } |
| 2297 | |
| 2298 | // For non-references, we need to strip qualifiers just in case |
| 2299 | // the template parameter was declared as 'const int' or whatever. |
| 2300 | valueKind = VK_RValue; |
| 2301 | type = type.getUnqualifiedType(); |
| 2302 | break; |
| 2303 | } |
| 2304 | |
| 2305 | case Decl::Var: |
| 2306 | // In C, "extern void blah;" is valid and is an r-value. |
| 2307 | if (!getLangOptions().CPlusPlus && |
| 2308 | !type.hasQualifiers() && |
| 2309 | type->isVoidType()) { |
| 2310 | valueKind = VK_RValue; |
| 2311 | break; |
| 2312 | } |
| 2313 | // fallthrough |
| 2314 | |
| 2315 | case Decl::ImplicitParam: |
| 2316 | case Decl::ParmVar: |
| 2317 | // These are always l-values. |
| 2318 | valueKind = VK_LValue; |
| 2319 | type = type.getNonReferenceType(); |
| 2320 | break; |
| 2321 | |
| 2322 | case Decl::Function: { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2323 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2324 | |
| 2325 | // If we're referring to a function with an __unknown_anytype |
| 2326 | // result type, make the entire expression __unknown_anytype. |
| 2327 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2328 | type = Context.UnknownAnyTy; |
| 2329 | valueKind = VK_RValue; |
| 2330 | break; |
| 2331 | } |
| 2332 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2333 | // Functions are l-values in C++. |
| 2334 | if (getLangOptions().CPlusPlus) { |
| 2335 | valueKind = VK_LValue; |
| 2336 | break; |
| 2337 | } |
| 2338 | |
| 2339 | // C99 DR 316 says that, if a function type comes from a |
| 2340 | // function definition (without a prototype), that type is only |
| 2341 | // used for checking compatibility. Therefore, when referencing |
| 2342 | // the function, we pretend that we don't have the full function |
| 2343 | // type. |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2344 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2345 | isa<FunctionProtoType>(fty)) |
| 2346 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2347 | fty->getExtInfo()); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2348 | |
| 2349 | // Functions are r-values in C. |
| 2350 | valueKind = VK_RValue; |
| 2351 | break; |
| 2352 | } |
| 2353 | |
| 2354 | case Decl::CXXMethod: |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2355 | // If we're referring to a method with an __unknown_anytype |
| 2356 | // result type, make the entire expression __unknown_anytype. |
| 2357 | // This should only be possible with a type written directly. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 2358 | if (const FunctionProtoType *proto |
| 2359 | = dyn_cast<FunctionProtoType>(VD->getType())) |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2360 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2361 | type = Context.UnknownAnyTy; |
| 2362 | valueKind = VK_RValue; |
| 2363 | break; |
| 2364 | } |
| 2365 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2366 | // C++ methods are l-values if static, r-values if non-static. |
| 2367 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2368 | valueKind = VK_LValue; |
| 2369 | break; |
| 2370 | } |
| 2371 | // fallthrough |
| 2372 | |
| 2373 | case Decl::CXXConversion: |
| 2374 | case Decl::CXXDestructor: |
| 2375 | case Decl::CXXConstructor: |
| 2376 | valueKind = VK_RValue; |
| 2377 | break; |
| 2378 | } |
| 2379 | |
| 2380 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2381 | } |
| 2382 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2383 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2384 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2385 | llvm_unreachable("unknown capture result"); |
| 2386 | return ExprError(); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 2387 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2388 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2389 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2390 | PredefinedExpr::IdentType IT; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2391 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2392 | switch (Kind) { |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2393 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2394 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2395 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2396 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2397 | } |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2398 | |
Chris Lattner | a81a027 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2399 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2400 | // string. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2401 | |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2402 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | 9462744 | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2403 | if (!currentDecl && getCurBlock()) |
| 2404 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2405 | if (!currentDecl) { |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2406 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2407 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2408 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2409 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2410 | QualType ResTy; |
| 2411 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2412 | ResTy = Context.DependentTy; |
| 2413 | } else { |
Anders Carlsson | 5bd8d19 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2414 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2415 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2416 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2417 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2418 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2419 | } |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2420 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2423 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2424 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2425 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2426 | StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2427 | if (Invalid) |
| 2428 | return ExprError(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2429 | |
Benjamin Kramer | 0a1abd4 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2430 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2431 | PP, Tok.getKind()); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2432 | if (Literal.hadError()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2433 | return ExprError(); |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2434 | |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2435 | QualType Ty; |
| 2436 | if (!getLangOptions().CPlusPlus) |
| 2437 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2438 | else if (Literal.isWide()) |
| 2439 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2440 | else if (Literal.isUTF16()) |
| 2441 | Ty = Context.Char16Ty; // u'x' -> char16_t in C++0x. |
| 2442 | else if (Literal.isUTF32()) |
| 2443 | Ty = Context.Char32Ty; // U'x' -> char32_t in C++0x. |
Eli Friedman | eb1df70 | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2444 | else if (Literal.isMultiChar()) |
| 2445 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2446 | else |
| 2447 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2448 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2449 | CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; |
| 2450 | if (Literal.isWide()) |
| 2451 | Kind = CharacterLiteral::Wide; |
| 2452 | else if (Literal.isUTF16()) |
| 2453 | Kind = CharacterLiteral::UTF16; |
| 2454 | else if (Literal.isUTF32()) |
| 2455 | Kind = CharacterLiteral::UTF32; |
| 2456 | |
| 2457 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, |
| 2458 | Tok.getLocation())); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2461 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2462 | // Fast path for a single digit (which is quite common). A single digit |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2463 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2464 | if (Tok.getLength() == 1) { |
Chris Lattner | 9240b3e | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2465 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2466 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2467 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 5faaef7 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2468 | Context.IntTy, Tok.getLocation())); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2469 | } |
Ted Kremenek | e981418 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2470 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2471 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | a1cf5f9 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2472 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2473 | IntegerBuffer.resize(Tok.getLength()+1); |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 2474 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2475 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2476 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2477 | bool Invalid = false; |
| 2478 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2479 | if (Invalid) |
| 2480 | return ExprError(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2481 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2482 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 2483 | Tok.getLocation(), PP); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2484 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2485 | return ExprError(); |
| 2486 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2487 | Expr *Res; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2488 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2489 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2490 | QualType Ty; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2491 | if (Literal.isFloat) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2492 | Ty = Context.FloatTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2493 | else if (!Literal.isLong) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2494 | Ty = Context.DoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2495 | else |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2496 | Ty = Context.LongDoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2497 | |
| 2498 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2499 | |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2500 | using llvm::APFloat; |
| 2501 | APFloat Val(Format); |
| 2502 | |
| 2503 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 122c831 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2504 | |
| 2505 | // Overflow is always an error, but underflow is only an error if |
| 2506 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2507 | if ((result & APFloat::opOverflow) || |
| 2508 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2509 | unsigned diagnostic; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2510 | llvm::SmallString<20> buffer; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2511 | if (result & APFloat::opOverflow) { |
John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2512 | diagnostic = diag::warn_float_overflow; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2513 | APFloat::getLargest(Format).toString(buffer); |
| 2514 | } else { |
John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2515 | diagnostic = diag::warn_float_underflow; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2516 | APFloat::getSmallest(Format).toString(buffer); |
| 2517 | } |
| 2518 | |
| 2519 | Diag(Tok.getLocation(), diagnostic) |
| 2520 | << Ty |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2521 | << StringRef(buffer.data(), buffer.size()); |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2525 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2526 | |
Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2527 | if (Ty == Context.DoubleTy) { |
| 2528 | if (getLangOptions().SinglePrecisionConstants) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2529 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2530 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2531 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2532 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2533 | } |
| 2534 | } |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2535 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2536 | return ExprError(); |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2537 | } else { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2538 | QualType Ty; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2539 | |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2540 | // long long is a C99 feature. |
| 2541 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 4a1ee05 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2542 | Literal.isLongLong) |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2543 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2544 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2545 | // Get the value in the widest-possible width. |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2546 | llvm::APInt ResultVal(Context.getTargetInfo().getIntMaxTWidth(), 0); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2547 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2548 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2549 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2550 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2551 | Ty = Context.UnsignedLongLongTy; |
| 2552 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2553 | "long long is not intmax_t?"); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2554 | } else { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2555 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2556 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2557 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2558 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2559 | // be an unsigned int. |
| 2560 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2561 | |
| 2562 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2563 | unsigned Width = 0; |
Chris Lattner | 7b939cf | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2564 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2565 | // Are int/unsigned possibilities? |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2566 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2567 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2568 | // Does it fit in a unsigned int? |
| 2569 | if (ResultVal.isIntN(IntSize)) { |
| 2570 | // Does it fit in a signed int? |
| 2571 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2572 | Ty = Context.IntTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2573 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2574 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2575 | Width = IntSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2576 | } |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2577 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2578 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2579 | // Are long/unsigned long possibilities? |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2580 | if (Ty.isNull() && !Literal.isLongLong) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2581 | unsigned LongSize = Context.getTargetInfo().getLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2582 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2583 | // Does it fit in a unsigned long? |
| 2584 | if (ResultVal.isIntN(LongSize)) { |
| 2585 | // Does it fit in a signed long? |
| 2586 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2587 | Ty = Context.LongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2588 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2589 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2590 | Width = LongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2591 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2594 | // Finally, check long long if needed. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2595 | if (Ty.isNull()) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2596 | unsigned LongLongSize = Context.getTargetInfo().getLongLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2597 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2598 | // Does it fit in a unsigned long long? |
| 2599 | if (ResultVal.isIntN(LongLongSize)) { |
| 2600 | // Does it fit in a signed long long? |
Francois Pichet | c3e73b3 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2601 | // To be compatible with MSVC, hex integer literals ending with the |
| 2602 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | bf711d9 | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2603 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2604 | (getLangOptions().MicrosoftExt && Literal.isLongLong))) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2605 | Ty = Context.LongLongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2606 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2607 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2608 | Width = LongLongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2609 | } |
| 2610 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2611 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2612 | // If we still couldn't decide a type, we probably have something that |
| 2613 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2614 | if (Ty.isNull()) { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2615 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2616 | Ty = Context.UnsignedLongLongTy; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2617 | Width = Context.getTargetInfo().getLongLongWidth(); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2618 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2619 | |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2620 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2621 | ResultVal = ResultVal.trunc(Width); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2622 | } |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2623 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2624 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2625 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2626 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2627 | if (Literal.isImaginary) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2628 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2629 | Context.getComplexType(Res->getType())); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2630 | |
| 2631 | return Owned(Res); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2634 | ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2635 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2636 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Chandler Carruth | 62da79c | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2639 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 2640 | SourceLocation Loc, |
| 2641 | SourceRange ArgRange) { |
| 2642 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 2643 | // scalar or vector data type argument..." |
| 2644 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 2645 | // type (C99 6.2.5p18) or void. |
| 2646 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 2647 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 2648 | << T << ArgRange; |
| 2649 | return true; |
| 2650 | } |
| 2651 | |
| 2652 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 2653 | "Scalar types should always be complete"); |
| 2654 | return false; |
| 2655 | } |
| 2656 | |
Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2657 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 2658 | SourceLocation Loc, |
| 2659 | SourceRange ArgRange, |
| 2660 | UnaryExprOrTypeTrait TraitKind) { |
| 2661 | // C99 6.5.3.4p1: |
| 2662 | if (T->isFunctionType()) { |
| 2663 | // alignof(function) is allowed as an extension. |
| 2664 | if (TraitKind == UETT_SizeOf) |
| 2665 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 2666 | return false; |
| 2667 | } |
| 2668 | |
| 2669 | // Allow sizeof(void)/alignof(void) as an extension. |
| 2670 | if (T->isVoidType()) { |
| 2671 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 2672 | return false; |
| 2673 | } |
| 2674 | |
| 2675 | return true; |
| 2676 | } |
| 2677 | |
| 2678 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 2679 | SourceLocation Loc, |
| 2680 | SourceRange ArgRange, |
| 2681 | UnaryExprOrTypeTrait TraitKind) { |
| 2682 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
| 2683 | if (S.LangOpts.ObjCNonFragileABI && T->isObjCObjectType()) { |
| 2684 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 2685 | << T << (TraitKind == UETT_SizeOf) |
| 2686 | << ArgRange; |
| 2687 | return true; |
| 2688 | } |
| 2689 | |
| 2690 | return false; |
| 2691 | } |
| 2692 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2693 | /// \brief Check the constrains on expression operands to unary type expression |
| 2694 | /// and type traits. |
| 2695 | /// |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2696 | /// Completes any types necessary and validates the constraints on the operand |
| 2697 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 2698 | /// the expression as it completes the type for that expression through template |
| 2699 | /// instantiation, etc. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2700 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E, |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2701 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2702 | QualType ExprTy = E->getType(); |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2703 | |
| 2704 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2705 | // the result is the size of the referenced type." |
| 2706 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2707 | // result shall be the alignment of the referenced type." |
| 2708 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2709 | ExprTy = Ref->getPointeeType(); |
| 2710 | |
| 2711 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2712 | return CheckVecStepTraitOperandType(*this, ExprTy, E->getExprLoc(), |
| 2713 | E->getSourceRange()); |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2714 | |
| 2715 | // Whitelist some types as extensions |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2716 | if (!CheckExtensionTraitOperandType(*this, ExprTy, E->getExprLoc(), |
| 2717 | E->getSourceRange(), ExprKind)) |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2718 | return false; |
| 2719 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2720 | if (RequireCompleteExprType(E, |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2721 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2722 | << ExprKind << E->getSourceRange(), |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2723 | std::make_pair(SourceLocation(), PDiag(0)))) |
| 2724 | return true; |
| 2725 | |
| 2726 | // Completeing the expression's type may have changed it. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2727 | ExprTy = E->getType(); |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2728 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2729 | ExprTy = Ref->getPointeeType(); |
| 2730 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2731 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, E->getExprLoc(), |
| 2732 | E->getSourceRange(), ExprKind)) |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2733 | return true; |
| 2734 | |
Nico Weber | 0870deb | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2735 | if (ExprKind == UETT_SizeOf) { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2736 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { |
Nico Weber | 0870deb | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2737 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 2738 | QualType OType = PVD->getOriginalType(); |
| 2739 | QualType Type = PVD->getType(); |
| 2740 | if (Type->isPointerType() && OType->isArrayType()) { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2741 | Diag(E->getExprLoc(), diag::warn_sizeof_array_param) |
Nico Weber | 0870deb | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2742 | << Type << OType; |
| 2743 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 2744 | } |
| 2745 | } |
| 2746 | } |
| 2747 | } |
| 2748 | |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2749 | return false; |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
| 2752 | /// \brief Check the constraints on operands to unary expression and type |
| 2753 | /// traits. |
| 2754 | /// |
| 2755 | /// This will complete any types necessary, and validate the various constraints |
| 2756 | /// on those operands. |
| 2757 | /// |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 2758 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2759 | /// C99 6.3.2.1p[2-4] all state: |
| 2760 | /// Except when it is the operand of the sizeof operator ... |
| 2761 | /// |
| 2762 | /// C++ [expr.sizeof]p4 |
| 2763 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 2764 | /// standard conversions are not applied to the operand of sizeof. |
| 2765 | /// |
| 2766 | /// This policy is followed for all of the unary trait expressions. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2767 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2768 | SourceLocation OpLoc, |
| 2769 | SourceRange ExprRange, |
| 2770 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2771 | if (ExprType->isDependentType()) |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2772 | return false; |
| 2773 | |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2774 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2775 | // the result is the size of the referenced type." |
| 2776 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2777 | // result shall be the alignment of the referenced type." |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2778 | if (const ReferenceType *Ref = ExprType->getAs<ReferenceType>()) |
| 2779 | ExprType = Ref->getPointeeType(); |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2780 | |
Chandler Carruth | 62da79c | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2781 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2782 | return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2783 | |
Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2784 | // Whitelist some types as extensions |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2785 | if (!CheckExtensionTraitOperandType(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2786 | ExprKind)) |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2787 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2788 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2789 | if (RequireCompleteType(OpLoc, ExprType, |
Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 2790 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2791 | << ExprKind << ExprRange)) |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2792 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2793 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2794 | if (CheckObjCTraitOperandConstraints(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2795 | ExprKind)) |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2796 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2797 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2798 | return false; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2801 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2802 | E = E->IgnoreParens(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2803 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | // alignof decl is always ok. |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2805 | if (isa<DeclRefExpr>(E)) |
| 2806 | return false; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2807 | |
| 2808 | // Cannot know anything else if the expression is dependent. |
| 2809 | if (E->isTypeDependent()) |
| 2810 | return false; |
| 2811 | |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2812 | if (E->getBitField()) { |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2813 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 2814 | << 1 << E->getSourceRange(); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2815 | return true; |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2816 | } |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2817 | |
| 2818 | // Alignment of a field access is always okay, so long as it isn't a |
| 2819 | // bit-field. |
| 2820 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2821 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2822 | return false; |
| 2823 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2824 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2827 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2828 | E = E->IgnoreParens(); |
| 2829 | |
| 2830 | // Cannot know anything else if the expression is dependent. |
| 2831 | if (E->isTypeDependent()) |
| 2832 | return false; |
| 2833 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2834 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2837 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2838 | ExprResult |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2839 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 2840 | SourceLocation OpLoc, |
| 2841 | UnaryExprOrTypeTrait ExprKind, |
| 2842 | SourceRange R) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2843 | if (!TInfo) |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2844 | return ExprError(); |
| 2845 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2846 | QualType T = TInfo->getType(); |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2847 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2848 | if (!T->isDependentType() && |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2849 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2850 | return ExprError(); |
| 2851 | |
| 2852 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2853 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 2854 | Context.getSizeType(), |
| 2855 | OpLoc, R.getEnd())); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | /// \brief Build a sizeof or alignof expression given an expression |
| 2859 | /// operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2860 | ExprResult |
Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2861 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 2862 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | 835af98 | 2011-06-22 23:21:00 +0000 | [diff] [blame] | 2863 | ExprResult PE = CheckPlaceholderExpr(E); |
| 2864 | if (PE.isInvalid()) |
| 2865 | return ExprError(); |
| 2866 | |
| 2867 | E = PE.get(); |
| 2868 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2869 | // Verify that the operand is valid. |
| 2870 | bool isInvalid = false; |
| 2871 | if (E->isTypeDependent()) { |
| 2872 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2873 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2874 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2875 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2876 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2877 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2878 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2879 | isInvalid = true; |
| 2880 | } else { |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2881 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | if (isInvalid) |
| 2885 | return ExprError(); |
| 2886 | |
| 2887 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2888 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2889 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2890 | E->getSourceRange().getEnd())); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2893 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 2894 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2895 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2896 | ExprResult |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2897 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2898 | UnaryExprOrTypeTrait ExprKind, bool IsType, |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2899 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 2900 | // If error parsing type, ignore. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2901 | if (TyOrEx == 0) return ExprError(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 2902 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2903 | if (IsType) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2904 | TypeSourceInfo *TInfo; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2905 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2906 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | } |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2908 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2909 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2910 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2911 | return move(Result); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2914 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2915 | bool IsReal) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2916 | if (V.get()->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2917 | return S.Context.DependentTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2918 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2919 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2920 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 2921 | V = S.DefaultLvalueConversion(V.take()); |
| 2922 | if (V.isInvalid()) |
| 2923 | return QualType(); |
| 2924 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2925 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2926 | // These operators return the element type of a complex type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2927 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2928 | return CT->getElementType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2929 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2930 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2931 | if (V.get()->getType()->isArithmeticType()) |
| 2932 | return V.get()->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2933 | |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2934 | // Test for placeholders. |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 2935 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2936 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2937 | if (PR.get() != V.get()) { |
| 2938 | V = move(PR); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2939 | return CheckRealImagOperand(S, V, Loc, IsReal); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2942 | // Reject anything else. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2943 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2944 | << (IsReal ? "__real" : "__imag"); |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2945 | return QualType(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2949 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2950 | ExprResult |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2951 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2952 | tok::TokenKind Kind, Expr *Input) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2953 | UnaryOperatorKind Opc; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2954 | switch (Kind) { |
| 2955 | default: assert(0 && "Unknown unary op!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2956 | case tok::plusplus: Opc = UO_PostInc; break; |
| 2957 | case tok::minusminus: Opc = UO_PostDec; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2958 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2959 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2960 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2963 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2964 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 2965 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2966 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2967 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2968 | if (Result.isInvalid()) return ExprError(); |
| 2969 | Base = Result.take(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2970 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2971 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2972 | |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2973 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2974 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2975 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2976 | Context.DependentTy, |
| 2977 | VK_LValue, OK_Ordinary, |
| 2978 | RLoc)); |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2981 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2982 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 254a1a2 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 2983 | LHSExp->getType()->isEnumeralType() || |
| 2984 | RHSExp->getType()->isRecordType() || |
| 2985 | RHSExp->getType()->isEnumeralType())) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2986 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2989 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2993 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2994 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2995 | Expr *Idx, SourceLocation RLoc) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2996 | Expr *LHSExp = Base; |
| 2997 | Expr *RHSExp = Idx; |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2998 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2999 | // Perform default conversions. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3000 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3001 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3002 | if (Result.isInvalid()) |
| 3003 | return ExprError(); |
| 3004 | LHSExp = Result.take(); |
| 3005 | } |
| 3006 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3007 | if (Result.isInvalid()) |
| 3008 | return ExprError(); |
| 3009 | RHSExp = Result.take(); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3010 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3011 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3012 | ExprValueKind VK = VK_LValue; |
| 3013 | ExprObjectKind OK = OK_Ordinary; |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 3014 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 3015 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3016 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3017 | // in the subscript position. As a result, we need to derive the array base |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 3018 | // and index from the expression types. |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3019 | Expr *BaseExpr, *IndexExpr; |
| 3020 | QualType ResultType; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3021 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3022 | BaseExpr = LHSExp; |
| 3023 | IndexExpr = RHSExp; |
| 3024 | ResultType = Context.DependentTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3025 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3026 | BaseExpr = LHSExp; |
| 3027 | IndexExpr = RHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3028 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3029 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | aee0cfd | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3030 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3031 | BaseExpr = RHSExp; |
| 3032 | IndexExpr = LHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3033 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3034 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3035 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3036 | BaseExpr = LHSExp; |
| 3037 | IndexExpr = RHSExp; |
| 3038 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3039 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3040 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3041 | // Handle the uncommon case of "123[Ptr]". |
| 3042 | BaseExpr = RHSExp; |
| 3043 | IndexExpr = LHSExp; |
| 3044 | ResultType = PTy->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3045 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3046 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3047 | IndexExpr = RHSExp; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3048 | VK = LHSExp->getValueKind(); |
| 3049 | if (VK != VK_RValue) |
| 3050 | OK = OK_VectorComponent; |
Nate Begeman | c1bf061 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3051 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3052 | // FIXME: need to deal with const... |
| 3053 | ResultType = VTy->getElementType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3054 | } else if (LHSTy->isArrayType()) { |
| 3055 | // If we see an array that wasn't promoted by |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3056 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3057 | // wasn't promoted because of the C90 rule that doesn't |
| 3058 | // allow promoting non-lvalue arrays. Warn, then |
| 3059 | // force the promotion here. |
| 3060 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3061 | LHSExp->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3062 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3063 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3064 | LHSTy = LHSExp->getType(); |
| 3065 | |
| 3066 | BaseExpr = LHSExp; |
| 3067 | IndexExpr = RHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3068 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3069 | } else if (RHSTy->isArrayType()) { |
| 3070 | // Same as previous, except for 123[f().a] case |
| 3071 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3072 | RHSExp->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3073 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3074 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3075 | RHSTy = RHSExp->getType(); |
| 3076 | |
| 3077 | BaseExpr = RHSExp; |
| 3078 | IndexExpr = LHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3079 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 3080 | } else { |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3081 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3082 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3083 | } |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 3084 | // C99 6.5.2.1p1 |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3085 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3086 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3087 | << IndexExpr->getSourceRange()); |
Steve Naroff | b29cdd5 | 2007-07-10 18:23:31 +0000 | [diff] [blame] | 3088 | |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3089 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | b7608d7 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3090 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3091 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 914244e | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3092 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3093 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3094 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3095 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3096 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3097 | // incomplete types are not object types. |
| 3098 | if (ResultType->isFunctionType()) { |
| 3099 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3100 | << ResultType << BaseExpr->getSourceRange(); |
| 3101 | return ExprError(); |
| 3102 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | |
Abramo Bagnara | 3aabb4b | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3104 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3105 | // GNU extension: subscripting on pointer to void |
Chandler Carruth | 4cc3f29 | 2011-06-27 16:32:27 +0000 | [diff] [blame] | 3106 | Diag(LLoc, diag::ext_gnu_subscript_void_type) |
| 3107 | << BaseExpr->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3108 | |
| 3109 | // C forbids expressions of unqualified void type from being l-values. |
| 3110 | // See IsCForbiddenLValueType. |
| 3111 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 3aabb4b | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3112 | } else if (!ResultType->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3113 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3114 | PDiag(diag::err_subscript_incomplete_type) |
| 3115 | << BaseExpr->getSourceRange())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3116 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3118 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3119 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3120 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3121 | << ResultType << BaseExpr->getSourceRange(); |
| 3122 | return ExprError(); |
| 3123 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3124 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3125 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 3126 | !ResultType.isCForbiddenLValueType()); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3127 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3128 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3129 | ResultType, VK, OK, RLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3132 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3133 | FunctionDecl *FD, |
| 3134 | ParmVarDecl *Param) { |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3135 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3136 | Diag(CallLoc, |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3137 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3138 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3139 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3140 | diag::note_default_argument_declared_here); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3141 | return ExprError(); |
| 3142 | } |
| 3143 | |
| 3144 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3145 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3146 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3147 | // Instantiate the expression. |
| 3148 | MultiLevelTemplateArgumentList ArgList |
| 3149 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3150 | |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3151 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3152 | = ArgList.getInnermost(); |
| 3153 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 3154 | Innermost.second); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3155 | |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3156 | ExprResult Result; |
| 3157 | { |
| 3158 | // C++ [dcl.fct.default]p5: |
| 3159 | // The names in the [default argument] expression are bound, and |
| 3160 | // the semantic constraints are checked, at the point where the |
| 3161 | // default argument expression appears. |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3162 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3163 | Result = SubstExpr(UninstExpr, ArgList); |
| 3164 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3165 | if (Result.isInvalid()) |
| 3166 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3167 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3168 | // Check the expression as an initializer for the parameter. |
| 3169 | InitializedEntity Entity |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3170 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3171 | InitializationKind Kind |
| 3172 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 3173 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 3174 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 25ab25f | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3175 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3176 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 3177 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 3178 | MultiExprArg(*this, &ResultE, 1)); |
| 3179 | if (Result.isInvalid()) |
| 3180 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3181 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3182 | // Build the default argument expression. |
| 3183 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 3184 | Result.takeAs<Expr>())); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3187 | // If the default expression creates temporaries, we need to |
| 3188 | // push them to the current stack of expression temporaries so they'll |
| 3189 | // be properly destroyed. |
| 3190 | // FIXME: We should really be rebuilding the default argument with new |
| 3191 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3192 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 3193 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 3194 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 3195 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 3196 | ExprTemporaries.push_back(Temporary); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3197 | ExprNeedsCleanups = true; |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3198 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3199 | |
| 3200 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 3201 | // Just mark all of the declarations in this potentially-evaluated expression |
| 3202 | // as being "referenced". |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3203 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3204 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3207 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3208 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3209 | /// function prototype Proto. Call is the call expression itself, and |
| 3210 | /// Fn is the function expression. For a C++ member function, this |
| 3211 | /// routine does not attempt to convert the object argument. Returns |
| 3212 | /// true if the call is ill-formed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3213 | bool |
| 3214 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3215 | FunctionDecl *FDecl, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3216 | const FunctionProtoType *Proto, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3217 | Expr **Args, unsigned NumArgs, |
| 3218 | SourceLocation RParenLoc) { |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3219 | // Bail out early if calling a builtin with custom typechecking. |
| 3220 | // We don't need to do this in the |
| 3221 | if (FDecl) |
| 3222 | if (unsigned ID = FDecl->getBuiltinID()) |
| 3223 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 3224 | return false; |
| 3225 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3226 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3227 | // assignment, to the types of the corresponding parameter, ... |
| 3228 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3229 | bool Invalid = false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3230 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3231 | // If too few arguments are available (and we don't have default |
| 3232 | // arguments for the remaining parameters), don't make the call. |
| 3233 | if (NumArgs < NumArgsInProto) { |
Peter Collingbourne | 3bc84ca | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3234 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) { |
| 3235 | Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3236 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 3237 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Peter Collingbourne | 3bc84ca | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3238 | |
| 3239 | // Emit the location of the prototype. |
| 3240 | if (FDecl && !FDecl->getBuiltinID()) |
| 3241 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3242 | << FDecl; |
| 3243 | |
| 3244 | return true; |
| 3245 | } |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3246 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | // If too many are passed and not variadic, error on the extras and drop |
| 3250 | // them. |
| 3251 | if (NumArgs > NumArgsInProto) { |
| 3252 | if (!Proto->isVariadic()) { |
| 3253 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3254 | diag::err_typecheck_call_too_many_args) |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3255 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 3256 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3257 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3258 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 99a337e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3259 | |
| 3260 | // Emit the location of the prototype. |
| 3261 | if (FDecl && !FDecl->getBuiltinID()) |
Peter Collingbourne | 3bc84ca | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3262 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3263 | << FDecl; |
Ted Kremenek | 99a337e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3264 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3265 | // This deletes the extra arguments. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3266 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3267 | return true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3268 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3269 | } |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3270 | SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3271 | VariadicCallType CallType = |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3272 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3273 | if (Fn->getType()->isBlockPointerType()) |
| 3274 | CallType = VariadicBlock; // Block |
| 3275 | else if (isa<MemberExpr>(Fn)) |
| 3276 | CallType = VariadicMethod; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3277 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3278 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3279 | if (Invalid) |
| 3280 | return true; |
| 3281 | unsigned TotalNumArgs = AllArgs.size(); |
| 3282 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3283 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3284 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3285 | return false; |
| 3286 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3287 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3288 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3289 | FunctionDecl *FDecl, |
| 3290 | const FunctionProtoType *Proto, |
| 3291 | unsigned FirstProtoArg, |
| 3292 | Expr **Args, unsigned NumArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3293 | SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3294 | VariadicCallType CallType) { |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3295 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3296 | unsigned NumArgsToCheck = NumArgs; |
| 3297 | bool Invalid = false; |
| 3298 | if (NumArgs != NumArgsInProto) |
| 3299 | // Use default arguments for missing arguments |
| 3300 | NumArgsToCheck = NumArgsInProto; |
| 3301 | unsigned ArgIx = 0; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3302 | // Continue to check argument types (even if we have too few/many args). |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3303 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3304 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3305 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3306 | Expr *Arg; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3307 | if (ArgIx < NumArgs) { |
| 3308 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3309 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3310 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3311 | ProtoArgType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3312 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3313 | << Arg->getSourceRange())) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3314 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3315 | |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3316 | // Pass the argument |
| 3317 | ParmVarDecl *Param = 0; |
| 3318 | if (FDecl && i < FDecl->getNumParams()) |
| 3319 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3320 | |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3321 | InitializedEntity Entity = |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3322 | Param? InitializedEntity::InitializeParameter(Context, Param) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3323 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
| 3324 | Proto->isArgConsumed(i)); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3325 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3326 | SourceLocation(), |
| 3327 | Owned(Arg)); |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3328 | if (ArgE.isInvalid()) |
| 3329 | return true; |
| 3330 | |
| 3331 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3332 | } else { |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 3333 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3334 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3335 | ExprResult ArgExpr = |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3336 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3337 | if (ArgExpr.isInvalid()) |
| 3338 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3339 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3340 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3341 | } |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3342 | |
| 3343 | // Check for array bounds violations for each argument to the call. This |
| 3344 | // check only triggers warnings when the argument isn't a more complex Expr |
| 3345 | // with its own checking, such as a BinaryOperator. |
| 3346 | CheckArrayAccess(Arg); |
| 3347 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3348 | AllArgs.push_back(Arg); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3349 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3350 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3351 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3352 | if (CallType != VariadicDoesNotApply) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3353 | |
| 3354 | // Assume that extern "C" functions with variadic arguments that |
| 3355 | // return __unknown_anytype aren't *really* variadic. |
| 3356 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 3357 | FDecl && FDecl->isExternC()) { |
| 3358 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 3359 | ExprResult arg; |
| 3360 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 3361 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 3362 | else |
| 3363 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 3364 | Invalid |= arg.isInvalid(); |
| 3365 | AllArgs.push_back(arg.take()); |
| 3366 | } |
| 3367 | |
| 3368 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 3369 | } else { |
| 3370 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3371 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, |
| 3372 | FDecl); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3373 | Invalid |= Arg.isInvalid(); |
| 3374 | AllArgs.push_back(Arg.take()); |
| 3375 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3376 | } |
| 3377 | } |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3378 | return Invalid; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3381 | /// Given a function expression of unknown-any type, try to rebuild it |
| 3382 | /// to have a function type. |
| 3383 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 3384 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3385 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3386 | /// This provides the location of the left/right parens and a list of comma |
| 3387 | /// locations. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3388 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3389 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3390 | MultiExprArg ArgExprs, SourceLocation RParenLoc, |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3391 | Expr *ExecConfig) { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3392 | unsigned NumArgs = ArgExprs.size(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3393 | |
| 3394 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3395 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3396 | if (Result.isInvalid()) return ExprError(); |
| 3397 | Fn = Result.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3398 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3399 | Expr **Args = ArgExprs.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3401 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3402 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3403 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3404 | if (NumArgs > 0) { |
| 3405 | // Pseudo-destructor calls should not have any arguments. |
| 3406 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3407 | << FixItHint::CreateRemoval( |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3408 | SourceRange(Args[0]->getLocStart(), |
| 3409 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3410 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3411 | NumArgs = 0; |
| 3412 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3414 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3415 | VK_RValue, RParenLoc)); |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3416 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3417 | |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3418 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3419 | // in which case we won't do any semantic analysis now. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3420 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3421 | // Fn. |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3422 | bool Dependent = false; |
| 3423 | if (Fn->isTypeDependent()) |
| 3424 | Dependent = true; |
| 3425 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 3426 | Dependent = true; |
| 3427 | |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3428 | if (Dependent) { |
| 3429 | if (ExecConfig) { |
| 3430 | return Owned(new (Context) CUDAKernelCallExpr( |
| 3431 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 3432 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 3433 | } else { |
| 3434 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 3435 | Context.DependentTy, VK_RValue, |
| 3436 | RParenLoc)); |
| 3437 | } |
| 3438 | } |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3439 | |
| 3440 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3441 | if (Fn->getType()->isRecordType()) |
| 3442 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3443 | RParenLoc)); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3444 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3445 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3446 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3447 | if (result.isInvalid()) return ExprError(); |
| 3448 | Fn = result.take(); |
| 3449 | } |
| 3450 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3451 | if (Fn->getType() == Context.BoundMemberTy) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3452 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3453 | RParenLoc); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3454 | } |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3455 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3456 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3457 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 3458 | if (Fn->getType() == Context.OverloadTy) { |
| 3459 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 3460 | |
| 3461 | // We aren't supposed to apply this logic if there's an '&' involved. |
| 3462 | if (!find.IsAddressOfOperand) { |
| 3463 | OverloadExpr *ovl = find.Expression; |
| 3464 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 3465 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
| 3466 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 3467 | RParenLoc, ExecConfig); |
| 3468 | } else { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3469 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3470 | RParenLoc); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3471 | } |
| 3472 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3475 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3476 | |
Eli Friedman | e14b199 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3477 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | 928479e | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 3478 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3479 | NamedDecl *NDecl = 0; |
Douglas Gregor | 59f16ed | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 3480 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 3481 | if (UnOp->getOpcode() == UO_AddrOf) |
| 3482 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 3483 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3484 | if (isa<DeclRefExpr>(NakedFn)) |
| 3485 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3486 | else if (isa<MemberExpr>(NakedFn)) |
| 3487 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3488 | |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3489 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 3490 | ExecConfig); |
| 3491 | } |
| 3492 | |
| 3493 | ExprResult |
| 3494 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3495 | MultiExprArg ExecConfig, SourceLocation GGGLoc) { |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3496 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 3497 | if (!ConfigDecl) |
| 3498 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 3499 | << "cudaConfigureCall"); |
| 3500 | QualType ConfigQTy = ConfigDecl->getType(); |
| 3501 | |
| 3502 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 3503 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 3504 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3505 | return ActOnCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, 0); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3508 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 3509 | /// |
| 3510 | /// __builtin_astype( value, dst type ) |
| 3511 | /// |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3512 | ExprResult Sema::ActOnAsTypeExpr(Expr *E, ParsedType ParsedDestTy, |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3513 | SourceLocation BuiltinLoc, |
| 3514 | SourceLocation RParenLoc) { |
| 3515 | ExprValueKind VK = VK_RValue; |
| 3516 | ExprObjectKind OK = OK_Ordinary; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3517 | QualType DstTy = GetTypeFromParser(ParsedDestTy); |
| 3518 | QualType SrcTy = E->getType(); |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3519 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 3520 | return ExprError(Diag(BuiltinLoc, |
| 3521 | diag::err_invalid_astype_of_different_size) |
Peter Collingbourne | 23f1bee | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 3522 | << DstTy |
| 3523 | << SrcTy |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3524 | << E->getSourceRange()); |
| 3525 | return Owned(new (Context) AsTypeExpr(E, DstTy, VK, OK, BuiltinLoc, |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3526 | RParenLoc)); |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3529 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3530 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3531 | /// unary-convert to an expression of function-pointer or |
| 3532 | /// block-pointer type. |
| 3533 | /// |
| 3534 | /// \param NDecl the declaration being called, if available |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3535 | ExprResult |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3536 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3537 | SourceLocation LParenLoc, |
| 3538 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3539 | SourceLocation RParenLoc, |
| 3540 | Expr *Config) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3541 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3542 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3543 | // Promote the function operand. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3544 | ExprResult Result = UsualUnaryConversions(Fn); |
| 3545 | if (Result.isInvalid()) |
| 3546 | return ExprError(); |
| 3547 | Fn = Result.take(); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3548 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3549 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3550 | // of arguments and function on error. |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3551 | CallExpr *TheCall; |
| 3552 | if (Config) { |
| 3553 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 3554 | cast<CallExpr>(Config), |
| 3555 | Args, NumArgs, |
| 3556 | Context.BoolTy, |
| 3557 | VK_RValue, |
| 3558 | RParenLoc); |
| 3559 | } else { |
| 3560 | TheCall = new (Context) CallExpr(Context, Fn, |
| 3561 | Args, NumArgs, |
| 3562 | Context.BoolTy, |
| 3563 | VK_RValue, |
| 3564 | RParenLoc); |
| 3565 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3566 | |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3567 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 3568 | |
| 3569 | // Bail out early if calling a builtin with custom typechecking. |
| 3570 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 3571 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 3572 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3573 | retry: |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3574 | const FunctionType *FuncT; |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3575 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3576 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3577 | // have type pointer to function". |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3578 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3579 | if (FuncT == 0) |
| 3580 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3581 | << Fn->getType() << Fn->getSourceRange()); |
| 3582 | } else if (const BlockPointerType *BPT = |
| 3583 | Fn->getType()->getAs<BlockPointerType>()) { |
| 3584 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 3585 | } else { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3586 | // Handle calls to expressions of unknown-any type. |
| 3587 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3588 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3589 | if (rewrite.isInvalid()) return ExprError(); |
| 3590 | Fn = rewrite.take(); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3591 | TheCall->setCallee(Fn); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3592 | goto retry; |
| 3593 | } |
| 3594 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3595 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3596 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3597 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3598 | |
Peter Collingbourne | 4b66c47 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 3599 | if (getLangOptions().CUDA) { |
| 3600 | if (Config) { |
| 3601 | // CUDA: Kernel calls must be to global functions |
| 3602 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 3603 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 3604 | << FDecl->getName() << Fn->getSourceRange()); |
| 3605 | |
| 3606 | // CUDA: Kernel function must have 'void' return type |
| 3607 | if (!FuncT->getResultType()->isVoidType()) |
| 3608 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 3609 | << Fn->getType() << Fn->getSourceRange()); |
| 3610 | } |
| 3611 | } |
| 3612 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3613 | // Check for a valid return type |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3614 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3615 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3616 | FDecl)) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3617 | return ExprError(); |
| 3618 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3619 | // We know the result type of the call, set it. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3620 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3621 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3622 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3623 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3624 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3625 | RParenLoc)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3626 | return ExprError(); |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3627 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3628 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3629 | |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3630 | if (FDecl) { |
| 3631 | // Check if we have too few/too many template arguments, based |
| 3632 | // on our knowledge of the function definition. |
| 3633 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3634 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3635 | const FunctionProtoType *Proto |
| 3636 | = Def->getType()->getAs<FunctionProtoType>(); |
| 3637 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3638 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3639 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3640 | } |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3641 | |
| 3642 | // If the function we're calling isn't a function prototype, but we have |
| 3643 | // a function prototype from a prior declaratiom, use that prototype. |
| 3644 | if (!FDecl->hasPrototype()) |
| 3645 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3648 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3649 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3650 | Expr *Arg = Args[i]; |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3651 | |
| 3652 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3653 | InitializedEntity Entity |
| 3654 | = InitializedEntity::InitializeParameter(Context, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3655 | Proto->getArgType(i), |
| 3656 | Proto->isArgConsumed(i)); |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3657 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 3658 | SourceLocation(), |
| 3659 | Owned(Arg)); |
| 3660 | if (ArgE.isInvalid()) |
| 3661 | return true; |
| 3662 | |
| 3663 | Arg = ArgE.takeAs<Expr>(); |
| 3664 | |
| 3665 | } else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3666 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 3667 | |
| 3668 | if (ArgE.isInvalid()) |
| 3669 | return true; |
| 3670 | |
| 3671 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
Douglas Gregor | 8302541 | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 3674 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3675 | Arg->getType(), |
| 3676 | PDiag(diag::err_call_incomplete_argument) |
| 3677 | << Arg->getSourceRange())) |
| 3678 | return ExprError(); |
| 3679 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3680 | TheCall->setArg(i, Arg); |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3681 | } |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 3682 | } |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3683 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3684 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3685 | if (!Method->isStatic()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3686 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3687 | << Fn->getSourceRange()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3688 | |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3689 | // Check for sentinels |
| 3690 | if (NDecl) |
| 3691 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3692 | |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3693 | // Do special checking on direct calls to functions. |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3694 | if (FDecl) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3695 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3696 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3697 | |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3698 | if (BuiltinID) |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 3699 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3700 | } else if (NDecl) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3701 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3702 | return ExprError(); |
| 3703 | } |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3704 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3705 | return MaybeBindToTemporary(TheCall); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3708 | ExprResult |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3709 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3710 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3711 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | 57eb2c5 | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3712 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3713 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3714 | |
| 3715 | TypeSourceInfo *TInfo; |
| 3716 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 3717 | if (!TInfo) |
| 3718 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 3719 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3720 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3723 | ExprResult |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3724 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3725 | SourceLocation RParenLoc, Expr *LiteralExpr) { |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3726 | QualType literalType = TInfo->getType(); |
Anders Carlsson | 2c1ec6d | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3727 | |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3728 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | 8566356 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 3729 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 3730 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 3731 | << SourceRange(LParenLoc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3732 | LiteralExpr->getSourceRange().getEnd()))) |
Argyrios Kyrtzidis | 8566356 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 3733 | return ExprError(); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3734 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3735 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3736 | << SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd())); |
Douglas Gregor | 1c37d9e | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3737 | } else if (!literalType->isDependentType() && |
| 3738 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3739 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3740 | << SourceRange(LParenLoc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3741 | LiteralExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3742 | return ExprError(); |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3743 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3744 | InitializedEntity Entity |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3745 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3746 | InitializationKind Kind |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3747 | = InitializationKind::CreateCStyleCast(LParenLoc, |
| 3748 | SourceRange(LParenLoc, RParenLoc)); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3749 | InitializationSequence InitSeq(*this, Entity, Kind, &LiteralExpr, 1); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3750 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3751 | MultiExprArg(*this, &LiteralExpr, 1), |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3752 | &literalType); |
| 3753 | if (Result.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3754 | return ExprError(); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3755 | LiteralExpr = Result.get(); |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3756 | |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3757 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3758 | if (isFileScope) { // 6.5.2.5p3 |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3759 | if (CheckForConstantInitializer(LiteralExpr, literalType)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3760 | return ExprError(); |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3761 | } |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3762 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3763 | // In C, compound literals are l-values for some reason. |
| 3764 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 3765 | |
Douglas Gregor | 9b71f0c | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 3766 | return MaybeBindToTemporary( |
| 3767 | new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3768 | VK, LiteralExpr, isFileScope)); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3771 | ExprResult |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3772 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3773 | SourceLocation RBraceLoc) { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3774 | unsigned NumInit = InitArgList.size(); |
| 3775 | Expr **InitList = InitArgList.release(); |
Anders Carlsson | 4692db0 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3776 | |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3777 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3778 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3779 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 3780 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 3781 | NumInit, RBraceLoc); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3782 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3783 | return Owned(E); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3786 | /// Do an explicit extend of the given block pointer if we're in ARC. |
| 3787 | static void maybeExtendBlockObject(Sema &S, ExprResult &E) { |
| 3788 | assert(E.get()->getType()->isBlockPointerType()); |
| 3789 | assert(E.get()->isRValue()); |
| 3790 | |
| 3791 | // Only do this in an r-value context. |
| 3792 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 3793 | |
| 3794 | E = ImplicitCastExpr::Create(S.Context, E.get()->getType(), |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3795 | CK_ARCExtendBlockObject, E.get(), |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3796 | /*base path*/ 0, VK_RValue); |
| 3797 | S.ExprNeedsCleanups = true; |
| 3798 | } |
| 3799 | |
| 3800 | /// Prepare a conversion of the given expression to an ObjC object |
| 3801 | /// pointer type. |
| 3802 | CastKind Sema::PrepareCastToObjCObjectPointer(ExprResult &E) { |
| 3803 | QualType type = E.get()->getType(); |
| 3804 | if (type->isObjCObjectPointerType()) { |
| 3805 | return CK_BitCast; |
| 3806 | } else if (type->isBlockPointerType()) { |
| 3807 | maybeExtendBlockObject(*this, E); |
| 3808 | return CK_BlockPointerToObjCPointerCast; |
| 3809 | } else { |
| 3810 | assert(type->isPointerType()); |
| 3811 | return CK_CPointerToObjCPointerCast; |
| 3812 | } |
| 3813 | } |
| 3814 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3815 | /// Prepares for a scalar cast, performing all the necessary stages |
| 3816 | /// except the final cast and returning the kind required. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3817 | static CastKind PrepareScalarCast(Sema &S, ExprResult &Src, QualType DestTy) { |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3818 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 3819 | // Also, callers should have filtered out the invalid cases with |
| 3820 | // pointers. Everything else should be possible. |
| 3821 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3822 | QualType SrcTy = Src.get()->getType(); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3823 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3824 | return CK_NoOp; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3825 | |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3826 | switch (Type::ScalarTypeKind SrcKind = SrcTy->getScalarTypeKind()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3827 | case Type::STK_MemberPointer: |
| 3828 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3829 | |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3830 | case Type::STK_CPointer: |
| 3831 | case Type::STK_BlockPointer: |
| 3832 | case Type::STK_ObjCObjectPointer: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3833 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3834 | case Type::STK_CPointer: |
| 3835 | return CK_BitCast; |
| 3836 | case Type::STK_BlockPointer: |
| 3837 | return (SrcKind == Type::STK_BlockPointer |
| 3838 | ? CK_BitCast : CK_AnyPointerToBlockPointerCast); |
| 3839 | case Type::STK_ObjCObjectPointer: |
| 3840 | if (SrcKind == Type::STK_ObjCObjectPointer) |
| 3841 | return CK_BitCast; |
| 3842 | else if (SrcKind == Type::STK_CPointer) |
| 3843 | return CK_CPointerToObjCPointerCast; |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3844 | else { |
| 3845 | maybeExtendBlockObject(S, Src); |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3846 | return CK_BlockPointerToObjCPointerCast; |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3847 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3848 | case Type::STK_Bool: |
| 3849 | return CK_PointerToBoolean; |
| 3850 | case Type::STK_Integral: |
| 3851 | return CK_PointerToIntegral; |
| 3852 | case Type::STK_Floating: |
| 3853 | case Type::STK_FloatingComplex: |
| 3854 | case Type::STK_IntegralComplex: |
| 3855 | case Type::STK_MemberPointer: |
| 3856 | llvm_unreachable("illegal cast from pointer"); |
| 3857 | } |
| 3858 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3859 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3860 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 3861 | case Type::STK_Integral: |
| 3862 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3863 | case Type::STK_CPointer: |
| 3864 | case Type::STK_ObjCObjectPointer: |
| 3865 | case Type::STK_BlockPointer: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3866 | if (Src.get()->isNullPointerConstant(S.Context, |
| 3867 | Expr::NPC_ValueDependentIsNull)) |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3868 | return CK_NullToPointer; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3869 | return CK_IntegralToPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3870 | case Type::STK_Bool: |
| 3871 | return CK_IntegralToBoolean; |
| 3872 | case Type::STK_Integral: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3873 | return CK_IntegralCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3874 | case Type::STK_Floating: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3875 | return CK_IntegralToFloating; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3876 | case Type::STK_IntegralComplex: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3877 | Src = S.ImpCastExprToType(Src.take(), |
| 3878 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3879 | CK_IntegralCast); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3880 | return CK_IntegralRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3881 | case Type::STK_FloatingComplex: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3882 | Src = S.ImpCastExprToType(Src.take(), |
| 3883 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3884 | CK_IntegralToFloating); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3885 | return CK_FloatingRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3886 | case Type::STK_MemberPointer: |
| 3887 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3888 | } |
| 3889 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3890 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3891 | case Type::STK_Floating: |
| 3892 | switch (DestTy->getScalarTypeKind()) { |
| 3893 | case Type::STK_Floating: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3894 | return CK_FloatingCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3895 | case Type::STK_Bool: |
| 3896 | return CK_FloatingToBoolean; |
| 3897 | case Type::STK_Integral: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3898 | return CK_FloatingToIntegral; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3899 | case Type::STK_FloatingComplex: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3900 | Src = S.ImpCastExprToType(Src.take(), |
| 3901 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3902 | CK_FloatingCast); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3903 | return CK_FloatingRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3904 | case Type::STK_IntegralComplex: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3905 | Src = S.ImpCastExprToType(Src.take(), |
| 3906 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3907 | CK_FloatingToIntegral); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3908 | return CK_IntegralRealToComplex; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3909 | case Type::STK_CPointer: |
| 3910 | case Type::STK_ObjCObjectPointer: |
| 3911 | case Type::STK_BlockPointer: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3912 | llvm_unreachable("valid float->pointer cast?"); |
| 3913 | case Type::STK_MemberPointer: |
| 3914 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3915 | } |
| 3916 | break; |
| 3917 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3918 | case Type::STK_FloatingComplex: |
| 3919 | switch (DestTy->getScalarTypeKind()) { |
| 3920 | case Type::STK_FloatingComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3921 | return CK_FloatingComplexCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3922 | case Type::STK_IntegralComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3923 | return CK_FloatingComplexToIntegralComplex; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3924 | case Type::STK_Floating: { |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3925 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3926 | if (S.Context.hasSameType(ET, DestTy)) |
| 3927 | return CK_FloatingComplexToReal; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3928 | Src = S.ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3929 | return CK_FloatingCast; |
| 3930 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3931 | case Type::STK_Bool: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3932 | return CK_FloatingComplexToBoolean; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3933 | case Type::STK_Integral: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3934 | Src = S.ImpCastExprToType(Src.take(), |
| 3935 | SrcTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3936 | CK_FloatingComplexToReal); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3937 | return CK_FloatingToIntegral; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3938 | case Type::STK_CPointer: |
| 3939 | case Type::STK_ObjCObjectPointer: |
| 3940 | case Type::STK_BlockPointer: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3941 | llvm_unreachable("valid complex float->pointer cast?"); |
| 3942 | case Type::STK_MemberPointer: |
| 3943 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3944 | } |
| 3945 | break; |
| 3946 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3947 | case Type::STK_IntegralComplex: |
| 3948 | switch (DestTy->getScalarTypeKind()) { |
| 3949 | case Type::STK_FloatingComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3950 | return CK_IntegralComplexToFloatingComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3951 | case Type::STK_IntegralComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3952 | return CK_IntegralComplexCast; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3953 | case Type::STK_Integral: { |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3954 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3955 | if (S.Context.hasSameType(ET, DestTy)) |
| 3956 | return CK_IntegralComplexToReal; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3957 | Src = S.ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3958 | return CK_IntegralCast; |
| 3959 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3960 | case Type::STK_Bool: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3961 | return CK_IntegralComplexToBoolean; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3962 | case Type::STK_Floating: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3963 | Src = S.ImpCastExprToType(Src.take(), |
| 3964 | SrcTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3965 | CK_IntegralComplexToReal); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3966 | return CK_IntegralToFloating; |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3967 | case Type::STK_CPointer: |
| 3968 | case Type::STK_ObjCObjectPointer: |
| 3969 | case Type::STK_BlockPointer: |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3970 | llvm_unreachable("valid complex int->pointer cast?"); |
| 3971 | case Type::STK_MemberPointer: |
| 3972 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3973 | } |
| 3974 | break; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3975 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3976 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3977 | llvm_unreachable("Unhandled scalar cast"); |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3980 | /// CheckCastTypes - Check type constraints for casting between types. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3981 | ExprResult Sema::CheckCastTypes(SourceLocation CastStartLoc, |
| 3982 | SourceRange TypeRange, QualType CastType, |
| 3983 | Expr *CastExpr, CastKind &Kind, |
| 3984 | ExprValueKind &VK, CXXCastPath &BasePath, |
| 3985 | bool FunctionalStyle) { |
| 3986 | if (CastExpr->getType() == Context.UnknownAnyTy) |
| 3987 | return checkUnknownAnyCast(TypeRange, CastType, CastExpr, Kind, VK, |
| 3988 | BasePath); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3989 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3990 | if (getLangOptions().CPlusPlus) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3991 | return CXXCheckCStyleCast(SourceRange(CastStartLoc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3992 | CastExpr->getLocEnd()), |
| 3993 | CastType, VK, CastExpr, Kind, BasePath, |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3994 | FunctionalStyle); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3995 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3996 | assert(!CastExpr->getType()->isPlaceholderType()); |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3997 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3998 | // We only support r-value casts in C. |
| 3999 | VK = VK_RValue; |
| 4000 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4001 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 4002 | // type needs to be scalar. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4003 | if (CastType->isVoidType()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4004 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4005 | ExprResult castExprRes = IgnoredValueConversions(CastExpr); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4006 | if (castExprRes.isInvalid()) |
| 4007 | return ExprError(); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4008 | CastExpr = castExprRes.take(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4009 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4010 | // Cast to void allows any expr type. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4011 | Kind = CK_ToVoid; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4012 | return Owned(CastExpr); |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 4013 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4014 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4015 | ExprResult castExprRes = DefaultFunctionArrayLvalueConversion(CastExpr); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4016 | if (castExprRes.isInvalid()) |
| 4017 | return ExprError(); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4018 | CastExpr = castExprRes.take(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4019 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4020 | if (RequireCompleteType(TypeRange.getBegin(), CastType, |
Eli Friedman | e98194d | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 4021 | diag::err_typecheck_cast_to_incomplete)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4022 | return ExprError(); |
Eli Friedman | e98194d | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 4023 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4024 | if (!CastType->isScalarType() && !CastType->isVectorType()) { |
| 4025 | if (Context.hasSameUnqualifiedType(CastType, CastExpr->getType()) && |
| 4026 | (CastType->isStructureType() || CastType->isUnionType())) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4027 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4028 | // FIXME: Check that the cast destination type is complete. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4029 | Diag(TypeRange.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 4030 | << CastType << CastExpr->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4031 | Kind = CK_NoOp; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4032 | return Owned(CastExpr); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4033 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4034 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4035 | if (CastType->isUnionType()) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4036 | // GCC cast to union extension |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4037 | RecordDecl *RD = CastType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4038 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4039 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4040 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4041 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4042 | CastExpr->getType()) && |
Abramo Bagnara | 5d3e724 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 4043 | !Field->isUnnamedBitfield()) { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4044 | Diag(TypeRange.getBegin(), diag::ext_typecheck_cast_to_union) |
| 4045 | << CastExpr->getSourceRange(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4046 | break; |
| 4047 | } |
| 4048 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4049 | if (Field == FieldEnd) { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4050 | Diag(TypeRange.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 4051 | << CastExpr->getType() << CastExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4052 | return ExprError(); |
| 4053 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4054 | Kind = CK_ToUnion; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4055 | return Owned(CastExpr); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4056 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4057 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4058 | // Reject any other conversions to non-scalar types. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4059 | Diag(TypeRange.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 4060 | << CastType << CastExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4061 | return ExprError(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4062 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4063 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4064 | // The type we're casting to is known to be a scalar or vector. |
| 4065 | |
| 4066 | // Require the operand to be a scalar or vector. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4067 | if (!CastExpr->getType()->isScalarType() && |
| 4068 | !CastExpr->getType()->isVectorType()) { |
| 4069 | Diag(CastExpr->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4070 | diag::err_typecheck_expect_scalar_operand) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4071 | << CastExpr->getType() << CastExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4072 | return ExprError(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4073 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4074 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4075 | if (CastType->isExtVectorType()) |
| 4076 | return CheckExtVectorCast(TypeRange, CastType, CastExpr, Kind); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4077 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4078 | if (CastType->isVectorType()) { |
| 4079 | if (CastType->getAs<VectorType>()->getVectorKind() == |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4080 | VectorType::AltiVecVector && |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4081 | (CastExpr->getType()->isIntegerType() || |
| 4082 | CastExpr->getType()->isFloatingType())) { |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4083 | Kind = CK_VectorSplat; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4084 | return Owned(CastExpr); |
| 4085 | } else if (CheckVectorCast(TypeRange, CastType, CastExpr->getType(), |
| 4086 | Kind)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4087 | return ExprError(); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4088 | } else |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4089 | return Owned(CastExpr); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4090 | } |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4091 | if (CastExpr->getType()->isVectorType()) { |
| 4092 | if (CheckVectorCast(TypeRange, CastExpr->getType(), CastType, Kind)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4093 | return ExprError(); |
| 4094 | else |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4095 | return Owned(CastExpr); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4096 | } |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4097 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4098 | // The source and target types are both scalars, i.e. |
| 4099 | // - arithmetic types (fundamental, enum, and complex) |
| 4100 | // - all kinds of pointers |
| 4101 | // Note that member pointers were filtered out with C++, above. |
| 4102 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4103 | if (isa<ObjCSelectorExpr>(CastExpr)) { |
| 4104 | Diag(CastExpr->getLocStart(), diag::err_cast_selector_expr); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4105 | return ExprError(); |
| 4106 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4107 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4108 | // If either type is a pointer, the other type has to be either an |
| 4109 | // integer or a pointer. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4110 | QualType CastExprType = CastExpr->getType(); |
| 4111 | if (!CastType->isArithmeticType()) { |
| 4112 | if (!CastExprType->isIntegralType(Context) && |
| 4113 | CastExprType->isArithmeticType()) { |
| 4114 | Diag(CastExpr->getLocStart(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4115 | diag::err_cast_pointer_from_non_pointer_int) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4116 | << CastExprType << CastExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4117 | return ExprError(); |
| 4118 | } |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4119 | } else if (!CastExpr->getType()->isArithmeticType()) { |
| 4120 | if (!CastType->isIntegralType(Context) && CastType->isArithmeticType()) { |
| 4121 | Diag(CastExpr->getLocStart(), diag::err_cast_pointer_to_non_pointer_int) |
| 4122 | << CastType << CastExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4123 | return ExprError(); |
| 4124 | } |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4125 | } |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4126 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4127 | if (getLangOptions().ObjCAutoRefCount) { |
| 4128 | // Diagnose problems with Objective-C casts involving lifetime qualifiers. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4129 | CheckObjCARCConversion(SourceRange(CastStartLoc, CastExpr->getLocEnd()), |
| 4130 | CastType, CastExpr, CCK_CStyleCast); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4131 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4132 | if (const PointerType *CastPtr = CastType->getAs<PointerType>()) { |
| 4133 | if (const PointerType *ExprPtr = CastExprType->getAs<PointerType>()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4134 | Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); |
| 4135 | Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); |
| 4136 | if (CastPtr->getPointeeType()->isObjCLifetimeType() && |
| 4137 | ExprPtr->getPointeeType()->isObjCLifetimeType() && |
| 4138 | !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4139 | Diag(CastExpr->getLocStart(), |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 4140 | diag::err_typecheck_incompatible_ownership) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4141 | << CastExprType << CastType << AA_Casting |
| 4142 | << CastExpr->getSourceRange(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4143 | |
| 4144 | return ExprError(); |
| 4145 | } |
| 4146 | } |
Fariborz Jahanian | 6f472e8 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 4147 | } |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4148 | else if (!CheckObjCARCUnavailableWeakConversion(CastType, CastExprType)) { |
| 4149 | Diag(CastExpr->getLocStart(), |
Fariborz Jahanian | f291340 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 4150 | diag::err_arc_convesion_of_weak_unavailable) << 1 |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4151 | << CastExprType << CastType |
| 4152 | << CastExpr->getSourceRange(); |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 4153 | return ExprError(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4154 | } |
| 4155 | } |
| 4156 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4157 | castExprRes = Owned(CastExpr); |
| 4158 | Kind = PrepareScalarCast(*this, castExprRes, CastType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4159 | if (castExprRes.isInvalid()) |
| 4160 | return ExprError(); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4161 | CastExpr = castExprRes.take(); |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4162 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4163 | if (Kind == CK_BitCast) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4164 | CheckCastAlign(CastExpr, CastType, TypeRange); |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4165 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4166 | return Owned(CastExpr); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4169 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4170 | CastKind &Kind) { |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4171 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4172 | |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4173 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4174 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4175 | return Diag(R.getBegin(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4176 | Ty->isVectorType() ? |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4177 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4178 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4179 | << VectorTy << Ty << R; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4180 | } else |
| 4181 | return Diag(R.getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4182 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4183 | << VectorTy << Ty << R; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4184 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4185 | Kind = CK_BitCast; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4186 | return false; |
| 4187 | } |
| 4188 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4189 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 4190 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4191 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4192 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4193 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4194 | |
Nate Begeman | c8961a4 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4195 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 4196 | // an ExtVectorType. |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4197 | if (SrcTy->isVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4198 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) { |
| 4199 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4200 | << DestTy << SrcTy << R; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4201 | return ExprError(); |
| 4202 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4203 | Kind = CK_BitCast; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4204 | return Owned(CastExpr); |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4207 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4208 | // conversion will take place first from scalar to elt type, and then |
| 4209 | // splat from elt type to vector. |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4210 | if (SrcTy->isPointerType()) |
| 4211 | return Diag(R.getBegin(), |
| 4212 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 4213 | << DestTy << SrcTy << R; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4214 | |
| 4215 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4216 | ExprResult CastExprRes = Owned(CastExpr); |
| 4217 | CastKind CK = PrepareScalarCast(*this, CastExprRes, DestElemTy); |
| 4218 | if (CastExprRes.isInvalid()) |
| 4219 | return ExprError(); |
| 4220 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4221 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4222 | Kind = CK_VectorSplat; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4223 | return Owned(CastExpr); |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4226 | ExprResult |
Argyrios Kyrtzidis | 7192a3b | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4227 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, |
| 4228 | Declarator &D, ParsedType &Ty, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4229 | SourceLocation RParenLoc, Expr *CastExpr) { |
| 4230 | assert(!D.isInvalidType() && (CastExpr != 0) && |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4231 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 1a2cf6b | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4232 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4233 | TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, CastExpr->getType()); |
Argyrios Kyrtzidis | 7192a3b | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4234 | if (D.isInvalidType()) |
| 4235 | return ExprError(); |
| 4236 | |
| 4237 | if (getLangOptions().CPlusPlus) { |
| 4238 | // Check that there are no default arguments (C++ only). |
| 4239 | CheckExtraCXXDefaultArguments(D); |
| 4240 | } |
| 4241 | |
| 4242 | QualType castType = castTInfo->getType(); |
| 4243 | Ty = CreateParsedType(castType, castTInfo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4244 | |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4245 | bool isVectorLiteral = false; |
| 4246 | |
| 4247 | // Check for an altivec or OpenCL literal, |
| 4248 | // i.e. all the elements are integer constants. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4249 | ParenExpr *PE = dyn_cast<ParenExpr>(CastExpr); |
| 4250 | ParenListExpr *PLE = dyn_cast<ParenListExpr>(CastExpr); |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4251 | if (getLangOptions().AltiVec && castType->isVectorType() && (PE || PLE)) { |
| 4252 | if (PLE && PLE->getNumExprs() == 0) { |
| 4253 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4254 | return ExprError(); |
| 4255 | } |
| 4256 | if (PE || PLE->getNumExprs() == 1) { |
| 4257 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); |
| 4258 | if (!E->getType()->isVectorType()) |
| 4259 | isVectorLiteral = true; |
| 4260 | } |
| 4261 | else |
| 4262 | isVectorLiteral = true; |
| 4263 | } |
| 4264 | |
| 4265 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
| 4266 | // then handle it as such. |
| 4267 | if (isVectorLiteral) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4268 | return BuildVectorLiteral(LParenLoc, RParenLoc, CastExpr, castTInfo); |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4269 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4270 | // If the Expr being casted is a ParenListExpr, handle it specially. |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4271 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 4272 | // sequence of BinOp comma operators. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4273 | if (isa<ParenListExpr>(CastExpr)) { |
| 4274 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, CastExpr); |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4275 | if (Result.isInvalid()) return ExprError(); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4276 | CastExpr = Result.take(); |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4277 | } |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4278 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4279 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, CastExpr); |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4280 | } |
| 4281 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4282 | ExprResult |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4283 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4284 | SourceLocation RParenLoc, Expr *CastExpr) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4285 | CastKind Kind = CK_Invalid; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4286 | ExprValueKind VK = VK_RValue; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4287 | CXXCastPath BasePath; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4288 | ExprResult CastResult = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4289 | CheckCastTypes(LParenLoc, SourceRange(LParenLoc, RParenLoc), Ty->getType(), |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4290 | CastExpr, Kind, VK, BasePath); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4291 | if (CastResult.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4292 | return ExprError(); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4293 | CastExpr = CastResult.take(); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 4294 | |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4295 | return Owned(CStyleCastExpr::Create( |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4296 | Context, Ty->getType().getNonLValueExprType(Context), VK, Kind, CastExpr, |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4297 | &BasePath, Ty, LParenLoc, RParenLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4300 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
| 4301 | SourceLocation RParenLoc, Expr *E, |
| 4302 | TypeSourceInfo *TInfo) { |
| 4303 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && |
| 4304 | "Expected paren or paren list expression"); |
| 4305 | |
| 4306 | Expr **exprs; |
| 4307 | unsigned numExprs; |
| 4308 | Expr *subExpr; |
| 4309 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { |
| 4310 | exprs = PE->getExprs(); |
| 4311 | numExprs = PE->getNumExprs(); |
| 4312 | } else { |
| 4313 | subExpr = cast<ParenExpr>(E)->getSubExpr(); |
| 4314 | exprs = &subExpr; |
| 4315 | numExprs = 1; |
| 4316 | } |
| 4317 | |
| 4318 | QualType Ty = TInfo->getType(); |
| 4319 | assert(Ty->isVectorType() && "Expected vector type"); |
| 4320 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4321 | SmallVector<Expr *, 8> initExprs; |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4322 | const VectorType *VTy = Ty->getAs<VectorType>(); |
| 4323 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 4324 | |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4325 | // '(...)' form of vector initialization in AltiVec: the number of |
| 4326 | // initializers must be one or must match the size of the vector. |
| 4327 | // If a single value is specified in the initializer then it will be |
| 4328 | // replicated to all the components of the vector |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4329 | if (VTy->getVectorKind() == VectorType::AltiVecVector) { |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4330 | // The number of initializers must be one or must match the size of the |
| 4331 | // vector. If a single value is specified in the initializer then it will |
| 4332 | // be replicated to all the components of the vector |
| 4333 | if (numExprs == 1) { |
| 4334 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4335 | ExprResult Literal = Owned(exprs[0]); |
| 4336 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4337 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4338 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4339 | } |
| 4340 | else if (numExprs < numElems) { |
| 4341 | Diag(E->getExprLoc(), |
| 4342 | diag::err_incorrect_number_of_vector_initializers); |
| 4343 | return ExprError(); |
| 4344 | } |
| 4345 | else |
| 4346 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4347 | initExprs.push_back(exprs[i]); |
| 4348 | } |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4349 | else { |
| 4350 | // For OpenCL, when the number of initializers is a single value, |
| 4351 | // it will be replicated to all components of the vector. |
| 4352 | if (getLangOptions().OpenCL && |
| 4353 | VTy->getVectorKind() == VectorType::GenericVector && |
| 4354 | numExprs == 1) { |
| 4355 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4356 | ExprResult Literal = Owned(exprs[0]); |
| 4357 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4358 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4359 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4360 | } |
| 4361 | |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4362 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4363 | initExprs.push_back(exprs[i]); |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4364 | } |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4365 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4366 | // braces instead of the original commas. |
| 4367 | InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc, |
| 4368 | &initExprs[0], |
| 4369 | initExprs.size(), RParenLoc); |
| 4370 | initE->setType(Ty); |
| 4371 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); |
| 4372 | } |
| 4373 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4374 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 4375 | /// of comma binary operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4376 | ExprResult |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4377 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *OrigExpr) { |
| 4378 | ParenListExpr *E = dyn_cast<ParenListExpr>(OrigExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4379 | if (!E) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4380 | return Owned(OrigExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4381 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4382 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4383 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4384 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4385 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4386 | E->getExpr(i)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4387 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4388 | if (Result.isInvalid()) return ExprError(); |
| 4389 | |
| 4390 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4393 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4394 | SourceLocation R, |
| 4395 | MultiExprArg Val) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4396 | unsigned nexprs = Val.size(); |
| 4397 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4398 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 4399 | Expr *expr; |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4400 | if (nexprs == 1) |
Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4401 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 4402 | else |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4403 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R, |
| 4404 | exprs[nexprs-1]->getType()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4405 | return Owned(expr); |
| 4406 | } |
| 4407 | |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4408 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4409 | /// constant and the other is not a pointer. Returns true if a diagnostic is |
| 4410 | /// emitted. |
Richard Trieu | d33e46e | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4411 | bool Sema::DiagnoseConditionalForNull(Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4412 | SourceLocation QuestionLoc) { |
Richard Trieu | d33e46e | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4413 | Expr *NullExpr = LHSExpr; |
| 4414 | Expr *NonPointerExpr = RHSExpr; |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4415 | Expr::NullPointerConstantKind NullKind = |
| 4416 | NullExpr->isNullPointerConstant(Context, |
| 4417 | Expr::NPC_ValueDependentIsNotNull); |
| 4418 | |
| 4419 | if (NullKind == Expr::NPCK_NotNull) { |
Richard Trieu | d33e46e | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4420 | NullExpr = RHSExpr; |
| 4421 | NonPointerExpr = LHSExpr; |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4422 | NullKind = |
| 4423 | NullExpr->isNullPointerConstant(Context, |
| 4424 | Expr::NPC_ValueDependentIsNotNull); |
| 4425 | } |
| 4426 | |
| 4427 | if (NullKind == Expr::NPCK_NotNull) |
| 4428 | return false; |
| 4429 | |
| 4430 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 4431 | // In this case, check to make sure that we got here from a "NULL" |
| 4432 | // string in the source code. |
| 4433 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 4434 | SourceLocation loc = NullExpr->getExprLoc(); |
| 4435 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4436 | return false; |
| 4437 | } |
| 4438 | |
| 4439 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 4440 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 4441 | << NonPointerExpr->getType() << DiagType |
| 4442 | << NonPointerExpr->getSourceRange(); |
| 4443 | return true; |
| 4444 | } |
| 4445 | |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4446 | /// \brief Return false if the condition expression is valid, true otherwise. |
| 4447 | static bool checkCondition(Sema &S, Expr *Cond) { |
| 4448 | QualType CondTy = Cond->getType(); |
| 4449 | |
| 4450 | // C99 6.5.15p2 |
| 4451 | if (CondTy->isScalarType()) return false; |
| 4452 | |
| 4453 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 4454 | if (S.getLangOptions().OpenCL && CondTy->isVectorType()) |
| 4455 | return false; |
| 4456 | |
| 4457 | // Emit the proper error message. |
| 4458 | S.Diag(Cond->getLocStart(), S.getLangOptions().OpenCL ? |
| 4459 | diag::err_typecheck_cond_expect_scalar : |
| 4460 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 4461 | << CondTy; |
| 4462 | return true; |
| 4463 | } |
| 4464 | |
| 4465 | /// \brief Return false if the two expressions can be converted to a vector, |
| 4466 | /// true otherwise |
| 4467 | static bool checkConditionalConvertScalarsToVectors(Sema &S, ExprResult &LHS, |
| 4468 | ExprResult &RHS, |
| 4469 | QualType CondTy) { |
| 4470 | // Both operands should be of scalar type. |
| 4471 | if (!LHS.get()->getType()->isScalarType()) { |
| 4472 | S.Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4473 | << CondTy; |
| 4474 | return true; |
| 4475 | } |
| 4476 | if (!RHS.get()->getType()->isScalarType()) { |
| 4477 | S.Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4478 | << CondTy; |
| 4479 | return true; |
| 4480 | } |
| 4481 | |
| 4482 | // Implicity convert these scalars to the type of the condition. |
| 4483 | LHS = S.ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 4484 | RHS = S.ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
| 4485 | return false; |
| 4486 | } |
| 4487 | |
| 4488 | /// \brief Handle when one or both operands are void type. |
| 4489 | static QualType checkConditionalVoidType(Sema &S, ExprResult &LHS, |
| 4490 | ExprResult &RHS) { |
| 4491 | Expr *LHSExpr = LHS.get(); |
| 4492 | Expr *RHSExpr = RHS.get(); |
| 4493 | |
| 4494 | if (!LHSExpr->getType()->isVoidType()) |
| 4495 | S.Diag(RHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4496 | << RHSExpr->getSourceRange(); |
| 4497 | if (!RHSExpr->getType()->isVoidType()) |
| 4498 | S.Diag(LHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4499 | << LHSExpr->getSourceRange(); |
| 4500 | LHS = S.ImpCastExprToType(LHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4501 | RHS = S.ImpCastExprToType(RHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4502 | return S.Context.VoidTy; |
| 4503 | } |
| 4504 | |
| 4505 | /// \brief Return false if the NullExpr can be promoted to PointerTy, |
| 4506 | /// true otherwise. |
| 4507 | static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr, |
| 4508 | QualType PointerTy) { |
| 4509 | if ((!PointerTy->isAnyPointerType() && !PointerTy->isBlockPointerType()) || |
| 4510 | !NullExpr.get()->isNullPointerConstant(S.Context, |
| 4511 | Expr::NPC_ValueDependentIsNull)) |
| 4512 | return true; |
| 4513 | |
| 4514 | NullExpr = S.ImpCastExprToType(NullExpr.take(), PointerTy, CK_NullToPointer); |
| 4515 | return false; |
| 4516 | } |
| 4517 | |
| 4518 | /// \brief Checks compatibility between two pointers and return the resulting |
| 4519 | /// type. |
| 4520 | static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, |
| 4521 | ExprResult &RHS, |
| 4522 | SourceLocation Loc) { |
| 4523 | QualType LHSTy = LHS.get()->getType(); |
| 4524 | QualType RHSTy = RHS.get()->getType(); |
| 4525 | |
| 4526 | if (S.Context.hasSameType(LHSTy, RHSTy)) { |
| 4527 | // Two identical pointers types are always compatible. |
| 4528 | return LHSTy; |
| 4529 | } |
| 4530 | |
| 4531 | QualType lhptee, rhptee; |
| 4532 | |
| 4533 | // Get the pointee types. |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4534 | if (const BlockPointerType *LHSBTy = LHSTy->getAs<BlockPointerType>()) { |
| 4535 | lhptee = LHSBTy->getPointeeType(); |
| 4536 | rhptee = RHSTy->castAs<BlockPointerType>()->getPointeeType(); |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4537 | } else { |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4538 | lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); |
| 4539 | rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
| 4542 | if (!S.Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4543 | rhptee.getUnqualifiedType())) { |
| 4544 | S.Diag(Loc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4545 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4546 | << RHS.get()->getSourceRange(); |
| 4547 | // In this situation, we assume void* type. No especially good |
| 4548 | // reason, but this is what gcc does, and we do have to pick |
| 4549 | // to get a consistent AST. |
| 4550 | QualType incompatTy = S.Context.getPointerType(S.Context.VoidTy); |
| 4551 | LHS = S.ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4552 | RHS = S.ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
| 4553 | return incompatTy; |
| 4554 | } |
| 4555 | |
| 4556 | // The pointer types are compatible. |
| 4557 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4558 | // differently qualified versions of compatible types, the result type is |
| 4559 | // a pointer to an appropriately qualified version of the *composite* |
| 4560 | // type. |
| 4561 | // FIXME: Need to calculate the composite type. |
| 4562 | // FIXME: Need to add qualifiers |
| 4563 | |
| 4564 | LHS = S.ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 4565 | RHS = S.ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
| 4566 | return LHSTy; |
| 4567 | } |
| 4568 | |
| 4569 | /// \brief Return the resulting type when the operands are both block pointers. |
| 4570 | static QualType checkConditionalBlockPointerCompatibility(Sema &S, |
| 4571 | ExprResult &LHS, |
| 4572 | ExprResult &RHS, |
| 4573 | SourceLocation Loc) { |
| 4574 | QualType LHSTy = LHS.get()->getType(); |
| 4575 | QualType RHSTy = RHS.get()->getType(); |
| 4576 | |
| 4577 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4578 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4579 | QualType destType = S.Context.getPointerType(S.Context.VoidTy); |
| 4580 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4581 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4582 | return destType; |
| 4583 | } |
| 4584 | S.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 4585 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4586 | << RHS.get()->getSourceRange(); |
| 4587 | return QualType(); |
| 4588 | } |
| 4589 | |
| 4590 | // We have 2 block pointer types. |
| 4591 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4592 | } |
| 4593 | |
| 4594 | /// \brief Return the resulting type when the operands are both pointers. |
| 4595 | static QualType |
| 4596 | checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, |
| 4597 | ExprResult &RHS, |
| 4598 | SourceLocation Loc) { |
| 4599 | // get the pointer types |
| 4600 | QualType LHSTy = LHS.get()->getType(); |
| 4601 | QualType RHSTy = RHS.get()->getType(); |
| 4602 | |
| 4603 | // get the "pointed to" types |
| 4604 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4605 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4606 | |
| 4607 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4608 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4609 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 4610 | QualType destPointee |
| 4611 | = S.Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4612 | QualType destType = S.Context.getPointerType(destPointee); |
| 4613 | // Add qualifiers if necessary. |
| 4614 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
| 4615 | // Promote to void*. |
| 4616 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4617 | return destType; |
| 4618 | } |
| 4619 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
| 4620 | QualType destPointee |
| 4621 | = S.Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4622 | QualType destType = S.Context.getPointerType(destPointee); |
| 4623 | // Add qualifiers if necessary. |
| 4624 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
| 4625 | // Promote to void*. |
| 4626 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4627 | return destType; |
| 4628 | } |
| 4629 | |
| 4630 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4631 | } |
| 4632 | |
| 4633 | /// \brief Return false if the first expression is not an integer and the second |
| 4634 | /// expression is not a pointer, true otherwise. |
| 4635 | static bool checkPointerIntegerMismatch(Sema &S, ExprResult &Int, |
| 4636 | Expr* PointerExpr, SourceLocation Loc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4637 | bool IsIntFirstExpr) { |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4638 | if (!PointerExpr->getType()->isPointerType() || |
| 4639 | !Int.get()->getType()->isIntegerType()) |
| 4640 | return false; |
| 4641 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4642 | Expr *Expr1 = IsIntFirstExpr ? Int.get() : PointerExpr; |
| 4643 | Expr *Expr2 = IsIntFirstExpr ? PointerExpr : Int.get(); |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4644 | |
| 4645 | S.Diag(Loc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4646 | << Expr1->getType() << Expr2->getType() |
| 4647 | << Expr1->getSourceRange() << Expr2->getSourceRange(); |
| 4648 | Int = S.ImpCastExprToType(Int.take(), PointerExpr->getType(), |
| 4649 | CK_IntegralToPointer); |
| 4650 | return true; |
| 4651 | } |
| 4652 | |
Richard Trieu | d33e46e | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4653 | /// Note that LHS is not null here, even if this is the gnu "x ?: y" extension. |
| 4654 | /// In that case, LHS = cond. |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4655 | /// C99 6.5.15 |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4656 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 4657 | ExprResult &RHS, ExprValueKind &VK, |
| 4658 | ExprObjectKind &OK, |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4659 | SourceLocation QuestionLoc) { |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 4660 | |
Richard Trieu | d33e46e | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4661 | ExprResult LHSResult = CheckPlaceholderExpr(LHS.get()); |
| 4662 | if (!LHSResult.isUsable()) return QualType(); |
| 4663 | LHS = move(LHSResult); |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4664 | |
Richard Trieu | d33e46e | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4665 | ExprResult RHSResult = CheckPlaceholderExpr(RHS.get()); |
| 4666 | if (!RHSResult.isUsable()) return QualType(); |
| 4667 | RHS = move(RHSResult); |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4668 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4669 | // C++ is sufficiently different to merit its own checker. |
| 4670 | if (getLangOptions().CPlusPlus) |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4671 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4672 | |
| 4673 | VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4674 | OK = OK_Ordinary; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4675 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4676 | Cond = UsualUnaryConversions(Cond.take()); |
| 4677 | if (Cond.isInvalid()) |
| 4678 | return QualType(); |
| 4679 | LHS = UsualUnaryConversions(LHS.take()); |
| 4680 | if (LHS.isInvalid()) |
| 4681 | return QualType(); |
| 4682 | RHS = UsualUnaryConversions(RHS.take()); |
| 4683 | if (RHS.isInvalid()) |
| 4684 | return QualType(); |
| 4685 | |
| 4686 | QualType CondTy = Cond.get()->getType(); |
| 4687 | QualType LHSTy = LHS.get()->getType(); |
| 4688 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4689 | |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4690 | // first, check the condition. |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4691 | if (checkCondition(*this, Cond.get())) |
| 4692 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4693 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4694 | // Now check the two expressions. |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4695 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 4696 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4697 | |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4698 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 4699 | // attempt to implicity convert them to the vector type to act like the |
| 4700 | // built in select. |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4701 | if (getLangOptions().OpenCL && CondTy->isVectorType()) |
| 4702 | if (checkConditionalConvertScalarsToVectors(*this, LHS, RHS, CondTy)) |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4703 | return QualType(); |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4704 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4705 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4706 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4707 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4708 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4709 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4710 | return QualType(); |
| 4711 | return LHS.get()->getType(); |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4712 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4713 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4714 | // If both operands are the same structure or union type, the result is that |
| 4715 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4716 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4717 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4718 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4719 | // "If both the operands have structure or union type, the result has |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4720 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4721 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4722 | // FIXME: Type of conditional expression must be complete in C mode. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4723 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4724 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4725 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4726 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4727 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4728 | return checkConditionalVoidType(*this, LHS, RHS); |
Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4729 | } |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4730 | |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4731 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4732 | // the type of the other operand." |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4733 | if (!checkConditionalNullPointer(*this, RHS, LHSTy)) return LHSTy; |
| 4734 | if (!checkConditionalNullPointer(*this, LHS, RHSTy)) return RHSTy; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4735 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4736 | // All objective-c pointer type analysis is done here. |
| 4737 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4738 | QuestionLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4739 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4740 | return QualType(); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4741 | if (!compositeType.isNull()) |
| 4742 | return compositeType; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4743 | |
| 4744 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4745 | // Handle block pointer types. |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4746 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) |
| 4747 | return checkConditionalBlockPointerCompatibility(*this, LHS, RHS, |
| 4748 | QuestionLoc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4749 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4750 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4751 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) |
| 4752 | return checkConditionalObjectPointersCompatibility(*this, LHS, RHS, |
| 4753 | QuestionLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4754 | |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4755 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 4756 | // null pointers have been filtered out by this point. |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4757 | if (checkPointerIntegerMismatch(*this, LHS, RHS.get(), QuestionLoc, |
| 4758 | /*isIntFirstExpr=*/true)) |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4759 | return RHSTy; |
Richard Trieu | 27ae4cb | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4760 | if (checkPointerIntegerMismatch(*this, RHS, LHS.get(), QuestionLoc, |
| 4761 | /*isIntFirstExpr=*/false)) |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4762 | return LHSTy; |
Daniel Dunbar | 484603b | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4763 | |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4764 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 4765 | // constant and the other is not a pointer type. In this case, the user most |
| 4766 | // likely forgot to take the address of the other expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4767 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4768 | return QualType(); |
| 4769 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4770 | // Otherwise, the operands are not compatible. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4771 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4772 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4773 | << RHS.get()->getSourceRange(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4774 | return QualType(); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 4775 | } |
| 4776 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4777 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4778 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4779 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4780 | SourceLocation QuestionLoc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4781 | QualType LHSTy = LHS.get()->getType(); |
| 4782 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4783 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4784 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4785 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4786 | // redefinition type if an attempt is made to access its fields. |
| 4787 | if (LHSTy->isObjCClassType() && |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4788 | (Context.hasSameType(RHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4789 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4790 | return LHSTy; |
| 4791 | } |
| 4792 | if (RHSTy->isObjCClassType() && |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4793 | (Context.hasSameType(LHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4794 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4795 | return RHSTy; |
| 4796 | } |
| 4797 | // And the same for struct objc_object* / id |
| 4798 | if (LHSTy->isObjCIdType() && |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4799 | (Context.hasSameType(RHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4800 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4801 | return LHSTy; |
| 4802 | } |
| 4803 | if (RHSTy->isObjCIdType() && |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4804 | (Context.hasSameType(LHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4805 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4806 | return RHSTy; |
| 4807 | } |
| 4808 | // And the same for struct objc_selector* / SEL |
| 4809 | if (Context.isObjCSelType(LHSTy) && |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4810 | (Context.hasSameType(RHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4811 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4812 | return LHSTy; |
| 4813 | } |
| 4814 | if (Context.isObjCSelType(RHSTy) && |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4815 | (Context.hasSameType(LHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4816 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4817 | return RHSTy; |
| 4818 | } |
| 4819 | // Check constraints for Objective-C object pointers types. |
| 4820 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4821 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4822 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4823 | // Two identical object pointer types are always compatible. |
| 4824 | return LHSTy; |
| 4825 | } |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4826 | const ObjCObjectPointerType *LHSOPT = LHSTy->castAs<ObjCObjectPointerType>(); |
| 4827 | const ObjCObjectPointerType *RHSOPT = RHSTy->castAs<ObjCObjectPointerType>(); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4828 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4829 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4830 | // If both operands are interfaces and either operand can be |
| 4831 | // assigned to the other, use that type as the composite |
| 4832 | // type. This allows |
| 4833 | // xxx ? (A*) a : (B*) b |
| 4834 | // where B is a subclass of A. |
| 4835 | // |
| 4836 | // Additionally, as for assignment, if either type is 'id' |
| 4837 | // allow silent coercion. Finally, if the types are |
| 4838 | // incompatible then make sure to use 'id' as the composite |
| 4839 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4840 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4841 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4842 | // It could return the composite type. |
| 4843 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4844 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4845 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4846 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4847 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4848 | RHSTy->isObjCQualifiedIdType()) && |
| 4849 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4850 | // Need to handle "id<xx>" explicitly. |
| 4851 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4852 | // id. Currently localizing to here until clear this should be |
| 4853 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4854 | compositeType = Context.getObjCIdType(); |
| 4855 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4856 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4857 | } else if (!(compositeType = |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4858 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4859 | ; |
| 4860 | else { |
| 4861 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4862 | << LHSTy << RHSTy |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4863 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4864 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4865 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4866 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4867 | return incompatTy; |
| 4868 | } |
| 4869 | // The object pointer types are compatible. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4870 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 4871 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4872 | return compositeType; |
| 4873 | } |
| 4874 | // Check Objective-C object pointer types and 'void *' |
| 4875 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4876 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4877 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4878 | QualType destPointee |
| 4879 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4880 | QualType destType = Context.getPointerType(destPointee); |
| 4881 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4882 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4883 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4884 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4885 | return destType; |
| 4886 | } |
| 4887 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4888 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4889 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4890 | QualType destPointee |
| 4891 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4892 | QualType destType = Context.getPointerType(destPointee); |
| 4893 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4894 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4895 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4896 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4897 | return destType; |
| 4898 | } |
| 4899 | return QualType(); |
| 4900 | } |
| 4901 | |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4902 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4903 | /// ParenRange in parentheses. |
| 4904 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4905 | const PartialDiagnostic &Note, |
| 4906 | SourceRange ParenRange) { |
| 4907 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 4908 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 4909 | EndLoc.isValid()) { |
| 4910 | Self.Diag(Loc, Note) |
| 4911 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 4912 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 4913 | } else { |
| 4914 | // We can't display the parentheses, so just show the bare note. |
| 4915 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4916 | } |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 4920 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 4921 | } |
| 4922 | |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4923 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 4924 | /// expression, either using a built-in or overloaded operator, |
Richard Trieu | d33e46e | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4925 | /// and sets *OpCode to the opcode and *RHSExprs to the right-hand side |
| 4926 | /// expression. |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4927 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
Richard Trieu | d33e46e | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4928 | Expr **RHSExprs) { |
Hans Wennborg | be207b3 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 4929 | // Don't strip parenthesis: we should not warn if E is in parenthesis. |
| 4930 | E = E->IgnoreImpCasts(); |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4931 | E = E->IgnoreConversionOperator(); |
Hans Wennborg | be207b3 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 4932 | E = E->IgnoreImpCasts(); |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4933 | |
| 4934 | // Built-in binary operator. |
| 4935 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 4936 | if (IsArithmeticOp(OP->getOpcode())) { |
| 4937 | *Opcode = OP->getOpcode(); |
Richard Trieu | d33e46e | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4938 | *RHSExprs = OP->getRHS(); |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4939 | return true; |
| 4940 | } |
| 4941 | } |
| 4942 | |
| 4943 | // Overloaded operator. |
| 4944 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 4945 | if (Call->getNumArgs() != 2) |
| 4946 | return false; |
| 4947 | |
| 4948 | // Make sure this is really a binary operator that is safe to pass into |
| 4949 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 4950 | OverloadedOperatorKind OO = Call->getOperator(); |
| 4951 | if (OO < OO_Plus || OO > OO_Arrow) |
| 4952 | return false; |
| 4953 | |
| 4954 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 4955 | if (IsArithmeticOp(OpKind)) { |
| 4956 | *Opcode = OpKind; |
Richard Trieu | d33e46e | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4957 | *RHSExprs = Call->getArg(1); |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4958 | return true; |
| 4959 | } |
| 4960 | } |
| 4961 | |
| 4962 | return false; |
| 4963 | } |
| 4964 | |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4965 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 4966 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 4967 | } |
| 4968 | |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4969 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 4970 | /// or is a logical expression such as (x==y) which has int type, but is |
| 4971 | /// commonly interpreted as boolean. |
| 4972 | static bool ExprLooksBoolean(Expr *E) { |
| 4973 | E = E->IgnoreParenImpCasts(); |
| 4974 | |
| 4975 | if (E->getType()->isBooleanType()) |
| 4976 | return true; |
| 4977 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 4978 | return IsLogicOp(OP->getOpcode()); |
| 4979 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 4980 | return OP->getOpcode() == UO_LNot; |
| 4981 | |
| 4982 | return false; |
| 4983 | } |
| 4984 | |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4985 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 4986 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 4987 | /// the conditional operator has higher precedence, for example: |
| 4988 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 4989 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 4990 | SourceLocation OpLoc, |
Chandler Carruth | 08dc2ba | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4991 | Expr *Condition, |
Richard Trieu | d33e46e | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4992 | Expr *LHSExpr, |
| 4993 | Expr *RHSExpr) { |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4994 | BinaryOperatorKind CondOpcode; |
| 4995 | Expr *CondRHS; |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4996 | |
Chandler Carruth | 08dc2ba | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4997 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4998 | return; |
| 4999 | if (!ExprLooksBoolean(CondRHS)) |
| 5000 | return; |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5001 | |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5002 | // The condition is an arithmetic binary expression, with a right- |
| 5003 | // hand side that looks boolean, so warn. |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5004 | |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5005 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 08dc2ba | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5006 | << Condition->getSourceRange() |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5007 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5008 | |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5009 | SuggestParentheses(Self, OpLoc, |
| 5010 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 5011 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 5012 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Chandler Carruth | f51c5a5 | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 5013 | |
| 5014 | SuggestParentheses(Self, OpLoc, |
| 5015 | Self.PDiag(diag::note_precedence_conditional_first), |
Richard Trieu | d33e46e | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5016 | SourceRange(CondRHS->getLocStart(), RHSExpr->getLocEnd())); |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5019 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 5020 | /// in the case of a the GNU conditional expr extension. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5021 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5022 | SourceLocation ColonLoc, |
| 5023 | Expr *CondExpr, Expr *LHSExpr, |
| 5024 | Expr *RHSExpr) { |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5025 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 5026 | // was the condition. |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5027 | OpaqueValueExpr *opaqueValue = 0; |
| 5028 | Expr *commonExpr = 0; |
| 5029 | if (LHSExpr == 0) { |
| 5030 | commonExpr = CondExpr; |
| 5031 | |
| 5032 | // We usually want to apply unary conversions *before* saving, except |
| 5033 | // in the special case of a C++ l-value conditional. |
| 5034 | if (!(getLangOptions().CPlusPlus |
| 5035 | && !commonExpr->isTypeDependent() |
| 5036 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 5037 | && commonExpr->isGLValue() |
| 5038 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 5039 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 5040 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5041 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 5042 | if (commonRes.isInvalid()) |
| 5043 | return ExprError(); |
| 5044 | commonExpr = commonRes.take(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 5048 | commonExpr->getType(), |
| 5049 | commonExpr->getValueKind(), |
| 5050 | commonExpr->getObjectKind()); |
| 5051 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5052 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5053 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5054 | ExprValueKind VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5055 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5056 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 5057 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5058 | VK, OK, QuestionLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5059 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 5060 | RHS.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5061 | return ExprError(); |
| 5062 | |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5063 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 5064 | RHS.get()); |
| 5065 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5066 | if (!commonExpr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5067 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 5068 | LHS.take(), ColonLoc, |
| 5069 | RHS.take(), result, VK, OK)); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5070 | |
| 5071 | return Owned(new (Context) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5072 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5073 | RHS.take(), QuestionLoc, ColonLoc, result, VK, |
| 5074 | OK)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 5075 | } |
| 5076 | |
Fariborz Jahanian | e20c0a5 | 2011-09-19 18:06:07 +0000 | [diff] [blame^] | 5077 | /// ConvertObjCSelfToClassRootType - convet type of 'self' in class method |
Fariborz Jahanian | db8015c | 2011-09-17 19:23:40 +0000 | [diff] [blame] | 5078 | /// to pointer to root of method's class. |
Fariborz Jahanian | e20c0a5 | 2011-09-19 18:06:07 +0000 | [diff] [blame^] | 5079 | static QualType |
| 5080 | ConvertObjCSelfToClassRootType(Sema &S, Expr *selfExpr) { |
| 5081 | QualType SelfType; |
Fariborz Jahanian | db8015c | 2011-09-17 19:23:40 +0000 | [diff] [blame] | 5082 | if (const ObjCMethodDecl *MD = S.GetMethodIfSelfExpr(selfExpr)) |
| 5083 | if (MD->isClassMethod()) { |
| 5084 | const ObjCInterfaceDecl *Root = 0; |
| 5085 | if (const ObjCInterfaceDecl * IDecl = MD->getClassInterface()) |
| 5086 | do { |
| 5087 | Root = IDecl; |
| 5088 | } while ((IDecl = IDecl->getSuperClass())); |
| 5089 | if (Root) |
Fariborz Jahanian | e20c0a5 | 2011-09-19 18:06:07 +0000 | [diff] [blame^] | 5090 | SelfType = S.Context.getObjCObjectPointerType( |
| 5091 | S.Context.getObjCInterfaceType(Root)); |
Fariborz Jahanian | db8015c | 2011-09-17 19:23:40 +0000 | [diff] [blame] | 5092 | } |
Fariborz Jahanian | e20c0a5 | 2011-09-19 18:06:07 +0000 | [diff] [blame^] | 5093 | return SelfType; |
Fariborz Jahanian | db8015c | 2011-09-17 19:23:40 +0000 | [diff] [blame] | 5094 | } |
| 5095 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5096 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5097 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 5098 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 5099 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 5100 | // FIXME: add a couple examples in this comment. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5101 | static Sema::AssignConvertType |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5102 | checkPointerTypesForAssignment(Sema &S, QualType LHSType, QualType RHSType) { |
| 5103 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5104 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5105 | |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 5106 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5107 | const Type *lhptee, *rhptee; |
| 5108 | Qualifiers lhq, rhq; |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5109 | llvm::tie(lhptee, lhq) = cast<PointerType>(LHSType)->getPointeeType().split(); |
| 5110 | llvm::tie(rhptee, rhq) = cast<PointerType>(RHSType)->getPointeeType().split(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5111 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5112 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5113 | |
| 5114 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 5115 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 5116 | // qualifiers of the type *pointed to* by the right; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5117 | Qualifiers lq; |
| 5118 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5119 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 5120 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 5121 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 5122 | // Ignore lifetime for further calculation. |
| 5123 | lhq.removeObjCLifetime(); |
| 5124 | rhq.removeObjCLifetime(); |
| 5125 | } |
| 5126 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5127 | if (!lhq.compatiblyIncludes(rhq)) { |
| 5128 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 5129 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 5130 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5131 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5132 | // It's okay to add or remove GC or lifetime qualifiers when converting to |
John McCall | 7853595 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5133 | // and from void*. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5134 | else if (lhq.withoutObjCGCAttr().withoutObjCGLifetime() |
| 5135 | .compatiblyIncludes( |
| 5136 | rhq.withoutObjCGCAttr().withoutObjCGLifetime()) |
John McCall | 7853595 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5137 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 5138 | ; // keep old |
| 5139 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5140 | // Treat lifetime mismatches as fatal. |
| 5141 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 5142 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5143 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5144 | // For GCC compatibility, other qualifier mismatches are treated |
| 5145 | // as still compatible in C. |
| 5146 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 5147 | } |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 5148 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5149 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5150 | // incomplete type and the other is a pointer to a qualified or unqualified |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 5151 | // version of void... |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5152 | if (lhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5153 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5154 | return ConvTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5155 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5156 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5157 | assert(rhptee->isFunctionType()); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5158 | return Sema::FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5159 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5160 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5161 | if (rhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5162 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5163 | return ConvTy; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5164 | |
| 5165 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5166 | assert(lhptee->isFunctionType()); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5167 | return Sema::FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5168 | } |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5169 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5170 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 5171 | // unqualified versions of compatible types, ... |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5172 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 5173 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5174 | // Check if the pointee types are compatible ignoring the sign. |
| 5175 | // We explicitly check for char so that we catch "char" vs |
| 5176 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5177 | if (lhptee->isCharType()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5178 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5179 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5180 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5181 | |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5182 | if (rhptee->isCharType()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5183 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5184 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5185 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5186 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5187 | if (ltrans == rtrans) { |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5188 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5189 | // takes priority over sign incompatibility because the sign |
| 5190 | // warning can be disabled. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5191 | if (ConvTy != Sema::Compatible) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5192 | return ConvTy; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5193 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5194 | return Sema::IncompatiblePointerSign; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5195 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5196 | |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5197 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5198 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5199 | // the eventual target type is the same and the pointers have the same |
| 5200 | // level of indirection, this must be the issue. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5201 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5202 | do { |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5203 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5204 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5205 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5206 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5207 | if (lhptee == rhptee) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5208 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5209 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5210 | |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5211 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5212 | return Sema::IncompatiblePointer; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5213 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5214 | return ConvTy; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 5215 | } |
| 5216 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5217 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5218 | /// block pointer types are compatible or whether a block and normal pointer |
| 5219 | /// are compatible. It is more restrict than comparing two function pointer |
| 5220 | // types. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5221 | static Sema::AssignConvertType |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5222 | checkBlockPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5223 | QualType RHSType) { |
| 5224 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5225 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5226 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5227 | QualType lhptee, rhptee; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5228 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5229 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5230 | lhptee = cast<BlockPointerType>(LHSType)->getPointeeType(); |
| 5231 | rhptee = cast<BlockPointerType>(RHSType)->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5232 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5233 | // In C++, the types have to match exactly. |
| 5234 | if (S.getLangOptions().CPlusPlus) |
| 5235 | return Sema::IncompatibleBlockPointer; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5236 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5237 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5238 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5239 | // For blocks we enforce that qualifiers are identical. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5240 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5241 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5242 | |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5243 | if (!S.Context.typesAreBlockPointerCompatible(LHSType, RHSType)) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5244 | return Sema::IncompatibleBlockPointer; |
| 5245 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5246 | return ConvTy; |
| 5247 | } |
| 5248 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5249 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5250 | /// for assignment compatibility. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5251 | static Sema::AssignConvertType |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5252 | checkObjCPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5253 | QualType RHSType) { |
| 5254 | assert(LHSType.isCanonical() && "LHS was not canonicalized!"); |
| 5255 | assert(RHSType.isCanonical() && "RHS was not canonicalized!"); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5256 | |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5257 | if (LHSType->isObjCBuiltinType()) { |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5258 | // Class is not compatible with ObjC object pointers. |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5259 | if (LHSType->isObjCClassType() && !RHSType->isObjCBuiltinType() && |
| 5260 | !RHSType->isObjCQualifiedClassType()) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5261 | return Sema::IncompatiblePointer; |
| 5262 | return Sema::Compatible; |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5263 | } |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5264 | if (RHSType->isObjCBuiltinType()) { |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5265 | if (RHSType->isObjCClassType() && !LHSType->isObjCBuiltinType() && |
| 5266 | !LHSType->isObjCQualifiedClassType()) |
Fariborz Jahanian | d923eb0 | 2011-09-15 20:40:18 +0000 | [diff] [blame] | 5267 | return Sema::IncompatiblePointer; |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5268 | return Sema::Compatible; |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5269 | } |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5270 | QualType lhptee = LHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5271 | QualType rhptee = RHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5272 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5273 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 5274 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5275 | |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5276 | if (S.Context.typesAreCompatible(LHSType, RHSType)) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5277 | return Sema::Compatible; |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5278 | if (LHSType->isObjCQualifiedIdType() || RHSType->isObjCQualifiedIdType()) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5279 | return Sema::IncompatibleObjCQualifiedId; |
| 5280 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5281 | } |
| 5282 | |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5283 | Sema::AssignConvertType |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5284 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5285 | QualType LHSType, QualType RHSType) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5286 | // Fake up an opaque expression. We don't actually care about what |
| 5287 | // cast operations are required, so if CheckAssignmentConstraints |
| 5288 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5289 | // usually happen on valid code. |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5290 | OpaqueValueExpr RHSExpr(Loc, RHSType, VK_RValue); |
| 5291 | ExprResult RHSPtr = &RHSExpr; |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5292 | CastKind K = CK_Invalid; |
| 5293 | |
Richard Trieu | a871b97 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5294 | return CheckAssignmentConstraints(LHSType, RHSPtr, K); |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5295 | } |
| 5296 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5297 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5298 | /// has code to accommodate several GCC extensions when type checking |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 5299 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5300 | /// |
| 5301 | /// int a, *pint; |
| 5302 | /// short *pshort; |
| 5303 | /// struct foo *pfoo; |
| 5304 | /// |
| 5305 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5306 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5307 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5308 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5309 | /// |
| 5310 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5311 | /// C99 spec dictates. |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 5312 | /// |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5313 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5314 | Sema::AssignConvertType |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5315 | Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5316 | CastKind &Kind) { |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5317 | QualType RHSType = RHS.get()->getType(); |
| 5318 | QualType OrigLHSType = LHSType; |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5319 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5320 | // Get canonical types. We're not formatting these types, just comparing |
| 5321 | // them. |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5322 | LHSType = Context.getCanonicalType(LHSType).getUnqualifiedType(); |
| 5323 | RHSType = Context.getCanonicalType(RHSType).getUnqualifiedType(); |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5324 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5325 | // Common case: no conversion required. |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5326 | if (LHSType == RHSType) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5327 | Kind = CK_NoOp; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5328 | return Compatible; |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5329 | } |
| 5330 | |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5331 | // If the left-hand side is a reference type, then we are in a |
| 5332 | // (rare!) case where we've allowed the use of references in C, |
| 5333 | // e.g., as a parameter type in a built-in function. In this case, |
| 5334 | // just make sure that the type referenced is compatible with the |
| 5335 | // right-hand side type. The caller is responsible for adjusting |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5336 | // LHSType so that the resulting expression does not have reference |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5337 | // type. |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5338 | if (const ReferenceType *LHSTypeRef = LHSType->getAs<ReferenceType>()) { |
| 5339 | if (Context.typesAreCompatible(LHSTypeRef->getPointeeType(), RHSType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5340 | Kind = CK_LValueBitCast; |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5341 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5342 | } |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5343 | return Incompatible; |
Fariborz Jahanian | a1e3420 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5344 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5345 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5346 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5347 | // to the same ExtVector type. |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5348 | if (LHSType->isExtVectorType()) { |
| 5349 | if (RHSType->isExtVectorType()) |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5350 | return Incompatible; |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5351 | if (RHSType->isArithmeticType()) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5352 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5353 | // element type. |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5354 | QualType elType = cast<ExtVectorType>(LHSType)->getElementType(); |
| 5355 | if (elType != RHSType) { |
| 5356 | Kind = PrepareScalarCast(*this, RHS, elType); |
| 5357 | RHS = ImpCastExprToType(RHS.take(), elType, Kind); |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5358 | } |
| 5359 | Kind = CK_VectorSplat; |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5360 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5361 | } |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5362 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5363 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5364 | // Conversions to or from vector type. |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5365 | if (LHSType->isVectorType() || RHSType->isVectorType()) { |
| 5366 | if (LHSType->isVectorType() && RHSType->isVectorType()) { |
Bob Wilson | 01856f3 | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5367 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5368 | // vector type and vice versa |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5369 | if (Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
Bob Wilson | 01856f3 | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5370 | Kind = CK_BitCast; |
| 5371 | return Compatible; |
| 5372 | } |
| 5373 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5374 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5375 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5376 | // no bits are changed but the result type is different. |
| 5377 | if (getLangOptions().LaxVectorConversions && |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5378 | (Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType))) { |
John McCall | 3065d04 | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5379 | Kind = CK_BitCast; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5380 | return IncompatibleVectors; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5381 | } |
Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5382 | } |
| 5383 | return Incompatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5384 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5385 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5386 | // Arithmetic conversions. |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5387 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType() && |
| 5388 | !(getLangOptions().CPlusPlus && LHSType->isEnumeralType())) { |
| 5389 | Kind = PrepareScalarCast(*this, RHS, LHSType); |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5390 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5391 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5392 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5393 | // Conversions to normal pointers. |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5394 | if (const PointerType *LHSPointer = dyn_cast<PointerType>(LHSType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5395 | // U* -> T* |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5396 | if (isa<PointerType>(RHSType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5397 | Kind = CK_BitCast; |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5398 | return checkPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5399 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5400 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5401 | // int -> T* |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5402 | if (RHSType->isIntegerType()) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5403 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 5404 | return IntToPointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5405 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5406 | |
| 5407 | // C pointers are not compatible with ObjC object pointers, |
| 5408 | // with two exceptions: |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5409 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5410 | // - conversions to void* |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5411 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5412 | Kind = CK_BitCast; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5413 | return Compatible; |
| 5414 | } |
| 5415 | |
| 5416 | // - conversions from 'Class' to the redefinition type |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5417 | if (RHSType->isObjCClassType() && |
| 5418 | Context.hasSameType(LHSType, |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5419 | Context.getObjCClassRedefinitionType())) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5420 | Kind = CK_BitCast; |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5421 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5422 | } |
Fariborz Jahanian | e20c0a5 | 2011-09-19 18:06:07 +0000 | [diff] [blame^] | 5423 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5424 | Kind = CK_BitCast; |
| 5425 | return IncompatiblePointer; |
| 5426 | } |
| 5427 | |
| 5428 | // U^ -> void* |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5429 | if (RHSType->getAs<BlockPointerType>()) { |
| 5430 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5431 | Kind = CK_BitCast; |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5432 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5433 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5434 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5435 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5436 | return Incompatible; |
| 5437 | } |
| 5438 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5439 | // Conversions to block pointers. |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5440 | if (isa<BlockPointerType>(LHSType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5441 | // U^ -> T^ |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5442 | if (RHSType->isBlockPointerType()) { |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5443 | Kind = CK_BitCast; |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5444 | return checkBlockPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5445 | } |
| 5446 | |
| 5447 | // int or null -> T^ |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5448 | if (RHSType->isIntegerType()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5449 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | 8163b7a | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5450 | return IntToBlockPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5451 | } |
| 5452 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5453 | // id -> T^ |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5454 | if (getLangOptions().ObjC1 && RHSType->isObjCIdType()) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5455 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5456 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5457 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5458 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5459 | // void* -> T^ |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5460 | if (const PointerType *RHSPT = RHSType->getAs<PointerType>()) |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5461 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 5462 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5463 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5464 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5465 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5466 | return Incompatible; |
| 5467 | } |
| 5468 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5469 | // Conversions to Objective-C pointers. |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5470 | if (isa<ObjCObjectPointerType>(LHSType)) { |
Fariborz Jahanian | e20c0a5 | 2011-09-19 18:06:07 +0000 | [diff] [blame^] | 5471 | QualType RHSQT = ConvertObjCSelfToClassRootType(*this, RHS.get()); |
| 5472 | if (!RHSQT.isNull()) |
| 5473 | RHSType = RHSQT; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5474 | // A* -> B* |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5475 | if (RHSType->isObjCObjectPointerType()) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5476 | Kind = CK_BitCast; |
Fariborz Jahanian | 6f472e8 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5477 | Sema::AssignConvertType result = |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5478 | checkObjCPointerTypesForAssignment(*this, LHSType, RHSType); |
Fariborz Jahanian | 6f472e8 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5479 | if (getLangOptions().ObjCAutoRefCount && |
| 5480 | result == Compatible && |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5481 | !CheckObjCARCUnavailableWeakConversion(OrigLHSType, RHSType)) |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5482 | result = IncompatibleObjCWeakRef; |
Fariborz Jahanian | 6f472e8 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5483 | return result; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5484 | } |
| 5485 | |
| 5486 | // int or null -> A* |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5487 | if (RHSType->isIntegerType()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5488 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5489 | return IntToPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5490 | } |
| 5491 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5492 | // In general, C pointers are not compatible with ObjC object pointers, |
| 5493 | // with two exceptions: |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5494 | if (isa<PointerType>(RHSType)) { |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5495 | Kind = CK_CPointerToObjCPointerCast; |
| 5496 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5497 | // - conversions from 'void*' |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5498 | if (RHSType->isVoidPointerType()) { |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5499 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
| 5502 | // - conversions to 'Class' from its redefinition type |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5503 | if (LHSType->isObjCClassType() && |
| 5504 | Context.hasSameType(RHSType, |
Douglas Gregor | 9767347 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5505 | Context.getObjCClassRedefinitionType())) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5506 | return Compatible; |
| 5507 | } |
| 5508 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5509 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5510 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5511 | |
| 5512 | // T^ -> A* |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5513 | if (RHSType->isBlockPointerType()) { |
John McCall | cd78e80 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 5514 | maybeExtendBlockObject(*this, RHS); |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5515 | Kind = CK_BlockPointerToObjCPointerCast; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5516 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5517 | } |
| 5518 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5519 | return Incompatible; |
| 5520 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5521 | |
| 5522 | // Conversions from pointers that are not covered by the above. |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5523 | if (isa<PointerType>(RHSType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5524 | // T* -> _Bool |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5525 | if (LHSType == Context.BoolTy) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5526 | Kind = CK_PointerToBoolean; |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5527 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5528 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5529 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5530 | // T* -> int |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5531 | if (LHSType->isIntegerType()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5532 | Kind = CK_PointerToIntegral; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5533 | return PointerToInt; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5534 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5535 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5536 | return Incompatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5537 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5538 | |
| 5539 | // Conversions from Objective-C pointers that are not covered by the above. |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5540 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5541 | // T* -> _Bool |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5542 | if (LHSType == Context.BoolTy) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5543 | Kind = CK_PointerToBoolean; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5544 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5545 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5546 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5547 | // T* -> int |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5548 | if (LHSType->isIntegerType()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5549 | Kind = CK_PointerToIntegral; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5550 | return PointerToInt; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5551 | } |
| 5552 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5553 | return Incompatible; |
| 5554 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5555 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5556 | // struct A -> struct B |
Richard Trieu | de4958f | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5557 | if (isa<TagType>(LHSType) && isa<TagType>(RHSType)) { |
| 5558 | if (Context.typesAreCompatible(LHSType, RHSType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5559 | Kind = CK_NoOp; |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5560 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5561 | } |
Bill Wendling | 216423b | 2007-05-30 06:30:29 +0000 | [diff] [blame] | 5562 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5563 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5564 | return Incompatible; |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 5565 | } |
| 5566 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5567 | /// \brief Constructs a transparent union from an expression that is |
| 5568 | /// used to initialize the transparent union. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5569 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, |
| 5570 | ExprResult &EResult, QualType UnionType, |
| 5571 | FieldDecl *Field) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5572 | // Build an initializer list that designates the appropriate member |
| 5573 | // of the transparent union. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5574 | Expr *E = EResult.take(); |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5575 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 5576 | &E, 1, |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5577 | SourceLocation()); |
| 5578 | Initializer->setType(UnionType); |
| 5579 | Initializer->setInitializedFieldInUnion(Field); |
| 5580 | |
| 5581 | // Build a compound literal constructing a value of the transparent |
| 5582 | // union type from this initializer list. |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5583 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5584 | EResult = S.Owned( |
| 5585 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 5586 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5587 | } |
| 5588 | |
| 5589 | Sema::AssignConvertType |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5590 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5591 | ExprResult &RHS) { |
| 5592 | QualType RHSType = RHS.get()->getType(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5593 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5594 | // If the ArgType is a Union type, we want to handle a potential |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5595 | // transparent_union GCC extension. |
| 5596 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5597 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5598 | return Incompatible; |
| 5599 | |
| 5600 | // The field to initialize within the transparent union. |
| 5601 | RecordDecl *UD = UT->getDecl(); |
| 5602 | FieldDecl *InitField = 0; |
| 5603 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5604 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5605 | itend = UD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5606 | it != itend; ++it) { |
| 5607 | if (it->getType()->isPointerType()) { |
| 5608 | // If the transparent union contains a pointer type, we allow: |
| 5609 | // 1) void pointer |
| 5610 | // 2) null pointer constant |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5611 | if (RHSType->isPointerType()) |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5612 | if (RHSType->castAs<PointerType>()->getPointeeType()->isVoidType()) { |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5613 | RHS = ImpCastExprToType(RHS.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5614 | InitField = *it; |
| 5615 | break; |
| 5616 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5617 | |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5618 | if (RHS.get()->isNullPointerConstant(Context, |
| 5619 | Expr::NPC_ValueDependentIsNull)) { |
| 5620 | RHS = ImpCastExprToType(RHS.take(), it->getType(), |
| 5621 | CK_NullToPointer); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5622 | InitField = *it; |
| 5623 | break; |
| 5624 | } |
| 5625 | } |
| 5626 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5627 | CastKind Kind = CK_Invalid; |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5628 | if (CheckAssignmentConstraints(it->getType(), RHS, Kind) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5629 | == Compatible) { |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5630 | RHS = ImpCastExprToType(RHS.take(), it->getType(), Kind); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5631 | InitField = *it; |
| 5632 | break; |
| 5633 | } |
| 5634 | } |
| 5635 | |
| 5636 | if (!InitField) |
| 5637 | return Incompatible; |
| 5638 | |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5639 | ConstructTransparentUnion(*this, Context, RHS, ArgType, InitField); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5640 | return Compatible; |
| 5641 | } |
| 5642 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5643 | Sema::AssignConvertType |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5644 | Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &RHS) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5645 | if (getLangOptions().CPlusPlus) { |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5646 | if (!LHSType->isRecordType()) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5647 | // C++ 5.17p3: If the left operand is not of class type, the |
| 5648 | // expression is implicitly converted (C++ 4) to the |
| 5649 | // cv-unqualified type of the left operand. |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5650 | ExprResult Res = PerformImplicitConversion(RHS.get(), |
| 5651 | LHSType.getUnqualifiedType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5652 | AA_Assigning); |
| 5653 | if (Res.isInvalid()) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5654 | return Incompatible; |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5655 | Sema::AssignConvertType result = Compatible; |
| 5656 | if (getLangOptions().ObjCAutoRefCount && |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5657 | !CheckObjCARCUnavailableWeakConversion(LHSType, |
| 5658 | RHS.get()->getType())) |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5659 | result = IncompatibleObjCWeakRef; |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5660 | RHS = move(Res); |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5661 | return result; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5662 | } |
| 5663 | |
| 5664 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 5665 | // structures. |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5666 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5667 | |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5668 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 5669 | // a null pointer constant. |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5670 | if ((LHSType->isPointerType() || |
| 5671 | LHSType->isObjCObjectPointerType() || |
| 5672 | LHSType->isBlockPointerType()) |
| 5673 | && RHS.get()->isNullPointerConstant(Context, |
| 5674 | Expr::NPC_ValueDependentIsNull)) { |
| 5675 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5676 | return Compatible; |
| 5677 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5678 | |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5679 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5680 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 5681 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | ef7c0ff | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 5682 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5683 | // |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5684 | // Suppress this for references: C++ 8.5.3p5. |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5685 | if (!LHSType->isReferenceType()) { |
| 5686 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 5687 | if (RHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5688 | return Incompatible; |
| 5689 | } |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5690 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5691 | CastKind Kind = CK_Invalid; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5692 | Sema::AssignConvertType result = |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5693 | CheckAssignmentConstraints(LHSType, RHS, Kind); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5694 | |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5695 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 5696 | // type of the assignment expression. |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5697 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 5698 | // so that we can use references in built-in functions even in C. |
| 5699 | // The getNonReferenceType() call makes sure that the resulting expression |
| 5700 | // does not have reference type. |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5701 | if (result != Incompatible && RHS.get()->getType() != LHSType) |
| 5702 | RHS = ImpCastExprToType(RHS.take(), |
| 5703 | LHSType.getNonLValueExprType(Context), Kind); |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5704 | return result; |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5707 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &LHS, |
| 5708 | ExprResult &RHS) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5709 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Richard Trieu | eb29914 | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5710 | << LHS.get()->getType() << RHS.get()->getType() |
| 5711 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5712 | return QualType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 5713 | } |
| 5714 | |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5715 | QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5716 | SourceLocation Loc, bool IsCompAssign) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5717 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5718 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5719 | QualType LHSType = |
| 5720 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 5721 | QualType RHSType = |
| 5722 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5723 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5724 | // If the vector types are identical, return. |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5725 | if (LHSType == RHSType) |
| 5726 | return LHSType; |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5727 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5728 | // Handle the case of equivalent AltiVec and GCC vector types |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5729 | if (LHSType->isVectorType() && RHSType->isVectorType() && |
| 5730 | Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
| 5731 | if (LHSType->isExtVectorType()) { |
| 5732 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 5733 | return LHSType; |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5734 | } |
| 5735 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5736 | if (!IsCompAssign) |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5737 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
| 5738 | return RHSType; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5739 | } |
| 5740 | |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5741 | if (getLangOptions().LaxVectorConversions && |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5742 | Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType)) { |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5743 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5744 | // vectors, the total size only needs to be the same. This is a |
| 5745 | // bitcast; no bits are changed but the result type is different. |
| 5746 | // FIXME: Should we really be allowing this? |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5747 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 5748 | return LHSType; |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5749 | } |
| 5750 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5751 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 5752 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 5753 | bool swapped = false; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5754 | if (RHSType->isExtVectorType() && !IsCompAssign) { |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5755 | swapped = true; |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5756 | std::swap(RHS, LHS); |
| 5757 | std::swap(RHSType, LHSType); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5758 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5759 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5760 | // Handle the case of an ext vector and scalar. |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5761 | if (const ExtVectorType *LV = LHSType->getAs<ExtVectorType>()) { |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5762 | QualType EltTy = LV->getElementType(); |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5763 | if (EltTy->isIntegralType(Context) && RHSType->isIntegralType(Context)) { |
| 5764 | int order = Context.getIntegerTypeOrder(EltTy, RHSType); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5765 | if (order > 0) |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5766 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_IntegralCast); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5767 | if (order >= 0) { |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5768 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 5769 | if (swapped) std::swap(RHS, LHS); |
| 5770 | return LHSType; |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5771 | } |
| 5772 | } |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5773 | if (EltTy->isRealFloatingType() && RHSType->isScalarType() && |
| 5774 | RHSType->isRealFloatingType()) { |
| 5775 | int order = Context.getFloatingTypeOrder(EltTy, RHSType); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5776 | if (order > 0) |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5777 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_FloatingCast); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5778 | if (order >= 0) { |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5779 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 5780 | if (swapped) std::swap(RHS, LHS); |
| 5781 | return LHSType; |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5782 | } |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5783 | } |
| 5784 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5785 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5786 | // Vectors of different size or scalar and non-ext-vector are errors. |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5787 | if (swapped) std::swap(RHS, LHS); |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5788 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5789 | << LHS.get()->getType() << RHS.get()->getType() |
| 5790 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 5791 | return QualType(); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
Richard Trieu | f8916e1 | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 5794 | // checkArithmeticNull - Detect when a NULL constant is used improperly in an |
| 5795 | // expression. These are mainly cases where the null pointer is used as an |
| 5796 | // integer instead of a pointer. |
| 5797 | static void checkArithmeticNull(Sema &S, ExprResult &LHS, ExprResult &RHS, |
| 5798 | SourceLocation Loc, bool IsCompare) { |
| 5799 | // The canonical way to check for a GNU null is with isNullPointerConstant, |
| 5800 | // but we use a bit of a hack here for speed; this is a relatively |
| 5801 | // hot path, and isNullPointerConstant is slow. |
| 5802 | bool LHSNull = isa<GNUNullExpr>(LHS.get()->IgnoreParenImpCasts()); |
| 5803 | bool RHSNull = isa<GNUNullExpr>(RHS.get()->IgnoreParenImpCasts()); |
| 5804 | |
| 5805 | QualType NonNullType = LHSNull ? RHS.get()->getType() : LHS.get()->getType(); |
| 5806 | |
| 5807 | // Avoid analyzing cases where the result will either be invalid (and |
| 5808 | // diagnosed as such) or entirely valid and not something to warn about. |
| 5809 | if ((!LHSNull && !RHSNull) || NonNullType->isBlockPointerType() || |
| 5810 | NonNullType->isMemberPointerType() || NonNullType->isFunctionType()) |
| 5811 | return; |
| 5812 | |
| 5813 | // Comparison operations would not make sense with a null pointer no matter |
| 5814 | // what the other expression is. |
| 5815 | if (!IsCompare) { |
| 5816 | S.Diag(Loc, diag::warn_null_in_arithmetic_operation) |
| 5817 | << (LHSNull ? LHS.get()->getSourceRange() : SourceRange()) |
| 5818 | << (RHSNull ? RHS.get()->getSourceRange() : SourceRange()); |
| 5819 | return; |
| 5820 | } |
| 5821 | |
| 5822 | // The rest of the operations only make sense with a null pointer |
| 5823 | // if the other expression is a pointer. |
| 5824 | if (LHSNull == RHSNull || NonNullType->isAnyPointerType() || |
| 5825 | NonNullType->canDecayToPointerType()) |
| 5826 | return; |
| 5827 | |
| 5828 | S.Diag(Loc, diag::warn_null_in_comparison_operation) |
| 5829 | << LHSNull /* LHS is NULL */ << NonNullType |
| 5830 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5831 | } |
| 5832 | |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5833 | QualType Sema::CheckMultiplyDivideOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5834 | SourceLocation Loc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5835 | bool IsCompAssign, bool IsDiv) { |
Richard Trieu | f8916e1 | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 5836 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 5837 | |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5838 | if (LHS.get()->getType()->isVectorType() || |
| 5839 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5840 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5841 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5842 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5843 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5844 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5845 | |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5846 | if (!LHS.get()->getType()->isArithmeticType() || |
| 5847 | !RHS.get()->getType()->isArithmeticType()) |
| 5848 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5849 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5850 | // Check for division by zero. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5851 | if (IsDiv && |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5852 | RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5853 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5854 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_division_by_zero) |
| 5855 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5856 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5857 | return compType; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 5858 | } |
| 5859 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5860 | QualType Sema::CheckRemainderOperands( |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5861 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | f8916e1 | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 5862 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 5863 | |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5864 | if (LHS.get()->getType()->isVectorType() || |
| 5865 | RHS.get()->getType()->isVectorType()) { |
| 5866 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 5867 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5868 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5869 | return InvalidOperands(Loc, LHS, RHS); |
Daniel Dunbar | 0d2bfec | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 5870 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5871 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5872 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5873 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5874 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5875 | |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5876 | if (!LHS.get()->getType()->isIntegerType() || |
| 5877 | !RHS.get()->getType()->isIntegerType()) |
| 5878 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5879 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5880 | // Check for remainder by zero. |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5881 | if (RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5882 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 859d23f | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5883 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_remainder_by_zero) |
| 5884 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5885 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5886 | return compType; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5887 | } |
| 5888 | |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5889 | /// \brief Diagnose invalid arithmetic on two void pointers. |
| 5890 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5891 | Expr *LHSExpr, Expr *RHSExpr) { |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5892 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5893 | ? diag::err_typecheck_pointer_arith_void_type |
| 5894 | : diag::ext_gnu_void_ptr) |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5895 | << 1 /* two pointers */ << LHSExpr->getSourceRange() |
| 5896 | << RHSExpr->getSourceRange(); |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5897 | } |
| 5898 | |
| 5899 | /// \brief Diagnose invalid arithmetic on a void pointer. |
| 5900 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, |
| 5901 | Expr *Pointer) { |
| 5902 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5903 | ? diag::err_typecheck_pointer_arith_void_type |
| 5904 | : diag::ext_gnu_void_ptr) |
| 5905 | << 0 /* one pointer */ << Pointer->getSourceRange(); |
| 5906 | } |
| 5907 | |
| 5908 | /// \brief Diagnose invalid arithmetic on two function pointers. |
| 5909 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, |
| 5910 | Expr *LHS, Expr *RHS) { |
| 5911 | assert(LHS->getType()->isAnyPointerType()); |
| 5912 | assert(RHS->getType()->isAnyPointerType()); |
| 5913 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5914 | ? diag::err_typecheck_pointer_arith_function_type |
| 5915 | : diag::ext_gnu_ptr_func_arith) |
| 5916 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() |
| 5917 | // We only show the second type if it differs from the first. |
| 5918 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), |
| 5919 | RHS->getType()) |
| 5920 | << RHS->getType()->getPointeeType() |
| 5921 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5922 | } |
| 5923 | |
| 5924 | /// \brief Diagnose invalid arithmetic on a function pointer. |
| 5925 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, |
| 5926 | Expr *Pointer) { |
| 5927 | assert(Pointer->getType()->isAnyPointerType()); |
| 5928 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5929 | ? diag::err_typecheck_pointer_arith_function_type |
| 5930 | : diag::ext_gnu_ptr_func_arith) |
| 5931 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() |
| 5932 | << 0 /* one pointer, so only one type */ |
| 5933 | << Pointer->getSourceRange(); |
| 5934 | } |
| 5935 | |
Richard Trieu | 993f3ab | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 5936 | /// \brief Emit error if Operand is incomplete pointer type |
Richard Trieu | aba2280 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5937 | /// |
| 5938 | /// \returns True if pointer has incomplete type |
| 5939 | static bool checkArithmeticIncompletePointerType(Sema &S, SourceLocation Loc, |
| 5940 | Expr *Operand) { |
| 5941 | if ((Operand->getType()->isPointerType() && |
| 5942 | !Operand->getType()->isDependentType()) || |
| 5943 | Operand->getType()->isObjCObjectPointerType()) { |
| 5944 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5945 | if (S.RequireCompleteType( |
| 5946 | Loc, PointeeTy, |
| 5947 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 5948 | << PointeeTy << Operand->getSourceRange())) |
| 5949 | return true; |
| 5950 | } |
| 5951 | return false; |
| 5952 | } |
| 5953 | |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5954 | /// \brief Check the validity of an arithmetic pointer operand. |
| 5955 | /// |
| 5956 | /// If the operand has pointer type, this code will check for pointer types |
| 5957 | /// which are invalid in arithmetic operations. These will be diagnosed |
| 5958 | /// appropriately, including whether or not the use is supported as an |
| 5959 | /// extension. |
| 5960 | /// |
| 5961 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5962 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, |
| 5963 | Expr *Operand) { |
| 5964 | if (!Operand->getType()->isAnyPointerType()) return true; |
| 5965 | |
| 5966 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5967 | if (PointeeTy->isVoidType()) { |
| 5968 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); |
| 5969 | return !S.getLangOptions().CPlusPlus; |
| 5970 | } |
| 5971 | if (PointeeTy->isFunctionType()) { |
| 5972 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); |
| 5973 | return !S.getLangOptions().CPlusPlus; |
| 5974 | } |
| 5975 | |
Richard Trieu | aba2280 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5976 | if (checkArithmeticIncompletePointerType(S, Loc, Operand)) return false; |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5977 | |
| 5978 | return true; |
| 5979 | } |
| 5980 | |
| 5981 | /// \brief Check the validity of a binary arithmetic operation w.r.t. pointer |
| 5982 | /// operands. |
| 5983 | /// |
| 5984 | /// This routine will diagnose any invalid arithmetic on pointer operands much |
| 5985 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic |
| 5986 | /// for emitting a single diagnostic even for operations where both LHS and RHS |
| 5987 | /// are (potentially problematic) pointers. |
| 5988 | /// |
| 5989 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5990 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5991 | Expr *LHSExpr, Expr *RHSExpr) { |
| 5992 | bool isLHSPointer = LHSExpr->getType()->isAnyPointerType(); |
| 5993 | bool isRHSPointer = RHSExpr->getType()->isAnyPointerType(); |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5994 | if (!isLHSPointer && !isRHSPointer) return true; |
| 5995 | |
| 5996 | QualType LHSPointeeTy, RHSPointeeTy; |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5997 | if (isLHSPointer) LHSPointeeTy = LHSExpr->getType()->getPointeeType(); |
| 5998 | if (isRHSPointer) RHSPointeeTy = RHSExpr->getType()->getPointeeType(); |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5999 | |
| 6000 | // Check for arithmetic on pointers to incomplete types. |
| 6001 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); |
| 6002 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); |
| 6003 | if (isLHSVoidPtr || isRHSVoidPtr) { |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6004 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHSExpr); |
| 6005 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHSExpr); |
| 6006 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6007 | |
| 6008 | return !S.getLangOptions().CPlusPlus; |
| 6009 | } |
| 6010 | |
| 6011 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); |
| 6012 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); |
| 6013 | if (isLHSFuncPtr || isRHSFuncPtr) { |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6014 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHSExpr); |
| 6015 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, |
| 6016 | RHSExpr); |
| 6017 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6018 | |
| 6019 | return !S.getLangOptions().CPlusPlus; |
| 6020 | } |
| 6021 | |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6022 | if (checkArithmeticIncompletePointerType(S, Loc, LHSExpr)) return false; |
| 6023 | if (checkArithmeticIncompletePointerType(S, Loc, RHSExpr)) return false; |
Richard Trieu | aba2280 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6024 | |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6025 | return true; |
| 6026 | } |
| 6027 | |
Richard Trieu | b10c631 | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6028 | /// \brief Check bad cases where we step over interface counts. |
| 6029 | static bool checkArithmethicPointerOnNonFragileABI(Sema &S, |
| 6030 | SourceLocation OpLoc, |
| 6031 | Expr *Op) { |
| 6032 | assert(Op->getType()->isAnyPointerType()); |
| 6033 | QualType PointeeTy = Op->getType()->getPointeeType(); |
| 6034 | if (!PointeeTy->isObjCObjectType() || !S.LangOpts.ObjCNonFragileABI) |
| 6035 | return true; |
| 6036 | |
| 6037 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 6038 | << PointeeTy << Op->getSourceRange(); |
| 6039 | return false; |
| 6040 | } |
| 6041 | |
Richard Trieu | 993f3ab | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 6042 | /// \brief Emit error when two pointers are incompatible. |
Richard Trieu | b10c631 | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6043 | static void diagnosePointerIncompatibility(Sema &S, SourceLocation Loc, |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6044 | Expr *LHSExpr, Expr *RHSExpr) { |
| 6045 | assert(LHSExpr->getType()->isAnyPointerType()); |
| 6046 | assert(RHSExpr->getType()->isAnyPointerType()); |
Richard Trieu | b10c631 | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6047 | S.Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6048 | << LHSExpr->getType() << RHSExpr->getType() << LHSExpr->getSourceRange() |
| 6049 | << RHSExpr->getSourceRange(); |
Richard Trieu | b10c631 | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6050 | } |
| 6051 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6052 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6053 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, QualType* CompLHSTy) { |
Richard Trieu | f8916e1 | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6054 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6055 | |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6056 | if (LHS.get()->getType()->isVectorType() || |
| 6057 | RHS.get()->getType()->isVectorType()) { |
| 6058 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6059 | if (CompLHSTy) *CompLHSTy = compType; |
| 6060 | return compType; |
| 6061 | } |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6062 | |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6063 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 6064 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6065 | return QualType(); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6066 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 6067 | // handle the common case first (both operands are arithmetic). |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6068 | if (LHS.get()->getType()->isArithmeticType() && |
| 6069 | RHS.get()->getType()->isArithmeticType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6070 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6071 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6072 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6073 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6074 | // Put any potential pointer into PExp |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6075 | Expr* PExp = LHS.get(), *IExp = RHS.get(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6076 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6077 | std::swap(PExp, IExp); |
| 6078 | |
Richard Trieu | b420bca | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6079 | if (!PExp->getType()->isAnyPointerType()) |
| 6080 | return InvalidOperands(Loc, LHS, RHS); |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6081 | |
Richard Trieu | b420bca | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6082 | if (!IExp->getType()->isIntegerType()) |
| 6083 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6084 | |
Richard Trieu | b420bca | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6085 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) |
| 6086 | return QualType(); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6087 | |
Richard Trieu | b420bca | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6088 | // Diagnose bad cases where we step over interface counts. |
| 6089 | if (!checkArithmethicPointerOnNonFragileABI(*this, Loc, PExp)) |
| 6090 | return QualType(); |
| 6091 | |
| 6092 | // Check array bounds for pointer arithemtic |
| 6093 | CheckArrayAccess(PExp, IExp); |
| 6094 | |
| 6095 | if (CompLHSTy) { |
| 6096 | QualType LHSTy = Context.isPromotableBitField(LHS.get()); |
| 6097 | if (LHSTy.isNull()) { |
| 6098 | LHSTy = LHS.get()->getType(); |
| 6099 | if (LHSTy->isPromotableIntegerType()) |
| 6100 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6101 | } |
Richard Trieu | b420bca | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6102 | *CompLHSTy = LHSTy; |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6103 | } |
| 6104 | |
Richard Trieu | b420bca | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6105 | return PExp->getType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6106 | } |
| 6107 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6108 | // C99 6.5.6 |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6109 | QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6110 | SourceLocation Loc, |
| 6111 | QualType* CompLHSTy) { |
Richard Trieu | f8916e1 | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6112 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6113 | |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6114 | if (LHS.get()->getType()->isVectorType() || |
| 6115 | RHS.get()->getType()->isVectorType()) { |
| 6116 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6117 | if (CompLHSTy) *CompLHSTy = compType; |
| 6118 | return compType; |
| 6119 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6120 | |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6121 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 6122 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6123 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6124 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6125 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6126 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6127 | // Handle the common case first (both operands are arithmetic). |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6128 | if (LHS.get()->getType()->isArithmeticType() && |
| 6129 | RHS.get()->getType()->isArithmeticType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6130 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6131 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6132 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6133 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6134 | // Either ptr - int or ptr - ptr. |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6135 | if (LHS.get()->getType()->isAnyPointerType()) { |
| 6136 | QualType lpointee = LHS.get()->getType()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6137 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6138 | // Diagnose bad cases where we step over interface counts. |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6139 | if (!checkArithmethicPointerOnNonFragileABI(*this, Loc, LHS.get())) |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6140 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6141 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6142 | // The result type of a pointer-int computation is the pointer type. |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6143 | if (RHS.get()->getType()->isIntegerType()) { |
| 6144 | if (!checkArithmeticOpPointerOperand(*this, Loc, LHS.get())) |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6145 | return QualType(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6146 | |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6147 | Expr *IExpr = RHS.get()->IgnoreParenCasts(); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6148 | UnaryOperator negRex(IExpr, UO_Minus, IExpr->getType(), VK_RValue, |
| 6149 | OK_Ordinary, IExpr->getExprLoc()); |
| 6150 | // Check array bounds for pointer arithemtic |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6151 | CheckArrayAccess(LHS.get()->IgnoreParenCasts(), &negRex); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6152 | |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6153 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
| 6154 | return LHS.get()->getType(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6155 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6156 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6157 | // Handle pointer-pointer subtractions. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6158 | if (const PointerType *RHSPTy |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6159 | = RHS.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 1974e53 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6160 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6161 | |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6162 | if (getLangOptions().CPlusPlus) { |
| 6163 | // Pointee types must be the same: C++ [expr.add] |
| 6164 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6165 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6166 | } |
| 6167 | } else { |
| 6168 | // Pointee types must be compatible C99 6.5.6p3 |
| 6169 | if (!Context.typesAreCompatible( |
| 6170 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6171 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6172 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6173 | return QualType(); |
| 6174 | } |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6175 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6176 | |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6177 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6178 | LHS.get(), RHS.get())) |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6179 | return QualType(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6180 | |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6181 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6182 | return Context.getPointerDiffType(); |
| 6183 | } |
| 6184 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6185 | |
Richard Trieu | 4ae7e97 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6186 | return InvalidOperands(Loc, LHS, RHS); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6187 | } |
| 6188 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6189 | static bool isScopedEnumerationType(QualType T) { |
| 6190 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6191 | return ET->getDecl()->isScoped(); |
| 6192 | return false; |
| 6193 | } |
| 6194 | |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6195 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6196 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6197 | QualType LHSType) { |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6198 | llvm::APSInt Right; |
| 6199 | // Check right/shifter operand |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6200 | if (RHS.get()->isValueDependent() || |
| 6201 | !RHS.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6202 | return; |
| 6203 | |
| 6204 | if (Right.isNegative()) { |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6205 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 63657fe | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6206 | S.PDiag(diag::warn_shift_negative) |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6207 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6208 | return; |
| 6209 | } |
| 6210 | llvm::APInt LeftBits(Right.getBitWidth(), |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6211 | S.Context.getTypeSize(LHS.get()->getType())); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6212 | if (Right.uge(LeftBits)) { |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6213 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 26bbc3d | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6214 | S.PDiag(diag::warn_shift_gt_typewidth) |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6215 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6216 | return; |
| 6217 | } |
| 6218 | if (Opc != BO_Shl) |
| 6219 | return; |
| 6220 | |
| 6221 | // When left shifting an ICE which is signed, we can check for overflow which |
| 6222 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 6223 | // integers have defined behavior modulo one more than the maximum value |
| 6224 | // representable in the result type, so never warn for those. |
| 6225 | llvm::APSInt Left; |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6226 | if (LHS.get()->isValueDependent() || |
| 6227 | !LHS.get()->isIntegerConstantExpr(Left, S.Context) || |
| 6228 | LHSType->hasUnsignedIntegerRepresentation()) |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6229 | return; |
| 6230 | llvm::APInt ResultBits = |
| 6231 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 6232 | if (LeftBits.uge(ResultBits)) |
| 6233 | return; |
| 6234 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 6235 | Result = Result.shl(Right); |
| 6236 | |
Ted Kremenek | 70f05fd | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6237 | // Print the bit representation of the signed integer as an unsigned |
| 6238 | // hexadecimal number. |
| 6239 | llvm::SmallString<40> HexResult; |
| 6240 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 6241 | |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6242 | // If we are only missing a sign bit, this is less likely to result in actual |
| 6243 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 6244 | // expected value. Thus we place this behind a different warning that can be |
| 6245 | // turned off separately if needed. |
| 6246 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | 70f05fd | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6247 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6248 | << HexResult.str() << LHSType |
| 6249 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6250 | return; |
| 6251 | } |
| 6252 | |
| 6253 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6254 | << HexResult.str() << Result.getMinSignedBits() << LHSType |
| 6255 | << Left.getBitWidth() << LHS.get()->getSourceRange() |
| 6256 | << RHS.get()->getSourceRange(); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6257 | } |
| 6258 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6259 | // C99 6.5.7 |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6260 | QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6261 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6262 | bool IsCompAssign) { |
Richard Trieu | f8916e1 | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6263 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6264 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6265 | // C99 6.5.7p2: Each of the operands shall have integer type. |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6266 | if (!LHS.get()->getType()->hasIntegerRepresentation() || |
| 6267 | !RHS.get()->getType()->hasIntegerRepresentation()) |
| 6268 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6269 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6270 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6271 | // hasIntegerRepresentation() above instead of this. |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6272 | if (isScopedEnumerationType(LHS.get()->getType()) || |
| 6273 | isScopedEnumerationType(RHS.get()->getType())) { |
| 6274 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6275 | } |
| 6276 | |
Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6277 | // Vector shifts promote their scalar inputs to vector type. |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6278 | if (LHS.get()->getType()->isVectorType() || |
| 6279 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6280 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6281 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6282 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6283 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6284 | |
John McCall | 57cdd88 | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6285 | // For the LHS, do usual unary conversions, but then reset them away |
| 6286 | // if this is a compound assignment. |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6287 | ExprResult OldLHS = LHS; |
| 6288 | LHS = UsualUnaryConversions(LHS.take()); |
| 6289 | if (LHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6290 | return QualType(); |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6291 | QualType LHSType = LHS.get()->getType(); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6292 | if (IsCompAssign) LHS = OldLHS; |
John McCall | 57cdd88 | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6293 | |
| 6294 | // The RHS is simpler. |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6295 | RHS = UsualUnaryConversions(RHS.take()); |
| 6296 | if (RHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6297 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6298 | |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6299 | // Sanity-check shift operands |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6300 | DiagnoseBadShiftValues(*this, LHS, RHS, Loc, Opc, LHSType); |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6301 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6302 | // "The type of the result is that of the promoted left operand." |
Richard Trieu | e4a19fb | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6303 | return LHSType; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6304 | } |
| 6305 | |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6306 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6307 | if (DeclContext *DC = D->getDeclContext()) { |
| 6308 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6309 | return true; |
| 6310 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6311 | return FD->isFunctionTemplateSpecialization(); |
| 6312 | } |
| 6313 | return false; |
| 6314 | } |
| 6315 | |
Richard Trieu | eea56f7 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6316 | /// If two different enums are compared, raise a warning. |
Richard Trieu | 1762d7c | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6317 | static void checkEnumComparison(Sema &S, SourceLocation Loc, ExprResult &LHS, |
| 6318 | ExprResult &RHS) { |
| 6319 | QualType LHSStrippedType = LHS.get()->IgnoreParenImpCasts()->getType(); |
| 6320 | QualType RHSStrippedType = RHS.get()->IgnoreParenImpCasts()->getType(); |
Richard Trieu | eea56f7 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6321 | |
| 6322 | const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>(); |
| 6323 | if (!LHSEnumType) |
| 6324 | return; |
| 6325 | const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>(); |
| 6326 | if (!RHSEnumType) |
| 6327 | return; |
| 6328 | |
| 6329 | // Ignore anonymous enums. |
| 6330 | if (!LHSEnumType->getDecl()->getIdentifier()) |
| 6331 | return; |
| 6332 | if (!RHSEnumType->getDecl()->getIdentifier()) |
| 6333 | return; |
| 6334 | |
| 6335 | if (S.Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) |
| 6336 | return; |
| 6337 | |
| 6338 | S.Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6339 | << LHSStrippedType << RHSStrippedType |
Richard Trieu | 1762d7c | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6340 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | eea56f7 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6341 | } |
| 6342 | |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6343 | /// \brief Diagnose bad pointer comparisons. |
| 6344 | static void diagnoseDistinctPointerComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | 1762d7c | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6345 | ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6346 | bool IsError) { |
| 6347 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_distinct_pointers |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6348 | : diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | 1762d7c | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6349 | << LHS.get()->getType() << RHS.get()->getType() |
| 6350 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6351 | } |
| 6352 | |
| 6353 | /// \brief Returns false if the pointers are converted to a composite type, |
| 6354 | /// true otherwise. |
| 6355 | static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, |
Richard Trieu | 1762d7c | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6356 | ExprResult &LHS, ExprResult &RHS) { |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6357 | // C++ [expr.rel]p2: |
| 6358 | // [...] Pointer conversions (4.10) and qualification |
| 6359 | // conversions (4.4) are performed on pointer operands (or on |
| 6360 | // a pointer operand and a null pointer constant) to bring |
| 6361 | // them to their composite pointer type. [...] |
| 6362 | // |
| 6363 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
| 6364 | // comparisons of pointers. |
| 6365 | |
| 6366 | // C++ [expr.eq]p2: |
| 6367 | // In addition, pointers to members can be compared, or a pointer to |
| 6368 | // member and a null pointer constant. Pointer to member conversions |
| 6369 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6370 | // them to a common type. If one operand is a null pointer constant, |
| 6371 | // the common type is the type of the other operand. Otherwise, the |
| 6372 | // common type is a pointer to member type similar (4.4) to the type |
| 6373 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6374 | // that is the union of the cv-qualification signatures of the operand |
| 6375 | // types. |
| 6376 | |
Richard Trieu | 1762d7c | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6377 | QualType LHSType = LHS.get()->getType(); |
| 6378 | QualType RHSType = RHS.get()->getType(); |
| 6379 | assert((LHSType->isPointerType() && RHSType->isPointerType()) || |
| 6380 | (LHSType->isMemberPointerType() && RHSType->isMemberPointerType())); |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6381 | |
| 6382 | bool NonStandardCompositeType = false; |
Richard Trieu | 48277e5 | 2011-09-02 21:44:27 +0000 | [diff] [blame] | 6383 | bool *BoolPtr = S.isSFINAEContext() ? 0 : &NonStandardCompositeType; |
Richard Trieu | 1762d7c | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6384 | QualType T = S.FindCompositePointerType(Loc, LHS, RHS, BoolPtr); |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6385 | if (T.isNull()) { |
Richard Trieu | 1762d7c | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6386 | diagnoseDistinctPointerComparison(S, Loc, LHS, RHS, /*isError*/true); |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6387 | return true; |
| 6388 | } |
| 6389 | |
| 6390 | if (NonStandardCompositeType) |
| 6391 | S.Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Richard Trieu | 1762d7c | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6392 | << LHSType << RHSType << T << LHS.get()->getSourceRange() |
| 6393 | << RHS.get()->getSourceRange(); |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6394 | |
Richard Trieu | 1762d7c | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6395 | LHS = S.ImpCastExprToType(LHS.take(), T, CK_BitCast); |
| 6396 | RHS = S.ImpCastExprToType(RHS.take(), T, CK_BitCast); |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6397 | return false; |
| 6398 | } |
| 6399 | |
| 6400 | static void diagnoseFunctionPointerToVoidComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | 1762d7c | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6401 | ExprResult &LHS, |
| 6402 | ExprResult &RHS, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6403 | bool IsError) { |
| 6404 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_fptr_to_void |
| 6405 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Richard Trieu | 1762d7c | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6406 | << LHS.get()->getType() << RHS.get()->getType() |
| 6407 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6408 | } |
| 6409 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6410 | // C99 6.5.8, C++ [expr.rel] |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6411 | QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6412 | SourceLocation Loc, unsigned OpaqueOpc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6413 | bool IsRelational) { |
Richard Trieu | f8916e1 | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6414 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/true); |
| 6415 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6416 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6417 | |
Chris Lattner | 9a152e2 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6418 | // Handle vector comparisons separately. |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6419 | if (LHS.get()->getType()->isVectorType() || |
| 6420 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6421 | return CheckVectorCompareOperands(LHS, RHS, Loc, IsRelational); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6422 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6423 | QualType LHSType = LHS.get()->getType(); |
| 6424 | QualType RHSType = RHS.get()->getType(); |
Benjamin Kramer | a66aaa9 | 2011-09-03 08:46:20 +0000 | [diff] [blame] | 6425 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6426 | Expr *LHSStripped = LHS.get()->IgnoreParenImpCasts(); |
| 6427 | Expr *RHSStripped = RHS.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6428 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6429 | checkEnumComparison(*this, Loc, LHS, RHS); |
Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6430 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6431 | if (!LHSType->hasFloatingRepresentation() && |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6432 | !(LHSType->isBlockPointerType() && IsRelational) && |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6433 | !LHS.get()->getLocStart().isMacroID() && |
| 6434 | !RHS.get()->getLocStart().isMacroID()) { |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6435 | // For non-floating point types, check for self-comparisons of the form |
| 6436 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6437 | // often indicate logic errors in the program. |
Chandler Carruth | 65a3818 | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6438 | // |
| 6439 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6440 | // expansion. Also don't warn about comparisons which are only self |
| 6441 | // comparisons within a template specialization. The warnings should catch |
| 6442 | // obvious cases in the definition of the template anyways. The idea is to |
| 6443 | // warn when the typed comparison operator will always evaluate to the same |
| 6444 | // result. |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6445 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6446 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | 853734e | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6447 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6448 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6449 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6450 | << 0 // self- |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6451 | << (Opc == BO_EQ |
| 6452 | || Opc == BO_LE |
| 6453 | || Opc == BO_GE)); |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6454 | } else if (LHSType->isArrayType() && RHSType->isArrayType() && |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6455 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6456 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6457 | // what is it always going to eval to? |
| 6458 | char always_evals_to; |
| 6459 | switch(Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6460 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6461 | always_evals_to = 0; // false |
| 6462 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6463 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6464 | always_evals_to = 1; // true |
| 6465 | break; |
| 6466 | default: |
| 6467 | // best we can say is 'a constant' |
| 6468 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6469 | break; |
| 6470 | } |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6471 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6472 | << 1 // array |
| 6473 | << always_evals_to); |
| 6474 | } |
| 6475 | } |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6476 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6477 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6478 | if (isa<CastExpr>(LHSStripped)) |
| 6479 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6480 | if (isa<CastExpr>(RHSStripped)) |
| 6481 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6482 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6483 | // Warn about comparisons against a string constant (unless the other |
| 6484 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6485 | Expr *literalString = 0; |
| 6486 | Expr *literalStringStripped = 0; |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6487 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6488 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6489 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6490 | literalString = LHS.get(); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6491 | literalStringStripped = LHSStripped; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6492 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 6493 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6494 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6495 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6496 | literalString = RHS.get(); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6497 | literalStringStripped = RHSStripped; |
| 6498 | } |
| 6499 | |
| 6500 | if (literalString) { |
| 6501 | std::string resultComparison; |
| 6502 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6503 | case BO_LT: resultComparison = ") < 0"; break; |
| 6504 | case BO_GT: resultComparison = ") > 0"; break; |
| 6505 | case BO_LE: resultComparison = ") <= 0"; break; |
| 6506 | case BO_GE: resultComparison = ") >= 0"; break; |
| 6507 | case BO_EQ: resultComparison = ") == 0"; break; |
| 6508 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6509 | default: assert(false && "Invalid comparison operator"); |
| 6510 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6511 | |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6512 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | 49862b8 | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 6513 | PDiag(diag::warn_stringcompare) |
| 6514 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 800b66b | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 6515 | << literalString->getSourceRange()); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6516 | } |
Ted Kremenek | e451eae | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 6517 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6518 | |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6519 | // C99 6.5.8p3 / C99 6.5.9p4 |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6520 | if (LHS.get()->getType()->isArithmeticType() && |
| 6521 | RHS.get()->getType()->isArithmeticType()) { |
| 6522 | UsualArithmeticConversions(LHS, RHS); |
| 6523 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6524 | return QualType(); |
| 6525 | } |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6526 | else { |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6527 | LHS = UsualUnaryConversions(LHS.take()); |
| 6528 | if (LHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6529 | return QualType(); |
| 6530 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6531 | RHS = UsualUnaryConversions(RHS.take()); |
| 6532 | if (RHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6533 | return QualType(); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6534 | } |
| 6535 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6536 | LHSType = LHS.get()->getType(); |
| 6537 | RHSType = RHS.get()->getType(); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6538 | |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6539 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 1bdd688 | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 6540 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6541 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6542 | if (IsRelational) { |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6543 | if (LHSType->isRealType() && RHSType->isRealType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6544 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6545 | } else { |
Ted Kremenek | e2763b0 | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 6546 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6547 | if (LHSType->hasFloatingRepresentation()) |
| 6548 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6549 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6550 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6551 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6552 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6553 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6554 | bool LHSIsNull = LHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6555 | Expr::NPC_ValueDependentIsNull); |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6556 | bool RHSIsNull = RHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6557 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6558 | |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6559 | // All of the following pointer-related warnings are GCC extensions, except |
| 6560 | // when handling null pointer constants. |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6561 | if (LHSType->isPointerType() && RHSType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6562 | QualType LCanPointeeTy = |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6563 | LHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6564 | QualType RCanPointeeTy = |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6565 | RHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6566 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6567 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6568 | if (LCanPointeeTy == RCanPointeeTy) |
| 6569 | return ResultTy; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6570 | if (!IsRelational && |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6571 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6572 | // Valid unless comparison between non-null pointer and function pointer |
| 6573 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6574 | // In a SFINAE context, we treat this as a hard error to maintain |
| 6575 | // conformance with the C++ standard. |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6576 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6577 | && !LHSIsNull && !RHSIsNull) { |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6578 | diagnoseFunctionPointerToVoidComparison( |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6579 | *this, Loc, LHS, RHS, /*isError*/ isSFINAEContext()); |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6580 | |
| 6581 | if (isSFINAEContext()) |
| 6582 | return QualType(); |
| 6583 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6584 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6585 | return ResultTy; |
| 6586 | } |
| 6587 | } |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6588 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6589 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6590 | return QualType(); |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6591 | else |
| 6592 | return ResultTy; |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6593 | } |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6594 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 6595 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 6596 | RCanPointeeTy.getUnqualifiedType())) { |
| 6597 | // Valid unless a relational comparison of function pointers |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6598 | if (IsRelational && LCanPointeeTy->isFunctionType()) { |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6599 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6600 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6601 | << RHS.get()->getSourceRange(); |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6602 | } |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6603 | } else if (!IsRelational && |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6604 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6605 | // Valid unless comparison between non-null pointer and function pointer |
| 6606 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6607 | && !LHSIsNull && !RHSIsNull) |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6608 | diagnoseFunctionPointerToVoidComparison(*this, Loc, LHS, RHS, |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6609 | /*isError*/false); |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6610 | } else { |
| 6611 | // Invalid |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6612 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false); |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 6613 | } |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6614 | if (LCanPointeeTy != RCanPointeeTy) { |
| 6615 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6616 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6617 | else |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6618 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6619 | } |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6620 | return ResultTy; |
Steve Naroff | cdee44c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 6621 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6622 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6623 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6624 | // Comparison of nullptr_t with itself. |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6625 | if (LHSType->isNullPtrType() && RHSType->isNullPtrType()) |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6626 | return ResultTy; |
| 6627 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6628 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6629 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6630 | if (RHSIsNull && |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6631 | ((LHSType->isAnyPointerType() || LHSType->isNullPtrType()) || |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6632 | (!IsRelational && |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6633 | (LHSType->isMemberPointerType() || LHSType->isBlockPointerType())))) { |
| 6634 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 6635 | LHSType->isMemberPointerType() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6636 | ? CK_NullToMemberPointer |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6637 | : CK_NullToPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6638 | return ResultTy; |
| 6639 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6640 | if (LHSIsNull && |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6641 | ((RHSType->isAnyPointerType() || RHSType->isNullPtrType()) || |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6642 | (!IsRelational && |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6643 | (RHSType->isMemberPointerType() || RHSType->isBlockPointerType())))) { |
| 6644 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 6645 | RHSType->isMemberPointerType() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6646 | ? CK_NullToMemberPointer |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6647 | : CK_NullToPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6648 | return ResultTy; |
| 6649 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6650 | |
| 6651 | // Comparison of member pointers. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6652 | if (!IsRelational && |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6653 | LHSType->isMemberPointerType() && RHSType->isMemberPointerType()) { |
| 6654 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6655 | return QualType(); |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6656 | else |
| 6657 | return ResultTy; |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6658 | } |
Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6659 | |
| 6660 | // Handle scoped enumeration types specifically, since they don't promote |
| 6661 | // to integers. |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6662 | if (LHS.get()->getType()->isEnumeralType() && |
| 6663 | Context.hasSameUnqualifiedType(LHS.get()->getType(), |
| 6664 | RHS.get()->getType())) |
Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6665 | return ResultTy; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6666 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6667 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6668 | // Handle block pointer types. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6669 | if (!IsRelational && LHSType->isBlockPointerType() && |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6670 | RHSType->isBlockPointerType()) { |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6671 | QualType lpointee = LHSType->castAs<BlockPointerType>()->getPointeeType(); |
| 6672 | QualType rpointee = RHSType->castAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6673 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6674 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | a6638ca | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 6675 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6676 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6677 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6678 | << RHS.get()->getSourceRange(); |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6679 | } |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6680 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6681 | return ResultTy; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6682 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6683 | |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6684 | // Allow block pointers to be compared with null pointer constants. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6685 | if (!IsRelational |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6686 | && ((LHSType->isBlockPointerType() && RHSType->isPointerType()) |
| 6687 | || (LHSType->isPointerType() && RHSType->isBlockPointerType()))) { |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6688 | if (!LHSIsNull && !RHSIsNull) { |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6689 | if (!((RHSType->isPointerType() && RHSType->castAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6690 | ->getPointeeType()->isVoidType()) |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6691 | || (LHSType->isPointerType() && LHSType->castAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6692 | ->getPointeeType()->isVoidType()))) |
| 6693 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6694 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6695 | << RHS.get()->getSourceRange(); |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6696 | } |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6697 | if (LHSIsNull && !RHSIsNull) |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6698 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 6699 | RHSType->isPointerType() ? CK_BitCast |
| 6700 | : CK_AnyPointerToBlockPointerCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6701 | else |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6702 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 6703 | LHSType->isPointerType() ? CK_BitCast |
| 6704 | : CK_AnyPointerToBlockPointerCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6705 | return ResultTy; |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6706 | } |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6707 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6708 | if (LHSType->isObjCObjectPointerType() || |
| 6709 | RHSType->isObjCObjectPointerType()) { |
| 6710 | const PointerType *LPT = LHSType->getAs<PointerType>(); |
| 6711 | const PointerType *RPT = RHSType->getAs<PointerType>(); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6712 | if (LPT || RPT) { |
| 6713 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 6714 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6715 | |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 6716 | if (!LPtrToVoid && !RPtrToVoid && |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6717 | !Context.typesAreCompatible(LHSType, RHSType)) { |
| 6718 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6719 | /*isError*/false); |
Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 6720 | } |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6721 | if (LHSIsNull && !RHSIsNull) |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6722 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 6723 | RPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6724 | else |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6725 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 6726 | LPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6727 | return ResultTy; |
Steve Naroff | ea54d9e | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 6728 | } |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6729 | if (LHSType->isObjCObjectPointerType() && |
| 6730 | RHSType->isObjCObjectPointerType()) { |
| 6731 | if (!Context.areComparableObjCPointerTypes(LHSType, RHSType)) |
| 6732 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | dd82a5c | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6733 | /*isError*/false); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6734 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6735 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6736 | else |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6737 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6738 | return ResultTy; |
Steve Naroff | b788d9b | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 6739 | } |
Fariborz Jahanian | 134cbef | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 6740 | } |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6741 | if ((LHSType->isAnyPointerType() && RHSType->isIntegerType()) || |
| 6742 | (LHSType->isIntegerType() && RHSType->isAnyPointerType())) { |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6743 | unsigned DiagID = 0; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6744 | bool isError = false; |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6745 | if ((LHSIsNull && LHSType->isIntegerType()) || |
| 6746 | (RHSIsNull && RHSType->isIntegerType())) { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6747 | if (IsRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6748 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6749 | } else if (IsRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6750 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6751 | else if (getLangOptions().CPlusPlus) { |
| 6752 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 6753 | isError = true; |
| 6754 | } else |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6755 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6756 | |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6757 | if (DiagID) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6758 | Diag(Loc, DiagID) |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6759 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6760 | << RHS.get()->getSourceRange(); |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6761 | if (isError) |
| 6762 | return QualType(); |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6763 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6764 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6765 | if (LHSType->isIntegerType()) |
| 6766 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6767 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6768 | else |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6769 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6770 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6771 | return ResultTy; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 6772 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6773 | |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6774 | // Handle block pointers. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6775 | if (!IsRelational && RHSIsNull |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6776 | && LHSType->isBlockPointerType() && RHSType->isIntegerType()) { |
| 6777 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6778 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6779 | } |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6780 | if (!IsRelational && LHSIsNull |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6781 | && LHSType->isIntegerType() && RHSType->isBlockPointerType()) { |
| 6782 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_NullToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6783 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6784 | } |
Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6785 | |
Richard Trieu | b80728f | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6786 | return InvalidOperands(Loc, LHS, RHS); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6787 | } |
| 6788 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6789 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6790 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6791 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 6792 | /// types. |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6793 | QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6794 | SourceLocation Loc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6795 | bool IsRelational) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6796 | // Check to make sure we're operating on vectors of the same type and width, |
| 6797 | // Allowing one side to be a scalar of element type. |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6798 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, /*isCompAssign*/false); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6799 | if (vType.isNull()) |
| 6800 | return vType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6801 | |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6802 | QualType LHSType = LHS.get()->getType(); |
| 6803 | QualType RHSType = RHS.get()->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6804 | |
Anton Yartsev | 530deb9 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6805 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 6806 | // bool for C++, int for C |
Anton Yartsev | 93900c7 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 6807 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 530deb9 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6808 | return Context.getLogicalOperationType(); |
| 6809 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6810 | // For non-floating point types, check for self-comparisons of the form |
| 6811 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6812 | // often indicate logic errors in the program. |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6813 | if (!LHSType->hasFloatingRepresentation()) { |
| 6814 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHS.get()->IgnoreParens())) |
| 6815 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHS.get()->IgnoreParens())) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6816 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6817 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6818 | PDiag(diag::warn_comparison_always) |
| 6819 | << 0 // self- |
| 6820 | << 2 // "a constant" |
| 6821 | ); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6822 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6823 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6824 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6825 | if (!IsRelational && LHSType->hasFloatingRepresentation()) { |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6826 | assert (RHSType->hasFloatingRepresentation()); |
| 6827 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6828 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6829 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6830 | // Return the type for the comparison, which is the same as vector type for |
| 6831 | // integer vectors, or an integer type of identical size and number of |
| 6832 | // elements for floating point vectors. |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6833 | if (LHSType->hasIntegerRepresentation()) |
| 6834 | return LHSType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6835 | |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6836 | const VectorType *VTy = LHSType->getAs<VectorType>(); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6837 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6838 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6839 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | 5d68896 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 6840 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6841 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 6842 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6843 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6844 | "Unhandled vector element size in vector compare"); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6845 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 6846 | } |
| 6847 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6848 | inline QualType Sema::CheckBitwiseOperands( |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6849 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | f8916e1 | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6850 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6851 | |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6852 | if (LHS.get()->getType()->isVectorType() || |
| 6853 | RHS.get()->getType()->isVectorType()) { |
| 6854 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 6855 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6856 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6857 | |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6858 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6859 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6860 | |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6861 | ExprResult LHSResult = Owned(LHS), RHSResult = Owned(RHS); |
| 6862 | QualType compType = UsualArithmeticConversions(LHSResult, RHSResult, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6863 | IsCompAssign); |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6864 | if (LHSResult.isInvalid() || RHSResult.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6865 | return QualType(); |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6866 | LHS = LHSResult.take(); |
| 6867 | RHS = RHSResult.take(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6868 | |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6869 | if (LHS.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 6870 | RHS.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6871 | return compType; |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6872 | return InvalidOperands(Loc, LHS, RHS); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6873 | } |
| 6874 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6875 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6876 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6877 | |
| 6878 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 6879 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 6880 | // is a constant. |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6881 | if (LHS.get()->getType()->isIntegerType() && |
| 6882 | !LHS.get()->getType()->isBooleanType() && |
| 6883 | RHS.get()->getType()->isIntegerType() && !RHS.get()->isValueDependent() && |
Richard Trieu | cfe3926 | 2011-07-15 00:00:51 +0000 | [diff] [blame] | 6884 | // Don't warn in macros or template instantiations. |
| 6885 | !Loc.isMacroID() && ActiveTemplateInstantiations.empty()) { |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6886 | // If the RHS can be constant folded, and if it constant folds to something |
| 6887 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 6888 | // happened to fold to true/false) then warn. |
Chandler Carruth | e54ff6c | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6889 | // Parens on the RHS are ignored. |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6890 | Expr::EvalResult Result; |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6891 | if (RHS.get()->Evaluate(Result, Context) && !Result.HasSideEffects) |
| 6892 | if ((getLangOptions().Bool && !RHS.get()->getType()->isBooleanType()) || |
Chandler Carruth | e54ff6c | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6893 | (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) { |
| 6894 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6895 | << RHS.get()->getSourceRange() |
Matt Beaumont-Gay | 0a0ba9d | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6896 | << (Opc == BO_LAnd ? "&&" : "||"); |
| 6897 | // Suggest replacing the logical operator with the bitwise version |
| 6898 | Diag(Loc, diag::note_logical_instead_of_bitwise_change_operator) |
| 6899 | << (Opc == BO_LAnd ? "&" : "|") |
| 6900 | << FixItHint::CreateReplacement(SourceRange( |
| 6901 | Loc, Lexer::getLocForEndOfToken(Loc, 0, getSourceManager(), |
| 6902 | getLangOptions())), |
| 6903 | Opc == BO_LAnd ? "&" : "|"); |
| 6904 | if (Opc == BO_LAnd) |
| 6905 | // Suggest replacing "Foo() && kNonZero" with "Foo()" |
| 6906 | Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant) |
| 6907 | << FixItHint::CreateRemoval( |
| 6908 | SourceRange( |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6909 | Lexer::getLocForEndOfToken(LHS.get()->getLocEnd(), |
Matt Beaumont-Gay | 0a0ba9d | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6910 | 0, getSourceManager(), |
| 6911 | getLangOptions()), |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6912 | RHS.get()->getLocEnd())); |
Matt Beaumont-Gay | 0a0ba9d | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6913 | } |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6914 | } |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6915 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6916 | if (!Context.getLangOptions().CPlusPlus) { |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6917 | LHS = UsualUnaryConversions(LHS.take()); |
| 6918 | if (LHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6919 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6920 | |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6921 | RHS = UsualUnaryConversions(RHS.take()); |
| 6922 | if (RHS.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6923 | return QualType(); |
| 6924 | |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6925 | if (!LHS.get()->getType()->isScalarType() || |
| 6926 | !RHS.get()->getType()->isScalarType()) |
| 6927 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6928 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6929 | return Context.IntTy; |
Anders Carlsson | 35a99d9 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 6930 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6931 | |
John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6932 | // The following is safe because we only use this method for |
| 6933 | // non-overloadable operands. |
| 6934 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6935 | // C++ [expr.log.and]p1 |
| 6936 | // C++ [expr.log.or]p1 |
John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6937 | // The operands are both contextually converted to type bool. |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6938 | ExprResult LHSRes = PerformContextuallyConvertToBool(LHS.get()); |
| 6939 | if (LHSRes.isInvalid()) |
| 6940 | return InvalidOperands(Loc, LHS, RHS); |
| 6941 | LHS = move(LHSRes); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6942 | |
Richard Trieu | bcce2f7 | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6943 | ExprResult RHSRes = PerformContextuallyConvertToBool(RHS.get()); |
| 6944 | if (RHSRes.isInvalid()) |
| 6945 | return InvalidOperands(Loc, LHS, RHS); |
| 6946 | RHS = move(RHSRes); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6947 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6948 | // C++ [expr.log.and]p2 |
| 6949 | // C++ [expr.log.or]p2 |
| 6950 | // The result is a bool. |
| 6951 | return Context.BoolTy; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 6952 | } |
| 6953 | |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6954 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 6955 | /// is a read-only property; return true if so. A readonly property expression |
| 6956 | /// depends on various declarations and thus must be treated specially. |
| 6957 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6958 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6959 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6960 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6961 | if (PropExpr->isImplicitProperty()) return false; |
| 6962 | |
| 6963 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6964 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 6965 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 6966 | PropExpr->getBase()->getType(); |
| 6967 | |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6968 | if (const ObjCObjectPointerType *OPT = |
| 6969 | BaseType->getAsObjCInterfacePointerType()) |
| 6970 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 6971 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 6972 | return true; |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6973 | } |
| 6974 | return false; |
| 6975 | } |
| 6976 | |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6977 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 6978 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6979 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 6980 | if (PropExpr->isImplicitProperty()) return false; |
| 6981 | |
| 6982 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6983 | QualType T = PDecl->getType(); |
| 6984 | if (T->isReferenceType()) |
Fariborz Jahanian | 20688cc | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 6985 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6986 | CanQualType CT = S.Context.getCanonicalType(T); |
| 6987 | return CT.isConstQualified(); |
| 6988 | } |
| 6989 | return false; |
| 6990 | } |
| 6991 | |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6992 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 6993 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 6994 | return false; |
| 6995 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 6996 | NamedDecl *Member = ME->getMemberDecl(); |
| 6997 | if (isa<FieldDecl>(Member)) { |
| 6998 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 6999 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 7000 | return false; |
| 7001 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 7002 | } |
| 7003 | return false; |
| 7004 | } |
| 7005 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7006 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 7007 | /// emit an error and return true. If so, return false. |
| 7008 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7009 | SourceLocation OrigLoc = Loc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7010 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7011 | &Loc); |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7012 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 7013 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 7014 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 7015 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7016 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 7017 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7018 | if (IsLV == Expr::MLV_Valid) |
| 7019 | return false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7020 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7021 | unsigned Diag = 0; |
| 7022 | bool NeedType = false; |
| 7023 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7024 | case Expr::MLV_ConstQualified: |
| 7025 | Diag = diag::err_typecheck_assign_const; |
| 7026 | |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7027 | // In ARC, use some specialized diagnostics for occasions where we |
| 7028 | // infer 'const'. These are always pseudo-strong variables. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7029 | if (S.getLangOptions().ObjCAutoRefCount) { |
| 7030 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 7031 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 7032 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 7033 | |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7034 | // Use the normal diagnostic if it's pseudo-__strong but the |
| 7035 | // user actually wrote 'const'. |
| 7036 | if (var->isARCPseudoStrong() && |
| 7037 | (!var->getTypeSourceInfo() || |
| 7038 | !var->getTypeSourceInfo()->getType().isConstQualified())) { |
| 7039 | // There are two pseudo-strong cases: |
| 7040 | // - self |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7041 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 7042 | if (method && var == method->getSelfDecl()) |
| 7043 | Diag = diag::err_typecheck_arr_assign_self; |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7044 | |
| 7045 | // - fast enumeration variables |
| 7046 | else |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7047 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7048 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7049 | SourceRange Assign; |
| 7050 | if (Loc != OrigLoc) |
| 7051 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 7052 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 7053 | // We need to preserve the AST regardless, so migration tool |
| 7054 | // can do its job. |
| 7055 | return false; |
| 7056 | } |
| 7057 | } |
| 7058 | } |
| 7059 | |
| 7060 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7061 | case Expr::MLV_ArrayType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7062 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 7063 | NeedType = true; |
| 7064 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7065 | case Expr::MLV_NotObjectType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7066 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 7067 | NeedType = true; |
| 7068 | break; |
Chris Lattner | 9b3bbe9 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 7069 | case Expr::MLV_LValueCast: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7070 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 7071 | break; |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7072 | case Expr::MLV_Valid: |
| 7073 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7074 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7075 | case Expr::MLV_MemberFunction: |
| 7076 | case Expr::MLV_ClassTemporary: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7077 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 7078 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7079 | case Expr::MLV_IncompleteType: |
| 7080 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | ed0cfbd | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 7081 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7082 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 7083 | << E->getSourceRange()); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7084 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7085 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 7086 | break; |
Steve Naroff | ba756cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 7087 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7088 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 7089 | break; |
Fariborz Jahanian | 8a1810f | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 7090 | case Expr::MLV_ReadonlyProperty: |
| 7091 | Diag = diag::error_readonly_property_assignment; |
| 7092 | break; |
Fariborz Jahanian | 5118c41 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 7093 | case Expr::MLV_NoSetterProperty: |
| 7094 | Diag = diag::error_nosetter_property_assignment; |
| 7095 | break; |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7096 | case Expr::MLV_InvalidMessageExpression: |
| 7097 | Diag = diag::error_readonly_message_assignment; |
| 7098 | break; |
Fariborz Jahanian | e8d2890 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 7099 | case Expr::MLV_SubObjCPropertySetting: |
| 7100 | Diag = diag::error_no_subobject_property_setting; |
| 7101 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7102 | } |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7103 | |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7104 | SourceRange Assign; |
| 7105 | if (Loc != OrigLoc) |
| 7106 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7107 | if (NeedType) |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7108 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7109 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7110 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7111 | return true; |
| 7112 | } |
| 7113 | |
| 7114 | |
| 7115 | |
| 7116 | // C99 6.5.16.1 |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7117 | QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS, |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7118 | SourceLocation Loc, |
| 7119 | QualType CompoundType) { |
| 7120 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7121 | if (CheckForModifiableLvalue(LHSExpr, Loc, *this)) |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7122 | return QualType(); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7123 | |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7124 | QualType LHSType = LHSExpr->getType(); |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7125 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : |
| 7126 | CompoundType; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7127 | AssignConvertType ConvTy; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7128 | if (CompoundType.isNull()) { |
Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7129 | QualType LHSTy(LHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7130 | // Simple assignment "x = y". |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7131 | if (LHSExpr->getObjectKind() == OK_ObjCProperty) { |
| 7132 | ExprResult LHSResult = Owned(LHSExpr); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7133 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 7134 | if (LHSResult.isInvalid()) |
| 7135 | return QualType(); |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7136 | LHSExpr = LHSResult.take(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7137 | } |
Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7138 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7139 | if (RHS.isInvalid()) |
| 7140 | return QualType(); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7141 | // Special case of NSObject attributes on c-style pointer types. |
| 7142 | if (ConvTy == IncompatiblePointer && |
| 7143 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7144 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7145 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7146 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7147 | ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7148 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7149 | if (ConvTy == Compatible && |
| 7150 | getLangOptions().ObjCNonFragileABI && |
| 7151 | LHSType->isObjCObjectType()) |
| 7152 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 7153 | << LHSType; |
| 7154 | |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7155 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 7156 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 7157 | // instead of "x += 4". |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7158 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7159 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 7160 | RHSCheck = ICE->getSubExpr(); |
| 7161 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7162 | if ((UO->getOpcode() == UO_Plus || |
| 7163 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7164 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7165 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7166 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 7167 | // And there is a space or other character before the subexpr of the |
| 7168 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | ed9f14c | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7169 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 7170 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7171 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7172 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7173 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7174 | } |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7175 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7176 | |
| 7177 | if (ConvTy == Compatible) { |
| 7178 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7179 | checkRetainCycles(LHSExpr, RHS.get()); |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 7180 | else if (getLangOptions().ObjCAutoRefCount) |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7181 | checkUnsafeExprAssigns(Loc, LHSExpr, RHS.get()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7182 | } |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7183 | } else { |
| 7184 | // Compound assignment "x += y" |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7185 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7186 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7187 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7188 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7189 | RHS.get(), AA_Assigning)) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7190 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7191 | |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7192 | CheckForNullPointerDereference(*this, LHSExpr); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7193 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 7194 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7195 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7196 | // it is the unqualified version of the type of the left operand. |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 7197 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7198 | // is converted to the type of the assignment expression (above). |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7199 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 7200 | // operand. |
John McCall | 01cbf2d | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7201 | return (getLangOptions().CPlusPlus |
| 7202 | ? LHSType : LHSType.getUnqualifiedType()); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 7203 | } |
| 7204 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7205 | // C99 6.5.17 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7206 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7207 | SourceLocation Loc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7208 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 639ffb0 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 7209 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7210 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 7211 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7212 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7213 | return QualType(); |
| 7214 | |
John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7215 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7216 | // operands, but not unary promotions. |
| 7217 | // C++'s comma does not do any conversions at all (C++ [expr.comma]p1). |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7218 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7219 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7220 | // containing site to determine what should be done with the RHS. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7221 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 7222 | if (LHS.isInvalid()) |
| 7223 | return QualType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7224 | |
| 7225 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7226 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 7227 | if (RHS.isInvalid()) |
| 7228 | return QualType(); |
| 7229 | if (!RHS.get()->getType()->isVoidType()) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7230 | S.RequireCompleteType(Loc, RHS.get()->getType(), |
| 7231 | diag::err_incomplete_type); |
John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7232 | } |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7233 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7234 | return RHS.get()->getType(); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 7235 | } |
| 7236 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7237 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7238 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7239 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7240 | ExprValueKind &VK, |
| 7241 | SourceLocation OpLoc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7242 | bool IsInc, bool IsPrefix) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7243 | if (Op->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7244 | return S.Context.DependentTy; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7245 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7246 | QualType ResType = Op->getType(); |
| 7247 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 7248 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7249 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7250 | // Decrement of bool is not allowed. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7251 | if (!IsInc) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7252 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7253 | return QualType(); |
| 7254 | } |
| 7255 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7256 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7257 | } else if (ResType->isRealType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7258 | // OK! |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7259 | } else if (ResType->isAnyPointerType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7260 | // C99 6.5.2.4p2, 6.5.6p2 |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7261 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7262 | return QualType(); |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7263 | |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7264 | // Diagnose bad cases where we step over interface counts. |
Richard Trieu | b10c631 | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 7265 | else if (!checkArithmethicPointerOnNonFragileABI(S, OpLoc, Op)) |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7266 | return QualType(); |
Eli Friedman | 090addd | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7267 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7268 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7269 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7270 | << ResType << Op->getSourceRange(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7271 | } else if (ResType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7272 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7273 | if (PR.isInvalid()) return QualType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7274 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7275 | IsInc, IsPrefix); |
Anton Yartsev | 85129b8 | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7276 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 7277 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7278 | } else { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7279 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7280 | << ResType << int(IsInc) << Op->getSourceRange(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7281 | return QualType(); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 7282 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7283 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | 9e1e551 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 7284 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7285 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7286 | return QualType(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7287 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7288 | // (in C or with postfix), the increment is the unqualified type of the |
| 7289 | // operand. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7290 | if (IsPrefix && S.getLangOptions().CPlusPlus) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7291 | VK = VK_LValue; |
| 7292 | return ResType; |
| 7293 | } else { |
| 7294 | VK = VK_RValue; |
| 7295 | return ResType.getUnqualifiedType(); |
| 7296 | } |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7297 | } |
| 7298 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7299 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7300 | assert(E->getValueKind() == VK_LValue && |
| 7301 | E->getObjectKind() == OK_ObjCProperty); |
| 7302 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 7303 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7304 | QualType T = E->getType(); |
| 7305 | QualType ReceiverType; |
| 7306 | if (PRE->isObjectReceiver()) |
| 7307 | ReceiverType = PRE->getBase()->getType(); |
| 7308 | else if (PRE->isSuperReceiver()) |
| 7309 | ReceiverType = PRE->getSuperReceiverType(); |
| 7310 | else |
| 7311 | ReceiverType = Context.getObjCInterfaceType(PRE->getClassReceiver()); |
| 7312 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7313 | ExprValueKind VK = VK_RValue; |
| 7314 | if (PRE->isImplicitProperty()) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7315 | if (ObjCMethodDecl *GetterMethod = |
Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7316 | PRE->getImplicitPropertyGetter()) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7317 | T = getMessageSendResultType(ReceiverType, GetterMethod, |
| 7318 | PRE->isClassReceiver(), |
| 7319 | PRE->isSuperReceiver()); |
| 7320 | VK = Expr::getValueKindForType(GetterMethod->getResultType()); |
Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7321 | } |
| 7322 | else { |
| 7323 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 7324 | << PRE->getBase()->getType(); |
| 7325 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7326 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7327 | |
| 7328 | E = ImplicitCastExpr::Create(Context, T, CK_GetObjCProperty, |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7329 | E, 0, VK); |
John McCall | 4f26cd8 | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 7330 | |
| 7331 | ExprResult Result = MaybeBindToTemporary(E); |
| 7332 | if (!Result.isInvalid()) |
| 7333 | E = Result.take(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7334 | |
| 7335 | return Owned(E); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7336 | } |
| 7337 | |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7338 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, |
| 7339 | QualType &LHSTy) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7340 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 7341 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 7342 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7343 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7344 | bool Consumed = false; |
| 7345 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7346 | if (PropRef->isImplicitProperty()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7347 | // If using property-dot syntax notation for assignment, and there is a |
| 7348 | // setter, RHS expression is being passed to the setter argument. So, |
| 7349 | // type conversion (and comparison) is RHS to setter's argument type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7350 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7351 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 7352 | LHSTy = (*P)->getType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7353 | Consumed = (getLangOptions().ObjCAutoRefCount && |
| 7354 | (*P)->hasAttr<NSConsumedAttr>()); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7355 | |
| 7356 | // Otherwise, if the getter returns an l-value, just call that. |
| 7357 | } else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7358 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7359 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 7360 | if (VK == VK_LValue) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7361 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 7362 | CK_GetObjCProperty, LHS.take(), 0, VK); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7363 | return; |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7364 | } |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7365 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7366 | } else if (getLangOptions().ObjCAutoRefCount) { |
| 7367 | const ObjCMethodDecl *setter |
| 7368 | = PropRef->getExplicitProperty()->getSetterMethodDecl(); |
| 7369 | if (setter) { |
| 7370 | ObjCMethodDecl::param_iterator P = setter->param_begin(); |
| 7371 | LHSTy = (*P)->getType(); |
| 7372 | Consumed = (*P)->hasAttr<NSConsumedAttr>(); |
| 7373 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7374 | } |
| 7375 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7376 | if ((getLangOptions().CPlusPlus && LHSTy->isRecordType()) || |
| 7377 | getLangOptions().ObjCAutoRefCount) { |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7378 | InitializedEntity Entity = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7379 | InitializedEntity::InitializeParameter(Context, LHSTy, Consumed); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7380 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7381 | if (!ArgE.isInvalid()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7382 | RHS = ArgE; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7383 | if (getLangOptions().ObjCAutoRefCount && !PropRef->isSuperReceiver()) |
| 7384 | checkRetainCycles(const_cast<Expr*>(PropRef->getBase()), RHS.get()); |
| 7385 | } |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7386 | } |
| 7387 | } |
| 7388 | |
| 7389 | |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7390 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7391 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7392 | /// where the declaration is needed for type checking. We only need to |
| 7393 | /// handle cases when the expression references a function designator |
| 7394 | /// or is an lvalue. Here are some examples: |
| 7395 | /// - &(x) => x |
| 7396 | /// - &*****f => f for f a function designator. |
| 7397 | /// - &s.xx => s |
| 7398 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7399 | /// - *(x + 1) -> x, if x is an array |
| 7400 | /// - &"123"[2] -> 0 |
| 7401 | /// - & __real__ x -> x |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7402 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7403 | switch (E->getStmtClass()) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7404 | case Stmt::DeclRefExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7405 | return cast<DeclRefExpr>(E)->getDecl(); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7406 | case Stmt::MemberExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7407 | // If this is an arrow operator, the address is an offset from |
| 7408 | // the base's value, so the object the base refers to is |
| 7409 | // irrelevant. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7410 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7411 | return 0; |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7412 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7413 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7414 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7415 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7416 | // promotion of register arrays earlier. |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7417 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7418 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7419 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7420 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7421 | } |
| 7422 | return 0; |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7423 | } |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7424 | case Stmt::UnaryOperatorClass: { |
| 7425 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7426 | |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7427 | switch(UO->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7428 | case UO_Real: |
| 7429 | case UO_Imag: |
| 7430 | case UO_Extension: |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7431 | return getPrimaryDecl(UO->getSubExpr()); |
| 7432 | default: |
| 7433 | return 0; |
| 7434 | } |
| 7435 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7436 | case Stmt::ParenExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7437 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7438 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7439 | // If the result of an implicit cast is an l-value, we care about |
| 7440 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7441 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7442 | default: |
| 7443 | return 0; |
| 7444 | } |
| 7445 | } |
| 7446 | |
Richard Trieu | 5f376f6 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7447 | namespace { |
| 7448 | enum { |
| 7449 | AO_Bit_Field = 0, |
| 7450 | AO_Vector_Element = 1, |
| 7451 | AO_Property_Expansion = 2, |
| 7452 | AO_Register_Variable = 3, |
| 7453 | AO_No_Error = 4 |
| 7454 | }; |
| 7455 | } |
Richard Trieu | 3fd7bb8 | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7456 | /// \brief Diagnose invalid operand for address of operations. |
| 7457 | /// |
| 7458 | /// \param Type The type of operand which cannot have its address taken. |
Richard Trieu | 3fd7bb8 | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7459 | static void diagnoseAddressOfInvalidType(Sema &S, SourceLocation Loc, |
| 7460 | Expr *E, unsigned Type) { |
| 7461 | S.Diag(Loc, diag::err_typecheck_address_of) << Type << E->getSourceRange(); |
| 7462 | } |
| 7463 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7464 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7465 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7466 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7467 | /// Note: The usual conversions are *not* applied to the operand of the & |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 7468 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7469 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7470 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7471 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 7472 | SourceLocation OpLoc) { |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7473 | if (OrigOp->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7474 | return S.Context.DependentTy; |
| 7475 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 7476 | return S.Context.OverloadTy; |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7477 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 7478 | return S.Context.UnknownAnyTy; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7479 | if (OrigOp->getType() == S.Context.BoundMemberTy) { |
| 7480 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 7481 | << OrigOp->getSourceRange(); |
| 7482 | return QualType(); |
| 7483 | } |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7484 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7485 | assert(!OrigOp->getType()->isPlaceholderType()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7486 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7487 | // Make sure to ignore parentheses in subsequent checks |
| 7488 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 19b8c4f | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 7489 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7490 | if (S.getLangOptions().C99) { |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7491 | // Implement C99-only parts of addressof rules. |
| 7492 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7493 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7494 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 7495 | // (assuming the deref expression is valid). |
| 7496 | return uOp->getSubExpr()->getType(); |
| 7497 | } |
| 7498 | // Technically, there should be a check for array subscript |
| 7499 | // expressions here, but the result of one is always an lvalue anyway. |
| 7500 | } |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7501 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7502 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Richard Trieu | 5f376f6 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7503 | unsigned AddressOfError = AO_No_Error; |
Nuno Lopes | 17f345f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 7504 | |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7505 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7506 | bool sfinae = S.isSFINAEContext(); |
| 7507 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 7508 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7509 | << op->getType() << op->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7510 | if (sfinae) |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7511 | return QualType(); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7512 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7513 | return S.Context.getPointerType(op->getType()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7514 | } else if (lval == Expr::LV_MemberFunction) { |
| 7515 | // If it's an instance method, make a member pointer. |
| 7516 | // The expression must have exactly the form &A::foo. |
| 7517 | |
| 7518 | // If the underlying expression isn't a decl ref, give up. |
| 7519 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7520 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7521 | << OrigOp->getSourceRange(); |
| 7522 | return QualType(); |
| 7523 | } |
| 7524 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 7525 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 7526 | |
| 7527 | // The id-expression was parenthesized. |
| 7528 | if (OrigOp != DRE) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7529 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7530 | << OrigOp->getSourceRange(); |
| 7531 | |
| 7532 | // The method was named without a qualifier. |
| 7533 | } else if (!DRE->getQualifier()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7534 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7535 | << op->getSourceRange(); |
| 7536 | } |
| 7537 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7538 | return S.Context.getMemberPointerType(op->getType(), |
| 7539 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7540 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7541 | // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7542 | // The operand must be either an l-value or a function designator |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7543 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7544 | // FIXME: emit more specific diag... |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7545 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 7546 | << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7547 | return QualType(); |
| 7548 | } |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7549 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7550 | // The operand cannot be a bit-field |
Richard Trieu | 5f376f6 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7551 | AddressOfError = AO_Bit_Field; |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7552 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7553 | // The operand cannot be an element of a vector |
Richard Trieu | 5f376f6 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7554 | AddressOfError = AO_Vector_Element; |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7555 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7556 | // cannot take address of a property expression. |
Richard Trieu | 5f376f6 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7557 | AddressOfError = AO_Property_Expansion; |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7558 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7559 | // We have an lvalue with a decl. Make sure the decl is not declared |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7560 | // with the register storage-class specifier. |
| 7561 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | e0fd5a9 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 7562 | // in C++ it is not error to take address of a register |
| 7563 | // variable (c++03 7.1.1P3) |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7564 | if (vd->getStorageClass() == SC_Register && |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7565 | !S.getLangOptions().CPlusPlus) { |
Richard Trieu | 5f376f6 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7566 | AddressOfError = AO_Register_Variable; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7567 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7568 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7569 | return S.Context.OverloadTy; |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7570 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 9aa8b55 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 7571 | // Okay: we can take the address of a field. |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7572 | // Could be a pointer to member, though, if there is an explicit |
| 7573 | // scope qualifier for the class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 7574 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7575 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7576 | if (Ctx && Ctx->isRecord()) { |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7577 | if (dcl->getType()->isReferenceType()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7578 | S.Diag(OpLoc, |
| 7579 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7580 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7581 | return QualType(); |
| 7582 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7583 | |
Argyrios Kyrtzidis | 8322b42 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 7584 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 7585 | Ctx = Ctx->getParent(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7586 | return S.Context.getMemberPointerType(op->getType(), |
| 7587 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7588 | } |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7589 | } |
Eli Friedman | 755c0c9 | 2011-08-26 20:28:17 +0000 | [diff] [blame] | 7590 | } else if (!isa<FunctionDecl>(dcl) && !isa<NonTypeTemplateParmDecl>(dcl)) |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 7591 | assert(0 && "Unknown/unexpected decl type"); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7592 | } |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7593 | |
Richard Trieu | 5f376f6 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7594 | if (AddressOfError != AO_No_Error) { |
| 7595 | diagnoseAddressOfInvalidType(S, OpLoc, op, AddressOfError); |
| 7596 | return QualType(); |
| 7597 | } |
| 7598 | |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7599 | if (lval == Expr::LV_IncompleteVoidType) { |
| 7600 | // Taking the address of a void variable is technically illegal, but we |
| 7601 | // allow it in cases which are otherwise valid. |
| 7602 | // Example: "extern void x; void* y = &x;". |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7603 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7604 | } |
| 7605 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7606 | // If the operand has type "type", the result has type "pointer to type". |
Douglas Gregor | 0bdcb8a | 2010-07-29 16:05:45 +0000 | [diff] [blame] | 7607 | if (op->getType()->isObjCObjectType()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7608 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 7609 | return S.Context.getPointerType(op->getType()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7610 | } |
| 7611 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7612 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7613 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 7614 | SourceLocation OpLoc) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7615 | if (Op->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7616 | return S.Context.DependentTy; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7617 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7618 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 7619 | if (ConvResult.isInvalid()) |
| 7620 | return QualType(); |
| 7621 | Op = ConvResult.take(); |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7622 | QualType OpTy = Op->getType(); |
| 7623 | QualType Result; |
Argyrios Kyrtzidis | 69a2c92 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 7624 | |
| 7625 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 7626 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 7627 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 7628 | Op->getSourceRange()); |
| 7629 | } |
| 7630 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7631 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 7632 | // is an incomplete type or void. It would be possible to warn about |
| 7633 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 7634 | // warning is unlikely to catch any mistakes. |
| 7635 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 7636 | Result = PT->getPointeeType(); |
| 7637 | else if (const ObjCObjectPointerType *OPT = |
| 7638 | OpTy->getAs<ObjCObjectPointerType>()) |
| 7639 | Result = OPT->getPointeeType(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7640 | else { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7641 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7642 | if (PR.isInvalid()) return QualType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7643 | if (PR.take() != Op) |
| 7644 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7645 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7646 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7647 | if (Result.isNull()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7648 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7649 | << OpTy << Op->getSourceRange(); |
| 7650 | return QualType(); |
| 7651 | } |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7652 | |
| 7653 | // Dereferences are usually l-values... |
| 7654 | VK = VK_LValue; |
| 7655 | |
| 7656 | // ...except that certain expressions are never l-values in C. |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 7657 | if (!S.getLangOptions().CPlusPlus && Result.isCForbiddenLValueType()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7658 | VK = VK_RValue; |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7659 | |
| 7660 | return Result; |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 7661 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7662 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7663 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7664 | tok::TokenKind Kind) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7665 | BinaryOperatorKind Opc; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7666 | switch (Kind) { |
| 7667 | default: assert(0 && "Unknown binop!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7668 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 7669 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 7670 | case tok::star: Opc = BO_Mul; break; |
| 7671 | case tok::slash: Opc = BO_Div; break; |
| 7672 | case tok::percent: Opc = BO_Rem; break; |
| 7673 | case tok::plus: Opc = BO_Add; break; |
| 7674 | case tok::minus: Opc = BO_Sub; break; |
| 7675 | case tok::lessless: Opc = BO_Shl; break; |
| 7676 | case tok::greatergreater: Opc = BO_Shr; break; |
| 7677 | case tok::lessequal: Opc = BO_LE; break; |
| 7678 | case tok::less: Opc = BO_LT; break; |
| 7679 | case tok::greaterequal: Opc = BO_GE; break; |
| 7680 | case tok::greater: Opc = BO_GT; break; |
| 7681 | case tok::exclaimequal: Opc = BO_NE; break; |
| 7682 | case tok::equalequal: Opc = BO_EQ; break; |
| 7683 | case tok::amp: Opc = BO_And; break; |
| 7684 | case tok::caret: Opc = BO_Xor; break; |
| 7685 | case tok::pipe: Opc = BO_Or; break; |
| 7686 | case tok::ampamp: Opc = BO_LAnd; break; |
| 7687 | case tok::pipepipe: Opc = BO_LOr; break; |
| 7688 | case tok::equal: Opc = BO_Assign; break; |
| 7689 | case tok::starequal: Opc = BO_MulAssign; break; |
| 7690 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 7691 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 7692 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 7693 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 7694 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 7695 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 7696 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 7697 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 7698 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 7699 | case tok::comma: Opc = BO_Comma; break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7700 | } |
| 7701 | return Opc; |
| 7702 | } |
| 7703 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7704 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7705 | tok::TokenKind Kind) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7706 | UnaryOperatorKind Opc; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7707 | switch (Kind) { |
| 7708 | default: assert(0 && "Unknown unary op!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7709 | case tok::plusplus: Opc = UO_PreInc; break; |
| 7710 | case tok::minusminus: Opc = UO_PreDec; break; |
| 7711 | case tok::amp: Opc = UO_AddrOf; break; |
| 7712 | case tok::star: Opc = UO_Deref; break; |
| 7713 | case tok::plus: Opc = UO_Plus; break; |
| 7714 | case tok::minus: Opc = UO_Minus; break; |
| 7715 | case tok::tilde: Opc = UO_Not; break; |
| 7716 | case tok::exclaim: Opc = UO_LNot; break; |
| 7717 | case tok::kw___real: Opc = UO_Real; break; |
| 7718 | case tok::kw___imag: Opc = UO_Imag; break; |
| 7719 | case tok::kw___extension__: Opc = UO_Extension; break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7720 | } |
| 7721 | return Opc; |
| 7722 | } |
| 7723 | |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7724 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 7725 | /// This warning is only emitted for builtin assignment operations. It is also |
| 7726 | /// suppressed in the event of macro expansions. |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7727 | static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7728 | SourceLocation OpLoc) { |
| 7729 | if (!S.ActiveTemplateInstantiations.empty()) |
| 7730 | return; |
| 7731 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 7732 | return; |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7733 | LHSExpr = LHSExpr->IgnoreParenImpCasts(); |
| 7734 | RHSExpr = RHSExpr->IgnoreParenImpCasts(); |
| 7735 | const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); |
| 7736 | const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); |
| 7737 | if (!LHSDeclRef || !RHSDeclRef || |
| 7738 | LHSDeclRef->getLocation().isMacroID() || |
| 7739 | RHSDeclRef->getLocation().isMacroID()) |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7740 | return; |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7741 | const ValueDecl *LHSDecl = |
| 7742 | cast<ValueDecl>(LHSDeclRef->getDecl()->getCanonicalDecl()); |
| 7743 | const ValueDecl *RHSDecl = |
| 7744 | cast<ValueDecl>(RHSDeclRef->getDecl()->getCanonicalDecl()); |
| 7745 | if (LHSDecl != RHSDecl) |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7746 | return; |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7747 | if (LHSDecl->getType().isVolatileQualified()) |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7748 | return; |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7749 | if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7750 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 7751 | return; |
| 7752 | |
| 7753 | S.Diag(OpLoc, diag::warn_self_assignment) |
Richard Trieu | da4f43a6 | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7754 | << LHSDeclRef->getType() |
| 7755 | << LHSExpr->getSourceRange() << RHSExpr->getSourceRange(); |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7756 | } |
| 7757 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7758 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 7759 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 7760 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7761 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7762 | BinaryOperatorKind Opc, |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7763 | Expr *LHSExpr, Expr *RHSExpr) { |
| 7764 | ExprResult LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7765 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7766 | // The following two variables are used for compound assignment operators |
| 7767 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 7768 | QualType CompResultTy; // Type of computation result |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7769 | ExprValueKind VK = VK_RValue; |
| 7770 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7771 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7772 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 7773 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 7774 | // But since an assignment can trigger target based overload, exclude it in |
| 7775 | // our blind search. i.e: |
| 7776 | // template<class T> void f(); template<class T, class U> void f(U); |
| 7777 | // f<int> == 0; // resolve f<int> blindly |
| 7778 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 7779 | if (Opc != BO_Assign) { |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7780 | ExprResult resolvedLHS = CheckPlaceholderExpr(LHS.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7781 | if (!resolvedLHS.isUsable()) return ExprError(); |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7782 | LHS = move(resolvedLHS); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7783 | |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7784 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHS.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7785 | if (!resolvedRHS.isUsable()) return ExprError(); |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7786 | RHS = move(resolvedRHS); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7787 | } |
| 7788 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7789 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7790 | case BO_Assign: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7791 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, QualType()); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7792 | if (getLangOptions().CPlusPlus && |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7793 | LHS.get()->getObjectKind() != OK_ObjCProperty) { |
| 7794 | VK = LHS.get()->getValueKind(); |
| 7795 | OK = LHS.get()->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7796 | } |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7797 | if (!ResultTy.isNull()) |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7798 | DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7799 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7800 | case BO_PtrMemD: |
| 7801 | case BO_PtrMemI: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7802 | ResultTy = CheckPointerToMemberOperands(LHS, RHS, VK, OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7803 | Opc == BO_PtrMemI); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7804 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7805 | case BO_Mul: |
| 7806 | case BO_Div: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7807 | ResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, false, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7808 | Opc == BO_Div); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7809 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7810 | case BO_Rem: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7811 | ResultTy = CheckRemainderOperands(LHS, RHS, OpLoc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7812 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7813 | case BO_Add: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7814 | ResultTy = CheckAdditionOperands(LHS, RHS, OpLoc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7815 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7816 | case BO_Sub: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7817 | ResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7818 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7819 | case BO_Shl: |
| 7820 | case BO_Shr: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7821 | ResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7822 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7823 | case BO_LE: |
| 7824 | case BO_LT: |
| 7825 | case BO_GE: |
| 7826 | case BO_GT: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7827 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, true); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7828 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7829 | case BO_EQ: |
| 7830 | case BO_NE: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7831 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, false); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7832 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7833 | case BO_And: |
| 7834 | case BO_Xor: |
| 7835 | case BO_Or: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7836 | ResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7837 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7838 | case BO_LAnd: |
| 7839 | case BO_LOr: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7840 | ResultTy = CheckLogicalOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7841 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7842 | case BO_MulAssign: |
| 7843 | case BO_DivAssign: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7844 | CompResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, true, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7845 | Opc == BO_DivAssign); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7846 | CompLHSTy = CompResultTy; |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7847 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7848 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7849 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7850 | case BO_RemAssign: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7851 | CompResultTy = CheckRemainderOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7852 | CompLHSTy = CompResultTy; |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7853 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7854 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7855 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7856 | case BO_AddAssign: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7857 | CompResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, &CompLHSTy); |
| 7858 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7859 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7860 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7861 | case BO_SubAssign: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7862 | CompResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc, &CompLHSTy); |
| 7863 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7864 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7865 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7866 | case BO_ShlAssign: |
| 7867 | case BO_ShrAssign: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7868 | CompResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc, true); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7869 | CompLHSTy = CompResultTy; |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7870 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7871 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7872 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7873 | case BO_AndAssign: |
| 7874 | case BO_XorAssign: |
| 7875 | case BO_OrAssign: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7876 | CompResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7877 | CompLHSTy = CompResultTy; |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7878 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7879 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7880 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7881 | case BO_Comma: |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7882 | ResultTy = CheckCommaOperands(*this, LHS, RHS, OpLoc); |
| 7883 | if (getLangOptions().CPlusPlus && !RHS.isInvalid()) { |
| 7884 | VK = RHS.get()->getValueKind(); |
| 7885 | OK = RHS.get()->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7886 | } |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7887 | break; |
| 7888 | } |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7889 | if (ResultTy.isNull() || LHS.isInvalid() || RHS.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7890 | return ExprError(); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7891 | |
| 7892 | // Check for array bounds violations for both sides of the BinaryOperator |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7893 | CheckArrayAccess(LHS.get()); |
| 7894 | CheckArrayAccess(RHS.get()); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7895 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7896 | if (CompResultTy.isNull()) |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7897 | return Owned(new (Context) BinaryOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7898 | ResultTy, VK, OK, OpLoc)); |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7899 | if (getLangOptions().CPlusPlus && LHS.get()->getObjectKind() != |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7900 | OK_ObjCProperty) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7901 | VK = VK_LValue; |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7902 | OK = LHS.get()->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7903 | } |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7904 | return Owned(new (Context) CompoundAssignOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7905 | ResultTy, VK, OK, CompLHSTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7906 | CompResultTy, OpLoc)); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7907 | } |
| 7908 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7909 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 7910 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 7911 | /// comparison operators have higher precedence. The most typical example of |
| 7912 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7913 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7914 | SourceLocation OpLoc, Expr *LHSExpr, |
| 7915 | Expr *RHSExpr) { |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7916 | typedef BinaryOperator BinOp; |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7917 | BinOp::Opcode LHSopc = static_cast<BinOp::Opcode>(-1), |
| 7918 | RHSopc = static_cast<BinOp::Opcode>(-1); |
| 7919 | if (BinOp *BO = dyn_cast<BinOp>(LHSExpr)) |
| 7920 | LHSopc = BO->getOpcode(); |
| 7921 | if (BinOp *BO = dyn_cast<BinOp>(RHSExpr)) |
| 7922 | RHSopc = BO->getOpcode(); |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7923 | |
| 7924 | // Subs are not binary operators. |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7925 | if (LHSopc == -1 && RHSopc == -1) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7926 | return; |
| 7927 | |
| 7928 | // Bitwise operations are sometimes used as eager logical ops. |
| 7929 | // Don't diagnose this. |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7930 | if ((BinOp::isComparisonOp(LHSopc) || BinOp::isBitwiseOp(LHSopc)) && |
| 7931 | (BinOp::isComparisonOp(RHSopc) || BinOp::isBitwiseOp(RHSopc))) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7932 | return; |
| 7933 | |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7934 | bool isLeftComp = BinOp::isComparisonOp(LHSopc); |
| 7935 | bool isRightComp = BinOp::isComparisonOp(RHSopc); |
Richard Trieu | 7308805 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7936 | if (!isLeftComp && !isRightComp) return; |
| 7937 | |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7938 | SourceRange DiagRange = isLeftComp ? SourceRange(LHSExpr->getLocStart(), |
| 7939 | OpLoc) |
| 7940 | : SourceRange(OpLoc, RHSExpr->getLocEnd()); |
| 7941 | std::string OpStr = isLeftComp ? BinOp::getOpcodeStr(LHSopc) |
| 7942 | : BinOp::getOpcodeStr(RHSopc); |
Richard Trieu | 7308805 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7943 | SourceRange ParensRange = isLeftComp ? |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7944 | SourceRange(cast<BinOp>(LHSExpr)->getRHS()->getLocStart(), |
| 7945 | RHSExpr->getLocEnd()) |
| 7946 | : SourceRange(LHSExpr->getLocStart(), |
| 7947 | cast<BinOp>(RHSExpr)->getLHS()->getLocStart()); |
Richard Trieu | 7308805 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7948 | |
| 7949 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 7950 | << DiagRange << BinOp::getOpcodeStr(Opc) << OpStr; |
| 7951 | SuggestParentheses(Self, OpLoc, |
| 7952 | Self.PDiag(diag::note_precedence_bitwise_silence) << OpStr, |
Richard Trieu | 4a287fb | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7953 | RHSExpr->getSourceRange()); |
Richard Trieu | 7308805 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7954 | SuggestParentheses(Self, OpLoc, |
| 7955 | Self.PDiag(diag::note_precedence_bitwise_first) << BinOp::getOpcodeStr(Opc), |
| 7956 | ParensRange); |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7957 | } |
| 7958 | |
Argyrios Kyrtzidis | 01bf777 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7959 | /// \brief It accepts a '&' expr that is inside a '|' one. |
| 7960 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression |
| 7961 | /// in parentheses. |
| 7962 | static void |
| 7963 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, |
| 7964 | BinaryOperator *Bop) { |
| 7965 | assert(Bop->getOpcode() == BO_And); |
| 7966 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) |
| 7967 | << Bop->getSourceRange() << OpLoc; |
| 7968 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
| 7969 | Self.PDiag(diag::note_bitwise_and_in_bitwise_or_silence), |
| 7970 | Bop->getSourceRange()); |
| 7971 | } |
| 7972 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7973 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 7974 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 7975 | /// in parentheses. |
| 7976 | static void |
| 7977 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | ad8b4d4 | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7978 | BinaryOperator *Bop) { |
| 7979 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7980 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 7981 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | ad8b4d4 | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7982 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7983 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7984 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7985 | } |
| 7986 | |
| 7987 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7988 | /// 'true'. |
| 7989 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 7990 | bool Res; |
| 7991 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 7992 | } |
| 7993 | |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7994 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7995 | /// 'false'. |
| 7996 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 7997 | bool Res; |
| 7998 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 7999 | } |
| 8000 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8001 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 8002 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8003 | Expr *LHSExpr, Expr *RHSExpr) { |
| 8004 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(LHSExpr)) { |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8005 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8006 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8007 | if (EvaluatesAsFalse(S, RHSExpr)) |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8008 | return; |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8009 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 8010 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 8011 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 8012 | } else if (Bop->getOpcode() == BO_LOr) { |
| 8013 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 8014 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 8015 | // "a || b && 1", but warn now. |
| 8016 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 8017 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 8018 | } |
| 8019 | } |
| 8020 | } |
| 8021 | } |
| 8022 | |
| 8023 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 8024 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8025 | Expr *LHSExpr, Expr *RHSExpr) { |
| 8026 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(RHSExpr)) { |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8027 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8028 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8029 | if (EvaluatesAsFalse(S, LHSExpr)) |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8030 | return; |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8031 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 8032 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 8033 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8034 | } |
| 8035 | } |
| 8036 | } |
| 8037 | |
Argyrios Kyrtzidis | 01bf777 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8038 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
| 8039 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, |
| 8040 | Expr *OrArg) { |
| 8041 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { |
| 8042 | if (Bop->getOpcode() == BO_And) |
| 8043 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); |
| 8044 | } |
| 8045 | } |
| 8046 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8047 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8048 | /// precedence. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8049 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8050 | SourceLocation OpLoc, Expr *LHSExpr, |
| 8051 | Expr *RHSExpr){ |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8052 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8053 | if (BinaryOperator::isBitwiseOp(Opc)) |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8054 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | 01bf777 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8055 | |
| 8056 | // Diagnose "arg1 & arg2 | arg3" |
| 8057 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8058 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, LHSExpr); |
| 8059 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, RHSExpr); |
Argyrios Kyrtzidis | 01bf777 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8060 | } |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8061 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8062 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 8063 | // We don't warn for 'assert(a || b && "bad")' since this is safe. |
Argyrios Kyrtzidis | b94e5a3 | 2010-11-17 18:54:22 +0000 | [diff] [blame] | 8064 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8065 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, LHSExpr, RHSExpr); |
| 8066 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8067 | } |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8068 | } |
| 8069 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8070 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8071 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8072 | tok::TokenKind Kind, |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8073 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8074 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8075 | assert((LHSExpr != 0) && "ActOnBinOp(): missing left expression"); |
| 8076 | assert((RHSExpr != 0) && "ActOnBinOp(): missing right expression"); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8077 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8078 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8079 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, LHSExpr, RHSExpr); |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8080 | |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8081 | return BuildBinOp(S, TokLoc, Opc, LHSExpr, RHSExpr); |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8082 | } |
| 8083 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8084 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8085 | BinaryOperatorKind Opc, |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8086 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8087 | if (getLangOptions().CPlusPlus) { |
| 8088 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8089 | |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8090 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) { |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8091 | UseBuiltinOperator = false; |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8092 | } else if (Opc == BO_Assign && |
| 8093 | LHSExpr->getObjectKind() == OK_ObjCProperty) { |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8094 | UseBuiltinOperator = true; |
| 8095 | } else { |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8096 | UseBuiltinOperator = !LHSExpr->getType()->isOverloadableType() && |
| 8097 | !RHSExpr->getType()->isOverloadableType(); |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8098 | } |
| 8099 | |
| 8100 | if (!UseBuiltinOperator) { |
| 8101 | // Find all of the overloaded operators visible from this |
| 8102 | // point. We perform both an operator-name lookup from the local |
| 8103 | // scope and an argument-dependent lookup based on the types of |
| 8104 | // the arguments. |
| 8105 | UnresolvedSet<16> Functions; |
| 8106 | OverloadedOperatorKind OverOp |
| 8107 | = BinaryOperator::getOverloadedOperator(Opc); |
| 8108 | if (S && OverOp != OO_None) |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8109 | LookupOverloadedOperatorName(OverOp, S, LHSExpr->getType(), |
| 8110 | RHSExpr->getType(), Functions); |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8111 | |
| 8112 | // Build the (potentially-overloaded, potentially-dependent) |
| 8113 | // binary operation. |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8114 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, LHSExpr, RHSExpr); |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8115 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8116 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8117 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8118 | // Build a built-in binary operation. |
Richard Trieu | f9bd0f5 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8119 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8120 | } |
| 8121 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8122 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8123 | UnaryOperatorKind Opc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8124 | Expr *InputExpr) { |
| 8125 | ExprResult Input = Owned(InputExpr); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8126 | ExprValueKind VK = VK_RValue; |
| 8127 | ExprObjectKind OK = OK_Ordinary; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8128 | QualType resultType; |
| 8129 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8130 | case UO_PreInc: |
| 8131 | case UO_PreDec: |
| 8132 | case UO_PostInc: |
| 8133 | case UO_PostDec: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8134 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8135 | Opc == UO_PreInc || |
| 8136 | Opc == UO_PostInc, |
| 8137 | Opc == UO_PreInc || |
| 8138 | Opc == UO_PreDec); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8139 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8140 | case UO_AddrOf: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8141 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8142 | break; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8143 | case UO_Deref: { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8144 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8145 | if (!resolved.isUsable()) return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8146 | Input = move(resolved); |
| 8147 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8148 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8149 | break; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8150 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8151 | case UO_Plus: |
| 8152 | case UO_Minus: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8153 | Input = UsualUnaryConversions(Input.take()); |
| 8154 | if (Input.isInvalid()) return ExprError(); |
| 8155 | resultType = Input.get()->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8156 | if (resultType->isDependentType()) |
| 8157 | break; |
Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8158 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8159 | resultType->isVectorType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8160 | break; |
| 8161 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 8162 | resultType->isEnumeralType()) |
| 8163 | break; |
| 8164 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8165 | Opc == UO_Plus && |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8166 | resultType->isPointerType()) |
| 8167 | break; |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8168 | else if (resultType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8169 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8170 | if (Input.isInvalid()) return ExprError(); |
| 8171 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8172 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8173 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8174 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8175 | << resultType << Input.get()->getSourceRange()); |
| 8176 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8177 | case UO_Not: // bitwise complement |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8178 | Input = UsualUnaryConversions(Input.take()); |
| 8179 | if (Input.isInvalid()) return ExprError(); |
| 8180 | resultType = Input.get()->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8181 | if (resultType->isDependentType()) |
| 8182 | break; |
Chris Lattner | 0d70761 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8183 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8184 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8185 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8186 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8187 | << resultType << Input.get()->getSourceRange(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8188 | else if (resultType->hasIntegerRepresentation()) |
| 8189 | break; |
| 8190 | else if (resultType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8191 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8192 | if (Input.isInvalid()) return ExprError(); |
| 8193 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8194 | } else { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8195 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8196 | << resultType << Input.get()->getSourceRange()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8197 | } |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8198 | break; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8199 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8200 | case UO_LNot: // logical negation |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 8201 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8202 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8203 | if (Input.isInvalid()) return ExprError(); |
| 8204 | resultType = Input.get()->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8205 | if (resultType->isDependentType()) |
| 8206 | break; |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8207 | if (resultType->isScalarType()) { |
| 8208 | // C99 6.5.3.3p1: ok, fallthrough; |
| 8209 | if (Context.getLangOptions().CPlusPlus) { |
| 8210 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 8211 | // operand contextually converted to bool. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8212 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 8213 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8214 | } |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8215 | } else if (resultType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8216 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8217 | if (Input.isInvalid()) return ExprError(); |
| 8218 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8219 | } else { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8220 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8221 | << resultType << Input.get()->getSourceRange()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8222 | } |
Douglas Gregor | db8c6fd | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8223 | |
Chris Lattner | be31ed8 | 2007-06-02 19:11:33 +0000 | [diff] [blame] | 8224 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8225 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 1bdd688 | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8226 | resultType = Context.getLogicalOperationType(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8227 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8228 | case UO_Real: |
| 8229 | case UO_Imag: |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8230 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8231 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8232 | if (Input.isInvalid()) return ExprError(); |
| 8233 | if (Input.get()->getValueKind() != VK_RValue && |
| 8234 | Input.get()->getObjectKind() == OK_Ordinary) |
| 8235 | VK = Input.get()->getValueKind(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8236 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8237 | case UO_Extension: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8238 | resultType = Input.get()->getType(); |
| 8239 | VK = Input.get()->getValueKind(); |
| 8240 | OK = Input.get()->getObjectKind(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 8241 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8242 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8243 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8244 | return ExprError(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8245 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8246 | // Check for array bounds violations in the operand of the UnaryOperator, |
| 8247 | // except for the '*' and '&' operators that have to be handled specially |
| 8248 | // by CheckArrayAccess (as there are special cases like &array[arraysize] |
| 8249 | // that are explicitly defined as valid by the standard). |
| 8250 | if (Opc != UO_AddrOf && Opc != UO_Deref) |
| 8251 | CheckArrayAccess(Input.get()); |
| 8252 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8253 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8254 | VK, OK, OpLoc)); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8255 | } |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 8256 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8257 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8258 | UnaryOperatorKind Opc, Expr *Input) { |
Anders Carlsson | 461a2c0 | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 8259 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 8ed2bac | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 8260 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8261 | // Find all of the overloaded operators visible from this |
| 8262 | // point. We perform both an operator-name lookup from the local |
| 8263 | // scope and an argument-dependent lookup based on the types of |
| 8264 | // the arguments. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8265 | UnresolvedSet<16> Functions; |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8266 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8267 | if (S && OverOp != OO_None) |
| 8268 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 8269 | Functions); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8270 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8271 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8272 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8273 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8274 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8275 | } |
| 8276 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8277 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8278 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8279 | tok::TokenKind Op, Expr *Input) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8280 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8281 | } |
| 8282 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8283 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8284 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8285 | LabelDecl *TheDecl) { |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8286 | TheDecl->setUsed(); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 8287 | // Create the AST node. The address of a label always has type 'void*'. |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8288 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8289 | Context.getPointerType(Context.VoidTy))); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 8290 | } |
| 8291 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8292 | /// Given the last statement in a statement-expression, check whether |
| 8293 | /// the result is a producing expression (like a call to an |
| 8294 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 8295 | /// release out of the full-expression. Otherwise, return null. |
| 8296 | /// Cannot fail. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8297 | static Expr *maybeRebuildARCConsumingStmt(Stmt *Statement) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8298 | // Should always be wrapped with one of these. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8299 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(Statement); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8300 | if (!cleanups) return 0; |
| 8301 | |
| 8302 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 8303 | if (!cast || cast->getCastKind() != CK_ARCConsumeObject) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8304 | return 0; |
| 8305 | |
| 8306 | // Splice out the cast. This shouldn't modify any interesting |
| 8307 | // features of the statement. |
| 8308 | Expr *producer = cast->getSubExpr(); |
| 8309 | assert(producer->getType() == cast->getType()); |
| 8310 | assert(producer->getValueKind() == cast->getValueKind()); |
| 8311 | cleanups->setSubExpr(producer); |
| 8312 | return cleanups; |
| 8313 | } |
| 8314 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8315 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8316 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8317 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8318 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8319 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8320 | |
Douglas Gregor | 6cf3f3c | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8321 | bool isFileScope |
| 8322 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | a69b076 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8323 | if (isFileScope) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8324 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | 52cc016 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8325 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8326 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8327 | // example, it is not possible to goto into a stmt expression apparently. |
| 8328 | // More semantic analysis is needed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8329 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8330 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8331 | // as the type of the stmtexpr. |
| 8332 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8333 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8334 | if (!Compound->body_empty()) { |
| 8335 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8336 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8337 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8338 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8339 | LastLabelStmt = Label; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8340 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8341 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8342 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8343 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8344 | // Do function/array conversion on the last expression, but not |
| 8345 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8346 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 8347 | if (LastExpr.isInvalid()) |
| 8348 | return ExprError(); |
| 8349 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8350 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8351 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8352 | // In ARC, if the final expression ends in a consume, splice |
| 8353 | // the consume out and bind it later. In the alternate case |
| 8354 | // (when dealing with a retainable type), the result |
| 8355 | // initialization will create a produce. In both cases the |
| 8356 | // result will be +1, and we'll need to balance that out with |
| 8357 | // a bind. |
| 8358 | if (Expr *rebuiltLastStmt |
| 8359 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 8360 | LastExpr = rebuiltLastStmt; |
| 8361 | } else { |
| 8362 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8363 | InitializedEntity::InitializeResult(LPLoc, |
| 8364 | Ty, |
| 8365 | false), |
| 8366 | SourceLocation(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8367 | LastExpr); |
| 8368 | } |
| 8369 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8370 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8371 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8372 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8373 | if (!LastLabelStmt) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8374 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8375 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8376 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8377 | StmtExprMayBindToTemp = true; |
| 8378 | } |
| 8379 | } |
| 8380 | } |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8381 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8382 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8383 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8384 | // expressions are not lvalues. |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8385 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8386 | if (StmtExprMayBindToTemp) |
| 8387 | return MaybeBindToTemporary(ResStmtExpr); |
| 8388 | return Owned(ResStmtExpr); |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8389 | } |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8390 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8391 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8392 | TypeSourceInfo *TInfo, |
| 8393 | OffsetOfComponent *CompPtr, |
| 8394 | unsigned NumComponents, |
| 8395 | SourceLocation RParenLoc) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8396 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8397 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8398 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8399 | |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8400 | // We must have at least one component that refers to the type, and the first |
| 8401 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8402 | // a struct/union/class. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8403 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8404 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 8405 | << ArgTy << TypeRange); |
| 8406 | |
| 8407 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 8408 | // with an incomplete type would be ill-formed. |
| 8409 | if (!Dependent |
| 8410 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 8411 | PDiag(diag::err_offsetof_incomplete_type) |
| 8412 | << TypeRange)) |
| 8413 | return ExprError(); |
| 8414 | |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8415 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 8416 | // GCC extension, diagnose them. |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 8417 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 8418 | // a system header! |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8419 | if (NumComponents != 1) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8420 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 8421 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8422 | |
| 8423 | bool DidWarnAboutNonPOD = false; |
| 8424 | QualType CurrentType = ArgTy; |
| 8425 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8426 | SmallVector<OffsetOfNode, 4> Comps; |
| 8427 | SmallVector<Expr*, 4> Exprs; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8428 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 8429 | const OffsetOfComponent &OC = CompPtr[i]; |
| 8430 | if (OC.isBrackets) { |
| 8431 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 8432 | if (!CurrentType->isDependentType()) { |
| 8433 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 8434 | if(!AT) |
| 8435 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 8436 | << CurrentType); |
| 8437 | CurrentType = AT->getElementType(); |
| 8438 | } else |
| 8439 | CurrentType = Context.DependentTy; |
| 8440 | |
| 8441 | // The expression must be an integral expression. |
| 8442 | // FIXME: An integral constant expression? |
| 8443 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 8444 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 8445 | !Idx->getType()->isIntegerType()) |
| 8446 | return ExprError(Diag(Idx->getLocStart(), |
| 8447 | diag::err_typecheck_subscript_not_integer) |
| 8448 | << Idx->getSourceRange()); |
| 8449 | |
| 8450 | // Record this array index. |
| 8451 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 8452 | Exprs.push_back(Idx); |
| 8453 | continue; |
| 8454 | } |
| 8455 | |
| 8456 | // Offset of a field. |
| 8457 | if (CurrentType->isDependentType()) { |
| 8458 | // We have the offset of a field, but we can't look into the dependent |
| 8459 | // type. Just record the identifier of the field. |
| 8460 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 8461 | CurrentType = Context.DependentTy; |
| 8462 | continue; |
| 8463 | } |
| 8464 | |
| 8465 | // We need to have a complete type to look into. |
| 8466 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 8467 | diag::err_offsetof_incomplete_type)) |
| 8468 | return ExprError(); |
| 8469 | |
| 8470 | // Look for the designated field. |
| 8471 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 8472 | if (!RC) |
| 8473 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 8474 | << CurrentType); |
| 8475 | RecordDecl *RD = RC->getDecl(); |
| 8476 | |
| 8477 | // C++ [lib.support.types]p5: |
| 8478 | // The macro offsetof accepts a restricted set of type arguments in this |
| 8479 | // International Standard. type shall be a POD structure or a POD union |
| 8480 | // (clause 9). |
| 8481 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 8482 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 55ae319 | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 8483 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8484 | PDiag(diag::warn_offsetof_non_pod_type) |
| 8485 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 8486 | << CurrentType)) |
| 8487 | DidWarnAboutNonPOD = true; |
| 8488 | } |
| 8489 | |
| 8490 | // Look for the field. |
| 8491 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 8492 | LookupQualifiedName(R, RD); |
| 8493 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8494 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 8495 | if (!MemberDecl) { |
Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 8496 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8497 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 8498 | } |
| 8499 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8500 | if (!MemberDecl) |
| 8501 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 8502 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 8503 | OC.LocEnd)); |
| 8504 | |
Douglas Gregor | 10982ea | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 8505 | // C99 7.17p3: |
| 8506 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 8507 | // |
| 8508 | // We diagnose this as an error. |
| 8509 | if (MemberDecl->getBitWidth()) { |
| 8510 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 8511 | << MemberDecl->getDeclName() |
| 8512 | << SourceRange(BuiltinLoc, RParenLoc); |
| 8513 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 8514 | return ExprError(); |
| 8515 | } |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8516 | |
| 8517 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8518 | if (IndirectMemberDecl) |
| 8519 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8520 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8521 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 8522 | // the base class indirections. |
| 8523 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8524 | /*DetectVirtual=*/false); |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8525 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8526 | CXXBasePath &Path = Paths.front(); |
| 8527 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 8528 | B != BEnd; ++B) |
| 8529 | Comps.push_back(OffsetOfNode(B->Base)); |
| 8530 | } |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8531 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8532 | if (IndirectMemberDecl) { |
| 8533 | for (IndirectFieldDecl::chain_iterator FI = |
| 8534 | IndirectMemberDecl->chain_begin(), |
| 8535 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 8536 | assert(isa<FieldDecl>(*FI)); |
| 8537 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 8538 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 8539 | } |
| 8540 | } else |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8541 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8542 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8543 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 8544 | } |
| 8545 | |
| 8546 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 8547 | TInfo, Comps.data(), Comps.size(), |
| 8548 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 8549 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8550 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8551 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8552 | SourceLocation BuiltinLoc, |
| 8553 | SourceLocation TypeLoc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8554 | ParsedType ParsedArgTy, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8555 | OffsetOfComponent *CompPtr, |
| 8556 | unsigned NumComponents, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8557 | SourceLocation RParenLoc) { |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8558 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8559 | TypeSourceInfo *ArgTInfo; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8560 | QualType ArgTy = GetTypeFromParser(ParsedArgTy, &ArgTInfo); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8561 | if (ArgTy.isNull()) |
| 8562 | return ExprError(); |
| 8563 | |
Eli Friedman | 06dcfd9 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 8564 | if (!ArgTInfo) |
| 8565 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 8566 | |
| 8567 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8568 | RParenLoc); |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8569 | } |
| 8570 | |
| 8571 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8572 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8573 | Expr *CondExpr, |
| 8574 | Expr *LHSExpr, Expr *RHSExpr, |
| 8575 | SourceLocation RPLoc) { |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8576 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 8577 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8578 | ExprValueKind VK = VK_RValue; |
| 8579 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8580 | QualType resType; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8581 | bool ValueDependent = false; |
Douglas Gregor | 0df9112 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 8582 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8583 | resType = Context.DependentTy; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8584 | ValueDependent = true; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8585 | } else { |
| 8586 | // The conditional expression is required to be a constant expression. |
| 8587 | llvm::APSInt condEval(32); |
| 8588 | SourceLocation ExpLoc; |
| 8589 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8590 | return ExprError(Diag(ExpLoc, |
| 8591 | diag::err_typecheck_choose_expr_requires_constant) |
| 8592 | << CondExpr->getSourceRange()); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8593 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8594 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8595 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 8596 | |
| 8597 | resType = ActiveExpr->getType(); |
| 8598 | ValueDependent = ActiveExpr->isValueDependent(); |
| 8599 | VK = ActiveExpr->getValueKind(); |
| 8600 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8601 | } |
| 8602 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8603 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8604 | resType, VK, OK, RPLoc, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8605 | resType->isDependentType(), |
| 8606 | ValueDependent)); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8607 | } |
| 8608 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8609 | //===----------------------------------------------------------------------===// |
| 8610 | // Clang Extensions. |
| 8611 | //===----------------------------------------------------------------------===// |
| 8612 | |
| 8613 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8614 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *CurScope) { |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8615 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8616 | PushBlockScope(CurScope, Block); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8617 | CurContext->addDecl(Block); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8618 | if (CurScope) |
| 8619 | PushDeclContext(CurScope, Block); |
Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8620 | else |
| 8621 | CurContext = Block; |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8622 | } |
| 8623 | |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8624 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 8625 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8626 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8627 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8628 | |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8629 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8630 | QualType T = Sig->getType(); |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8631 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8632 | // GetTypeForDeclarator always produces a function type for a block |
| 8633 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 8634 | // unless the function was written with a typedef. |
| 8635 | assert(T->isFunctionType() && |
| 8636 | "GetTypeForDeclarator made a non-function block signature"); |
| 8637 | |
| 8638 | // Look for an explicit signature in that function type. |
| 8639 | FunctionProtoTypeLoc ExplicitSignature; |
| 8640 | |
| 8641 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 8642 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 8643 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 8644 | |
| 8645 | // Check whether that explicit signature was synthesized by |
| 8646 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 8647 | // written signature. |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8648 | if (ExplicitSignature.getLocalRangeBegin() == |
| 8649 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8650 | // This would be much cheaper if we stored TypeLocs instead of |
| 8651 | // TypeSourceInfos. |
| 8652 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 8653 | unsigned Size = Result.getFullDataSize(); |
| 8654 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 8655 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 8656 | |
| 8657 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 8658 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8659 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8660 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8661 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 8662 | CurBlock->FunctionType = T; |
| 8663 | |
| 8664 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 8665 | QualType RetTy = Fn->getResultType(); |
| 8666 | bool isVariadic = |
| 8667 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 8668 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8669 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8670 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8671 | // Don't allow returning a objc interface by value. |
| 8672 | if (RetTy->isObjCObjectType()) { |
| 8673 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 8674 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 8675 | return; |
| 8676 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8677 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8678 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8679 | // return type. TODO: what should we do with declarators like: |
| 8680 | // ^ * { ... } |
| 8681 | // If the answer is "apply template argument deduction".... |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8682 | if (RetTy != Context.DependentTy) |
| 8683 | CurBlock->ReturnType = RetTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8684 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8685 | // Push block parameters from the declarator if we had them. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8686 | SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8687 | if (ExplicitSignature) { |
| 8688 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 8689 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8690 | if (Param->getIdentifier() == 0 && |
| 8691 | !Param->isImplicit() && |
| 8692 | !Param->isInvalidDecl() && |
| 8693 | !getLangOptions().CPlusPlus) |
| 8694 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8695 | Params.push_back(Param); |
Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8696 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8697 | |
| 8698 | // Fake up parameter variables if we have a typedef, like |
| 8699 | // ^ fntype { ... } |
| 8700 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 8701 | for (FunctionProtoType::arg_type_iterator |
| 8702 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 8703 | ParmVarDecl *Param = |
| 8704 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 8705 | ParamInfo.getSourceRange().getBegin(), |
| 8706 | *I); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8707 | Params.push_back(Param); |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8708 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8709 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8710 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8711 | // Set the parameters on the block decl. |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8712 | if (!Params.empty()) { |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8713 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8714 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 8715 | CurBlock->TheDecl->param_end(), |
| 8716 | /*CheckParameterNames=*/false); |
| 8717 | } |
| 8718 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8719 | // Finally we can process decl attributes. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 8720 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8721 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8722 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8723 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 8724 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 8725 | // FIXME: remove the attribute. |
| 8726 | } |
| 8727 | |
| 8728 | // Put the parameter variables in scope. We can bail out immediately |
| 8729 | // if we don't have any. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8730 | if (Params.empty()) |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8731 | return; |
| 8732 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8733 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8734 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 8735 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 8736 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8737 | // If this has an identifier, add it to the scope stack. |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8738 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8739 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8740 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8741 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8742 | } |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8743 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8744 | } |
| 8745 | |
| 8746 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 8747 | /// is invoked to pop the information about the block from the action impl. |
| 8748 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8749 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 41b8694 | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 8750 | PopDeclContext(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8751 | PopFunctionOrBlockScope(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8752 | } |
| 8753 | |
| 8754 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 8755 | /// literal was successfully completed. ^(int x){...} |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8756 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | 60f8449 | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8757 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 8758 | // If blocks are disabled, emit an error. |
| 8759 | if (!LangOpts.Blocks) |
| 8760 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8761 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8762 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8763 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8764 | PopDeclContext(); |
| 8765 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8766 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 8767 | if (!BSI->ReturnType.isNull()) |
| 8768 | RetTy = BSI->ReturnType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8769 | |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 8770 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8771 | QualType BlockTy; |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8772 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8773 | // Set the captured variables on the block. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 8774 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 8775 | BSI->CapturesCXXThis); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8776 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8777 | // If the user wrote a function type in some form, try to use that. |
| 8778 | if (!BSI->FunctionType.isNull()) { |
| 8779 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 8780 | |
| 8781 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 8782 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 8783 | |
| 8784 | // Turn protoless block types into nullary block types. |
| 8785 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8786 | FunctionProtoType::ExtProtoInfo EPI; |
| 8787 | EPI.ExtInfo = Ext; |
| 8788 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8789 | |
| 8790 | // Otherwise, if we don't need to change anything about the function type, |
| 8791 | // preserve its sugar structure. |
| 8792 | } else if (FTy->getResultType() == RetTy && |
| 8793 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 8794 | BlockTy = BSI->FunctionType; |
| 8795 | |
| 8796 | // Otherwise, make the minimal modifications to the function type. |
| 8797 | } else { |
| 8798 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8799 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8800 | EPI.TypeQuals = 0; // FIXME: silently? |
| 8801 | EPI.ExtInfo = Ext; |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8802 | BlockTy = Context.getFunctionType(RetTy, |
| 8803 | FPT->arg_type_begin(), |
| 8804 | FPT->getNumArgs(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8805 | EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8806 | } |
| 8807 | |
| 8808 | // If we don't have a function type, just build one from nothing. |
| 8809 | } else { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8810 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8811 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8812 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8813 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8814 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8815 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 8816 | BSI->TheDecl->param_end()); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8817 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8818 | |
Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 8819 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8820 | if (getCurFunction()->NeedsScopeChecking() && |
| 8821 | !hasAnyUnrecoverableErrorsInThisFunction()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8822 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8823 | |
Chris Lattner | 60f8449 | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8824 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8825 | |
Fariborz Jahanian | 256d39d | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 8826 | for (BlockDecl::capture_const_iterator ci = BSI->TheDecl->capture_begin(), |
| 8827 | ce = BSI->TheDecl->capture_end(); ci != ce; ++ci) { |
| 8828 | const VarDecl *variable = ci->getVariable(); |
| 8829 | QualType T = variable->getType(); |
| 8830 | QualType::DestructionKind destructKind = T.isDestructedType(); |
| 8831 | if (destructKind != QualType::DK_none) |
| 8832 | getCurFunction()->setHasBranchProtectedScope(); |
| 8833 | } |
| 8834 | |
Douglas Gregor | 49695f0 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 8835 | computeNRVO(Body, getCurBlock()); |
| 8836 | |
Benjamin Kramer | a4fb836 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 8837 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
| 8838 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 8839 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
| 8840 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8841 | return Owned(Result); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8842 | } |
| 8843 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8844 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8845 | Expr *E, ParsedType Ty, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8846 | SourceLocation RPLoc) { |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8847 | TypeSourceInfo *TInfo; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8848 | GetTypeFromParser(Ty, &TInfo); |
| 8849 | return BuildVAArgExpr(BuiltinLoc, E, TInfo, RPLoc); |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8850 | } |
| 8851 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8852 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8853 | Expr *E, TypeSourceInfo *TInfo, |
| 8854 | SourceLocation RPLoc) { |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8855 | Expr *OrigExpr = E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8856 | |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8857 | // Get the va_list type |
| 8858 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8859 | if (VaListType->isArrayType()) { |
| 8860 | // Deal with implicit array decay; for example, on x86-64, |
| 8861 | // va_list is an array, but it's supposed to decay to |
| 8862 | // a pointer for va_arg. |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8863 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8864 | // Make sure the input expression also decays appropriately. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8865 | ExprResult Result = UsualUnaryConversions(E); |
| 8866 | if (Result.isInvalid()) |
| 8867 | return ExprError(); |
| 8868 | E = Result.take(); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8869 | } else { |
| 8870 | // Otherwise, the va_list argument must be an l-value because |
| 8871 | // it is modified by va_arg. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8872 | if (!E->isTypeDependent() && |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8873 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8874 | return ExprError(); |
| 8875 | } |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8876 | |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8877 | if (!E->isTypeDependent() && |
| 8878 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8879 | return ExprError(Diag(E->getLocStart(), |
| 8880 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8881 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 3f5cd77 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 8882 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8883 | |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8884 | if (!TInfo->getType()->isDependentType()) { |
| 8885 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
| 8886 | PDiag(diag::err_second_parameter_to_va_arg_incomplete) |
| 8887 | << TInfo->getTypeLoc().getSourceRange())) |
| 8888 | return ExprError(); |
David Majnemer | 254a5c0 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 8889 | |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8890 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
| 8891 | TInfo->getType(), |
| 8892 | PDiag(diag::err_second_parameter_to_va_arg_abstract) |
| 8893 | << TInfo->getTypeLoc().getSourceRange())) |
| 8894 | return ExprError(); |
| 8895 | |
Douglas Gregor | 7e1eb93 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8896 | if (!TInfo->getType().isPODType(Context)) { |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8897 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 7e1eb93 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8898 | TInfo->getType()->isObjCLifetimeType() |
| 8899 | ? diag::warn_second_parameter_to_va_arg_ownership_qualified |
| 8900 | : diag::warn_second_parameter_to_va_arg_not_pod) |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8901 | << TInfo->getType() |
| 8902 | << TInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | 7e1eb93 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8903 | } |
Eli Friedman | 6290ae4 | 2011-07-11 21:45:59 +0000 | [diff] [blame] | 8904 | |
| 8905 | // Check for va_arg where arguments of the given type will be promoted |
| 8906 | // (i.e. this va_arg is guaranteed to have undefined behavior). |
| 8907 | QualType PromoteType; |
| 8908 | if (TInfo->getType()->isPromotableIntegerType()) { |
| 8909 | PromoteType = Context.getPromotedIntegerType(TInfo->getType()); |
| 8910 | if (Context.typesAreCompatible(PromoteType, TInfo->getType())) |
| 8911 | PromoteType = QualType(); |
| 8912 | } |
| 8913 | if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) |
| 8914 | PromoteType = Context.DoubleTy; |
| 8915 | if (!PromoteType.isNull()) |
| 8916 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 8917 | diag::warn_second_parameter_to_va_arg_never_compatible) |
| 8918 | << TInfo->getType() |
| 8919 | << PromoteType |
| 8920 | << TInfo->getTypeLoc().getSourceRange(); |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8921 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8922 | |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8923 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 8924 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 8925 | } |
| 8926 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8927 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8928 | // The type of __null will be int or long, depending on the size of |
| 8929 | // pointers on the target. |
| 8930 | QualType Ty; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8931 | unsigned pw = Context.getTargetInfo().getPointerWidth(0); |
| 8932 | if (pw == Context.getTargetInfo().getIntWidth()) |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8933 | Ty = Context.IntTy; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8934 | else if (pw == Context.getTargetInfo().getLongWidth()) |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8935 | Ty = Context.LongTy; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8936 | else if (pw == Context.getTargetInfo().getLongLongWidth()) |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8937 | Ty = Context.LongLongTy; |
| 8938 | else { |
| 8939 | assert(!"I don't know size of pointer!"); |
| 8940 | Ty = Context.IntTy; |
| 8941 | } |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8942 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8943 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8944 | } |
| 8945 | |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8946 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8947 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8948 | if (!SemaRef.getLangOptions().ObjC1) |
| 8949 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8950 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8951 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 8952 | if (!PT) |
| 8953 | return; |
| 8954 | |
| 8955 | // Check if the destination is of type 'id'. |
| 8956 | if (!PT->isObjCIdType()) { |
| 8957 | // Check if the destination is the 'NSString' interface. |
| 8958 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 8959 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 8960 | return; |
| 8961 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8962 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8963 | // Strip off any parens and casts. |
| 8964 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 8965 | if (!SL || !SL->isAscii()) |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8966 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8967 | |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8968 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8969 | } |
| 8970 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8971 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 8972 | SourceLocation Loc, |
| 8973 | QualType DstType, QualType SrcType, |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8974 | Expr *SrcExpr, AssignmentAction Action, |
| 8975 | bool *Complained) { |
| 8976 | if (Complained) |
| 8977 | *Complained = false; |
| 8978 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8979 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8980 | bool CheckInferredResultType = false; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8981 | bool isInvalid = false; |
| 8982 | unsigned DiagKind; |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8983 | FixItHint Hint; |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8984 | ConversionFixItGenerator ConvHints; |
| 8985 | bool MayHaveConvFixit = false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8986 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8987 | switch (ConvTy) { |
| 8988 | default: assert(0 && "Unknown conversion type"); |
| 8989 | case Compatible: return false; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8990 | case PointerToInt: |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8991 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8992 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8993 | MayHaveConvFixit = true; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8994 | break; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8995 | case IntToPointer: |
| 8996 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8997 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8998 | MayHaveConvFixit = true; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8999 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9000 | case IncompatiblePointer: |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9001 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9002 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9003 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 9004 | SrcType->isObjCObjectPointerType(); |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9005 | if (Hint.isNull() && !CheckInferredResultType) { |
| 9006 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9007 | } |
| 9008 | MayHaveConvFixit = true; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9009 | break; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9010 | case IncompatiblePointerSign: |
| 9011 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 9012 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9013 | case FunctionVoidPointer: |
| 9014 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 9015 | break; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9016 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 71de91c | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9017 | // Perform array-to-pointer decay if necessary. |
| 9018 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9019 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9020 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9021 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9022 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9023 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9024 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9025 | |
| 9026 | |
| 9027 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 9028 | DiagKind = diag::err_typecheck_incompatible_ownership; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9029 | break; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9030 | } |
| 9031 | |
| 9032 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9033 | // fallthrough |
| 9034 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9035 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9036 | // If the qualifiers lost were because we were applying the |
| 9037 | // (deprecated) C++ conversion from a string literal to a char* |
| 9038 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9039 | // Ideally, this check would be performed in |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9040 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9041 | // bit of refactoring (so that the second argument is an |
| 9042 | // expression, rather than a type), which should be done as part |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9043 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9044 | // C++ semantics. |
| 9045 | if (getLangOptions().CPlusPlus && |
| 9046 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9047 | return false; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9048 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9049 | break; |
Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9050 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | b98dade | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9051 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9052 | break; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9053 | case IntToBlockPointer: |
| 9054 | DiagKind = diag::err_int_to_block_pointer; |
| 9055 | break; |
| 9056 | case IncompatibleBlockPointer: |
Mike Stump | d79b5a8 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9057 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9058 | break; |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9059 | case IncompatibleObjCQualifiedId: |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9060 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9061 | // it can give a more specific diagnostic. |
| 9062 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9063 | break; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9064 | case IncompatibleVectors: |
| 9065 | DiagKind = diag::warn_incompatible_vectors; |
| 9066 | break; |
Fariborz Jahanian | 6f472e8 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 9067 | case IncompatibleObjCWeakRef: |
| 9068 | DiagKind = diag::err_arc_weak_unavailable_assign; |
| 9069 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9070 | case Incompatible: |
| 9071 | DiagKind = diag::err_typecheck_convert_incompatible; |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9072 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9073 | MayHaveConvFixit = true; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9074 | isInvalid = true; |
| 9075 | break; |
| 9076 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9077 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9078 | QualType FirstType, SecondType; |
| 9079 | switch (Action) { |
| 9080 | case AA_Assigning: |
| 9081 | case AA_Initializing: |
| 9082 | // The destination type comes first. |
| 9083 | FirstType = DstType; |
| 9084 | SecondType = SrcType; |
| 9085 | break; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9086 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9087 | case AA_Returning: |
| 9088 | case AA_Passing: |
| 9089 | case AA_Converting: |
| 9090 | case AA_Sending: |
| 9091 | case AA_Casting: |
| 9092 | // The source type comes first. |
| 9093 | FirstType = SrcType; |
| 9094 | SecondType = DstType; |
| 9095 | break; |
| 9096 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9097 | |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9098 | PartialDiagnostic FDiag = PDiag(DiagKind); |
| 9099 | FDiag << FirstType << SecondType << Action << SrcExpr->getSourceRange(); |
| 9100 | |
| 9101 | // If we can fix the conversion, suggest the FixIts. |
| 9102 | assert(ConvHints.isNull() || Hint.isNull()); |
| 9103 | if (!ConvHints.isNull()) { |
| 9104 | for (llvm::SmallVector<FixItHint, 1>::iterator |
| 9105 | HI = ConvHints.Hints.begin(), HE = ConvHints.Hints.end(); |
| 9106 | HI != HE; ++HI) |
| 9107 | FDiag << *HI; |
| 9108 | } else { |
| 9109 | FDiag << Hint; |
| 9110 | } |
| 9111 | if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } |
| 9112 | |
| 9113 | Diag(Loc, FDiag); |
| 9114 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9115 | if (CheckInferredResultType) |
| 9116 | EmitRelatedResultTypeNote(SrcExpr); |
| 9117 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9118 | if (Complained) |
| 9119 | *Complained = true; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9120 | return isInvalid; |
| 9121 | } |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9122 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 9123 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9124 | llvm::APSInt ICEResult; |
| 9125 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 9126 | if (Result) |
| 9127 | *Result = ICEResult; |
| 9128 | return false; |
| 9129 | } |
| 9130 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9131 | Expr::EvalResult EvalResult; |
| 9132 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9133 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9134 | EvalResult.HasSideEffects) { |
| 9135 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 9136 | |
| 9137 | if (EvalResult.Diag) { |
| 9138 | // We only show the note if it's not the usual "invalid subexpression" |
| 9139 | // or if it's actually in a subexpression. |
| 9140 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 9141 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 9142 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 9143 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9144 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9145 | return true; |
| 9146 | } |
| 9147 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9148 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 9149 | E->getSourceRange(); |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9150 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9151 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9152 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 9153 | != Diagnostic::Ignored) |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9154 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9155 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9156 | if (Result) |
| 9157 | *Result = EvalResult.Val.getInt(); |
| 9158 | return false; |
| 9159 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9160 | |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9161 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9162 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9163 | ExprEvalContexts.push_back( |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9164 | ExpressionEvaluationContextRecord(NewContext, |
| 9165 | ExprTemporaries.size(), |
| 9166 | ExprNeedsCleanups)); |
| 9167 | ExprNeedsCleanups = false; |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9168 | } |
| 9169 | |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9170 | void Sema::PopExpressionEvaluationContext() { |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9171 | // Pop the current expression evaluation context off the stack. |
| 9172 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 9173 | ExprEvalContexts.pop_back(); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9174 | |
Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9175 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 9176 | if (Rec.PotentiallyReferenced) { |
| 9177 | // Mark any remaining declarations in the current position of the stack |
| 9178 | // as "referenced". If they were not meant to be referenced, semantic |
| 9179 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9180 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9181 | I = Rec.PotentiallyReferenced->begin(), |
| 9182 | IEnd = Rec.PotentiallyReferenced->end(); |
| 9183 | I != IEnd; ++I) |
| 9184 | MarkDeclarationReferenced(I->first, I->second); |
| 9185 | } |
| 9186 | |
| 9187 | if (Rec.PotentiallyDiagnosed) { |
| 9188 | // Emit any pending diagnostics. |
| 9189 | for (PotentiallyEmittedDiagnostics::iterator |
| 9190 | I = Rec.PotentiallyDiagnosed->begin(), |
| 9191 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 9192 | I != IEnd; ++I) |
| 9193 | Diag(I->first, I->second); |
| 9194 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9195 | } |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9196 | |
| 9197 | // When are coming out of an unevaluated context, clear out any |
| 9198 | // temporaries that we may have created as part of the evaluation of |
| 9199 | // the expression in that context: they aren't relevant because they |
| 9200 | // will never be constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9201 | if (Rec.Context == Unevaluated) { |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9202 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 9203 | ExprTemporaries.end()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9204 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
| 9205 | |
| 9206 | // Otherwise, merge the contexts together. |
| 9207 | } else { |
| 9208 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
| 9209 | } |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9210 | |
| 9211 | // Destroy the popped expression evaluation record. |
| 9212 | Rec.Destroy(); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9213 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9214 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9215 | void Sema::DiscardCleanupsInEvaluationContext() { |
| 9216 | ExprTemporaries.erase( |
| 9217 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 9218 | ExprTemporaries.end()); |
| 9219 | ExprNeedsCleanups = false; |
| 9220 | } |
| 9221 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9222 | /// \brief Note that the given declaration was referenced in the source code. |
| 9223 | /// |
| 9224 | /// This routine should be invoke whenever a given declaration is referenced |
| 9225 | /// in the source code, and where that reference occurred. If this declaration |
| 9226 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 9227 | /// C99 6.9p3), then the declaration will be marked as used. |
| 9228 | /// |
| 9229 | /// \param Loc the location where the declaration was referenced. |
| 9230 | /// |
| 9231 | /// \param D the declaration that has been referenced by the source code. |
| 9232 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 9233 | assert(D && "No declaration?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9234 | |
Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 9235 | D->setReferenced(); |
| 9236 | |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9237 | if (D->isUsed(false)) |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9238 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9239 | |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9240 | // Mark a parameter or variable declaration "used", regardless of whether |
| 9241 | // we're in a template or not. The reason for this is that unevaluated |
| 9242 | // expressions (e.g. (void)sizeof()) constitute a use for warning purposes |
| 9243 | // (-Wunused-variables and -Wunused-parameters) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9244 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9245 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 73067a0 | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 9246 | D->setUsed(); |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9247 | return; |
| 9248 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9249 | |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9250 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 9251 | return; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9252 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9253 | // Do not mark anything as "used" within a dependent context; wait for |
| 9254 | // an instantiation. |
| 9255 | if (CurContext->isDependentContext()) |
| 9256 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9257 | |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9258 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9259 | case Unevaluated: |
| 9260 | // We are in an expression that is not potentially evaluated; do nothing. |
| 9261 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9262 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9263 | case PotentiallyEvaluated: |
| 9264 | // We are in a potentially-evaluated expression, so this declaration is |
| 9265 | // "used"; handle this below. |
| 9266 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9267 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9268 | case PotentiallyPotentiallyEvaluated: |
| 9269 | // We are in an expression that may be potentially evaluated; queue this |
| 9270 | // declaration reference until we know whether the expression is |
| 9271 | // potentially evaluated. |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9272 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9273 | return; |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9274 | |
| 9275 | case PotentiallyEvaluatedIfUsed: |
| 9276 | // Referenced declarations will only be used if the construct in the |
| 9277 | // containing expression is used. |
| 9278 | return; |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9279 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9280 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9281 | // Note that this declaration has been used. |
Fariborz Jahanian | 3a36343 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 9282 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9283 | if (Constructor->isDefaulted()) { |
| 9284 | if (Constructor->isDefaultConstructor()) { |
| 9285 | if (Constructor->isTrivial()) |
| 9286 | return; |
| 9287 | if (!Constructor->isUsed(false)) |
| 9288 | DefineImplicitDefaultConstructor(Loc, Constructor); |
| 9289 | } else if (Constructor->isCopyConstructor()) { |
| 9290 | if (!Constructor->isUsed(false)) |
| 9291 | DefineImplicitCopyConstructor(Loc, Constructor); |
| 9292 | } else if (Constructor->isMoveConstructor()) { |
| 9293 | if (!Constructor->isUsed(false)) |
| 9294 | DefineImplicitMoveConstructor(Loc, Constructor); |
| 9295 | } |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9296 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9297 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9298 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9299 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9300 | if (Destructor->isDefaulted() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9301 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9302 | if (Destructor->isVirtual()) |
| 9303 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9304 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 9305 | if (MethodDecl->isDefaulted() && MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9306 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9307 | if (!MethodDecl->isUsed(false)) { |
| 9308 | if (MethodDecl->isCopyAssignmentOperator()) |
| 9309 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
| 9310 | else |
| 9311 | DefineImplicitMoveAssignment(Loc, MethodDecl); |
| 9312 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9313 | } else if (MethodDecl->isVirtual()) |
| 9314 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9315 | } |
Fariborz Jahanian | 49796cc7 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 9316 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9317 | // Recursive functions should be marked when used from another function. |
| 9318 | if (CurContext == Function) return; |
| 9319 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9320 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 9321 | // class templates. |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 9322 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9323 | bool AlreadyInstantiated = false; |
| 9324 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 9325 | = Function->getTemplateSpecializationInfo()) { |
| 9326 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 9327 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9328 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9329 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9330 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9331 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9332 | = Function->getMemberSpecializationInfo()) { |
| 9333 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 9334 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9335 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9336 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9337 | AlreadyInstantiated = true; |
| 9338 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9339 | |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9340 | if (!AlreadyInstantiated) { |
| 9341 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 9342 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 9343 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 9344 | Loc)); |
| 9345 | else |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9346 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9347 | } |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9348 | } else { |
| 9349 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9350 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 9351 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | 34ecff2 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 9352 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9353 | MarkDeclarationReferenced(Loc, *i); |
| 9354 | } |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9355 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9356 | |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9357 | // Keep track of used but undefined functions. |
| 9358 | if (!Function->isPure() && !Function->hasBody() && |
| 9359 | Function->getLinkage() != ExternalLinkage) { |
| 9360 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 9361 | if (old.isInvalid()) old = Loc; |
| 9362 | } |
Argyrios Kyrtzidis | dfffabd | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 9363 | |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9364 | Function->setUsed(true); |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9365 | return; |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9366 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9367 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9368 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9369 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9370 | if (Var->isStaticDataMember() && |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9371 | Var->getInstantiatedFromStaticDataMember()) { |
| 9372 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 9373 | assert(MSInfo && "Missing member specialization information?"); |
| 9374 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 9375 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 9376 | MSInfo->setPointOfInstantiation(Loc); |
Sebastian Redl | 2ac2c72 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 9377 | // This is a modification of an existing AST node. Notify listeners. |
| 9378 | if (ASTMutationListener *L = getASTMutationListener()) |
| 9379 | L->StaticDataMemberInstantiated(Var); |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9380 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9381 | } |
| 9382 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9383 | |
John McCall | 15dd404 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9384 | // Keep track of used but undefined variables. We make a hole in |
| 9385 | // the warning for static const data members with in-line |
| 9386 | // initializers. |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9387 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 15dd404 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9388 | && Var->getLinkage() != ExternalLinkage |
| 9389 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9390 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 9391 | if (old.isInvalid()) old = Loc; |
| 9392 | } |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9393 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9394 | D->setUsed(true); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9395 | return; |
Sam Weinig | bae6914 | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 9396 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9397 | } |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9398 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9399 | namespace { |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9400 | // Mark all of the declarations referenced |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9401 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9402 | // of when we're entering |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9403 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 9404 | Sema &S; |
| 9405 | SourceLocation Loc; |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9406 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9407 | public: |
| 9408 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9409 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9410 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9411 | |
| 9412 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 9413 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9414 | }; |
| 9415 | } |
| 9416 | |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9417 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 9418 | const TemplateArgument &Arg) { |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9419 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 9420 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 9421 | } |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9422 | |
| 9423 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9424 | } |
| 9425 | |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9426 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9427 | if (ClassTemplateSpecializationDecl *Spec |
| 9428 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 9429 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9430 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9431 | } |
| 9432 | |
Chandler Carruth | c65667c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 9433 | return true; |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9434 | } |
| 9435 | |
| 9436 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 9437 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9438 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9439 | } |
| 9440 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9441 | namespace { |
| 9442 | /// \brief Helper class that marks all of the declarations referenced by |
| 9443 | /// potentially-evaluated subexpressions as "referenced". |
| 9444 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 9445 | Sema &S; |
| 9446 | |
| 9447 | public: |
| 9448 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 9449 | |
| 9450 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 9451 | |
| 9452 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 9453 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9454 | } |
| 9455 | |
| 9456 | void VisitMemberExpr(MemberExpr *E) { |
| 9457 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9458 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9459 | } |
| 9460 | |
| 9461 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 9462 | if (E->getConstructor()) |
| 9463 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 9464 | if (E->getOperatorNew()) |
| 9465 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 9466 | if (E->getOperatorDelete()) |
| 9467 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9468 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9469 | } |
| 9470 | |
| 9471 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 9472 | if (E->getOperatorDelete()) |
| 9473 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 9474 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 9475 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 9476 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 9477 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 9478 | S.LookupDestructor(Record)); |
| 9479 | } |
| 9480 | |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9481 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9482 | } |
| 9483 | |
| 9484 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 9485 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9486 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9487 | } |
| 9488 | |
| 9489 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 9490 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9491 | } |
Douglas Gregor | f0873f4 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 9492 | |
| 9493 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 9494 | Visit(E->getExpr()); |
| 9495 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9496 | }; |
| 9497 | } |
| 9498 | |
| 9499 | /// \brief Mark any declarations that appear within this expression or any |
| 9500 | /// potentially-evaluated subexpressions as "referenced". |
| 9501 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 9502 | EvaluatedExprMarker(*this).Visit(E); |
| 9503 | } |
| 9504 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9505 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 9506 | /// of the program being compiled. |
| 9507 | /// |
| 9508 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9509 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9510 | /// possibility that the code will actually be executable. Code in sizeof() |
| 9511 | /// expressions, code used only during overload resolution, etc., are not |
| 9512 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 9513 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9514 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9515 | /// later. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9516 | /// |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9517 | /// This routine should be used for all diagnostics that describe the run-time |
| 9518 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 9519 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 9520 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9521 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *Statement, |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9522 | const PartialDiagnostic &PD) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9523 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9524 | case Unevaluated: |
| 9525 | // The argument will never be evaluated, so don't complain. |
| 9526 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9527 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9528 | case PotentiallyEvaluated: |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9529 | case PotentiallyEvaluatedIfUsed: |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9530 | if (Statement && getCurFunctionOrMethodDecl()) { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 9531 | FunctionScopes.back()->PossiblyUnreachableDiags. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9532 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, Statement)); |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 9533 | } |
| 9534 | else |
| 9535 | Diag(Loc, PD); |
| 9536 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9537 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9538 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9539 | case PotentiallyPotentiallyEvaluated: |
| 9540 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 9541 | break; |
| 9542 | } |
| 9543 | |
| 9544 | return false; |
| 9545 | } |
| 9546 | |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9547 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 9548 | CallExpr *CE, FunctionDecl *FD) { |
| 9549 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 9550 | return false; |
| 9551 | |
| 9552 | PartialDiagnostic Note = |
| 9553 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 9554 | << FD->getDeclName() : PDiag(); |
| 9555 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9556 | |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9557 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9558 | FD ? |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9559 | PDiag(diag::err_call_function_incomplete_return) |
| 9560 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9561 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9562 | << CE->getSourceRange(), |
| 9563 | std::make_pair(NoteLoc, Note))) |
| 9564 | return true; |
| 9565 | |
| 9566 | return false; |
| 9567 | } |
| 9568 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9569 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9570 | // will prevent this condition from triggering, which is what we want. |
| 9571 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 9572 | SourceLocation Loc; |
| 9573 | |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9574 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9575 | bool IsOrAssign = false; |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9576 | |
Chandler Carruth | f87d6c0 | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 9577 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9578 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9579 | return; |
| 9580 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9581 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 9582 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9583 | // Greylist some idioms by putting them into a warning subcategory. |
| 9584 | if (ObjCMessageExpr *ME |
| 9585 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 9586 | Selector Sel = ME->getSelector(); |
| 9587 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9588 | // self = [<foo> init...] |
Fariborz Jahanian | db8015c | 2011-09-17 19:23:40 +0000 | [diff] [blame] | 9589 | if (GetMethodIfSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9590 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9591 | |
| 9592 | // <foo> = [<bar> nextObject] |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9593 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9594 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9595 | } |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9596 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9597 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | f87d6c0 | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 9598 | } else if (CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9599 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9600 | return; |
| 9601 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9602 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9603 | Loc = Op->getOperatorLoc(); |
| 9604 | } else { |
| 9605 | // Not an assignment. |
| 9606 | return; |
| 9607 | } |
| 9608 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9609 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9610 | |
Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9611 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 9612 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 9613 | Diag(Loc, diag::note_condition_assign_silence) |
| 9614 | << FixItHint::CreateInsertion(Open, "(") |
| 9615 | << FixItHint::CreateInsertion(Close, ")"); |
| 9616 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9617 | if (IsOrAssign) |
| 9618 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 9619 | << FixItHint::CreateReplacement(Loc, "!="); |
| 9620 | else |
| 9621 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 9622 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9623 | } |
| 9624 | |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9625 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 9626 | /// that the user intended an assignment used as condition. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9627 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *ParenE) { |
Argyrios Kyrtzidis | f4f8278 | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9628 | // Don't warn if the parens came from a macro. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9629 | SourceLocation parenLoc = ParenE->getLocStart(); |
Argyrios Kyrtzidis | f4f8278 | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9630 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 9631 | return; |
Argyrios Kyrtzidis | ba699d6 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 9632 | // Don't warn for dependent expressions. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9633 | if (ParenE->isTypeDependent()) |
Argyrios Kyrtzidis | ba699d6 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 9634 | return; |
Argyrios Kyrtzidis | f4f8278 | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9635 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9636 | Expr *E = ParenE->IgnoreParens(); |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9637 | |
| 9638 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 582dd68 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 9639 | if (opE->getOpcode() == BO_EQ && |
| 9640 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 9641 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9642 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | c358d9f | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 9643 | |
Ted Kremenek | ae02209 | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9644 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Ted Kremenek | ae02209 | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9645 | Diag(Loc, diag::note_equality_comparison_silence) |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9646 | << FixItHint::CreateRemoval(ParenE->getSourceRange().getBegin()) |
| 9647 | << FixItHint::CreateRemoval(ParenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9648 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 9649 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9650 | } |
| 9651 | } |
| 9652 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9653 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9654 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9655 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 9656 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9657 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9658 | ExprResult result = CheckPlaceholderExpr(E); |
| 9659 | if (result.isInvalid()) return ExprError(); |
| 9660 | E = result.take(); |
Argyrios Kyrtzidis | ca76629 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 9661 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9662 | if (!E->isTypeDependent()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9663 | if (getLangOptions().CPlusPlus) |
| 9664 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 9665 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9666 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 9667 | if (ERes.isInvalid()) |
| 9668 | return ExprError(); |
| 9669 | E = ERes.take(); |
John McCall | 29cb2fd | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 9670 | |
| 9671 | QualType T = E->getType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9672 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 9673 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 9674 | << T << E->getSourceRange(); |
| 9675 | return ExprError(); |
| 9676 | } |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9677 | } |
| 9678 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9679 | return Owned(E); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9680 | } |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9681 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9682 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9683 | Expr *SubExpr) { |
| 9684 | if (!SubExpr) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9685 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9686 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9687 | return CheckBooleanCondition(SubExpr, Loc); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9688 | } |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9689 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9690 | namespace { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9691 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 9692 | /// to have an appropriate type. |
| 9693 | struct RebuildUnknownAnyFunction |
| 9694 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 9695 | |
| 9696 | Sema &S; |
| 9697 | |
| 9698 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 9699 | |
| 9700 | ExprResult VisitStmt(Stmt *S) { |
| 9701 | llvm_unreachable("unexpected statement!"); |
| 9702 | return ExprError(); |
| 9703 | } |
| 9704 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9705 | ExprResult VisitExpr(Expr *E) { |
| 9706 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 9707 | << E->getSourceRange(); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9708 | return ExprError(); |
| 9709 | } |
| 9710 | |
| 9711 | /// Rebuild an expression which simply semantically wraps another |
| 9712 | /// expression which it shares the type and value kind of. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9713 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 9714 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 9715 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9716 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9717 | Expr *SubExpr = SubResult.take(); |
| 9718 | E->setSubExpr(SubExpr); |
| 9719 | E->setType(SubExpr->getType()); |
| 9720 | E->setValueKind(SubExpr->getValueKind()); |
| 9721 | assert(E->getObjectKind() == OK_Ordinary); |
| 9722 | return E; |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9723 | } |
| 9724 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9725 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 9726 | return rebuildSugarExpr(E); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9727 | } |
| 9728 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9729 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 9730 | return rebuildSugarExpr(E); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9731 | } |
| 9732 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9733 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 9734 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 9735 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9736 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9737 | Expr *SubExpr = SubResult.take(); |
| 9738 | E->setSubExpr(SubExpr); |
| 9739 | E->setType(S.Context.getPointerType(SubExpr->getType())); |
| 9740 | assert(E->getValueKind() == VK_RValue); |
| 9741 | assert(E->getObjectKind() == OK_Ordinary); |
| 9742 | return E; |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9743 | } |
| 9744 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9745 | ExprResult resolveDecl(Expr *E, ValueDecl *VD) { |
| 9746 | if (!isa<FunctionDecl>(VD)) return VisitExpr(E); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9747 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9748 | E->setType(VD->getType()); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9749 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9750 | assert(E->getValueKind() == VK_RValue); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9751 | if (S.getLangOptions().CPlusPlus && |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9752 | !(isa<CXXMethodDecl>(VD) && |
| 9753 | cast<CXXMethodDecl>(VD)->isInstance())) |
| 9754 | E->setValueKind(VK_LValue); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9755 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9756 | return E; |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9757 | } |
| 9758 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9759 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 9760 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9761 | } |
| 9762 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9763 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 9764 | return resolveDecl(E, E->getDecl()); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9765 | } |
| 9766 | }; |
| 9767 | } |
| 9768 | |
| 9769 | /// Given a function expression of unknown-any type, try to rebuild it |
| 9770 | /// to have a function type. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9771 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *FunctionExpr) { |
| 9772 | ExprResult Result = RebuildUnknownAnyFunction(S).Visit(FunctionExpr); |
| 9773 | if (Result.isInvalid()) return ExprError(); |
| 9774 | return S.DefaultFunctionArrayConversion(Result.take()); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9775 | } |
| 9776 | |
| 9777 | namespace { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9778 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 9779 | /// into one which resolves the type directly on the referring |
| 9780 | /// expression. Strict preservation of the original source |
| 9781 | /// structure is not a goal. |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9782 | struct RebuildUnknownAnyExpr |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9783 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9784 | |
| 9785 | Sema &S; |
| 9786 | |
| 9787 | /// The current destination type. |
| 9788 | QualType DestType; |
| 9789 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9790 | RebuildUnknownAnyExpr(Sema &S, QualType CastType) |
| 9791 | : S(S), DestType(CastType) {} |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9792 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9793 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9794 | llvm_unreachable("unexpected statement!"); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9795 | return ExprError(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9796 | } |
| 9797 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9798 | ExprResult VisitExpr(Expr *E) { |
| 9799 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 9800 | << E->getSourceRange(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9801 | return ExprError(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9802 | } |
| 9803 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9804 | ExprResult VisitCallExpr(CallExpr *E); |
| 9805 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *E); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9806 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9807 | /// Rebuild an expression which simply semantically wraps another |
| 9808 | /// expression which it shares the type and value kind of. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9809 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 9810 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 9811 | if (SubResult.isInvalid()) return ExprError(); |
| 9812 | Expr *SubExpr = SubResult.take(); |
| 9813 | E->setSubExpr(SubExpr); |
| 9814 | E->setType(SubExpr->getType()); |
| 9815 | E->setValueKind(SubExpr->getValueKind()); |
| 9816 | assert(E->getObjectKind() == OK_Ordinary); |
| 9817 | return E; |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9818 | } |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9819 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9820 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 9821 | return rebuildSugarExpr(E); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9822 | } |
| 9823 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9824 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 9825 | return rebuildSugarExpr(E); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9826 | } |
| 9827 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9828 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 9829 | const PointerType *Ptr = DestType->getAs<PointerType>(); |
| 9830 | if (!Ptr) { |
| 9831 | S.Diag(E->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 9832 | << E->getSourceRange(); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9833 | return ExprError(); |
| 9834 | } |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9835 | assert(E->getValueKind() == VK_RValue); |
| 9836 | assert(E->getObjectKind() == OK_Ordinary); |
| 9837 | E->setType(DestType); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9838 | |
| 9839 | // Build the sub-expression as if it were an object of the pointee type. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9840 | DestType = Ptr->getPointeeType(); |
| 9841 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 9842 | if (SubResult.isInvalid()) return ExprError(); |
| 9843 | E->setSubExpr(SubResult.take()); |
| 9844 | return E; |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9845 | } |
| 9846 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9847 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *E); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9848 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9849 | ExprResult resolveDecl(Expr *E, ValueDecl *VD); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9850 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9851 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 9852 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9853 | } |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9854 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9855 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 9856 | return resolveDecl(E, E->getDecl()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9857 | } |
| 9858 | }; |
| 9859 | } |
| 9860 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9861 | /// Rebuilds a call expression which yielded __unknown_anytype. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9862 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) { |
| 9863 | Expr *CalleeExpr = E->getCallee(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9864 | |
| 9865 | enum FnKind { |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9866 | FK_MemberFunction, |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9867 | FK_FunctionPointer, |
| 9868 | FK_BlockPointer |
| 9869 | }; |
| 9870 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9871 | FnKind Kind; |
| 9872 | QualType CalleeType = CalleeExpr->getType(); |
| 9873 | if (CalleeType == S.Context.BoundMemberTy) { |
| 9874 | assert(isa<CXXMemberCallExpr>(E) || isa<CXXOperatorCallExpr>(E)); |
| 9875 | Kind = FK_MemberFunction; |
| 9876 | CalleeType = Expr::findBoundMemberType(CalleeExpr); |
| 9877 | } else if (const PointerType *Ptr = CalleeType->getAs<PointerType>()) { |
| 9878 | CalleeType = Ptr->getPointeeType(); |
| 9879 | Kind = FK_FunctionPointer; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9880 | } else { |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9881 | CalleeType = CalleeType->castAs<BlockPointerType>()->getPointeeType(); |
| 9882 | Kind = FK_BlockPointer; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9883 | } |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9884 | const FunctionType *FnType = CalleeType->castAs<FunctionType>(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9885 | |
| 9886 | // Verify that this is a legal result type of a function. |
| 9887 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 9888 | unsigned diagID = diag::err_func_returning_array_function; |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9889 | if (Kind == FK_BlockPointer) |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9890 | diagID = diag::err_block_returning_array_function; |
| 9891 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9892 | S.Diag(E->getExprLoc(), diagID) |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9893 | << DestType->isFunctionType() << DestType; |
| 9894 | return ExprError(); |
| 9895 | } |
| 9896 | |
| 9897 | // Otherwise, go ahead and set DestType as the call's result. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9898 | E->setType(DestType.getNonLValueExprType(S.Context)); |
| 9899 | E->setValueKind(Expr::getValueKindForType(DestType)); |
| 9900 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9901 | |
| 9902 | // Rebuild the function type, replacing the result type with DestType. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9903 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType)) |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9904 | DestType = S.Context.getFunctionType(DestType, |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9905 | Proto->arg_type_begin(), |
| 9906 | Proto->getNumArgs(), |
| 9907 | Proto->getExtProtoInfo()); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9908 | else |
| 9909 | DestType = S.Context.getFunctionNoProtoType(DestType, |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9910 | FnType->getExtInfo()); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9911 | |
| 9912 | // Rebuild the appropriate pointer-to-function type. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9913 | switch (Kind) { |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9914 | case FK_MemberFunction: |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9915 | // Nothing to do. |
| 9916 | break; |
| 9917 | |
| 9918 | case FK_FunctionPointer: |
| 9919 | DestType = S.Context.getPointerType(DestType); |
| 9920 | break; |
| 9921 | |
| 9922 | case FK_BlockPointer: |
| 9923 | DestType = S.Context.getBlockPointerType(DestType); |
| 9924 | break; |
| 9925 | } |
| 9926 | |
| 9927 | // Finally, we can recurse. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9928 | ExprResult CalleeResult = Visit(CalleeExpr); |
| 9929 | if (!CalleeResult.isUsable()) return ExprError(); |
| 9930 | E->setCallee(CalleeResult.take()); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9931 | |
| 9932 | // Bind a temporary if necessary. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9933 | return S.MaybeBindToTemporary(E); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9934 | } |
| 9935 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9936 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9937 | // Verify that this is a legal result type of a call. |
| 9938 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9939 | S.Diag(E->getExprLoc(), diag::err_func_returning_array_function) |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9940 | << DestType->isFunctionType() << DestType; |
| 9941 | return ExprError(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9942 | } |
| 9943 | |
John McCall | 3f4138c | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 9944 | // Rewrite the method result type if available. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9945 | if (ObjCMethodDecl *Method = E->getMethodDecl()) { |
| 9946 | assert(Method->getResultType() == S.Context.UnknownAnyTy); |
| 9947 | Method->setResultType(DestType); |
John McCall | 3f4138c | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 9948 | } |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9949 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9950 | // Change the type of the message. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9951 | E->setType(DestType.getNonReferenceType()); |
| 9952 | E->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9953 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9954 | return S.MaybeBindToTemporary(E); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9955 | } |
| 9956 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9957 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9958 | // The only case we should ever see here is a function-to-pointer decay. |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9959 | assert(E->getCastKind() == CK_FunctionToPointerDecay); |
| 9960 | assert(E->getValueKind() == VK_RValue); |
| 9961 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9962 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9963 | E->setType(DestType); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9964 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9965 | // Rebuild the sub-expression as the pointee (function) type. |
| 9966 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 9967 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9968 | ExprResult Result = Visit(E->getSubExpr()); |
| 9969 | if (!Result.isUsable()) return ExprError(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9970 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9971 | E->setSubExpr(Result.take()); |
| 9972 | return S.Owned(E); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9973 | } |
| 9974 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9975 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *E, ValueDecl *VD) { |
| 9976 | ExprValueKind ValueKind = VK_LValue; |
| 9977 | QualType Type = DestType; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9978 | |
| 9979 | // We know how to make this work for certain kinds of decls: |
| 9980 | |
| 9981 | // - functions |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9982 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(VD)) { |
| 9983 | if (const PointerType *Ptr = Type->getAs<PointerType>()) { |
| 9984 | DestType = Ptr->getPointeeType(); |
| 9985 | ExprResult Result = resolveDecl(E, VD); |
| 9986 | if (Result.isInvalid()) return ExprError(); |
| 9987 | return S.ImpCastExprToType(Result.take(), Type, |
John McCall | 9a877fe | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 9988 | CK_FunctionToPointerDecay, VK_RValue); |
| 9989 | } |
| 9990 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9991 | if (!Type->isFunctionType()) { |
| 9992 | S.Diag(E->getExprLoc(), diag::err_unknown_any_function) |
| 9993 | << VD << E->getSourceRange(); |
John McCall | 9a877fe | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 9994 | return ExprError(); |
| 9995 | } |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9996 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 9997 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 9998 | if (MD->isInstance()) { |
| 9999 | ValueKind = VK_RValue; |
| 10000 | Type = S.Context.BoundMemberTy; |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10001 | } |
| 10002 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10003 | // Function references aren't l-values in C. |
| 10004 | if (!S.getLangOptions().CPlusPlus) |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10005 | ValueKind = VK_RValue; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10006 | |
| 10007 | // - variables |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10008 | } else if (isa<VarDecl>(VD)) { |
| 10009 | if (const ReferenceType *RefTy = Type->getAs<ReferenceType>()) { |
| 10010 | Type = RefTy->getPointeeType(); |
| 10011 | } else if (Type->isFunctionType()) { |
| 10012 | S.Diag(E->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 10013 | << VD << E->getSourceRange(); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10014 | return ExprError(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10015 | } |
| 10016 | |
| 10017 | // - nothing else |
| 10018 | } else { |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10019 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 10020 | << VD << E->getSourceRange(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10021 | return ExprError(); |
| 10022 | } |
| 10023 | |
Richard Trieu | 10162ab | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10024 | VD->setType(DestType); |
| 10025 | E->setType(Type); |
| 10026 | E->setValueKind(ValueKind); |
| 10027 | return S.Owned(E); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10028 | } |
| 10029 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10030 | /// Check a cast of an unknown-any type. We intentionally only |
| 10031 | /// trigger this for C-style casts. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10032 | ExprResult Sema::checkUnknownAnyCast(SourceRange TypeRange, QualType CastType, |
| 10033 | Expr *CastExpr, CastKind &CastKind, |
| 10034 | ExprValueKind &VK, CXXCastPath &Path) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10035 | // Rewrite the casted expression from scratch. |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10036 | ExprResult result = RebuildUnknownAnyExpr(*this, CastType).Visit(CastExpr); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10037 | if (!result.isUsable()) return ExprError(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10038 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10039 | CastExpr = result.take(); |
| 10040 | VK = CastExpr->getValueKind(); |
| 10041 | CastKind = CK_NoOp; |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10042 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10043 | return CastExpr; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10044 | } |
| 10045 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10046 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *E) { |
| 10047 | Expr *orig = E; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10048 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10049 | while (true) { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10050 | E = E->IgnoreParenImpCasts(); |
| 10051 | if (CallExpr *call = dyn_cast<CallExpr>(E)) { |
| 10052 | E = call->getCallee(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10053 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10054 | } else { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10055 | break; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10056 | } |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10057 | } |
| 10058 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10059 | SourceLocation loc; |
| 10060 | NamedDecl *d; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10061 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(E)) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10062 | loc = ref->getLocation(); |
| 10063 | d = ref->getDecl(); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10064 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(E)) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10065 | loc = mem->getMemberLoc(); |
| 10066 | d = mem->getMemberDecl(); |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10067 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(E)) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10068 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10069 | loc = msg->getSelectorLoc(); |
| 10070 | d = msg->getMethodDecl(); |
John McCall | fa6f5d6 | 2011-08-31 20:57:36 +0000 | [diff] [blame] | 10071 | if (!d) { |
| 10072 | S.Diag(loc, diag::err_uncasted_send_to_unknown_any_method) |
| 10073 | << static_cast<unsigned>(msg->isClassMessage()) << msg->getSelector() |
| 10074 | << orig->getSourceRange(); |
| 10075 | return ExprError(); |
| 10076 | } |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10077 | } else { |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10078 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10079 | << E->getSourceRange(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10080 | return ExprError(); |
| 10081 | } |
| 10082 | |
| 10083 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10084 | |
| 10085 | // Never recoverable. |
| 10086 | return ExprError(); |
| 10087 | } |
| 10088 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10089 | /// Check for operands with placeholder types and complain if found. |
| 10090 | /// Returns true if there was an error and no recovery was possible. |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 10091 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10092 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 10093 | QualType type = E->getType(); |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10094 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10095 | // Overloaded expressions. |
| 10096 | if (type == Context.OverloadTy) |
| 10097 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
Douglas Gregor | 89f3cd5 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 10098 | E->getSourceRange(), |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10099 | QualType(), |
| 10100 | diag::err_ovl_unresolvable); |
| 10101 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10102 | // Bound member functions. |
| 10103 | if (type == Context.BoundMemberTy) { |
| 10104 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 10105 | << E->getSourceRange(); |
| 10106 | return ExprError(); |
| 10107 | } |
| 10108 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10109 | // Expressions of unknown type. |
| 10110 | if (type == Context.UnknownAnyTy) |
| 10111 | return diagnoseUnknownAnyExpr(*this, E); |
| 10112 | |
| 10113 | assert(!type->isPlaceholderType()); |
| 10114 | return Owned(E); |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10115 | } |
Richard Trieu | 2c850c0 | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10116 | |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10117 | bool Sema::CheckCaseExpression(Expr *E) { |
| 10118 | if (E->isTypeDependent()) |
Richard Trieu | 2c850c0 | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10119 | return true; |
Richard Trieu | ba63ce6 | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10120 | if (E->isValueDependent() || E->isIntegerConstantExpr(Context)) |
| 10121 | return E->getType()->isIntegralOrEnumerationType(); |
Richard Trieu | 2c850c0 | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10122 | return false; |
| 10123 | } |