Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 15 | #include "clang/Sema/DelayedDiagnostic.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Initialization.h" |
| 17 | #include "clang/Sema/Lookup.h" |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 19 | #include "clang/Sema/AnalysisBasedWarnings.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTConsumer.h" |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 23 | #include "clang/AST/CXXInheritance.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 26 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 27 | #include "clang/AST/Expr.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 28 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 29 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 30 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 31 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 32 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 35 | #include "clang/Lex/LiteralSupport.h" |
| 36 | #include "clang/Lex/Preprocessor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 37 | #include "clang/Sema/DeclSpec.h" |
| 38 | #include "clang/Sema/Designator.h" |
| 39 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 40 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 41 | #include "clang/Sema/ParsedTemplate.h" |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 42 | #include "clang/Sema/SemaFixItUtils.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 43 | #include "clang/Sema/Template.h" |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 44 | #include "TreeTransform.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 46 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 48 | /// \brief Determine whether the use of this declaration is valid, without |
| 49 | /// emitting diagnostics. |
| 50 | bool Sema::CanUseDecl(NamedDecl *D) { |
| 51 | // See if this is an auto-typed variable whose initializer we are parsing. |
| 52 | if (ParsingInitForAutoVars.count(D)) |
| 53 | return false; |
| 54 | |
| 55 | // See if this is a deleted function. |
| 56 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 57 | if (FD->isDeleted()) |
| 58 | return false; |
| 59 | } |
Sebastian Redl | 28bdb14 | 2011-10-16 18:19:16 +0000 | [diff] [blame] | 60 | |
| 61 | // See if this function is unavailable. |
| 62 | if (D->getAvailability() == AR_Unavailable && |
| 63 | cast<Decl>(CurContext)->getAvailability() != AR_Unavailable) |
| 64 | return false; |
| 65 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 66 | return true; |
| 67 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 69 | static AvailabilityResult DiagnoseAvailabilityOfDecl(Sema &S, |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 70 | NamedDecl *D, SourceLocation Loc, |
| 71 | const ObjCInterfaceDecl *UnknownObjCClass) { |
| 72 | // See if this declaration is unavailable or deprecated. |
| 73 | std::string Message; |
| 74 | AvailabilityResult Result = D->getAvailability(&Message); |
Fariborz Jahanian | 39b4fc8 | 2011-11-28 19:45:58 +0000 | [diff] [blame] | 75 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) |
| 76 | if (Result == AR_Available) { |
| 77 | const DeclContext *DC = ECD->getDeclContext(); |
| 78 | if (const EnumDecl *TheEnumDecl = dyn_cast<EnumDecl>(DC)) |
| 79 | Result = TheEnumDecl->getAvailability(&Message); |
| 80 | } |
| 81 | |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 82 | switch (Result) { |
| 83 | case AR_Available: |
| 84 | case AR_NotYetIntroduced: |
| 85 | break; |
| 86 | |
| 87 | case AR_Deprecated: |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 88 | S.EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass); |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 89 | break; |
| 90 | |
| 91 | case AR_Unavailable: |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 92 | if (S.getCurContextAvailability() != AR_Unavailable) { |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 93 | if (Message.empty()) { |
| 94 | if (!UnknownObjCClass) |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 95 | S.Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 96 | else |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 97 | S.Diag(Loc, diag::warn_unavailable_fwdclass_message) |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 98 | << D->getDeclName(); |
| 99 | } |
| 100 | else |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 101 | S.Diag(Loc, diag::err_unavailable_message) |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 102 | << D->getDeclName() << Message; |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 103 | S.Diag(D->getLocation(), diag::note_unavailable_here) |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 104 | << isa<FunctionDecl>(D) << false; |
| 105 | } |
| 106 | break; |
| 107 | } |
| 108 | return Result; |
| 109 | } |
| 110 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 111 | /// \brief Determine whether the use of this declaration is valid, and |
| 112 | /// emit any corresponding diagnostics. |
| 113 | /// |
| 114 | /// This routine diagnoses various problems with referencing |
| 115 | /// declarations that can occur when using a declaration. For example, |
| 116 | /// it might warn if a deprecated or unavailable declaration is being |
| 117 | /// used, or produce an error (and return true) if a C++0x deleted |
| 118 | /// function is being used. |
| 119 | /// |
| 120 | /// \returns true if there was an error (this declaration cannot be |
| 121 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 122 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 123 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 124 | const ObjCInterfaceDecl *UnknownObjCClass) { |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 125 | if (getLangOptions().CPlusPlus && isa<FunctionDecl>(D)) { |
| 126 | // If there were any diagnostics suppressed by template argument deduction, |
| 127 | // emit them now. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 128 | llvm::DenseMap<Decl *, SmallVector<PartialDiagnosticAt, 1> >::iterator |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 129 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 130 | if (Pos != SuppressedDiagnostics.end()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 131 | SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 132 | for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) |
| 133 | Diag(Suppressed[I].first, Suppressed[I].second); |
| 134 | |
| 135 | // Clear out the list of suppressed diagnostics, so that we don't emit |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 136 | // them again for this specialization. However, we don't obsolete this |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 137 | // entry from the table, because we want to avoid ever emitting these |
| 138 | // diagnostics again. |
| 139 | Suppressed.clear(); |
| 140 | } |
| 141 | } |
| 142 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 143 | // See if this is an auto-typed variable whose initializer we are parsing. |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 144 | if (ParsingInitForAutoVars.count(D)) { |
| 145 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 146 | << D->getDeclName(); |
| 147 | return true; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 150 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 151 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 152 | if (FD->isDeleted()) { |
| 153 | Diag(Loc, diag::err_deleted_function_use); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 154 | Diag(D->getLocation(), diag::note_unavailable_here) << 1 << true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 155 | return true; |
| 156 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 157 | } |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 158 | DiagnoseAvailabilityOfDecl(*this, D, Loc, UnknownObjCClass); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 159 | |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 160 | // Warn if this is used but marked unused. |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 161 | if (D->hasAttr<UnusedAttr>()) |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 162 | Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 163 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 166 | /// \brief Retrieve the message suffix that should be added to a |
| 167 | /// diagnostic complaining about the given function being deleted or |
| 168 | /// unavailable. |
| 169 | std::string Sema::getDeletedOrUnavailableSuffix(const FunctionDecl *FD) { |
| 170 | // FIXME: C++0x implicitly-deleted special member functions could be |
| 171 | // detected here so that we could improve diagnostics to say, e.g., |
| 172 | // "base class 'A' had a deleted copy constructor". |
| 173 | if (FD->isDeleted()) |
| 174 | return std::string(); |
| 175 | |
| 176 | std::string Message; |
| 177 | if (FD->getAvailability(&Message)) |
| 178 | return ": " + Message; |
| 179 | |
| 180 | return std::string(); |
| 181 | } |
| 182 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 183 | /// DiagnoseSentinelCalls - This routine checks whether a call or |
| 184 | /// message-send is to a declaration with the sentinel attribute, and |
| 185 | /// if so, it checks that the requirements of the sentinel are |
| 186 | /// satisfied. |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 187 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 188 | Expr **args, unsigned numArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 189 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 191 | return; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 192 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 193 | // The number of formal parameters of the declaration. |
| 194 | unsigned numFormalParams; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 196 | // The kind of declaration. This is also an index into a %select in |
| 197 | // the diagnostic. |
| 198 | enum CalleeType { CT_Function, CT_Method, CT_Block } calleeType; |
| 199 | |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 200 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 201 | numFormalParams = MD->param_size(); |
| 202 | calleeType = CT_Method; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 203 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 204 | numFormalParams = FD->param_size(); |
| 205 | calleeType = CT_Function; |
| 206 | } else if (isa<VarDecl>(D)) { |
| 207 | QualType type = cast<ValueDecl>(D)->getType(); |
| 208 | const FunctionType *fn = 0; |
| 209 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 210 | fn = ptr->getPointeeType()->getAs<FunctionType>(); |
| 211 | if (!fn) return; |
| 212 | calleeType = CT_Function; |
| 213 | } else if (const BlockPointerType *ptr = type->getAs<BlockPointerType>()) { |
| 214 | fn = ptr->getPointeeType()->castAs<FunctionType>(); |
| 215 | calleeType = CT_Block; |
| 216 | } else { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 217 | return; |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 218 | } |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 219 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 220 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fn)) { |
| 221 | numFormalParams = proto->getNumArgs(); |
| 222 | } else { |
| 223 | numFormalParams = 0; |
| 224 | } |
| 225 | } else { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 226 | return; |
| 227 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 228 | |
| 229 | // "nullPos" is the number of formal parameters at the end which |
| 230 | // effectively count as part of the variadic arguments. This is |
| 231 | // useful if you would prefer to not have *any* formal parameters, |
| 232 | // but the language forces you to have at least one. |
| 233 | unsigned nullPos = attr->getNullPos(); |
| 234 | assert((nullPos == 0 || nullPos == 1) && "invalid null position on sentinel"); |
| 235 | numFormalParams = (nullPos > numFormalParams ? 0 : numFormalParams - nullPos); |
| 236 | |
| 237 | // The number of arguments which should follow the sentinel. |
| 238 | unsigned numArgsAfterSentinel = attr->getSentinel(); |
| 239 | |
| 240 | // If there aren't enough arguments for all the formal parameters, |
| 241 | // the sentinel, and the args after the sentinel, complain. |
| 242 | if (numArgs < numFormalParams + numArgsAfterSentinel + 1) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 243 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 244 | Diag(D->getLocation(), diag::note_sentinel_here) << calleeType; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 245 | return; |
| 246 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 247 | |
| 248 | // Otherwise, find the sentinel expression. |
| 249 | Expr *sentinelExpr = args[numArgs - numArgsAfterSentinel - 1]; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 250 | if (!sentinelExpr) return; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 251 | if (sentinelExpr->isValueDependent()) return; |
Argyrios Kyrtzidis | 8deabc1 | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 252 | if (Context.isSentinelNullExpr(sentinelExpr)) return; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 253 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 254 | // Pick a reasonable string to insert. Optimistically use 'nil' or |
| 255 | // 'NULL' if those are actually defined in the context. Only use |
| 256 | // 'nil' for ObjC methods, where it's much more likely that the |
| 257 | // variadic arguments form a list of object pointers. |
| 258 | SourceLocation MissingNilLoc |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 259 | = PP.getLocForEndOfToken(sentinelExpr->getLocEnd()); |
| 260 | std::string NullValue; |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 261 | if (calleeType == CT_Method && |
| 262 | PP.getIdentifierInfo("nil")->hasMacroDefinition()) |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 263 | NullValue = "nil"; |
| 264 | else if (PP.getIdentifierInfo("NULL")->hasMacroDefinition()) |
| 265 | NullValue = "NULL"; |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 266 | else |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 267 | NullValue = "(void*) 0"; |
Eli Friedman | 39834ba | 2011-09-27 23:46:37 +0000 | [diff] [blame] | 268 | |
| 269 | if (MissingNilLoc.isInvalid()) |
| 270 | Diag(Loc, diag::warn_missing_sentinel) << calleeType; |
| 271 | else |
| 272 | Diag(MissingNilLoc, diag::warn_missing_sentinel) |
| 273 | << calleeType |
| 274 | << FixItHint::CreateInsertion(MissingNilLoc, ", " + NullValue); |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 275 | Diag(D->getLocation(), diag::note_sentinel_here) << calleeType; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 278 | SourceRange Sema::getExprRange(Expr *E) const { |
| 279 | return E ? E->getSourceRange() : SourceRange(); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 282 | //===----------------------------------------------------------------------===// |
| 283 | // Standard Promotions and Conversions |
| 284 | //===----------------------------------------------------------------------===// |
| 285 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 286 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 287 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 288 | // Handle any placeholder expressions which made it here. |
| 289 | if (E->getType()->isPlaceholderType()) { |
| 290 | ExprResult result = CheckPlaceholderExpr(E); |
| 291 | if (result.isInvalid()) return ExprError(); |
| 292 | E = result.take(); |
| 293 | } |
| 294 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 295 | QualType Ty = E->getType(); |
| 296 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 297 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 298 | if (Ty->isFunctionType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 299 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 300 | CK_FunctionToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 301 | else if (Ty->isArrayType()) { |
| 302 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 303 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 304 | // type 'array of type' is converted to an expression that has type 'pointer |
| 305 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 306 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 307 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 308 | // |
| 309 | // C++ 4.2p1: |
| 310 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 311 | // T" can be converted to an rvalue of type "pointer to T". |
| 312 | // |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 313 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || E->isLValue()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 314 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 315 | CK_ArrayToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 316 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 317 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 320 | static void CheckForNullPointerDereference(Sema &S, Expr *E) { |
| 321 | // Check to see if we are dereferencing a null pointer. If so, |
| 322 | // and if not volatile-qualified, this is undefined behavior that the |
| 323 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 324 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 325 | // only handles the pattern "*null", which is a very syntactic check. |
| 326 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 327 | if (UO->getOpcode() == UO_Deref && |
| 328 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 329 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) && |
| 330 | !UO->getType().isVolatileQualified()) { |
| 331 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 332 | S.PDiag(diag::warn_indirection_through_null) |
| 333 | << UO->getSubExpr()->getSourceRange()); |
| 334 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 335 | S.PDiag(diag::note_indirection_through_null)); |
| 336 | } |
| 337 | } |
| 338 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 339 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 340 | // Handle any placeholder expressions which made it here. |
| 341 | if (E->getType()->isPlaceholderType()) { |
| 342 | ExprResult result = CheckPlaceholderExpr(E); |
| 343 | if (result.isInvalid()) return ExprError(); |
| 344 | E = result.take(); |
| 345 | } |
| 346 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 347 | // C++ [conv.lval]p1: |
| 348 | // A glvalue of a non-function, non-array type T can be |
| 349 | // converted to a prvalue. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 350 | if (!E->isGLValue()) return Owned(E); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 351 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 352 | QualType T = E->getType(); |
| 353 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 354 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 355 | // We can't do lvalue-to-rvalue on atomics yet. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 356 | if (T->isAtomicType()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 357 | return Owned(E); |
| 358 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 359 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 360 | // expressions of certain types in C++. |
| 361 | if (getLangOptions().CPlusPlus && |
| 362 | (E->getType() == Context.OverloadTy || |
| 363 | T->isDependentType() || |
| 364 | T->isRecordType())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 365 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 366 | |
| 367 | // The C standard is actually really unclear on this point, and |
| 368 | // DR106 tells us what the result should be but not why. It's |
| 369 | // generally best to say that void types just doesn't undergo |
| 370 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 371 | // 'void' type are never l-values, but qualified void can be. |
| 372 | if (T->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 373 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 374 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 375 | CheckForNullPointerDereference(*this, E); |
| 376 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 377 | // C++ [conv.lval]p1: |
| 378 | // [...] If T is a non-class type, the type of the prvalue is the |
| 379 | // cv-unqualified version of T. Otherwise, the type of the |
| 380 | // rvalue is T. |
| 381 | // |
| 382 | // C99 6.3.2.1p2: |
| 383 | // If the lvalue has qualified type, the value has the unqualified |
| 384 | // version of the type of the lvalue; otherwise, the value has the |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 385 | // type of the lvalue. |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 386 | if (T.hasQualifiers()) |
| 387 | T = T.getUnqualifiedType(); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 388 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 389 | UpdateMarkingForLValueToRValue(E); |
| 390 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 391 | ExprResult Res = Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 392 | E, 0, VK_RValue)); |
| 393 | |
| 394 | return Res; |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 395 | } |
| 396 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 397 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 398 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 399 | if (Res.isInvalid()) |
| 400 | return ExprError(); |
| 401 | Res = DefaultLvalueConversion(Res.take()); |
| 402 | if (Res.isInvalid()) |
| 403 | return ExprError(); |
| 404 | return move(Res); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 408 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 410 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 411 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 412 | /// In these instances, this routine should *not* be called. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 413 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 414 | // First, convert to an r-value. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 415 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 416 | if (Res.isInvalid()) |
| 417 | return Owned(E); |
| 418 | E = Res.take(); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 419 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 420 | QualType Ty = E->getType(); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 421 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 422 | |
| 423 | // Half FP is a bit different: it's a storage-only type, meaning that any |
| 424 | // "use" of it should be promoted to float. |
| 425 | if (Ty->isHalfType()) |
| 426 | return ImpCastExprToType(Res.take(), Context.FloatTy, CK_FloatingCast); |
| 427 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 428 | // Try to perform integral promotions if the object has a theoretically |
| 429 | // promotable type. |
| 430 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 431 | // C99 6.3.1.1p2: |
| 432 | // |
| 433 | // The following may be used in an expression wherever an int or |
| 434 | // unsigned int may be used: |
| 435 | // - an object or expression with an integer type whose integer |
| 436 | // conversion rank is less than or equal to the rank of int |
| 437 | // and unsigned int. |
| 438 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 439 | // |
| 440 | // If an int can represent all values of the original type, the |
| 441 | // value is converted to an int; otherwise, it is converted to an |
| 442 | // unsigned int. These are called the integer promotions. All |
| 443 | // other types are unchanged by the integer promotions. |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 444 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 445 | QualType PTy = Context.isPromotableBitField(E); |
| 446 | if (!PTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 447 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 448 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 449 | } |
| 450 | if (Ty->isPromotableIntegerType()) { |
| 451 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 452 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 453 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 454 | } |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 455 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 456 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 459 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | /// do not have a prototype. Arguments that have type float are promoted to |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 461 | /// double. All other argument types are converted by UsualUnaryConversions(). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 462 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 463 | QualType Ty = E->getType(); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 464 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 466 | ExprResult Res = UsualUnaryConversions(E); |
| 467 | if (Res.isInvalid()) |
| 468 | return Owned(E); |
| 469 | E = Res.take(); |
John McCall | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 470 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 471 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 472 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 473 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 474 | |
John McCall | 96a914a | 2011-08-27 22:06:17 +0000 | [diff] [blame] | 475 | // C++ performs lvalue-to-rvalue conversion as a default argument |
John McCall | 709bca8 | 2011-08-29 23:55:37 +0000 | [diff] [blame] | 476 | // promotion, even on class types, but note: |
| 477 | // C++11 [conv.lval]p2: |
| 478 | // When an lvalue-to-rvalue conversion occurs in an unevaluated |
| 479 | // operand or a subexpression thereof the value contained in the |
| 480 | // referenced object is not accessed. Otherwise, if the glvalue |
| 481 | // has a class type, the conversion copy-initializes a temporary |
| 482 | // of type T from the glvalue and the result of the conversion |
| 483 | // is a prvalue for the temporary. |
Eli Friedman | 55693fb | 2012-01-17 02:13:45 +0000 | [diff] [blame] | 484 | // FIXME: add some way to gate this entire thing for correctness in |
| 485 | // potentially potentially evaluated contexts. |
| 486 | if (getLangOptions().CPlusPlus && E->isGLValue() && |
| 487 | ExprEvalContexts.back().Context != Unevaluated) { |
| 488 | ExprResult Temp = PerformCopyInitialization( |
| 489 | InitializedEntity::InitializeTemporary(E->getType()), |
| 490 | E->getExprLoc(), |
| 491 | Owned(E)); |
| 492 | if (Temp.isInvalid()) |
| 493 | return ExprError(); |
| 494 | E = Temp.get(); |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 495 | } |
| 496 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 497 | return Owned(E); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 500 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 501 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 502 | /// interfaces passed by value. |
| 503 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 504 | FunctionDecl *FDecl) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 505 | if (const BuiltinType *PlaceholderTy = E->getType()->getAsPlaceholderType()) { |
| 506 | // Strip the unbridged-cast placeholder expression off, if applicable. |
| 507 | if (PlaceholderTy->getKind() == BuiltinType::ARCUnbridgedCast && |
| 508 | (CT == VariadicMethod || |
| 509 | (FDecl && FDecl->hasAttr<CFAuditedTransferAttr>()))) { |
| 510 | E = stripARCUnbridgedCast(E); |
| 511 | |
| 512 | // Otherwise, do normal placeholder checking. |
| 513 | } else { |
| 514 | ExprResult ExprRes = CheckPlaceholderExpr(E); |
| 515 | if (ExprRes.isInvalid()) |
| 516 | return ExprError(); |
| 517 | E = ExprRes.take(); |
| 518 | } |
| 519 | } |
Douglas Gregor | 8d5e18c | 2011-06-17 00:15:10 +0000 | [diff] [blame] | 520 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 521 | ExprResult ExprRes = DefaultArgumentPromotion(E); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 522 | if (ExprRes.isInvalid()) |
| 523 | return ExprError(); |
| 524 | E = ExprRes.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 526 | // Don't allow one to pass an Objective-C interface to a vararg. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 527 | if (E->getType()->isObjCObjectType() && |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 528 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 529 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 530 | << E->getType() << CT)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 531 | return ExprError(); |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 532 | |
Douglas Gregor | b8e778d | 2011-10-14 20:34:19 +0000 | [diff] [blame] | 533 | // Complain about passing non-POD types through varargs. However, don't |
| 534 | // perform this check for incomplete types, which we can get here when we're |
| 535 | // in an unevaluated context. |
| 536 | if (!E->getType()->isIncompleteType() && !E->getType().isPODType(Context)) { |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 537 | // C++0x [expr.call]p7: |
| 538 | // Passing a potentially-evaluated argument of class type (Clause 9) |
| 539 | // having a non-trivial copy constructor, a non-trivial move constructor, |
| 540 | // or a non-trivial destructor, with no corresponding parameter, |
| 541 | // is conditionally-supported with implementation-defined semantics. |
| 542 | bool TrivialEnough = false; |
| 543 | if (getLangOptions().CPlusPlus0x && !E->getType()->isDependentType()) { |
| 544 | if (CXXRecordDecl *Record = E->getType()->getAsCXXRecordDecl()) { |
| 545 | if (Record->hasTrivialCopyConstructor() && |
| 546 | Record->hasTrivialMoveConstructor() && |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 547 | Record->hasTrivialDestructor()) { |
| 548 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 549 | PDiag(diag::warn_cxx98_compat_pass_non_pod_arg_to_vararg) |
| 550 | << E->getType() << CT); |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 551 | TrivialEnough = true; |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 552 | } |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 555 | |
| 556 | if (!TrivialEnough && |
| 557 | getLangOptions().ObjCAutoRefCount && |
| 558 | E->getType()->isObjCLifetimeType()) |
| 559 | TrivialEnough = true; |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 560 | |
| 561 | if (TrivialEnough) { |
| 562 | // Nothing to diagnose. This is okay. |
| 563 | } else if (DiagRuntimeBehavior(E->getLocStart(), 0, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 564 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 565 | << getLangOptions().CPlusPlus0x << E->getType() |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 566 | << CT)) { |
| 567 | // Turn this into a trap. |
| 568 | CXXScopeSpec SS; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 569 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 570 | UnqualifiedId Name; |
| 571 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
| 572 | E->getLocStart()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 573 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, TemplateKWLoc, Name, |
| 574 | true, false); |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 575 | if (TrapFn.isInvalid()) |
| 576 | return ExprError(); |
| 577 | |
| 578 | ExprResult Call = ActOnCallExpr(TUScope, TrapFn.get(), E->getLocStart(), |
| 579 | MultiExprArg(), E->getLocEnd()); |
| 580 | if (Call.isInvalid()) |
| 581 | return ExprError(); |
| 582 | |
| 583 | ExprResult Comma = ActOnBinOp(TUScope, E->getLocStart(), tok::comma, |
| 584 | Call.get(), E); |
| 585 | if (Comma.isInvalid()) |
John McCall | 66c2030 | 2011-08-26 18:41:18 +0000 | [diff] [blame] | 586 | return ExprError(); |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 587 | E = Comma.get(); |
| 588 | } |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 589 | } |
| 590 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 591 | return Owned(E); |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 594 | /// \brief Converts an integer to complex float type. Helper function of |
| 595 | /// UsualArithmeticConversions() |
| 596 | /// |
| 597 | /// \return false if the integer expression is an integer type and is |
| 598 | /// successfully converted to the complex type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 599 | static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr, |
| 600 | ExprResult &ComplexExpr, |
| 601 | QualType IntTy, |
| 602 | QualType ComplexTy, |
| 603 | bool SkipCast) { |
| 604 | if (IntTy->isComplexType() || IntTy->isRealFloatingType()) return true; |
| 605 | if (SkipCast) return false; |
| 606 | if (IntTy->isIntegerType()) { |
| 607 | QualType fpTy = cast<ComplexType>(ComplexTy)->getElementType(); |
| 608 | IntExpr = S.ImpCastExprToType(IntExpr.take(), fpTy, CK_IntegralToFloating); |
| 609 | IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 610 | CK_FloatingRealToComplex); |
| 611 | } else { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 612 | assert(IntTy->isComplexIntegerType()); |
| 613 | IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 614 | CK_IntegralComplexToFloatingComplex); |
| 615 | } |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | /// \brief Takes two complex float types and converts them to the same type. |
| 620 | /// Helper function of UsualArithmeticConversions() |
| 621 | static QualType |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 622 | handleComplexFloatToComplexFloatConverstion(Sema &S, ExprResult &LHS, |
| 623 | ExprResult &RHS, QualType LHSType, |
| 624 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 625 | bool IsCompAssign) { |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 626 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 627 | |
| 628 | if (order < 0) { |
| 629 | // _Complex float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 630 | if (!IsCompAssign) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 631 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingComplexCast); |
| 632 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 633 | } |
| 634 | if (order > 0) |
| 635 | // _Complex float -> _Complex double |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 636 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingComplexCast); |
| 637 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /// \brief Converts otherExpr to complex float and promotes complexExpr if |
| 641 | /// necessary. Helper function of UsualArithmeticConversions() |
| 642 | static QualType handleOtherComplexFloatConversion(Sema &S, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 643 | ExprResult &ComplexExpr, |
| 644 | ExprResult &OtherExpr, |
| 645 | QualType ComplexTy, |
| 646 | QualType OtherTy, |
| 647 | bool ConvertComplexExpr, |
| 648 | bool ConvertOtherExpr) { |
| 649 | int order = S.Context.getFloatingTypeOrder(ComplexTy, OtherTy); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 650 | |
| 651 | // If just the complexExpr is complex, the otherExpr needs to be converted, |
| 652 | // and the complexExpr might need to be promoted. |
| 653 | if (order > 0) { // complexExpr is wider |
| 654 | // float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 655 | if (ConvertOtherExpr) { |
| 656 | QualType fp = cast<ComplexType>(ComplexTy)->getElementType(); |
| 657 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), fp, CK_FloatingCast); |
| 658 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 659 | CK_FloatingRealToComplex); |
| 660 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 661 | return ComplexTy; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | // otherTy is at least as wide. Find its corresponding complex type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 665 | QualType result = (order == 0 ? ComplexTy : |
| 666 | S.Context.getComplexType(OtherTy)); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 667 | |
| 668 | // double -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 669 | if (ConvertOtherExpr) |
| 670 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 671 | CK_FloatingRealToComplex); |
| 672 | |
| 673 | // _Complex float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 674 | if (ConvertComplexExpr && order < 0) |
| 675 | ComplexExpr = S.ImpCastExprToType(ComplexExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 676 | CK_FloatingComplexCast); |
| 677 | |
| 678 | return result; |
| 679 | } |
| 680 | |
| 681 | /// \brief Handle arithmetic conversion with complex types. Helper function of |
| 682 | /// UsualArithmeticConversions() |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 683 | static QualType handleComplexFloatConversion(Sema &S, ExprResult &LHS, |
| 684 | ExprResult &RHS, QualType LHSType, |
| 685 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 686 | bool IsCompAssign) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 687 | // if we have an integer operand, the result is the complex type. |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 688 | if (!handleIntegerToComplexFloatConversion(S, RHS, LHS, RHSType, LHSType, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 689 | /*skipCast*/false)) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 690 | return LHSType; |
| 691 | if (!handleIntegerToComplexFloatConversion(S, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 692 | /*skipCast*/IsCompAssign)) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 693 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 694 | |
| 695 | // This handles complex/complex, complex/float, or float/complex. |
| 696 | // When both operands are complex, the shorter operand is converted to the |
| 697 | // type of the longer, and that is the type of the result. This corresponds |
| 698 | // to what is done when combining two real floating-point operands. |
| 699 | // The fun begins when size promotion occur across type domains. |
| 700 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 701 | // floating-point type, the less precise type is converted, within it's |
| 702 | // real or complex domain, to the precision of the other type. For example, |
| 703 | // when combining a "long double" with a "double _Complex", the |
| 704 | // "double _Complex" is promoted to "long double _Complex". |
| 705 | |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 706 | bool LHSComplexFloat = LHSType->isComplexType(); |
| 707 | bool RHSComplexFloat = RHSType->isComplexType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 708 | |
| 709 | // If both are complex, just cast to the more precise type. |
| 710 | if (LHSComplexFloat && RHSComplexFloat) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 711 | return handleComplexFloatToComplexFloatConverstion(S, LHS, RHS, |
| 712 | LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 713 | IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 714 | |
| 715 | // If only one operand is complex, promote it if necessary and convert the |
| 716 | // other operand to complex. |
| 717 | if (LHSComplexFloat) |
| 718 | return handleOtherComplexFloatConversion( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 719 | S, LHS, RHS, LHSType, RHSType, /*convertComplexExpr*/!IsCompAssign, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 720 | /*convertOtherExpr*/ true); |
| 721 | |
| 722 | assert(RHSComplexFloat); |
| 723 | return handleOtherComplexFloatConversion( |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 724 | S, RHS, LHS, RHSType, LHSType, /*convertComplexExpr*/true, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 725 | /*convertOtherExpr*/ !IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /// \brief Hande arithmetic conversion from integer to float. Helper function |
| 729 | /// of UsualArithmeticConversions() |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 730 | static QualType handleIntToFloatConversion(Sema &S, ExprResult &FloatExpr, |
| 731 | ExprResult &IntExpr, |
| 732 | QualType FloatTy, QualType IntTy, |
| 733 | bool ConvertFloat, bool ConvertInt) { |
| 734 | if (IntTy->isIntegerType()) { |
| 735 | if (ConvertInt) |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 736 | // Convert intExpr to the lhs floating point type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 737 | IntExpr = S.ImpCastExprToType(IntExpr.take(), FloatTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 738 | CK_IntegralToFloating); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 739 | return FloatTy; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | // Convert both sides to the appropriate complex float. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 743 | assert(IntTy->isComplexIntegerType()); |
| 744 | QualType result = S.Context.getComplexType(FloatTy); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 745 | |
| 746 | // _Complex int -> _Complex float |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 747 | if (ConvertInt) |
| 748 | IntExpr = S.ImpCastExprToType(IntExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 749 | CK_IntegralComplexToFloatingComplex); |
| 750 | |
| 751 | // float -> _Complex float |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 752 | if (ConvertFloat) |
| 753 | FloatExpr = S.ImpCastExprToType(FloatExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 754 | CK_FloatingRealToComplex); |
| 755 | |
| 756 | return result; |
| 757 | } |
| 758 | |
| 759 | /// \brief Handle arithmethic conversion with floating point types. Helper |
| 760 | /// function of UsualArithmeticConversions() |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 761 | static QualType handleFloatConversion(Sema &S, ExprResult &LHS, |
| 762 | ExprResult &RHS, QualType LHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 763 | QualType RHSType, bool IsCompAssign) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 764 | bool LHSFloat = LHSType->isRealFloatingType(); |
| 765 | bool RHSFloat = RHSType->isRealFloatingType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 766 | |
| 767 | // If we have two real floating types, convert the smaller operand |
| 768 | // to the bigger result. |
| 769 | if (LHSFloat && RHSFloat) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 770 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 771 | if (order > 0) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 772 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingCast); |
| 773 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | assert(order < 0 && "illegal float comparison"); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 777 | if (!IsCompAssign) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 778 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingCast); |
| 779 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | if (LHSFloat) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 783 | return handleIntToFloatConversion(S, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 784 | /*convertFloat=*/!IsCompAssign, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 785 | /*convertInt=*/ true); |
| 786 | assert(RHSFloat); |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 787 | return handleIntToFloatConversion(S, RHS, LHS, RHSType, LHSType, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 788 | /*convertInt=*/ true, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 789 | /*convertFloat=*/!IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /// \brief Handle conversions with GCC complex int extension. Helper function |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 793 | /// of UsualArithmeticConversions() |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 794 | // FIXME: if the operands are (int, _Complex long), we currently |
| 795 | // don't promote the complex. Also, signedness? |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 796 | static QualType handleComplexIntConversion(Sema &S, ExprResult &LHS, |
| 797 | ExprResult &RHS, QualType LHSType, |
| 798 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 799 | bool IsCompAssign) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 800 | const ComplexType *LHSComplexInt = LHSType->getAsComplexIntegerType(); |
| 801 | const ComplexType *RHSComplexInt = RHSType->getAsComplexIntegerType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 802 | |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 803 | if (LHSComplexInt && RHSComplexInt) { |
| 804 | int order = S.Context.getIntegerTypeOrder(LHSComplexInt->getElementType(), |
| 805 | RHSComplexInt->getElementType()); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 806 | assert(order && "inequal types with equal element ordering"); |
| 807 | if (order > 0) { |
| 808 | // _Complex int -> _Complex long |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 809 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralComplexCast); |
| 810 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 813 | if (!IsCompAssign) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 814 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralComplexCast); |
| 815 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 818 | if (LHSComplexInt) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 819 | // int -> _Complex int |
Eli Friedman | ddadaa4 | 2011-11-12 03:56:23 +0000 | [diff] [blame] | 820 | // FIXME: This needs to take integer ranks into account |
| 821 | RHS = S.ImpCastExprToType(RHS.take(), LHSComplexInt->getElementType(), |
| 822 | CK_IntegralCast); |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 823 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralRealToComplex); |
| 824 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 827 | assert(RHSComplexInt); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 828 | // int -> _Complex int |
Eli Friedman | ddadaa4 | 2011-11-12 03:56:23 +0000 | [diff] [blame] | 829 | // FIXME: This needs to take integer ranks into account |
| 830 | if (!IsCompAssign) { |
| 831 | LHS = S.ImpCastExprToType(LHS.take(), RHSComplexInt->getElementType(), |
| 832 | CK_IntegralCast); |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 833 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralRealToComplex); |
Eli Friedman | ddadaa4 | 2011-11-12 03:56:23 +0000 | [diff] [blame] | 834 | } |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 835 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | /// \brief Handle integer arithmetic conversions. Helper function of |
| 839 | /// UsualArithmeticConversions() |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 840 | static QualType handleIntegerConversion(Sema &S, ExprResult &LHS, |
| 841 | ExprResult &RHS, QualType LHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 842 | QualType RHSType, bool IsCompAssign) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 843 | // The rules for this case are in C99 6.3.1.8 |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 844 | int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); |
| 845 | bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); |
| 846 | bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); |
| 847 | if (LHSSigned == RHSSigned) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 848 | // Same signedness; use the higher-ranked type |
| 849 | if (order >= 0) { |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 850 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 851 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 852 | } else if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 853 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 854 | return RHSType; |
| 855 | } else if (order != (LHSSigned ? 1 : -1)) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 856 | // The unsigned type has greater than or equal rank to the |
| 857 | // signed type, so use the unsigned type |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 858 | if (RHSSigned) { |
| 859 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 860 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 861 | } else if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 862 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 863 | return RHSType; |
| 864 | } else if (S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 865 | // The two types are different widths; if we are here, that |
| 866 | // means the signed type is larger than the unsigned type, so |
| 867 | // use the signed type. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 868 | if (LHSSigned) { |
| 869 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 870 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 871 | } else if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 872 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 873 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 874 | } else { |
| 875 | // The signed type is higher-ranked than the unsigned type, |
| 876 | // but isn't actually any bigger (like unsigned int and long |
| 877 | // on most 32-bit systems). Use the unsigned type corresponding |
| 878 | // to the signed type. |
| 879 | QualType result = |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 880 | S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType : RHSType); |
| 881 | RHS = S.ImpCastExprToType(RHS.take(), result, CK_IntegralCast); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 882 | if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 883 | LHS = S.ImpCastExprToType(LHS.take(), result, CK_IntegralCast); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 884 | return result; |
| 885 | } |
| 886 | } |
| 887 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 888 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 889 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 891 | /// responsible for emitting appropriate error diagnostics. |
| 892 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 893 | /// GCC. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 894 | QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 895 | bool IsCompAssign) { |
| 896 | if (!IsCompAssign) { |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 897 | LHS = UsualUnaryConversions(LHS.take()); |
| 898 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 899 | return QualType(); |
| 900 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 901 | |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 902 | RHS = UsualUnaryConversions(RHS.take()); |
| 903 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 904 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 905 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 906 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 907 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 908 | QualType LHSType = |
| 909 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 910 | QualType RHSType = |
| 911 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 912 | |
| 913 | // If both types are identical, no conversion is needed. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 914 | if (LHSType == RHSType) |
| 915 | return LHSType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 916 | |
| 917 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 918 | // The caller can deal with this (e.g. pointer + int). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 919 | if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType()) |
| 920 | return LHSType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 921 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 922 | // Apply unary and bitfield promotions to the LHS's type. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 923 | QualType LHSUnpromotedType = LHSType; |
| 924 | if (LHSType->isPromotableIntegerType()) |
| 925 | LHSType = Context.getPromotedIntegerType(LHSType); |
| 926 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(LHS.get()); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 927 | if (!LHSBitfieldPromoteTy.isNull()) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 928 | LHSType = LHSBitfieldPromoteTy; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 929 | if (LHSType != LHSUnpromotedType && !IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 930 | LHS = ImpCastExprToType(LHS.take(), LHSType, CK_IntegralCast); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 931 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 932 | // If both types are identical, no conversion is needed. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 933 | if (LHSType == RHSType) |
| 934 | return LHSType; |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 935 | |
| 936 | // At this point, we have two different arithmetic types. |
| 937 | |
| 938 | // Handle complex types first (C99 6.3.1.8p1). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 939 | if (LHSType->isComplexType() || RHSType->isComplexType()) |
| 940 | return handleComplexFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 941 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 942 | |
| 943 | // Now handle "real" floating types (i.e. float, double, long double). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 944 | if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) |
| 945 | return handleFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 946 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 947 | |
| 948 | // Handle GCC complex int extension. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 949 | if (LHSType->isComplexIntegerType() || RHSType->isComplexIntegerType()) |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 950 | return handleComplexIntConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 951 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 952 | |
| 953 | // Finally, we have two differing integer types. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 954 | return handleIntegerConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 955 | IsCompAssign); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 958 | //===----------------------------------------------------------------------===// |
| 959 | // Semantic Analysis for various Expression Types |
| 960 | //===----------------------------------------------------------------------===// |
| 961 | |
| 962 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 963 | ExprResult |
| 964 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 965 | SourceLocation DefaultLoc, |
| 966 | SourceLocation RParenLoc, |
| 967 | Expr *ControllingExpr, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 968 | MultiTypeArg ArgTypes, |
| 969 | MultiExprArg ArgExprs) { |
| 970 | unsigned NumAssocs = ArgTypes.size(); |
| 971 | assert(NumAssocs == ArgExprs.size()); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 972 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 973 | ParsedType *ParsedTypes = ArgTypes.release(); |
| 974 | Expr **Exprs = ArgExprs.release(); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 975 | |
| 976 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 977 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 978 | if (ParsedTypes[i]) |
| 979 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 980 | else |
| 981 | Types[i] = 0; |
| 982 | } |
| 983 | |
| 984 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 985 | ControllingExpr, Types, Exprs, |
| 986 | NumAssocs); |
Benjamin Kramer | 5bf47f7 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 987 | delete [] Types; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 988 | return ER; |
| 989 | } |
| 990 | |
| 991 | ExprResult |
| 992 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 993 | SourceLocation DefaultLoc, |
| 994 | SourceLocation RParenLoc, |
| 995 | Expr *ControllingExpr, |
| 996 | TypeSourceInfo **Types, |
| 997 | Expr **Exprs, |
| 998 | unsigned NumAssocs) { |
| 999 | bool TypeErrorFound = false, |
| 1000 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 1001 | ContainsUnexpandedParameterPack |
| 1002 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 1003 | |
| 1004 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 1005 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 1006 | ContainsUnexpandedParameterPack = true; |
| 1007 | |
| 1008 | if (Types[i]) { |
| 1009 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 1010 | ContainsUnexpandedParameterPack = true; |
| 1011 | |
| 1012 | if (Types[i]->getType()->isDependentType()) { |
| 1013 | IsResultDependent = true; |
| 1014 | } else { |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1015 | // C11 6.5.1.1p2 "The type name in a generic association shall specify a |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1016 | // complete object type other than a variably modified type." |
| 1017 | unsigned D = 0; |
| 1018 | if (Types[i]->getType()->isIncompleteType()) |
| 1019 | D = diag::err_assoc_type_incomplete; |
| 1020 | else if (!Types[i]->getType()->isObjectType()) |
| 1021 | D = diag::err_assoc_type_nonobject; |
| 1022 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 1023 | D = diag::err_assoc_type_variably_modified; |
| 1024 | |
| 1025 | if (D != 0) { |
| 1026 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 1027 | << Types[i]->getTypeLoc().getSourceRange() |
| 1028 | << Types[i]->getType(); |
| 1029 | TypeErrorFound = true; |
| 1030 | } |
| 1031 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1032 | // C11 6.5.1.1p2 "No two generic associations in the same generic |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1033 | // selection shall specify compatible types." |
| 1034 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 1035 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 1036 | Context.typesAreCompatible(Types[i]->getType(), |
| 1037 | Types[j]->getType())) { |
| 1038 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 1039 | diag::err_assoc_compatible_types) |
| 1040 | << Types[j]->getTypeLoc().getSourceRange() |
| 1041 | << Types[j]->getType() |
| 1042 | << Types[i]->getType(); |
| 1043 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 1044 | diag::note_compat_assoc) |
| 1045 | << Types[i]->getTypeLoc().getSourceRange() |
| 1046 | << Types[i]->getType(); |
| 1047 | TypeErrorFound = true; |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | if (TypeErrorFound) |
| 1053 | return ExprError(); |
| 1054 | |
| 1055 | // If we determined that the generic selection is result-dependent, don't |
| 1056 | // try to compute the result expression. |
| 1057 | if (IsResultDependent) |
| 1058 | return Owned(new (Context) GenericSelectionExpr( |
| 1059 | Context, KeyLoc, ControllingExpr, |
| 1060 | Types, Exprs, NumAssocs, DefaultLoc, |
| 1061 | RParenLoc, ContainsUnexpandedParameterPack)); |
| 1062 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1063 | SmallVector<unsigned, 1> CompatIndices; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1064 | unsigned DefaultIndex = -1U; |
| 1065 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 1066 | if (!Types[i]) |
| 1067 | DefaultIndex = i; |
| 1068 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 1069 | Types[i]->getType())) |
| 1070 | CompatIndices.push_back(i); |
| 1071 | } |
| 1072 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1073 | // C11 6.5.1.1p2 "The controlling expression of a generic selection shall have |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1074 | // type compatible with at most one of the types named in its generic |
| 1075 | // association list." |
| 1076 | if (CompatIndices.size() > 1) { |
| 1077 | // We strip parens here because the controlling expression is typically |
| 1078 | // parenthesized in macro definitions. |
| 1079 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1080 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 1081 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 1082 | << (unsigned) CompatIndices.size(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1083 | for (SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1084 | E = CompatIndices.end(); I != E; ++I) { |
| 1085 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 1086 | diag::note_compat_assoc) |
| 1087 | << Types[*I]->getTypeLoc().getSourceRange() |
| 1088 | << Types[*I]->getType(); |
| 1089 | } |
| 1090 | return ExprError(); |
| 1091 | } |
| 1092 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1093 | // C11 6.5.1.1p2 "If a generic selection has no default generic association, |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1094 | // its controlling expression shall have type compatible with exactly one of |
| 1095 | // the types named in its generic association list." |
| 1096 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 1097 | // We strip parens here because the controlling expression is typically |
| 1098 | // parenthesized in macro definitions. |
| 1099 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1100 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 1101 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 1102 | return ExprError(); |
| 1103 | } |
| 1104 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1105 | // C11 6.5.1.1p3 "If a generic selection has a generic association with a |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1106 | // type name that is compatible with the type of the controlling expression, |
| 1107 | // then the result expression of the generic selection is the expression |
| 1108 | // in that generic association. Otherwise, the result expression of the |
| 1109 | // generic selection is the expression in the default generic association." |
| 1110 | unsigned ResultIndex = |
| 1111 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 1112 | |
| 1113 | return Owned(new (Context) GenericSelectionExpr( |
| 1114 | Context, KeyLoc, ControllingExpr, |
| 1115 | Types, Exprs, NumAssocs, DefaultLoc, |
| 1116 | RParenLoc, ContainsUnexpandedParameterPack, |
| 1117 | ResultIndex)); |
| 1118 | } |
| 1119 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1120 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1121 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 1122 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 1123 | /// multiple tokens. However, the common case is that StringToks points to one |
| 1124 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1125 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1126 | ExprResult |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1127 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1128 | assert(NumStringToks && "Must have at least one string!"); |
| 1129 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1130 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1131 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1132 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1133 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1134 | SmallVector<SourceLocation, 4> StringTokLocs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1135 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 1136 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1137 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1138 | QualType StrTy = Context.CharTy; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1139 | if (Literal.isWide()) |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1140 | StrTy = Context.getWCharType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1141 | else if (Literal.isUTF16()) |
| 1142 | StrTy = Context.Char16Ty; |
| 1143 | else if (Literal.isUTF32()) |
| 1144 | StrTy = Context.Char32Ty; |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 1145 | else if (Literal.isPascal()) |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1146 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1147 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1148 | StringLiteral::StringKind Kind = StringLiteral::Ascii; |
| 1149 | if (Literal.isWide()) |
| 1150 | Kind = StringLiteral::Wide; |
| 1151 | else if (Literal.isUTF8()) |
| 1152 | Kind = StringLiteral::UTF8; |
| 1153 | else if (Literal.isUTF16()) |
| 1154 | Kind = StringLiteral::UTF16; |
| 1155 | else if (Literal.isUTF32()) |
| 1156 | Kind = StringLiteral::UTF32; |
| 1157 | |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1158 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
Chris Lattner | 7dc480f | 2010-06-15 18:05:34 +0000 | [diff] [blame] | 1159 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1160 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1161 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1162 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 1163 | // the nul terminator character as well as the string length for pascal |
| 1164 | // strings. |
| 1165 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1166 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1167 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1169 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1170 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1171 | Kind, Literal.Pascal, StrTy, |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1172 | &StringTokLocs[0], |
| 1173 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1176 | ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1177 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1178 | SourceLocation Loc, |
| 1179 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1180 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1181 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1184 | /// BuildDeclRefExpr - Build an expression that references a |
| 1185 | /// declaration that does not require a closure capture. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1186 | ExprResult |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1187 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1188 | const DeclarationNameInfo &NameInfo, |
| 1189 | const CXXScopeSpec *SS) { |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 1190 | if (getLangOptions().CUDA) |
| 1191 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 1192 | if (const FunctionDecl *Callee = dyn_cast<FunctionDecl>(D)) { |
| 1193 | CUDAFunctionTarget CallerTarget = IdentifyCUDATarget(Caller), |
| 1194 | CalleeTarget = IdentifyCUDATarget(Callee); |
| 1195 | if (CheckCUDATarget(CallerTarget, CalleeTarget)) { |
| 1196 | Diag(NameInfo.getLoc(), diag::err_ref_bad_target) |
| 1197 | << CalleeTarget << D->getIdentifier() << CallerTarget; |
| 1198 | Diag(D->getLocation(), diag::note_previous_decl) |
| 1199 | << D->getIdentifier(); |
| 1200 | return ExprError(); |
| 1201 | } |
| 1202 | } |
| 1203 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1204 | DeclRefExpr *E = DeclRefExpr::Create(Context, |
| 1205 | SS ? SS->getWithLocInContext(Context) |
| 1206 | : NestedNameSpecifierLoc(), |
| 1207 | SourceLocation(), |
| 1208 | D, NameInfo, Ty, VK); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1210 | MarkDeclRefReferenced(E); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1211 | |
| 1212 | // Just in case we're building an illegal pointer-to-member. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1213 | FieldDecl *FD = dyn_cast<FieldDecl>(D); |
| 1214 | if (FD && FD->isBitField()) |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1215 | E->setObjectKind(OK_BitField); |
| 1216 | |
| 1217 | return Owned(E); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1220 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1221 | /// possibly a list of template arguments. |
| 1222 | /// |
| 1223 | /// If this produces template arguments, it is permitted to call |
| 1224 | /// DecomposeTemplateName. |
| 1225 | /// |
| 1226 | /// This actually loses a lot of source location information for |
| 1227 | /// non-standard name kinds; we should consider preserving that in |
| 1228 | /// some way. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1229 | void |
| 1230 | Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, |
| 1231 | TemplateArgumentListInfo &Buffer, |
| 1232 | DeclarationNameInfo &NameInfo, |
| 1233 | const TemplateArgumentListInfo *&TemplateArgs) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1234 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1235 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1236 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1237 | |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1238 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1239 | Id.TemplateId->getTemplateArgs(), |
| 1240 | Id.TemplateId->NumArgs); |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1241 | translateTemplateArguments(TemplateArgsPtr, Buffer); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1242 | TemplateArgsPtr.release(); |
| 1243 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1244 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1245 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1246 | NameInfo = Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1247 | TemplateArgs = &Buffer; |
| 1248 | } else { |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1249 | NameInfo = GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1250 | TemplateArgs = 0; |
| 1251 | } |
| 1252 | } |
| 1253 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1254 | /// Diagnose an empty lookup. |
| 1255 | /// |
| 1256 | /// \return false if new lookup candidates were found |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1257 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
Kaelyn Uhrain | 4798f8d | 2012-01-18 05:58:54 +0000 | [diff] [blame] | 1258 | CorrectionCandidateCallback &CCC, |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1259 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1260 | llvm::ArrayRef<Expr *> Args) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1261 | DeclarationName Name = R.getLookupName(); |
| 1262 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1263 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1264 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1265 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1266 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1267 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1268 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1269 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1270 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1271 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1272 | // If the original lookup was an unqualified lookup, fake an |
| 1273 | // unqualified lookup. This is useful when (for example) the |
| 1274 | // original lookup would not have found something because it was a |
| 1275 | // dependent name. |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 1276 | DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
| 1277 | while (DC) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1278 | if (isa<CXXRecordDecl>(DC)) { |
| 1279 | LookupQualifiedName(R, DC); |
| 1280 | |
| 1281 | if (!R.empty()) { |
| 1282 | // Don't give errors about ambiguities in this lookup. |
| 1283 | R.suppressDiagnostics(); |
| 1284 | |
Francois Pichet | e6226ae | 2011-11-17 03:44:24 +0000 | [diff] [blame] | 1285 | // During a default argument instantiation the CurContext points |
| 1286 | // to a CXXMethodDecl; but we can't apply a this-> fixit inside a |
| 1287 | // function parameter list, hence add an explicit check. |
| 1288 | bool isDefaultArgument = !ActiveTemplateInstantiations.empty() && |
| 1289 | ActiveTemplateInstantiations.back().Kind == |
| 1290 | ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1291 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1292 | bool isInstance = CurMethod && |
| 1293 | CurMethod->isInstance() && |
Francois Pichet | e6226ae | 2011-11-17 03:44:24 +0000 | [diff] [blame] | 1294 | DC == CurMethod->getParent() && !isDefaultArgument; |
| 1295 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1296 | |
| 1297 | // Give a code modification hint to insert 'this->'. |
| 1298 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1299 | // Actually quite difficult! |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1300 | if (isInstance) { |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1301 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1302 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1303 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1304 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1305 | if (DepMethod) { |
Francois Pichet | e614d6c | 2011-11-15 23:33:34 +0000 | [diff] [blame] | 1306 | if (getLangOptions().MicrosoftMode) |
Francois Pichet | 0f74d1e | 2011-09-07 00:14:57 +0000 | [diff] [blame] | 1307 | diagnostic = diag::warn_found_via_dependent_bases_lookup; |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1308 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1309 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1310 | QualType DepThisType = DepMethod->getThisType(Context); |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 1311 | CheckCXXThisCapture(R.getNameLoc()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1312 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1313 | R.getNameLoc(), DepThisType, false); |
| 1314 | TemplateArgumentListInfo TList; |
| 1315 | if (ULE->hasExplicitTemplateArgs()) |
| 1316 | ULE->copyTemplateArgumentsInto(TList); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1317 | |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1318 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1319 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1320 | CXXDependentScopeMemberExpr *DepExpr = |
| 1321 | CXXDependentScopeMemberExpr::Create( |
| 1322 | Context, DepThis, DepThisType, true, SourceLocation(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1323 | SS.getWithLocInContext(Context), |
| 1324 | ULE->getTemplateKeywordLoc(), 0, |
Francois Pichet | f740012 | 2011-09-04 23:00:48 +0000 | [diff] [blame] | 1325 | R.getLookupNameInfo(), |
| 1326 | ULE->hasExplicitTemplateArgs() ? &TList : 0); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1327 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1328 | } else { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1329 | // FIXME: we should be able to handle this case too. It is correct |
| 1330 | // to add this-> here. This is a workaround for PR7947. |
| 1331 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1332 | } |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1333 | } else { |
Francois Pichet | e614d6c | 2011-11-15 23:33:34 +0000 | [diff] [blame] | 1334 | if (getLangOptions().MicrosoftMode) |
| 1335 | diagnostic = diag::warn_found_via_dependent_bases_lookup; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1336 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1337 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1338 | |
| 1339 | // Do we really want to note all of these? |
| 1340 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1341 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1342 | |
Francois Pichet | e6226ae | 2011-11-17 03:44:24 +0000 | [diff] [blame] | 1343 | // Return true if we are inside a default argument instantiation |
| 1344 | // and the found name refers to an instance member function, otherwise |
| 1345 | // the function calling DiagnoseEmptyLookup will try to create an |
| 1346 | // implicit member call and this is wrong for default argument. |
| 1347 | if (isDefaultArgument && ((*R.begin())->isCXXInstanceMember())) { |
| 1348 | Diag(R.getNameLoc(), diag::err_member_call_without_object); |
| 1349 | return true; |
| 1350 | } |
| 1351 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1352 | // Tell the callee to try to recover. |
| 1353 | return false; |
| 1354 | } |
Douglas Gregor | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1355 | |
| 1356 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1357 | } |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 1358 | |
| 1359 | // In Microsoft mode, if we are performing lookup from within a friend |
| 1360 | // function definition declared at class scope then we must set |
| 1361 | // DC to the lexical parent to be able to search into the parent |
| 1362 | // class. |
Lang Hames | 36ef702 | 2011-11-29 22:37:13 +0000 | [diff] [blame] | 1363 | if (getLangOptions().MicrosoftMode && isa<FunctionDecl>(DC) && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 1364 | cast<FunctionDecl>(DC)->getFriendObjectKind() && |
| 1365 | DC->getLexicalParent()->isRecord()) |
| 1366 | DC = DC->getLexicalParent(); |
| 1367 | else |
| 1368 | DC = DC->getParent(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1371 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1372 | TypoCorrection Corrected; |
| 1373 | if (S && (Corrected = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1374 | S, &SS, CCC))) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1375 | std::string CorrectedStr(Corrected.getAsString(getLangOptions())); |
| 1376 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
| 1377 | R.setLookupName(Corrected.getCorrection()); |
| 1378 | |
Hans Wennborg | 701d1e7 | 2011-07-12 08:45:31 +0000 | [diff] [blame] | 1379 | if (NamedDecl *ND = Corrected.getCorrectionDecl()) { |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1380 | if (Corrected.isOverloaded()) { |
| 1381 | OverloadCandidateSet OCS(R.getNameLoc()); |
| 1382 | OverloadCandidateSet::iterator Best; |
| 1383 | for (TypoCorrection::decl_iterator CD = Corrected.begin(), |
| 1384 | CDEnd = Corrected.end(); |
| 1385 | CD != CDEnd; ++CD) { |
Kaelyn Uhrain | adc7a73 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1386 | if (FunctionTemplateDecl *FTD = |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1387 | dyn_cast<FunctionTemplateDecl>(*CD)) |
| 1388 | AddTemplateOverloadCandidate( |
| 1389 | FTD, DeclAccessPair::make(FTD, AS_none), ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1390 | Args, OCS); |
Kaelyn Uhrain | adc7a73 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1391 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*CD)) |
| 1392 | if (!ExplicitTemplateArgs || ExplicitTemplateArgs->size() == 0) |
| 1393 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1394 | Args, OCS); |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1395 | } |
| 1396 | switch (OCS.BestViableFunction(*this, R.getNameLoc(), Best)) { |
| 1397 | case OR_Success: |
| 1398 | ND = Best->Function; |
| 1399 | break; |
| 1400 | default: |
Kaelyn Uhrain | 844d572 | 2011-08-04 23:30:54 +0000 | [diff] [blame] | 1401 | break; |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1402 | } |
| 1403 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1404 | R.addDecl(ND); |
| 1405 | if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1406 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1407 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr |
| 1408 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1409 | else |
| 1410 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1411 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1412 | << SS.getRange() |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1413 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
| 1414 | if (ND) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1415 | Diag(ND->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1416 | << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1417 | |
| 1418 | // Tell the callee to try to recover. |
| 1419 | return false; |
| 1420 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1421 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1422 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1423 | // FIXME: If we ended up with a typo for a type name or |
| 1424 | // Objective-C class name, we're in trouble because the parser |
| 1425 | // is in the wrong place to recover. Suggest the typo |
| 1426 | // correction, but don't make it a fix-it since we're not going |
| 1427 | // to recover well anyway. |
| 1428 | if (SS.isEmpty()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1429 | Diag(R.getNameLoc(), diagnostic_suggest) |
| 1430 | << Name << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1431 | else |
| 1432 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1433 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1434 | << SS.getRange(); |
| 1435 | |
| 1436 | // Don't try to recover; it won't work. |
| 1437 | return true; |
| 1438 | } |
| 1439 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1440 | // FIXME: We found a keyword. Suggest it, but don't provide a fix-it |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1441 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1442 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1443 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1444 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1445 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1446 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1447 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1448 | return true; |
| 1449 | } |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1450 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1451 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1452 | |
| 1453 | // Emit a special diagnostic for failed member lookups. |
| 1454 | // FIXME: computing the declaration context might fail here (?) |
| 1455 | if (!SS.isEmpty()) { |
| 1456 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1457 | << Name << computeDeclContext(SS, false) |
| 1458 | << SS.getRange(); |
| 1459 | return true; |
| 1460 | } |
| 1461 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1462 | // Give up, we can't recover. |
| 1463 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1464 | return true; |
| 1465 | } |
| 1466 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1467 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1468 | CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1469 | SourceLocation TemplateKWLoc, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1470 | UnqualifiedId &Id, |
| 1471 | bool HasTrailingLParen, |
Kaelyn Uhrain | cd78e61 | 2012-01-25 20:49:08 +0000 | [diff] [blame] | 1472 | bool IsAddressOfOperand, |
| 1473 | CorrectionCandidateCallback *CCC) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1474 | assert(!(IsAddressOfOperand && HasTrailingLParen) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1475 | "cannot be direct & operand and have a trailing lparen"); |
| 1476 | |
| 1477 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1478 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1479 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1480 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1481 | |
| 1482 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1483 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1484 | const TemplateArgumentListInfo *TemplateArgs; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1485 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1486 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1487 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1488 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1489 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1490 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1491 | // C++ [temp.dep.expr]p3: |
| 1492 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1493 | // -- an identifier that was declared with a dependent type, |
| 1494 | // (note: handled after lookup) |
| 1495 | // -- a template-id that is dependent, |
| 1496 | // (note: handled in BuildTemplateIdExpr) |
| 1497 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1498 | // -- a nested-name-specifier that contains a class-name that |
| 1499 | // names a dependent type. |
| 1500 | // Determine whether this is a member of an unknown specialization; |
| 1501 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1502 | bool DependentID = false; |
| 1503 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1504 | Name.getCXXNameType()->isDependentType()) { |
| 1505 | DependentID = true; |
| 1506 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1507 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1508 | if (RequireCompleteDeclContext(SS, DC)) |
| 1509 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1510 | } else { |
| 1511 | DependentID = true; |
| 1512 | } |
| 1513 | } |
| 1514 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1515 | if (DependentID) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1516 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1517 | IsAddressOfOperand, TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1518 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1519 | // Perform the required lookup. |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1520 | LookupResult R(*this, NameInfo, |
| 1521 | (Id.getKind() == UnqualifiedId::IK_ImplicitSelfParam) |
| 1522 | ? LookupObjCImplicitSelfParam : LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1523 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1524 | // Lookup the template name again to correctly establish the context in |
| 1525 | // which it was found. This is really unfortunate as we already did the |
| 1526 | // lookup to determine that it was a template name in the first place. If |
| 1527 | // this becomes a performance hit, we can work harder to preserve those |
| 1528 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1529 | bool MemberOfUnknownSpecialization; |
| 1530 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1531 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1532 | |
| 1533 | if (MemberOfUnknownSpecialization || |
| 1534 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1535 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1536 | IsAddressOfOperand, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1537 | } else { |
Benjamin Kramer | b7ff74a | 2012-01-20 14:57:34 +0000 | [diff] [blame] | 1538 | bool IvarLookupFollowUp = II && !SS.isSet() && getCurMethodDecl(); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1539 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1541 | // If the result might be in a dependent base class, this is a dependent |
| 1542 | // id-expression. |
| 1543 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1544 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1545 | IsAddressOfOperand, TemplateArgs); |
| 1546 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1547 | // If this reference is in an Objective-C method, then we need to do |
| 1548 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1549 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1550 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1551 | if (E.isInvalid()) |
| 1552 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1554 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1555 | return Owned(Ex); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1556 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1557 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1558 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1559 | if (R.isAmbiguous()) |
| 1560 | return ExprError(); |
| 1561 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1562 | // Determine whether this name might be a candidate for |
| 1563 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1564 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1565 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1566 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1567 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1568 | // in C90, extension in C99, forbidden in C++). |
| 1569 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1570 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1571 | if (D) R.addDecl(D); |
| 1572 | } |
| 1573 | |
| 1574 | // If this name wasn't predeclared and if this is not a function |
| 1575 | // call, diagnose the problem. |
| 1576 | if (R.empty()) { |
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1577 | |
| 1578 | // In Microsoft mode, if we are inside a template class member function |
| 1579 | // and we can't resolve an identifier then assume the identifier is type |
| 1580 | // dependent. The goal is to postpone name lookup to instantiation time |
| 1581 | // to be able to search into type dependent base classes. |
| 1582 | if (getLangOptions().MicrosoftMode && CurContext->isDependentContext() && |
| 1583 | isa<CXXMethodDecl>(CurContext)) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1584 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1585 | IsAddressOfOperand, TemplateArgs); |
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1586 | |
Kaelyn Uhrain | 4798f8d | 2012-01-18 05:58:54 +0000 | [diff] [blame] | 1587 | CorrectionCandidateCallback DefaultValidator; |
Kaelyn Uhrain | cd78e61 | 2012-01-25 20:49:08 +0000 | [diff] [blame] | 1588 | if (DiagnoseEmptyLookup(S, SS, R, CCC ? *CCC : DefaultValidator)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1589 | return ExprError(); |
| 1590 | |
| 1591 | assert(!R.empty() && |
| 1592 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1593 | |
| 1594 | // If we found an Objective-C instance variable, let |
| 1595 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1596 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1597 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1598 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1599 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Fariborz Jahanian | bc2b91a | 2011-09-23 23:11:38 +0000 | [diff] [blame] | 1600 | // In a hopelessly buggy code, Objective-C instance variable |
| 1601 | // lookup fails and no expression will be built to reference it. |
| 1602 | if (!E.isInvalid() && !E.get()) |
| 1603 | return ExprError(); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1604 | return move(E); |
| 1605 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1606 | } |
| 1607 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1609 | // This is guaranteed from this point on. |
| 1610 | assert(!R.empty() || ADL); |
| 1611 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1612 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1613 | // C++ [class.mfct.non-static]p3: |
| 1614 | // When an id-expression that is not part of a class member access |
| 1615 | // syntax and not used to form a pointer to member is used in the |
| 1616 | // body of a non-static member function of class X, if name lookup |
| 1617 | // resolves the name in the id-expression to a non-static non-type |
| 1618 | // member of some class C, the id-expression is transformed into a |
| 1619 | // class member access expression using (*this) as the |
| 1620 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1621 | // |
| 1622 | // But we don't actually need to do this for '&' operands if R |
| 1623 | // resolved to a function or overloaded function set, because the |
| 1624 | // expression is ill-formed if it actually works out to be a |
| 1625 | // non-static member function: |
| 1626 | // |
| 1627 | // C++ [expr.ref]p4: |
| 1628 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1629 | // [t]he expression can be used only as the left-hand operand of a |
| 1630 | // member function call. |
| 1631 | // |
| 1632 | // There are other safeguards against such uses, but it's important |
| 1633 | // to get this right here so that we don't end up making a |
| 1634 | // spuriously dependent expression if we're inside a dependent |
| 1635 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1636 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1637 | bool MightBeImplicitMember; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1638 | if (!IsAddressOfOperand) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1639 | MightBeImplicitMember = true; |
| 1640 | else if (!SS.isEmpty()) |
| 1641 | MightBeImplicitMember = false; |
| 1642 | else if (R.isOverloadedResult()) |
| 1643 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1644 | else if (R.isUnresolvableResult()) |
| 1645 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1646 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1647 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1648 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1649 | |
| 1650 | if (MightBeImplicitMember) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1651 | return BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 1652 | R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 1655 | if (TemplateArgs || TemplateKWLoc.isValid()) |
| 1656 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, ADL, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1657 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1658 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1659 | } |
| 1660 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1661 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1662 | /// declaration name, generally during template instantiation. |
| 1663 | /// There's a large number of things which don't need to be done along |
| 1664 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1665 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1666 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1667 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1668 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1669 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 1670 | return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), |
| 1671 | NameInfo, /*TemplateArgs=*/0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1672 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1673 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1674 | return ExprError(); |
| 1675 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1676 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1677 | LookupQualifiedName(R, DC); |
| 1678 | |
| 1679 | if (R.isAmbiguous()) |
| 1680 | return ExprError(); |
| 1681 | |
| 1682 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1683 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1684 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1685 | return ExprError(); |
| 1686 | } |
| 1687 | |
| 1688 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1689 | } |
| 1690 | |
| 1691 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1692 | /// detected that we're currently inside an ObjC method. Perform some |
| 1693 | /// additional lookup. |
| 1694 | /// |
| 1695 | /// Ideally, most of this would be done by lookup, but there's |
| 1696 | /// actually quite a lot of extra work involved. |
| 1697 | /// |
| 1698 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1699 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1700 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1701 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1702 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1703 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1704 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1705 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1706 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1707 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1708 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1709 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1710 | |
| 1711 | // If we're in a class method, we don't normally want to look for |
| 1712 | // ivars. But if we don't find anything else, and there's an |
| 1713 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1714 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1715 | |
| 1716 | bool LookForIvars; |
| 1717 | if (Lookup.empty()) |
| 1718 | LookForIvars = true; |
| 1719 | else if (IsClassMethod) |
| 1720 | LookForIvars = false; |
| 1721 | else |
| 1722 | LookForIvars = (Lookup.isSingleResult() && |
| 1723 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1724 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1725 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1726 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1727 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | 7c81c2a | 2011-10-19 02:25:16 +0000 | [diff] [blame] | 1728 | ObjCIvarDecl *IV = 0; |
| 1729 | if (IFace && (IV = IFace->lookupInstanceVariable(II, ClassDeclared))) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1730 | // Diagnose using an ivar in a class method. |
| 1731 | if (IsClassMethod) |
| 1732 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1733 | << IV->getDeclName()); |
| 1734 | |
| 1735 | // If we're referencing an invalid decl, just return this as a silent |
| 1736 | // error node. The error diagnostic was already emitted on the decl. |
| 1737 | if (IV->isInvalidDecl()) |
| 1738 | return ExprError(); |
| 1739 | |
| 1740 | // Check if referencing a field with __attribute__((deprecated)). |
| 1741 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1742 | return ExprError(); |
| 1743 | |
| 1744 | // Diagnose the use of an ivar outside of the declaring class. |
| 1745 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1746 | !declaresSameEntity(ClassDeclared, IFace)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1747 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1748 | |
| 1749 | // FIXME: This should use a new expr for a direct reference, don't |
| 1750 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1751 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1752 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1753 | SelfName.setIdentifier(&II, SourceLocation()); |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1754 | SelfName.setKind(UnqualifiedId::IK_ImplicitSelfParam); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1755 | CXXScopeSpec SelfScopeSpec; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1756 | SourceLocation TemplateKWLoc; |
| 1757 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, TemplateKWLoc, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1758 | SelfName, false, false); |
| 1759 | if (SelfExpr.isInvalid()) |
| 1760 | return ExprError(); |
| 1761 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1762 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 1763 | if (SelfExpr.isInvalid()) |
| 1764 | return ExprError(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1765 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1766 | MarkAnyDeclReferenced(Loc, IV); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1767 | return Owned(new (Context) |
| 1768 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1769 | SelfExpr.take(), true, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1770 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1771 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1772 | // We should warn if a local variable hides an ivar. |
Fariborz Jahanian | 90f7b62 | 2011-11-08 22:51:27 +0000 | [diff] [blame] | 1773 | if (ObjCInterfaceDecl *IFace = CurMethod->getClassInterface()) { |
| 1774 | ObjCInterfaceDecl *ClassDeclared; |
| 1775 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1776 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1777 | declaresSameEntity(IFace, ClassDeclared)) |
Fariborz Jahanian | 90f7b62 | 2011-11-08 22:51:27 +0000 | [diff] [blame] | 1778 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1779 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1780 | } |
Fariborz Jahanian | b5ea9db | 2011-12-20 22:21:08 +0000 | [diff] [blame] | 1781 | } else if (Lookup.isSingleResult() && |
| 1782 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()) { |
| 1783 | // If accessing a stand-alone ivar in a class method, this is an error. |
| 1784 | if (const ObjCIvarDecl *IV = dyn_cast<ObjCIvarDecl>(Lookup.getFoundDecl())) |
| 1785 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1786 | << IV->getDeclName()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1789 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1790 | // FIXME. Consolidate this with similar code in LookupName. |
| 1791 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1792 | if (!(getLangOptions().CPlusPlus && |
| 1793 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1794 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1795 | S, Lookup.isForRedeclaration(), |
| 1796 | Lookup.getNameLoc()); |
| 1797 | if (D) Lookup.addDecl(D); |
| 1798 | } |
| 1799 | } |
| 1800 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1801 | // Sentinel value saying that we didn't do anything special. |
| 1802 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1803 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1804 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1805 | /// \brief Cast a base object to a member's actual type. |
| 1806 | /// |
| 1807 | /// Logically this happens in three phases: |
| 1808 | /// |
| 1809 | /// * First we cast from the base type to the naming class. |
| 1810 | /// The naming class is the class into which we were looking |
| 1811 | /// when we found the member; it's the qualifier type if a |
| 1812 | /// qualifier was provided, and otherwise it's the base type. |
| 1813 | /// |
| 1814 | /// * Next we cast from the naming class to the declaring class. |
| 1815 | /// If the member we found was brought into a class's scope by |
| 1816 | /// a using declaration, this is that class; otherwise it's |
| 1817 | /// the class declaring the member. |
| 1818 | /// |
| 1819 | /// * Finally we cast from the declaring class to the "true" |
| 1820 | /// declaring class of the member. This conversion does not |
| 1821 | /// obey access control. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1822 | ExprResult |
| 1823 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1824 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1825 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1826 | NamedDecl *Member) { |
| 1827 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 1828 | if (!RD) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1829 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1830 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1831 | QualType DestRecordType; |
| 1832 | QualType DestType; |
| 1833 | QualType FromRecordType; |
| 1834 | QualType FromType = From->getType(); |
| 1835 | bool PointerConversions = false; |
| 1836 | if (isa<FieldDecl>(Member)) { |
| 1837 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1838 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1839 | if (FromType->getAs<PointerType>()) { |
| 1840 | DestType = Context.getPointerType(DestRecordType); |
| 1841 | FromRecordType = FromType->getPointeeType(); |
| 1842 | PointerConversions = true; |
| 1843 | } else { |
| 1844 | DestType = DestRecordType; |
| 1845 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1846 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1847 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 1848 | if (Method->isStatic()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1849 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1850 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1851 | DestType = Method->getThisType(Context); |
| 1852 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1853 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1854 | if (FromType->getAs<PointerType>()) { |
| 1855 | FromRecordType = FromType->getPointeeType(); |
| 1856 | PointerConversions = true; |
| 1857 | } else { |
| 1858 | FromRecordType = FromType; |
| 1859 | DestType = DestRecordType; |
| 1860 | } |
| 1861 | } else { |
| 1862 | // No conversion necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1863 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1864 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1865 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1866 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1867 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1868 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1869 | // If the unqualified types are the same, no conversion is necessary. |
| 1870 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1871 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1872 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1873 | SourceRange FromRange = From->getSourceRange(); |
| 1874 | SourceLocation FromLoc = FromRange.getBegin(); |
| 1875 | |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 1876 | ExprValueKind VK = From->getValueKind(); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1877 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1878 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1879 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1880 | // class name. |
| 1881 | // |
| 1882 | // If the member was a qualified name and the qualified referred to a |
| 1883 | // specific base subobject type, we'll cast to that intermediate type |
| 1884 | // first and then to the object in which the member is declared. That allows |
| 1885 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 1886 | // |
| 1887 | // class Base { public: int x; }; |
| 1888 | // class Derived1 : public Base { }; |
| 1889 | // class Derived2 : public Base { }; |
| 1890 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 1891 | // |
| 1892 | // void VeryDerived::f() { |
| 1893 | // x = 17; // error: ambiguous base subobjects |
| 1894 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 1895 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1896 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1897 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 1898 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 1899 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 1900 | |
| 1901 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 1902 | |
| 1903 | // In C++98, the qualifier type doesn't actually have to be a base |
| 1904 | // type of the object type, in which case we just ignore it. |
| 1905 | // Otherwise build the appropriate casts. |
| 1906 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1907 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1908 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1909 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1910 | return ExprError(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1911 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1912 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1913 | QType = Context.getPointerType(QType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1914 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 1915 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1916 | |
| 1917 | FromType = QType; |
| 1918 | FromRecordType = QRecordType; |
| 1919 | |
| 1920 | // If the qualifier type was the same as the destination type, |
| 1921 | // we're done. |
| 1922 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1923 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1924 | } |
| 1925 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1926 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1927 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1928 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1929 | // If we actually found the member through a using declaration, cast |
| 1930 | // down to the using declaration's type. |
| 1931 | // |
| 1932 | // Pointer equality is fine here because only one declaration of a |
| 1933 | // class ever has member declarations. |
| 1934 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 1935 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 1936 | QualType URecordType = Context.getTypeDeclType( |
| 1937 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 1938 | |
| 1939 | // We only need to do this if the naming-class to declaring-class |
| 1940 | // conversion is non-trivial. |
| 1941 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 1942 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1943 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1944 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1945 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1946 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1947 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1948 | QualType UType = URecordType; |
| 1949 | if (PointerConversions) |
| 1950 | UType = Context.getPointerType(UType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1951 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 1952 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1953 | FromType = UType; |
| 1954 | FromRecordType = URecordType; |
| 1955 | } |
| 1956 | |
| 1957 | // We don't do access control for the conversion from the |
| 1958 | // declaring class to the true declaring class. |
| 1959 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1960 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1961 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1962 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1963 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 1964 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1965 | IgnoreAccess)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1966 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1967 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1968 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 1969 | VK, &BasePath); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1970 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1971 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1972 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1973 | const LookupResult &R, |
| 1974 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1975 | // Only when used directly as the postfix-expression of a call. |
| 1976 | if (!HasTrailingLParen) |
| 1977 | return false; |
| 1978 | |
| 1979 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1980 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1981 | return false; |
| 1982 | |
| 1983 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1984 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1985 | return false; |
| 1986 | |
| 1987 | // Turn off ADL when we find certain kinds of declarations during |
| 1988 | // normal lookup: |
| 1989 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 1990 | NamedDecl *D = *I; |
| 1991 | |
| 1992 | // C++0x [basic.lookup.argdep]p3: |
| 1993 | // -- a declaration of a class member |
| 1994 | // Since using decls preserve this property, we check this on the |
| 1995 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1996 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1997 | return false; |
| 1998 | |
| 1999 | // C++0x [basic.lookup.argdep]p3: |
| 2000 | // -- a block-scope function declaration that is not a |
| 2001 | // using-declaration |
| 2002 | // NOTE: we also trigger this for function templates (in fact, we |
| 2003 | // don't check the decl type at all, since all other decl types |
| 2004 | // turn off ADL anyway). |
| 2005 | if (isa<UsingShadowDecl>(D)) |
| 2006 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2007 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2008 | return false; |
| 2009 | |
| 2010 | // C++0x [basic.lookup.argdep]p3: |
| 2011 | // -- a declaration that is neither a function or a function |
| 2012 | // template |
| 2013 | // And also for builtin functions. |
| 2014 | if (isa<FunctionDecl>(D)) { |
| 2015 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2016 | |
| 2017 | // But also builtin functions. |
| 2018 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2019 | return false; |
| 2020 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2021 | return false; |
| 2022 | } |
| 2023 | |
| 2024 | return true; |
| 2025 | } |
| 2026 | |
| 2027 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2028 | /// Diagnoses obvious problems with the use of the given declaration |
| 2029 | /// as an expression. This is only actually called for lookups that |
| 2030 | /// were not overloaded, and it doesn't promise that the declaration |
| 2031 | /// will in fact be used. |
| 2032 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2033 | if (isa<TypedefNameDecl>(D)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2034 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2035 | return true; |
| 2036 | } |
| 2037 | |
| 2038 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2039 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2040 | return true; |
| 2041 | } |
| 2042 | |
| 2043 | if (isa<NamespaceDecl>(D)) { |
| 2044 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2045 | return true; |
| 2046 | } |
| 2047 | |
| 2048 | return false; |
| 2049 | } |
| 2050 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2051 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2052 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2053 | LookupResult &R, |
| 2054 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2055 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2056 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2057 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2058 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2059 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2060 | |
| 2061 | // We only need to check the declaration if there's exactly one |
| 2062 | // result, because in the overloaded case the results can only be |
| 2063 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2064 | if (R.isSingleResult() && |
| 2065 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2066 | return ExprError(); |
| 2067 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2068 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2069 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2070 | // we've picked a target. |
| 2071 | R.suppressDiagnostics(); |
| 2072 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2073 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2074 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2075 | SS.getWithLocInContext(Context), |
| 2076 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2077 | NeedsADL, R.isOverloadedResult(), |
| 2078 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2079 | |
| 2080 | return Owned(ULE); |
| 2081 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2082 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 2083 | static bool shouldBuildBlockDeclRef(ValueDecl *D, Sema &S) { |
| 2084 | // Check for a variable with local storage not from the current scope; |
| 2085 | // we need to create BlockDeclRefExprs for these. |
| 2086 | // FIXME: BlockDeclRefExpr shouldn't exist! |
| 2087 | VarDecl *var = dyn_cast<VarDecl>(D); |
| 2088 | if (!var) |
| 2089 | return false; |
| 2090 | if (var->getDeclContext() == S.CurContext) |
| 2091 | return false; |
| 2092 | if (!var->hasLocalStorage()) |
| 2093 | return false; |
| 2094 | return S.getCurBlock() != 0; |
| 2095 | } |
| 2096 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 2097 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *VD, |
| 2098 | const DeclarationNameInfo &NameInfo) { |
| 2099 | VarDecl *var = cast<VarDecl>(VD); |
| 2100 | QualType exprType = var->getType().getNonReferenceType(); |
| 2101 | |
| 2102 | bool HasBlockAttr = var->hasAttr<BlocksAttr>(); |
| 2103 | bool ConstAdded = false; |
| 2104 | if (!HasBlockAttr) { |
| 2105 | ConstAdded = !exprType.isConstQualified(); |
| 2106 | exprType.addConst(); |
| 2107 | } |
| 2108 | |
| 2109 | BlockDeclRefExpr *BDRE = |
| 2110 | new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 2111 | NameInfo.getLoc(), HasBlockAttr, |
| 2112 | ConstAdded); |
| 2113 | |
| 2114 | S.MarkBlockDeclRefReferenced(BDRE); |
| 2115 | |
| 2116 | return S.Owned(BDRE); |
| 2117 | } |
| 2118 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2119 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2120 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2121 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2122 | const DeclarationNameInfo &NameInfo, |
| 2123 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2124 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2125 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2126 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2127 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2128 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2129 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2130 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2131 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2132 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2133 | // Specifically diagnose references to class templates that are missing |
| 2134 | // a template argument list. |
| 2135 | Diag(Loc, diag::err_template_decl_ref) |
| 2136 | << Template << SS.getRange(); |
| 2137 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2138 | return ExprError(); |
| 2139 | } |
| 2140 | |
| 2141 | // Make sure that we're referring to a value. |
| 2142 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2143 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2144 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2145 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2146 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2147 | return ExprError(); |
| 2148 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2149 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2150 | // Check whether this declaration can be used. Note that we suppress |
| 2151 | // this check when we're going to perform argument-dependent lookup |
| 2152 | // on this function name, because this might not be the function |
| 2153 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2154 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2155 | return ExprError(); |
| 2156 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2157 | // Only create DeclRefExpr's for valid Decl's. |
| 2158 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2159 | return ExprError(); |
| 2160 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2161 | // Handle members of anonymous structs and unions. If we got here, |
| 2162 | // and the reference is to a class member indirect field, then this |
| 2163 | // must be the subject of a pointer-to-member expression. |
| 2164 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2165 | if (!indirectField->isCXXClassMember()) |
| 2166 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2167 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2168 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 2169 | { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2170 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2171 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2172 | |
| 2173 | switch (D->getKind()) { |
| 2174 | // Ignore all the non-ValueDecl kinds. |
| 2175 | #define ABSTRACT_DECL(kind) |
| 2176 | #define VALUE(type, base) |
| 2177 | #define DECL(type, base) \ |
| 2178 | case Decl::type: |
| 2179 | #include "clang/AST/DeclNodes.inc" |
| 2180 | llvm_unreachable("invalid value decl kind"); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2181 | |
| 2182 | // These shouldn't make it here. |
| 2183 | case Decl::ObjCAtDefsField: |
| 2184 | case Decl::ObjCIvar: |
| 2185 | llvm_unreachable("forming non-member reference to ivar?"); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2186 | |
| 2187 | // Enum constants are always r-values and never references. |
| 2188 | // Unresolved using declarations are dependent. |
| 2189 | case Decl::EnumConstant: |
| 2190 | case Decl::UnresolvedUsingValue: |
| 2191 | valueKind = VK_RValue; |
| 2192 | break; |
| 2193 | |
| 2194 | // Fields and indirect fields that got here must be for |
| 2195 | // pointer-to-member expressions; we just call them l-values for |
| 2196 | // internal consistency, because this subexpression doesn't really |
| 2197 | // exist in the high-level semantics. |
| 2198 | case Decl::Field: |
| 2199 | case Decl::IndirectField: |
| 2200 | assert(getLangOptions().CPlusPlus && |
| 2201 | "building reference to field in C?"); |
| 2202 | |
| 2203 | // These can't have reference type in well-formed programs, but |
| 2204 | // for internal consistency we do this anyway. |
| 2205 | type = type.getNonReferenceType(); |
| 2206 | valueKind = VK_LValue; |
| 2207 | break; |
| 2208 | |
| 2209 | // Non-type template parameters are either l-values or r-values |
| 2210 | // depending on the type. |
| 2211 | case Decl::NonTypeTemplateParm: { |
| 2212 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2213 | type = reftype->getPointeeType(); |
| 2214 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2215 | break; |
| 2216 | } |
| 2217 | |
| 2218 | // For non-references, we need to strip qualifiers just in case |
| 2219 | // the template parameter was declared as 'const int' or whatever. |
| 2220 | valueKind = VK_RValue; |
| 2221 | type = type.getUnqualifiedType(); |
| 2222 | break; |
| 2223 | } |
| 2224 | |
| 2225 | case Decl::Var: |
| 2226 | // In C, "extern void blah;" is valid and is an r-value. |
| 2227 | if (!getLangOptions().CPlusPlus && |
| 2228 | !type.hasQualifiers() && |
| 2229 | type->isVoidType()) { |
| 2230 | valueKind = VK_RValue; |
| 2231 | break; |
| 2232 | } |
| 2233 | // fallthrough |
| 2234 | |
| 2235 | case Decl::ImplicitParam: |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2236 | case Decl::ParmVar: { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2237 | // These are always l-values. |
| 2238 | valueKind = VK_LValue; |
| 2239 | type = type.getNonReferenceType(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 2240 | |
| 2241 | if (shouldBuildBlockDeclRef(VD, *this)) |
| 2242 | return BuildBlockDeclRefExpr(*this, VD, NameInfo); |
| 2243 | |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2244 | // FIXME: Does the addition of const really only apply in |
| 2245 | // potentially-evaluated contexts? Since the variable isn't actually |
| 2246 | // captured in an unevaluated context, it seems that the answer is no. |
| 2247 | if (ExprEvalContexts.back().Context != Sema::Unevaluated) { |
| 2248 | QualType CapturedType = getCapturedDeclRefType(cast<VarDecl>(VD), Loc); |
| 2249 | if (!CapturedType.isNull()) |
| 2250 | type = CapturedType; |
| 2251 | } |
| 2252 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2253 | break; |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2256 | case Decl::Function: { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2257 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2258 | |
| 2259 | // If we're referring to a function with an __unknown_anytype |
| 2260 | // result type, make the entire expression __unknown_anytype. |
| 2261 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2262 | type = Context.UnknownAnyTy; |
| 2263 | valueKind = VK_RValue; |
| 2264 | break; |
| 2265 | } |
| 2266 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2267 | // Functions are l-values in C++. |
| 2268 | if (getLangOptions().CPlusPlus) { |
| 2269 | valueKind = VK_LValue; |
| 2270 | break; |
| 2271 | } |
| 2272 | |
| 2273 | // C99 DR 316 says that, if a function type comes from a |
| 2274 | // function definition (without a prototype), that type is only |
| 2275 | // used for checking compatibility. Therefore, when referencing |
| 2276 | // the function, we pretend that we don't have the full function |
| 2277 | // type. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2278 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2279 | isa<FunctionProtoType>(fty)) |
| 2280 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2281 | fty->getExtInfo()); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2282 | |
| 2283 | // Functions are r-values in C. |
| 2284 | valueKind = VK_RValue; |
| 2285 | break; |
| 2286 | } |
| 2287 | |
| 2288 | case Decl::CXXMethod: |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2289 | // If we're referring to a method with an __unknown_anytype |
| 2290 | // result type, make the entire expression __unknown_anytype. |
| 2291 | // This should only be possible with a type written directly. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 2292 | if (const FunctionProtoType *proto |
| 2293 | = dyn_cast<FunctionProtoType>(VD->getType())) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2294 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2295 | type = Context.UnknownAnyTy; |
| 2296 | valueKind = VK_RValue; |
| 2297 | break; |
| 2298 | } |
| 2299 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2300 | // C++ methods are l-values if static, r-values if non-static. |
| 2301 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2302 | valueKind = VK_LValue; |
| 2303 | break; |
| 2304 | } |
| 2305 | // fallthrough |
| 2306 | |
| 2307 | case Decl::CXXConversion: |
| 2308 | case Decl::CXXDestructor: |
| 2309 | case Decl::CXXConstructor: |
| 2310 | valueKind = VK_RValue; |
| 2311 | break; |
| 2312 | } |
| 2313 | |
| 2314 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2315 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2318 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2319 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2320 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2321 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2322 | default: llvm_unreachable("Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2323 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2324 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2325 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2326 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2327 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2328 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2329 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2330 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2331 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2332 | if (!currentDecl && getCurBlock()) |
| 2333 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2334 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2335 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2336 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2337 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2338 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2339 | QualType ResTy; |
| 2340 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2341 | ResTy = Context.DependentTy; |
| 2342 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2343 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2344 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2345 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2346 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2347 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2348 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2349 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2352 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2353 | SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2354 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2355 | StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2356 | if (Invalid) |
| 2357 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2358 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2359 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2360 | PP, Tok.getKind()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2361 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2362 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2363 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2364 | QualType Ty; |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2365 | if (Literal.isWide()) |
| 2366 | Ty = Context.WCharTy; // L'x' -> wchar_t in C and C++. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2367 | else if (Literal.isUTF16()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2368 | Ty = Context.Char16Ty; // u'x' -> char16_t in C11 and C++11. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2369 | else if (Literal.isUTF32()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2370 | Ty = Context.Char32Ty; // U'x' -> char32_t in C11 and C++11. |
| 2371 | else if (!getLangOptions().CPlusPlus || Literal.isMultiChar()) |
| 2372 | Ty = Context.IntTy; // 'x' -> int in C, 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2373 | else |
| 2374 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2375 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2376 | CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; |
| 2377 | if (Literal.isWide()) |
| 2378 | Kind = CharacterLiteral::Wide; |
| 2379 | else if (Literal.isUTF16()) |
| 2380 | Kind = CharacterLiteral::UTF16; |
| 2381 | else if (Literal.isUTF32()) |
| 2382 | Kind = CharacterLiteral::UTF32; |
| 2383 | |
| 2384 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, |
| 2385 | Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2388 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2389 | // Fast path for a single digit (which is quite common). A single digit |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2390 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2391 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2392 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2393 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2394 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2395 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2396 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2397 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2398 | SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2399 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2400 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2401 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2402 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2403 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2404 | bool Invalid = false; |
| 2405 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2406 | if (Invalid) |
| 2407 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2408 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2409 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2410 | Tok.getLocation(), PP); |
| 2411 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2412 | return ExprError(); |
| 2413 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2414 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2415 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2416 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2417 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2418 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2419 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2420 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2421 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2422 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2423 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2424 | |
| 2425 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2426 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2427 | using llvm::APFloat; |
| 2428 | APFloat Val(Format); |
| 2429 | |
| 2430 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2431 | |
| 2432 | // Overflow is always an error, but underflow is only an error if |
| 2433 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2434 | if ((result & APFloat::opOverflow) || |
| 2435 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2436 | unsigned diagnostic; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2437 | SmallString<20> buffer; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2438 | if (result & APFloat::opOverflow) { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2439 | diagnostic = diag::warn_float_overflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2440 | APFloat::getLargest(Format).toString(buffer); |
| 2441 | } else { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2442 | diagnostic = diag::warn_float_underflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2443 | APFloat::getSmallest(Format).toString(buffer); |
| 2444 | } |
| 2445 | |
| 2446 | Diag(Tok.getLocation(), diagnostic) |
| 2447 | << Ty |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2448 | << StringRef(buffer.data(), buffer.size()); |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2452 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2453 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2454 | if (Ty == Context.DoubleTy) { |
| 2455 | if (getLangOptions().SinglePrecisionConstants) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2456 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2457 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2458 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2459 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2460 | } |
| 2461 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2462 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2463 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2464 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2465 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2466 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2467 | // long long is a C99 feature. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2468 | if (!getLangOptions().C99 && Literal.isLongLong) |
| 2469 | Diag(Tok.getLocation(), |
| 2470 | getLangOptions().CPlusPlus0x ? |
| 2471 | diag::warn_cxx98_compat_longlong : diag::ext_longlong); |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2472 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2473 | // Get the value in the widest-possible width. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2474 | llvm::APInt ResultVal(Context.getTargetInfo().getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2475 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2476 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2477 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2478 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2479 | Ty = Context.UnsignedLongLongTy; |
| 2480 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2481 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2482 | } else { |
| 2483 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2484 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2486 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2487 | // be an unsigned int. |
| 2488 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2489 | |
| 2490 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2491 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2492 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2493 | // Are int/unsigned possibilities? |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2494 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2495 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2496 | // Does it fit in a unsigned int? |
| 2497 | if (ResultVal.isIntN(IntSize)) { |
| 2498 | // Does it fit in a signed int? |
| 2499 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2500 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2501 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2502 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2503 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2504 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2505 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2506 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2507 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2508 | if (Ty.isNull() && !Literal.isLongLong) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2509 | unsigned LongSize = Context.getTargetInfo().getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2510 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2511 | // Does it fit in a unsigned long? |
| 2512 | if (ResultVal.isIntN(LongSize)) { |
| 2513 | // Does it fit in a signed long? |
| 2514 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2515 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2516 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2517 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2518 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2519 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2522 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2523 | if (Ty.isNull()) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2524 | unsigned LongLongSize = Context.getTargetInfo().getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2525 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2526 | // Does it fit in a unsigned long long? |
| 2527 | if (ResultVal.isIntN(LongLongSize)) { |
| 2528 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2529 | // To be compatible with MSVC, hex integer literals ending with the |
| 2530 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2531 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2532 | (getLangOptions().MicrosoftExt && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2533 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2534 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2535 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2536 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2537 | } |
| 2538 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2539 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2540 | // If we still couldn't decide a type, we probably have something that |
| 2541 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2542 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2543 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2544 | Ty = Context.UnsignedLongLongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2545 | Width = Context.getTargetInfo().getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2546 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2547 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2548 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2549 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2550 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2551 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2552 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2553 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2554 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2555 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2556 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2557 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2558 | |
| 2559 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2562 | ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2563 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2564 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2567 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 2568 | SourceLocation Loc, |
| 2569 | SourceRange ArgRange) { |
| 2570 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 2571 | // scalar or vector data type argument..." |
| 2572 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 2573 | // type (C99 6.2.5p18) or void. |
| 2574 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 2575 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 2576 | << T << ArgRange; |
| 2577 | return true; |
| 2578 | } |
| 2579 | |
| 2580 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 2581 | "Scalar types should always be complete"); |
| 2582 | return false; |
| 2583 | } |
| 2584 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2585 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 2586 | SourceLocation Loc, |
| 2587 | SourceRange ArgRange, |
| 2588 | UnaryExprOrTypeTrait TraitKind) { |
| 2589 | // C99 6.5.3.4p1: |
| 2590 | if (T->isFunctionType()) { |
| 2591 | // alignof(function) is allowed as an extension. |
| 2592 | if (TraitKind == UETT_SizeOf) |
| 2593 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 2594 | return false; |
| 2595 | } |
| 2596 | |
| 2597 | // Allow sizeof(void)/alignof(void) as an extension. |
| 2598 | if (T->isVoidType()) { |
| 2599 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 2600 | return false; |
| 2601 | } |
| 2602 | |
| 2603 | return true; |
| 2604 | } |
| 2605 | |
| 2606 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 2607 | SourceLocation Loc, |
| 2608 | SourceRange ArgRange, |
| 2609 | UnaryExprOrTypeTrait TraitKind) { |
| 2610 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
| 2611 | if (S.LangOpts.ObjCNonFragileABI && T->isObjCObjectType()) { |
| 2612 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 2613 | << T << (TraitKind == UETT_SizeOf) |
| 2614 | << ArgRange; |
| 2615 | return true; |
| 2616 | } |
| 2617 | |
| 2618 | return false; |
| 2619 | } |
| 2620 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2621 | /// \brief Check the constrains on expression operands to unary type expression |
| 2622 | /// and type traits. |
| 2623 | /// |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2624 | /// Completes any types necessary and validates the constraints on the operand |
| 2625 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 2626 | /// the expression as it completes the type for that expression through template |
| 2627 | /// instantiation, etc. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2628 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2629 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2630 | QualType ExprTy = E->getType(); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2631 | |
| 2632 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2633 | // the result is the size of the referenced type." |
| 2634 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2635 | // result shall be the alignment of the referenced type." |
| 2636 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2637 | ExprTy = Ref->getPointeeType(); |
| 2638 | |
| 2639 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2640 | return CheckVecStepTraitOperandType(*this, ExprTy, E->getExprLoc(), |
| 2641 | E->getSourceRange()); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2642 | |
| 2643 | // Whitelist some types as extensions |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2644 | if (!CheckExtensionTraitOperandType(*this, ExprTy, E->getExprLoc(), |
| 2645 | E->getSourceRange(), ExprKind)) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2646 | return false; |
| 2647 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2648 | if (RequireCompleteExprType(E, |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2649 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2650 | << ExprKind << E->getSourceRange(), |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2651 | std::make_pair(SourceLocation(), PDiag(0)))) |
| 2652 | return true; |
| 2653 | |
| 2654 | // Completeing the expression's type may have changed it. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2655 | ExprTy = E->getType(); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2656 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2657 | ExprTy = Ref->getPointeeType(); |
| 2658 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2659 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, E->getExprLoc(), |
| 2660 | E->getSourceRange(), ExprKind)) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2661 | return true; |
| 2662 | |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2663 | if (ExprKind == UETT_SizeOf) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2664 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2665 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 2666 | QualType OType = PVD->getOriginalType(); |
| 2667 | QualType Type = PVD->getType(); |
| 2668 | if (Type->isPointerType() && OType->isArrayType()) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2669 | Diag(E->getExprLoc(), diag::warn_sizeof_array_param) |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2670 | << Type << OType; |
| 2671 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 2672 | } |
| 2673 | } |
| 2674 | } |
| 2675 | } |
| 2676 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2677 | return false; |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | /// \brief Check the constraints on operands to unary expression and type |
| 2681 | /// traits. |
| 2682 | /// |
| 2683 | /// This will complete any types necessary, and validate the various constraints |
| 2684 | /// on those operands. |
| 2685 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2686 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2687 | /// C99 6.3.2.1p[2-4] all state: |
| 2688 | /// Except when it is the operand of the sizeof operator ... |
| 2689 | /// |
| 2690 | /// C++ [expr.sizeof]p4 |
| 2691 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 2692 | /// standard conversions are not applied to the operand of sizeof. |
| 2693 | /// |
| 2694 | /// This policy is followed for all of the unary trait expressions. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2695 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2696 | SourceLocation OpLoc, |
| 2697 | SourceRange ExprRange, |
| 2698 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2699 | if (ExprType->isDependentType()) |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2700 | return false; |
| 2701 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2702 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2703 | // the result is the size of the referenced type." |
| 2704 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2705 | // result shall be the alignment of the referenced type." |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2706 | if (const ReferenceType *Ref = ExprType->getAs<ReferenceType>()) |
| 2707 | ExprType = Ref->getPointeeType(); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2708 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2709 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2710 | return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2711 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2712 | // Whitelist some types as extensions |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2713 | if (!CheckExtensionTraitOperandType(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2714 | ExprKind)) |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2715 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2717 | if (RequireCompleteType(OpLoc, ExprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 2718 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2719 | << ExprKind << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2720 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2722 | if (CheckObjCTraitOperandConstraints(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2723 | ExprKind)) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2724 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2725 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2726 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2729 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2730 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2731 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2732 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2733 | if (isa<DeclRefExpr>(E)) |
| 2734 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2735 | |
| 2736 | // Cannot know anything else if the expression is dependent. |
| 2737 | if (E->isTypeDependent()) |
| 2738 | return false; |
| 2739 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2740 | if (E->getBitField()) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2741 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 2742 | << 1 << E->getSourceRange(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2743 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2744 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2745 | |
| 2746 | // Alignment of a field access is always okay, so long as it isn't a |
| 2747 | // bit-field. |
| 2748 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2749 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2750 | return false; |
| 2751 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2752 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2755 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2756 | E = E->IgnoreParens(); |
| 2757 | |
| 2758 | // Cannot know anything else if the expression is dependent. |
| 2759 | if (E->isTypeDependent()) |
| 2760 | return false; |
| 2761 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2762 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2765 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2766 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2767 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 2768 | SourceLocation OpLoc, |
| 2769 | UnaryExprOrTypeTrait ExprKind, |
| 2770 | SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2771 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2772 | return ExprError(); |
| 2773 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2774 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2775 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2776 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2777 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2778 | return ExprError(); |
| 2779 | |
| 2780 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2781 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 2782 | Context.getSizeType(), |
| 2783 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | /// \brief Build a sizeof or alignof expression given an expression |
| 2787 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2788 | ExprResult |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2789 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 2790 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | 4f0845e | 2011-06-22 23:21:00 +0000 | [diff] [blame] | 2791 | ExprResult PE = CheckPlaceholderExpr(E); |
| 2792 | if (PE.isInvalid()) |
| 2793 | return ExprError(); |
| 2794 | |
| 2795 | E = PE.get(); |
| 2796 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2797 | // Verify that the operand is valid. |
| 2798 | bool isInvalid = false; |
| 2799 | if (E->isTypeDependent()) { |
| 2800 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2801 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2802 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2803 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2804 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2805 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2806 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2807 | isInvalid = true; |
| 2808 | } else { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2809 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | if (isInvalid) |
| 2813 | return ExprError(); |
| 2814 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 2815 | if (ExprKind == UETT_SizeOf && E->getType()->isVariableArrayType()) { |
| 2816 | PE = TranformToPotentiallyEvaluated(E); |
| 2817 | if (PE.isInvalid()) return ExprError(); |
| 2818 | E = PE.take(); |
| 2819 | } |
| 2820 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2821 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2822 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2823 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2824 | E->getSourceRange().getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2827 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 2828 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2829 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2830 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2831 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2832 | UnaryExprOrTypeTrait ExprKind, bool IsType, |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2833 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2834 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2835 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2836 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2837 | if (IsType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2838 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2839 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2840 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2841 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2842 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2843 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2844 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2845 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2848 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2849 | bool IsReal) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2850 | if (V.get()->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2851 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2853 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2854 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 2855 | V = S.DefaultLvalueConversion(V.take()); |
| 2856 | if (V.isInvalid()) |
| 2857 | return QualType(); |
| 2858 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2859 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2860 | // These operators return the element type of a complex type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2861 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2862 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2864 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2865 | if (V.get()->getType()->isArithmeticType()) |
| 2866 | return V.get()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2867 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2868 | // Test for placeholders. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 2869 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2870 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2871 | if (PR.get() != V.get()) { |
| 2872 | V = move(PR); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2873 | return CheckRealImagOperand(S, V, Loc, IsReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2876 | // Reject anything else. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2877 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2878 | << (IsReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2879 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
| 2882 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2883 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2884 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2885 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2886 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2887 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2888 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2889 | default: llvm_unreachable("Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2890 | case tok::plusplus: Opc = UO_PostInc; break; |
| 2891 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2892 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2893 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2894 | // Since this might is a postfix expression, get rid of ParenListExprs. |
| 2895 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Input); |
| 2896 | if (Result.isInvalid()) return ExprError(); |
| 2897 | Input = Result.take(); |
| 2898 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2899 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2902 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2903 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 2904 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2905 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2906 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2907 | if (Result.isInvalid()) return ExprError(); |
| 2908 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2909 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2910 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2911 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2912 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2913 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2914 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2915 | Context.DependentTy, |
| 2916 | VK_LValue, OK_Ordinary, |
| 2917 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2920 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2921 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 2922 | LHSExp->getType()->isEnumeralType() || |
| 2923 | RHSExp->getType()->isRecordType() || |
| 2924 | RHSExp->getType()->isEnumeralType())) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2925 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2928 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2932 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2933 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2934 | Expr *Idx, SourceLocation RLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2935 | Expr *LHSExp = Base; |
| 2936 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2937 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2938 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2939 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 2940 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 2941 | if (Result.isInvalid()) |
| 2942 | return ExprError(); |
| 2943 | LHSExp = Result.take(); |
| 2944 | } |
| 2945 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 2946 | if (Result.isInvalid()) |
| 2947 | return ExprError(); |
| 2948 | RHSExp = Result.take(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2949 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2950 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2951 | ExprValueKind VK = VK_LValue; |
| 2952 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2953 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2954 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2955 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2956 | // in the subscript position. As a result, we need to derive the array base |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2957 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2958 | Expr *BaseExpr, *IndexExpr; |
| 2959 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2960 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 2961 | BaseExpr = LHSExp; |
| 2962 | IndexExpr = RHSExp; |
| 2963 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2964 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2965 | BaseExpr = LHSExp; |
| 2966 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2967 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2968 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 2969 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2970 | BaseExpr = RHSExp; |
| 2971 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2972 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2973 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2974 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2975 | BaseExpr = LHSExp; |
| 2976 | IndexExpr = RHSExp; |
| 2977 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2978 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2979 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2980 | // Handle the uncommon case of "123[Ptr]". |
| 2981 | BaseExpr = RHSExp; |
| 2982 | IndexExpr = LHSExp; |
| 2983 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2984 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 2985 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2986 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2987 | VK = LHSExp->getValueKind(); |
| 2988 | if (VK != VK_RValue) |
| 2989 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 2990 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2991 | // FIXME: need to deal with const... |
| 2992 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2993 | } else if (LHSTy->isArrayType()) { |
| 2994 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2995 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2996 | // wasn't promoted because of the C90 rule that doesn't |
| 2997 | // allow promoting non-lvalue arrays. Warn, then |
| 2998 | // force the promotion here. |
| 2999 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3000 | LHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3001 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3002 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3003 | LHSTy = LHSExp->getType(); |
| 3004 | |
| 3005 | BaseExpr = LHSExp; |
| 3006 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3007 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3008 | } else if (RHSTy->isArrayType()) { |
| 3009 | // Same as previous, except for 123[f().a] case |
| 3010 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3011 | RHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3012 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3013 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3014 | RHSTy = RHSExp->getType(); |
| 3015 | |
| 3016 | BaseExpr = RHSExp; |
| 3017 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3018 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3019 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3020 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3021 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3022 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3023 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3024 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3025 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3026 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3027 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3028 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3029 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3030 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3031 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3032 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3033 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3034 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3035 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3036 | // incomplete types are not object types. |
| 3037 | if (ResultType->isFunctionType()) { |
| 3038 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3039 | << ResultType << BaseExpr->getSourceRange(); |
| 3040 | return ExprError(); |
| 3041 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3042 | |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3043 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3044 | // GNU extension: subscripting on pointer to void |
Chandler Carruth | 6628969 | 2011-06-27 16:32:27 +0000 | [diff] [blame] | 3045 | Diag(LLoc, diag::ext_gnu_subscript_void_type) |
| 3046 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3047 | |
| 3048 | // C forbids expressions of unqualified void type from being l-values. |
| 3049 | // See IsCForbiddenLValueType. |
| 3050 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3051 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3052 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3053 | PDiag(diag::err_subscript_incomplete_type) |
| 3054 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3055 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3057 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3058 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3059 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3060 | << ResultType << BaseExpr->getSourceRange(); |
| 3061 | return ExprError(); |
| 3062 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3063 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3064 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 3065 | !ResultType.isCForbiddenLValueType()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3066 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3067 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3068 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3071 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3072 | FunctionDecl *FD, |
| 3073 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3074 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3075 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3076 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3077 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3078 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3079 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3080 | return ExprError(); |
| 3081 | } |
| 3082 | |
| 3083 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3084 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3085 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3086 | // Instantiate the expression. |
| 3087 | MultiLevelTemplateArgumentList ArgList |
| 3088 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3089 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3090 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3091 | = ArgList.getInnermost(); |
| 3092 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 3093 | Innermost.second); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3094 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3095 | ExprResult Result; |
| 3096 | { |
| 3097 | // C++ [dcl.fct.default]p5: |
| 3098 | // The names in the [default argument] expression are bound, and |
| 3099 | // the semantic constraints are checked, at the point where the |
| 3100 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3101 | ContextRAII SavedContext(*this, FD); |
Douglas Gregor | 7bdc152 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 3102 | LocalInstantiationScope Local(*this); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3103 | Result = SubstExpr(UninstExpr, ArgList); |
| 3104 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3105 | if (Result.isInvalid()) |
| 3106 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3107 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3108 | // Check the expression as an initializer for the parameter. |
| 3109 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3110 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3111 | InitializationKind Kind |
| 3112 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 3113 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 3114 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3115 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3116 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 3117 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 3118 | MultiExprArg(*this, &ResultE, 1)); |
| 3119 | if (Result.isInvalid()) |
| 3120 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3121 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3122 | // Build the default argument expression. |
| 3123 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 3124 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3127 | // If the default expression creates temporaries, we need to |
| 3128 | // push them to the current stack of expression temporaries so they'll |
| 3129 | // be properly destroyed. |
| 3130 | // FIXME: We should really be rebuilding the default argument with new |
| 3131 | // bound temporaries; see the comment in PR5810. |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3132 | // We don't need to do that with block decls, though, because |
| 3133 | // blocks in default argument expression can never capture anything. |
| 3134 | if (isa<ExprWithCleanups>(Param->getInit())) { |
| 3135 | // Set the "needs cleanups" bit regardless of whether there are |
| 3136 | // any explicit objects. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3137 | ExprNeedsCleanups = true; |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3138 | |
| 3139 | // Append all the objects to the cleanup list. Right now, this |
| 3140 | // should always be a no-op, because blocks in default argument |
| 3141 | // expressions should never be able to capture anything. |
| 3142 | assert(!cast<ExprWithCleanups>(Param->getInit())->getNumObjects() && |
| 3143 | "default argument expression has capturing blocks?"); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3144 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3145 | |
| 3146 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 3147 | // Just mark all of the declarations in this potentially-evaluated expression |
| 3148 | // as being "referenced". |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 3149 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg(), |
| 3150 | /*SkipLocalVariables=*/true); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3151 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3154 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3155 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3156 | /// function prototype Proto. Call is the call expression itself, and |
| 3157 | /// Fn is the function expression. For a C++ member function, this |
| 3158 | /// routine does not attempt to convert the object argument. Returns |
| 3159 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3160 | bool |
| 3161 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3162 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3163 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3164 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3165 | SourceLocation RParenLoc, |
| 3166 | bool IsExecConfig) { |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3167 | // Bail out early if calling a builtin with custom typechecking. |
| 3168 | // We don't need to do this in the |
| 3169 | if (FDecl) |
| 3170 | if (unsigned ID = FDecl->getBuiltinID()) |
| 3171 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 3172 | return false; |
| 3173 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3174 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3175 | // assignment, to the types of the corresponding parameter, ... |
| 3176 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3177 | bool Invalid = false; |
Peter Collingbourne | af15b4d | 2011-10-02 23:49:20 +0000 | [diff] [blame] | 3178 | unsigned MinArgs = FDecl ? FDecl->getMinRequiredArguments() : NumArgsInProto; |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3179 | unsigned FnKind = Fn->getType()->isBlockPointerType() |
| 3180 | ? 1 /* block */ |
| 3181 | : (IsExecConfig ? 3 /* kernel function (exec config) */ |
| 3182 | : 0 /* function */); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3183 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3184 | // If too few arguments are available (and we don't have default |
| 3185 | // arguments for the remaining parameters), don't make the call. |
| 3186 | if (NumArgs < NumArgsInProto) { |
Peter Collingbourne | af15b4d | 2011-10-02 23:49:20 +0000 | [diff] [blame] | 3187 | if (NumArgs < MinArgs) { |
| 3188 | Diag(RParenLoc, MinArgs == NumArgsInProto |
| 3189 | ? diag::err_typecheck_call_too_few_args |
| 3190 | : diag::err_typecheck_call_too_few_args_at_least) |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3191 | << FnKind |
Peter Collingbourne | af15b4d | 2011-10-02 23:49:20 +0000 | [diff] [blame] | 3192 | << MinArgs << NumArgs << Fn->getSourceRange(); |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3193 | |
| 3194 | // Emit the location of the prototype. |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3195 | if (FDecl && !FDecl->getBuiltinID() && !IsExecConfig) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3196 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3197 | << FDecl; |
| 3198 | |
| 3199 | return true; |
| 3200 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3201 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3202 | } |
| 3203 | |
| 3204 | // If too many are passed and not variadic, error on the extras and drop |
| 3205 | // them. |
| 3206 | if (NumArgs > NumArgsInProto) { |
| 3207 | if (!Proto->isVariadic()) { |
| 3208 | Diag(Args[NumArgsInProto]->getLocStart(), |
Peter Collingbourne | af15b4d | 2011-10-02 23:49:20 +0000 | [diff] [blame] | 3209 | MinArgs == NumArgsInProto |
| 3210 | ? diag::err_typecheck_call_too_many_args |
| 3211 | : diag::err_typecheck_call_too_many_args_at_most) |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3212 | << FnKind |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 3213 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3214 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3215 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3216 | |
| 3217 | // Emit the location of the prototype. |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3218 | if (FDecl && !FDecl->getBuiltinID() && !IsExecConfig) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3219 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3220 | << FDecl; |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3221 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3222 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3223 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3224 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3225 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3226 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3227 | SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3228 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3229 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3230 | if (Fn->getType()->isBlockPointerType()) |
| 3231 | CallType = VariadicBlock; // Block |
| 3232 | else if (isa<MemberExpr>(Fn)) |
| 3233 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3234 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3235 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3236 | if (Invalid) |
| 3237 | return true; |
| 3238 | unsigned TotalNumArgs = AllArgs.size(); |
| 3239 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3240 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3241 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3242 | return false; |
| 3243 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3244 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3245 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3246 | FunctionDecl *FDecl, |
| 3247 | const FunctionProtoType *Proto, |
| 3248 | unsigned FirstProtoArg, |
| 3249 | Expr **Args, unsigned NumArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3250 | SmallVector<Expr *, 8> &AllArgs, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3251 | VariadicCallType CallType, |
| 3252 | bool AllowExplicit) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3253 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3254 | unsigned NumArgsToCheck = NumArgs; |
| 3255 | bool Invalid = false; |
| 3256 | if (NumArgs != NumArgsInProto) |
| 3257 | // Use default arguments for missing arguments |
| 3258 | NumArgsToCheck = NumArgsInProto; |
| 3259 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3260 | // Continue to check argument types (even if we have too few/many args). |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3261 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3262 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3263 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3264 | Expr *Arg; |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3265 | ParmVarDecl *Param; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3266 | if (ArgIx < NumArgs) { |
| 3267 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3268 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3269 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3270 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3271 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3272 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3273 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3274 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3275 | // Pass the argument |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3276 | Param = 0; |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3277 | if (FDecl && i < FDecl->getNumParams()) |
| 3278 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3279 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3280 | // Strip the unbridged-cast placeholder expression off, if applicable. |
| 3281 | if (Arg->getType() == Context.ARCUnbridgedCastTy && |
| 3282 | FDecl && FDecl->hasAttr<CFAuditedTransferAttr>() && |
| 3283 | (!Param || !Param->hasAttr<CFConsumedAttr>())) |
| 3284 | Arg = stripARCUnbridgedCast(Arg); |
| 3285 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3286 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3287 | Param? InitializedEntity::InitializeParameter(Context, Param) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3288 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
| 3289 | Proto->isArgConsumed(i)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3290 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3291 | SourceLocation(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3292 | Owned(Arg), |
| 3293 | /*TopLevelOfInitList=*/false, |
| 3294 | AllowExplicit); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3295 | if (ArgE.isInvalid()) |
| 3296 | return true; |
| 3297 | |
| 3298 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3299 | } else { |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3300 | Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3301 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3302 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3303 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3304 | if (ArgExpr.isInvalid()) |
| 3305 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3306 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3307 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3308 | } |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3309 | |
| 3310 | // Check for array bounds violations for each argument to the call. This |
| 3311 | // check only triggers warnings when the argument isn't a more complex Expr |
| 3312 | // with its own checking, such as a BinaryOperator. |
| 3313 | CheckArrayAccess(Arg); |
| 3314 | |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3315 | // Check for violations of C99 static array rules (C99 6.7.5.3p7). |
| 3316 | CheckStaticArrayArgument(CallLoc, Param, Arg); |
| 3317 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3318 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3319 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3320 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3321 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3322 | if (CallType != VariadicDoesNotApply) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3323 | |
| 3324 | // Assume that extern "C" functions with variadic arguments that |
| 3325 | // return __unknown_anytype aren't *really* variadic. |
| 3326 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 3327 | FDecl && FDecl->isExternC()) { |
| 3328 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 3329 | ExprResult arg; |
| 3330 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 3331 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 3332 | else |
| 3333 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 3334 | Invalid |= arg.isInvalid(); |
| 3335 | AllArgs.push_back(arg.take()); |
| 3336 | } |
| 3337 | |
| 3338 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 3339 | } else { |
| 3340 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3341 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, |
| 3342 | FDecl); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3343 | Invalid |= Arg.isInvalid(); |
| 3344 | AllArgs.push_back(Arg.take()); |
| 3345 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3346 | } |
Ted Kremenek | 615eb7c | 2011-09-26 23:36:13 +0000 | [diff] [blame] | 3347 | |
| 3348 | // Check for array bounds violations. |
| 3349 | for (unsigned i = ArgIx; i != NumArgs; ++i) |
| 3350 | CheckArrayAccess(Args[i]); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3351 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3352 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3355 | static void DiagnoseCalleeStaticArrayParam(Sema &S, ParmVarDecl *PVD) { |
| 3356 | TypeLoc TL = PVD->getTypeSourceInfo()->getTypeLoc(); |
| 3357 | if (ArrayTypeLoc *ATL = dyn_cast<ArrayTypeLoc>(&TL)) |
| 3358 | S.Diag(PVD->getLocation(), diag::note_callee_static_array) |
| 3359 | << ATL->getLocalSourceRange(); |
| 3360 | } |
| 3361 | |
| 3362 | /// CheckStaticArrayArgument - If the given argument corresponds to a static |
| 3363 | /// array parameter, check that it is non-null, and that if it is formed by |
| 3364 | /// array-to-pointer decay, the underlying array is sufficiently large. |
| 3365 | /// |
| 3366 | /// C99 6.7.5.3p7: If the keyword static also appears within the [ and ] of the |
| 3367 | /// array type derivation, then for each call to the function, the value of the |
| 3368 | /// corresponding actual argument shall provide access to the first element of |
| 3369 | /// an array with at least as many elements as specified by the size expression. |
| 3370 | void |
| 3371 | Sema::CheckStaticArrayArgument(SourceLocation CallLoc, |
| 3372 | ParmVarDecl *Param, |
| 3373 | const Expr *ArgExpr) { |
| 3374 | // Static array parameters are not supported in C++. |
| 3375 | if (!Param || getLangOptions().CPlusPlus) |
| 3376 | return; |
| 3377 | |
| 3378 | QualType OrigTy = Param->getOriginalType(); |
| 3379 | |
| 3380 | const ArrayType *AT = Context.getAsArrayType(OrigTy); |
| 3381 | if (!AT || AT->getSizeModifier() != ArrayType::Static) |
| 3382 | return; |
| 3383 | |
| 3384 | if (ArgExpr->isNullPointerConstant(Context, |
| 3385 | Expr::NPC_NeverValueDependent)) { |
| 3386 | Diag(CallLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
| 3387 | DiagnoseCalleeStaticArrayParam(*this, Param); |
| 3388 | return; |
| 3389 | } |
| 3390 | |
| 3391 | const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT); |
| 3392 | if (!CAT) |
| 3393 | return; |
| 3394 | |
| 3395 | const ConstantArrayType *ArgCAT = |
| 3396 | Context.getAsConstantArrayType(ArgExpr->IgnoreParenImpCasts()->getType()); |
| 3397 | if (!ArgCAT) |
| 3398 | return; |
| 3399 | |
| 3400 | if (ArgCAT->getSize().ult(CAT->getSize())) { |
| 3401 | Diag(CallLoc, diag::warn_static_array_too_small) |
| 3402 | << ArgExpr->getSourceRange() |
| 3403 | << (unsigned) ArgCAT->getSize().getZExtValue() |
| 3404 | << (unsigned) CAT->getSize().getZExtValue(); |
| 3405 | DiagnoseCalleeStaticArrayParam(*this, Param); |
| 3406 | } |
| 3407 | } |
| 3408 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3409 | /// Given a function expression of unknown-any type, try to rebuild it |
| 3410 | /// to have a function type. |
| 3411 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 3412 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3413 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3414 | /// This provides the location of the left/right parens and a list of comma |
| 3415 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3416 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3417 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3418 | MultiExprArg ArgExprs, SourceLocation RParenLoc, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3419 | Expr *ExecConfig, bool IsExecConfig) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3420 | unsigned NumArgs = ArgExprs.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3421 | |
| 3422 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3423 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3424 | if (Result.isInvalid()) return ExprError(); |
| 3425 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3426 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3427 | Expr **Args = ArgExprs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3428 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3429 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3430 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3431 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3432 | if (NumArgs > 0) { |
| 3433 | // Pseudo-destructor calls should not have any arguments. |
| 3434 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3435 | << FixItHint::CreateRemoval( |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3436 | SourceRange(Args[0]->getLocStart(), |
| 3437 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3439 | NumArgs = 0; |
| 3440 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3441 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3442 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3443 | VK_RValue, RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3444 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3445 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3446 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3447 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3448 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3449 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3450 | bool Dependent = false; |
| 3451 | if (Fn->isTypeDependent()) |
| 3452 | Dependent = true; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3453 | else if (Expr::hasAnyTypeDependentArguments( |
| 3454 | llvm::makeArrayRef(Args, NumArgs))) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3455 | Dependent = true; |
| 3456 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3457 | if (Dependent) { |
| 3458 | if (ExecConfig) { |
| 3459 | return Owned(new (Context) CUDAKernelCallExpr( |
| 3460 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 3461 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 3462 | } else { |
| 3463 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 3464 | Context.DependentTy, VK_RValue, |
| 3465 | RParenLoc)); |
| 3466 | } |
| 3467 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3468 | |
| 3469 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3470 | if (Fn->getType()->isRecordType()) |
| 3471 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3472 | RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3473 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3474 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3475 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3476 | if (result.isInvalid()) return ExprError(); |
| 3477 | Fn = result.take(); |
| 3478 | } |
| 3479 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3480 | if (Fn->getType() == Context.BoundMemberTy) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3481 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3482 | RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3483 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3484 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3485 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3486 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 3487 | if (Fn->getType() == Context.OverloadTy) { |
| 3488 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 3489 | |
Douglas Gregor | ee697e6 | 2011-10-13 18:10:35 +0000 | [diff] [blame] | 3490 | // We aren't supposed to apply this logic for if there's an '&' involved. |
Douglas Gregor | 64a371f | 2011-10-13 18:26:27 +0000 | [diff] [blame] | 3491 | if (!find.HasFormOfMemberPointer) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3492 | OverloadExpr *ovl = find.Expression; |
| 3493 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 3494 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
| 3495 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 3496 | RParenLoc, ExecConfig); |
| 3497 | } else { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3498 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3499 | RParenLoc); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3500 | } |
| 3501 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3504 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | f1d1ca5 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 3505 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3506 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3507 | if (result.isInvalid()) return ExprError(); |
| 3508 | Fn = result.take(); |
| 3509 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3510 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3511 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 3512 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3513 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 3514 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 3515 | if (UnOp->getOpcode() == UO_AddrOf) |
| 3516 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 3517 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3518 | if (isa<DeclRefExpr>(NakedFn)) |
| 3519 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3520 | else if (isa<MemberExpr>(NakedFn)) |
| 3521 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3522 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3523 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3524 | ExecConfig, IsExecConfig); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | ExprResult |
| 3528 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3529 | MultiExprArg ExecConfig, SourceLocation GGGLoc) { |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3530 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 3531 | if (!ConfigDecl) |
| 3532 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 3533 | << "cudaConfigureCall"); |
| 3534 | QualType ConfigQTy = ConfigDecl->getType(); |
| 3535 | |
| 3536 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 3537 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3538 | MarkFunctionReferenced(LLLLoc, ConfigDecl); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3539 | |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3540 | return ActOnCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, 0, |
| 3541 | /*IsExecConfig=*/true); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3544 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 3545 | /// |
| 3546 | /// __builtin_astype( value, dst type ) |
| 3547 | /// |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3548 | ExprResult Sema::ActOnAsTypeExpr(Expr *E, ParsedType ParsedDestTy, |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3549 | SourceLocation BuiltinLoc, |
| 3550 | SourceLocation RParenLoc) { |
| 3551 | ExprValueKind VK = VK_RValue; |
| 3552 | ExprObjectKind OK = OK_Ordinary; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3553 | QualType DstTy = GetTypeFromParser(ParsedDestTy); |
| 3554 | QualType SrcTy = E->getType(); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3555 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 3556 | return ExprError(Diag(BuiltinLoc, |
| 3557 | diag::err_invalid_astype_of_different_size) |
Peter Collingbourne | af9cddf | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 3558 | << DstTy |
| 3559 | << SrcTy |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3560 | << E->getSourceRange()); |
| 3561 | return Owned(new (Context) AsTypeExpr(E, DstTy, VK, OK, BuiltinLoc, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3562 | RParenLoc)); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3565 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3566 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3567 | /// unary-convert to an expression of function-pointer or |
| 3568 | /// block-pointer type. |
| 3569 | /// |
| 3570 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3571 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3572 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3573 | SourceLocation LParenLoc, |
| 3574 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3575 | SourceLocation RParenLoc, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3576 | Expr *Config, bool IsExecConfig) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3577 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3578 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3579 | // Promote the function operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3580 | ExprResult Result = UsualUnaryConversions(Fn); |
| 3581 | if (Result.isInvalid()) |
| 3582 | return ExprError(); |
| 3583 | Fn = Result.take(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3584 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3585 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3586 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3587 | CallExpr *TheCall; |
| 3588 | if (Config) { |
| 3589 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 3590 | cast<CallExpr>(Config), |
| 3591 | Args, NumArgs, |
| 3592 | Context.BoolTy, |
| 3593 | VK_RValue, |
| 3594 | RParenLoc); |
| 3595 | } else { |
| 3596 | TheCall = new (Context) CallExpr(Context, Fn, |
| 3597 | Args, NumArgs, |
| 3598 | Context.BoolTy, |
| 3599 | VK_RValue, |
| 3600 | RParenLoc); |
| 3601 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3602 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3603 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 3604 | |
| 3605 | // Bail out early if calling a builtin with custom typechecking. |
| 3606 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 3607 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 3608 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3609 | retry: |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3610 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3611 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3612 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3613 | // have type pointer to function". |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3614 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3615 | if (FuncT == 0) |
| 3616 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3617 | << Fn->getType() << Fn->getSourceRange()); |
| 3618 | } else if (const BlockPointerType *BPT = |
| 3619 | Fn->getType()->getAs<BlockPointerType>()) { |
| 3620 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 3621 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3622 | // Handle calls to expressions of unknown-any type. |
| 3623 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3624 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3625 | if (rewrite.isInvalid()) return ExprError(); |
| 3626 | Fn = rewrite.take(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3627 | TheCall->setCallee(Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3628 | goto retry; |
| 3629 | } |
| 3630 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3631 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3632 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3633 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3634 | |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 3635 | if (getLangOptions().CUDA) { |
| 3636 | if (Config) { |
| 3637 | // CUDA: Kernel calls must be to global functions |
| 3638 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 3639 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 3640 | << FDecl->getName() << Fn->getSourceRange()); |
| 3641 | |
| 3642 | // CUDA: Kernel function must have 'void' return type |
| 3643 | if (!FuncT->getResultType()->isVoidType()) |
| 3644 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 3645 | << Fn->getType() << Fn->getSourceRange()); |
Peter Collingbourne | 8591a7f | 2011-10-02 23:49:15 +0000 | [diff] [blame] | 3646 | } else { |
| 3647 | // CUDA: Calls to global functions must be configured |
| 3648 | if (FDecl && FDecl->hasAttr<CUDAGlobalAttr>()) |
| 3649 | return ExprError(Diag(LParenLoc, diag::err_global_call_not_config) |
| 3650 | << FDecl->getName() << Fn->getSourceRange()); |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 3651 | } |
| 3652 | } |
| 3653 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3654 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3655 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3656 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3657 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3658 | return ExprError(); |
| 3659 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3660 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3661 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3662 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3663 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3664 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3665 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3666 | RParenLoc, IsExecConfig)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3667 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3668 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3669 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3670 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3671 | if (FDecl) { |
| 3672 | // Check if we have too few/too many template arguments, based |
| 3673 | // on our knowledge of the function definition. |
| 3674 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3675 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3676 | const FunctionProtoType *Proto |
| 3677 | = Def->getType()->getAs<FunctionProtoType>(); |
| 3678 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3679 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3680 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3681 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3682 | |
| 3683 | // If the function we're calling isn't a function prototype, but we have |
| 3684 | // a function prototype from a prior declaratiom, use that prototype. |
| 3685 | if (!FDecl->hasPrototype()) |
| 3686 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3689 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3690 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3691 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3692 | |
| 3693 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3694 | InitializedEntity Entity |
| 3695 | = InitializedEntity::InitializeParameter(Context, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3696 | Proto->getArgType(i), |
| 3697 | Proto->isArgConsumed(i)); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3698 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 3699 | SourceLocation(), |
| 3700 | Owned(Arg)); |
| 3701 | if (ArgE.isInvalid()) |
| 3702 | return true; |
| 3703 | |
| 3704 | Arg = ArgE.takeAs<Expr>(); |
| 3705 | |
| 3706 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3707 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 3708 | |
| 3709 | if (ArgE.isInvalid()) |
| 3710 | return true; |
| 3711 | |
| 3712 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 3715 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3716 | Arg->getType(), |
| 3717 | PDiag(diag::err_call_incomplete_argument) |
| 3718 | << Arg->getSourceRange())) |
| 3719 | return ExprError(); |
| 3720 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3721 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3722 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3723 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3724 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3725 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3726 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3727 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3728 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3729 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3730 | // Check for sentinels |
| 3731 | if (NDecl) |
| 3732 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3733 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3734 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3735 | if (FDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3736 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3737 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3739 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 3740 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3741 | } else if (NDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3742 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3743 | return ExprError(); |
| 3744 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3745 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3746 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3747 | } |
| 3748 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3749 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3750 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3751 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3752 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3753 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3754 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3755 | |
| 3756 | TypeSourceInfo *TInfo; |
| 3757 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 3758 | if (!TInfo) |
| 3759 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 3760 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3761 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3764 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3765 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3766 | SourceLocation RParenLoc, Expr *LiteralExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3767 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3768 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3769 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 3770 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 3771 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 3772 | << SourceRange(LParenLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3773 | LiteralExpr->getSourceRange().getEnd()))) |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 3774 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3775 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3776 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3777 | << SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3778 | } else if (!literalType->isDependentType() && |
| 3779 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3780 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3781 | << SourceRange(LParenLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3782 | LiteralExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3783 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3784 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3785 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3786 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3787 | InitializationKind Kind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3788 | = InitializationKind::CreateCStyleCast(LParenLoc, |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 3789 | SourceRange(LParenLoc, RParenLoc), |
| 3790 | /*InitList=*/true); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3791 | InitializationSequence InitSeq(*this, Entity, Kind, &LiteralExpr, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3792 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3793 | MultiExprArg(*this, &LiteralExpr, 1), |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3794 | &literalType); |
| 3795 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3796 | return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3797 | LiteralExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3798 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3799 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3800 | if (isFileScope) { // 6.5.2.5p3 |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3801 | if (CheckForConstantInitializer(LiteralExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3802 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3803 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3804 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3805 | // In C, compound literals are l-values for some reason. |
| 3806 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 3807 | |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 3808 | return MaybeBindToTemporary( |
| 3809 | new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3810 | VK, LiteralExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3813 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3814 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3815 | SourceLocation RBraceLoc) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3816 | unsigned NumInit = InitArgList.size(); |
| 3817 | Expr **InitList = InitArgList.release(); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3818 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 3819 | // Immediately handle non-overload placeholders. Overloads can be |
| 3820 | // resolved contextually, but everything else here can't. |
| 3821 | for (unsigned I = 0; I != NumInit; ++I) { |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 3822 | if (InitList[I]->getType()->isNonOverloadPlaceholderType()) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 3823 | ExprResult result = CheckPlaceholderExpr(InitList[I]); |
| 3824 | |
| 3825 | // Ignore failures; dropping the entire initializer list because |
| 3826 | // of one failure would be terrible for indexing/etc. |
| 3827 | if (result.isInvalid()) continue; |
| 3828 | |
| 3829 | InitList[I] = result.take(); |
| 3830 | } |
| 3831 | } |
| 3832 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3833 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3834 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3835 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 3836 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 3837 | NumInit, RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3838 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3839 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3842 | /// Do an explicit extend of the given block pointer if we're in ARC. |
| 3843 | static void maybeExtendBlockObject(Sema &S, ExprResult &E) { |
| 3844 | assert(E.get()->getType()->isBlockPointerType()); |
| 3845 | assert(E.get()->isRValue()); |
| 3846 | |
| 3847 | // Only do this in an r-value context. |
| 3848 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 3849 | |
| 3850 | E = ImplicitCastExpr::Create(S.Context, E.get()->getType(), |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3851 | CK_ARCExtendBlockObject, E.get(), |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 3852 | /*base path*/ 0, VK_RValue); |
| 3853 | S.ExprNeedsCleanups = true; |
| 3854 | } |
| 3855 | |
| 3856 | /// Prepare a conversion of the given expression to an ObjC object |
| 3857 | /// pointer type. |
| 3858 | CastKind Sema::PrepareCastToObjCObjectPointer(ExprResult &E) { |
| 3859 | QualType type = E.get()->getType(); |
| 3860 | if (type->isObjCObjectPointerType()) { |
| 3861 | return CK_BitCast; |
| 3862 | } else if (type->isBlockPointerType()) { |
| 3863 | maybeExtendBlockObject(*this, E); |
| 3864 | return CK_BlockPointerToObjCPointerCast; |
| 3865 | } else { |
| 3866 | assert(type->isPointerType()); |
| 3867 | return CK_CPointerToObjCPointerCast; |
| 3868 | } |
| 3869 | } |
| 3870 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3871 | /// Prepares for a scalar cast, performing all the necessary stages |
| 3872 | /// except the final cast and returning the kind required. |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 3873 | CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3874 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 3875 | // Also, callers should have filtered out the invalid cases with |
| 3876 | // pointers. Everything else should be possible. |
| 3877 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3878 | QualType SrcTy = Src.get()->getType(); |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 3879 | if (const AtomicType *SrcAtomicTy = SrcTy->getAs<AtomicType>()) |
| 3880 | SrcTy = SrcAtomicTy->getValueType(); |
| 3881 | if (const AtomicType *DestAtomicTy = DestTy->getAs<AtomicType>()) |
| 3882 | DestTy = DestAtomicTy->getValueType(); |
| 3883 | |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 3884 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3885 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3886 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3887 | switch (Type::ScalarTypeKind SrcKind = SrcTy->getScalarTypeKind()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3888 | case Type::STK_MemberPointer: |
| 3889 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3890 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3891 | case Type::STK_CPointer: |
| 3892 | case Type::STK_BlockPointer: |
| 3893 | case Type::STK_ObjCObjectPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3894 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3895 | case Type::STK_CPointer: |
| 3896 | return CK_BitCast; |
| 3897 | case Type::STK_BlockPointer: |
| 3898 | return (SrcKind == Type::STK_BlockPointer |
| 3899 | ? CK_BitCast : CK_AnyPointerToBlockPointerCast); |
| 3900 | case Type::STK_ObjCObjectPointer: |
| 3901 | if (SrcKind == Type::STK_ObjCObjectPointer) |
| 3902 | return CK_BitCast; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3903 | if (SrcKind == Type::STK_CPointer) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3904 | return CK_CPointerToObjCPointerCast; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3905 | maybeExtendBlockObject(*this, Src); |
| 3906 | return CK_BlockPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3907 | case Type::STK_Bool: |
| 3908 | return CK_PointerToBoolean; |
| 3909 | case Type::STK_Integral: |
| 3910 | return CK_PointerToIntegral; |
| 3911 | case Type::STK_Floating: |
| 3912 | case Type::STK_FloatingComplex: |
| 3913 | case Type::STK_IntegralComplex: |
| 3914 | case Type::STK_MemberPointer: |
| 3915 | llvm_unreachable("illegal cast from pointer"); |
| 3916 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3917 | llvm_unreachable("Should have returned before this"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3918 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3919 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 3920 | case Type::STK_Integral: |
| 3921 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3922 | case Type::STK_CPointer: |
| 3923 | case Type::STK_ObjCObjectPointer: |
| 3924 | case Type::STK_BlockPointer: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 3925 | if (Src.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3926 | Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3927 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3928 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3929 | case Type::STK_Bool: |
| 3930 | return CK_IntegralToBoolean; |
| 3931 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3932 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3933 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3934 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3935 | case Type::STK_IntegralComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 3936 | Src = ImpCastExprToType(Src.take(), |
| 3937 | DestTy->castAs<ComplexType>()->getElementType(), |
| 3938 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3939 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3940 | case Type::STK_FloatingComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 3941 | Src = ImpCastExprToType(Src.take(), |
| 3942 | DestTy->castAs<ComplexType>()->getElementType(), |
| 3943 | CK_IntegralToFloating); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3944 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3945 | case Type::STK_MemberPointer: |
| 3946 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3947 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3948 | llvm_unreachable("Should have returned before this"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3949 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3950 | case Type::STK_Floating: |
| 3951 | switch (DestTy->getScalarTypeKind()) { |
| 3952 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3953 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3954 | case Type::STK_Bool: |
| 3955 | return CK_FloatingToBoolean; |
| 3956 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3957 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3958 | case Type::STK_FloatingComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 3959 | Src = ImpCastExprToType(Src.take(), |
| 3960 | DestTy->castAs<ComplexType>()->getElementType(), |
| 3961 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3962 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3963 | case Type::STK_IntegralComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 3964 | Src = ImpCastExprToType(Src.take(), |
| 3965 | DestTy->castAs<ComplexType>()->getElementType(), |
| 3966 | CK_FloatingToIntegral); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3967 | return CK_IntegralRealToComplex; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3968 | case Type::STK_CPointer: |
| 3969 | case Type::STK_ObjCObjectPointer: |
| 3970 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3971 | llvm_unreachable("valid float->pointer cast?"); |
| 3972 | case Type::STK_MemberPointer: |
| 3973 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3974 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3975 | llvm_unreachable("Should have returned before this"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3976 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3977 | case Type::STK_FloatingComplex: |
| 3978 | switch (DestTy->getScalarTypeKind()) { |
| 3979 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3980 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3981 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3982 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3983 | case Type::STK_Floating: { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 3984 | QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); |
| 3985 | if (Context.hasSameType(ET, DestTy)) |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3986 | return CK_FloatingComplexToReal; |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 3987 | Src = ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3988 | return CK_FloatingCast; |
| 3989 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3990 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3991 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3992 | case Type::STK_Integral: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 3993 | Src = ImpCastExprToType(Src.take(), |
| 3994 | SrcTy->castAs<ComplexType>()->getElementType(), |
| 3995 | CK_FloatingComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3996 | return CK_FloatingToIntegral; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3997 | case Type::STK_CPointer: |
| 3998 | case Type::STK_ObjCObjectPointer: |
| 3999 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4000 | llvm_unreachable("valid complex float->pointer cast?"); |
| 4001 | case Type::STK_MemberPointer: |
| 4002 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4003 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4004 | llvm_unreachable("Should have returned before this"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4005 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4006 | case Type::STK_IntegralComplex: |
| 4007 | switch (DestTy->getScalarTypeKind()) { |
| 4008 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4009 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4010 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4011 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4012 | case Type::STK_Integral: { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4013 | QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); |
| 4014 | if (Context.hasSameType(ET, DestTy)) |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4015 | return CK_IntegralComplexToReal; |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4016 | Src = ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4017 | return CK_IntegralCast; |
| 4018 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4019 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4020 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4021 | case Type::STK_Floating: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4022 | Src = ImpCastExprToType(Src.take(), |
| 4023 | SrcTy->castAs<ComplexType>()->getElementType(), |
| 4024 | CK_IntegralComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4025 | return CK_IntegralToFloating; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4026 | case Type::STK_CPointer: |
| 4027 | case Type::STK_ObjCObjectPointer: |
| 4028 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4029 | llvm_unreachable("valid complex int->pointer cast?"); |
| 4030 | case Type::STK_MemberPointer: |
| 4031 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4032 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4033 | llvm_unreachable("Should have returned before this"); |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4034 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4035 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4036 | llvm_unreachable("Unhandled scalar cast"); |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4037 | } |
| 4038 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4039 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4040 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4041 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4042 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4043 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4044 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4045 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4046 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4047 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4048 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4049 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4050 | } else |
| 4051 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4052 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4053 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4054 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4055 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4056 | return false; |
| 4057 | } |
| 4058 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4059 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 4060 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4061 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4062 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4063 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4064 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4065 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 4066 | // an ExtVectorType. |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4067 | // In OpenCL, casts between vectors of different types are not allowed. |
| 4068 | // (See OpenCL 6.2). |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4069 | if (SrcTy->isVectorType()) { |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4070 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy) |
| 4071 | || (getLangOptions().OpenCL && |
| 4072 | (DestTy.getCanonicalType() != SrcTy.getCanonicalType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4073 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4074 | << DestTy << SrcTy << R; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4075 | return ExprError(); |
| 4076 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4077 | Kind = CK_BitCast; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4078 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4081 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4082 | // conversion will take place first from scalar to elt type, and then |
| 4083 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4084 | if (SrcTy->isPointerType()) |
| 4085 | return Diag(R.getBegin(), |
| 4086 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 4087 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4088 | |
| 4089 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4090 | ExprResult CastExprRes = Owned(CastExpr); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4091 | CastKind CK = PrepareScalarCast(CastExprRes, DestElemTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4092 | if (CastExprRes.isInvalid()) |
| 4093 | return ExprError(); |
| 4094 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4095 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4096 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4097 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4100 | ExprResult |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4101 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, |
| 4102 | Declarator &D, ParsedType &Ty, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4103 | SourceLocation RParenLoc, Expr *CastExpr) { |
| 4104 | assert(!D.isInvalidType() && (CastExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4105 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4106 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4107 | TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, CastExpr->getType()); |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4108 | if (D.isInvalidType()) |
| 4109 | return ExprError(); |
| 4110 | |
| 4111 | if (getLangOptions().CPlusPlus) { |
| 4112 | // Check that there are no default arguments (C++ only). |
| 4113 | CheckExtraCXXDefaultArguments(D); |
| 4114 | } |
| 4115 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4116 | checkUnusedDeclAttributes(D); |
| 4117 | |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4118 | QualType castType = castTInfo->getType(); |
| 4119 | Ty = CreateParsedType(castType, castTInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4120 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4121 | bool isVectorLiteral = false; |
| 4122 | |
| 4123 | // Check for an altivec or OpenCL literal, |
| 4124 | // i.e. all the elements are integer constants. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4125 | ParenExpr *PE = dyn_cast<ParenExpr>(CastExpr); |
| 4126 | ParenListExpr *PLE = dyn_cast<ParenListExpr>(CastExpr); |
Tobias Grosser | 37c31c2 | 2011-09-21 18:28:29 +0000 | [diff] [blame] | 4127 | if ((getLangOptions().AltiVec || getLangOptions().OpenCL) |
| 4128 | && castType->isVectorType() && (PE || PLE)) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4129 | if (PLE && PLE->getNumExprs() == 0) { |
| 4130 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4131 | return ExprError(); |
| 4132 | } |
| 4133 | if (PE || PLE->getNumExprs() == 1) { |
| 4134 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); |
| 4135 | if (!E->getType()->isVectorType()) |
| 4136 | isVectorLiteral = true; |
| 4137 | } |
| 4138 | else |
| 4139 | isVectorLiteral = true; |
| 4140 | } |
| 4141 | |
| 4142 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
| 4143 | // then handle it as such. |
| 4144 | if (isVectorLiteral) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4145 | return BuildVectorLiteral(LParenLoc, RParenLoc, CastExpr, castTInfo); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4146 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4147 | // If the Expr being casted is a ParenListExpr, handle it specially. |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4148 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 4149 | // sequence of BinOp comma operators. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4150 | if (isa<ParenListExpr>(CastExpr)) { |
| 4151 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, CastExpr); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4152 | if (Result.isInvalid()) return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4153 | CastExpr = Result.take(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4154 | } |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4155 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4156 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, CastExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4157 | } |
| 4158 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4159 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
| 4160 | SourceLocation RParenLoc, Expr *E, |
| 4161 | TypeSourceInfo *TInfo) { |
| 4162 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && |
| 4163 | "Expected paren or paren list expression"); |
| 4164 | |
| 4165 | Expr **exprs; |
| 4166 | unsigned numExprs; |
| 4167 | Expr *subExpr; |
| 4168 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { |
| 4169 | exprs = PE->getExprs(); |
| 4170 | numExprs = PE->getNumExprs(); |
| 4171 | } else { |
| 4172 | subExpr = cast<ParenExpr>(E)->getSubExpr(); |
| 4173 | exprs = &subExpr; |
| 4174 | numExprs = 1; |
| 4175 | } |
| 4176 | |
| 4177 | QualType Ty = TInfo->getType(); |
| 4178 | assert(Ty->isVectorType() && "Expected vector type"); |
| 4179 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4180 | SmallVector<Expr *, 8> initExprs; |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4181 | const VectorType *VTy = Ty->getAs<VectorType>(); |
| 4182 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 4183 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4184 | // '(...)' form of vector initialization in AltiVec: the number of |
| 4185 | // initializers must be one or must match the size of the vector. |
| 4186 | // If a single value is specified in the initializer then it will be |
| 4187 | // replicated to all the components of the vector |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4188 | if (VTy->getVectorKind() == VectorType::AltiVecVector) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4189 | // The number of initializers must be one or must match the size of the |
| 4190 | // vector. If a single value is specified in the initializer then it will |
| 4191 | // be replicated to all the components of the vector |
| 4192 | if (numExprs == 1) { |
| 4193 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
Richard Smith | 61ffd09 | 2011-10-27 23:31:58 +0000 | [diff] [blame] | 4194 | ExprResult Literal = DefaultLvalueConversion(exprs[0]); |
| 4195 | if (Literal.isInvalid()) |
| 4196 | return ExprError(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4197 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4198 | PrepareScalarCast(Literal, ElemTy)); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4199 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4200 | } |
| 4201 | else if (numExprs < numElems) { |
| 4202 | Diag(E->getExprLoc(), |
| 4203 | diag::err_incorrect_number_of_vector_initializers); |
| 4204 | return ExprError(); |
| 4205 | } |
| 4206 | else |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 4207 | initExprs.append(exprs, exprs + numExprs); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4208 | } |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4209 | else { |
| 4210 | // For OpenCL, when the number of initializers is a single value, |
| 4211 | // it will be replicated to all components of the vector. |
| 4212 | if (getLangOptions().OpenCL && |
| 4213 | VTy->getVectorKind() == VectorType::GenericVector && |
| 4214 | numExprs == 1) { |
| 4215 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
Richard Smith | 61ffd09 | 2011-10-27 23:31:58 +0000 | [diff] [blame] | 4216 | ExprResult Literal = DefaultLvalueConversion(exprs[0]); |
| 4217 | if (Literal.isInvalid()) |
| 4218 | return ExprError(); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4219 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4220 | PrepareScalarCast(Literal, ElemTy)); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4221 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4222 | } |
| 4223 | |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 4224 | initExprs.append(exprs, exprs + numExprs); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4225 | } |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4226 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4227 | // braces instead of the original commas. |
| 4228 | InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc, |
| 4229 | &initExprs[0], |
| 4230 | initExprs.size(), RParenLoc); |
| 4231 | initE->setType(Ty); |
| 4232 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); |
| 4233 | } |
| 4234 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4235 | /// This is not an AltiVec-style cast or or C++ direct-initialization, so turn |
| 4236 | /// the ParenListExpr into a sequence of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4237 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4238 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *OrigExpr) { |
| 4239 | ParenListExpr *E = dyn_cast<ParenListExpr>(OrigExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4240 | if (!E) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4241 | return Owned(OrigExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4242 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4243 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4244 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4245 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4246 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4247 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4248 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4249 | if (Result.isInvalid()) return ExprError(); |
| 4250 | |
| 4251 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4254 | ExprResult Sema::ActOnParenListExpr(SourceLocation L, |
| 4255 | SourceLocation R, |
| 4256 | MultiExprArg Val) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4257 | unsigned nexprs = Val.size(); |
| 4258 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4259 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4260 | Expr *expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4261 | return Owned(expr); |
| 4262 | } |
| 4263 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4264 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4265 | /// constant and the other is not a pointer. Returns true if a diagnostic is |
| 4266 | /// emitted. |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4267 | bool Sema::DiagnoseConditionalForNull(Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4268 | SourceLocation QuestionLoc) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4269 | Expr *NullExpr = LHSExpr; |
| 4270 | Expr *NonPointerExpr = RHSExpr; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4271 | Expr::NullPointerConstantKind NullKind = |
| 4272 | NullExpr->isNullPointerConstant(Context, |
| 4273 | Expr::NPC_ValueDependentIsNotNull); |
| 4274 | |
| 4275 | if (NullKind == Expr::NPCK_NotNull) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4276 | NullExpr = RHSExpr; |
| 4277 | NonPointerExpr = LHSExpr; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4278 | NullKind = |
| 4279 | NullExpr->isNullPointerConstant(Context, |
| 4280 | Expr::NPC_ValueDependentIsNotNull); |
| 4281 | } |
| 4282 | |
| 4283 | if (NullKind == Expr::NPCK_NotNull) |
| 4284 | return false; |
| 4285 | |
| 4286 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 4287 | // In this case, check to make sure that we got here from a "NULL" |
| 4288 | // string in the source code. |
| 4289 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 4290 | SourceLocation loc = NullExpr->getExprLoc(); |
| 4291 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4292 | return false; |
| 4293 | } |
| 4294 | |
| 4295 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 4296 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 4297 | << NonPointerExpr->getType() << DiagType |
| 4298 | << NonPointerExpr->getSourceRange(); |
| 4299 | return true; |
| 4300 | } |
| 4301 | |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4302 | /// \brief Return false if the condition expression is valid, true otherwise. |
| 4303 | static bool checkCondition(Sema &S, Expr *Cond) { |
| 4304 | QualType CondTy = Cond->getType(); |
| 4305 | |
| 4306 | // C99 6.5.15p2 |
| 4307 | if (CondTy->isScalarType()) return false; |
| 4308 | |
| 4309 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 4310 | if (S.getLangOptions().OpenCL && CondTy->isVectorType()) |
| 4311 | return false; |
| 4312 | |
| 4313 | // Emit the proper error message. |
| 4314 | S.Diag(Cond->getLocStart(), S.getLangOptions().OpenCL ? |
| 4315 | diag::err_typecheck_cond_expect_scalar : |
| 4316 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 4317 | << CondTy; |
| 4318 | return true; |
| 4319 | } |
| 4320 | |
| 4321 | /// \brief Return false if the two expressions can be converted to a vector, |
| 4322 | /// true otherwise |
| 4323 | static bool checkConditionalConvertScalarsToVectors(Sema &S, ExprResult &LHS, |
| 4324 | ExprResult &RHS, |
| 4325 | QualType CondTy) { |
| 4326 | // Both operands should be of scalar type. |
| 4327 | if (!LHS.get()->getType()->isScalarType()) { |
| 4328 | S.Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4329 | << CondTy; |
| 4330 | return true; |
| 4331 | } |
| 4332 | if (!RHS.get()->getType()->isScalarType()) { |
| 4333 | S.Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4334 | << CondTy; |
| 4335 | return true; |
| 4336 | } |
| 4337 | |
| 4338 | // Implicity convert these scalars to the type of the condition. |
| 4339 | LHS = S.ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 4340 | RHS = S.ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
| 4341 | return false; |
| 4342 | } |
| 4343 | |
| 4344 | /// \brief Handle when one or both operands are void type. |
| 4345 | static QualType checkConditionalVoidType(Sema &S, ExprResult &LHS, |
| 4346 | ExprResult &RHS) { |
| 4347 | Expr *LHSExpr = LHS.get(); |
| 4348 | Expr *RHSExpr = RHS.get(); |
| 4349 | |
| 4350 | if (!LHSExpr->getType()->isVoidType()) |
| 4351 | S.Diag(RHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4352 | << RHSExpr->getSourceRange(); |
| 4353 | if (!RHSExpr->getType()->isVoidType()) |
| 4354 | S.Diag(LHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4355 | << LHSExpr->getSourceRange(); |
| 4356 | LHS = S.ImpCastExprToType(LHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4357 | RHS = S.ImpCastExprToType(RHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4358 | return S.Context.VoidTy; |
| 4359 | } |
| 4360 | |
| 4361 | /// \brief Return false if the NullExpr can be promoted to PointerTy, |
| 4362 | /// true otherwise. |
| 4363 | static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr, |
| 4364 | QualType PointerTy) { |
| 4365 | if ((!PointerTy->isAnyPointerType() && !PointerTy->isBlockPointerType()) || |
| 4366 | !NullExpr.get()->isNullPointerConstant(S.Context, |
| 4367 | Expr::NPC_ValueDependentIsNull)) |
| 4368 | return true; |
| 4369 | |
| 4370 | NullExpr = S.ImpCastExprToType(NullExpr.take(), PointerTy, CK_NullToPointer); |
| 4371 | return false; |
| 4372 | } |
| 4373 | |
| 4374 | /// \brief Checks compatibility between two pointers and return the resulting |
| 4375 | /// type. |
| 4376 | static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, |
| 4377 | ExprResult &RHS, |
| 4378 | SourceLocation Loc) { |
| 4379 | QualType LHSTy = LHS.get()->getType(); |
| 4380 | QualType RHSTy = RHS.get()->getType(); |
| 4381 | |
| 4382 | if (S.Context.hasSameType(LHSTy, RHSTy)) { |
| 4383 | // Two identical pointers types are always compatible. |
| 4384 | return LHSTy; |
| 4385 | } |
| 4386 | |
| 4387 | QualType lhptee, rhptee; |
| 4388 | |
| 4389 | // Get the pointee types. |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4390 | if (const BlockPointerType *LHSBTy = LHSTy->getAs<BlockPointerType>()) { |
| 4391 | lhptee = LHSBTy->getPointeeType(); |
| 4392 | rhptee = RHSTy->castAs<BlockPointerType>()->getPointeeType(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4393 | } else { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4394 | lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); |
| 4395 | rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | if (!S.Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4399 | rhptee.getUnqualifiedType())) { |
| 4400 | S.Diag(Loc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4401 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4402 | << RHS.get()->getSourceRange(); |
| 4403 | // In this situation, we assume void* type. No especially good |
| 4404 | // reason, but this is what gcc does, and we do have to pick |
| 4405 | // to get a consistent AST. |
| 4406 | QualType incompatTy = S.Context.getPointerType(S.Context.VoidTy); |
| 4407 | LHS = S.ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4408 | RHS = S.ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
| 4409 | return incompatTy; |
| 4410 | } |
| 4411 | |
| 4412 | // The pointer types are compatible. |
| 4413 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4414 | // differently qualified versions of compatible types, the result type is |
| 4415 | // a pointer to an appropriately qualified version of the *composite* |
| 4416 | // type. |
| 4417 | // FIXME: Need to calculate the composite type. |
| 4418 | // FIXME: Need to add qualifiers |
| 4419 | |
| 4420 | LHS = S.ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 4421 | RHS = S.ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
| 4422 | return LHSTy; |
| 4423 | } |
| 4424 | |
| 4425 | /// \brief Return the resulting type when the operands are both block pointers. |
| 4426 | static QualType checkConditionalBlockPointerCompatibility(Sema &S, |
| 4427 | ExprResult &LHS, |
| 4428 | ExprResult &RHS, |
| 4429 | SourceLocation Loc) { |
| 4430 | QualType LHSTy = LHS.get()->getType(); |
| 4431 | QualType RHSTy = RHS.get()->getType(); |
| 4432 | |
| 4433 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4434 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4435 | QualType destType = S.Context.getPointerType(S.Context.VoidTy); |
| 4436 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4437 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4438 | return destType; |
| 4439 | } |
| 4440 | S.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 4441 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4442 | << RHS.get()->getSourceRange(); |
| 4443 | return QualType(); |
| 4444 | } |
| 4445 | |
| 4446 | // We have 2 block pointer types. |
| 4447 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4448 | } |
| 4449 | |
| 4450 | /// \brief Return the resulting type when the operands are both pointers. |
| 4451 | static QualType |
| 4452 | checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, |
| 4453 | ExprResult &RHS, |
| 4454 | SourceLocation Loc) { |
| 4455 | // get the pointer types |
| 4456 | QualType LHSTy = LHS.get()->getType(); |
| 4457 | QualType RHSTy = RHS.get()->getType(); |
| 4458 | |
| 4459 | // get the "pointed to" types |
| 4460 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4461 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4462 | |
| 4463 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4464 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4465 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 4466 | QualType destPointee |
| 4467 | = S.Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4468 | QualType destType = S.Context.getPointerType(destPointee); |
| 4469 | // Add qualifiers if necessary. |
| 4470 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
| 4471 | // Promote to void*. |
| 4472 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4473 | return destType; |
| 4474 | } |
| 4475 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
| 4476 | QualType destPointee |
| 4477 | = S.Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4478 | QualType destType = S.Context.getPointerType(destPointee); |
| 4479 | // Add qualifiers if necessary. |
| 4480 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
| 4481 | // Promote to void*. |
| 4482 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4483 | return destType; |
| 4484 | } |
| 4485 | |
| 4486 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4487 | } |
| 4488 | |
| 4489 | /// \brief Return false if the first expression is not an integer and the second |
| 4490 | /// expression is not a pointer, true otherwise. |
| 4491 | static bool checkPointerIntegerMismatch(Sema &S, ExprResult &Int, |
| 4492 | Expr* PointerExpr, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4493 | bool IsIntFirstExpr) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4494 | if (!PointerExpr->getType()->isPointerType() || |
| 4495 | !Int.get()->getType()->isIntegerType()) |
| 4496 | return false; |
| 4497 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4498 | Expr *Expr1 = IsIntFirstExpr ? Int.get() : PointerExpr; |
| 4499 | Expr *Expr2 = IsIntFirstExpr ? PointerExpr : Int.get(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4500 | |
| 4501 | S.Diag(Loc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4502 | << Expr1->getType() << Expr2->getType() |
| 4503 | << Expr1->getSourceRange() << Expr2->getSourceRange(); |
| 4504 | Int = S.ImpCastExprToType(Int.take(), PointerExpr->getType(), |
| 4505 | CK_IntegralToPointer); |
| 4506 | return true; |
| 4507 | } |
| 4508 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4509 | /// Note that LHS is not null here, even if this is the gnu "x ?: y" extension. |
| 4510 | /// In that case, LHS = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4511 | /// C99 6.5.15 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4512 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 4513 | ExprResult &RHS, ExprValueKind &VK, |
| 4514 | ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4515 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 4516 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4517 | ExprResult LHSResult = CheckPlaceholderExpr(LHS.get()); |
| 4518 | if (!LHSResult.isUsable()) return QualType(); |
| 4519 | LHS = move(LHSResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4520 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4521 | ExprResult RHSResult = CheckPlaceholderExpr(RHS.get()); |
| 4522 | if (!RHSResult.isUsable()) return QualType(); |
| 4523 | RHS = move(RHSResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4524 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4525 | // C++ is sufficiently different to merit its own checker. |
| 4526 | if (getLangOptions().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4527 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4528 | |
| 4529 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4530 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4531 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4532 | Cond = UsualUnaryConversions(Cond.take()); |
| 4533 | if (Cond.isInvalid()) |
| 4534 | return QualType(); |
| 4535 | LHS = UsualUnaryConversions(LHS.take()); |
| 4536 | if (LHS.isInvalid()) |
| 4537 | return QualType(); |
| 4538 | RHS = UsualUnaryConversions(RHS.take()); |
| 4539 | if (RHS.isInvalid()) |
| 4540 | return QualType(); |
| 4541 | |
| 4542 | QualType CondTy = Cond.get()->getType(); |
| 4543 | QualType LHSTy = LHS.get()->getType(); |
| 4544 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4545 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4546 | // first, check the condition. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4547 | if (checkCondition(*this, Cond.get())) |
| 4548 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4549 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4550 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4551 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 4552 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4553 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4554 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 4555 | // attempt to implicity convert them to the vector type to act like the |
| 4556 | // built in select. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4557 | if (getLangOptions().OpenCL && CondTy->isVectorType()) |
| 4558 | if (checkConditionalConvertScalarsToVectors(*this, LHS, RHS, CondTy)) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4559 | return QualType(); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4560 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4561 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4562 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4563 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4564 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4565 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4566 | return QualType(); |
| 4567 | return LHS.get()->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4568 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4569 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4570 | // If both operands are the same structure or union type, the result is that |
| 4571 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4572 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4573 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4574 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4575 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4576 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4577 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4578 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4579 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4580 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4581 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4582 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4583 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4584 | return checkConditionalVoidType(*this, LHS, RHS); |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4585 | } |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4586 | |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4587 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4588 | // the type of the other operand." |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4589 | if (!checkConditionalNullPointer(*this, RHS, LHSTy)) return LHSTy; |
| 4590 | if (!checkConditionalNullPointer(*this, LHS, RHSTy)) return RHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4591 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4592 | // All objective-c pointer type analysis is done here. |
| 4593 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4594 | QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4595 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4596 | return QualType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4597 | if (!compositeType.isNull()) |
| 4598 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4599 | |
| 4600 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4601 | // Handle block pointer types. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4602 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) |
| 4603 | return checkConditionalBlockPointerCompatibility(*this, LHS, RHS, |
| 4604 | QuestionLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4605 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4606 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4607 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) |
| 4608 | return checkConditionalObjectPointersCompatibility(*this, LHS, RHS, |
| 4609 | QuestionLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4610 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4611 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 4612 | // null pointers have been filtered out by this point. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4613 | if (checkPointerIntegerMismatch(*this, LHS, RHS.get(), QuestionLoc, |
| 4614 | /*isIntFirstExpr=*/true)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4615 | return RHSTy; |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4616 | if (checkPointerIntegerMismatch(*this, RHS, LHS.get(), QuestionLoc, |
| 4617 | /*isIntFirstExpr=*/false)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4618 | return LHSTy; |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4619 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4620 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 4621 | // constant and the other is not a pointer type. In this case, the user most |
| 4622 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4623 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4624 | return QualType(); |
| 4625 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4626 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4627 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4628 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4629 | << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4630 | return QualType(); |
| 4631 | } |
| 4632 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4633 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4634 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4635 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4636 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4637 | QualType LHSTy = LHS.get()->getType(); |
| 4638 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4639 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4640 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4641 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4642 | // redefinition type if an attempt is made to access its fields. |
| 4643 | if (LHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4644 | (Context.hasSameType(RHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4645 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4646 | return LHSTy; |
| 4647 | } |
| 4648 | if (RHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4649 | (Context.hasSameType(LHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4650 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4651 | return RHSTy; |
| 4652 | } |
| 4653 | // And the same for struct objc_object* / id |
| 4654 | if (LHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4655 | (Context.hasSameType(RHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4656 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4657 | return LHSTy; |
| 4658 | } |
| 4659 | if (RHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4660 | (Context.hasSameType(LHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4661 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4662 | return RHSTy; |
| 4663 | } |
| 4664 | // And the same for struct objc_selector* / SEL |
| 4665 | if (Context.isObjCSelType(LHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4666 | (Context.hasSameType(RHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4667 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4668 | return LHSTy; |
| 4669 | } |
| 4670 | if (Context.isObjCSelType(RHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4671 | (Context.hasSameType(LHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4672 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4673 | return RHSTy; |
| 4674 | } |
| 4675 | // Check constraints for Objective-C object pointers types. |
| 4676 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4677 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4678 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4679 | // Two identical object pointer types are always compatible. |
| 4680 | return LHSTy; |
| 4681 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4682 | const ObjCObjectPointerType *LHSOPT = LHSTy->castAs<ObjCObjectPointerType>(); |
| 4683 | const ObjCObjectPointerType *RHSOPT = RHSTy->castAs<ObjCObjectPointerType>(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4684 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4685 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4686 | // If both operands are interfaces and either operand can be |
| 4687 | // assigned to the other, use that type as the composite |
| 4688 | // type. This allows |
| 4689 | // xxx ? (A*) a : (B*) b |
| 4690 | // where B is a subclass of A. |
| 4691 | // |
| 4692 | // Additionally, as for assignment, if either type is 'id' |
| 4693 | // allow silent coercion. Finally, if the types are |
| 4694 | // incompatible then make sure to use 'id' as the composite |
| 4695 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4696 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4697 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4698 | // It could return the composite type. |
| 4699 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4700 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4701 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4702 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4703 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4704 | RHSTy->isObjCQualifiedIdType()) && |
| 4705 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4706 | // Need to handle "id<xx>" explicitly. |
| 4707 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4708 | // id. Currently localizing to here until clear this should be |
| 4709 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4710 | compositeType = Context.getObjCIdType(); |
| 4711 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4712 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4713 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4714 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4715 | ; |
| 4716 | else { |
| 4717 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4718 | << LHSTy << RHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4719 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4720 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4721 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4722 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4723 | return incompatTy; |
| 4724 | } |
| 4725 | // The object pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4726 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 4727 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4728 | return compositeType; |
| 4729 | } |
| 4730 | // Check Objective-C object pointer types and 'void *' |
| 4731 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
Eli Friedman | a66eccb | 2012-02-25 00:23:44 +0000 | [diff] [blame] | 4732 | if (getLangOptions().ObjCAutoRefCount) { |
| 4733 | // ARC forbids the implicit conversion of object pointers to 'void *', |
| 4734 | // so these types are not compatible. |
| 4735 | Diag(QuestionLoc, diag::err_cond_voidptr_arc) << LHSTy << RHSTy |
| 4736 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 4737 | LHS = RHS = true; |
| 4738 | return QualType(); |
| 4739 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4740 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4741 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4742 | QualType destPointee |
| 4743 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4744 | QualType destType = Context.getPointerType(destPointee); |
| 4745 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4746 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4747 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4748 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4749 | return destType; |
| 4750 | } |
| 4751 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
Eli Friedman | a66eccb | 2012-02-25 00:23:44 +0000 | [diff] [blame] | 4752 | if (getLangOptions().ObjCAutoRefCount) { |
| 4753 | // ARC forbids the implicit conversion of object pointers to 'void *', |
| 4754 | // so these types are not compatible. |
| 4755 | Diag(QuestionLoc, diag::err_cond_voidptr_arc) << LHSTy << RHSTy |
| 4756 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 4757 | LHS = RHS = true; |
| 4758 | return QualType(); |
| 4759 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4760 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4761 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4762 | QualType destPointee |
| 4763 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4764 | QualType destType = Context.getPointerType(destPointee); |
| 4765 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4766 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4767 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4768 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4769 | return destType; |
| 4770 | } |
| 4771 | return QualType(); |
| 4772 | } |
| 4773 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4774 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4775 | /// ParenRange in parentheses. |
| 4776 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4777 | const PartialDiagnostic &Note, |
| 4778 | SourceRange ParenRange) { |
| 4779 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 4780 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 4781 | EndLoc.isValid()) { |
| 4782 | Self.Diag(Loc, Note) |
| 4783 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 4784 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 4785 | } else { |
| 4786 | // We can't display the parentheses, so just show the bare note. |
| 4787 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4788 | } |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4789 | } |
| 4790 | |
| 4791 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 4792 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 4793 | } |
| 4794 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4795 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 4796 | /// expression, either using a built-in or overloaded operator, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4797 | /// and sets *OpCode to the opcode and *RHSExprs to the right-hand side |
| 4798 | /// expression. |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4799 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4800 | Expr **RHSExprs) { |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 4801 | // Don't strip parenthesis: we should not warn if E is in parenthesis. |
| 4802 | E = E->IgnoreImpCasts(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4803 | E = E->IgnoreConversionOperator(); |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 4804 | E = E->IgnoreImpCasts(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4805 | |
| 4806 | // Built-in binary operator. |
| 4807 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 4808 | if (IsArithmeticOp(OP->getOpcode())) { |
| 4809 | *Opcode = OP->getOpcode(); |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4810 | *RHSExprs = OP->getRHS(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4811 | return true; |
| 4812 | } |
| 4813 | } |
| 4814 | |
| 4815 | // Overloaded operator. |
| 4816 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 4817 | if (Call->getNumArgs() != 2) |
| 4818 | return false; |
| 4819 | |
| 4820 | // Make sure this is really a binary operator that is safe to pass into |
| 4821 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 4822 | OverloadedOperatorKind OO = Call->getOperator(); |
| 4823 | if (OO < OO_Plus || OO > OO_Arrow) |
| 4824 | return false; |
| 4825 | |
| 4826 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 4827 | if (IsArithmeticOp(OpKind)) { |
| 4828 | *Opcode = OpKind; |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4829 | *RHSExprs = Call->getArg(1); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4830 | return true; |
| 4831 | } |
| 4832 | } |
| 4833 | |
| 4834 | return false; |
| 4835 | } |
| 4836 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4837 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 4838 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 4839 | } |
| 4840 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4841 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 4842 | /// or is a logical expression such as (x==y) which has int type, but is |
| 4843 | /// commonly interpreted as boolean. |
| 4844 | static bool ExprLooksBoolean(Expr *E) { |
| 4845 | E = E->IgnoreParenImpCasts(); |
| 4846 | |
| 4847 | if (E->getType()->isBooleanType()) |
| 4848 | return true; |
| 4849 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 4850 | return IsLogicOp(OP->getOpcode()); |
| 4851 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 4852 | return OP->getOpcode() == UO_LNot; |
| 4853 | |
| 4854 | return false; |
| 4855 | } |
| 4856 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4857 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 4858 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 4859 | /// the conditional operator has higher precedence, for example: |
| 4860 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 4861 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 4862 | SourceLocation OpLoc, |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4863 | Expr *Condition, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4864 | Expr *LHSExpr, |
| 4865 | Expr *RHSExpr) { |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4866 | BinaryOperatorKind CondOpcode; |
| 4867 | Expr *CondRHS; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4868 | |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4869 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4870 | return; |
| 4871 | if (!ExprLooksBoolean(CondRHS)) |
| 4872 | return; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4873 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4874 | // The condition is an arithmetic binary expression, with a right- |
| 4875 | // hand side that looks boolean, so warn. |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4876 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4877 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4878 | << Condition->getSourceRange() |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4879 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4880 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4881 | SuggestParentheses(Self, OpLoc, |
| 4882 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 4883 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 4884 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Chandler Carruth | 9d5353c | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 4885 | |
| 4886 | SuggestParentheses(Self, OpLoc, |
| 4887 | Self.PDiag(diag::note_precedence_conditional_first), |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4888 | SourceRange(CondRHS->getLocStart(), RHSExpr->getLocEnd())); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4891 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4892 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4893 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4894 | SourceLocation ColonLoc, |
| 4895 | Expr *CondExpr, Expr *LHSExpr, |
| 4896 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4897 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 4898 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4899 | OpaqueValueExpr *opaqueValue = 0; |
| 4900 | Expr *commonExpr = 0; |
| 4901 | if (LHSExpr == 0) { |
| 4902 | commonExpr = CondExpr; |
| 4903 | |
| 4904 | // We usually want to apply unary conversions *before* saving, except |
| 4905 | // in the special case of a C++ l-value conditional. |
| 4906 | if (!(getLangOptions().CPlusPlus |
| 4907 | && !commonExpr->isTypeDependent() |
| 4908 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 4909 | && commonExpr->isGLValue() |
| 4910 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 4911 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 4912 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4913 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 4914 | if (commonRes.isInvalid()) |
| 4915 | return ExprError(); |
| 4916 | commonExpr = commonRes.take(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 4920 | commonExpr->getType(), |
| 4921 | commonExpr->getValueKind(), |
Douglas Gregor | 97df54e | 2012-02-23 22:17:26 +0000 | [diff] [blame] | 4922 | commonExpr->getObjectKind(), |
| 4923 | commonExpr); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4924 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 4925 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4926 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4927 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4928 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4929 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 4930 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4931 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4932 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 4933 | RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4934 | return ExprError(); |
| 4935 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4936 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 4937 | RHS.get()); |
| 4938 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4939 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4940 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 4941 | LHS.take(), ColonLoc, |
| 4942 | RHS.take(), result, VK, OK)); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4943 | |
| 4944 | return Owned(new (Context) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4945 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4946 | RHS.take(), QuestionLoc, ColonLoc, result, VK, |
| 4947 | OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 4950 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4951 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4952 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 4953 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 4954 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 4955 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 4956 | checkPointerTypesForAssignment(Sema &S, QualType LHSType, QualType RHSType) { |
| 4957 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 4958 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4959 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4960 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 4961 | const Type *lhptee, *rhptee; |
| 4962 | Qualifiers lhq, rhq; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 4963 | llvm::tie(lhptee, lhq) = cast<PointerType>(LHSType)->getPointeeType().split(); |
| 4964 | llvm::tie(rhptee, rhq) = cast<PointerType>(RHSType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4965 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 4966 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4967 | |
| 4968 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 4969 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 4970 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 4971 | Qualifiers lq; |
| 4972 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4973 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 4974 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 4975 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 4976 | // Ignore lifetime for further calculation. |
| 4977 | lhq.removeObjCLifetime(); |
| 4978 | rhq.removeObjCLifetime(); |
| 4979 | } |
| 4980 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 4981 | if (!lhq.compatiblyIncludes(rhq)) { |
| 4982 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 4983 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 4984 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 4985 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4986 | // It's okay to add or remove GC or lifetime qualifiers when converting to |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 4987 | // and from void*. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4988 | else if (lhq.withoutObjCGCAttr().withoutObjCLifetime() |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4989 | .compatiblyIncludes( |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4990 | rhq.withoutObjCGCAttr().withoutObjCLifetime()) |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 4991 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 4992 | ; // keep old |
| 4993 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4994 | // Treat lifetime mismatches as fatal. |
| 4995 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 4996 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 4997 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 4998 | // For GCC compatibility, other qualifier mismatches are treated |
| 4999 | // as still compatible in C. |
| 5000 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 5001 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5002 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5003 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5004 | // incomplete type and the other is a pointer to a qualified or unqualified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5005 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5006 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5007 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5008 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5009 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5010 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5011 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5012 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5013 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5014 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5015 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5016 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5017 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5018 | |
| 5019 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5020 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5021 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5022 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5023 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5024 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5025 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5026 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 5027 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5028 | // Check if the pointee types are compatible ignoring the sign. |
| 5029 | // We explicitly check for char so that we catch "char" vs |
| 5030 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5031 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5032 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5033 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5034 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5035 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5036 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5037 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5038 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5039 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5040 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5041 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5042 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5043 | // takes priority over sign incompatibility because the sign |
| 5044 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5045 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5046 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5047 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5048 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5049 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5050 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5051 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5052 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5053 | // the eventual target type is the same and the pointers have the same |
| 5054 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5055 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5056 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5057 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5058 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5059 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5060 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5061 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5062 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5063 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5064 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5065 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5066 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5067 | } |
Fariborz Jahanian | 53c8167 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 5068 | if (!S.getLangOptions().CPlusPlus && |
| 5069 | S.IsNoReturnConversion(ltrans, rtrans, ltrans)) |
| 5070 | return Sema::IncompatiblePointer; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5071 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5072 | } |
| 5073 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5074 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5075 | /// block pointer types are compatible or whether a block and normal pointer |
| 5076 | /// are compatible. It is more restrict than comparing two function pointer |
| 5077 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5078 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5079 | checkBlockPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5080 | QualType RHSType) { |
| 5081 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5082 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5083 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5084 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5085 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5086 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5087 | lhptee = cast<BlockPointerType>(LHSType)->getPointeeType(); |
| 5088 | rhptee = cast<BlockPointerType>(RHSType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5089 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5090 | // In C++, the types have to match exactly. |
| 5091 | if (S.getLangOptions().CPlusPlus) |
| 5092 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5093 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5094 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5095 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5096 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5097 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5098 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5099 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5100 | if (!S.Context.typesAreBlockPointerCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5101 | return Sema::IncompatibleBlockPointer; |
| 5102 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5103 | return ConvTy; |
| 5104 | } |
| 5105 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5106 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5107 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5108 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5109 | checkObjCPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5110 | QualType RHSType) { |
| 5111 | assert(LHSType.isCanonical() && "LHS was not canonicalized!"); |
| 5112 | assert(RHSType.isCanonical() && "RHS was not canonicalized!"); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5113 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5114 | if (LHSType->isObjCBuiltinType()) { |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5115 | // Class is not compatible with ObjC object pointers. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5116 | if (LHSType->isObjCClassType() && !RHSType->isObjCBuiltinType() && |
| 5117 | !RHSType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5118 | return Sema::IncompatiblePointer; |
| 5119 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5120 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5121 | if (RHSType->isObjCBuiltinType()) { |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5122 | if (RHSType->isObjCClassType() && !LHSType->isObjCBuiltinType() && |
| 5123 | !LHSType->isObjCQualifiedClassType()) |
Fariborz Jahanian | 412a496 | 2011-09-15 20:40:18 +0000 | [diff] [blame] | 5124 | return Sema::IncompatiblePointer; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5125 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5126 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5127 | QualType lhptee = LHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5128 | QualType rhptee = RHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5129 | |
Fariborz Jahanian | f2b4f7b | 2012-01-12 22:12:08 +0000 | [diff] [blame] | 5130 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee) && |
| 5131 | // make an exception for id<P> |
| 5132 | !LHSType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5133 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5134 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5135 | if (S.Context.typesAreCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5136 | return Sema::Compatible; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5137 | if (LHSType->isObjCQualifiedIdType() || RHSType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5138 | return Sema::IncompatibleObjCQualifiedId; |
| 5139 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5142 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5143 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5144 | QualType LHSType, QualType RHSType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5145 | // Fake up an opaque expression. We don't actually care about what |
| 5146 | // cast operations are required, so if CheckAssignmentConstraints |
| 5147 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5148 | // usually happen on valid code. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5149 | OpaqueValueExpr RHSExpr(Loc, RHSType, VK_RValue); |
| 5150 | ExprResult RHSPtr = &RHSExpr; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5151 | CastKind K = CK_Invalid; |
| 5152 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5153 | return CheckAssignmentConstraints(LHSType, RHSPtr, K); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5156 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5157 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5158 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5159 | /// |
| 5160 | /// int a, *pint; |
| 5161 | /// short *pshort; |
| 5162 | /// struct foo *pfoo; |
| 5163 | /// |
| 5164 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5165 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5166 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5167 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5168 | /// |
| 5169 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5170 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5171 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5172 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5173 | Sema::AssignConvertType |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5174 | Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5175 | CastKind &Kind) { |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5176 | QualType RHSType = RHS.get()->getType(); |
| 5177 | QualType OrigLHSType = LHSType; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5178 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5179 | // Get canonical types. We're not formatting these types, just comparing |
| 5180 | // them. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5181 | LHSType = Context.getCanonicalType(LHSType).getUnqualifiedType(); |
| 5182 | RHSType = Context.getCanonicalType(RHSType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5183 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5184 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5185 | // Common case: no conversion required. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5186 | if (LHSType == RHSType) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5187 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5188 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5191 | if (const AtomicType *AtomicTy = dyn_cast<AtomicType>(LHSType)) { |
| 5192 | if (AtomicTy->getValueType() == RHSType) { |
| 5193 | Kind = CK_NonAtomicToAtomic; |
| 5194 | return Compatible; |
| 5195 | } |
| 5196 | } |
| 5197 | |
| 5198 | if (const AtomicType *AtomicTy = dyn_cast<AtomicType>(RHSType)) { |
| 5199 | if (AtomicTy->getValueType() == LHSType) { |
| 5200 | Kind = CK_AtomicToNonAtomic; |
| 5201 | return Compatible; |
| 5202 | } |
| 5203 | } |
| 5204 | |
| 5205 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5206 | // If the left-hand side is a reference type, then we are in a |
| 5207 | // (rare!) case where we've allowed the use of references in C, |
| 5208 | // e.g., as a parameter type in a built-in function. In this case, |
| 5209 | // just make sure that the type referenced is compatible with the |
| 5210 | // right-hand side type. The caller is responsible for adjusting |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5211 | // LHSType so that the resulting expression does not have reference |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5212 | // type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5213 | if (const ReferenceType *LHSTypeRef = LHSType->getAs<ReferenceType>()) { |
| 5214 | if (Context.typesAreCompatible(LHSTypeRef->getPointeeType(), RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5215 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5216 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5217 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5218 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5219 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5220 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5221 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5222 | // to the same ExtVector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5223 | if (LHSType->isExtVectorType()) { |
| 5224 | if (RHSType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5225 | return Incompatible; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5226 | if (RHSType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5227 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5228 | // element type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5229 | QualType elType = cast<ExtVectorType>(LHSType)->getElementType(); |
| 5230 | if (elType != RHSType) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 5231 | Kind = PrepareScalarCast(RHS, elType); |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5232 | RHS = ImpCastExprToType(RHS.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5233 | } |
| 5234 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5235 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5236 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5237 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5238 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5239 | // Conversions to or from vector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5240 | if (LHSType->isVectorType() || RHSType->isVectorType()) { |
| 5241 | if (LHSType->isVectorType() && RHSType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5242 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5243 | // vector type and vice versa |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5244 | if (Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5245 | Kind = CK_BitCast; |
| 5246 | return Compatible; |
| 5247 | } |
| 5248 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5249 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5250 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5251 | // no bits are changed but the result type is different. |
| 5252 | if (getLangOptions().LaxVectorConversions && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5253 | (Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5254 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5255 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5256 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5257 | } |
| 5258 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5259 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5260 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5261 | // Arithmetic conversions. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5262 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType() && |
| 5263 | !(getLangOptions().CPlusPlus && LHSType->isEnumeralType())) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 5264 | Kind = PrepareScalarCast(RHS, LHSType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5265 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5266 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5267 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5268 | // Conversions to normal pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5269 | if (const PointerType *LHSPointer = dyn_cast<PointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5270 | // U* -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5271 | if (isa<PointerType>(RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5272 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5273 | return checkPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5274 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5275 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5276 | // int -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5277 | if (RHSType->isIntegerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5278 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 5279 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5280 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5281 | |
| 5282 | // C pointers are not compatible with ObjC object pointers, |
| 5283 | // with two exceptions: |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5284 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5285 | // - conversions to void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5286 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5287 | Kind = CK_BitCast; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5288 | return Compatible; |
| 5289 | } |
| 5290 | |
| 5291 | // - conversions from 'Class' to the redefinition type |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5292 | if (RHSType->isObjCClassType() && |
| 5293 | Context.hasSameType(LHSType, |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5294 | Context.getObjCClassRedefinitionType())) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5295 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5296 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5297 | } |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 5298 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5299 | Kind = CK_BitCast; |
| 5300 | return IncompatiblePointer; |
| 5301 | } |
| 5302 | |
| 5303 | // U^ -> void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5304 | if (RHSType->getAs<BlockPointerType>()) { |
| 5305 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5306 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5307 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5308 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5309 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5310 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5311 | return Incompatible; |
| 5312 | } |
| 5313 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5314 | // Conversions to block pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5315 | if (isa<BlockPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5316 | // U^ -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5317 | if (RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5318 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5319 | return checkBlockPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | // int or null -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5323 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5324 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5325 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5326 | } |
| 5327 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5328 | // id -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5329 | if (getLangOptions().ObjC1 && RHSType->isObjCIdType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5330 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5331 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5332 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5333 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5334 | // void* -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5335 | if (const PointerType *RHSPT = RHSType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5336 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 5337 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5338 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5339 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5340 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5341 | return Incompatible; |
| 5342 | } |
| 5343 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5344 | // Conversions to Objective-C pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5345 | if (isa<ObjCObjectPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5346 | // A* -> B* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5347 | if (RHSType->isObjCObjectPointerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5348 | Kind = CK_BitCast; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5349 | Sema::AssignConvertType result = |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5350 | checkObjCPointerTypesForAssignment(*this, LHSType, RHSType); |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5351 | if (getLangOptions().ObjCAutoRefCount && |
| 5352 | result == Compatible && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5353 | !CheckObjCARCUnavailableWeakConversion(OrigLHSType, RHSType)) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5354 | result = IncompatibleObjCWeakRef; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5355 | return result; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
| 5358 | // int or null -> A* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5359 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5360 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5361 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5362 | } |
| 5363 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5364 | // In general, C pointers are not compatible with ObjC object pointers, |
| 5365 | // with two exceptions: |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5366 | if (isa<PointerType>(RHSType)) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5367 | Kind = CK_CPointerToObjCPointerCast; |
| 5368 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5369 | // - conversions from 'void*' |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5370 | if (RHSType->isVoidPointerType()) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5371 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5372 | } |
| 5373 | |
| 5374 | // - conversions to 'Class' from its redefinition type |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5375 | if (LHSType->isObjCClassType() && |
| 5376 | Context.hasSameType(RHSType, |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5377 | Context.getObjCClassRedefinitionType())) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5378 | return Compatible; |
| 5379 | } |
| 5380 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5381 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5382 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5383 | |
| 5384 | // T^ -> A* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5385 | if (RHSType->isBlockPointerType()) { |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 5386 | maybeExtendBlockObject(*this, RHS); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5387 | Kind = CK_BlockPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5388 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5389 | } |
| 5390 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5391 | return Incompatible; |
| 5392 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5393 | |
| 5394 | // Conversions from pointers that are not covered by the above. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5395 | if (isa<PointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5396 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5397 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5398 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5399 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5400 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5401 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5402 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5403 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5404 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5405 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5406 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5407 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5408 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5409 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5410 | |
| 5411 | // Conversions from Objective-C pointers that are not covered by the above. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5412 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5413 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5414 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5415 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5416 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5417 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5418 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5419 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5420 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5421 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5422 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5423 | } |
| 5424 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5425 | return Incompatible; |
| 5426 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5427 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5428 | // struct A -> struct B |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5429 | if (isa<TagType>(LHSType) && isa<TagType>(RHSType)) { |
| 5430 | if (Context.typesAreCompatible(LHSType, RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5431 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5432 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5433 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5434 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5435 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5436 | return Incompatible; |
| 5437 | } |
| 5438 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5439 | /// \brief Constructs a transparent union from an expression that is |
| 5440 | /// used to initialize the transparent union. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5441 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, |
| 5442 | ExprResult &EResult, QualType UnionType, |
| 5443 | FieldDecl *Field) { |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5444 | // Build an initializer list that designates the appropriate member |
| 5445 | // of the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5446 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5447 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 5448 | &E, 1, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5449 | SourceLocation()); |
| 5450 | Initializer->setType(UnionType); |
| 5451 | Initializer->setInitializedFieldInUnion(Field); |
| 5452 | |
| 5453 | // Build a compound literal constructing a value of the transparent |
| 5454 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5455 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5456 | EResult = S.Owned( |
| 5457 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 5458 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | Sema::AssignConvertType |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5462 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5463 | ExprResult &RHS) { |
| 5464 | QualType RHSType = RHS.get()->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5465 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5466 | // If the ArgType is a Union type, we want to handle a potential |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5467 | // transparent_union GCC extension. |
| 5468 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5469 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5470 | return Incompatible; |
| 5471 | |
| 5472 | // The field to initialize within the transparent union. |
| 5473 | RecordDecl *UD = UT->getDecl(); |
| 5474 | FieldDecl *InitField = 0; |
| 5475 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5476 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5477 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5478 | it != itend; ++it) { |
| 5479 | if (it->getType()->isPointerType()) { |
| 5480 | // If the transparent union contains a pointer type, we allow: |
| 5481 | // 1) void pointer |
| 5482 | // 2) null pointer constant |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5483 | if (RHSType->isPointerType()) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5484 | if (RHSType->castAs<PointerType>()->getPointeeType()->isVoidType()) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5485 | RHS = ImpCastExprToType(RHS.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5486 | InitField = *it; |
| 5487 | break; |
| 5488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5489 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5490 | if (RHS.get()->isNullPointerConstant(Context, |
| 5491 | Expr::NPC_ValueDependentIsNull)) { |
| 5492 | RHS = ImpCastExprToType(RHS.take(), it->getType(), |
| 5493 | CK_NullToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5494 | InitField = *it; |
| 5495 | break; |
| 5496 | } |
| 5497 | } |
| 5498 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5499 | CastKind Kind = CK_Invalid; |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5500 | if (CheckAssignmentConstraints(it->getType(), RHS, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5501 | == Compatible) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5502 | RHS = ImpCastExprToType(RHS.take(), it->getType(), Kind); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5503 | InitField = *it; |
| 5504 | break; |
| 5505 | } |
| 5506 | } |
| 5507 | |
| 5508 | if (!InitField) |
| 5509 | return Incompatible; |
| 5510 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5511 | ConstructTransparentUnion(*this, Context, RHS, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5512 | return Compatible; |
| 5513 | } |
| 5514 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5515 | Sema::AssignConvertType |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5516 | Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
| 5517 | bool Diagnose) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5518 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5519 | if (!LHSType->isRecordType() && !LHSType->isAtomicType()) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5520 | // C++ 5.17p3: If the left operand is not of class type, the |
| 5521 | // expression is implicitly converted (C++ 4) to the |
| 5522 | // cv-unqualified type of the left operand. |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 5523 | ExprResult Res; |
| 5524 | if (Diagnose) { |
| 5525 | Res = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), |
| 5526 | AA_Assigning); |
| 5527 | } else { |
| 5528 | ImplicitConversionSequence ICS = |
| 5529 | TryImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), |
| 5530 | /*SuppressUserConversions=*/false, |
| 5531 | /*AllowExplicit=*/false, |
| 5532 | /*InOverloadResolution=*/false, |
| 5533 | /*CStyle=*/false, |
| 5534 | /*AllowObjCWritebackConversion=*/false); |
| 5535 | if (ICS.isFailure()) |
| 5536 | return Incompatible; |
| 5537 | Res = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), |
| 5538 | ICS, AA_Assigning); |
| 5539 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5540 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5541 | return Incompatible; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5542 | Sema::AssignConvertType result = Compatible; |
| 5543 | if (getLangOptions().ObjCAutoRefCount && |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5544 | !CheckObjCARCUnavailableWeakConversion(LHSType, |
| 5545 | RHS.get()->getType())) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5546 | result = IncompatibleObjCWeakRef; |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5547 | RHS = move(Res); |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5548 | return result; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 5552 | // structures. |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5553 | // FIXME: We also fall through for atomics; not sure what should |
| 5554 | // happen there, though. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5555 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5556 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5557 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 5558 | // a null pointer constant. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5559 | if ((LHSType->isPointerType() || |
| 5560 | LHSType->isObjCObjectPointerType() || |
| 5561 | LHSType->isBlockPointerType()) |
| 5562 | && RHS.get()->isNullPointerConstant(Context, |
| 5563 | Expr::NPC_ValueDependentIsNull)) { |
| 5564 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5565 | return Compatible; |
| 5566 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5567 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5568 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5569 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 5570 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 5571 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5572 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5573 | // Suppress this for references: C++ 8.5.3p5. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5574 | if (!LHSType->isReferenceType()) { |
| 5575 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 5576 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5577 | return Incompatible; |
| 5578 | } |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5579 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5580 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5581 | Sema::AssignConvertType result = |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5582 | CheckAssignmentConstraints(LHSType, RHS, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5583 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5584 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 5585 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5586 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 5587 | // so that we can use references in built-in functions even in C. |
| 5588 | // The getNonReferenceType() call makes sure that the resulting expression |
| 5589 | // does not have reference type. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5590 | if (result != Incompatible && RHS.get()->getType() != LHSType) |
| 5591 | RHS = ImpCastExprToType(RHS.take(), |
| 5592 | LHSType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5593 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5594 | } |
| 5595 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5596 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &LHS, |
| 5597 | ExprResult &RHS) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5598 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5599 | << LHS.get()->getType() << RHS.get()->getType() |
| 5600 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5601 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5602 | } |
| 5603 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5604 | QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5605 | SourceLocation Loc, bool IsCompAssign) { |
Richard Smith | 9c129f8 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 5606 | if (!IsCompAssign) { |
| 5607 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); |
| 5608 | if (LHS.isInvalid()) |
| 5609 | return QualType(); |
| 5610 | } |
| 5611 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 5612 | if (RHS.isInvalid()) |
| 5613 | return QualType(); |
| 5614 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5615 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5616 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5617 | QualType LHSType = |
| 5618 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 5619 | QualType RHSType = |
| 5620 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5621 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5622 | // If the vector types are identical, return. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5623 | if (LHSType == RHSType) |
| 5624 | return LHSType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5625 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5626 | // Handle the case of equivalent AltiVec and GCC vector types |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5627 | if (LHSType->isVectorType() && RHSType->isVectorType() && |
| 5628 | Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
| 5629 | if (LHSType->isExtVectorType()) { |
| 5630 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 5631 | return LHSType; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5632 | } |
| 5633 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5634 | if (!IsCompAssign) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5635 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
| 5636 | return RHSType; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5637 | } |
| 5638 | |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5639 | if (getLangOptions().LaxVectorConversions && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5640 | Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType)) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5641 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5642 | // vectors, the total size only needs to be the same. This is a |
| 5643 | // bitcast; no bits are changed but the result type is different. |
| 5644 | // FIXME: Should we really be allowing this? |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5645 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 5646 | return LHSType; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5647 | } |
| 5648 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5649 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 5650 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 5651 | bool swapped = false; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5652 | if (RHSType->isExtVectorType() && !IsCompAssign) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5653 | swapped = true; |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5654 | std::swap(RHS, LHS); |
| 5655 | std::swap(RHSType, LHSType); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5656 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5657 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5658 | // Handle the case of an ext vector and scalar. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5659 | if (const ExtVectorType *LV = LHSType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5660 | QualType EltTy = LV->getElementType(); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5661 | if (EltTy->isIntegralType(Context) && RHSType->isIntegralType(Context)) { |
| 5662 | int order = Context.getIntegerTypeOrder(EltTy, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5663 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5664 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5665 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5666 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 5667 | if (swapped) std::swap(RHS, LHS); |
| 5668 | return LHSType; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5669 | } |
| 5670 | } |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5671 | if (EltTy->isRealFloatingType() && RHSType->isScalarType() && |
| 5672 | RHSType->isRealFloatingType()) { |
| 5673 | int order = Context.getFloatingTypeOrder(EltTy, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5674 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5675 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5676 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5677 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 5678 | if (swapped) std::swap(RHS, LHS); |
| 5679 | return LHSType; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5680 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5681 | } |
| 5682 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5683 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5684 | // Vectors of different size or scalar and non-ext-vector are errors. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5685 | if (swapped) std::swap(RHS, LHS); |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5686 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5687 | << LHS.get()->getType() << RHS.get()->getType() |
| 5688 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5689 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 5692 | // checkArithmeticNull - Detect when a NULL constant is used improperly in an |
| 5693 | // expression. These are mainly cases where the null pointer is used as an |
| 5694 | // integer instead of a pointer. |
| 5695 | static void checkArithmeticNull(Sema &S, ExprResult &LHS, ExprResult &RHS, |
| 5696 | SourceLocation Loc, bool IsCompare) { |
| 5697 | // The canonical way to check for a GNU null is with isNullPointerConstant, |
| 5698 | // but we use a bit of a hack here for speed; this is a relatively |
| 5699 | // hot path, and isNullPointerConstant is slow. |
| 5700 | bool LHSNull = isa<GNUNullExpr>(LHS.get()->IgnoreParenImpCasts()); |
| 5701 | bool RHSNull = isa<GNUNullExpr>(RHS.get()->IgnoreParenImpCasts()); |
| 5702 | |
| 5703 | QualType NonNullType = LHSNull ? RHS.get()->getType() : LHS.get()->getType(); |
| 5704 | |
| 5705 | // Avoid analyzing cases where the result will either be invalid (and |
| 5706 | // diagnosed as such) or entirely valid and not something to warn about. |
| 5707 | if ((!LHSNull && !RHSNull) || NonNullType->isBlockPointerType() || |
| 5708 | NonNullType->isMemberPointerType() || NonNullType->isFunctionType()) |
| 5709 | return; |
| 5710 | |
| 5711 | // Comparison operations would not make sense with a null pointer no matter |
| 5712 | // what the other expression is. |
| 5713 | if (!IsCompare) { |
| 5714 | S.Diag(Loc, diag::warn_null_in_arithmetic_operation) |
| 5715 | << (LHSNull ? LHS.get()->getSourceRange() : SourceRange()) |
| 5716 | << (RHSNull ? RHS.get()->getSourceRange() : SourceRange()); |
| 5717 | return; |
| 5718 | } |
| 5719 | |
| 5720 | // The rest of the operations only make sense with a null pointer |
| 5721 | // if the other expression is a pointer. |
| 5722 | if (LHSNull == RHSNull || NonNullType->isAnyPointerType() || |
| 5723 | NonNullType->canDecayToPointerType()) |
| 5724 | return; |
| 5725 | |
| 5726 | S.Diag(Loc, diag::warn_null_in_comparison_operation) |
| 5727 | << LHSNull /* LHS is NULL */ << NonNullType |
| 5728 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5729 | } |
| 5730 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5731 | QualType Sema::CheckMultiplyDivideOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5732 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5733 | bool IsCompAssign, bool IsDiv) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 5734 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 5735 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5736 | if (LHS.get()->getType()->isVectorType() || |
| 5737 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5738 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5739 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5740 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5741 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5742 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5743 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5744 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5745 | if (!LHS.get()->getType()->isArithmeticType() || |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5746 | !RHS.get()->getType()->isArithmeticType()) { |
| 5747 | if (IsCompAssign && |
| 5748 | LHS.get()->getType()->isAtomicType() && |
| 5749 | RHS.get()->getType()->isArithmeticType()) |
| 5750 | return compType; |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5751 | return InvalidOperands(Loc, LHS, RHS); |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5752 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5753 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5754 | // Check for division by zero. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5755 | if (IsDiv && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5756 | RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5757 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5758 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_division_by_zero) |
| 5759 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5760 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5761 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5762 | } |
| 5763 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5764 | QualType Sema::CheckRemainderOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5765 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 5766 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 5767 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5768 | if (LHS.get()->getType()->isVectorType() || |
| 5769 | RHS.get()->getType()->isVectorType()) { |
| 5770 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 5771 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5772 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5773 | return InvalidOperands(Loc, LHS, RHS); |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 5774 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5775 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5776 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5777 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5778 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5779 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5780 | if (!LHS.get()->getType()->isIntegerType() || |
| 5781 | !RHS.get()->getType()->isIntegerType()) |
| 5782 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5783 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5784 | // Check for remainder by zero. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5785 | if (RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5786 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5787 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_remainder_by_zero) |
| 5788 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5789 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5790 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5791 | } |
| 5792 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5793 | /// \brief Diagnose invalid arithmetic on two void pointers. |
| 5794 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5795 | Expr *LHSExpr, Expr *RHSExpr) { |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5796 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5797 | ? diag::err_typecheck_pointer_arith_void_type |
| 5798 | : diag::ext_gnu_void_ptr) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5799 | << 1 /* two pointers */ << LHSExpr->getSourceRange() |
| 5800 | << RHSExpr->getSourceRange(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5801 | } |
| 5802 | |
| 5803 | /// \brief Diagnose invalid arithmetic on a void pointer. |
| 5804 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, |
| 5805 | Expr *Pointer) { |
| 5806 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5807 | ? diag::err_typecheck_pointer_arith_void_type |
| 5808 | : diag::ext_gnu_void_ptr) |
| 5809 | << 0 /* one pointer */ << Pointer->getSourceRange(); |
| 5810 | } |
| 5811 | |
| 5812 | /// \brief Diagnose invalid arithmetic on two function pointers. |
| 5813 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, |
| 5814 | Expr *LHS, Expr *RHS) { |
| 5815 | assert(LHS->getType()->isAnyPointerType()); |
| 5816 | assert(RHS->getType()->isAnyPointerType()); |
| 5817 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5818 | ? diag::err_typecheck_pointer_arith_function_type |
| 5819 | : diag::ext_gnu_ptr_func_arith) |
| 5820 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() |
| 5821 | // We only show the second type if it differs from the first. |
| 5822 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), |
| 5823 | RHS->getType()) |
| 5824 | << RHS->getType()->getPointeeType() |
| 5825 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5826 | } |
| 5827 | |
| 5828 | /// \brief Diagnose invalid arithmetic on a function pointer. |
| 5829 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, |
| 5830 | Expr *Pointer) { |
| 5831 | assert(Pointer->getType()->isAnyPointerType()); |
| 5832 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5833 | ? diag::err_typecheck_pointer_arith_function_type |
| 5834 | : diag::ext_gnu_ptr_func_arith) |
| 5835 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() |
| 5836 | << 0 /* one pointer, so only one type */ |
| 5837 | << Pointer->getSourceRange(); |
| 5838 | } |
| 5839 | |
Richard Trieu | d9f1934 | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 5840 | /// \brief Emit error if Operand is incomplete pointer type |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5841 | /// |
| 5842 | /// \returns True if pointer has incomplete type |
| 5843 | static bool checkArithmeticIncompletePointerType(Sema &S, SourceLocation Loc, |
| 5844 | Expr *Operand) { |
| 5845 | if ((Operand->getType()->isPointerType() && |
| 5846 | !Operand->getType()->isDependentType()) || |
| 5847 | Operand->getType()->isObjCObjectPointerType()) { |
| 5848 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5849 | if (S.RequireCompleteType( |
| 5850 | Loc, PointeeTy, |
| 5851 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 5852 | << PointeeTy << Operand->getSourceRange())) |
| 5853 | return true; |
| 5854 | } |
| 5855 | return false; |
| 5856 | } |
| 5857 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5858 | /// \brief Check the validity of an arithmetic pointer operand. |
| 5859 | /// |
| 5860 | /// If the operand has pointer type, this code will check for pointer types |
| 5861 | /// which are invalid in arithmetic operations. These will be diagnosed |
| 5862 | /// appropriately, including whether or not the use is supported as an |
| 5863 | /// extension. |
| 5864 | /// |
| 5865 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5866 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, |
| 5867 | Expr *Operand) { |
| 5868 | if (!Operand->getType()->isAnyPointerType()) return true; |
| 5869 | |
| 5870 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5871 | if (PointeeTy->isVoidType()) { |
| 5872 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); |
| 5873 | return !S.getLangOptions().CPlusPlus; |
| 5874 | } |
| 5875 | if (PointeeTy->isFunctionType()) { |
| 5876 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); |
| 5877 | return !S.getLangOptions().CPlusPlus; |
| 5878 | } |
| 5879 | |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5880 | if (checkArithmeticIncompletePointerType(S, Loc, Operand)) return false; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5881 | |
| 5882 | return true; |
| 5883 | } |
| 5884 | |
| 5885 | /// \brief Check the validity of a binary arithmetic operation w.r.t. pointer |
| 5886 | /// operands. |
| 5887 | /// |
| 5888 | /// This routine will diagnose any invalid arithmetic on pointer operands much |
| 5889 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic |
| 5890 | /// for emitting a single diagnostic even for operations where both LHS and RHS |
| 5891 | /// are (potentially problematic) pointers. |
| 5892 | /// |
| 5893 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5894 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5895 | Expr *LHSExpr, Expr *RHSExpr) { |
| 5896 | bool isLHSPointer = LHSExpr->getType()->isAnyPointerType(); |
| 5897 | bool isRHSPointer = RHSExpr->getType()->isAnyPointerType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5898 | if (!isLHSPointer && !isRHSPointer) return true; |
| 5899 | |
| 5900 | QualType LHSPointeeTy, RHSPointeeTy; |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5901 | if (isLHSPointer) LHSPointeeTy = LHSExpr->getType()->getPointeeType(); |
| 5902 | if (isRHSPointer) RHSPointeeTy = RHSExpr->getType()->getPointeeType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5903 | |
| 5904 | // Check for arithmetic on pointers to incomplete types. |
| 5905 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); |
| 5906 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); |
| 5907 | if (isLHSVoidPtr || isRHSVoidPtr) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5908 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHSExpr); |
| 5909 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHSExpr); |
| 5910 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5911 | |
| 5912 | return !S.getLangOptions().CPlusPlus; |
| 5913 | } |
| 5914 | |
| 5915 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); |
| 5916 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); |
| 5917 | if (isLHSFuncPtr || isRHSFuncPtr) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5918 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHSExpr); |
| 5919 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, |
| 5920 | RHSExpr); |
| 5921 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5922 | |
| 5923 | return !S.getLangOptions().CPlusPlus; |
| 5924 | } |
| 5925 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5926 | if (checkArithmeticIncompletePointerType(S, Loc, LHSExpr)) return false; |
| 5927 | if (checkArithmeticIncompletePointerType(S, Loc, RHSExpr)) return false; |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5928 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5929 | return true; |
| 5930 | } |
| 5931 | |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 5932 | /// \brief Check bad cases where we step over interface counts. |
| 5933 | static bool checkArithmethicPointerOnNonFragileABI(Sema &S, |
| 5934 | SourceLocation OpLoc, |
| 5935 | Expr *Op) { |
| 5936 | assert(Op->getType()->isAnyPointerType()); |
| 5937 | QualType PointeeTy = Op->getType()->getPointeeType(); |
| 5938 | if (!PointeeTy->isObjCObjectType() || !S.LangOpts.ObjCNonFragileABI) |
| 5939 | return true; |
| 5940 | |
| 5941 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 5942 | << PointeeTy << Op->getSourceRange(); |
| 5943 | return false; |
| 5944 | } |
| 5945 | |
Richard Trieu | d9f1934 | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 5946 | /// \brief Emit error when two pointers are incompatible. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 5947 | static void diagnosePointerIncompatibility(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5948 | Expr *LHSExpr, Expr *RHSExpr) { |
| 5949 | assert(LHSExpr->getType()->isAnyPointerType()); |
| 5950 | assert(RHSExpr->getType()->isAnyPointerType()); |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 5951 | S.Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5952 | << LHSExpr->getType() << RHSExpr->getType() << LHSExpr->getSourceRange() |
| 5953 | << RHSExpr->getSourceRange(); |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 5954 | } |
| 5955 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5956 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5957 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, QualType* CompLHSTy) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 5958 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 5959 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5960 | if (LHS.get()->getType()->isVectorType() || |
| 5961 | RHS.get()->getType()->isVectorType()) { |
| 5962 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5963 | if (CompLHSTy) *CompLHSTy = compType; |
| 5964 | return compType; |
| 5965 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5966 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5967 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 5968 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5969 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5970 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5971 | // handle the common case first (both operands are arithmetic). |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5972 | if (LHS.get()->getType()->isArithmeticType() && |
| 5973 | RHS.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5974 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5975 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5976 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5977 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5978 | if (LHS.get()->getType()->isAtomicType() && |
| 5979 | RHS.get()->getType()->isArithmeticType()) { |
| 5980 | *CompLHSTy = LHS.get()->getType(); |
| 5981 | return compType; |
| 5982 | } |
| 5983 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5984 | // Put any potential pointer into PExp |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 5985 | Expr* PExp = LHS.get(), *IExp = RHS.get(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5986 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5987 | std::swap(PExp, IExp); |
| 5988 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 5989 | if (!PExp->getType()->isAnyPointerType()) |
| 5990 | return InvalidOperands(Loc, LHS, RHS); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5991 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 5992 | if (!IExp->getType()->isIntegerType()) |
| 5993 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5994 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 5995 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) |
| 5996 | return QualType(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5997 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 5998 | // Diagnose bad cases where we step over interface counts. |
| 5999 | if (!checkArithmethicPointerOnNonFragileABI(*this, Loc, PExp)) |
| 6000 | return QualType(); |
| 6001 | |
| 6002 | // Check array bounds for pointer arithemtic |
| 6003 | CheckArrayAccess(PExp, IExp); |
| 6004 | |
| 6005 | if (CompLHSTy) { |
| 6006 | QualType LHSTy = Context.isPromotableBitField(LHS.get()); |
| 6007 | if (LHSTy.isNull()) { |
| 6008 | LHSTy = LHS.get()->getType(); |
| 6009 | if (LHSTy->isPromotableIntegerType()) |
| 6010 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6011 | } |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6012 | *CompLHSTy = LHSTy; |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6013 | } |
| 6014 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6015 | return PExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6016 | } |
| 6017 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6018 | // C99 6.5.6 |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6019 | QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6020 | SourceLocation Loc, |
| 6021 | QualType* CompLHSTy) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6022 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6023 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6024 | if (LHS.get()->getType()->isVectorType() || |
| 6025 | RHS.get()->getType()->isVectorType()) { |
| 6026 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6027 | if (CompLHSTy) *CompLHSTy = compType; |
| 6028 | return compType; |
| 6029 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6030 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6031 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 6032 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6033 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6034 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6035 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6036 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6037 | // Handle the common case first (both operands are arithmetic). |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6038 | if (LHS.get()->getType()->isArithmeticType() && |
| 6039 | RHS.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6040 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6041 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6042 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6043 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 6044 | if (LHS.get()->getType()->isAtomicType() && |
| 6045 | RHS.get()->getType()->isArithmeticType()) { |
| 6046 | *CompLHSTy = LHS.get()->getType(); |
| 6047 | return compType; |
| 6048 | } |
| 6049 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6050 | // Either ptr - int or ptr - ptr. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6051 | if (LHS.get()->getType()->isAnyPointerType()) { |
| 6052 | QualType lpointee = LHS.get()->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6053 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6054 | // Diagnose bad cases where we step over interface counts. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6055 | if (!checkArithmethicPointerOnNonFragileABI(*this, Loc, LHS.get())) |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6056 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6057 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6058 | // The result type of a pointer-int computation is the pointer type. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6059 | if (RHS.get()->getType()->isIntegerType()) { |
| 6060 | if (!checkArithmeticOpPointerOperand(*this, Loc, LHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6061 | return QualType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6062 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6063 | // Check array bounds for pointer arithemtic |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 6064 | CheckArrayAccess(LHS.get(), RHS.get(), /*ArraySubscriptExpr*/0, |
| 6065 | /*AllowOnePastEnd*/true, /*IndexNegated*/true); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6066 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6067 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
| 6068 | return LHS.get()->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6069 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6070 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6071 | // Handle pointer-pointer subtractions. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6072 | if (const PointerType *RHSPTy |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6073 | = RHS.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6074 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6075 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6076 | if (getLangOptions().CPlusPlus) { |
| 6077 | // Pointee types must be the same: C++ [expr.add] |
| 6078 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6079 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6080 | } |
| 6081 | } else { |
| 6082 | // Pointee types must be compatible C99 6.5.6p3 |
| 6083 | if (!Context.typesAreCompatible( |
| 6084 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6085 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6086 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6087 | return QualType(); |
| 6088 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6089 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6090 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6091 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6092 | LHS.get(), RHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6093 | return QualType(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6094 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6095 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6096 | return Context.getPointerDiffType(); |
| 6097 | } |
| 6098 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6099 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6100 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6101 | } |
| 6102 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6103 | static bool isScopedEnumerationType(QualType T) { |
| 6104 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6105 | return ET->getDecl()->isScoped(); |
| 6106 | return false; |
| 6107 | } |
| 6108 | |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6109 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6110 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6111 | QualType LHSType) { |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6112 | llvm::APSInt Right; |
| 6113 | // Check right/shifter operand |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6114 | if (RHS.get()->isValueDependent() || |
| 6115 | !RHS.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6116 | return; |
| 6117 | |
| 6118 | if (Right.isNegative()) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6119 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6120 | S.PDiag(diag::warn_shift_negative) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6121 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6122 | return; |
| 6123 | } |
| 6124 | llvm::APInt LeftBits(Right.getBitWidth(), |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6125 | S.Context.getTypeSize(LHS.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6126 | if (Right.uge(LeftBits)) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6127 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6128 | S.PDiag(diag::warn_shift_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6129 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6130 | return; |
| 6131 | } |
| 6132 | if (Opc != BO_Shl) |
| 6133 | return; |
| 6134 | |
| 6135 | // When left shifting an ICE which is signed, we can check for overflow which |
| 6136 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 6137 | // integers have defined behavior modulo one more than the maximum value |
| 6138 | // representable in the result type, so never warn for those. |
| 6139 | llvm::APSInt Left; |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6140 | if (LHS.get()->isValueDependent() || |
| 6141 | !LHS.get()->isIntegerConstantExpr(Left, S.Context) || |
| 6142 | LHSType->hasUnsignedIntegerRepresentation()) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6143 | return; |
| 6144 | llvm::APInt ResultBits = |
| 6145 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 6146 | if (LeftBits.uge(ResultBits)) |
| 6147 | return; |
| 6148 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 6149 | Result = Result.shl(Right); |
| 6150 | |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6151 | // Print the bit representation of the signed integer as an unsigned |
| 6152 | // hexadecimal number. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6153 | SmallString<40> HexResult; |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6154 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 6155 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6156 | // If we are only missing a sign bit, this is less likely to result in actual |
| 6157 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 6158 | // expected value. Thus we place this behind a different warning that can be |
| 6159 | // turned off separately if needed. |
| 6160 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6161 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6162 | << HexResult.str() << LHSType |
| 6163 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6164 | return; |
| 6165 | } |
| 6166 | |
| 6167 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6168 | << HexResult.str() << Result.getMinSignedBits() << LHSType |
| 6169 | << Left.getBitWidth() << LHS.get()->getSourceRange() |
| 6170 | << RHS.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6171 | } |
| 6172 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6173 | // C99 6.5.7 |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6174 | QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6175 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6176 | bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6177 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6178 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6179 | // C99 6.5.7p2: Each of the operands shall have integer type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6180 | if (!LHS.get()->getType()->hasIntegerRepresentation() || |
| 6181 | !RHS.get()->getType()->hasIntegerRepresentation()) |
| 6182 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6183 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6184 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6185 | // hasIntegerRepresentation() above instead of this. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6186 | if (isScopedEnumerationType(LHS.get()->getType()) || |
| 6187 | isScopedEnumerationType(RHS.get()->getType())) { |
| 6188 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6189 | } |
| 6190 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6191 | // Vector shifts promote their scalar inputs to vector type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6192 | if (LHS.get()->getType()->isVectorType() || |
| 6193 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6194 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6195 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6196 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6197 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6198 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6199 | // For the LHS, do usual unary conversions, but then reset them away |
| 6200 | // if this is a compound assignment. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6201 | ExprResult OldLHS = LHS; |
| 6202 | LHS = UsualUnaryConversions(LHS.take()); |
| 6203 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6204 | return QualType(); |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6205 | QualType LHSType = LHS.get()->getType(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6206 | if (IsCompAssign) LHS = OldLHS; |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6207 | |
| 6208 | // The RHS is simpler. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6209 | RHS = UsualUnaryConversions(RHS.take()); |
| 6210 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6211 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6212 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6213 | // Sanity-check shift operands |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6214 | DiagnoseBadShiftValues(*this, LHS, RHS, Loc, Opc, LHSType); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6215 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6216 | // "The type of the result is that of the promoted left operand." |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6217 | return LHSType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6218 | } |
| 6219 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6220 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6221 | if (DeclContext *DC = D->getDeclContext()) { |
| 6222 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6223 | return true; |
| 6224 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6225 | return FD->isFunctionTemplateSpecialization(); |
| 6226 | } |
| 6227 | return false; |
| 6228 | } |
| 6229 | |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6230 | /// If two different enums are compared, raise a warning. |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6231 | static void checkEnumComparison(Sema &S, SourceLocation Loc, ExprResult &LHS, |
| 6232 | ExprResult &RHS) { |
| 6233 | QualType LHSStrippedType = LHS.get()->IgnoreParenImpCasts()->getType(); |
| 6234 | QualType RHSStrippedType = RHS.get()->IgnoreParenImpCasts()->getType(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6235 | |
| 6236 | const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>(); |
| 6237 | if (!LHSEnumType) |
| 6238 | return; |
| 6239 | const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>(); |
| 6240 | if (!RHSEnumType) |
| 6241 | return; |
| 6242 | |
| 6243 | // Ignore anonymous enums. |
| 6244 | if (!LHSEnumType->getDecl()->getIdentifier()) |
| 6245 | return; |
| 6246 | if (!RHSEnumType->getDecl()->getIdentifier()) |
| 6247 | return; |
| 6248 | |
| 6249 | if (S.Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) |
| 6250 | return; |
| 6251 | |
| 6252 | S.Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6253 | << LHSStrippedType << RHSStrippedType |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6254 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6255 | } |
| 6256 | |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6257 | /// \brief Diagnose bad pointer comparisons. |
| 6258 | static void diagnoseDistinctPointerComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6259 | ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6260 | bool IsError) { |
| 6261 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_distinct_pointers |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6262 | : diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6263 | << LHS.get()->getType() << RHS.get()->getType() |
| 6264 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6265 | } |
| 6266 | |
| 6267 | /// \brief Returns false if the pointers are converted to a composite type, |
| 6268 | /// true otherwise. |
| 6269 | static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6270 | ExprResult &LHS, ExprResult &RHS) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6271 | // C++ [expr.rel]p2: |
| 6272 | // [...] Pointer conversions (4.10) and qualification |
| 6273 | // conversions (4.4) are performed on pointer operands (or on |
| 6274 | // a pointer operand and a null pointer constant) to bring |
| 6275 | // them to their composite pointer type. [...] |
| 6276 | // |
| 6277 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
| 6278 | // comparisons of pointers. |
| 6279 | |
| 6280 | // C++ [expr.eq]p2: |
| 6281 | // In addition, pointers to members can be compared, or a pointer to |
| 6282 | // member and a null pointer constant. Pointer to member conversions |
| 6283 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6284 | // them to a common type. If one operand is a null pointer constant, |
| 6285 | // the common type is the type of the other operand. Otherwise, the |
| 6286 | // common type is a pointer to member type similar (4.4) to the type |
| 6287 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6288 | // that is the union of the cv-qualification signatures of the operand |
| 6289 | // types. |
| 6290 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6291 | QualType LHSType = LHS.get()->getType(); |
| 6292 | QualType RHSType = RHS.get()->getType(); |
| 6293 | assert((LHSType->isPointerType() && RHSType->isPointerType()) || |
| 6294 | (LHSType->isMemberPointerType() && RHSType->isMemberPointerType())); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6295 | |
| 6296 | bool NonStandardCompositeType = false; |
Richard Trieu | 43dff1b | 2011-09-02 21:44:27 +0000 | [diff] [blame] | 6297 | bool *BoolPtr = S.isSFINAEContext() ? 0 : &NonStandardCompositeType; |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6298 | QualType T = S.FindCompositePointerType(Loc, LHS, RHS, BoolPtr); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6299 | if (T.isNull()) { |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6300 | diagnoseDistinctPointerComparison(S, Loc, LHS, RHS, /*isError*/true); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6301 | return true; |
| 6302 | } |
| 6303 | |
| 6304 | if (NonStandardCompositeType) |
| 6305 | S.Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6306 | << LHSType << RHSType << T << LHS.get()->getSourceRange() |
| 6307 | << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6308 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6309 | LHS = S.ImpCastExprToType(LHS.take(), T, CK_BitCast); |
| 6310 | RHS = S.ImpCastExprToType(RHS.take(), T, CK_BitCast); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6311 | return false; |
| 6312 | } |
| 6313 | |
| 6314 | static void diagnoseFunctionPointerToVoidComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6315 | ExprResult &LHS, |
| 6316 | ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6317 | bool IsError) { |
| 6318 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_fptr_to_void |
| 6319 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6320 | << LHS.get()->getType() << RHS.get()->getType() |
| 6321 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6322 | } |
| 6323 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6324 | // C99 6.5.8, C++ [expr.rel] |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6325 | QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6326 | SourceLocation Loc, unsigned OpaqueOpc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6327 | bool IsRelational) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6328 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/true); |
| 6329 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6330 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6331 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6332 | // Handle vector comparisons separately. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6333 | if (LHS.get()->getType()->isVectorType() || |
| 6334 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6335 | return CheckVectorCompareOperands(LHS, RHS, Loc, IsRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6336 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6337 | QualType LHSType = LHS.get()->getType(); |
| 6338 | QualType RHSType = RHS.get()->getType(); |
Benjamin Kramer | fec0959 | 2011-09-03 08:46:20 +0000 | [diff] [blame] | 6339 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6340 | Expr *LHSStripped = LHS.get()->IgnoreParenImpCasts(); |
| 6341 | Expr *RHSStripped = RHS.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6342 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6343 | checkEnumComparison(*this, Loc, LHS, RHS); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6344 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6345 | if (!LHSType->hasFloatingRepresentation() && |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6346 | !(LHSType->isBlockPointerType() && IsRelational) && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6347 | !LHS.get()->getLocStart().isMacroID() && |
| 6348 | !RHS.get()->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6349 | // For non-floating point types, check for self-comparisons of the form |
| 6350 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6351 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6352 | // |
| 6353 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6354 | // expansion. Also don't warn about comparisons which are only self |
| 6355 | // comparisons within a template specialization. The warnings should catch |
| 6356 | // obvious cases in the definition of the template anyways. The idea is to |
| 6357 | // warn when the typed comparison operator will always evaluate to the same |
| 6358 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6359 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6360 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6361 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6362 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6363 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6364 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6365 | << (Opc == BO_EQ |
| 6366 | || Opc == BO_LE |
| 6367 | || Opc == BO_GE)); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6368 | } else if (LHSType->isArrayType() && RHSType->isArrayType() && |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6369 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6370 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6371 | // what is it always going to eval to? |
| 6372 | char always_evals_to; |
| 6373 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6374 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6375 | always_evals_to = 0; // false |
| 6376 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6377 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6378 | always_evals_to = 1; // true |
| 6379 | break; |
| 6380 | default: |
| 6381 | // best we can say is 'a constant' |
| 6382 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6383 | break; |
| 6384 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6385 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6386 | << 1 // array |
| 6387 | << always_evals_to); |
| 6388 | } |
| 6389 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6390 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6391 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6392 | if (isa<CastExpr>(LHSStripped)) |
| 6393 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6394 | if (isa<CastExpr>(RHSStripped)) |
| 6395 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6396 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6397 | // Warn about comparisons against a string constant (unless the other |
| 6398 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6399 | Expr *literalString = 0; |
| 6400 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6401 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6402 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6403 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6404 | literalString = LHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6405 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6406 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 6407 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6408 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6409 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6410 | literalString = RHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6411 | literalStringStripped = RHSStripped; |
| 6412 | } |
| 6413 | |
| 6414 | if (literalString) { |
| 6415 | std::string resultComparison; |
| 6416 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6417 | case BO_LT: resultComparison = ") < 0"; break; |
| 6418 | case BO_GT: resultComparison = ") > 0"; break; |
| 6419 | case BO_LE: resultComparison = ") <= 0"; break; |
| 6420 | case BO_GE: resultComparison = ") >= 0"; break; |
| 6421 | case BO_EQ: resultComparison = ") == 0"; break; |
| 6422 | case BO_NE: resultComparison = ") != 0"; break; |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6423 | default: llvm_unreachable("Invalid comparison operator"); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6424 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6425 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6426 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 6427 | PDiag(diag::warn_stringcompare) |
| 6428 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 6429 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6430 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 6431 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6432 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6433 | // C99 6.5.8p3 / C99 6.5.9p4 |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6434 | if (LHS.get()->getType()->isArithmeticType() && |
| 6435 | RHS.get()->getType()->isArithmeticType()) { |
| 6436 | UsualArithmeticConversions(LHS, RHS); |
| 6437 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6438 | return QualType(); |
| 6439 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6440 | else { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6441 | LHS = UsualUnaryConversions(LHS.take()); |
| 6442 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6443 | return QualType(); |
| 6444 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6445 | RHS = UsualUnaryConversions(RHS.take()); |
| 6446 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6447 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6448 | } |
| 6449 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6450 | LHSType = LHS.get()->getType(); |
| 6451 | RHSType = RHS.get()->getType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6452 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6453 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 6454 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6455 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6456 | if (IsRelational) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6457 | if (LHSType->isRealType() && RHSType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6458 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6459 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 6460 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6461 | if (LHSType->hasFloatingRepresentation()) |
| 6462 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6463 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6464 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6465 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6466 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6467 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6468 | bool LHSIsNull = LHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6469 | Expr::NPC_ValueDependentIsNull); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6470 | bool RHSIsNull = RHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6471 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6472 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6473 | // All of the following pointer-related warnings are GCC extensions, except |
| 6474 | // when handling null pointer constants. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6475 | if (LHSType->isPointerType() && RHSType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6476 | QualType LCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6477 | LHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6478 | QualType RCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6479 | RHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6480 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6481 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6482 | if (LCanPointeeTy == RCanPointeeTy) |
| 6483 | return ResultTy; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6484 | if (!IsRelational && |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6485 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6486 | // Valid unless comparison between non-null pointer and function pointer |
| 6487 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6488 | // In a SFINAE context, we treat this as a hard error to maintain |
| 6489 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6490 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6491 | && !LHSIsNull && !RHSIsNull) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6492 | diagnoseFunctionPointerToVoidComparison( |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6493 | *this, Loc, LHS, RHS, /*isError*/ isSFINAEContext()); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6494 | |
| 6495 | if (isSFINAEContext()) |
| 6496 | return QualType(); |
| 6497 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6498 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6499 | return ResultTy; |
| 6500 | } |
| 6501 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6502 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6503 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6504 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6505 | else |
| 6506 | return ResultTy; |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6507 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6508 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 6509 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 6510 | RCanPointeeTy.getUnqualifiedType())) { |
| 6511 | // Valid unless a relational comparison of function pointers |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6512 | if (IsRelational && LCanPointeeTy->isFunctionType()) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6513 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6514 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6515 | << RHS.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6516 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6517 | } else if (!IsRelational && |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6518 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6519 | // Valid unless comparison between non-null pointer and function pointer |
| 6520 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6521 | && !LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6522 | diagnoseFunctionPointerToVoidComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6523 | /*isError*/false); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6524 | } else { |
| 6525 | // Invalid |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6526 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6527 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6528 | if (LCanPointeeTy != RCanPointeeTy) { |
| 6529 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6530 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6531 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6532 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6533 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6534 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 6535 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6536 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6537 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6538 | // Comparison of nullptr_t with itself. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6539 | if (LHSType->isNullPtrType() && RHSType->isNullPtrType()) |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6540 | return ResultTy; |
| 6541 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6542 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6543 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6544 | if (RHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6545 | ((LHSType->isAnyPointerType() || LHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6546 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6547 | (LHSType->isMemberPointerType() || LHSType->isBlockPointerType())))) { |
| 6548 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 6549 | LHSType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6550 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6551 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6552 | return ResultTy; |
| 6553 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6554 | if (LHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6555 | ((RHSType->isAnyPointerType() || RHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6556 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6557 | (RHSType->isMemberPointerType() || RHSType->isBlockPointerType())))) { |
| 6558 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 6559 | RHSType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6560 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6561 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6562 | return ResultTy; |
| 6563 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6564 | |
| 6565 | // Comparison of member pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6566 | if (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6567 | LHSType->isMemberPointerType() && RHSType->isMemberPointerType()) { |
| 6568 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6569 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6570 | else |
| 6571 | return ResultTy; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6572 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6573 | |
| 6574 | // Handle scoped enumeration types specifically, since they don't promote |
| 6575 | // to integers. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6576 | if (LHS.get()->getType()->isEnumeralType() && |
| 6577 | Context.hasSameUnqualifiedType(LHS.get()->getType(), |
| 6578 | RHS.get()->getType())) |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6579 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6580 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6581 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6582 | // Handle block pointer types. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6583 | if (!IsRelational && LHSType->isBlockPointerType() && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6584 | RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6585 | QualType lpointee = LHSType->castAs<BlockPointerType>()->getPointeeType(); |
| 6586 | QualType rpointee = RHSType->castAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6587 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6588 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 6589 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6590 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6591 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6592 | << RHS.get()->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6593 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6594 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6595 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6596 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6597 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6598 | // Allow block pointers to be compared with null pointer constants. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6599 | if (!IsRelational |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6600 | && ((LHSType->isBlockPointerType() && RHSType->isPointerType()) |
| 6601 | || (LHSType->isPointerType() && RHSType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6602 | if (!LHSIsNull && !RHSIsNull) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6603 | if (!((RHSType->isPointerType() && RHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6604 | ->getPointeeType()->isVoidType()) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6605 | || (LHSType->isPointerType() && LHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6606 | ->getPointeeType()->isVoidType()))) |
| 6607 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6608 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6609 | << RHS.get()->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6610 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6611 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6612 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 6613 | RHSType->isPointerType() ? CK_BitCast |
| 6614 | : CK_AnyPointerToBlockPointerCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6615 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6616 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 6617 | LHSType->isPointerType() ? CK_BitCast |
| 6618 | : CK_AnyPointerToBlockPointerCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6619 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6620 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6621 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6622 | if (LHSType->isObjCObjectPointerType() || |
| 6623 | RHSType->isObjCObjectPointerType()) { |
| 6624 | const PointerType *LPT = LHSType->getAs<PointerType>(); |
| 6625 | const PointerType *RPT = RHSType->getAs<PointerType>(); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6626 | if (LPT || RPT) { |
| 6627 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 6628 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6629 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 6630 | if (!LPtrToVoid && !RPtrToVoid && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6631 | !Context.typesAreCompatible(LHSType, RHSType)) { |
| 6632 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6633 | /*isError*/false); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 6634 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6635 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6636 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 6637 | RPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6638 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6639 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 6640 | LPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6641 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 6642 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6643 | if (LHSType->isObjCObjectPointerType() && |
| 6644 | RHSType->isObjCObjectPointerType()) { |
| 6645 | if (!Context.areComparableObjCPointerTypes(LHSType, RHSType)) |
| 6646 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6647 | /*isError*/false); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6648 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6649 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6650 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6651 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6652 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 6653 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 6654 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6655 | if ((LHSType->isAnyPointerType() && RHSType->isIntegerType()) || |
| 6656 | (LHSType->isIntegerType() && RHSType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6657 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6658 | bool isError = false; |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6659 | if ((LHSIsNull && LHSType->isIntegerType()) || |
| 6660 | (RHSIsNull && RHSType->isIntegerType())) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6661 | if (IsRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6662 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6663 | } else if (IsRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6664 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6665 | else if (getLangOptions().CPlusPlus) { |
| 6666 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 6667 | isError = true; |
| 6668 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6669 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6670 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6671 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6672 | Diag(Loc, DiagID) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6673 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 6674 | << RHS.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6675 | if (isError) |
| 6676 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6677 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6678 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6679 | if (LHSType->isIntegerType()) |
| 6680 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6681 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6682 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6683 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6684 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6685 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6686 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6687 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6688 | // Handle block pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6689 | if (!IsRelational && RHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6690 | && LHSType->isBlockPointerType() && RHSType->isIntegerType()) { |
| 6691 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6692 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6693 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6694 | if (!IsRelational && LHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6695 | && LHSType->isIntegerType() && RHSType->isBlockPointerType()) { |
| 6696 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6697 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6698 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6699 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6700 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6701 | } |
| 6702 | |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 6703 | |
| 6704 | // Return a signed type that is of identical size and number of elements. |
| 6705 | // For floating point vectors, return an integer type of identical size |
| 6706 | // and number of elements. |
| 6707 | QualType Sema::GetSignedVectorType(QualType V) { |
| 6708 | const VectorType *VTy = V->getAs<VectorType>(); |
| 6709 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
| 6710 | if (TypeSize == Context.getTypeSize(Context.CharTy)) |
| 6711 | return Context.getExtVectorType(Context.CharTy, VTy->getNumElements()); |
| 6712 | else if (TypeSize == Context.getTypeSize(Context.ShortTy)) |
| 6713 | return Context.getExtVectorType(Context.ShortTy, VTy->getNumElements()); |
| 6714 | else if (TypeSize == Context.getTypeSize(Context.IntTy)) |
| 6715 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
| 6716 | else if (TypeSize == Context.getTypeSize(Context.LongTy)) |
| 6717 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 6718 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
| 6719 | "Unhandled vector element size in vector compare"); |
| 6720 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 6721 | } |
| 6722 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6723 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6724 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6725 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 6726 | /// types. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6727 | QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6728 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6729 | bool IsRelational) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6730 | // Check to make sure we're operating on vectors of the same type and width, |
| 6731 | // Allowing one side to be a scalar of element type. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6732 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, /*isCompAssign*/false); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6733 | if (vType.isNull()) |
| 6734 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6735 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6736 | QualType LHSType = LHS.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6737 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6738 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 6739 | // bool for C++, int for C |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 6740 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6741 | return Context.getLogicalOperationType(); |
| 6742 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6743 | // For non-floating point types, check for self-comparisons of the form |
| 6744 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6745 | // often indicate logic errors in the program. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6746 | if (!LHSType->hasFloatingRepresentation()) { |
Richard Smith | 9c129f8 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 6747 | if (DeclRefExpr* DRL |
| 6748 | = dyn_cast<DeclRefExpr>(LHS.get()->IgnoreParenImpCasts())) |
| 6749 | if (DeclRefExpr* DRR |
| 6750 | = dyn_cast<DeclRefExpr>(RHS.get()->IgnoreParenImpCasts())) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6751 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6752 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6753 | PDiag(diag::warn_comparison_always) |
| 6754 | << 0 // self- |
| 6755 | << 2 // "a constant" |
| 6756 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6757 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6758 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6759 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6760 | if (!IsRelational && LHSType->hasFloatingRepresentation()) { |
David Blaikie | 52e4c60 | 2012-01-16 05:16:03 +0000 | [diff] [blame] | 6761 | assert (RHS.get()->getType()->hasFloatingRepresentation()); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6762 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6763 | } |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 6764 | |
| 6765 | // Return a signed type for the vector. |
| 6766 | return GetSignedVectorType(LHSType); |
| 6767 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6768 | |
Tanya Lattner | b0f9dd2 | 2012-01-19 01:16:16 +0000 | [diff] [blame] | 6769 | QualType Sema::CheckVectorLogicalOperands(ExprResult &LHS, ExprResult &RHS, |
| 6770 | SourceLocation Loc) { |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 6771 | // Ensure that either both operands are of the same vector type, or |
| 6772 | // one operand is of a vector type and the other is of its element type. |
| 6773 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, false); |
| 6774 | if (vType.isNull() || vType->isFloatingType()) |
| 6775 | return InvalidOperands(Loc, LHS, RHS); |
| 6776 | |
| 6777 | return GetSignedVectorType(LHS.get()->getType()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6778 | } |
| 6779 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6780 | inline QualType Sema::CheckBitwiseOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6781 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6782 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6783 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6784 | if (LHS.get()->getType()->isVectorType() || |
| 6785 | RHS.get()->getType()->isVectorType()) { |
| 6786 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 6787 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6788 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6789 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6790 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6791 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6792 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6793 | ExprResult LHSResult = Owned(LHS), RHSResult = Owned(RHS); |
| 6794 | QualType compType = UsualArithmeticConversions(LHSResult, RHSResult, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6795 | IsCompAssign); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6796 | if (LHSResult.isInvalid() || RHSResult.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6797 | return QualType(); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6798 | LHS = LHSResult.take(); |
| 6799 | RHS = RHSResult.take(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6800 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6801 | if (LHS.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 6802 | RHS.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6803 | return compType; |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6804 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6805 | } |
| 6806 | |
| 6807 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6808 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6809 | |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 6810 | // Check vector operands differently. |
| 6811 | if (LHS.get()->getType()->isVectorType() || RHS.get()->getType()->isVectorType()) |
| 6812 | return CheckVectorLogicalOperands(LHS, RHS, Loc); |
| 6813 | |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6814 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 6815 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 6816 | // is a constant. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6817 | if (LHS.get()->getType()->isIntegerType() && |
| 6818 | !LHS.get()->getType()->isBooleanType() && |
| 6819 | RHS.get()->getType()->isIntegerType() && !RHS.get()->isValueDependent() && |
Richard Trieu | e5adf59 | 2011-07-15 00:00:51 +0000 | [diff] [blame] | 6820 | // Don't warn in macros or template instantiations. |
| 6821 | !Loc.isMacroID() && ActiveTemplateInstantiations.empty()) { |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6822 | // If the RHS can be constant folded, and if it constant folds to something |
| 6823 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 6824 | // happened to fold to true/false) then warn. |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6825 | // Parens on the RHS are ignored. |
Richard Smith | 909c555 | 2011-10-16 23:01:09 +0000 | [diff] [blame] | 6826 | llvm::APSInt Result; |
| 6827 | if (RHS.get()->EvaluateAsInt(Result, Context)) |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6828 | if ((getLangOptions().Bool && !RHS.get()->getType()->isBooleanType()) || |
Richard Smith | 909c555 | 2011-10-16 23:01:09 +0000 | [diff] [blame] | 6829 | (Result != 0 && Result != 1)) { |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6830 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6831 | << RHS.get()->getSourceRange() |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6832 | << (Opc == BO_LAnd ? "&&" : "||"); |
| 6833 | // Suggest replacing the logical operator with the bitwise version |
| 6834 | Diag(Loc, diag::note_logical_instead_of_bitwise_change_operator) |
| 6835 | << (Opc == BO_LAnd ? "&" : "|") |
| 6836 | << FixItHint::CreateReplacement(SourceRange( |
| 6837 | Loc, Lexer::getLocForEndOfToken(Loc, 0, getSourceManager(), |
| 6838 | getLangOptions())), |
| 6839 | Opc == BO_LAnd ? "&" : "|"); |
| 6840 | if (Opc == BO_LAnd) |
| 6841 | // Suggest replacing "Foo() && kNonZero" with "Foo()" |
| 6842 | Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant) |
| 6843 | << FixItHint::CreateRemoval( |
| 6844 | SourceRange( |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6845 | Lexer::getLocForEndOfToken(LHS.get()->getLocEnd(), |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6846 | 0, getSourceManager(), |
| 6847 | getLangOptions()), |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6848 | RHS.get()->getLocEnd())); |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6849 | } |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6850 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6851 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6852 | if (!Context.getLangOptions().CPlusPlus) { |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6853 | LHS = UsualUnaryConversions(LHS.take()); |
| 6854 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6855 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6856 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6857 | RHS = UsualUnaryConversions(RHS.take()); |
| 6858 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6859 | return QualType(); |
| 6860 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6861 | if (!LHS.get()->getType()->isScalarType() || |
| 6862 | !RHS.get()->getType()->isScalarType()) |
| 6863 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6864 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6865 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 6866 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6867 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6868 | // The following is safe because we only use this method for |
| 6869 | // non-overloadable operands. |
| 6870 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6871 | // C++ [expr.log.and]p1 |
| 6872 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6873 | // The operands are both contextually converted to type bool. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6874 | ExprResult LHSRes = PerformContextuallyConvertToBool(LHS.get()); |
| 6875 | if (LHSRes.isInvalid()) |
| 6876 | return InvalidOperands(Loc, LHS, RHS); |
| 6877 | LHS = move(LHSRes); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6878 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 6879 | ExprResult RHSRes = PerformContextuallyConvertToBool(RHS.get()); |
| 6880 | if (RHSRes.isInvalid()) |
| 6881 | return InvalidOperands(Loc, LHS, RHS); |
| 6882 | RHS = move(RHSRes); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6883 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6884 | // C++ [expr.log.and]p2 |
| 6885 | // C++ [expr.log.or]p2 |
| 6886 | // The result is a bool. |
| 6887 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6888 | } |
| 6889 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6890 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 6891 | /// is a read-only property; return true if so. A readonly property expression |
| 6892 | /// depends on various declarations and thus must be treated specially. |
| 6893 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6894 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6895 | const ObjCPropertyRefExpr *PropExpr = dyn_cast<ObjCPropertyRefExpr>(E); |
| 6896 | if (!PropExpr) return false; |
| 6897 | if (PropExpr->isImplicitProperty()) return false; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6898 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6899 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6900 | QualType BaseType = PropExpr->isSuperReceiver() ? |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6901 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 6902 | PropExpr->getBase()->getType(); |
| 6903 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6904 | if (const ObjCObjectPointerType *OPT = |
| 6905 | BaseType->getAsObjCInterfacePointerType()) |
| 6906 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 6907 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 6908 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6909 | return false; |
| 6910 | } |
| 6911 | |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6912 | static bool IsConstProperty(Expr *E, Sema &S) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6913 | const ObjCPropertyRefExpr *PropExpr = dyn_cast<ObjCPropertyRefExpr>(E); |
| 6914 | if (!PropExpr) return false; |
| 6915 | if (PropExpr->isImplicitProperty()) return false; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6916 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6917 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6918 | QualType T = PDecl->getType().getNonReferenceType(); |
| 6919 | return T.isConstQualified(); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6920 | } |
| 6921 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6922 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 6923 | const MemberExpr *ME = dyn_cast<MemberExpr>(E); |
| 6924 | if (!ME) return false; |
| 6925 | if (!isa<FieldDecl>(ME->getMemberDecl())) return false; |
| 6926 | ObjCMessageExpr *Base = |
| 6927 | dyn_cast<ObjCMessageExpr>(ME->getBase()->IgnoreParenImpCasts()); |
| 6928 | if (!Base) return false; |
| 6929 | return Base->getMethodDecl() != 0; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6930 | } |
| 6931 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6932 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 6933 | /// emit an error and return true. If so, return false. |
| 6934 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6935 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6936 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6937 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6938 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 6939 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6940 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 6941 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6942 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 6943 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6944 | if (IsLV == Expr::MLV_Valid) |
| 6945 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6946 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6947 | unsigned Diag = 0; |
| 6948 | bool NeedType = false; |
| 6949 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6950 | case Expr::MLV_ConstQualified: |
| 6951 | Diag = diag::err_typecheck_assign_const; |
| 6952 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6953 | // In ARC, use some specialized diagnostics for occasions where we |
| 6954 | // infer 'const'. These are always pseudo-strong variables. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6955 | if (S.getLangOptions().ObjCAutoRefCount) { |
| 6956 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 6957 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 6958 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 6959 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6960 | // Use the normal diagnostic if it's pseudo-__strong but the |
| 6961 | // user actually wrote 'const'. |
| 6962 | if (var->isARCPseudoStrong() && |
| 6963 | (!var->getTypeSourceInfo() || |
| 6964 | !var->getTypeSourceInfo()->getType().isConstQualified())) { |
| 6965 | // There are two pseudo-strong cases: |
| 6966 | // - self |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6967 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 6968 | if (method && var == method->getSelfDecl()) |
Ted Kremenek | 2bbcd5c | 2011-11-14 21:59:25 +0000 | [diff] [blame] | 6969 | Diag = method->isClassMethod() |
| 6970 | ? diag::err_typecheck_arc_assign_self_class_method |
| 6971 | : diag::err_typecheck_arc_assign_self; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6972 | |
| 6973 | // - fast enumeration variables |
| 6974 | else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6975 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6976 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6977 | SourceRange Assign; |
| 6978 | if (Loc != OrigLoc) |
| 6979 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 6980 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 6981 | // We need to preserve the AST regardless, so migration tool |
| 6982 | // can do its job. |
| 6983 | return false; |
| 6984 | } |
| 6985 | } |
| 6986 | } |
| 6987 | |
| 6988 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6989 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6990 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 6991 | NeedType = true; |
| 6992 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6993 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6994 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 6995 | NeedType = true; |
| 6996 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 6997 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6998 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 6999 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7000 | case Expr::MLV_Valid: |
| 7001 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7002 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7003 | case Expr::MLV_MemberFunction: |
| 7004 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7005 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 7006 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7007 | case Expr::MLV_IncompleteType: |
| 7008 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 7009 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7010 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 7011 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7012 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7013 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 7014 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 7015 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7016 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 7017 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 7018 | case Expr::MLV_ReadonlyProperty: |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 7019 | case Expr::MLV_NoSetterProperty: |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7020 | llvm_unreachable("readonly properties should be processed differently"); |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7021 | case Expr::MLV_InvalidMessageExpression: |
| 7022 | Diag = diag::error_readonly_message_assignment; |
| 7023 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 7024 | case Expr::MLV_SubObjCPropertySetting: |
| 7025 | Diag = diag::error_no_subobject_property_setting; |
| 7026 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7027 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7028 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7029 | SourceRange Assign; |
| 7030 | if (Loc != OrigLoc) |
| 7031 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7032 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7033 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7034 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7035 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7036 | return true; |
| 7037 | } |
| 7038 | |
| 7039 | |
| 7040 | |
| 7041 | // C99 6.5.16.1 |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7042 | QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7043 | SourceLocation Loc, |
| 7044 | QualType CompoundType) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7045 | assert(!LHSExpr->hasPlaceholderType(BuiltinType::PseudoObject)); |
| 7046 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7047 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7048 | if (CheckForModifiableLvalue(LHSExpr, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7049 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7050 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7051 | QualType LHSType = LHSExpr->getType(); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7052 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : |
| 7053 | CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7054 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7055 | if (CompoundType.isNull()) { |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7056 | QualType LHSTy(LHSType); |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7057 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7058 | if (RHS.isInvalid()) |
| 7059 | return QualType(); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7060 | // Special case of NSObject attributes on c-style pointer types. |
| 7061 | if (ConvTy == IncompatiblePointer && |
| 7062 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7063 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7064 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7065 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7066 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7067 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7068 | if (ConvTy == Compatible && |
Fariborz Jahanian | 466f45a | 2012-01-24 19:40:13 +0000 | [diff] [blame] | 7069 | LHSType->isObjCObjectType()) |
Fariborz Jahanian | 7b383e4 | 2012-01-24 18:05:45 +0000 | [diff] [blame] | 7070 | Diag(Loc, diag::err_objc_object_assignment) |
| 7071 | << LHSType; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7072 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7073 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 7074 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 7075 | // instead of "x += 4". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7076 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7077 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 7078 | RHSCheck = ICE->getSubExpr(); |
| 7079 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7080 | if ((UO->getOpcode() == UO_Plus || |
| 7081 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7082 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7083 | // Only if the two operators are exactly adjacent. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 7084 | Loc.getLocWithOffset(1) == UO->getOperatorLoc() && |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7085 | // And there is a space or other character before the subexpr of the |
| 7086 | // unary +/-. We don't want to warn on "x=-1". |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 7087 | Loc.getLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7088 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7089 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7090 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7091 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7092 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7093 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7094 | |
| 7095 | if (ConvTy == Compatible) { |
| 7096 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7097 | checkRetainCycles(LHSExpr, RHS.get()); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 7098 | else if (getLangOptions().ObjCAutoRefCount) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7099 | checkUnsafeExprAssigns(Loc, LHSExpr, RHS.get()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7100 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7101 | } else { |
| 7102 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7103 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7104 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7105 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7106 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7107 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7108 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7109 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7110 | CheckForNullPointerDereference(*this, LHSExpr); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7111 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7112 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7113 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7114 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7115 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7116 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7117 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 7118 | // operand. |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7119 | return (getLangOptions().CPlusPlus |
| 7120 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7121 | } |
| 7122 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7123 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7124 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7125 | SourceLocation Loc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7126 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 7127 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7128 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 7129 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7130 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7131 | return QualType(); |
| 7132 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7133 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7134 | // operands, but not unary promotions. |
| 7135 | // C++'s comma does not do any conversions at all (C++ [expr.comma]p1). |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7136 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7137 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7138 | // containing site to determine what should be done with the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7139 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 7140 | if (LHS.isInvalid()) |
| 7141 | return QualType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7142 | |
| 7143 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7144 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 7145 | if (RHS.isInvalid()) |
| 7146 | return QualType(); |
| 7147 | if (!RHS.get()->getType()->isVoidType()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7148 | S.RequireCompleteType(Loc, RHS.get()->getType(), |
| 7149 | diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7150 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7151 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7152 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7153 | } |
| 7154 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7155 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7156 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7157 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7158 | ExprValueKind &VK, |
| 7159 | SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7160 | bool IsInc, bool IsPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7161 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7162 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7163 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7164 | QualType ResType = Op->getType(); |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 7165 | // Atomic types can be used for increment / decrement where the non-atomic |
| 7166 | // versions can, so ignore the _Atomic() specifier for the purpose of |
| 7167 | // checking. |
| 7168 | if (const AtomicType *ResAtomicType = ResType->getAs<AtomicType>()) |
| 7169 | ResType = ResAtomicType->getValueType(); |
| 7170 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7171 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7172 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7173 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7174 | // Decrement of bool is not allowed. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7175 | if (!IsInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7176 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7177 | return QualType(); |
| 7178 | } |
| 7179 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7180 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7181 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7182 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7183 | } else if (ResType->isAnyPointerType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7184 | // C99 6.5.2.4p2, 6.5.6p2 |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7185 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7186 | return QualType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7187 | |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7188 | // Diagnose bad cases where we step over interface counts. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 7189 | else if (!checkArithmethicPointerOnNonFragileABI(S, OpLoc, Op)) |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7190 | return QualType(); |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7191 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7192 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7193 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7194 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7195 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7196 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7197 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7198 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7199 | IsInc, IsPrefix); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7200 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 7201 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7202 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7203 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7204 | << ResType << int(IsInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7205 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7206 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7207 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 7208 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7209 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7210 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7211 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7212 | // (in C or with postfix), the increment is the unqualified type of the |
| 7213 | // operand. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7214 | if (IsPrefix && S.getLangOptions().CPlusPlus) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7215 | VK = VK_LValue; |
| 7216 | return ResType; |
| 7217 | } else { |
| 7218 | VK = VK_RValue; |
| 7219 | return ResType.getUnqualifiedType(); |
| 7220 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7221 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7222 | |
| 7223 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7224 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7225 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7226 | /// where the declaration is needed for type checking. We only need to |
| 7227 | /// handle cases when the expression references a function designator |
| 7228 | /// or is an lvalue. Here are some examples: |
| 7229 | /// - &(x) => x |
| 7230 | /// - &*****f => f for f a function designator. |
| 7231 | /// - &s.xx => s |
| 7232 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7233 | /// - *(x + 1) -> x, if x is an array |
| 7234 | /// - &"123"[2] -> 0 |
| 7235 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7236 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7237 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7238 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7239 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7240 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7241 | // If this is an arrow operator, the address is an offset from |
| 7242 | // the base's value, so the object the base refers to is |
| 7243 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7244 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7245 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7246 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7247 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7248 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7249 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7250 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7251 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7252 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7253 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7254 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7255 | } |
| 7256 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7257 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7258 | case Stmt::UnaryOperatorClass: { |
| 7259 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7260 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7261 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7262 | case UO_Real: |
| 7263 | case UO_Imag: |
| 7264 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7265 | return getPrimaryDecl(UO->getSubExpr()); |
| 7266 | default: |
| 7267 | return 0; |
| 7268 | } |
| 7269 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7270 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7271 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7272 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7273 | // If the result of an implicit cast is an l-value, we care about |
| 7274 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7275 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7276 | default: |
| 7277 | return 0; |
| 7278 | } |
| 7279 | } |
| 7280 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7281 | namespace { |
| 7282 | enum { |
| 7283 | AO_Bit_Field = 0, |
| 7284 | AO_Vector_Element = 1, |
| 7285 | AO_Property_Expansion = 2, |
| 7286 | AO_Register_Variable = 3, |
| 7287 | AO_No_Error = 4 |
| 7288 | }; |
| 7289 | } |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7290 | /// \brief Diagnose invalid operand for address of operations. |
| 7291 | /// |
| 7292 | /// \param Type The type of operand which cannot have its address taken. |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7293 | static void diagnoseAddressOfInvalidType(Sema &S, SourceLocation Loc, |
| 7294 | Expr *E, unsigned Type) { |
| 7295 | S.Diag(Loc, diag::err_typecheck_address_of) << Type << E->getSourceRange(); |
| 7296 | } |
| 7297 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7298 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7299 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7300 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7301 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7302 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7303 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7304 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7305 | static QualType CheckAddressOfOperand(Sema &S, ExprResult &OrigOp, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7306 | SourceLocation OpLoc) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7307 | if (const BuiltinType *PTy = OrigOp.get()->getType()->getAsPlaceholderType()){ |
| 7308 | if (PTy->getKind() == BuiltinType::Overload) { |
| 7309 | if (!isa<OverloadExpr>(OrigOp.get()->IgnoreParens())) { |
| 7310 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 7311 | << OrigOp.get()->getSourceRange(); |
| 7312 | return QualType(); |
| 7313 | } |
| 7314 | |
| 7315 | return S.Context.OverloadTy; |
| 7316 | } |
| 7317 | |
| 7318 | if (PTy->getKind() == BuiltinType::UnknownAny) |
| 7319 | return S.Context.UnknownAnyTy; |
| 7320 | |
| 7321 | if (PTy->getKind() == BuiltinType::BoundMember) { |
| 7322 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 7323 | << OrigOp.get()->getSourceRange(); |
Douglas Gregor | 44efed0 | 2011-10-09 19:10:41 +0000 | [diff] [blame] | 7324 | return QualType(); |
| 7325 | } |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7326 | |
| 7327 | OrigOp = S.CheckPlaceholderExpr(OrigOp.take()); |
| 7328 | if (OrigOp.isInvalid()) return QualType(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7329 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7330 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7331 | if (OrigOp.get()->isTypeDependent()) |
| 7332 | return S.Context.DependentTy; |
| 7333 | |
| 7334 | assert(!OrigOp.get()->getType()->isPlaceholderType()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7335 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7336 | // Make sure to ignore parentheses in subsequent checks |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7337 | Expr *op = OrigOp.get()->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 7338 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7339 | if (S.getLangOptions().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7340 | // Implement C99-only parts of addressof rules. |
| 7341 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7342 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7343 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 7344 | // (assuming the deref expression is valid). |
| 7345 | return uOp->getSubExpr()->getType(); |
| 7346 | } |
| 7347 | // Technically, there should be a check for array subscript |
| 7348 | // expressions here, but the result of one is always an lvalue anyway. |
| 7349 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7350 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7351 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7352 | unsigned AddressOfError = AO_No_Error; |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 7353 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7354 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7355 | bool sfinae = S.isSFINAEContext(); |
| 7356 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 7357 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7358 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7359 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7360 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7361 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7362 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7363 | } else if (lval == Expr::LV_MemberFunction) { |
| 7364 | // If it's an instance method, make a member pointer. |
| 7365 | // The expression must have exactly the form &A::foo. |
| 7366 | |
| 7367 | // If the underlying expression isn't a decl ref, give up. |
| 7368 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7369 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7370 | << OrigOp.get()->getSourceRange(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7371 | return QualType(); |
| 7372 | } |
| 7373 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 7374 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 7375 | |
| 7376 | // The id-expression was parenthesized. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7377 | if (OrigOp.get() != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7378 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7379 | << OrigOp.get()->getSourceRange(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7380 | |
| 7381 | // The method was named without a qualifier. |
| 7382 | } else if (!DRE->getQualifier()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7383 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7384 | << op->getSourceRange(); |
| 7385 | } |
| 7386 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7387 | return S.Context.getMemberPointerType(op->getType(), |
| 7388 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7389 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7390 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7391 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7392 | if (!op->getType()->isFunctionType()) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7393 | // Use a special diagnostic for loads from property references. |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 7394 | if (isa<PseudoObjectExpr>(op)) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7395 | AddressOfError = AO_Property_Expansion; |
| 7396 | } else { |
| 7397 | // FIXME: emit more specific diag... |
| 7398 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 7399 | << op->getSourceRange(); |
| 7400 | return QualType(); |
| 7401 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7402 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7403 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7404 | // The operand cannot be a bit-field |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7405 | AddressOfError = AO_Bit_Field; |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7406 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7407 | // The operand cannot be an element of a vector |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7408 | AddressOfError = AO_Vector_Element; |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7409 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7410 | // We have an lvalue with a decl. Make sure the decl is not declared |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7411 | // with the register storage-class specifier. |
| 7412 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 7413 | // in C++ it is not error to take address of a register |
| 7414 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7415 | if (vd->getStorageClass() == SC_Register && |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7416 | !S.getLangOptions().CPlusPlus) { |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7417 | AddressOfError = AO_Register_Variable; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7418 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7419 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7420 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7421 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 7422 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7423 | // Could be a pointer to member, though, if there is an explicit |
| 7424 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 7425 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7426 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7427 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7428 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7429 | S.Diag(OpLoc, |
| 7430 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7431 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7432 | return QualType(); |
| 7433 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7434 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 7435 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 7436 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7437 | return S.Context.getMemberPointerType(op->getType(), |
| 7438 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7439 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7440 | } |
Eli Friedman | 7b2f51c | 2011-08-26 20:28:17 +0000 | [diff] [blame] | 7441 | } else if (!isa<FunctionDecl>(dcl) && !isa<NonTypeTemplateParmDecl>(dcl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7442 | llvm_unreachable("Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7443 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7444 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7445 | if (AddressOfError != AO_No_Error) { |
| 7446 | diagnoseAddressOfInvalidType(S, OpLoc, op, AddressOfError); |
| 7447 | return QualType(); |
| 7448 | } |
| 7449 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7450 | if (lval == Expr::LV_IncompleteVoidType) { |
| 7451 | // Taking the address of a void variable is technically illegal, but we |
| 7452 | // allow it in cases which are otherwise valid. |
| 7453 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7454 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7455 | } |
| 7456 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7457 | // If the operand has type "type", the result has type "pointer to type". |
Douglas Gregor | 8f70ddb | 2010-07-29 16:05:45 +0000 | [diff] [blame] | 7458 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7459 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 7460 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7461 | } |
| 7462 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7463 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7464 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 7465 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7466 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7467 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7468 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7469 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 7470 | if (ConvResult.isInvalid()) |
| 7471 | return QualType(); |
| 7472 | Op = ConvResult.take(); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7473 | QualType OpTy = Op->getType(); |
| 7474 | QualType Result; |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 7475 | |
| 7476 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 7477 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 7478 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 7479 | Op->getSourceRange()); |
| 7480 | } |
| 7481 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7482 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 7483 | // is an incomplete type or void. It would be possible to warn about |
| 7484 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 7485 | // warning is unlikely to catch any mistakes. |
| 7486 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 7487 | Result = PT->getPointeeType(); |
| 7488 | else if (const ObjCObjectPointerType *OPT = |
| 7489 | OpTy->getAs<ObjCObjectPointerType>()) |
| 7490 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7491 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7492 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7493 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7494 | if (PR.take() != Op) |
| 7495 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7496 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7497 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7498 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7499 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7500 | << OpTy << Op->getSourceRange(); |
| 7501 | return QualType(); |
| 7502 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7503 | |
| 7504 | // Dereferences are usually l-values... |
| 7505 | VK = VK_LValue; |
| 7506 | |
| 7507 | // ...except that certain expressions are never l-values in C. |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 7508 | if (!S.getLangOptions().CPlusPlus && Result.isCForbiddenLValueType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7509 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7510 | |
| 7511 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7512 | } |
| 7513 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7514 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7515 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7516 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7517 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7518 | default: llvm_unreachable("Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7519 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 7520 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 7521 | case tok::star: Opc = BO_Mul; break; |
| 7522 | case tok::slash: Opc = BO_Div; break; |
| 7523 | case tok::percent: Opc = BO_Rem; break; |
| 7524 | case tok::plus: Opc = BO_Add; break; |
| 7525 | case tok::minus: Opc = BO_Sub; break; |
| 7526 | case tok::lessless: Opc = BO_Shl; break; |
| 7527 | case tok::greatergreater: Opc = BO_Shr; break; |
| 7528 | case tok::lessequal: Opc = BO_LE; break; |
| 7529 | case tok::less: Opc = BO_LT; break; |
| 7530 | case tok::greaterequal: Opc = BO_GE; break; |
| 7531 | case tok::greater: Opc = BO_GT; break; |
| 7532 | case tok::exclaimequal: Opc = BO_NE; break; |
| 7533 | case tok::equalequal: Opc = BO_EQ; break; |
| 7534 | case tok::amp: Opc = BO_And; break; |
| 7535 | case tok::caret: Opc = BO_Xor; break; |
| 7536 | case tok::pipe: Opc = BO_Or; break; |
| 7537 | case tok::ampamp: Opc = BO_LAnd; break; |
| 7538 | case tok::pipepipe: Opc = BO_LOr; break; |
| 7539 | case tok::equal: Opc = BO_Assign; break; |
| 7540 | case tok::starequal: Opc = BO_MulAssign; break; |
| 7541 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 7542 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 7543 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 7544 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 7545 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 7546 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 7547 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 7548 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 7549 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 7550 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7551 | } |
| 7552 | return Opc; |
| 7553 | } |
| 7554 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7555 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7556 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7557 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7558 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7559 | default: llvm_unreachable("Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7560 | case tok::plusplus: Opc = UO_PreInc; break; |
| 7561 | case tok::minusminus: Opc = UO_PreDec; break; |
| 7562 | case tok::amp: Opc = UO_AddrOf; break; |
| 7563 | case tok::star: Opc = UO_Deref; break; |
| 7564 | case tok::plus: Opc = UO_Plus; break; |
| 7565 | case tok::minus: Opc = UO_Minus; break; |
| 7566 | case tok::tilde: Opc = UO_Not; break; |
| 7567 | case tok::exclaim: Opc = UO_LNot; break; |
| 7568 | case tok::kw___real: Opc = UO_Real; break; |
| 7569 | case tok::kw___imag: Opc = UO_Imag; break; |
| 7570 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7571 | } |
| 7572 | return Opc; |
| 7573 | } |
| 7574 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7575 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 7576 | /// This warning is only emitted for builtin assignment operations. It is also |
| 7577 | /// suppressed in the event of macro expansions. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7578 | static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7579 | SourceLocation OpLoc) { |
| 7580 | if (!S.ActiveTemplateInstantiations.empty()) |
| 7581 | return; |
| 7582 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 7583 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7584 | LHSExpr = LHSExpr->IgnoreParenImpCasts(); |
| 7585 | RHSExpr = RHSExpr->IgnoreParenImpCasts(); |
| 7586 | const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); |
| 7587 | const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); |
| 7588 | if (!LHSDeclRef || !RHSDeclRef || |
| 7589 | LHSDeclRef->getLocation().isMacroID() || |
| 7590 | RHSDeclRef->getLocation().isMacroID()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7591 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7592 | const ValueDecl *LHSDecl = |
| 7593 | cast<ValueDecl>(LHSDeclRef->getDecl()->getCanonicalDecl()); |
| 7594 | const ValueDecl *RHSDecl = |
| 7595 | cast<ValueDecl>(RHSDeclRef->getDecl()->getCanonicalDecl()); |
| 7596 | if (LHSDecl != RHSDecl) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7597 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7598 | if (LHSDecl->getType().isVolatileQualified()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7599 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7600 | if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7601 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 7602 | return; |
| 7603 | |
| 7604 | S.Diag(OpLoc, diag::warn_self_assignment) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7605 | << LHSDeclRef->getType() |
| 7606 | << LHSExpr->getSourceRange() << RHSExpr->getSourceRange(); |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7607 | } |
| 7608 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7609 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 7610 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 7611 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7612 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7613 | BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7614 | Expr *LHSExpr, Expr *RHSExpr) { |
Sebastian Redl | 0d8ab2e | 2012-02-27 20:34:02 +0000 | [diff] [blame] | 7615 | if (getLangOptions().CPlusPlus0x && isa<InitListExpr>(RHSExpr)) { |
| 7616 | // The syntax only allows initializer lists on the RHS of assignment, |
| 7617 | // so we don't need to worry about accepting invalid code for |
| 7618 | // non-assignment operators. |
| 7619 | // C++11 5.17p9: |
| 7620 | // The meaning of x = {v} [...] is that of x = T(v) [...]. The meaning |
| 7621 | // of x = {} is x = T(). |
| 7622 | InitializationKind Kind = |
| 7623 | InitializationKind::CreateDirectList(RHSExpr->getLocStart()); |
| 7624 | InitializedEntity Entity = |
| 7625 | InitializedEntity::InitializeTemporary(LHSExpr->getType()); |
| 7626 | InitializationSequence InitSeq(*this, Entity, Kind, &RHSExpr, 1); |
| 7627 | ExprResult Init = InitSeq.Perform(*this, Entity, Kind, |
| 7628 | MultiExprArg(&RHSExpr, 1)); |
| 7629 | if (Init.isInvalid()) |
| 7630 | return Init; |
| 7631 | RHSExpr = Init.take(); |
| 7632 | } |
| 7633 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7634 | ExprResult LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7635 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7636 | // The following two variables are used for compound assignment operators |
| 7637 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 7638 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7639 | ExprValueKind VK = VK_RValue; |
| 7640 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7641 | |
| 7642 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7643 | case BO_Assign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7644 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, QualType()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7645 | if (getLangOptions().CPlusPlus && |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7646 | LHS.get()->getObjectKind() != OK_ObjCProperty) { |
| 7647 | VK = LHS.get()->getValueKind(); |
| 7648 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7649 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7650 | if (!ResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7651 | DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7652 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7653 | case BO_PtrMemD: |
| 7654 | case BO_PtrMemI: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7655 | ResultTy = CheckPointerToMemberOperands(LHS, RHS, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7656 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7657 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7658 | case BO_Mul: |
| 7659 | case BO_Div: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7660 | ResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7661 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7662 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7663 | case BO_Rem: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7664 | ResultTy = CheckRemainderOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7665 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7666 | case BO_Add: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7667 | ResultTy = CheckAdditionOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7668 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7669 | case BO_Sub: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7670 | ResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7671 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7672 | case BO_Shl: |
| 7673 | case BO_Shr: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7674 | ResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7675 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7676 | case BO_LE: |
| 7677 | case BO_LT: |
| 7678 | case BO_GE: |
| 7679 | case BO_GT: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7680 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7681 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7682 | case BO_EQ: |
| 7683 | case BO_NE: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7684 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7685 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7686 | case BO_And: |
| 7687 | case BO_Xor: |
| 7688 | case BO_Or: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7689 | ResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7690 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7691 | case BO_LAnd: |
| 7692 | case BO_LOr: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7693 | ResultTy = CheckLogicalOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7694 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7695 | case BO_MulAssign: |
| 7696 | case BO_DivAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7697 | CompResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7698 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7699 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7700 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7701 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7702 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7703 | case BO_RemAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7704 | CompResultTy = CheckRemainderOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7705 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7706 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7707 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7708 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7709 | case BO_AddAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7710 | CompResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, &CompLHSTy); |
| 7711 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7712 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7713 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7714 | case BO_SubAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7715 | CompResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc, &CompLHSTy); |
| 7716 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7717 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7718 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7719 | case BO_ShlAssign: |
| 7720 | case BO_ShrAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7721 | CompResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7722 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7723 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7724 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7725 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7726 | case BO_AndAssign: |
| 7727 | case BO_XorAssign: |
| 7728 | case BO_OrAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7729 | CompResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7730 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7731 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 7732 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7733 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7734 | case BO_Comma: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7735 | ResultTy = CheckCommaOperands(*this, LHS, RHS, OpLoc); |
| 7736 | if (getLangOptions().CPlusPlus && !RHS.isInvalid()) { |
| 7737 | VK = RHS.get()->getValueKind(); |
| 7738 | OK = RHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7739 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7740 | break; |
| 7741 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7742 | if (ResultTy.isNull() || LHS.isInvalid() || RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7743 | return ExprError(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7744 | |
| 7745 | // Check for array bounds violations for both sides of the BinaryOperator |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7746 | CheckArrayAccess(LHS.get()); |
| 7747 | CheckArrayAccess(RHS.get()); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7748 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7749 | if (CompResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7750 | return Owned(new (Context) BinaryOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7751 | ResultTy, VK, OK, OpLoc)); |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7752 | if (getLangOptions().CPlusPlus && LHS.get()->getObjectKind() != |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7753 | OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7754 | VK = VK_LValue; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7755 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7756 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7757 | return Owned(new (Context) CompoundAssignOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7758 | ResultTy, VK, OK, CompLHSTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7759 | CompResultTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7760 | } |
| 7761 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7762 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 7763 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 7764 | /// comparison operators have higher precedence. The most typical example of |
| 7765 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7766 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7767 | SourceLocation OpLoc, Expr *LHSExpr, |
| 7768 | Expr *RHSExpr) { |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7769 | typedef BinaryOperator BinOp; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7770 | BinOp::Opcode LHSopc = static_cast<BinOp::Opcode>(-1), |
| 7771 | RHSopc = static_cast<BinOp::Opcode>(-1); |
| 7772 | if (BinOp *BO = dyn_cast<BinOp>(LHSExpr)) |
| 7773 | LHSopc = BO->getOpcode(); |
| 7774 | if (BinOp *BO = dyn_cast<BinOp>(RHSExpr)) |
| 7775 | RHSopc = BO->getOpcode(); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7776 | |
| 7777 | // Subs are not binary operators. |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7778 | if (LHSopc == -1 && RHSopc == -1) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7779 | return; |
| 7780 | |
| 7781 | // Bitwise operations are sometimes used as eager logical ops. |
| 7782 | // Don't diagnose this. |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7783 | if ((BinOp::isComparisonOp(LHSopc) || BinOp::isBitwiseOp(LHSopc)) && |
| 7784 | (BinOp::isComparisonOp(RHSopc) || BinOp::isBitwiseOp(RHSopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7785 | return; |
| 7786 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7787 | bool isLeftComp = BinOp::isComparisonOp(LHSopc); |
| 7788 | bool isRightComp = BinOp::isComparisonOp(RHSopc); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7789 | if (!isLeftComp && !isRightComp) return; |
| 7790 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7791 | SourceRange DiagRange = isLeftComp ? SourceRange(LHSExpr->getLocStart(), |
| 7792 | OpLoc) |
| 7793 | : SourceRange(OpLoc, RHSExpr->getLocEnd()); |
| 7794 | std::string OpStr = isLeftComp ? BinOp::getOpcodeStr(LHSopc) |
| 7795 | : BinOp::getOpcodeStr(RHSopc); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7796 | SourceRange ParensRange = isLeftComp ? |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7797 | SourceRange(cast<BinOp>(LHSExpr)->getRHS()->getLocStart(), |
| 7798 | RHSExpr->getLocEnd()) |
| 7799 | : SourceRange(LHSExpr->getLocStart(), |
| 7800 | cast<BinOp>(RHSExpr)->getLHS()->getLocStart()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7801 | |
| 7802 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 7803 | << DiagRange << BinOp::getOpcodeStr(Opc) << OpStr; |
| 7804 | SuggestParentheses(Self, OpLoc, |
| 7805 | Self.PDiag(diag::note_precedence_bitwise_silence) << OpStr, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 7806 | RHSExpr->getSourceRange()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7807 | SuggestParentheses(Self, OpLoc, |
| 7808 | Self.PDiag(diag::note_precedence_bitwise_first) << BinOp::getOpcodeStr(Opc), |
| 7809 | ParensRange); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7810 | } |
| 7811 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7812 | /// \brief It accepts a '&' expr that is inside a '|' one. |
| 7813 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression |
| 7814 | /// in parentheses. |
| 7815 | static void |
| 7816 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, |
| 7817 | BinaryOperator *Bop) { |
| 7818 | assert(Bop->getOpcode() == BO_And); |
| 7819 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) |
| 7820 | << Bop->getSourceRange() << OpLoc; |
| 7821 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
| 7822 | Self.PDiag(diag::note_bitwise_and_in_bitwise_or_silence), |
| 7823 | Bop->getSourceRange()); |
| 7824 | } |
| 7825 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7826 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 7827 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 7828 | /// in parentheses. |
| 7829 | static void |
| 7830 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7831 | BinaryOperator *Bop) { |
| 7832 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7833 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 7834 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7835 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7836 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7837 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7838 | } |
| 7839 | |
| 7840 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7841 | /// 'true'. |
| 7842 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 7843 | bool Res; |
| 7844 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 7845 | } |
| 7846 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7847 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7848 | /// 'false'. |
| 7849 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 7850 | bool Res; |
| 7851 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 7852 | } |
| 7853 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7854 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 7855 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7856 | Expr *LHSExpr, Expr *RHSExpr) { |
| 7857 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(LHSExpr)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7858 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7859 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7860 | if (EvaluatesAsFalse(S, RHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7861 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7862 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 7863 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 7864 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 7865 | } else if (Bop->getOpcode() == BO_LOr) { |
| 7866 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 7867 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 7868 | // "a || b && 1", but warn now. |
| 7869 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 7870 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 7871 | } |
| 7872 | } |
| 7873 | } |
| 7874 | } |
| 7875 | |
| 7876 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 7877 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7878 | Expr *LHSExpr, Expr *RHSExpr) { |
| 7879 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(RHSExpr)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7880 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7881 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7882 | if (EvaluatesAsFalse(S, LHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7883 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7884 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 7885 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 7886 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7887 | } |
| 7888 | } |
| 7889 | } |
| 7890 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7891 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
| 7892 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, |
| 7893 | Expr *OrArg) { |
| 7894 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { |
| 7895 | if (Bop->getOpcode() == BO_And) |
| 7896 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); |
| 7897 | } |
| 7898 | } |
| 7899 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7900 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7901 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7902 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7903 | SourceLocation OpLoc, Expr *LHSExpr, |
| 7904 | Expr *RHSExpr){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7905 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7906 | if (BinaryOperator::isBitwiseOp(Opc)) |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7907 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7908 | |
| 7909 | // Diagnose "arg1 & arg2 | arg3" |
| 7910 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7911 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, LHSExpr); |
| 7912 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7913 | } |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7914 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7915 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 7916 | // We don't warn for 'assert(a || b && "bad")' since this is safe. |
Argyrios Kyrtzidis | d92ccaa | 2010-11-17 18:54:22 +0000 | [diff] [blame] | 7917 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7918 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, LHSExpr, RHSExpr); |
| 7919 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7920 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7921 | } |
| 7922 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7923 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7924 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7925 | tok::TokenKind Kind, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7926 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7927 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7928 | assert((LHSExpr != 0) && "ActOnBinOp(): missing left expression"); |
| 7929 | assert((RHSExpr != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7930 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7931 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7932 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, LHSExpr, RHSExpr); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7933 | |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7934 | return BuildBinOp(S, TokLoc, Opc, LHSExpr, RHSExpr); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7935 | } |
| 7936 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7937 | /// Build an overloaded binary operator expression in the given scope. |
| 7938 | static ExprResult BuildOverloadedBinOp(Sema &S, Scope *Sc, SourceLocation OpLoc, |
| 7939 | BinaryOperatorKind Opc, |
| 7940 | Expr *LHS, Expr *RHS) { |
| 7941 | // Find all of the overloaded operators visible from this |
| 7942 | // point. We perform both an operator-name lookup from the local |
| 7943 | // scope and an argument-dependent lookup based on the types of |
| 7944 | // the arguments. |
| 7945 | UnresolvedSet<16> Functions; |
| 7946 | OverloadedOperatorKind OverOp |
| 7947 | = BinaryOperator::getOverloadedOperator(Opc); |
| 7948 | if (Sc && OverOp != OO_None) |
| 7949 | S.LookupOverloadedOperatorName(OverOp, Sc, LHS->getType(), |
| 7950 | RHS->getType(), Functions); |
| 7951 | |
| 7952 | // Build the (potentially-overloaded, potentially-dependent) |
| 7953 | // binary operation. |
| 7954 | return S.CreateOverloadedBinOp(OpLoc, Opc, Functions, LHS, RHS); |
| 7955 | } |
| 7956 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7957 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7958 | BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 7959 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 7960 | // We want to end up calling one of checkPseudoObjectAssignment |
| 7961 | // (if the LHS is a pseudo-object), BuildOverloadedBinOp (if |
| 7962 | // both expressions are overloadable or either is type-dependent), |
| 7963 | // or CreateBuiltinBinOp (in any other case). We also want to get |
| 7964 | // any placeholder types out of the way. |
| 7965 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7966 | // Handle pseudo-objects in the LHS. |
| 7967 | if (const BuiltinType *pty = LHSExpr->getType()->getAsPlaceholderType()) { |
| 7968 | // Assignments with a pseudo-object l-value need special analysis. |
| 7969 | if (pty->getKind() == BuiltinType::PseudoObject && |
| 7970 | BinaryOperator::isAssignmentOp(Opc)) |
| 7971 | return checkPseudoObjectAssignment(S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 7972 | |
| 7973 | // Don't resolve overloads if the other type is overloadable. |
| 7974 | if (pty->getKind() == BuiltinType::Overload) { |
| 7975 | // We can't actually test that if we still have a placeholder, |
| 7976 | // though. Fortunately, none of the exceptions we see in that |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 7977 | // code below are valid when the LHS is an overload set. Note |
| 7978 | // that an overload set can be dependently-typed, but it never |
| 7979 | // instantiates to having an overloadable type. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7980 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); |
| 7981 | if (resolvedRHS.isInvalid()) return ExprError(); |
| 7982 | RHSExpr = resolvedRHS.take(); |
| 7983 | |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 7984 | if (RHSExpr->isTypeDependent() || |
| 7985 | RHSExpr->getType()->isOverloadableType()) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7986 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 7987 | } |
| 7988 | |
| 7989 | ExprResult LHS = CheckPlaceholderExpr(LHSExpr); |
| 7990 | if (LHS.isInvalid()) return ExprError(); |
| 7991 | LHSExpr = LHS.take(); |
| 7992 | } |
| 7993 | |
| 7994 | // Handle pseudo-objects in the RHS. |
| 7995 | if (const BuiltinType *pty = RHSExpr->getType()->getAsPlaceholderType()) { |
| 7996 | // An overload in the RHS can potentially be resolved by the type |
| 7997 | // being assigned to. |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 7998 | if (Opc == BO_Assign && pty->getKind() == BuiltinType::Overload) { |
| 7999 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) |
| 8000 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8001 | |
Eli Friedman | 8788491 | 2012-01-17 21:27:43 +0000 | [diff] [blame] | 8002 | if (LHSExpr->getType()->isOverloadableType()) |
| 8003 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8004 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8005 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8006 | } |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8007 | |
| 8008 | // Don't resolve overloads if the other type is overloadable. |
| 8009 | if (pty->getKind() == BuiltinType::Overload && |
| 8010 | LHSExpr->getType()->isOverloadableType()) |
| 8011 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8012 | |
| 8013 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); |
| 8014 | if (!resolvedRHS.isUsable()) return ExprError(); |
| 8015 | RHSExpr = resolvedRHS.take(); |
| 8016 | } |
| 8017 | |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8018 | if (getLangOptions().CPlusPlus) { |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8019 | // If either expression is type-dependent, always build an |
| 8020 | // overloaded op. |
| 8021 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) |
| 8022 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8023 | |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8024 | // Otherwise, build an overloaded op if either expression has an |
| 8025 | // overloadable type. |
| 8026 | if (LHSExpr->getType()->isOverloadableType() || |
| 8027 | RHSExpr->getType()->isOverloadableType()) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8028 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8029 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8030 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8031 | // Build a built-in binary operation. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8032 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8033 | } |
| 8034 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8035 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8036 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8037 | Expr *InputExpr) { |
| 8038 | ExprResult Input = Owned(InputExpr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8039 | ExprValueKind VK = VK_RValue; |
| 8040 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8041 | QualType resultType; |
| 8042 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8043 | case UO_PreInc: |
| 8044 | case UO_PreDec: |
| 8045 | case UO_PostInc: |
| 8046 | case UO_PostDec: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8047 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8048 | Opc == UO_PreInc || |
| 8049 | Opc == UO_PostInc, |
| 8050 | Opc == UO_PreInc || |
| 8051 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8052 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8053 | case UO_AddrOf: |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8054 | resultType = CheckAddressOfOperand(*this, Input, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8055 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8056 | case UO_Deref: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8057 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8058 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8059 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8060 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8061 | case UO_Plus: |
| 8062 | case UO_Minus: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8063 | Input = UsualUnaryConversions(Input.take()); |
| 8064 | if (Input.isInvalid()) return ExprError(); |
| 8065 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8066 | if (resultType->isDependentType()) |
| 8067 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8068 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8069 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8070 | break; |
| 8071 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 8072 | resultType->isEnumeralType()) |
| 8073 | break; |
| 8074 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8075 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8076 | resultType->isPointerType()) |
| 8077 | break; |
| 8078 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8079 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8080 | << resultType << Input.get()->getSourceRange()); |
| 8081 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8082 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8083 | Input = UsualUnaryConversions(Input.take()); |
| 8084 | if (Input.isInvalid()) return ExprError(); |
| 8085 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8086 | if (resultType->isDependentType()) |
| 8087 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8088 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8089 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8090 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8091 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8092 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8093 | else if (resultType->hasIntegerRepresentation()) |
| 8094 | break; |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8095 | else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8096 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8097 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8098 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8099 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8100 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8101 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8102 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8103 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8104 | if (Input.isInvalid()) return ExprError(); |
| 8105 | resultType = Input.get()->getType(); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 8106 | |
| 8107 | // Though we still have to promote half FP to float... |
| 8108 | if (resultType->isHalfType()) { |
| 8109 | Input = ImpCastExprToType(Input.take(), Context.FloatTy, CK_FloatingCast).take(); |
| 8110 | resultType = Context.FloatTy; |
| 8111 | } |
| 8112 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8113 | if (resultType->isDependentType()) |
| 8114 | break; |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8115 | if (resultType->isScalarType()) { |
| 8116 | // C99 6.5.3.3p1: ok, fallthrough; |
| 8117 | if (Context.getLangOptions().CPlusPlus) { |
| 8118 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 8119 | // operand contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8120 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 8121 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8122 | } |
Tanya Lattner | b0f9dd2 | 2012-01-19 01:16:16 +0000 | [diff] [blame] | 8123 | } else if (resultType->isExtVectorType()) { |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 8124 | // Vector logical not returns the signed variant of the operand type. |
| 8125 | resultType = GetSignedVectorType(resultType); |
| 8126 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8127 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8128 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8129 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8130 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8131 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8132 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8133 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8134 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8135 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8136 | case UO_Real: |
| 8137 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8138 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8139 | // _Real maps ordinary l-values into ordinary l-values. _Imag maps ordinary |
| 8140 | // complex l-values to ordinary l-values and all other values to r-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8141 | if (Input.isInvalid()) return ExprError(); |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8142 | if (Opc == UO_Real || Input.get()->getType()->isAnyComplexType()) { |
| 8143 | if (Input.get()->getValueKind() != VK_RValue && |
| 8144 | Input.get()->getObjectKind() == OK_Ordinary) |
| 8145 | VK = Input.get()->getValueKind(); |
| 8146 | } else if (!getLangOptions().CPlusPlus) { |
| 8147 | // In C, a volatile scalar is read by __imag. In C++, it is not. |
| 8148 | Input = DefaultLvalueConversion(Input.take()); |
| 8149 | } |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8150 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8151 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8152 | resultType = Input.get()->getType(); |
| 8153 | VK = Input.get()->getValueKind(); |
| 8154 | OK = Input.get()->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8155 | break; |
| 8156 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8157 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8158 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8159 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8160 | // Check for array bounds violations in the operand of the UnaryOperator, |
| 8161 | // except for the '*' and '&' operators that have to be handled specially |
| 8162 | // by CheckArrayAccess (as there are special cases like &array[arraysize] |
| 8163 | // that are explicitly defined as valid by the standard). |
| 8164 | if (Opc != UO_AddrOf && Opc != UO_Deref) |
| 8165 | CheckArrayAccess(Input.get()); |
| 8166 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8167 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8168 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8169 | } |
| 8170 | |
Douglas Gregor | d3d0853 | 2011-12-14 21:23:13 +0000 | [diff] [blame] | 8171 | /// \brief Determine whether the given expression is a qualified member |
| 8172 | /// access expression, of a form that could be turned into a pointer to member |
| 8173 | /// with the address-of operator. |
| 8174 | static bool isQualifiedMemberAccess(Expr *E) { |
| 8175 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 8176 | if (!DRE->getQualifier()) |
| 8177 | return false; |
| 8178 | |
| 8179 | ValueDecl *VD = DRE->getDecl(); |
| 8180 | if (!VD->isCXXClassMember()) |
| 8181 | return false; |
| 8182 | |
| 8183 | if (isa<FieldDecl>(VD) || isa<IndirectFieldDecl>(VD)) |
| 8184 | return true; |
| 8185 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(VD)) |
| 8186 | return Method->isInstance(); |
| 8187 | |
| 8188 | return false; |
| 8189 | } |
| 8190 | |
| 8191 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 8192 | if (!ULE->getQualifier()) |
| 8193 | return false; |
| 8194 | |
| 8195 | for (UnresolvedLookupExpr::decls_iterator D = ULE->decls_begin(), |
| 8196 | DEnd = ULE->decls_end(); |
| 8197 | D != DEnd; ++D) { |
| 8198 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*D)) { |
| 8199 | if (Method->isInstance()) |
| 8200 | return true; |
| 8201 | } else { |
| 8202 | // Overload set does not contain methods. |
| 8203 | break; |
| 8204 | } |
| 8205 | } |
| 8206 | |
| 8207 | return false; |
| 8208 | } |
| 8209 | |
| 8210 | return false; |
| 8211 | } |
| 8212 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8213 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8214 | UnaryOperatorKind Opc, Expr *Input) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8215 | // First things first: handle placeholders so that the |
| 8216 | // overloaded-operator check considers the right type. |
| 8217 | if (const BuiltinType *pty = Input->getType()->getAsPlaceholderType()) { |
| 8218 | // Increment and decrement of pseudo-object references. |
| 8219 | if (pty->getKind() == BuiltinType::PseudoObject && |
| 8220 | UnaryOperator::isIncrementDecrementOp(Opc)) |
| 8221 | return checkPseudoObjectIncDec(S, OpLoc, Opc, Input); |
| 8222 | |
| 8223 | // extension is always a builtin operator. |
| 8224 | if (Opc == UO_Extension) |
| 8225 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
| 8226 | |
| 8227 | // & gets special logic for several kinds of placeholder. |
| 8228 | // The builtin code knows what to do. |
| 8229 | if (Opc == UO_AddrOf && |
| 8230 | (pty->getKind() == BuiltinType::Overload || |
| 8231 | pty->getKind() == BuiltinType::UnknownAny || |
| 8232 | pty->getKind() == BuiltinType::BoundMember)) |
| 8233 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
| 8234 | |
| 8235 | // Anything else needs to be handled now. |
| 8236 | ExprResult Result = CheckPlaceholderExpr(Input); |
| 8237 | if (Result.isInvalid()) return ExprError(); |
| 8238 | Input = Result.take(); |
| 8239 | } |
| 8240 | |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 8241 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Douglas Gregor | d3d0853 | 2011-12-14 21:23:13 +0000 | [diff] [blame] | 8242 | UnaryOperator::getOverloadedOperator(Opc) != OO_None && |
| 8243 | !(Opc == UO_AddrOf && isQualifiedMemberAccess(Input))) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8244 | // Find all of the overloaded operators visible from this |
| 8245 | // point. We perform both an operator-name lookup from the local |
| 8246 | // scope and an argument-dependent lookup based on the types of |
| 8247 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8248 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8249 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8250 | if (S && OverOp != OO_None) |
| 8251 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 8252 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8253 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8254 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8255 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8256 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8257 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8258 | } |
| 8259 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8260 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8261 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8262 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8263 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8264 | } |
| 8265 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8266 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8267 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8268 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8269 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8270 | // Create the AST node. The address of a label always has type 'void*'. |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8271 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8272 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8273 | } |
| 8274 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8275 | /// Given the last statement in a statement-expression, check whether |
| 8276 | /// the result is a producing expression (like a call to an |
| 8277 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 8278 | /// release out of the full-expression. Otherwise, return null. |
| 8279 | /// Cannot fail. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8280 | static Expr *maybeRebuildARCConsumingStmt(Stmt *Statement) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8281 | // Should always be wrapped with one of these. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8282 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(Statement); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8283 | if (!cleanups) return 0; |
| 8284 | |
| 8285 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 8286 | if (!cast || cast->getCastKind() != CK_ARCConsumeObject) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8287 | return 0; |
| 8288 | |
| 8289 | // Splice out the cast. This shouldn't modify any interesting |
| 8290 | // features of the statement. |
| 8291 | Expr *producer = cast->getSubExpr(); |
| 8292 | assert(producer->getType() == cast->getType()); |
| 8293 | assert(producer->getValueKind() == cast->getValueKind()); |
| 8294 | cleanups->setSubExpr(producer); |
| 8295 | return cleanups; |
| 8296 | } |
| 8297 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8298 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8299 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8300 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8301 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8302 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8303 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8304 | bool isFileScope |
| 8305 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8306 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8307 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8308 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8309 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8310 | // example, it is not possible to goto into a stmt expression apparently. |
| 8311 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8312 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8313 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8314 | // as the type of the stmtexpr. |
| 8315 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8316 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8317 | if (!Compound->body_empty()) { |
| 8318 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8319 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8320 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8321 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8322 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8323 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8324 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8325 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8326 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8327 | // Do function/array conversion on the last expression, but not |
| 8328 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8329 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 8330 | if (LastExpr.isInvalid()) |
| 8331 | return ExprError(); |
| 8332 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8333 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8334 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8335 | // In ARC, if the final expression ends in a consume, splice |
| 8336 | // the consume out and bind it later. In the alternate case |
| 8337 | // (when dealing with a retainable type), the result |
| 8338 | // initialization will create a produce. In both cases the |
| 8339 | // result will be +1, and we'll need to balance that out with |
| 8340 | // a bind. |
| 8341 | if (Expr *rebuiltLastStmt |
| 8342 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 8343 | LastExpr = rebuiltLastStmt; |
| 8344 | } else { |
| 8345 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8346 | InitializedEntity::InitializeResult(LPLoc, |
| 8347 | Ty, |
| 8348 | false), |
| 8349 | SourceLocation(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8350 | LastExpr); |
| 8351 | } |
| 8352 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8353 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8354 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8355 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8356 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8357 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8358 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8359 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8360 | StmtExprMayBindToTemp = true; |
| 8361 | } |
| 8362 | } |
| 8363 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8364 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8365 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8366 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8367 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8368 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8369 | if (StmtExprMayBindToTemp) |
| 8370 | return MaybeBindToTemporary(ResStmtExpr); |
| 8371 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8372 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8373 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8374 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8375 | TypeSourceInfo *TInfo, |
| 8376 | OffsetOfComponent *CompPtr, |
| 8377 | unsigned NumComponents, |
| 8378 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8379 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8380 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8381 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8382 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8383 | // We must have at least one component that refers to the type, and the first |
| 8384 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8385 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8386 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8387 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 8388 | << ArgTy << TypeRange); |
| 8389 | |
| 8390 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 8391 | // with an incomplete type would be ill-formed. |
| 8392 | if (!Dependent |
| 8393 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 8394 | PDiag(diag::err_offsetof_incomplete_type) |
| 8395 | << TypeRange)) |
| 8396 | return ExprError(); |
| 8397 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8398 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 8399 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 8400 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 8401 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8402 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8403 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 8404 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8405 | |
| 8406 | bool DidWarnAboutNonPOD = false; |
| 8407 | QualType CurrentType = ArgTy; |
| 8408 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8409 | SmallVector<OffsetOfNode, 4> Comps; |
| 8410 | SmallVector<Expr*, 4> Exprs; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8411 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 8412 | const OffsetOfComponent &OC = CompPtr[i]; |
| 8413 | if (OC.isBrackets) { |
| 8414 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 8415 | if (!CurrentType->isDependentType()) { |
| 8416 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 8417 | if(!AT) |
| 8418 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 8419 | << CurrentType); |
| 8420 | CurrentType = AT->getElementType(); |
| 8421 | } else |
| 8422 | CurrentType = Context.DependentTy; |
| 8423 | |
Richard Smith | ea01143 | 2011-10-17 23:29:39 +0000 | [diff] [blame] | 8424 | ExprResult IdxRval = DefaultLvalueConversion(static_cast<Expr*>(OC.U.E)); |
| 8425 | if (IdxRval.isInvalid()) |
| 8426 | return ExprError(); |
| 8427 | Expr *Idx = IdxRval.take(); |
| 8428 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8429 | // The expression must be an integral expression. |
| 8430 | // FIXME: An integral constant expression? |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8431 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 8432 | !Idx->getType()->isIntegerType()) |
| 8433 | return ExprError(Diag(Idx->getLocStart(), |
| 8434 | diag::err_typecheck_subscript_not_integer) |
| 8435 | << Idx->getSourceRange()); |
Richard Smith | d82e5d3 | 2011-10-17 05:48:07 +0000 | [diff] [blame] | 8436 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8437 | // Record this array index. |
| 8438 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
Richard Smith | ea01143 | 2011-10-17 23:29:39 +0000 | [diff] [blame] | 8439 | Exprs.push_back(Idx); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8440 | continue; |
| 8441 | } |
| 8442 | |
| 8443 | // Offset of a field. |
| 8444 | if (CurrentType->isDependentType()) { |
| 8445 | // We have the offset of a field, but we can't look into the dependent |
| 8446 | // type. Just record the identifier of the field. |
| 8447 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 8448 | CurrentType = Context.DependentTy; |
| 8449 | continue; |
| 8450 | } |
| 8451 | |
| 8452 | // We need to have a complete type to look into. |
| 8453 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 8454 | diag::err_offsetof_incomplete_type)) |
| 8455 | return ExprError(); |
| 8456 | |
| 8457 | // Look for the designated field. |
| 8458 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 8459 | if (!RC) |
| 8460 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 8461 | << CurrentType); |
| 8462 | RecordDecl *RD = RC->getDecl(); |
| 8463 | |
| 8464 | // C++ [lib.support.types]p5: |
| 8465 | // The macro offsetof accepts a restricted set of type arguments in this |
| 8466 | // International Standard. type shall be a POD structure or a POD union |
| 8467 | // (clause 9). |
| 8468 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 8469 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 8470 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8471 | PDiag(diag::warn_offsetof_non_pod_type) |
| 8472 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 8473 | << CurrentType)) |
| 8474 | DidWarnAboutNonPOD = true; |
| 8475 | } |
| 8476 | |
| 8477 | // Look for the field. |
| 8478 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 8479 | LookupQualifiedName(R, RD); |
| 8480 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8481 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 8482 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 8483 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8484 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 8485 | } |
| 8486 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8487 | if (!MemberDecl) |
| 8488 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 8489 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 8490 | OC.LocEnd)); |
| 8491 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 8492 | // C99 7.17p3: |
| 8493 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 8494 | // |
| 8495 | // We diagnose this as an error. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8496 | if (MemberDecl->isBitField()) { |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 8497 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 8498 | << MemberDecl->getDeclName() |
| 8499 | << SourceRange(BuiltinLoc, RParenLoc); |
| 8500 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 8501 | return ExprError(); |
| 8502 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8503 | |
| 8504 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8505 | if (IndirectMemberDecl) |
| 8506 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8507 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8508 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 8509 | // the base class indirections. |
| 8510 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8511 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8512 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8513 | CXXBasePath &Path = Paths.front(); |
| 8514 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 8515 | B != BEnd; ++B) |
| 8516 | Comps.push_back(OffsetOfNode(B->Base)); |
| 8517 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8518 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8519 | if (IndirectMemberDecl) { |
| 8520 | for (IndirectFieldDecl::chain_iterator FI = |
| 8521 | IndirectMemberDecl->chain_begin(), |
| 8522 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 8523 | assert(isa<FieldDecl>(*FI)); |
| 8524 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 8525 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 8526 | } |
| 8527 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8528 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8529 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8530 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 8531 | } |
| 8532 | |
| 8533 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 8534 | TInfo, Comps.data(), Comps.size(), |
| 8535 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 8536 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8537 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8538 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8539 | SourceLocation BuiltinLoc, |
| 8540 | SourceLocation TypeLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8541 | ParsedType ParsedArgTy, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8542 | OffsetOfComponent *CompPtr, |
| 8543 | unsigned NumComponents, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8544 | SourceLocation RParenLoc) { |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8545 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8546 | TypeSourceInfo *ArgTInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8547 | QualType ArgTy = GetTypeFromParser(ParsedArgTy, &ArgTInfo); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8548 | if (ArgTy.isNull()) |
| 8549 | return ExprError(); |
| 8550 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 8551 | if (!ArgTInfo) |
| 8552 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 8553 | |
| 8554 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8555 | RParenLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8556 | } |
| 8557 | |
| 8558 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8559 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8560 | Expr *CondExpr, |
| 8561 | Expr *LHSExpr, Expr *RHSExpr, |
| 8562 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8563 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 8564 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8565 | ExprValueKind VK = VK_RValue; |
| 8566 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8567 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8568 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 8569 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8570 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8571 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8572 | } else { |
| 8573 | // The conditional expression is required to be a constant expression. |
| 8574 | llvm::APSInt condEval(32); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 8575 | ExprResult CondICE = VerifyIntegerConstantExpression(CondExpr, &condEval, |
| 8576 | PDiag(diag::err_typecheck_choose_expr_requires_constant), false); |
| 8577 | if (CondICE.isInvalid()) |
| 8578 | return ExprError(); |
| 8579 | CondExpr = CondICE.take(); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8580 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8581 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8582 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 8583 | |
| 8584 | resType = ActiveExpr->getType(); |
| 8585 | ValueDependent = ActiveExpr->isValueDependent(); |
| 8586 | VK = ActiveExpr->getValueKind(); |
| 8587 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8588 | } |
| 8589 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8590 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8591 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8592 | resType->isDependentType(), |
| 8593 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8594 | } |
| 8595 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8596 | //===----------------------------------------------------------------------===// |
| 8597 | // Clang Extensions. |
| 8598 | //===----------------------------------------------------------------------===// |
| 8599 | |
| 8600 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8601 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *CurScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8602 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8603 | PushBlockScope(CurScope, Block); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8604 | CurContext->addDecl(Block); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8605 | if (CurScope) |
| 8606 | PushDeclContext(CurScope, Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8607 | else |
| 8608 | CurContext = Block; |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 8609 | |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 8610 | getCurBlock()->HasImplicitReturnType = true; |
| 8611 | |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 8612 | // Enter a new evaluation context to insulate the block from any |
| 8613 | // cleanups from the enclosing full-expression. |
| 8614 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8615 | } |
| 8616 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8617 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 8618 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8619 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8620 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8621 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8622 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8623 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8624 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8625 | // GetTypeForDeclarator always produces a function type for a block |
| 8626 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 8627 | // unless the function was written with a typedef. |
| 8628 | assert(T->isFunctionType() && |
| 8629 | "GetTypeForDeclarator made a non-function block signature"); |
| 8630 | |
| 8631 | // Look for an explicit signature in that function type. |
| 8632 | FunctionProtoTypeLoc ExplicitSignature; |
| 8633 | |
| 8634 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 8635 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 8636 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 8637 | |
| 8638 | // Check whether that explicit signature was synthesized by |
| 8639 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 8640 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8641 | if (ExplicitSignature.getLocalRangeBegin() == |
| 8642 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8643 | // This would be much cheaper if we stored TypeLocs instead of |
| 8644 | // TypeSourceInfos. |
| 8645 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 8646 | unsigned Size = Result.getFullDataSize(); |
| 8647 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 8648 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 8649 | |
| 8650 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 8651 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8652 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8653 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8654 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 8655 | CurBlock->FunctionType = T; |
| 8656 | |
| 8657 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 8658 | QualType RetTy = Fn->getResultType(); |
| 8659 | bool isVariadic = |
| 8660 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 8661 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8662 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8663 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8664 | // Don't allow returning a objc interface by value. |
| 8665 | if (RetTy->isObjCObjectType()) { |
| 8666 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 8667 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 8668 | return; |
| 8669 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8670 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8671 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8672 | // return type. TODO: what should we do with declarators like: |
| 8673 | // ^ * { ... } |
| 8674 | // If the answer is "apply template argument deduction".... |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 8675 | if (RetTy != Context.DependentTy) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8676 | CurBlock->ReturnType = RetTy; |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 8677 | CurBlock->TheDecl->setBlockMissingReturnType(false); |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 8678 | CurBlock->HasImplicitReturnType = false; |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 8679 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8680 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8681 | // Push block parameters from the declarator if we had them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8682 | SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8683 | if (ExplicitSignature) { |
| 8684 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 8685 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8686 | if (Param->getIdentifier() == 0 && |
| 8687 | !Param->isImplicit() && |
| 8688 | !Param->isInvalidDecl() && |
| 8689 | !getLangOptions().CPlusPlus) |
| 8690 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8691 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8692 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8693 | |
| 8694 | // Fake up parameter variables if we have a typedef, like |
| 8695 | // ^ fntype { ... } |
| 8696 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 8697 | for (FunctionProtoType::arg_type_iterator |
| 8698 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 8699 | ParmVarDecl *Param = |
| 8700 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 8701 | ParamInfo.getSourceRange().getBegin(), |
| 8702 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8703 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8704 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8705 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8706 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8707 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8708 | if (!Params.empty()) { |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8709 | CurBlock->TheDecl->setParams(Params); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8710 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 8711 | CurBlock->TheDecl->param_end(), |
| 8712 | /*CheckParameterNames=*/false); |
| 8713 | } |
| 8714 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8715 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 8716 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8717 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8718 | // Put the parameter variables in scope. We can bail out immediately |
| 8719 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8720 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8721 | return; |
| 8722 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8723 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8724 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 8725 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 8726 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8727 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8728 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8729 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8730 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8731 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8732 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8733 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8734 | } |
| 8735 | |
| 8736 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 8737 | /// is invoked to pop the information about the block from the action impl. |
| 8738 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 8739 | // Leave the expression-evaluation context. |
| 8740 | DiscardCleanupsInEvaluationContext(); |
| 8741 | PopExpressionEvaluationContext(); |
| 8742 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8743 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 8744 | PopDeclContext(); |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 8745 | PopFunctionScopeInfo(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8746 | } |
| 8747 | |
| 8748 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 8749 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8750 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8751 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 8752 | // If blocks are disabled, emit an error. |
| 8753 | if (!LangOpts.Blocks) |
| 8754 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8755 | |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 8756 | // Leave the expression-evaluation context. |
| 8757 | assert(!ExprNeedsCleanups && "cleanups within block not correctly bound!"); |
| 8758 | PopExpressionEvaluationContext(); |
| 8759 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8760 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8761 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8762 | PopDeclContext(); |
| 8763 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8764 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 8765 | if (!BSI->ReturnType.isNull()) |
| 8766 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8767 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 8768 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8769 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8770 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8771 | // Set the captured variables on the block. |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 8772 | // FIXME: Share capture structure between BlockDecl and CapturingScopeInfo! |
| 8773 | SmallVector<BlockDecl::Capture, 4> Captures; |
| 8774 | for (unsigned i = 0, e = BSI->Captures.size(); i != e; i++) { |
| 8775 | CapturingScopeInfo::Capture &Cap = BSI->Captures[i]; |
| 8776 | if (Cap.isThisCapture()) |
| 8777 | continue; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 8778 | BlockDecl::Capture NewCap(Cap.getVariable(), Cap.isBlockCapture(), |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 8779 | Cap.isNested(), Cap.getCopyExpr()); |
| 8780 | Captures.push_back(NewCap); |
| 8781 | } |
| 8782 | BSI->TheDecl->setCaptures(Context, Captures.begin(), Captures.end(), |
| 8783 | BSI->CXXThisCaptureIndex != 0); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8784 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8785 | // If the user wrote a function type in some form, try to use that. |
| 8786 | if (!BSI->FunctionType.isNull()) { |
| 8787 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 8788 | |
| 8789 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 8790 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 8791 | |
| 8792 | // Turn protoless block types into nullary block types. |
| 8793 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8794 | FunctionProtoType::ExtProtoInfo EPI; |
| 8795 | EPI.ExtInfo = Ext; |
| 8796 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8797 | |
| 8798 | // Otherwise, if we don't need to change anything about the function type, |
| 8799 | // preserve its sugar structure. |
| 8800 | } else if (FTy->getResultType() == RetTy && |
| 8801 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 8802 | BlockTy = BSI->FunctionType; |
| 8803 | |
| 8804 | // Otherwise, make the minimal modifications to the function type. |
| 8805 | } else { |
| 8806 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8807 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8808 | EPI.TypeQuals = 0; // FIXME: silently? |
| 8809 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8810 | BlockTy = Context.getFunctionType(RetTy, |
| 8811 | FPT->arg_type_begin(), |
| 8812 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8813 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8814 | } |
| 8815 | |
| 8816 | // If we don't have a function type, just build one from nothing. |
| 8817 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8818 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8819 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8820 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8821 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8822 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8823 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 8824 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8825 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8826 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 8827 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8828 | if (getCurFunction()->NeedsScopeChecking() && |
| 8829 | !hasAnyUnrecoverableErrorsInThisFunction()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8830 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8831 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8832 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8833 | |
Fariborz Jahanian | 4e7c7f2 | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 8834 | for (BlockDecl::capture_const_iterator ci = BSI->TheDecl->capture_begin(), |
| 8835 | ce = BSI->TheDecl->capture_end(); ci != ce; ++ci) { |
| 8836 | const VarDecl *variable = ci->getVariable(); |
| 8837 | QualType T = variable->getType(); |
| 8838 | QualType::DestructionKind destructKind = T.isDestructedType(); |
| 8839 | if (destructKind != QualType::DK_none) |
| 8840 | getCurFunction()->setHasBranchProtectedScope(); |
| 8841 | } |
| 8842 | |
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 8843 | computeNRVO(Body, getCurBlock()); |
| 8844 | |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 8845 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
| 8846 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 8847 | PopFunctionScopeInfo(&WP, Result->getBlockDecl(), Result); |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 8848 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 8849 | // If the block isn't obviously global, i.e. it captures anything at |
| 8850 | // all, mark this full-expression as needing a cleanup. |
| 8851 | if (Result->getBlockDecl()->hasCaptures()) { |
| 8852 | ExprCleanupObjects.push_back(Result->getBlockDecl()); |
| 8853 | ExprNeedsCleanups = true; |
| 8854 | } |
| 8855 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8856 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8857 | } |
| 8858 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8859 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8860 | Expr *E, ParsedType Ty, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8861 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8862 | TypeSourceInfo *TInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8863 | GetTypeFromParser(Ty, &TInfo); |
| 8864 | return BuildVAArgExpr(BuiltinLoc, E, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8865 | } |
| 8866 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8867 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8868 | Expr *E, TypeSourceInfo *TInfo, |
| 8869 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8870 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8871 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8872 | // Get the va_list type |
| 8873 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8874 | if (VaListType->isArrayType()) { |
| 8875 | // Deal with implicit array decay; for example, on x86-64, |
| 8876 | // va_list is an array, but it's supposed to decay to |
| 8877 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8878 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8879 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8880 | ExprResult Result = UsualUnaryConversions(E); |
| 8881 | if (Result.isInvalid()) |
| 8882 | return ExprError(); |
| 8883 | E = Result.take(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8884 | } else { |
| 8885 | // Otherwise, the va_list argument must be an l-value because |
| 8886 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8887 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8888 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8889 | return ExprError(); |
| 8890 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8891 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8892 | if (!E->isTypeDependent() && |
| 8893 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8894 | return ExprError(Diag(E->getLocStart(), |
| 8895 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8896 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 8897 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8898 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8899 | if (!TInfo->getType()->isDependentType()) { |
| 8900 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
| 8901 | PDiag(diag::err_second_parameter_to_va_arg_incomplete) |
| 8902 | << TInfo->getTypeLoc().getSourceRange())) |
| 8903 | return ExprError(); |
David Majnemer | db11b01 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 8904 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8905 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
| 8906 | TInfo->getType(), |
| 8907 | PDiag(diag::err_second_parameter_to_va_arg_abstract) |
| 8908 | << TInfo->getTypeLoc().getSourceRange())) |
| 8909 | return ExprError(); |
| 8910 | |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8911 | if (!TInfo->getType().isPODType(Context)) { |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8912 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8913 | TInfo->getType()->isObjCLifetimeType() |
| 8914 | ? diag::warn_second_parameter_to_va_arg_ownership_qualified |
| 8915 | : diag::warn_second_parameter_to_va_arg_not_pod) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8916 | << TInfo->getType() |
| 8917 | << TInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8918 | } |
Eli Friedman | 46d37c1 | 2011-07-11 21:45:59 +0000 | [diff] [blame] | 8919 | |
| 8920 | // Check for va_arg where arguments of the given type will be promoted |
| 8921 | // (i.e. this va_arg is guaranteed to have undefined behavior). |
| 8922 | QualType PromoteType; |
| 8923 | if (TInfo->getType()->isPromotableIntegerType()) { |
| 8924 | PromoteType = Context.getPromotedIntegerType(TInfo->getType()); |
| 8925 | if (Context.typesAreCompatible(PromoteType, TInfo->getType())) |
| 8926 | PromoteType = QualType(); |
| 8927 | } |
| 8928 | if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) |
| 8929 | PromoteType = Context.DoubleTy; |
| 8930 | if (!PromoteType.isNull()) |
| 8931 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 8932 | diag::warn_second_parameter_to_va_arg_never_compatible) |
| 8933 | << TInfo->getType() |
| 8934 | << PromoteType |
| 8935 | << TInfo->getTypeLoc().getSourceRange(); |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8936 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8937 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8938 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 8939 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 8940 | } |
| 8941 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8942 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8943 | // The type of __null will be int or long, depending on the size of |
| 8944 | // pointers on the target. |
| 8945 | QualType Ty; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8946 | unsigned pw = Context.getTargetInfo().getPointerWidth(0); |
| 8947 | if (pw == Context.getTargetInfo().getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8948 | Ty = Context.IntTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8949 | else if (pw == Context.getTargetInfo().getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8950 | Ty = Context.LongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8951 | else if (pw == Context.getTargetInfo().getLongLongWidth()) |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8952 | Ty = Context.LongLongTy; |
| 8953 | else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8954 | llvm_unreachable("I don't know size of pointer!"); |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8955 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8956 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8957 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8958 | } |
| 8959 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8960 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8961 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8962 | if (!SemaRef.getLangOptions().ObjC1) |
| 8963 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8964 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8965 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 8966 | if (!PT) |
| 8967 | return; |
| 8968 | |
| 8969 | // Check if the destination is of type 'id'. |
| 8970 | if (!PT->isObjCIdType()) { |
| 8971 | // Check if the destination is the 'NSString' interface. |
| 8972 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 8973 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 8974 | return; |
| 8975 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8976 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 8977 | // Ignore any parens, implicit casts (should only be |
| 8978 | // array-to-pointer decays), and not-so-opaque values. The last is |
| 8979 | // important for making this trigger for property assignments. |
| 8980 | SrcExpr = SrcExpr->IgnoreParenImpCasts(); |
| 8981 | if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(SrcExpr)) |
| 8982 | if (OV->getSourceExpr()) |
| 8983 | SrcExpr = OV->getSourceExpr()->IgnoreParenImpCasts(); |
| 8984 | |
| 8985 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 8986 | if (!SL || !SL->isAscii()) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8987 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8988 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8989 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8990 | } |
| 8991 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8992 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 8993 | SourceLocation Loc, |
| 8994 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8995 | Expr *SrcExpr, AssignmentAction Action, |
| 8996 | bool *Complained) { |
| 8997 | if (Complained) |
| 8998 | *Complained = false; |
| 8999 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9000 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9001 | bool CheckInferredResultType = false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9002 | bool isInvalid = false; |
Eli Friedman | fd81978 | 2012-02-29 20:59:56 +0000 | [diff] [blame] | 9003 | unsigned DiagKind = 0; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9004 | FixItHint Hint; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9005 | ConversionFixItGenerator ConvHints; |
| 9006 | bool MayHaveConvFixit = false; |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9007 | bool MayHaveFunctionDiff = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9008 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9009 | switch (ConvTy) { |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9010 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9011 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9012 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9013 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9014 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9015 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9016 | case IntToPointer: |
| 9017 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9018 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9019 | MayHaveConvFixit = true; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9020 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9021 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9022 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9023 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9024 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 9025 | SrcType->isObjCObjectPointerType(); |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9026 | if (Hint.isNull() && !CheckInferredResultType) { |
| 9027 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9028 | } |
| 9029 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9030 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9031 | case IncompatiblePointerSign: |
| 9032 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 9033 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9034 | case FunctionVoidPointer: |
| 9035 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 9036 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9037 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9038 | // Perform array-to-pointer decay if necessary. |
| 9039 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9040 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9041 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9042 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9043 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9044 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9045 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9046 | |
| 9047 | |
| 9048 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 9049 | DiagKind = diag::err_typecheck_incompatible_ownership; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9050 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9051 | } |
| 9052 | |
| 9053 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9054 | // fallthrough |
| 9055 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9056 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9057 | // If the qualifiers lost were because we were applying the |
| 9058 | // (deprecated) C++ conversion from a string literal to a char* |
| 9059 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9060 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9061 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9062 | // bit of refactoring (so that the second argument is an |
| 9063 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9064 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9065 | // C++ semantics. |
| 9066 | if (getLangOptions().CPlusPlus && |
| 9067 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9068 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9069 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9070 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9071 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9072 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9073 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9074 | case IntToBlockPointer: |
| 9075 | DiagKind = diag::err_int_to_block_pointer; |
| 9076 | break; |
| 9077 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9078 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9079 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9080 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9081 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9082 | // it can give a more specific diagnostic. |
| 9083 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9084 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9085 | case IncompatibleVectors: |
| 9086 | DiagKind = diag::warn_incompatible_vectors; |
| 9087 | break; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 9088 | case IncompatibleObjCWeakRef: |
| 9089 | DiagKind = diag::err_arc_weak_unavailable_assign; |
| 9090 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9091 | case Incompatible: |
| 9092 | DiagKind = diag::err_typecheck_convert_incompatible; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9093 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9094 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9095 | isInvalid = true; |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9096 | MayHaveFunctionDiff = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9097 | break; |
| 9098 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9099 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9100 | QualType FirstType, SecondType; |
| 9101 | switch (Action) { |
| 9102 | case AA_Assigning: |
| 9103 | case AA_Initializing: |
| 9104 | // The destination type comes first. |
| 9105 | FirstType = DstType; |
| 9106 | SecondType = SrcType; |
| 9107 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9108 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9109 | case AA_Returning: |
| 9110 | case AA_Passing: |
| 9111 | case AA_Converting: |
| 9112 | case AA_Sending: |
| 9113 | case AA_Casting: |
| 9114 | // The source type comes first. |
| 9115 | FirstType = SrcType; |
| 9116 | SecondType = DstType; |
| 9117 | break; |
| 9118 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9119 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9120 | PartialDiagnostic FDiag = PDiag(DiagKind); |
| 9121 | FDiag << FirstType << SecondType << Action << SrcExpr->getSourceRange(); |
| 9122 | |
| 9123 | // If we can fix the conversion, suggest the FixIts. |
| 9124 | assert(ConvHints.isNull() || Hint.isNull()); |
| 9125 | if (!ConvHints.isNull()) { |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 9126 | for (std::vector<FixItHint>::iterator HI = ConvHints.Hints.begin(), |
| 9127 | HE = ConvHints.Hints.end(); HI != HE; ++HI) |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9128 | FDiag << *HI; |
| 9129 | } else { |
| 9130 | FDiag << Hint; |
| 9131 | } |
| 9132 | if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } |
| 9133 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9134 | if (MayHaveFunctionDiff) |
| 9135 | HandleFunctionTypeMismatch(FDiag, SecondType, FirstType); |
| 9136 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9137 | Diag(Loc, FDiag); |
| 9138 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9139 | if (SecondType == Context.OverloadTy) |
| 9140 | NoteAllOverloadCandidates(OverloadExpr::find(SrcExpr).Expression, |
| 9141 | FirstType); |
| 9142 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9143 | if (CheckInferredResultType) |
| 9144 | EmitRelatedResultTypeNote(SrcExpr); |
| 9145 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9146 | if (Complained) |
| 9147 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9148 | return isInvalid; |
| 9149 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9150 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9151 | ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, |
| 9152 | llvm::APSInt *Result) { |
| 9153 | return VerifyIntegerConstantExpression(E, Result, |
| 9154 | PDiag(diag::err_expr_not_ice) << LangOpts.CPlusPlus); |
| 9155 | } |
| 9156 | |
| 9157 | ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, |
| 9158 | PartialDiagnostic NotIceDiag, |
| 9159 | bool AllowFold, |
| 9160 | PartialDiagnostic FoldDiag) { |
| 9161 | SourceLocation DiagLoc = E->getSourceRange().getBegin(); |
| 9162 | |
| 9163 | if (getLangOptions().CPlusPlus0x) { |
| 9164 | // C++11 [expr.const]p5: |
| 9165 | // If an expression of literal class type is used in a context where an |
| 9166 | // integral constant expression is required, then that class type shall |
| 9167 | // have a single non-explicit conversion function to an integral or |
| 9168 | // unscoped enumeration type |
| 9169 | ExprResult Converted; |
| 9170 | if (NotIceDiag.getDiagID()) { |
| 9171 | Converted = ConvertToIntegralOrEnumerationType( |
| 9172 | DiagLoc, E, |
| 9173 | PDiag(diag::err_ice_not_integral), |
| 9174 | PDiag(diag::err_ice_incomplete_type), |
| 9175 | PDiag(diag::err_ice_explicit_conversion), |
| 9176 | PDiag(diag::note_ice_conversion_here), |
| 9177 | PDiag(diag::err_ice_ambiguous_conversion), |
| 9178 | PDiag(diag::note_ice_conversion_here), |
| 9179 | PDiag(0), |
| 9180 | /*AllowScopedEnumerations*/ false); |
| 9181 | } else { |
| 9182 | // The caller wants to silently enquire whether this is an ICE. Don't |
| 9183 | // produce any diagnostics if it isn't. |
| 9184 | Converted = ConvertToIntegralOrEnumerationType( |
| 9185 | DiagLoc, E, PDiag(), PDiag(), PDiag(), PDiag(), |
| 9186 | PDiag(), PDiag(), PDiag(), false); |
| 9187 | } |
| 9188 | if (Converted.isInvalid()) |
| 9189 | return Converted; |
| 9190 | E = Converted.take(); |
| 9191 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) |
| 9192 | return ExprError(); |
| 9193 | } else if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { |
| 9194 | // An ICE must be of integral or unscoped enumeration type. |
| 9195 | if (NotIceDiag.getDiagID()) |
| 9196 | Diag(DiagLoc, NotIceDiag) << E->getSourceRange(); |
| 9197 | return ExprError(); |
| 9198 | } |
| 9199 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 9200 | // Circumvent ICE checking in C++11 to avoid evaluating the expression twice |
| 9201 | // in the non-ICE case. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9202 | if (!getLangOptions().CPlusPlus0x && E->isIntegerConstantExpr(Context)) { |
| 9203 | if (Result) |
| 9204 | *Result = E->EvaluateKnownConstInt(Context); |
| 9205 | return Owned(E); |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9206 | } |
| 9207 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9208 | Expr::EvalResult EvalResult; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 9209 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 9210 | EvalResult.Diag = &Notes; |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9211 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 9212 | // Try to evaluate the expression, and produce diagnostics explaining why it's |
| 9213 | // not a constant expression as a side-effect. |
| 9214 | bool Folded = E->EvaluateAsRValue(EvalResult, Context) && |
| 9215 | EvalResult.Val.isInt() && !EvalResult.HasSideEffects; |
| 9216 | |
| 9217 | // In C++11, we can rely on diagnostics being produced for any expression |
| 9218 | // which is not a constant expression. If no diagnostics were produced, then |
| 9219 | // this is a constant expression. |
| 9220 | if (Folded && getLangOptions().CPlusPlus0x && Notes.empty()) { |
| 9221 | if (Result) |
| 9222 | *Result = EvalResult.Val.getInt(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9223 | return Owned(E); |
| 9224 | } |
| 9225 | |
| 9226 | // If our only note is the usual "invalid subexpression" note, just point |
| 9227 | // the caret at its location rather than producing an essentially |
| 9228 | // redundant note. |
| 9229 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 9230 | diag::note_invalid_subexpr_in_const_expr) { |
| 9231 | DiagLoc = Notes[0].first; |
| 9232 | Notes.clear(); |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 9233 | } |
| 9234 | |
| 9235 | if (!Folded || !AllowFold) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9236 | if (NotIceDiag.getDiagID()) { |
| 9237 | Diag(DiagLoc, NotIceDiag) << E->getSourceRange(); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 9238 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 9239 | Diag(Notes[I].first, Notes[I].second); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9240 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9241 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9242 | return ExprError(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9243 | } |
| 9244 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9245 | if (FoldDiag.getDiagID()) |
| 9246 | Diag(DiagLoc, FoldDiag) << E->getSourceRange(); |
| 9247 | else |
| 9248 | Diag(DiagLoc, diag::ext_expr_not_ice) |
| 9249 | << E->getSourceRange() << LangOpts.CPlusPlus; |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 9250 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 9251 | Diag(Notes[I].first, Notes[I].second); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9252 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9253 | if (Result) |
| 9254 | *Result = EvalResult.Val.getInt(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9255 | return Owned(E); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9256 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9257 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 9258 | namespace { |
| 9259 | // Handle the case where we conclude a expression which we speculatively |
| 9260 | // considered to be unevaluated is actually evaluated. |
| 9261 | class TransformToPE : public TreeTransform<TransformToPE> { |
| 9262 | typedef TreeTransform<TransformToPE> BaseTransform; |
| 9263 | |
| 9264 | public: |
| 9265 | TransformToPE(Sema &SemaRef) : BaseTransform(SemaRef) { } |
| 9266 | |
| 9267 | // Make sure we redo semantic analysis |
| 9268 | bool AlwaysRebuild() { return true; } |
| 9269 | |
Eli Friedman | 56ff283 | 2012-02-06 23:29:57 +0000 | [diff] [blame] | 9270 | // Make sure we handle LabelStmts correctly. |
| 9271 | // FIXME: This does the right thing, but maybe we need a more general |
| 9272 | // fix to TreeTransform? |
| 9273 | StmtResult TransformLabelStmt(LabelStmt *S) { |
| 9274 | S->getDecl()->setStmt(0); |
| 9275 | return BaseTransform::TransformLabelStmt(S); |
| 9276 | } |
| 9277 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 9278 | // We need to special-case DeclRefExprs referring to FieldDecls which |
| 9279 | // are not part of a member pointer formation; normal TreeTransforming |
| 9280 | // doesn't catch this case because of the way we represent them in the AST. |
| 9281 | // FIXME: This is a bit ugly; is it really the best way to handle this |
| 9282 | // case? |
| 9283 | // |
| 9284 | // Error on DeclRefExprs referring to FieldDecls. |
| 9285 | ExprResult TransformDeclRefExpr(DeclRefExpr *E) { |
| 9286 | if (isa<FieldDecl>(E->getDecl()) && |
| 9287 | SemaRef.ExprEvalContexts.back().Context != Sema::Unevaluated) |
| 9288 | return SemaRef.Diag(E->getLocation(), |
| 9289 | diag::err_invalid_non_static_member_use) |
| 9290 | << E->getDecl() << E->getSourceRange(); |
| 9291 | |
| 9292 | return BaseTransform::TransformDeclRefExpr(E); |
| 9293 | } |
| 9294 | |
| 9295 | // Exception: filter out member pointer formation |
| 9296 | ExprResult TransformUnaryOperator(UnaryOperator *E) { |
| 9297 | if (E->getOpcode() == UO_AddrOf && E->getType()->isMemberPointerType()) |
| 9298 | return E; |
| 9299 | |
| 9300 | return BaseTransform::TransformUnaryOperator(E); |
| 9301 | } |
| 9302 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 9303 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 9304 | // Lambdas never need to be transformed. |
| 9305 | return E; |
| 9306 | } |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 9307 | }; |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 9308 | } |
| 9309 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 9310 | ExprResult Sema::TranformToPotentiallyEvaluated(Expr *E) { |
Eli Friedman | 72b8b1e | 2012-02-29 04:03:55 +0000 | [diff] [blame] | 9311 | assert(ExprEvalContexts.back().Context == Unevaluated && |
| 9312 | "Should only transform unevaluated expressions"); |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 9313 | ExprEvalContexts.back().Context = |
| 9314 | ExprEvalContexts[ExprEvalContexts.size()-2].Context; |
| 9315 | if (ExprEvalContexts.back().Context == Unevaluated) |
| 9316 | return E; |
| 9317 | return TransformToPE(*this).TransformExpr(E); |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 9318 | } |
| 9319 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9320 | void |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 9321 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext, |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 9322 | Decl *LambdaContextDecl, |
| 9323 | bool IsDecltype) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9324 | ExprEvalContexts.push_back( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9325 | ExpressionEvaluationContextRecord(NewContext, |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9326 | ExprCleanupObjects.size(), |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 9327 | ExprNeedsCleanups, |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 9328 | LambdaContextDecl, |
| 9329 | IsDecltype)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9330 | ExprNeedsCleanups = false; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 9331 | if (!MaybeODRUseExprs.empty()) |
| 9332 | std::swap(MaybeODRUseExprs, ExprEvalContexts.back().SavedMaybeODRUseExprs); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9333 | } |
| 9334 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9335 | void Sema::PopExpressionEvaluationContext() { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9336 | ExpressionEvaluationContextRecord& Rec = ExprEvalContexts.back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9337 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 9338 | if (!Rec.Lambdas.empty()) { |
| 9339 | if (Rec.Context == Unevaluated) { |
| 9340 | // C++11 [expr.prim.lambda]p2: |
| 9341 | // A lambda-expression shall not appear in an unevaluated operand |
| 9342 | // (Clause 5). |
| 9343 | for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I) |
| 9344 | Diag(Rec.Lambdas[I]->getLocStart(), |
| 9345 | diag::err_lambda_unevaluated_operand); |
| 9346 | } else { |
| 9347 | // Mark the capture expressions odr-used. This was deferred |
| 9348 | // during lambda expression creation. |
| 9349 | for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I) { |
| 9350 | LambdaExpr *Lambda = Rec.Lambdas[I]; |
| 9351 | for (LambdaExpr::capture_init_iterator |
| 9352 | C = Lambda->capture_init_begin(), |
| 9353 | CEnd = Lambda->capture_init_end(); |
| 9354 | C != CEnd; ++C) { |
| 9355 | MarkDeclarationsReferencedInExpr(*C); |
| 9356 | } |
| 9357 | } |
| 9358 | } |
| 9359 | } |
| 9360 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9361 | // When are coming out of an unevaluated context, clear out any |
| 9362 | // temporaries that we may have created as part of the evaluation of |
| 9363 | // the expression in that context: they aren't relevant because they |
| 9364 | // will never be constructed. |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 9365 | if (Rec.Context == Unevaluated || Rec.Context == ConstantEvaluated) { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9366 | ExprCleanupObjects.erase(ExprCleanupObjects.begin() + Rec.NumCleanupObjects, |
| 9367 | ExprCleanupObjects.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9368 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 9369 | CleanupVarDeclMarking(); |
| 9370 | std::swap(MaybeODRUseExprs, Rec.SavedMaybeODRUseExprs); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9371 | // Otherwise, merge the contexts together. |
| 9372 | } else { |
| 9373 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 9374 | MaybeODRUseExprs.insert(Rec.SavedMaybeODRUseExprs.begin(), |
| 9375 | Rec.SavedMaybeODRUseExprs.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9376 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9377 | |
| 9378 | // Pop the current expression evaluation context off the stack. |
| 9379 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9380 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9381 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9382 | void Sema::DiscardCleanupsInEvaluationContext() { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9383 | ExprCleanupObjects.erase( |
| 9384 | ExprCleanupObjects.begin() + ExprEvalContexts.back().NumCleanupObjects, |
| 9385 | ExprCleanupObjects.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9386 | ExprNeedsCleanups = false; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 9387 | MaybeODRUseExprs.clear(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9388 | } |
| 9389 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 9390 | ExprResult Sema::HandleExprEvaluationContextForTypeof(Expr *E) { |
| 9391 | if (!E->getType()->isVariablyModifiedType()) |
| 9392 | return E; |
| 9393 | return TranformToPotentiallyEvaluated(E); |
| 9394 | } |
| 9395 | |
Benjamin Kramer | 5bbc385 | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 9396 | static bool IsPotentiallyEvaluatedContext(Sema &SemaRef) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9397 | // Do not mark anything as "used" within a dependent context; wait for |
| 9398 | // an instantiation. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9399 | if (SemaRef.CurContext->isDependentContext()) |
| 9400 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9401 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9402 | switch (SemaRef.ExprEvalContexts.back().Context) { |
| 9403 | case Sema::Unevaluated: |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9404 | // We are in an expression that is not potentially evaluated; do nothing. |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 9405 | // (Depending on how you read the standard, we actually do need to do |
| 9406 | // something here for null pointer constants, but the standard's |
| 9407 | // definition of a null pointer constant is completely crazy.) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9408 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9409 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9410 | case Sema::ConstantEvaluated: |
| 9411 | case Sema::PotentiallyEvaluated: |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 9412 | // We are in a potentially evaluated expression (or a constant-expression |
| 9413 | // in C++03); we need to do implicit template instantiation, implicitly |
| 9414 | // define class members, and mark most declarations as used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9415 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9416 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9417 | case Sema::PotentiallyEvaluatedIfUsed: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9418 | // Referenced declarations will only be used if the construct in the |
| 9419 | // containing expression is used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9420 | return false; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9421 | } |
Matt Beaumont-Gay | 4f7dcdb | 2012-02-02 18:35:35 +0000 | [diff] [blame] | 9422 | llvm_unreachable("Invalid context"); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9423 | } |
| 9424 | |
| 9425 | /// \brief Mark a function referenced, and check whether it is odr-used |
| 9426 | /// (C++ [basic.def.odr]p2, C99 6.9p3) |
| 9427 | void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func) { |
| 9428 | assert(Func && "No function?"); |
| 9429 | |
| 9430 | Func->setReferenced(); |
| 9431 | |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 9432 | // Don't mark this function as used multiple times, unless it's a constexpr |
| 9433 | // function which we need to instantiate. |
| 9434 | if (Func->isUsed(false) && |
| 9435 | !(Func->isConstexpr() && !Func->getBody() && |
| 9436 | Func->isImplicitlyInstantiable())) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9437 | return; |
| 9438 | |
| 9439 | if (!IsPotentiallyEvaluatedContext(*this)) |
| 9440 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9441 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9442 | // Note that this declaration has been used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9443 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9444 | if (Constructor->isDefaulted() && !Constructor->isDeleted()) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9445 | if (Constructor->isDefaultConstructor()) { |
| 9446 | if (Constructor->isTrivial()) |
| 9447 | return; |
| 9448 | if (!Constructor->isUsed(false)) |
| 9449 | DefineImplicitDefaultConstructor(Loc, Constructor); |
| 9450 | } else if (Constructor->isCopyConstructor()) { |
| 9451 | if (!Constructor->isUsed(false)) |
| 9452 | DefineImplicitCopyConstructor(Loc, Constructor); |
| 9453 | } else if (Constructor->isMoveConstructor()) { |
| 9454 | if (!Constructor->isUsed(false)) |
| 9455 | DefineImplicitMoveConstructor(Loc, Constructor); |
| 9456 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9457 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9458 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9459 | MarkVTableUsed(Loc, Constructor->getParent()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9460 | } else if (CXXDestructorDecl *Destructor = |
| 9461 | dyn_cast<CXXDestructorDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9462 | if (Destructor->isDefaulted() && !Destructor->isDeleted() && |
| 9463 | !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9464 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9465 | if (Destructor->isVirtual()) |
| 9466 | MarkVTableUsed(Loc, Destructor->getParent()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9467 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9468 | if (MethodDecl->isDefaulted() && !MethodDecl->isDeleted() && |
| 9469 | MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9470 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9471 | if (!MethodDecl->isUsed(false)) { |
| 9472 | if (MethodDecl->isCopyAssignmentOperator()) |
| 9473 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
| 9474 | else |
| 9475 | DefineImplicitMoveAssignment(Loc, MethodDecl); |
| 9476 | } |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9477 | } else if (isa<CXXConversionDecl>(MethodDecl) && |
| 9478 | MethodDecl->getParent()->isLambda()) { |
| 9479 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(MethodDecl); |
| 9480 | if (Conversion->isLambdaToBlockPointerConversion()) |
| 9481 | DefineImplicitLambdaToBlockPointerConversion(Loc, Conversion); |
| 9482 | else |
| 9483 | DefineImplicitLambdaToFunctionPointerConversion(Loc, Conversion); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9484 | } else if (MethodDecl->isVirtual()) |
| 9485 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9486 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9487 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9488 | // Recursive functions should be marked when used from another function. |
| 9489 | // FIXME: Is this really right? |
| 9490 | if (CurContext == Func) return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9491 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9492 | // Implicit instantiation of function templates and member functions of |
| 9493 | // class templates. |
| 9494 | if (Func->isImplicitlyInstantiable()) { |
| 9495 | bool AlreadyInstantiated = false; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 9496 | SourceLocation PointOfInstantiation = Loc; |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9497 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 9498 | = Func->getTemplateSpecializationInfo()) { |
| 9499 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 9500 | SpecInfo->setPointOfInstantiation(Loc); |
| 9501 | else if (SpecInfo->getTemplateSpecializationKind() |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 9502 | == TSK_ImplicitInstantiation) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9503 | AlreadyInstantiated = true; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 9504 | PointOfInstantiation = SpecInfo->getPointOfInstantiation(); |
| 9505 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9506 | } else if (MemberSpecializationInfo *MSInfo |
| 9507 | = Func->getMemberSpecializationInfo()) { |
| 9508 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9509 | MSInfo->setPointOfInstantiation(Loc); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9510 | else if (MSInfo->getTemplateSpecializationKind() |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 9511 | == TSK_ImplicitInstantiation) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9512 | AlreadyInstantiated = true; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 9513 | PointOfInstantiation = MSInfo->getPointOfInstantiation(); |
| 9514 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9515 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9516 | |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 9517 | if (!AlreadyInstantiated || Func->isConstexpr()) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9518 | if (isa<CXXRecordDecl>(Func->getDeclContext()) && |
| 9519 | cast<CXXRecordDecl>(Func->getDeclContext())->isLocalClass()) |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 9520 | PendingLocalImplicitInstantiations.push_back( |
| 9521 | std::make_pair(Func, PointOfInstantiation)); |
| 9522 | else if (Func->isConstexpr()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9523 | // Do not defer instantiations of constexpr functions, to avoid the |
| 9524 | // expression evaluator needing to call back into Sema if it sees a |
| 9525 | // call to such a function. |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 9526 | InstantiateFunctionDefinition(PointOfInstantiation, Func); |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 9527 | else { |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 9528 | PendingInstantiations.push_back(std::make_pair(Func, |
| 9529 | PointOfInstantiation)); |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 9530 | // Notify the consumer that a function was implicitly instantiated. |
| 9531 | Consumer.HandleCXXImplicitFunctionInstantiation(Func); |
| 9532 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9533 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9534 | } else { |
| 9535 | // Walk redefinitions, as some of them may be instantiable. |
| 9536 | for (FunctionDecl::redecl_iterator i(Func->redecls_begin()), |
| 9537 | e(Func->redecls_end()); i != e; ++i) { |
| 9538 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
| 9539 | MarkFunctionReferenced(Loc, *i); |
| 9540 | } |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 9541 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9542 | |
| 9543 | // Keep track of used but undefined functions. |
| 9544 | if (!Func->isPure() && !Func->hasBody() && |
| 9545 | Func->getLinkage() != ExternalLinkage) { |
| 9546 | SourceLocation &old = UndefinedInternals[Func->getCanonicalDecl()]; |
| 9547 | if (old.isInvalid()) old = Loc; |
| 9548 | } |
| 9549 | |
| 9550 | Func->setUsed(true); |
| 9551 | } |
| 9552 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9553 | static void |
| 9554 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
| 9555 | VarDecl *var, DeclContext *DC) { |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 9556 | DeclContext *VarDC = var->getDeclContext(); |
| 9557 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9558 | // If the parameter still belongs to the translation unit, then |
| 9559 | // we're actually just using one parameter in the declaration of |
| 9560 | // the next. |
| 9561 | if (isa<ParmVarDecl>(var) && |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 9562 | isa<TranslationUnitDecl>(VarDC)) |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9563 | return; |
| 9564 | |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 9565 | // For C code, don't diagnose about capture if we're not actually in code |
| 9566 | // right now; it's impossible to write a non-constant expression outside of |
| 9567 | // function context, so we'll get other (more useful) diagnostics later. |
| 9568 | // |
| 9569 | // For C++, things get a bit more nasty... it would be nice to suppress this |
| 9570 | // diagnostic for certain cases like using a local variable in an array bound |
| 9571 | // for a member of a local class, but the correct predicate is not obvious. |
| 9572 | if (!S.getLangOptions().CPlusPlus && !S.CurContext->isFunctionOrMethod()) |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9573 | return; |
| 9574 | |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 9575 | if (isa<CXXMethodDecl>(VarDC) && |
| 9576 | cast<CXXRecordDecl>(VarDC->getParent())->isLambda()) { |
| 9577 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_lambda) |
| 9578 | << var->getIdentifier(); |
| 9579 | } else if (FunctionDecl *fn = dyn_cast<FunctionDecl>(VarDC)) { |
| 9580 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 9581 | << var->getIdentifier() << fn->getDeclName(); |
| 9582 | } else if (isa<BlockDecl>(VarDC)) { |
| 9583 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_block) |
| 9584 | << var->getIdentifier(); |
| 9585 | } else { |
| 9586 | // FIXME: Is there any other context where a local variable can be |
| 9587 | // declared? |
| 9588 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_context) |
| 9589 | << var->getIdentifier(); |
| 9590 | } |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9591 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9592 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 9593 | << var->getIdentifier(); |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 9594 | |
| 9595 | // FIXME: Add additional diagnostic info about class etc. which prevents |
| 9596 | // capture. |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9597 | } |
| 9598 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9599 | /// \brief Capture the given variable in the given lambda expression. |
| 9600 | static ExprResult captureInLambda(Sema &S, LambdaScopeInfo *LSI, |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9601 | VarDecl *Var, QualType FieldType, |
| 9602 | QualType DeclRefType, |
| 9603 | SourceLocation Loc) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9604 | CXXRecordDecl *Lambda = LSI->Lambda; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9605 | |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 9606 | // Build the non-static data member. |
| 9607 | FieldDecl *Field |
| 9608 | = FieldDecl::Create(S.Context, Lambda, Loc, Loc, 0, FieldType, |
| 9609 | S.Context.getTrivialTypeSourceInfo(FieldType, Loc), |
| 9610 | 0, false, false); |
| 9611 | Field->setImplicit(true); |
| 9612 | Field->setAccess(AS_private); |
Douglas Gregor | 20f87a4 | 2012-02-09 02:12:34 +0000 | [diff] [blame] | 9613 | Lambda->addDecl(Field); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 9614 | |
| 9615 | // C++11 [expr.prim.lambda]p21: |
| 9616 | // When the lambda-expression is evaluated, the entities that |
| 9617 | // are captured by copy are used to direct-initialize each |
| 9618 | // corresponding non-static data member of the resulting closure |
| 9619 | // object. (For array members, the array elements are |
| 9620 | // direct-initialized in increasing subscript order.) These |
| 9621 | // initializations are performed in the (unspecified) order in |
| 9622 | // which the non-static data members are declared. |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 9623 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 9624 | // Introduce a new evaluation context for the initialization, so |
| 9625 | // that temporaries introduced as part of the capture are retained |
| 9626 | // to be re-"exported" from the lambda expression itself. |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 9627 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 9628 | |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 9629 | // C++ [expr.prim.labda]p12: |
| 9630 | // An entity captured by a lambda-expression is odr-used (3.2) in |
| 9631 | // the scope containing the lambda-expression. |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9632 | Expr *Ref = new (S.Context) DeclRefExpr(Var, DeclRefType, VK_LValue, Loc); |
Eli Friedman | 88530d5 | 2012-03-01 21:32:56 +0000 | [diff] [blame^] | 9633 | Var->setReferenced(true); |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 9634 | Var->setUsed(true); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 9635 | |
| 9636 | // When the field has array type, create index variables for each |
| 9637 | // dimension of the array. We use these index variables to subscript |
| 9638 | // the source array, and other clients (e.g., CodeGen) will perform |
| 9639 | // the necessary iteration with these index variables. |
| 9640 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 9641 | QualType BaseType = FieldType; |
| 9642 | QualType SizeType = S.Context.getSizeType(); |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 9643 | LSI->ArrayIndexStarts.push_back(LSI->ArrayIndexVars.size()); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 9644 | while (const ConstantArrayType *Array |
| 9645 | = S.Context.getAsConstantArrayType(BaseType)) { |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 9646 | // Create the iteration variable for this array index. |
| 9647 | IdentifierInfo *IterationVarName = 0; |
| 9648 | { |
| 9649 | SmallString<8> Str; |
| 9650 | llvm::raw_svector_ostream OS(Str); |
| 9651 | OS << "__i" << IndexVariables.size(); |
| 9652 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 9653 | } |
| 9654 | VarDecl *IterationVar |
| 9655 | = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
| 9656 | IterationVarName, SizeType, |
| 9657 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
| 9658 | SC_None, SC_None); |
| 9659 | IndexVariables.push_back(IterationVar); |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 9660 | LSI->ArrayIndexVars.push_back(IterationVar); |
| 9661 | |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 9662 | // Create a reference to the iteration variable. |
| 9663 | ExprResult IterationVarRef |
| 9664 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); |
| 9665 | assert(!IterationVarRef.isInvalid() && |
| 9666 | "Reference to invented variable cannot fail!"); |
| 9667 | IterationVarRef = S.DefaultLvalueConversion(IterationVarRef.take()); |
| 9668 | assert(!IterationVarRef.isInvalid() && |
| 9669 | "Conversion of invented variable cannot fail!"); |
| 9670 | |
| 9671 | // Subscript the array with this iteration variable. |
| 9672 | ExprResult Subscript = S.CreateBuiltinArraySubscriptExpr( |
| 9673 | Ref, Loc, IterationVarRef.take(), Loc); |
| 9674 | if (Subscript.isInvalid()) { |
| 9675 | S.CleanupVarDeclMarking(); |
| 9676 | S.DiscardCleanupsInEvaluationContext(); |
| 9677 | S.PopExpressionEvaluationContext(); |
| 9678 | return ExprError(); |
| 9679 | } |
| 9680 | |
| 9681 | Ref = Subscript.take(); |
| 9682 | BaseType = Array->getElementType(); |
| 9683 | } |
| 9684 | |
| 9685 | // Construct the entity that we will be initializing. For an array, this |
| 9686 | // will be first element in the array, which may require several levels |
| 9687 | // of array-subscript entities. |
| 9688 | SmallVector<InitializedEntity, 4> Entities; |
| 9689 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 9690 | Entities.push_back( |
| 9691 | InitializedEntity::InitializeLambdaCapture(Var, Field, Loc)); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 9692 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 9693 | Entities.push_back(InitializedEntity::InitializeElement(S.Context, |
| 9694 | 0, |
| 9695 | Entities.back())); |
| 9696 | |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 9697 | InitializationKind InitKind |
| 9698 | = InitializationKind::CreateDirect(Loc, Loc, Loc); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 9699 | InitializationSequence Init(S, Entities.back(), InitKind, &Ref, 1); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 9700 | ExprResult Result(true); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 9701 | if (!Init.Diagnose(S, Entities.back(), InitKind, &Ref, 1)) |
| 9702 | Result = Init.Perform(S, Entities.back(), InitKind, |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 9703 | MultiExprArg(S, &Ref, 1)); |
| 9704 | |
| 9705 | // If this initialization requires any cleanups (e.g., due to a |
| 9706 | // default argument to a copy constructor), note that for the |
| 9707 | // lambda. |
| 9708 | if (S.ExprNeedsCleanups) |
| 9709 | LSI->ExprNeedsCleanups = true; |
| 9710 | |
| 9711 | // Exit the expression evaluation context used for the capture. |
| 9712 | S.CleanupVarDeclMarking(); |
| 9713 | S.DiscardCleanupsInEvaluationContext(); |
| 9714 | S.PopExpressionEvaluationContext(); |
| 9715 | return Result; |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 9716 | } |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 9717 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9718 | bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc, |
| 9719 | TryCaptureKind Kind, SourceLocation EllipsisLoc, |
| 9720 | bool BuildAndDiagnose, |
| 9721 | QualType &CaptureType, |
| 9722 | QualType &DeclRefType) { |
| 9723 | bool Nested = false; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9724 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9725 | DeclContext *DC = CurContext; |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9726 | if (Var->getDeclContext() == DC) return true; |
| 9727 | if (!Var->hasLocalStorage()) return true; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9728 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9729 | bool HasBlocksAttr = Var->hasAttr<BlocksAttr>(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9730 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9731 | // Walk up the stack to determine whether we can capture the variable, |
| 9732 | // performing the "simple" checks that don't depend on type. We stop when |
| 9733 | // we've either hit the declared scope of the variable or find an existing |
| 9734 | // capture of that variable. |
| 9735 | CaptureType = Var->getType(); |
| 9736 | DeclRefType = CaptureType.getNonReferenceType(); |
| 9737 | bool Explicit = (Kind != TryCapture_Implicit); |
| 9738 | unsigned FunctionScopesIndex = FunctionScopes.size() - 1; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9739 | do { |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9740 | // Only block literals and lambda expressions can capture; other |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9741 | // scopes don't work. |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9742 | DeclContext *ParentDC; |
| 9743 | if (isa<BlockDecl>(DC)) |
| 9744 | ParentDC = DC->getParent(); |
| 9745 | else if (isa<CXXMethodDecl>(DC) && |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9746 | cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call && |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9747 | cast<CXXRecordDecl>(DC->getParent())->isLambda()) |
| 9748 | ParentDC = DC->getParent()->getParent(); |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9749 | else { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9750 | if (BuildAndDiagnose) |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9751 | diagnoseUncapturableValueReference(*this, Loc, Var, DC); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9752 | return true; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9753 | } |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9754 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9755 | CapturingScopeInfo *CSI = |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9756 | cast<CapturingScopeInfo>(FunctionScopes[FunctionScopesIndex]); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9757 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9758 | // Check whether we've already captured it. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9759 | if (CSI->CaptureMap.count(Var)) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9760 | // If we found a capture, any subcaptures are nested. |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9761 | Nested = true; |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9762 | |
| 9763 | // Retrieve the capture type for this variable. |
| 9764 | CaptureType = CSI->getCapture(Var).getCaptureType(); |
| 9765 | |
| 9766 | // Compute the type of an expression that refers to this variable. |
| 9767 | DeclRefType = CaptureType.getNonReferenceType(); |
| 9768 | |
| 9769 | const CapturingScopeInfo::Capture &Cap = CSI->getCapture(Var); |
| 9770 | if (Cap.isCopyCapture() && |
| 9771 | !(isa<LambdaScopeInfo>(CSI) && cast<LambdaScopeInfo>(CSI)->Mutable)) |
| 9772 | DeclRefType.addConst(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9773 | break; |
| 9774 | } |
| 9775 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9776 | bool IsBlock = isa<BlockScopeInfo>(CSI); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9777 | bool IsLambda = !IsBlock; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9778 | |
| 9779 | // Lambdas are not allowed to capture unnamed variables |
| 9780 | // (e.g. anonymous unions). |
| 9781 | // FIXME: The C++11 rule don't actually state this explicitly, but I'm |
| 9782 | // assuming that's the intent. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9783 | if (IsLambda && !Var->getDeclName()) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9784 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9785 | Diag(Loc, diag::err_lambda_capture_anonymous_var); |
| 9786 | Diag(Var->getLocation(), diag::note_declared_at); |
| 9787 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9788 | return true; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9789 | } |
| 9790 | |
| 9791 | // Prohibit variably-modified types; they're difficult to deal with. |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9792 | if (Var->getType()->isVariablyModifiedType()) { |
| 9793 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9794 | if (IsBlock) |
| 9795 | Diag(Loc, diag::err_ref_vm_type); |
| 9796 | else |
| 9797 | Diag(Loc, diag::err_lambda_capture_vm_type) << Var->getDeclName(); |
| 9798 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 9799 | << Var->getDeclName(); |
| 9800 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9801 | return true; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9802 | } |
| 9803 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9804 | // Lambdas are not allowed to capture __block variables; they don't |
| 9805 | // support the expected semantics. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9806 | if (IsLambda && HasBlocksAttr) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9807 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9808 | Diag(Loc, diag::err_lambda_capture_block) |
| 9809 | << Var->getDeclName(); |
| 9810 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 9811 | << Var->getDeclName(); |
| 9812 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9813 | return true; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9814 | } |
| 9815 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9816 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_None && !Explicit) { |
| 9817 | // No capture-default |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9818 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9819 | Diag(Loc, diag::err_lambda_impcap) << Var->getDeclName(); |
| 9820 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 9821 | << Var->getDeclName(); |
| 9822 | Diag(cast<LambdaScopeInfo>(CSI)->Lambda->getLocStart(), |
| 9823 | diag::note_lambda_decl); |
| 9824 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9825 | return true; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 9826 | } |
| 9827 | |
| 9828 | FunctionScopesIndex--; |
| 9829 | DC = ParentDC; |
| 9830 | Explicit = false; |
| 9831 | } while (!Var->getDeclContext()->Equals(DC)); |
| 9832 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9833 | // Walk back down the scope stack, computing the type of the capture at |
| 9834 | // each step, checking type-specific requirements, and adding captures if |
| 9835 | // requested. |
| 9836 | for (unsigned I = ++FunctionScopesIndex, N = FunctionScopes.size(); I != N; |
| 9837 | ++I) { |
| 9838 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[I]); |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 9839 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9840 | // Compute the type of the capture and of a reference to the capture within |
| 9841 | // this scope. |
| 9842 | if (isa<BlockScopeInfo>(CSI)) { |
| 9843 | Expr *CopyExpr = 0; |
| 9844 | bool ByRef = false; |
| 9845 | |
| 9846 | // Blocks are not allowed to capture arrays. |
| 9847 | if (CaptureType->isArrayType()) { |
| 9848 | if (BuildAndDiagnose) { |
| 9849 | Diag(Loc, diag::err_ref_array_type); |
| 9850 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 9851 | << Var->getDeclName(); |
| 9852 | } |
| 9853 | return true; |
| 9854 | } |
| 9855 | |
| 9856 | if (HasBlocksAttr || CaptureType->isReferenceType()) { |
| 9857 | // Block capture by reference does not change the capture or |
| 9858 | // declaration reference types. |
| 9859 | ByRef = true; |
| 9860 | } else { |
| 9861 | // Block capture by copy introduces 'const'. |
| 9862 | CaptureType = CaptureType.getNonReferenceType().withConst(); |
| 9863 | DeclRefType = CaptureType; |
| 9864 | |
| 9865 | if (getLangOptions().CPlusPlus && BuildAndDiagnose) { |
| 9866 | if (const RecordType *Record = DeclRefType->getAs<RecordType>()) { |
| 9867 | // The capture logic needs the destructor, so make sure we mark it. |
| 9868 | // Usually this is unnecessary because most local variables have |
| 9869 | // their destructors marked at declaration time, but parameters are |
| 9870 | // an exception because it's technically only the call site that |
| 9871 | // actually requires the destructor. |
| 9872 | if (isa<ParmVarDecl>(Var)) |
| 9873 | FinalizeVarWithDestructor(Var, Record); |
| 9874 | |
| 9875 | // According to the blocks spec, the capture of a variable from |
| 9876 | // the stack requires a const copy constructor. This is not true |
| 9877 | // of the copy/move done to move a __block variable to the heap. |
| 9878 | Expr *DeclRef = new (Context) DeclRefExpr(Var, |
| 9879 | DeclRefType.withConst(), |
| 9880 | VK_LValue, Loc); |
| 9881 | ExprResult Result |
| 9882 | = PerformCopyInitialization( |
| 9883 | InitializedEntity::InitializeBlock(Var->getLocation(), |
| 9884 | CaptureType, false), |
| 9885 | Loc, Owned(DeclRef)); |
| 9886 | |
| 9887 | // Build a full-expression copy expression if initialization |
| 9888 | // succeeded and used a non-trivial constructor. Recover from |
| 9889 | // errors by pretending that the copy isn't necessary. |
| 9890 | if (!Result.isInvalid() && |
| 9891 | !cast<CXXConstructExpr>(Result.get())->getConstructor() |
| 9892 | ->isTrivial()) { |
| 9893 | Result = MaybeCreateExprWithCleanups(Result); |
| 9894 | CopyExpr = Result.take(); |
| 9895 | } |
| 9896 | } |
| 9897 | } |
| 9898 | } |
| 9899 | |
| 9900 | // Actually capture the variable. |
| 9901 | if (BuildAndDiagnose) |
| 9902 | CSI->addCapture(Var, HasBlocksAttr, ByRef, Nested, Loc, |
| 9903 | SourceLocation(), CaptureType, CopyExpr); |
| 9904 | Nested = true; |
| 9905 | continue; |
| 9906 | } |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 9907 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9908 | LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CSI); |
| 9909 | |
| 9910 | // Determine whether we are capturing by reference or by value. |
| 9911 | bool ByRef = false; |
| 9912 | if (I == N - 1 && Kind != TryCapture_Implicit) { |
| 9913 | ByRef = (Kind == TryCapture_ExplicitByRef); |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9914 | } else { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9915 | ByRef = (LSI->ImpCaptureStyle == LambdaScopeInfo::ImpCap_LambdaByref); |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9916 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9917 | |
| 9918 | // Compute the type of the field that will capture this variable. |
| 9919 | if (ByRef) { |
| 9920 | // C++11 [expr.prim.lambda]p15: |
| 9921 | // An entity is captured by reference if it is implicitly or |
| 9922 | // explicitly captured but not captured by copy. It is |
| 9923 | // unspecified whether additional unnamed non-static data |
| 9924 | // members are declared in the closure type for entities |
| 9925 | // captured by reference. |
| 9926 | // |
| 9927 | // FIXME: It is not clear whether we want to build an lvalue reference |
| 9928 | // to the DeclRefType or to CaptureType.getNonReferenceType(). GCC appears |
| 9929 | // to do the former, while EDG does the latter. Core issue 1249 will |
| 9930 | // clarify, but for now we follow GCC because it's a more permissive and |
| 9931 | // easily defensible position. |
| 9932 | CaptureType = Context.getLValueReferenceType(DeclRefType); |
| 9933 | } else { |
| 9934 | // C++11 [expr.prim.lambda]p14: |
| 9935 | // For each entity captured by copy, an unnamed non-static |
| 9936 | // data member is declared in the closure type. The |
| 9937 | // declaration order of these members is unspecified. The type |
| 9938 | // of such a data member is the type of the corresponding |
| 9939 | // captured entity if the entity is not a reference to an |
| 9940 | // object, or the referenced type otherwise. [Note: If the |
| 9941 | // captured entity is a reference to a function, the |
| 9942 | // corresponding data member is also a reference to a |
| 9943 | // function. - end note ] |
| 9944 | if (const ReferenceType *RefType = CaptureType->getAs<ReferenceType>()){ |
| 9945 | if (!RefType->getPointeeType()->isFunctionType()) |
| 9946 | CaptureType = RefType->getPointeeType(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9947 | } |
| 9948 | } |
| 9949 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9950 | // Capture this variable in the lambda. |
| 9951 | Expr *CopyExpr = 0; |
| 9952 | if (BuildAndDiagnose) { |
| 9953 | ExprResult Result = captureInLambda(*this, LSI, Var, CaptureType, |
| 9954 | DeclRefType, Loc); |
| 9955 | if (!Result.isInvalid()) |
| 9956 | CopyExpr = Result.take(); |
| 9957 | } |
| 9958 | |
| 9959 | // Compute the type of a reference to this captured variable. |
| 9960 | if (ByRef) |
| 9961 | DeclRefType = CaptureType.getNonReferenceType(); |
| 9962 | else { |
| 9963 | // C++ [expr.prim.lambda]p5: |
| 9964 | // The closure type for a lambda-expression has a public inline |
| 9965 | // function call operator [...]. This function call operator is |
| 9966 | // declared const (9.3.1) if and only if the lambda-expression’s |
| 9967 | // parameter-declaration-clause is not followed by mutable. |
| 9968 | DeclRefType = CaptureType.getNonReferenceType(); |
| 9969 | if (!LSI->Mutable && !CaptureType->isReferenceType()) |
| 9970 | DeclRefType.addConst(); |
| 9971 | } |
| 9972 | |
| 9973 | // Add the capture. |
| 9974 | if (BuildAndDiagnose) |
| 9975 | CSI->addCapture(Var, /*IsBlock=*/false, ByRef, Nested, Loc, |
| 9976 | EllipsisLoc, CaptureType, CopyExpr); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 9977 | Nested = true; |
| 9978 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 9979 | |
| 9980 | return false; |
| 9981 | } |
| 9982 | |
| 9983 | bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc, |
| 9984 | TryCaptureKind Kind, SourceLocation EllipsisLoc) { |
| 9985 | QualType CaptureType; |
| 9986 | QualType DeclRefType; |
| 9987 | return tryCaptureVariable(Var, Loc, Kind, EllipsisLoc, |
| 9988 | /*BuildAndDiagnose=*/true, CaptureType, |
| 9989 | DeclRefType); |
| 9990 | } |
| 9991 | |
| 9992 | QualType Sema::getCapturedDeclRefType(VarDecl *Var, SourceLocation Loc) { |
| 9993 | QualType CaptureType; |
| 9994 | QualType DeclRefType; |
| 9995 | |
| 9996 | // Determine whether we can capture this variable. |
| 9997 | if (tryCaptureVariable(Var, Loc, TryCapture_Implicit, SourceLocation(), |
| 9998 | /*BuildAndDiagnose=*/false, CaptureType, DeclRefType)) |
| 9999 | return QualType(); |
| 10000 | |
| 10001 | return DeclRefType; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10002 | } |
| 10003 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10004 | static void MarkVarDeclODRUsed(Sema &SemaRef, VarDecl *Var, |
| 10005 | SourceLocation Loc) { |
| 10006 | // Keep track of used but undefined variables. |
Eli Friedman | 0cc5d40 | 2012-02-04 00:54:05 +0000 | [diff] [blame] | 10007 | // FIXME: We shouldn't suppress this warning for static data members. |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10008 | if (Var->hasDefinition() == VarDecl::DeclarationOnly && |
Eli Friedman | 0cc5d40 | 2012-02-04 00:54:05 +0000 | [diff] [blame] | 10009 | Var->getLinkage() != ExternalLinkage && |
| 10010 | !(Var->isStaticDataMember() && Var->hasInit())) { |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10011 | SourceLocation &old = SemaRef.UndefinedInternals[Var->getCanonicalDecl()]; |
| 10012 | if (old.isInvalid()) old = Loc; |
| 10013 | } |
| 10014 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10015 | SemaRef.tryCaptureVariable(Var, Loc); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10016 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10017 | Var->setUsed(true); |
| 10018 | } |
| 10019 | |
| 10020 | void Sema::UpdateMarkingForLValueToRValue(Expr *E) { |
| 10021 | // Per C++11 [basic.def.odr], a variable is odr-used "unless it is |
| 10022 | // an object that satisfies the requirements for appearing in a |
| 10023 | // constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) |
| 10024 | // is immediately applied." This function handles the lvalue-to-rvalue |
| 10025 | // conversion part. |
| 10026 | MaybeODRUseExprs.erase(E->IgnoreParens()); |
| 10027 | } |
| 10028 | |
Eli Friedman | ac62601 | 2012-02-29 03:16:56 +0000 | [diff] [blame] | 10029 | ExprResult Sema::ActOnConstantExpression(ExprResult Res) { |
| 10030 | if (!Res.isUsable()) |
| 10031 | return Res; |
| 10032 | |
| 10033 | // If a constant-expression is a reference to a variable where we delay |
| 10034 | // deciding whether it is an odr-use, just assume we will apply the |
| 10035 | // lvalue-to-rvalue conversion. In the one case where this doesn't happen |
| 10036 | // (a non-type template argument), we have special handling anyway. |
| 10037 | UpdateMarkingForLValueToRValue(Res.get()); |
| 10038 | return Res; |
| 10039 | } |
| 10040 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10041 | void Sema::CleanupVarDeclMarking() { |
| 10042 | for (llvm::SmallPtrSetIterator<Expr*> i = MaybeODRUseExprs.begin(), |
| 10043 | e = MaybeODRUseExprs.end(); |
| 10044 | i != e; ++i) { |
| 10045 | VarDecl *Var; |
| 10046 | SourceLocation Loc; |
| 10047 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(*i)) { |
| 10048 | Var = BDRE->getDecl(); |
| 10049 | Loc = BDRE->getLocation(); |
| 10050 | } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*i)) { |
| 10051 | Var = cast<VarDecl>(DRE->getDecl()); |
| 10052 | Loc = DRE->getLocation(); |
| 10053 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(*i)) { |
| 10054 | Var = cast<VarDecl>(ME->getMemberDecl()); |
| 10055 | Loc = ME->getMemberLoc(); |
| 10056 | } else { |
| 10057 | llvm_unreachable("Unexpcted expression"); |
| 10058 | } |
| 10059 | |
| 10060 | MarkVarDeclODRUsed(*this, Var, Loc); |
| 10061 | } |
| 10062 | |
| 10063 | MaybeODRUseExprs.clear(); |
| 10064 | } |
| 10065 | |
| 10066 | // Mark a VarDecl referenced, and perform the necessary handling to compute |
| 10067 | // odr-uses. |
| 10068 | static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc, |
| 10069 | VarDecl *Var, Expr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10070 | Var->setReferenced(); |
| 10071 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10072 | if (!IsPotentiallyEvaluatedContext(SemaRef)) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10073 | return; |
| 10074 | |
| 10075 | // Implicit instantiation of static data members of class templates. |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10076 | if (Var->isStaticDataMember() && Var->getInstantiatedFromStaticDataMember()) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10077 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 10078 | assert(MSInfo && "Missing member specialization information?"); |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10079 | bool AlreadyInstantiated = !MSInfo->getPointOfInstantiation().isInvalid(); |
| 10080 | if (MSInfo->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && |
| 10081 | (!AlreadyInstantiated || Var->isUsableInConstantExpressions())) { |
| 10082 | if (!AlreadyInstantiated) { |
| 10083 | // This is a modification of an existing AST node. Notify listeners. |
| 10084 | if (ASTMutationListener *L = SemaRef.getASTMutationListener()) |
| 10085 | L->StaticDataMemberInstantiated(Var); |
| 10086 | MSInfo->setPointOfInstantiation(Loc); |
| 10087 | } |
| 10088 | SourceLocation PointOfInstantiation = MSInfo->getPointOfInstantiation(); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10089 | if (Var->isUsableInConstantExpressions()) |
| 10090 | // Do not defer instantiations of variables which could be used in a |
| 10091 | // constant expression. |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10092 | SemaRef.InstantiateStaticDataMemberDefinition(PointOfInstantiation,Var); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10093 | else |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10094 | SemaRef.PendingInstantiations.push_back( |
| 10095 | std::make_pair(Var, PointOfInstantiation)); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10096 | } |
| 10097 | } |
| 10098 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10099 | // Per C++11 [basic.def.odr], a variable is odr-used "unless it is |
| 10100 | // an object that satisfies the requirements for appearing in a |
| 10101 | // constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) |
| 10102 | // is immediately applied." We check the first part here, and |
| 10103 | // Sema::UpdateMarkingForLValueToRValue deals with the second part. |
| 10104 | // Note that we use the C++11 definition everywhere because nothing in |
| 10105 | // C++03 depends on whether we get the C++03 version correct. |
| 10106 | const VarDecl *DefVD; |
| 10107 | if (E && !isa<ParmVarDecl>(Var) && |
| 10108 | Var->isUsableInConstantExpressions() && |
| 10109 | Var->getAnyInitializer(DefVD) && DefVD->checkInitIsICE()) |
| 10110 | SemaRef.MaybeODRUseExprs.insert(E); |
| 10111 | else |
| 10112 | MarkVarDeclODRUsed(SemaRef, Var, Loc); |
| 10113 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10114 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10115 | /// \brief Mark a variable referenced, and check whether it is odr-used |
| 10116 | /// (C++ [basic.def.odr]p2, C99 6.9p3). Note that this should not be |
| 10117 | /// used directly for normal expressions referring to VarDecl. |
| 10118 | void Sema::MarkVariableReferenced(SourceLocation Loc, VarDecl *Var) { |
| 10119 | DoMarkVarDeclReferenced(*this, Loc, Var, 0); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10120 | } |
| 10121 | |
| 10122 | static void MarkExprReferenced(Sema &SemaRef, SourceLocation Loc, |
| 10123 | Decl *D, Expr *E) { |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10124 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 10125 | DoMarkVarDeclReferenced(SemaRef, Loc, Var, E); |
| 10126 | return; |
| 10127 | } |
| 10128 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10129 | SemaRef.MarkAnyDeclReferenced(Loc, D); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10130 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10131 | |
| 10132 | /// \brief Perform reference-marking and odr-use handling for a |
| 10133 | /// BlockDeclRefExpr. |
| 10134 | void Sema::MarkBlockDeclRefReferenced(BlockDeclRefExpr *E) { |
| 10135 | MarkExprReferenced(*this, E->getLocation(), E->getDecl(), E); |
| 10136 | } |
| 10137 | |
| 10138 | /// \brief Perform reference-marking and odr-use handling for a DeclRefExpr. |
| 10139 | void Sema::MarkDeclRefReferenced(DeclRefExpr *E) { |
| 10140 | MarkExprReferenced(*this, E->getLocation(), E->getDecl(), E); |
| 10141 | } |
| 10142 | |
| 10143 | /// \brief Perform reference-marking and odr-use handling for a MemberExpr. |
| 10144 | void Sema::MarkMemberReferenced(MemberExpr *E) { |
| 10145 | MarkExprReferenced(*this, E->getMemberLoc(), E->getMemberDecl(), E); |
| 10146 | } |
| 10147 | |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 10148 | /// \brief Perform marking for a reference to an arbitrary declaration. It |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10149 | /// marks the declaration referenced, and performs odr-use checking for functions |
| 10150 | /// and variables. This method should not be used when building an normal |
| 10151 | /// expression which refers to a variable. |
| 10152 | void Sema::MarkAnyDeclReferenced(SourceLocation Loc, Decl *D) { |
| 10153 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 10154 | MarkVariableReferenced(Loc, VD); |
| 10155 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 10156 | MarkFunctionReferenced(Loc, FD); |
| 10157 | else |
| 10158 | D->setReferenced(); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10159 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10160 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10161 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10162 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10163 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10164 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10165 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 10166 | Sema &S; |
| 10167 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10168 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10169 | public: |
| 10170 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10171 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10172 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10173 | |
| 10174 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 10175 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10176 | }; |
| 10177 | } |
| 10178 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10179 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 10180 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10181 | if (Arg.getKind() == TemplateArgument::Declaration) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10182 | S.MarkAnyDeclReferenced(Loc, Arg.getAsDecl()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10183 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10184 | |
| 10185 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10186 | } |
| 10187 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10188 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10189 | if (ClassTemplateSpecializationDecl *Spec |
| 10190 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 10191 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 10192 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10193 | } |
| 10194 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 10195 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10196 | } |
| 10197 | |
| 10198 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 10199 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10200 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10201 | } |
| 10202 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10203 | namespace { |
| 10204 | /// \brief Helper class that marks all of the declarations referenced by |
| 10205 | /// potentially-evaluated subexpressions as "referenced". |
| 10206 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 10207 | Sema &S; |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 10208 | bool SkipLocalVariables; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10209 | |
| 10210 | public: |
| 10211 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 10212 | |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 10213 | EvaluatedExprMarker(Sema &S, bool SkipLocalVariables) |
| 10214 | : Inherited(S.Context), S(S), SkipLocalVariables(SkipLocalVariables) { } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10215 | |
| 10216 | void VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 10217 | // If we were asked not to visit local variables, don't. |
| 10218 | if (SkipLocalVariables) { |
| 10219 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
| 10220 | if (VD->hasLocalStorage()) |
| 10221 | return; |
| 10222 | } |
| 10223 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10224 | S.MarkDeclRefReferenced(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10225 | } |
| 10226 | |
| 10227 | void VisitMemberExpr(MemberExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10228 | S.MarkMemberReferenced(E); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10229 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10230 | } |
| 10231 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10232 | void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10233 | S.MarkFunctionReferenced(E->getLocStart(), |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10234 | const_cast<CXXDestructorDecl*>(E->getTemporary()->getDestructor())); |
| 10235 | Visit(E->getSubExpr()); |
| 10236 | } |
| 10237 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10238 | void VisitCXXNewExpr(CXXNewExpr *E) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10239 | if (E->getOperatorNew()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10240 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorNew()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10241 | if (E->getOperatorDelete()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10242 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10243 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10244 | } |
Sebastian Redl | 2aed8b8 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 10245 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10246 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 10247 | if (E->getOperatorDelete()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10248 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 10249 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 10250 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 10251 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10252 | S.MarkFunctionReferenced(E->getLocStart(), |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 10253 | S.LookupDestructor(Record)); |
| 10254 | } |
| 10255 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10256 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10257 | } |
| 10258 | |
| 10259 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10260 | S.MarkFunctionReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10261 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10262 | } |
| 10263 | |
| 10264 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 10265 | // If we were asked not to visit local variables, don't. |
| 10266 | if (SkipLocalVariables && E->getDecl()->hasLocalStorage()) |
| 10267 | return; |
| 10268 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10269 | S.MarkBlockDeclRefReferenced(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10270 | } |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 10271 | |
| 10272 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 10273 | Visit(E->getExpr()); |
| 10274 | } |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10275 | |
| 10276 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 10277 | Inherited::VisitImplicitCastExpr(E); |
| 10278 | |
| 10279 | if (E->getCastKind() == CK_LValueToRValue) |
| 10280 | S.UpdateMarkingForLValueToRValue(E->getSubExpr()); |
| 10281 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10282 | }; |
| 10283 | } |
| 10284 | |
| 10285 | /// \brief Mark any declarations that appear within this expression or any |
| 10286 | /// potentially-evaluated subexpressions as "referenced". |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 10287 | /// |
| 10288 | /// \param SkipLocalVariables If true, don't mark local variables as |
| 10289 | /// 'referenced'. |
| 10290 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E, |
| 10291 | bool SkipLocalVariables) { |
| 10292 | EvaluatedExprMarker(*this, SkipLocalVariables).Visit(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10293 | } |
| 10294 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10295 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 10296 | /// of the program being compiled. |
| 10297 | /// |
| 10298 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10299 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10300 | /// possibility that the code will actually be executable. Code in sizeof() |
| 10301 | /// expressions, code used only during overload resolution, etc., are not |
| 10302 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 10303 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10304 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10305 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10306 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10307 | /// This routine should be used for all diagnostics that describe the run-time |
| 10308 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 10309 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 10310 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10311 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *Statement, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10312 | const PartialDiagnostic &PD) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10313 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10314 | case Unevaluated: |
| 10315 | // The argument will never be evaluated, so don't complain. |
| 10316 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10317 | |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 10318 | case ConstantEvaluated: |
| 10319 | // Relevant diagnostics should be produced by constant evaluation. |
| 10320 | break; |
| 10321 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10322 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10323 | case PotentiallyEvaluatedIfUsed: |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10324 | if (Statement && getCurFunctionOrMethodDecl()) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 10325 | FunctionScopes.back()->PossiblyUnreachableDiags. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10326 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, Statement)); |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 10327 | } |
| 10328 | else |
| 10329 | Diag(Loc, PD); |
| 10330 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10331 | return true; |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10332 | } |
| 10333 | |
| 10334 | return false; |
| 10335 | } |
| 10336 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10337 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 10338 | CallExpr *CE, FunctionDecl *FD) { |
| 10339 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 10340 | return false; |
| 10341 | |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 10342 | // If we're inside a decltype's expression, don't check for a valid return |
| 10343 | // type or construct temporaries until we know whether this is the last call. |
| 10344 | if (ExprEvalContexts.back().IsDecltype) { |
| 10345 | ExprEvalContexts.back().DelayedDecltypeCalls.push_back(CE); |
| 10346 | return false; |
| 10347 | } |
| 10348 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10349 | PartialDiagnostic Note = |
| 10350 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 10351 | << FD->getDeclName() : PDiag(); |
| 10352 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10353 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10354 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10355 | FD ? |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10356 | PDiag(diag::err_call_function_incomplete_return) |
| 10357 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10358 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10359 | << CE->getSourceRange(), |
| 10360 | std::make_pair(NoteLoc, Note))) |
| 10361 | return true; |
| 10362 | |
| 10363 | return false; |
| 10364 | } |
| 10365 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10366 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10367 | // will prevent this condition from triggering, which is what we want. |
| 10368 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 10369 | SourceLocation Loc; |
| 10370 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10371 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10372 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10373 | |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 10374 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10375 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10376 | return; |
| 10377 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10378 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 10379 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10380 | // Greylist some idioms by putting them into a warning subcategory. |
| 10381 | if (ObjCMessageExpr *ME |
| 10382 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 10383 | Selector Sel = ME->getSelector(); |
| 10384 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10385 | // self = [<foo> init...] |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 10386 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10387 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10388 | |
| 10389 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10390 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10391 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10392 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10393 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10394 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 10395 | } else if (CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10396 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10397 | return; |
| 10398 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10399 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10400 | Loc = Op->getOperatorLoc(); |
| 10401 | } else { |
| 10402 | // Not an assignment. |
| 10403 | return; |
| 10404 | } |
| 10405 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 10406 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10407 | |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 10408 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 10409 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 10410 | Diag(Loc, diag::note_condition_assign_silence) |
| 10411 | << FixItHint::CreateInsertion(Open, "(") |
| 10412 | << FixItHint::CreateInsertion(Close, ")"); |
| 10413 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10414 | if (IsOrAssign) |
| 10415 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 10416 | << FixItHint::CreateReplacement(Loc, "!="); |
| 10417 | else |
| 10418 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 10419 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10420 | } |
| 10421 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10422 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 10423 | /// that the user intended an assignment used as condition. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10424 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *ParenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10425 | // Don't warn if the parens came from a macro. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10426 | SourceLocation parenLoc = ParenE->getLocStart(); |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10427 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 10428 | return; |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 10429 | // Don't warn for dependent expressions. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10430 | if (ParenE->isTypeDependent()) |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 10431 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10432 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10433 | Expr *E = ParenE->IgnoreParens(); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10434 | |
| 10435 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 10436 | if (opE->getOpcode() == BO_EQ && |
| 10437 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 10438 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10439 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 10440 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 10441 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 10442 | Diag(Loc, diag::note_equality_comparison_silence) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10443 | << FixItHint::CreateRemoval(ParenE->getSourceRange().getBegin()) |
| 10444 | << FixItHint::CreateRemoval(ParenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 10445 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 10446 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10447 | } |
| 10448 | } |
| 10449 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10450 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10451 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10452 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 10453 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10454 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10455 | ExprResult result = CheckPlaceholderExpr(E); |
| 10456 | if (result.isInvalid()) return ExprError(); |
| 10457 | E = result.take(); |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 10458 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10459 | if (!E->isTypeDependent()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 10460 | if (getLangOptions().CPlusPlus) |
| 10461 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 10462 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10463 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 10464 | if (ERes.isInvalid()) |
| 10465 | return ExprError(); |
| 10466 | E = ERes.take(); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 10467 | |
| 10468 | QualType T = E->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10469 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 10470 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 10471 | << T << E->getSourceRange(); |
| 10472 | return ExprError(); |
| 10473 | } |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10474 | } |
| 10475 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10476 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10477 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10478 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10479 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10480 | Expr *SubExpr) { |
| 10481 | if (!SubExpr) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10482 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10483 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10484 | return CheckBooleanCondition(SubExpr, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10485 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10486 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10487 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10488 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 10489 | /// to have an appropriate type. |
| 10490 | struct RebuildUnknownAnyFunction |
| 10491 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 10492 | |
| 10493 | Sema &S; |
| 10494 | |
| 10495 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 10496 | |
| 10497 | ExprResult VisitStmt(Stmt *S) { |
| 10498 | llvm_unreachable("unexpected statement!"); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10499 | } |
| 10500 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10501 | ExprResult VisitExpr(Expr *E) { |
| 10502 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 10503 | << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10504 | return ExprError(); |
| 10505 | } |
| 10506 | |
| 10507 | /// Rebuild an expression which simply semantically wraps another |
| 10508 | /// expression which it shares the type and value kind of. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10509 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 10510 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 10511 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10512 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10513 | Expr *SubExpr = SubResult.take(); |
| 10514 | E->setSubExpr(SubExpr); |
| 10515 | E->setType(SubExpr->getType()); |
| 10516 | E->setValueKind(SubExpr->getValueKind()); |
| 10517 | assert(E->getObjectKind() == OK_Ordinary); |
| 10518 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10519 | } |
| 10520 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10521 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 10522 | return rebuildSugarExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10523 | } |
| 10524 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10525 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 10526 | return rebuildSugarExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10527 | } |
| 10528 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10529 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 10530 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 10531 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10532 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10533 | Expr *SubExpr = SubResult.take(); |
| 10534 | E->setSubExpr(SubExpr); |
| 10535 | E->setType(S.Context.getPointerType(SubExpr->getType())); |
| 10536 | assert(E->getValueKind() == VK_RValue); |
| 10537 | assert(E->getObjectKind() == OK_Ordinary); |
| 10538 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10539 | } |
| 10540 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10541 | ExprResult resolveDecl(Expr *E, ValueDecl *VD) { |
| 10542 | if (!isa<FunctionDecl>(VD)) return VisitExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10543 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10544 | E->setType(VD->getType()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10545 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10546 | assert(E->getValueKind() == VK_RValue); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10547 | if (S.getLangOptions().CPlusPlus && |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10548 | !(isa<CXXMethodDecl>(VD) && |
| 10549 | cast<CXXMethodDecl>(VD)->isInstance())) |
| 10550 | E->setValueKind(VK_LValue); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10551 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10552 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10553 | } |
| 10554 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10555 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 10556 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10557 | } |
| 10558 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10559 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 10560 | return resolveDecl(E, E->getDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10561 | } |
| 10562 | }; |
| 10563 | } |
| 10564 | |
| 10565 | /// Given a function expression of unknown-any type, try to rebuild it |
| 10566 | /// to have a function type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10567 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *FunctionExpr) { |
| 10568 | ExprResult Result = RebuildUnknownAnyFunction(S).Visit(FunctionExpr); |
| 10569 | if (Result.isInvalid()) return ExprError(); |
| 10570 | return S.DefaultFunctionArrayConversion(Result.take()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10571 | } |
| 10572 | |
| 10573 | namespace { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10574 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 10575 | /// into one which resolves the type directly on the referring |
| 10576 | /// expression. Strict preservation of the original source |
| 10577 | /// structure is not a goal. |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10578 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10579 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10580 | |
| 10581 | Sema &S; |
| 10582 | |
| 10583 | /// The current destination type. |
| 10584 | QualType DestType; |
| 10585 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10586 | RebuildUnknownAnyExpr(Sema &S, QualType CastType) |
| 10587 | : S(S), DestType(CastType) {} |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10588 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10589 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10590 | llvm_unreachable("unexpected statement!"); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10591 | } |
| 10592 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10593 | ExprResult VisitExpr(Expr *E) { |
| 10594 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10595 | << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10596 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10597 | } |
| 10598 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10599 | ExprResult VisitCallExpr(CallExpr *E); |
| 10600 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10601 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10602 | /// Rebuild an expression which simply semantically wraps another |
| 10603 | /// expression which it shares the type and value kind of. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10604 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 10605 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 10606 | if (SubResult.isInvalid()) return ExprError(); |
| 10607 | Expr *SubExpr = SubResult.take(); |
| 10608 | E->setSubExpr(SubExpr); |
| 10609 | E->setType(SubExpr->getType()); |
| 10610 | E->setValueKind(SubExpr->getValueKind()); |
| 10611 | assert(E->getObjectKind() == OK_Ordinary); |
| 10612 | return E; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10613 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10614 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10615 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 10616 | return rebuildSugarExpr(E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10617 | } |
| 10618 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10619 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 10620 | return rebuildSugarExpr(E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10621 | } |
| 10622 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10623 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 10624 | const PointerType *Ptr = DestType->getAs<PointerType>(); |
| 10625 | if (!Ptr) { |
| 10626 | S.Diag(E->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 10627 | << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10628 | return ExprError(); |
| 10629 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10630 | assert(E->getValueKind() == VK_RValue); |
| 10631 | assert(E->getObjectKind() == OK_Ordinary); |
| 10632 | E->setType(DestType); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10633 | |
| 10634 | // Build the sub-expression as if it were an object of the pointee type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10635 | DestType = Ptr->getPointeeType(); |
| 10636 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 10637 | if (SubResult.isInvalid()) return ExprError(); |
| 10638 | E->setSubExpr(SubResult.take()); |
| 10639 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10640 | } |
| 10641 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10642 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10643 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10644 | ExprResult resolveDecl(Expr *E, ValueDecl *VD); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10645 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10646 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 10647 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10648 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10649 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10650 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 10651 | return resolveDecl(E, E->getDecl()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10652 | } |
| 10653 | }; |
| 10654 | } |
| 10655 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10656 | /// Rebuilds a call expression which yielded __unknown_anytype. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10657 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) { |
| 10658 | Expr *CalleeExpr = E->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10659 | |
| 10660 | enum FnKind { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10661 | FK_MemberFunction, |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10662 | FK_FunctionPointer, |
| 10663 | FK_BlockPointer |
| 10664 | }; |
| 10665 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10666 | FnKind Kind; |
| 10667 | QualType CalleeType = CalleeExpr->getType(); |
| 10668 | if (CalleeType == S.Context.BoundMemberTy) { |
| 10669 | assert(isa<CXXMemberCallExpr>(E) || isa<CXXOperatorCallExpr>(E)); |
| 10670 | Kind = FK_MemberFunction; |
| 10671 | CalleeType = Expr::findBoundMemberType(CalleeExpr); |
| 10672 | } else if (const PointerType *Ptr = CalleeType->getAs<PointerType>()) { |
| 10673 | CalleeType = Ptr->getPointeeType(); |
| 10674 | Kind = FK_FunctionPointer; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10675 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10676 | CalleeType = CalleeType->castAs<BlockPointerType>()->getPointeeType(); |
| 10677 | Kind = FK_BlockPointer; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10678 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10679 | const FunctionType *FnType = CalleeType->castAs<FunctionType>(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10680 | |
| 10681 | // Verify that this is a legal result type of a function. |
| 10682 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 10683 | unsigned diagID = diag::err_func_returning_array_function; |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10684 | if (Kind == FK_BlockPointer) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10685 | diagID = diag::err_block_returning_array_function; |
| 10686 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10687 | S.Diag(E->getExprLoc(), diagID) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10688 | << DestType->isFunctionType() << DestType; |
| 10689 | return ExprError(); |
| 10690 | } |
| 10691 | |
| 10692 | // Otherwise, go ahead and set DestType as the call's result. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10693 | E->setType(DestType.getNonLValueExprType(S.Context)); |
| 10694 | E->setValueKind(Expr::getValueKindForType(DestType)); |
| 10695 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10696 | |
| 10697 | // Rebuild the function type, replacing the result type with DestType. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10698 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType)) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10699 | DestType = S.Context.getFunctionType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10700 | Proto->arg_type_begin(), |
| 10701 | Proto->getNumArgs(), |
| 10702 | Proto->getExtProtoInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10703 | else |
| 10704 | DestType = S.Context.getFunctionNoProtoType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10705 | FnType->getExtInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10706 | |
| 10707 | // Rebuild the appropriate pointer-to-function type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10708 | switch (Kind) { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10709 | case FK_MemberFunction: |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10710 | // Nothing to do. |
| 10711 | break; |
| 10712 | |
| 10713 | case FK_FunctionPointer: |
| 10714 | DestType = S.Context.getPointerType(DestType); |
| 10715 | break; |
| 10716 | |
| 10717 | case FK_BlockPointer: |
| 10718 | DestType = S.Context.getBlockPointerType(DestType); |
| 10719 | break; |
| 10720 | } |
| 10721 | |
| 10722 | // Finally, we can recurse. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10723 | ExprResult CalleeResult = Visit(CalleeExpr); |
| 10724 | if (!CalleeResult.isUsable()) return ExprError(); |
| 10725 | E->setCallee(CalleeResult.take()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10726 | |
| 10727 | // Bind a temporary if necessary. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10728 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10729 | } |
| 10730 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10731 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10732 | // Verify that this is a legal result type of a call. |
| 10733 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10734 | S.Diag(E->getExprLoc(), diag::err_func_returning_array_function) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10735 | << DestType->isFunctionType() << DestType; |
| 10736 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10737 | } |
| 10738 | |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 10739 | // Rewrite the method result type if available. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10740 | if (ObjCMethodDecl *Method = E->getMethodDecl()) { |
| 10741 | assert(Method->getResultType() == S.Context.UnknownAnyTy); |
| 10742 | Method->setResultType(DestType); |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 10743 | } |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10744 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10745 | // Change the type of the message. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10746 | E->setType(DestType.getNonReferenceType()); |
| 10747 | E->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10748 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10749 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10750 | } |
| 10751 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10752 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10753 | // The only case we should ever see here is a function-to-pointer decay. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10754 | assert(E->getCastKind() == CK_FunctionToPointerDecay); |
| 10755 | assert(E->getValueKind() == VK_RValue); |
| 10756 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10757 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10758 | E->setType(DestType); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10759 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10760 | // Rebuild the sub-expression as the pointee (function) type. |
| 10761 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 10762 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10763 | ExprResult Result = Visit(E->getSubExpr()); |
| 10764 | if (!Result.isUsable()) return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10765 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10766 | E->setSubExpr(Result.take()); |
| 10767 | return S.Owned(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10768 | } |
| 10769 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10770 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *E, ValueDecl *VD) { |
| 10771 | ExprValueKind ValueKind = VK_LValue; |
| 10772 | QualType Type = DestType; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10773 | |
| 10774 | // We know how to make this work for certain kinds of decls: |
| 10775 | |
| 10776 | // - functions |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10777 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(VD)) { |
| 10778 | if (const PointerType *Ptr = Type->getAs<PointerType>()) { |
| 10779 | DestType = Ptr->getPointeeType(); |
| 10780 | ExprResult Result = resolveDecl(E, VD); |
| 10781 | if (Result.isInvalid()) return ExprError(); |
| 10782 | return S.ImpCastExprToType(Result.take(), Type, |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 10783 | CK_FunctionToPointerDecay, VK_RValue); |
| 10784 | } |
| 10785 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10786 | if (!Type->isFunctionType()) { |
| 10787 | S.Diag(E->getExprLoc(), diag::err_unknown_any_function) |
| 10788 | << VD << E->getSourceRange(); |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 10789 | return ExprError(); |
| 10790 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10791 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10792 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 10793 | if (MD->isInstance()) { |
| 10794 | ValueKind = VK_RValue; |
| 10795 | Type = S.Context.BoundMemberTy; |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10796 | } |
| 10797 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10798 | // Function references aren't l-values in C. |
| 10799 | if (!S.getLangOptions().CPlusPlus) |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10800 | ValueKind = VK_RValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10801 | |
| 10802 | // - variables |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10803 | } else if (isa<VarDecl>(VD)) { |
| 10804 | if (const ReferenceType *RefTy = Type->getAs<ReferenceType>()) { |
| 10805 | Type = RefTy->getPointeeType(); |
| 10806 | } else if (Type->isFunctionType()) { |
| 10807 | S.Diag(E->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 10808 | << VD << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10809 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10810 | } |
| 10811 | |
| 10812 | // - nothing else |
| 10813 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10814 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 10815 | << VD << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10816 | return ExprError(); |
| 10817 | } |
| 10818 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 10819 | VD->setType(DestType); |
| 10820 | E->setType(Type); |
| 10821 | E->setValueKind(ValueKind); |
| 10822 | return S.Owned(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10823 | } |
| 10824 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10825 | /// Check a cast of an unknown-any type. We intentionally only |
| 10826 | /// trigger this for C-style casts. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10827 | ExprResult Sema::checkUnknownAnyCast(SourceRange TypeRange, QualType CastType, |
| 10828 | Expr *CastExpr, CastKind &CastKind, |
| 10829 | ExprValueKind &VK, CXXCastPath &Path) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10830 | // Rewrite the casted expression from scratch. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10831 | ExprResult result = RebuildUnknownAnyExpr(*this, CastType).Visit(CastExpr); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10832 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10833 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10834 | CastExpr = result.take(); |
| 10835 | VK = CastExpr->getValueKind(); |
| 10836 | CastKind = CK_NoOp; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10837 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10838 | return CastExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10839 | } |
| 10840 | |
Douglas Gregor | f1d1ca5 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 10841 | ExprResult Sema::forceUnknownAnyToType(Expr *E, QualType ToType) { |
| 10842 | return RebuildUnknownAnyExpr(*this, ToType).Visit(E); |
| 10843 | } |
| 10844 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10845 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *E) { |
| 10846 | Expr *orig = E; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10847 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10848 | while (true) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10849 | E = E->IgnoreParenImpCasts(); |
| 10850 | if (CallExpr *call = dyn_cast<CallExpr>(E)) { |
| 10851 | E = call->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10852 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10853 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10854 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10855 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10856 | } |
| 10857 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10858 | SourceLocation loc; |
| 10859 | NamedDecl *d; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10860 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10861 | loc = ref->getLocation(); |
| 10862 | d = ref->getDecl(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10863 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10864 | loc = mem->getMemberLoc(); |
| 10865 | d = mem->getMemberDecl(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10866 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10867 | diagID = diag::err_uncasted_call_of_unknown_any; |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 10868 | loc = msg->getSelectorStartLoc(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10869 | d = msg->getMethodDecl(); |
John McCall | 819e745 | 2011-08-31 20:57:36 +0000 | [diff] [blame] | 10870 | if (!d) { |
| 10871 | S.Diag(loc, diag::err_uncasted_send_to_unknown_any_method) |
| 10872 | << static_cast<unsigned>(msg->isClassMessage()) << msg->getSelector() |
| 10873 | << orig->getSourceRange(); |
| 10874 | return ExprError(); |
| 10875 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10876 | } else { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10877 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10878 | << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10879 | return ExprError(); |
| 10880 | } |
| 10881 | |
| 10882 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10883 | |
| 10884 | // Never recoverable. |
| 10885 | return ExprError(); |
| 10886 | } |
| 10887 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10888 | /// Check for operands with placeholder types and complain if found. |
| 10889 | /// Returns true if there was an error and no recovery was possible. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 10890 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10891 | const BuiltinType *placeholderType = E->getType()->getAsPlaceholderType(); |
| 10892 | if (!placeholderType) return Owned(E); |
| 10893 | |
| 10894 | switch (placeholderType->getKind()) { |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10895 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10896 | // Overloaded expressions. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10897 | case BuiltinType::Overload: { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10898 | // Try to resolve a single function template specialization. |
| 10899 | // This is obligatory. |
| 10900 | ExprResult result = Owned(E); |
| 10901 | if (ResolveAndFixSingleFunctionTemplateSpecialization(result, false)) { |
| 10902 | return result; |
| 10903 | |
| 10904 | // If that failed, try to recover with a call. |
| 10905 | } else { |
| 10906 | tryToRecoverWithCall(result, PDiag(diag::err_ovl_unresolvable), |
| 10907 | /*complain*/ true); |
| 10908 | return result; |
| 10909 | } |
| 10910 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10911 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10912 | // Bound member functions. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10913 | case BuiltinType::BoundMember: { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 10914 | ExprResult result = Owned(E); |
| 10915 | tryToRecoverWithCall(result, PDiag(diag::err_bound_member_function), |
| 10916 | /*complain*/ true); |
| 10917 | return result; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10918 | } |
| 10919 | |
| 10920 | // ARC unbridged casts. |
| 10921 | case BuiltinType::ARCUnbridgedCast: { |
| 10922 | Expr *realCast = stripARCUnbridgedCast(E); |
| 10923 | diagnoseARCUnbridgedCast(realCast); |
| 10924 | return Owned(realCast); |
| 10925 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10926 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10927 | // Expressions of unknown type. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10928 | case BuiltinType::UnknownAny: |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10929 | return diagnoseUnknownAnyExpr(*this, E); |
| 10930 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 10931 | // Pseudo-objects. |
| 10932 | case BuiltinType::PseudoObject: |
| 10933 | return checkPseudoObjectRValue(E); |
| 10934 | |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 10935 | // Everything else should be impossible. |
| 10936 | #define BUILTIN_TYPE(Id, SingletonId) \ |
| 10937 | case BuiltinType::Id: |
| 10938 | #define PLACEHOLDER_TYPE(Id, SingletonId) |
| 10939 | #include "clang/AST/BuiltinTypes.def" |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 10940 | break; |
| 10941 | } |
| 10942 | |
| 10943 | llvm_unreachable("invalid placeholder type!"); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10944 | } |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10945 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10946 | bool Sema::CheckCaseExpression(Expr *E) { |
| 10947 | if (E->isTypeDependent()) |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10948 | return true; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 10949 | if (E->isValueDependent() || E->isIntegerConstantExpr(Context)) |
| 10950 | return E->getType()->isIntegralOrEnumerationType(); |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10951 | return false; |
| 10952 | } |