Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 12 | // Constant expression evaluation produces four main results: |
| 13 | // |
| 14 | // * A success/failure flag indicating whether constant folding was successful. |
| 15 | // This is the 'bool' return value used by most of the code in this file. A |
| 16 | // 'false' return value indicates that constant folding has failed, and any |
| 17 | // appropriate diagnostic has already been produced. |
| 18 | // |
| 19 | // * An evaluated result, valid only if constant folding has not failed. |
| 20 | // |
| 21 | // * A flag indicating if evaluation encountered (unevaluated) side-effects. |
| 22 | // These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1), |
| 23 | // where it is possible to determine the evaluated result regardless. |
| 24 | // |
| 25 | // * A set of notes indicating why the evaluation was not a constant expression |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 26 | // (under the C++11 / C++1y rules only, at the moment), or, if folding failed |
| 27 | // too, why the expression could not be folded. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 28 | // |
| 29 | // If we are checking for a potential constant expression, failure to constant |
| 30 | // fold a potential constant sub-expression will be indicated by a 'false' |
| 31 | // return value (the expression could not be folded) and no diagnostic (the |
| 32 | // expression is not necessarily non-constant). |
| 33 | // |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #include "clang/AST/APValue.h" |
| 37 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 38 | #include "clang/AST/ASTDiagnostic.h" |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 39 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 40 | #include "clang/AST/Expr.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 41 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 1904f44 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 42 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 43 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 44 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 45 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | b807c9c | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 47 | #include "llvm/Support/raw_ostream.h" |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 48 | #include <cstring> |
Richard Smith | c804232 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 49 | #include <functional> |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 50 | |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 51 | using namespace clang; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 52 | using llvm::APSInt; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 53 | using llvm::APFloat; |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 54 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 55 | static bool IsGlobalLValue(APValue::LValueBase B); |
| 56 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 57 | namespace { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 58 | struct LValue; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 59 | struct CallStackFrame; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 60 | struct EvalInfo; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 61 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 62 | static QualType getType(APValue::LValueBase B) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 63 | if (!B) return QualType(); |
| 64 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) |
| 65 | return D->getType(); |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 66 | |
| 67 | const Expr *Base = B.get<const Expr*>(); |
| 68 | |
| 69 | // For a materialized temporary, the type of the temporary we materialized |
| 70 | // may not be the type of the expression. |
| 71 | if (const MaterializeTemporaryExpr *MTE = |
| 72 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 73 | SmallVector<const Expr *, 2> CommaLHSs; |
| 74 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 75 | const Expr *Temp = MTE->GetTemporaryExpr(); |
| 76 | const Expr *Inner = Temp->skipRValueSubobjectAdjustments(CommaLHSs, |
| 77 | Adjustments); |
| 78 | // Keep any cv-qualifiers from the reference if we generated a temporary |
| 79 | // for it. |
| 80 | if (Inner != Temp) |
| 81 | return Inner->getType(); |
| 82 | } |
| 83 | |
| 84 | return Base->getType(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 87 | /// Get an LValue path entry, which is known to not be an array index, as a |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 88 | /// field or base class. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 89 | static |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 90 | APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 91 | APValue::BaseOrMemberType Value; |
| 92 | Value.setFromOpaqueValue(E.BaseOrMember); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 93 | return Value; |
| 94 | } |
| 95 | |
| 96 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 97 | /// field declaration. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 98 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 99 | return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 100 | } |
| 101 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 102 | /// base class declaration. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 103 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 104 | return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 105 | } |
| 106 | /// Determine whether this LValue path entry for a base class names a virtual |
| 107 | /// base class. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 108 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 109 | return getAsBaseOrMember(E).getInt(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 112 | /// Find the path length and type of the most-derived subobject in the given |
| 113 | /// path, and find the size of the containing array, if any. |
| 114 | static |
| 115 | unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base, |
| 116 | ArrayRef<APValue::LValuePathEntry> Path, |
| 117 | uint64_t &ArraySize, QualType &Type) { |
| 118 | unsigned MostDerivedLength = 0; |
| 119 | Type = Base; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 120 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 121 | if (Type->isArrayType()) { |
| 122 | const ConstantArrayType *CAT = |
| 123 | cast<ConstantArrayType>(Ctx.getAsArrayType(Type)); |
| 124 | Type = CAT->getElementType(); |
| 125 | ArraySize = CAT->getSize().getZExtValue(); |
| 126 | MostDerivedLength = I + 1; |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 127 | } else if (Type->isAnyComplexType()) { |
| 128 | const ComplexType *CT = Type->castAs<ComplexType>(); |
| 129 | Type = CT->getElementType(); |
| 130 | ArraySize = 2; |
| 131 | MostDerivedLength = I + 1; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 132 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
| 133 | Type = FD->getType(); |
| 134 | ArraySize = 0; |
| 135 | MostDerivedLength = I + 1; |
| 136 | } else { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 137 | // Path[I] describes a base class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 138 | ArraySize = 0; |
| 139 | } |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 140 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 141 | return MostDerivedLength; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 144 | // The order of this enum is important for diagnostics. |
| 145 | enum CheckSubobjectKind { |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 146 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 147 | CSK_This, CSK_Real, CSK_Imag |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 150 | /// A path from a glvalue to a subobject of that glvalue. |
| 151 | struct SubobjectDesignator { |
| 152 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 153 | /// lvalues can still be folded, but they are not core constant expressions |
| 154 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| 155 | bool Invalid : 1; |
| 156 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 157 | /// Is this a pointer one past the end of an object? |
| 158 | bool IsOnePastTheEnd : 1; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 159 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 160 | /// The length of the path to the most-derived object of which this is a |
| 161 | /// subobject. |
| 162 | unsigned MostDerivedPathLength : 30; |
| 163 | |
| 164 | /// The size of the array of which the most-derived object is an element, or |
| 165 | /// 0 if the most-derived object is not an array element. |
| 166 | uint64_t MostDerivedArraySize; |
| 167 | |
| 168 | /// The type of the most derived object referred to by this address. |
| 169 | QualType MostDerivedType; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 170 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 171 | typedef APValue::LValuePathEntry PathEntry; |
| 172 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 173 | /// The entries on the path from the glvalue to the designated subobject. |
| 174 | SmallVector<PathEntry, 8> Entries; |
| 175 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 176 | SubobjectDesignator() : Invalid(true) {} |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 177 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 178 | explicit SubobjectDesignator(QualType T) |
| 179 | : Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0), |
| 180 | MostDerivedArraySize(0), MostDerivedType(T) {} |
| 181 | |
| 182 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
| 183 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
| 184 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 185 | if (!Invalid) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 186 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 187 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 188 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| 189 | if (V.getLValueBase()) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 190 | MostDerivedPathLength = |
| 191 | findMostDerivedSubobject(Ctx, getType(V.getLValueBase()), |
| 192 | V.getLValuePath(), MostDerivedArraySize, |
| 193 | MostDerivedType); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 197 | void setInvalid() { |
| 198 | Invalid = true; |
| 199 | Entries.clear(); |
| 200 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 201 | |
| 202 | /// Determine whether this is a one-past-the-end pointer. |
| 203 | bool isOnePastTheEnd() const { |
Richard Smith | 33b44ab | 2014-07-23 23:50:25 +0000 | [diff] [blame] | 204 | assert(!Invalid); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 205 | if (IsOnePastTheEnd) |
| 206 | return true; |
| 207 | if (MostDerivedArraySize && |
| 208 | Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) |
| 209 | return true; |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | /// Check that this refers to a valid subobject. |
| 214 | bool isValidSubobject() const { |
| 215 | if (Invalid) |
| 216 | return false; |
| 217 | return !isOnePastTheEnd(); |
| 218 | } |
| 219 | /// Check that this refers to a valid subobject, and if not, produce a |
| 220 | /// relevant diagnostic and set the designator as invalid. |
| 221 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
| 222 | |
| 223 | /// Update this designator to refer to the first element within this array. |
| 224 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 225 | PathEntry Entry; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 226 | Entry.ArrayIndex = 0; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 227 | Entries.push_back(Entry); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 228 | |
| 229 | // This is a most-derived object. |
| 230 | MostDerivedType = CAT->getElementType(); |
| 231 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
| 232 | MostDerivedPathLength = Entries.size(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 233 | } |
| 234 | /// Update this designator to refer to the given base or member of this |
| 235 | /// object. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 236 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 237 | PathEntry Entry; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 238 | APValue::BaseOrMemberType Value(D, Virtual); |
| 239 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 240 | Entries.push_back(Entry); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 241 | |
| 242 | // If this isn't a base class, it's a new most-derived object. |
| 243 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 244 | MostDerivedType = FD->getType(); |
| 245 | MostDerivedArraySize = 0; |
| 246 | MostDerivedPathLength = Entries.size(); |
| 247 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 248 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 249 | /// Update this designator to refer to the given complex component. |
| 250 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
| 251 | PathEntry Entry; |
| 252 | Entry.ArrayIndex = Imag; |
| 253 | Entries.push_back(Entry); |
| 254 | |
| 255 | // This is technically a most-derived object, though in practice this |
| 256 | // is unlikely to matter. |
| 257 | MostDerivedType = EltTy; |
| 258 | MostDerivedArraySize = 2; |
| 259 | MostDerivedPathLength = Entries.size(); |
| 260 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 261 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 262 | /// Add N to the address of this subobject. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 263 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 264 | if (Invalid) return; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 265 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 266 | Entries.back().ArrayIndex += N; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 267 | if (Entries.back().ArrayIndex > MostDerivedArraySize) { |
| 268 | diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex); |
| 269 | setInvalid(); |
| 270 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 271 | return; |
| 272 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 273 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 274 | // nonarray object behaves the same as a pointer to the first element of |
| 275 | // an array of length one with the type of the object as its element type. |
| 276 | if (IsOnePastTheEnd && N == (uint64_t)-1) |
| 277 | IsOnePastTheEnd = false; |
| 278 | else if (!IsOnePastTheEnd && N == 1) |
| 279 | IsOnePastTheEnd = true; |
| 280 | else if (N != 0) { |
| 281 | diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 282 | setInvalid(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 283 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 284 | } |
| 285 | }; |
| 286 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 287 | /// A stack frame in the constexpr call stack. |
| 288 | struct CallStackFrame { |
| 289 | EvalInfo &Info; |
| 290 | |
| 291 | /// Parent - The caller of this stack frame. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 292 | CallStackFrame *Caller; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 293 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 294 | /// CallLoc - The location of the call expression for this call. |
| 295 | SourceLocation CallLoc; |
| 296 | |
| 297 | /// Callee - The function which was called. |
| 298 | const FunctionDecl *Callee; |
| 299 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 300 | /// Index - The call index of this call. |
| 301 | unsigned Index; |
| 302 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 303 | /// This - The binding for the this pointer in this call, if any. |
| 304 | const LValue *This; |
| 305 | |
Nick Lewycky | e2b2caa | 2013-09-22 10:07:22 +0000 | [diff] [blame] | 306 | /// Arguments - Parameter bindings for this function call, indexed by |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 307 | /// parameters' function scope indices. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 308 | APValue *Arguments; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 309 | |
Eli Friedman | 4830ec8 | 2012-06-25 21:21:08 +0000 | [diff] [blame] | 310 | // Note that we intentionally use std::map here so that references to |
| 311 | // values are stable. |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 312 | typedef std::map<const void*, APValue> MapTy; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 313 | typedef MapTy::const_iterator temp_iterator; |
| 314 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 315 | MapTy Temporaries; |
| 316 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 317 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 318 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 319 | APValue *Arguments); |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 320 | ~CallStackFrame(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 321 | |
| 322 | APValue *getTemporary(const void *Key) { |
| 323 | MapTy::iterator I = Temporaries.find(Key); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 324 | return I == Temporaries.end() ? nullptr : &I->second; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 325 | } |
| 326 | APValue &createTemporary(const void *Key, bool IsLifetimeExtended); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 329 | /// Temporarily override 'this'. |
| 330 | class ThisOverrideRAII { |
| 331 | public: |
| 332 | ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable) |
| 333 | : Frame(Frame), OldThis(Frame.This) { |
| 334 | if (Enable) |
| 335 | Frame.This = NewThis; |
| 336 | } |
| 337 | ~ThisOverrideRAII() { |
| 338 | Frame.This = OldThis; |
| 339 | } |
| 340 | private: |
| 341 | CallStackFrame &Frame; |
| 342 | const LValue *OldThis; |
| 343 | }; |
| 344 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 345 | /// A partial diagnostic which we might know in advance that we are not going |
| 346 | /// to emit. |
| 347 | class OptionalDiagnostic { |
| 348 | PartialDiagnostic *Diag; |
| 349 | |
| 350 | public: |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 351 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = nullptr) |
| 352 | : Diag(Diag) {} |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 353 | |
| 354 | template<typename T> |
| 355 | OptionalDiagnostic &operator<<(const T &v) { |
| 356 | if (Diag) |
| 357 | *Diag << v; |
| 358 | return *this; |
| 359 | } |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 360 | |
| 361 | OptionalDiagnostic &operator<<(const APSInt &I) { |
| 362 | if (Diag) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 363 | SmallVector<char, 32> Buffer; |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 364 | I.toString(Buffer); |
| 365 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 366 | } |
| 367 | return *this; |
| 368 | } |
| 369 | |
| 370 | OptionalDiagnostic &operator<<(const APFloat &F) { |
| 371 | if (Diag) { |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 372 | // FIXME: Force the precision of the source value down so we don't |
| 373 | // print digits which are usually useless (we don't really care here if |
| 374 | // we truncate a digit by accident in edge cases). Ideally, |
| 375 | // APFloat::toString would automatically print the shortest |
| 376 | // representation which rounds to the correct value, but it's a bit |
| 377 | // tricky to implement. |
| 378 | unsigned precision = |
| 379 | llvm::APFloat::semanticsPrecision(F.getSemantics()); |
| 380 | precision = (precision * 59 + 195) / 196; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 381 | SmallVector<char, 32> Buffer; |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 382 | F.toString(Buffer, precision); |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 383 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 384 | } |
| 385 | return *this; |
| 386 | } |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 389 | /// A cleanup, and a flag indicating whether it is lifetime-extended. |
| 390 | class Cleanup { |
| 391 | llvm::PointerIntPair<APValue*, 1, bool> Value; |
| 392 | |
| 393 | public: |
| 394 | Cleanup(APValue *Val, bool IsLifetimeExtended) |
| 395 | : Value(Val, IsLifetimeExtended) {} |
| 396 | |
| 397 | bool isLifetimeExtended() const { return Value.getInt(); } |
| 398 | void endLifetime() { |
| 399 | *Value.getPointer() = APValue(); |
| 400 | } |
| 401 | }; |
| 402 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 403 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 404 | /// information about a subexpression as it is folded. It retains information |
| 405 | /// about the AST context, but also maintains information about the folded |
| 406 | /// expression. |
| 407 | /// |
| 408 | /// If an expression could be evaluated, it is still possible it is not a C |
| 409 | /// "integer constant expression" or constant expression. If not, this struct |
| 410 | /// captures information about how and why not. |
| 411 | /// |
| 412 | /// One bit of information passed *into* the request for constant folding |
| 413 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 414 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 415 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 416 | /// certain things in certain situations. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 417 | struct EvalInfo { |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 418 | ASTContext &Ctx; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 419 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 420 | /// EvalStatus - Contains information about the evaluation. |
| 421 | Expr::EvalStatus &EvalStatus; |
| 422 | |
| 423 | /// CurrentCall - The top of the constexpr call stack. |
| 424 | CallStackFrame *CurrentCall; |
| 425 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 426 | /// CallStackDepth - The number of calls in the call stack right now. |
| 427 | unsigned CallStackDepth; |
| 428 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 429 | /// NextCallIndex - The next call index to assign. |
| 430 | unsigned NextCallIndex; |
| 431 | |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 432 | /// StepsLeft - The remaining number of evaluation steps we're permitted |
| 433 | /// to perform. This is essentially a limit for the number of statements |
| 434 | /// we will evaluate. |
| 435 | unsigned StepsLeft; |
| 436 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 437 | /// BottomFrame - The frame in which evaluation started. This must be |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 438 | /// initialized after CurrentCall and CallStackDepth. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 439 | CallStackFrame BottomFrame; |
| 440 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 441 | /// A stack of values whose lifetimes end at the end of some surrounding |
| 442 | /// evaluation frame. |
| 443 | llvm::SmallVector<Cleanup, 16> CleanupStack; |
| 444 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 445 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 446 | /// evaluated, if any. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 447 | APValue::LValueBase EvaluatingDecl; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 448 | |
| 449 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 450 | /// declaration whose initializer is being evaluated, if any. |
| 451 | APValue *EvaluatingDeclValue; |
| 452 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 453 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 454 | /// notes attached to it will also be stored, otherwise they will not be. |
| 455 | bool HasActiveDiagnostic; |
| 456 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 457 | enum EvaluationMode { |
| 458 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 459 | /// is not a constant expression. |
| 460 | EM_ConstantExpression, |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 461 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 462 | /// Evaluate as a potential constant expression. Keep going if we hit a |
| 463 | /// construct that we can't evaluate yet (because we don't yet know the |
| 464 | /// value of something) but stop if we hit something that could never be |
| 465 | /// a constant expression. |
| 466 | EM_PotentialConstantExpression, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 467 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 468 | /// Fold the expression to a constant. Stop if we hit a side-effect that |
| 469 | /// we can't model. |
| 470 | EM_ConstantFold, |
| 471 | |
| 472 | /// Evaluate the expression looking for integer overflow and similar |
| 473 | /// issues. Don't worry about side-effects, and try to visit all |
| 474 | /// subexpressions. |
| 475 | EM_EvaluateForOverflow, |
| 476 | |
| 477 | /// Evaluate in any way we know how. Don't worry about side-effects that |
| 478 | /// can't be modeled. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 479 | EM_IgnoreSideEffects, |
| 480 | |
| 481 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 482 | /// is not a constant expression. Some expressions can be retried in the |
| 483 | /// optimizer if we don't constant fold them here, but in an unevaluated |
| 484 | /// context we try to fold them immediately since the optimizer never |
| 485 | /// gets a chance to look at it. |
| 486 | EM_ConstantExpressionUnevaluated, |
| 487 | |
| 488 | /// Evaluate as a potential constant expression. Keep going if we hit a |
| 489 | /// construct that we can't evaluate yet (because we don't yet know the |
| 490 | /// value of something) but stop if we hit something that could never be |
| 491 | /// a constant expression. Some expressions can be retried in the |
| 492 | /// optimizer if we don't constant fold them here, but in an unevaluated |
| 493 | /// context we try to fold them immediately since the optimizer never |
| 494 | /// gets a chance to look at it. |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 495 | EM_PotentialConstantExpressionUnevaluated, |
| 496 | |
| 497 | /// Evaluate as a constant expression. Continue evaluating if we find a |
| 498 | /// MemberExpr with a base that can't be evaluated. |
| 499 | EM_DesignatorFold, |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 500 | } EvalMode; |
| 501 | |
| 502 | /// Are we checking whether the expression is a potential constant |
| 503 | /// expression? |
| 504 | bool checkingPotentialConstantExpression() const { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 505 | return EvalMode == EM_PotentialConstantExpression || |
| 506 | EvalMode == EM_PotentialConstantExpressionUnevaluated; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /// Are we checking an expression for overflow? |
| 510 | // FIXME: We should check for any kind of undefined or suspicious behavior |
| 511 | // in such constructs, not just overflow. |
| 512 | bool checkingForOverflow() { return EvalMode == EM_EvaluateForOverflow; } |
| 513 | |
| 514 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 515 | : Ctx(const_cast<ASTContext &>(C)), EvalStatus(S), CurrentCall(nullptr), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 516 | CallStackDepth(0), NextCallIndex(1), |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 517 | StepsLeft(getLangOpts().ConstexprStepLimit), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 518 | BottomFrame(*this, SourceLocation(), nullptr, nullptr, nullptr), |
| 519 | EvaluatingDecl((const ValueDecl *)nullptr), |
| 520 | EvaluatingDeclValue(nullptr), HasActiveDiagnostic(false), |
| 521 | EvalMode(Mode) {} |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 522 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 523 | void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value) { |
| 524 | EvaluatingDecl = Base; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 525 | EvaluatingDeclValue = &Value; |
| 526 | } |
| 527 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 528 | const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); } |
Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 529 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 530 | bool CheckCallLimit(SourceLocation Loc) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 531 | // Don't perform any constexpr calls (other than the call we're checking) |
| 532 | // when checking a potential constant expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 533 | if (checkingPotentialConstantExpression() && CallStackDepth > 1) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 534 | return false; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 535 | if (NextCallIndex == 0) { |
| 536 | // NextCallIndex has wrapped around. |
| 537 | Diag(Loc, diag::note_constexpr_call_limit_exceeded); |
| 538 | return false; |
| 539 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 540 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 541 | return true; |
| 542 | Diag(Loc, diag::note_constexpr_depth_limit_exceeded) |
| 543 | << getLangOpts().ConstexprCallDepth; |
| 544 | return false; |
Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 545 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 546 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 547 | CallStackFrame *getCallFrame(unsigned CallIndex) { |
| 548 | assert(CallIndex && "no call index in getCallFrame"); |
| 549 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
| 550 | // be null in this loop. |
| 551 | CallStackFrame *Frame = CurrentCall; |
| 552 | while (Frame->Index > CallIndex) |
| 553 | Frame = Frame->Caller; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 554 | return (Frame->Index == CallIndex) ? Frame : nullptr; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 557 | bool nextStep(const Stmt *S) { |
| 558 | if (!StepsLeft) { |
| 559 | Diag(S->getLocStart(), diag::note_constexpr_step_limit_exceeded); |
| 560 | return false; |
| 561 | } |
| 562 | --StepsLeft; |
| 563 | return true; |
| 564 | } |
| 565 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 566 | private: |
| 567 | /// Add a diagnostic to the diagnostics list. |
| 568 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 569 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 570 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 571 | return EvalStatus.Diag->back().second; |
| 572 | } |
| 573 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 574 | /// Add notes containing a call stack to the current point of evaluation. |
| 575 | void addCallStack(unsigned Limit); |
| 576 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 577 | public: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 578 | /// Diagnose that the evaluation cannot be folded. |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 579 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId |
| 580 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 581 | unsigned ExtraNotes = 0) { |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 582 | if (EvalStatus.Diag) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 583 | // If we have a prior diagnostic, it will be noting that the expression |
| 584 | // isn't a constant expression. This diagnostic is more important, |
| 585 | // unless we require this evaluation to produce a constant expression. |
| 586 | // |
| 587 | // FIXME: We might want to show both diagnostics to the user in |
| 588 | // EM_ConstantFold mode. |
| 589 | if (!EvalStatus.Diag->empty()) { |
| 590 | switch (EvalMode) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 591 | case EM_ConstantFold: |
| 592 | case EM_IgnoreSideEffects: |
| 593 | case EM_EvaluateForOverflow: |
| 594 | if (!EvalStatus.HasSideEffects) |
| 595 | break; |
| 596 | // We've had side-effects; we want the diagnostic from them, not |
| 597 | // some later problem. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 598 | case EM_ConstantExpression: |
| 599 | case EM_PotentialConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 600 | case EM_ConstantExpressionUnevaluated: |
| 601 | case EM_PotentialConstantExpressionUnevaluated: |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 602 | case EM_DesignatorFold: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 603 | HasActiveDiagnostic = false; |
| 604 | return OptionalDiagnostic(); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 608 | unsigned CallStackNotes = CallStackDepth - 1; |
| 609 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
| 610 | if (Limit) |
| 611 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 612 | if (checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 613 | CallStackNotes = 0; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 614 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 615 | HasActiveDiagnostic = true; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 616 | EvalStatus.Diag->clear(); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 617 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
| 618 | addDiag(Loc, DiagId); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 619 | if (!checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 620 | addCallStack(Limit); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 621 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 622 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 623 | HasActiveDiagnostic = false; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 624 | return OptionalDiagnostic(); |
| 625 | } |
| 626 | |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 627 | OptionalDiagnostic Diag(const Expr *E, diag::kind DiagId |
| 628 | = diag::note_invalid_subexpr_in_const_expr, |
| 629 | unsigned ExtraNotes = 0) { |
| 630 | if (EvalStatus.Diag) |
| 631 | return Diag(E->getExprLoc(), DiagId, ExtraNotes); |
| 632 | HasActiveDiagnostic = false; |
| 633 | return OptionalDiagnostic(); |
| 634 | } |
| 635 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 636 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 637 | /// expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 638 | /// |
| 639 | /// FIXME: Stop evaluating if we're in EM_ConstantExpression or |
| 640 | /// EM_PotentialConstantExpression mode and we produce one of these. |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 641 | template<typename LocArg> |
| 642 | OptionalDiagnostic CCEDiag(LocArg Loc, diag::kind DiagId |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 643 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 644 | unsigned ExtraNotes = 0) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 645 | // Don't override a previous diagnostic. Don't bother collecting |
| 646 | // diagnostics if we're evaluating for overflow. |
Richard Smith | e9ff770 | 2013-11-05 22:23:30 +0000 | [diff] [blame] | 647 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) { |
Eli Friedman | ebea9af | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 648 | HasActiveDiagnostic = false; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 649 | return OptionalDiagnostic(); |
Eli Friedman | ebea9af | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 650 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 651 | return Diag(Loc, DiagId, ExtraNotes); |
| 652 | } |
| 653 | |
| 654 | /// Add a note to a prior diagnostic. |
| 655 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 656 | if (!HasActiveDiagnostic) |
| 657 | return OptionalDiagnostic(); |
| 658 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 659 | } |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 660 | |
| 661 | /// Add a stack of notes to a prior diagnostic. |
| 662 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
| 663 | if (HasActiveDiagnostic) { |
| 664 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
| 665 | Diags.begin(), Diags.end()); |
| 666 | } |
| 667 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 668 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 669 | /// Should we continue evaluation after encountering a side-effect that we |
| 670 | /// couldn't model? |
| 671 | bool keepEvaluatingAfterSideEffect() { |
| 672 | switch (EvalMode) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 673 | case EM_PotentialConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 674 | case EM_PotentialConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 675 | case EM_EvaluateForOverflow: |
| 676 | case EM_IgnoreSideEffects: |
| 677 | return true; |
| 678 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 679 | case EM_ConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 680 | case EM_ConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 681 | case EM_ConstantFold: |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 682 | case EM_DesignatorFold: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 683 | return false; |
| 684 | } |
Aaron Ballman | f682f53 | 2013-11-06 18:15:02 +0000 | [diff] [blame] | 685 | llvm_unreachable("Missed EvalMode case"); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /// Note that we have had a side-effect, and determine whether we should |
| 689 | /// keep evaluating. |
| 690 | bool noteSideEffect() { |
| 691 | EvalStatus.HasSideEffects = true; |
| 692 | return keepEvaluatingAfterSideEffect(); |
| 693 | } |
| 694 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 695 | /// Should we continue evaluation as much as possible after encountering a |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 696 | /// construct which can't be reduced to a value? |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 697 | bool keepEvaluatingAfterFailure() { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 698 | if (!StepsLeft) |
| 699 | return false; |
| 700 | |
| 701 | switch (EvalMode) { |
| 702 | case EM_PotentialConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 703 | case EM_PotentialConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 704 | case EM_EvaluateForOverflow: |
| 705 | return true; |
| 706 | |
| 707 | case EM_ConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 708 | case EM_ConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 709 | case EM_ConstantFold: |
| 710 | case EM_IgnoreSideEffects: |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 711 | case EM_DesignatorFold: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 712 | return false; |
| 713 | } |
Aaron Ballman | f682f53 | 2013-11-06 18:15:02 +0000 | [diff] [blame] | 714 | llvm_unreachable("Missed EvalMode case"); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 715 | } |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 716 | |
| 717 | bool allowInvalidBaseExpr() const { |
| 718 | return EvalMode == EM_DesignatorFold; |
| 719 | } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 720 | }; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 721 | |
| 722 | /// Object used to treat all foldable expressions as constant expressions. |
| 723 | struct FoldConstant { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 724 | EvalInfo &Info; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 725 | bool Enabled; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 726 | bool HadNoPriorDiags; |
| 727 | EvalInfo::EvaluationMode OldMode; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 728 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 729 | explicit FoldConstant(EvalInfo &Info, bool Enabled) |
| 730 | : Info(Info), |
| 731 | Enabled(Enabled), |
| 732 | HadNoPriorDiags(Info.EvalStatus.Diag && |
| 733 | Info.EvalStatus.Diag->empty() && |
| 734 | !Info.EvalStatus.HasSideEffects), |
| 735 | OldMode(Info.EvalMode) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 736 | if (Enabled && |
| 737 | (Info.EvalMode == EvalInfo::EM_ConstantExpression || |
| 738 | Info.EvalMode == EvalInfo::EM_ConstantExpressionUnevaluated)) |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 739 | Info.EvalMode = EvalInfo::EM_ConstantFold; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 740 | } |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 741 | void keepDiagnostics() { Enabled = false; } |
| 742 | ~FoldConstant() { |
| 743 | if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() && |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 744 | !Info.EvalStatus.HasSideEffects) |
| 745 | Info.EvalStatus.Diag->clear(); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 746 | Info.EvalMode = OldMode; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 747 | } |
| 748 | }; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 749 | |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 750 | /// RAII object used to treat the current evaluation as the correct pointer |
| 751 | /// offset fold for the current EvalMode |
| 752 | struct FoldOffsetRAII { |
| 753 | EvalInfo &Info; |
| 754 | EvalInfo::EvaluationMode OldMode; |
| 755 | explicit FoldOffsetRAII(EvalInfo &Info, bool Subobject) |
| 756 | : Info(Info), OldMode(Info.EvalMode) { |
| 757 | if (!Info.checkingPotentialConstantExpression()) |
| 758 | Info.EvalMode = Subobject ? EvalInfo::EM_DesignatorFold |
| 759 | : EvalInfo::EM_ConstantFold; |
| 760 | } |
| 761 | |
| 762 | ~FoldOffsetRAII() { Info.EvalMode = OldMode; } |
| 763 | }; |
| 764 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 765 | /// RAII object used to suppress diagnostics and side-effects from a |
| 766 | /// speculative evaluation. |
| 767 | class SpeculativeEvaluationRAII { |
| 768 | EvalInfo &Info; |
| 769 | Expr::EvalStatus Old; |
| 770 | |
| 771 | public: |
| 772 | SpeculativeEvaluationRAII(EvalInfo &Info, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 773 | SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr) |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 774 | : Info(Info), Old(Info.EvalStatus) { |
| 775 | Info.EvalStatus.Diag = NewDiag; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 776 | // If we're speculatively evaluating, we may have skipped over some |
| 777 | // evaluations and missed out a side effect. |
| 778 | Info.EvalStatus.HasSideEffects = true; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 779 | } |
| 780 | ~SpeculativeEvaluationRAII() { |
| 781 | Info.EvalStatus = Old; |
| 782 | } |
| 783 | }; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 784 | |
| 785 | /// RAII object wrapping a full-expression or block scope, and handling |
| 786 | /// the ending of the lifetime of temporaries created within it. |
| 787 | template<bool IsFullExpression> |
| 788 | class ScopeRAII { |
| 789 | EvalInfo &Info; |
| 790 | unsigned OldStackSize; |
| 791 | public: |
| 792 | ScopeRAII(EvalInfo &Info) |
| 793 | : Info(Info), OldStackSize(Info.CleanupStack.size()) {} |
| 794 | ~ScopeRAII() { |
| 795 | // Body moved to a static method to encourage the compiler to inline away |
| 796 | // instances of this class. |
| 797 | cleanup(Info, OldStackSize); |
| 798 | } |
| 799 | private: |
| 800 | static void cleanup(EvalInfo &Info, unsigned OldStackSize) { |
| 801 | unsigned NewEnd = OldStackSize; |
| 802 | for (unsigned I = OldStackSize, N = Info.CleanupStack.size(); |
| 803 | I != N; ++I) { |
| 804 | if (IsFullExpression && Info.CleanupStack[I].isLifetimeExtended()) { |
| 805 | // Full-expression cleanup of a lifetime-extended temporary: nothing |
| 806 | // to do, just move this cleanup to the right place in the stack. |
| 807 | std::swap(Info.CleanupStack[I], Info.CleanupStack[NewEnd]); |
| 808 | ++NewEnd; |
| 809 | } else { |
| 810 | // End the lifetime of the object. |
| 811 | Info.CleanupStack[I].endLifetime(); |
| 812 | } |
| 813 | } |
| 814 | Info.CleanupStack.erase(Info.CleanupStack.begin() + NewEnd, |
| 815 | Info.CleanupStack.end()); |
| 816 | } |
| 817 | }; |
| 818 | typedef ScopeRAII<false> BlockScopeRAII; |
| 819 | typedef ScopeRAII<true> FullExpressionRAII; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 820 | } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 821 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 822 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 823 | CheckSubobjectKind CSK) { |
| 824 | if (Invalid) |
| 825 | return false; |
| 826 | if (isOnePastTheEnd()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 827 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 828 | << CSK; |
| 829 | setInvalid(); |
| 830 | return false; |
| 831 | } |
| 832 | return true; |
| 833 | } |
| 834 | |
| 835 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
| 836 | const Expr *E, uint64_t N) { |
| 837 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 838 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 839 | << static_cast<int>(N) << /*array*/ 0 |
| 840 | << static_cast<unsigned>(MostDerivedArraySize); |
| 841 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 842 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 843 | << static_cast<int>(N) << /*non-array*/ 1; |
| 844 | setInvalid(); |
| 845 | } |
| 846 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 847 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 848 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 849 | APValue *Arguments) |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 850 | : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 851 | Index(Info.NextCallIndex++), This(This), Arguments(Arguments) { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 852 | Info.CurrentCall = this; |
| 853 | ++Info.CallStackDepth; |
| 854 | } |
| 855 | |
| 856 | CallStackFrame::~CallStackFrame() { |
| 857 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 858 | --Info.CallStackDepth; |
| 859 | Info.CurrentCall = Caller; |
| 860 | } |
| 861 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 862 | APValue &CallStackFrame::createTemporary(const void *Key, |
| 863 | bool IsLifetimeExtended) { |
| 864 | APValue &Result = Temporaries[Key]; |
| 865 | assert(Result.isUninit() && "temporary created multiple times"); |
| 866 | Info.CleanupStack.push_back(Cleanup(&Result, IsLifetimeExtended)); |
| 867 | return Result; |
| 868 | } |
| 869 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 870 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 871 | |
| 872 | void EvalInfo::addCallStack(unsigned Limit) { |
| 873 | // Determine which calls to skip, if any. |
| 874 | unsigned ActiveCalls = CallStackDepth - 1; |
| 875 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
| 876 | if (Limit && Limit < ActiveCalls) { |
| 877 | SkipStart = Limit / 2 + Limit % 2; |
| 878 | SkipEnd = ActiveCalls - Limit / 2; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 881 | // Walk the call stack and add the diagnostics. |
| 882 | unsigned CallIdx = 0; |
| 883 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
| 884 | Frame = Frame->Caller, ++CallIdx) { |
| 885 | // Skip this call? |
| 886 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
| 887 | if (CallIdx == SkipStart) { |
| 888 | // Note that we're skipping calls. |
| 889 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
| 890 | << unsigned(ActiveCalls - Limit); |
| 891 | } |
| 892 | continue; |
| 893 | } |
| 894 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 895 | SmallVector<char, 128> Buffer; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 896 | llvm::raw_svector_ostream Out(Buffer); |
| 897 | describeCall(Frame, Out); |
| 898 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | namespace { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 903 | struct ComplexValue { |
| 904 | private: |
| 905 | bool IsInt; |
| 906 | |
| 907 | public: |
| 908 | APSInt IntReal, IntImag; |
| 909 | APFloat FloatReal, FloatImag; |
| 910 | |
| 911 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 912 | |
| 913 | void makeComplexFloat() { IsInt = false; } |
| 914 | bool isComplexFloat() const { return !IsInt; } |
| 915 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 916 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 917 | |
| 918 | void makeComplexInt() { IsInt = true; } |
| 919 | bool isComplexInt() const { return IsInt; } |
| 920 | APSInt &getComplexIntReal() { return IntReal; } |
| 921 | APSInt &getComplexIntImag() { return IntImag; } |
| 922 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 923 | void moveInto(APValue &v) const { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 924 | if (isComplexFloat()) |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 925 | v = APValue(FloatReal, FloatImag); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 926 | else |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 927 | v = APValue(IntReal, IntImag); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 928 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 929 | void setFrom(const APValue &v) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 930 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 931 | if (v.isComplexFloat()) { |
| 932 | makeComplexFloat(); |
| 933 | FloatReal = v.getComplexFloatReal(); |
| 934 | FloatImag = v.getComplexFloatImag(); |
| 935 | } else { |
| 936 | makeComplexInt(); |
| 937 | IntReal = v.getComplexIntReal(); |
| 938 | IntImag = v.getComplexIntImag(); |
| 939 | } |
| 940 | } |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 941 | }; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 942 | |
| 943 | struct LValue { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 944 | APValue::LValueBase Base; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 945 | CharUnits Offset; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 946 | bool InvalidBase : 1; |
| 947 | unsigned CallIndex : 31; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 948 | SubobjectDesignator Designator; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 949 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 950 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 951 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 952 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 953 | unsigned getLValueCallIndex() const { return CallIndex; } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 954 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 955 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 956 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 957 | void moveInto(APValue &V) const { |
| 958 | if (Designator.Invalid) |
| 959 | V = APValue(Base, Offset, APValue::NoLValuePath(), CallIndex); |
| 960 | else |
| 961 | V = APValue(Base, Offset, Designator.Entries, |
| 962 | Designator.IsOnePastTheEnd, CallIndex); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 963 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 964 | void setFrom(ASTContext &Ctx, const APValue &V) { |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 965 | assert(V.isLValue()); |
| 966 | Base = V.getLValueBase(); |
| 967 | Offset = V.getLValueOffset(); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 968 | InvalidBase = false; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 969 | CallIndex = V.getLValueCallIndex(); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 970 | Designator = SubobjectDesignator(Ctx, V); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 971 | } |
| 972 | |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 973 | void set(APValue::LValueBase B, unsigned I = 0, bool BInvalid = false) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 974 | Base = B; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 975 | Offset = CharUnits::Zero(); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 976 | InvalidBase = BInvalid; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 977 | CallIndex = I; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 978 | Designator = SubobjectDesignator(getType(B)); |
| 979 | } |
| 980 | |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 981 | void setInvalid(APValue::LValueBase B, unsigned I = 0) { |
| 982 | set(B, I, true); |
| 983 | } |
| 984 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 985 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 986 | // a diagnostic and mark the designator as invalid. |
| 987 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 988 | CheckSubobjectKind CSK) { |
| 989 | if (Designator.Invalid) |
| 990 | return false; |
| 991 | if (!Base) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 992 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 993 | << CSK; |
| 994 | Designator.setInvalid(); |
| 995 | return false; |
| 996 | } |
| 997 | return true; |
| 998 | } |
| 999 | |
| 1000 | // Check this LValue refers to an object. If not, set the designator to be |
| 1001 | // invalid and emit a diagnostic. |
| 1002 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
Richard Smith | 6c6bbfa | 2014-04-08 12:19:28 +0000 | [diff] [blame] | 1003 | return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) && |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1004 | Designator.checkSubobject(Info, E, CSK); |
| 1005 | } |
| 1006 | |
| 1007 | void addDecl(EvalInfo &Info, const Expr *E, |
| 1008 | const Decl *D, bool Virtual = false) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1009 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
| 1010 | Designator.addDeclUnchecked(D, Virtual); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1011 | } |
| 1012 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1013 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
| 1014 | Designator.addArrayUnchecked(CAT); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1015 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1016 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1017 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
| 1018 | Designator.addComplexUnchecked(EltTy, Imag); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1019 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1020 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 6c6bbfa | 2014-04-08 12:19:28 +0000 | [diff] [blame] | 1021 | if (N && checkNullPointer(Info, E, CSK_ArrayIndex)) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1022 | Designator.adjustIndex(Info, E, N); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1023 | } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1024 | }; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1025 | |
| 1026 | struct MemberPtr { |
| 1027 | MemberPtr() {} |
| 1028 | explicit MemberPtr(const ValueDecl *Decl) : |
| 1029 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 1030 | |
| 1031 | /// The member or (direct or indirect) field referred to by this member |
| 1032 | /// pointer, or 0 if this is a null member pointer. |
| 1033 | const ValueDecl *getDecl() const { |
| 1034 | return DeclAndIsDerivedMember.getPointer(); |
| 1035 | } |
| 1036 | /// Is this actually a member of some type derived from the relevant class? |
| 1037 | bool isDerivedMember() const { |
| 1038 | return DeclAndIsDerivedMember.getInt(); |
| 1039 | } |
| 1040 | /// Get the class which the declaration actually lives in. |
| 1041 | const CXXRecordDecl *getContainingRecord() const { |
| 1042 | return cast<CXXRecordDecl>( |
| 1043 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 1044 | } |
| 1045 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1046 | void moveInto(APValue &V) const { |
| 1047 | V = APValue(getDecl(), isDerivedMember(), Path); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1048 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1049 | void setFrom(const APValue &V) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1050 | assert(V.isMemberPointer()); |
| 1051 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 1052 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 1053 | Path.clear(); |
| 1054 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 1055 | Path.insert(Path.end(), P.begin(), P.end()); |
| 1056 | } |
| 1057 | |
| 1058 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 1059 | /// whether the member is a member of some class derived from the class type |
| 1060 | /// of the member pointer. |
| 1061 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 1062 | /// Path - The path of base/derived classes from the member declaration's |
| 1063 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 1064 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 1065 | |
| 1066 | /// Perform a cast towards the class of the Decl (either up or down the |
| 1067 | /// hierarchy). |
| 1068 | bool castBack(const CXXRecordDecl *Class) { |
| 1069 | assert(!Path.empty()); |
| 1070 | const CXXRecordDecl *Expected; |
| 1071 | if (Path.size() >= 2) |
| 1072 | Expected = Path[Path.size() - 2]; |
| 1073 | else |
| 1074 | Expected = getContainingRecord(); |
| 1075 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 1076 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 1077 | // if B does not contain the original member and is not a base or |
| 1078 | // derived class of the class containing the original member, the result |
| 1079 | // of the cast is undefined. |
| 1080 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 1081 | // (D::*). We consider that to be a language defect. |
| 1082 | return false; |
| 1083 | } |
| 1084 | Path.pop_back(); |
| 1085 | return true; |
| 1086 | } |
| 1087 | /// Perform a base-to-derived member pointer cast. |
| 1088 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 1089 | if (!getDecl()) |
| 1090 | return true; |
| 1091 | if (!isDerivedMember()) { |
| 1092 | Path.push_back(Derived); |
| 1093 | return true; |
| 1094 | } |
| 1095 | if (!castBack(Derived)) |
| 1096 | return false; |
| 1097 | if (Path.empty()) |
| 1098 | DeclAndIsDerivedMember.setInt(false); |
| 1099 | return true; |
| 1100 | } |
| 1101 | /// Perform a derived-to-base member pointer cast. |
| 1102 | bool castToBase(const CXXRecordDecl *Base) { |
| 1103 | if (!getDecl()) |
| 1104 | return true; |
| 1105 | if (Path.empty()) |
| 1106 | DeclAndIsDerivedMember.setInt(true); |
| 1107 | if (isDerivedMember()) { |
| 1108 | Path.push_back(Base); |
| 1109 | return true; |
| 1110 | } |
| 1111 | return castBack(Base); |
| 1112 | } |
| 1113 | }; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1114 | |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 1115 | /// Compare two member pointers, which are assumed to be of the same type. |
| 1116 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
| 1117 | if (!LHS.getDecl() || !RHS.getDecl()) |
| 1118 | return !LHS.getDecl() && !RHS.getDecl(); |
| 1119 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
| 1120 | return false; |
| 1121 | return LHS.Path == RHS.Path; |
| 1122 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1123 | } |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1124 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1125 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1126 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
| 1127 | const LValue &This, const Expr *E, |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1128 | bool AllowNonLiteralTypes = false); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1129 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 1130 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1131 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 1132 | EvalInfo &Info); |
| 1133 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1134 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1135 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 1136 | EvalInfo &Info); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1137 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1138 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 1139 | static bool EvaluateAtomic(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1140 | |
| 1141 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1142 | // Misc utilities |
| 1143 | //===----------------------------------------------------------------------===// |
| 1144 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1145 | /// Produce a string describing the given constexpr call. |
| 1146 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out) { |
| 1147 | unsigned ArgIndex = 0; |
| 1148 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
| 1149 | !isa<CXXConstructorDecl>(Frame->Callee) && |
| 1150 | cast<CXXMethodDecl>(Frame->Callee)->isInstance(); |
| 1151 | |
| 1152 | if (!IsMemberCall) |
| 1153 | Out << *Frame->Callee << '('; |
| 1154 | |
| 1155 | if (Frame->This && IsMemberCall) { |
| 1156 | APValue Val; |
| 1157 | Frame->This->moveInto(Val); |
| 1158 | Val.printPretty(Out, Frame->Info.Ctx, |
| 1159 | Frame->This->Designator.MostDerivedType); |
| 1160 | // FIXME: Add parens around Val if needed. |
| 1161 | Out << "->" << *Frame->Callee << '('; |
| 1162 | IsMemberCall = false; |
| 1163 | } |
| 1164 | |
| 1165 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
| 1166 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
| 1167 | if (ArgIndex > (unsigned)IsMemberCall) |
| 1168 | Out << ", "; |
| 1169 | |
| 1170 | const ParmVarDecl *Param = *I; |
| 1171 | const APValue &Arg = Frame->Arguments[ArgIndex]; |
| 1172 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
| 1173 | |
| 1174 | if (ArgIndex == 0 && IsMemberCall) |
| 1175 | Out << "->" << *Frame->Callee << '('; |
| 1176 | } |
| 1177 | |
| 1178 | Out << ')'; |
| 1179 | } |
| 1180 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1181 | /// Evaluate an expression to see if it had side-effects, and discard its |
| 1182 | /// result. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1183 | /// \return \c true if the caller should keep evaluating. |
| 1184 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1185 | APValue Scratch; |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 1186 | if (!Evaluate(Scratch, Info, E)) |
| 1187 | // We don't need the value, but we might have skipped a side effect here. |
| 1188 | return Info.noteSideEffect(); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1189 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 1192 | /// Sign- or zero-extend a value to 64 bits. If it's already 64 bits, just |
| 1193 | /// return its existing value. |
| 1194 | static int64_t getExtValue(const APSInt &Value) { |
| 1195 | return Value.isSigned() ? Value.getSExtValue() |
| 1196 | : static_cast<int64_t>(Value.getZExtValue()); |
| 1197 | } |
| 1198 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1199 | /// Should this call expression be treated as a string literal? |
| 1200 | static bool IsStringLiteralCall(const CallExpr *E) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 1201 | unsigned Builtin = E->getBuiltinCallee(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1202 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 1203 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 1204 | } |
| 1205 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1206 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1207 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 1208 | // constant expression of pointer type that evaluates to... |
| 1209 | |
| 1210 | // ... a null pointer value, or a prvalue core constant expression of type |
| 1211 | // std::nullptr_t. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1212 | if (!B) return true; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1213 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1214 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 1215 | // ... the address of an object with static storage duration, |
| 1216 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 1217 | return VD->hasGlobalStorage(); |
| 1218 | // ... the address of a function, |
| 1219 | return isa<FunctionDecl>(D); |
| 1220 | } |
| 1221 | |
| 1222 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1223 | switch (E->getStmtClass()) { |
| 1224 | default: |
| 1225 | return false; |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1226 | case Expr::CompoundLiteralExprClass: { |
| 1227 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 1228 | return CLE->isFileScope() && CLE->isLValue(); |
| 1229 | } |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1230 | case Expr::MaterializeTemporaryExprClass: |
| 1231 | // A materialized temporary might have been lifetime-extended to static |
| 1232 | // storage duration. |
| 1233 | return cast<MaterializeTemporaryExpr>(E)->getStorageDuration() == SD_Static; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1234 | // A string literal has static storage duration. |
| 1235 | case Expr::StringLiteralClass: |
| 1236 | case Expr::PredefinedExprClass: |
| 1237 | case Expr::ObjCStringLiteralClass: |
| 1238 | case Expr::ObjCEncodeExprClass: |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 1239 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 1240 | case Expr::CXXUuidofExprClass: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1241 | return true; |
| 1242 | case Expr::CallExprClass: |
| 1243 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 1244 | // For GCC compatibility, &&label has static storage duration. |
| 1245 | case Expr::AddrLabelExprClass: |
| 1246 | return true; |
| 1247 | // A Block literal expression may be used as the initialization value for |
| 1248 | // Block variables at global or local static scope. |
| 1249 | case Expr::BlockExprClass: |
| 1250 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1251 | case Expr::ImplicitValueInitExprClass: |
| 1252 | // FIXME: |
| 1253 | // We can never form an lvalue with an implicit value initialization as its |
| 1254 | // base through expression evaluation, so these only appear in one case: the |
| 1255 | // implicit variable declaration we invent when checking whether a constexpr |
| 1256 | // constructor can produce a constant expression. We must assume that such |
| 1257 | // an expression might be a global lvalue. |
| 1258 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1259 | } |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1262 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
| 1263 | assert(Base && "no location for a null lvalue"); |
| 1264 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 1265 | if (VD) |
| 1266 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1267 | else |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 1268 | Info.Note(Base.get<const Expr*>()->getExprLoc(), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1269 | diag::note_constexpr_temporary_here); |
| 1270 | } |
| 1271 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1272 | /// Check that this reference or pointer core constant expression is a valid |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1273 | /// value for an address or reference constant expression. Return true if we |
| 1274 | /// can fold this expression, whether or not it's a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1275 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
| 1276 | QualType Type, const LValue &LVal) { |
| 1277 | bool IsReferenceType = Type->isReferenceType(); |
| 1278 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1279 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 1280 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 1281 | |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1282 | // Check that the object is a global. Note that the fake 'this' object we |
| 1283 | // manufacture when checking potential constant expressions is conservatively |
| 1284 | // assumed to be global here. |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1285 | if (!IsGlobalLValue(Base)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1286 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1287 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1288 | Info.Diag(Loc, diag::note_constexpr_non_global, 1) |
| 1289 | << IsReferenceType << !Designator.Entries.empty() |
| 1290 | << !!VD << VD; |
| 1291 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1292 | } else { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1293 | Info.Diag(Loc); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1294 | } |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1295 | // Don't allow references to temporaries to escape. |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1296 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1297 | } |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1298 | assert((Info.checkingPotentialConstantExpression() || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1299 | LVal.getLValueCallIndex() == 0) && |
| 1300 | "have call index for global lvalue"); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1301 | |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1302 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
| 1303 | if (const VarDecl *Var = dyn_cast<const VarDecl>(VD)) { |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1304 | // Check if this is a thread-local variable. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1305 | if (Var->getTLSKind()) |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1306 | return false; |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1307 | |
Hans Wennborg | 82dd877 | 2014-06-25 22:19:48 +0000 | [diff] [blame] | 1308 | // A dllimport variable never acts like a constant. |
| 1309 | if (Var->hasAttr<DLLImportAttr>()) |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1310 | return false; |
| 1311 | } |
| 1312 | if (const auto *FD = dyn_cast<const FunctionDecl>(VD)) { |
| 1313 | // __declspec(dllimport) must be handled very carefully: |
| 1314 | // We must never initialize an expression with the thunk in C++. |
| 1315 | // Doing otherwise would allow the same id-expression to yield |
| 1316 | // different addresses for the same function in different translation |
| 1317 | // units. However, this means that we must dynamically initialize the |
| 1318 | // expression with the contents of the import address table at runtime. |
| 1319 | // |
| 1320 | // The C language has no notion of ODR; furthermore, it has no notion of |
| 1321 | // dynamic initialization. This means that we are permitted to |
| 1322 | // perform initialization with the address of the thunk. |
Hans Wennborg | 82dd877 | 2014-06-25 22:19:48 +0000 | [diff] [blame] | 1323 | if (Info.getLangOpts().CPlusPlus && FD->hasAttr<DLLImportAttr>()) |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1324 | return false; |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1328 | // Allow address constant expressions to be past-the-end pointers. This is |
| 1329 | // an extension: the standard requires them to point to an object. |
| 1330 | if (!IsReferenceType) |
| 1331 | return true; |
| 1332 | |
| 1333 | // A reference constant expression must refer to an object. |
| 1334 | if (!Base) { |
| 1335 | // FIXME: diagnostic |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1336 | Info.CCEDiag(Loc); |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1337 | return true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1340 | // Does this refer one past the end of some object? |
Richard Smith | 33b44ab | 2014-07-23 23:50:25 +0000 | [diff] [blame] | 1341 | if (!Designator.Invalid && Designator.isOnePastTheEnd()) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1342 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1343 | Info.Diag(Loc, diag::note_constexpr_past_end, 1) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1344 | << !Designator.Entries.empty() << !!VD << VD; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1345 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1348 | return true; |
| 1349 | } |
| 1350 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1351 | /// Check that this core constant expression is of literal type, and if not, |
| 1352 | /// produce an appropriate diagnostic. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1353 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1354 | const LValue *This = nullptr) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1355 | if (!E->isRValue() || E->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1356 | return true; |
| 1357 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1358 | // C++1y: A constant initializer for an object o [...] may also invoke |
| 1359 | // constexpr constructors for o and its subobjects even if those objects |
| 1360 | // are of non-literal class types. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 1361 | if (Info.getLangOpts().CPlusPlus14 && This && |
Richard Smith | 37dc92e | 2013-05-16 05:04:51 +0000 | [diff] [blame] | 1362 | Info.EvaluatingDecl == This->getLValueBase()) |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1363 | return true; |
| 1364 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1365 | // Prvalue constant expressions must be of literal types. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1366 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1367 | Info.Diag(E, diag::note_constexpr_nonliteral) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1368 | << E->getType(); |
| 1369 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1370 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1371 | return false; |
| 1372 | } |
| 1373 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1374 | /// Check that this core constant expression value is a valid value for a |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1375 | /// constant expression. If not, report an appropriate diagnostic. Does not |
| 1376 | /// check that the expression is of literal type. |
| 1377 | static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, |
| 1378 | QualType Type, const APValue &Value) { |
Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 1379 | if (Value.isUninit()) { |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 1380 | Info.Diag(DiagLoc, diag::note_constexpr_uninitialized) |
| 1381 | << true << Type; |
Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 1382 | return false; |
| 1383 | } |
| 1384 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 1385 | // We allow _Atomic(T) to be initialized from anything that T can be |
| 1386 | // initialized from. |
| 1387 | if (const AtomicType *AT = Type->getAs<AtomicType>()) |
| 1388 | Type = AT->getValueType(); |
| 1389 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1390 | // Core issue 1454: For a literal constant expression of array or class type, |
| 1391 | // each subobject of its value shall have been initialized by a constant |
| 1392 | // expression. |
| 1393 | if (Value.isArray()) { |
| 1394 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
| 1395 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
| 1396 | if (!CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1397 | Value.getArrayInitializedElt(I))) |
| 1398 | return false; |
| 1399 | } |
| 1400 | if (!Value.hasArrayFiller()) |
| 1401 | return true; |
| 1402 | return CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1403 | Value.getArrayFiller()); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1404 | } |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1405 | if (Value.isUnion() && Value.getUnionField()) { |
| 1406 | return CheckConstantExpression(Info, DiagLoc, |
| 1407 | Value.getUnionField()->getType(), |
| 1408 | Value.getUnionValue()); |
| 1409 | } |
| 1410 | if (Value.isStruct()) { |
| 1411 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
| 1412 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1413 | unsigned BaseIndex = 0; |
| 1414 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
| 1415 | End = CD->bases_end(); I != End; ++I, ++BaseIndex) { |
| 1416 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1417 | Value.getStructBase(BaseIndex))) |
| 1418 | return false; |
| 1419 | } |
| 1420 | } |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1421 | for (const auto *I : RD->fields()) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1422 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1423 | Value.getStructField(I->getFieldIndex()))) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1424 | return false; |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | if (Value.isLValue()) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1429 | LValue LVal; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1430 | LVal.setFrom(Info.Ctx, Value); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1431 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal); |
| 1432 | } |
| 1433 | |
| 1434 | // Everything else is fine. |
| 1435 | return true; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 1438 | static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1439 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | static bool IsLiteralLValue(const LValue &Value) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1443 | if (Value.CallIndex) |
| 1444 | return false; |
| 1445 | const Expr *E = Value.Base.dyn_cast<const Expr*>(); |
| 1446 | return E && !isa<MaterializeTemporaryExpr>(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1449 | static bool IsWeakLValue(const LValue &Value) { |
| 1450 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1451 | return Decl && Decl->isWeak(); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 1454 | static bool isZeroSized(const LValue &Value) { |
| 1455 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
David Majnemer | 27db358 | 2014-12-11 19:36:24 +0000 | [diff] [blame] | 1456 | if (Decl && isa<VarDecl>(Decl)) { |
| 1457 | QualType Ty = Decl->getType(); |
David Majnemer | 8c92b87 | 2014-12-14 08:40:47 +0000 | [diff] [blame] | 1458 | if (Ty->isArrayType()) |
| 1459 | return Ty->isIncompleteType() || |
| 1460 | Decl->getASTContext().getTypeSize(Ty) == 0; |
David Majnemer | 27db358 | 2014-12-11 19:36:24 +0000 | [diff] [blame] | 1461 | } |
| 1462 | return false; |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1465 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1466 | // A null base expression indicates a null pointer. These are always |
| 1467 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1468 | if (!Value.getLValueBase()) { |
| 1469 | Result = !Value.getLValueOffset().isZero(); |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1470 | return true; |
| 1471 | } |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 1472 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1473 | // We have a non-null base. These are generally known to be true, but if it's |
| 1474 | // a weak declaration it can be null at runtime. |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1475 | Result = true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1476 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1477 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1480 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1481 | switch (Val.getKind()) { |
| 1482 | case APValue::Uninitialized: |
| 1483 | return false; |
| 1484 | case APValue::Int: |
| 1485 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1486 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1487 | case APValue::Float: |
| 1488 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1489 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1490 | case APValue::ComplexInt: |
| 1491 | Result = Val.getComplexIntReal().getBoolValue() || |
| 1492 | Val.getComplexIntImag().getBoolValue(); |
| 1493 | return true; |
| 1494 | case APValue::ComplexFloat: |
| 1495 | Result = !Val.getComplexFloatReal().isZero() || |
| 1496 | !Val.getComplexFloatImag().isZero(); |
| 1497 | return true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1498 | case APValue::LValue: |
| 1499 | return EvalPointerValueAsBool(Val, Result); |
| 1500 | case APValue::MemberPointer: |
| 1501 | Result = Val.getMemberPointerDecl(); |
| 1502 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1503 | case APValue::Vector: |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1504 | case APValue::Array: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1505 | case APValue::Struct: |
| 1506 | case APValue::Union: |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 1507 | case APValue::AddrLabelDiff: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1508 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1511 | llvm_unreachable("unknown APValue kind"); |
| 1512 | } |
| 1513 | |
| 1514 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 1515 | EvalInfo &Info) { |
| 1516 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1517 | APValue Val; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1518 | if (!Evaluate(Val, Info, E)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1519 | return false; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1520 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1523 | template<typename T> |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1524 | static void HandleOverflow(EvalInfo &Info, const Expr *E, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1525 | const T &SrcValue, QualType DestType) { |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1526 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 1527 | << SrcValue << DestType; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 1531 | QualType SrcType, const APFloat &Value, |
| 1532 | QualType DestType, APSInt &Result) { |
| 1533 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1534 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1535 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1537 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1538 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1539 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 1540 | & APFloat::opInvalidOp) |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1541 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1542 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1545 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 1546 | QualType SrcType, QualType DestType, |
| 1547 | APFloat &Result) { |
| 1548 | APFloat Value = Result; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1549 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1550 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 1551 | APFloat::rmNearestTiesToEven, &ignored) |
| 1552 | & APFloat::opOverflow) |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1553 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1554 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 1557 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 1558 | QualType DestType, QualType SrcType, |
| 1559 | APSInt &Value) { |
| 1560 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1561 | APSInt Result = Value; |
| 1562 | // Figure out if this is a truncate, extend or noop cast. |
| 1563 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1564 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1565 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1566 | return Result; |
| 1567 | } |
| 1568 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1569 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 1570 | QualType SrcType, const APSInt &Value, |
| 1571 | QualType DestType, APFloat &Result) { |
| 1572 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 1573 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 1574 | APFloat::rmNearestTiesToEven) |
| 1575 | & APFloat::opOverflow) |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1576 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1577 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 1580 | static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E, |
| 1581 | APValue &Value, const FieldDecl *FD) { |
| 1582 | assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield"); |
| 1583 | |
| 1584 | if (!Value.isInt()) { |
| 1585 | // Trying to store a pointer-cast-to-integer into a bitfield. |
| 1586 | // FIXME: In this case, we should provide the diagnostic for casting |
| 1587 | // a pointer to an integer. |
| 1588 | assert(Value.isLValue() && "integral value neither int nor lvalue?"); |
| 1589 | Info.Diag(E); |
| 1590 | return false; |
| 1591 | } |
| 1592 | |
| 1593 | APSInt &Int = Value.getInt(); |
| 1594 | unsigned OldBitWidth = Int.getBitWidth(); |
| 1595 | unsigned NewBitWidth = FD->getBitWidthValue(Info.Ctx); |
| 1596 | if (NewBitWidth < OldBitWidth) |
| 1597 | Int = Int.trunc(NewBitWidth).extend(OldBitWidth); |
| 1598 | return true; |
| 1599 | } |
| 1600 | |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1601 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 1602 | llvm::APInt &Res) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1603 | APValue SVal; |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1604 | if (!Evaluate(SVal, Info, E)) |
| 1605 | return false; |
| 1606 | if (SVal.isInt()) { |
| 1607 | Res = SVal.getInt(); |
| 1608 | return true; |
| 1609 | } |
| 1610 | if (SVal.isFloat()) { |
| 1611 | Res = SVal.getFloat().bitcastToAPInt(); |
| 1612 | return true; |
| 1613 | } |
| 1614 | if (SVal.isVector()) { |
| 1615 | QualType VecTy = E->getType(); |
| 1616 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 1617 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 1618 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 1619 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 1620 | Res = llvm::APInt::getNullValue(VecSize); |
| 1621 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 1622 | APValue &Elt = SVal.getVectorElt(i); |
| 1623 | llvm::APInt EltAsInt; |
| 1624 | if (Elt.isInt()) { |
| 1625 | EltAsInt = Elt.getInt(); |
| 1626 | } else if (Elt.isFloat()) { |
| 1627 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 1628 | } else { |
| 1629 | // Don't try to handle vectors of anything other than int or float |
| 1630 | // (not sure if it's possible to hit this case). |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1631 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1632 | return false; |
| 1633 | } |
| 1634 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 1635 | if (BigEndian) |
| 1636 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 1637 | else |
| 1638 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 1639 | } |
| 1640 | return true; |
| 1641 | } |
| 1642 | // Give up if the input isn't an int, float, or vector. For example, we |
| 1643 | // reject "(v4i16)(intptr_t)&a". |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1644 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1645 | return false; |
| 1646 | } |
| 1647 | |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 1648 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 1649 | /// bits, and check for overflow in the original type (if that type was not an |
| 1650 | /// unsigned type). |
| 1651 | template<typename Operation> |
| 1652 | static APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 1653 | const APSInt &LHS, const APSInt &RHS, |
| 1654 | unsigned BitWidth, Operation Op) { |
| 1655 | if (LHS.isUnsigned()) |
| 1656 | return Op(LHS, RHS); |
| 1657 | |
| 1658 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
| 1659 | APSInt Result = Value.trunc(LHS.getBitWidth()); |
| 1660 | if (Result.extend(BitWidth) != Value) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1661 | if (Info.checkingForOverflow()) |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 1662 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
| 1663 | diag::warn_integer_constant_overflow) |
| 1664 | << Result.toString(10) << E->getType(); |
| 1665 | else |
| 1666 | HandleOverflow(Info, E, Value, E->getType()); |
| 1667 | } |
| 1668 | return Result; |
| 1669 | } |
| 1670 | |
| 1671 | /// Perform the given binary integer operation. |
| 1672 | static bool handleIntIntBinOp(EvalInfo &Info, const Expr *E, const APSInt &LHS, |
| 1673 | BinaryOperatorKind Opcode, APSInt RHS, |
| 1674 | APSInt &Result) { |
| 1675 | switch (Opcode) { |
| 1676 | default: |
| 1677 | Info.Diag(E); |
| 1678 | return false; |
| 1679 | case BO_Mul: |
| 1680 | Result = CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
| 1681 | std::multiplies<APSInt>()); |
| 1682 | return true; |
| 1683 | case BO_Add: |
| 1684 | Result = CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 1685 | std::plus<APSInt>()); |
| 1686 | return true; |
| 1687 | case BO_Sub: |
| 1688 | Result = CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 1689 | std::minus<APSInt>()); |
| 1690 | return true; |
| 1691 | case BO_And: Result = LHS & RHS; return true; |
| 1692 | case BO_Xor: Result = LHS ^ RHS; return true; |
| 1693 | case BO_Or: Result = LHS | RHS; return true; |
| 1694 | case BO_Div: |
| 1695 | case BO_Rem: |
| 1696 | if (RHS == 0) { |
| 1697 | Info.Diag(E, diag::note_expr_divide_by_zero); |
| 1698 | return false; |
| 1699 | } |
| 1700 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. |
| 1701 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 1702 | LHS.isSigned() && LHS.isMinSignedValue()) |
| 1703 | HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType()); |
| 1704 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
| 1705 | return true; |
| 1706 | case BO_Shl: { |
| 1707 | if (Info.getLangOpts().OpenCL) |
| 1708 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 1709 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 1710 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 1711 | RHS.isUnsigned()); |
| 1712 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 1713 | // During constant-folding, a negative shift is an opposite shift. Such |
| 1714 | // a shift is not a constant expression. |
| 1715 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 1716 | RHS = -RHS; |
| 1717 | goto shift_right; |
| 1718 | } |
| 1719 | shift_left: |
| 1720 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
| 1721 | // the shifted type. |
| 1722 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 1723 | if (SA != RHS) { |
| 1724 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 1725 | << RHS << E->getType() << LHS.getBitWidth(); |
| 1726 | } else if (LHS.isSigned()) { |
| 1727 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
| 1728 | // operand, and must not overflow the corresponding unsigned type. |
| 1729 | if (LHS.isNegative()) |
| 1730 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
| 1731 | else if (LHS.countLeadingZeros() < SA) |
| 1732 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
| 1733 | } |
| 1734 | Result = LHS << SA; |
| 1735 | return true; |
| 1736 | } |
| 1737 | case BO_Shr: { |
| 1738 | if (Info.getLangOpts().OpenCL) |
| 1739 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 1740 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 1741 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 1742 | RHS.isUnsigned()); |
| 1743 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 1744 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 1745 | // shift is not a constant expression. |
| 1746 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 1747 | RHS = -RHS; |
| 1748 | goto shift_left; |
| 1749 | } |
| 1750 | shift_right: |
| 1751 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 1752 | // shifted type. |
| 1753 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 1754 | if (SA != RHS) |
| 1755 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 1756 | << RHS << E->getType() << LHS.getBitWidth(); |
| 1757 | Result = LHS >> SA; |
| 1758 | return true; |
| 1759 | } |
| 1760 | |
| 1761 | case BO_LT: Result = LHS < RHS; return true; |
| 1762 | case BO_GT: Result = LHS > RHS; return true; |
| 1763 | case BO_LE: Result = LHS <= RHS; return true; |
| 1764 | case BO_GE: Result = LHS >= RHS; return true; |
| 1765 | case BO_EQ: Result = LHS == RHS; return true; |
| 1766 | case BO_NE: Result = LHS != RHS; return true; |
| 1767 | } |
| 1768 | } |
| 1769 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 1770 | /// Perform the given binary floating-point operation, in-place, on LHS. |
| 1771 | static bool handleFloatFloatBinOp(EvalInfo &Info, const Expr *E, |
| 1772 | APFloat &LHS, BinaryOperatorKind Opcode, |
| 1773 | const APFloat &RHS) { |
| 1774 | switch (Opcode) { |
| 1775 | default: |
| 1776 | Info.Diag(E); |
| 1777 | return false; |
| 1778 | case BO_Mul: |
| 1779 | LHS.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1780 | break; |
| 1781 | case BO_Add: |
| 1782 | LHS.add(RHS, APFloat::rmNearestTiesToEven); |
| 1783 | break; |
| 1784 | case BO_Sub: |
| 1785 | LHS.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1786 | break; |
| 1787 | case BO_Div: |
| 1788 | LHS.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1789 | break; |
| 1790 | } |
| 1791 | |
| 1792 | if (LHS.isInfinity() || LHS.isNaN()) |
| 1793 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
| 1794 | return true; |
| 1795 | } |
| 1796 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1797 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 1798 | /// truncating the lvalue's path to the given length. |
| 1799 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 1800 | const RecordDecl *TruncatedType, |
| 1801 | unsigned TruncatedElements) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1802 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1803 | |
| 1804 | // Check we actually point to a derived class object. |
| 1805 | if (TruncatedElements == D.Entries.size()) |
| 1806 | return true; |
| 1807 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 1808 | "not casting to a derived class"); |
| 1809 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 1810 | return false; |
| 1811 | |
| 1812 | // Truncate the path to the subobject, and remove any derived-to-base offsets. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1813 | const RecordDecl *RD = TruncatedType; |
| 1814 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1815 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1816 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1817 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1818 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1819 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1820 | else |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1821 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 1822 | RD = Base; |
| 1823 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1824 | D.Entries.resize(TruncatedElements); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1825 | return true; |
| 1826 | } |
| 1827 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1828 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1829 | const CXXRecordDecl *Derived, |
| 1830 | const CXXRecordDecl *Base, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1831 | const ASTRecordLayout *RL = nullptr) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1832 | if (!RL) { |
| 1833 | if (Derived->isInvalidDecl()) return false; |
| 1834 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 1835 | } |
| 1836 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1837 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1838 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1839 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1842 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1843 | const CXXRecordDecl *DerivedDecl, |
| 1844 | const CXXBaseSpecifier *Base) { |
| 1845 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 1846 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1847 | if (!Base->isVirtual()) |
| 1848 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1849 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1850 | SubobjectDesignator &D = Obj.Designator; |
| 1851 | if (D.Invalid) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1852 | return false; |
| 1853 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1854 | // Extract most-derived object and corresponding type. |
| 1855 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 1856 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 1857 | return false; |
| 1858 | |
| 1859 | // Find the virtual base class. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1860 | if (DerivedDecl->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1861 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 1862 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1863 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1864 | return true; |
| 1865 | } |
| 1866 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1867 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
| 1868 | QualType Type, LValue &Result) { |
| 1869 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 1870 | PathE = E->path_end(); |
| 1871 | PathI != PathE; ++PathI) { |
| 1872 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 1873 | *PathI)) |
| 1874 | return false; |
| 1875 | Type = (*PathI)->getType(); |
| 1876 | } |
| 1877 | return true; |
| 1878 | } |
| 1879 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1880 | /// Update LVal to refer to the given field, which must be a member of the type |
| 1881 | /// currently described by LVal. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1882 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1883 | const FieldDecl *FD, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1884 | const ASTRecordLayout *RL = nullptr) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1885 | if (!RL) { |
| 1886 | if (FD->getParent()->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1887 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1888 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1889 | |
| 1890 | unsigned I = FD->getFieldIndex(); |
| 1891 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1892 | LVal.addDecl(Info, E, FD); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1893 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1896 | /// Update LVal to refer to the given indirect field. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1897 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1898 | LValue &LVal, |
| 1899 | const IndirectFieldDecl *IFD) { |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 1900 | for (const auto *C : IFD->chain()) |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 1901 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(C))) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1902 | return false; |
| 1903 | return true; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1906 | /// Get the size of the given type in char units. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1907 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
| 1908 | QualType Type, CharUnits &Size) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1909 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1910 | // extension. |
| 1911 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 1912 | Size = CharUnits::One(); |
| 1913 | return true; |
| 1914 | } |
| 1915 | |
| 1916 | if (!Type->isConstantSizeType()) { |
| 1917 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1918 | // FIXME: Better diagnostic. |
| 1919 | Info.Diag(Loc); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1920 | return false; |
| 1921 | } |
| 1922 | |
| 1923 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 1924 | return true; |
| 1925 | } |
| 1926 | |
| 1927 | /// Update a pointer value to model pointer arithmetic. |
| 1928 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1929 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1930 | /// \param LVal - The pointer value to be updated. |
| 1931 | /// \param EltTy - The pointee type represented by LVal. |
| 1932 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1933 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 1934 | LValue &LVal, QualType EltTy, |
| 1935 | int64_t Adjustment) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1936 | CharUnits SizeOfPointee; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1937 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1938 | return false; |
| 1939 | |
| 1940 | // Compute the new offset in the appropriate width. |
| 1941 | LVal.Offset += Adjustment * SizeOfPointee; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1942 | LVal.adjustIndex(Info, E, Adjustment); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1943 | return true; |
| 1944 | } |
| 1945 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1946 | /// Update an lvalue to refer to a component of a complex number. |
| 1947 | /// \param Info - Information about the ongoing evaluation. |
| 1948 | /// \param LVal - The lvalue to be updated. |
| 1949 | /// \param EltTy - The complex number's component type. |
| 1950 | /// \param Imag - False for the real component, true for the imaginary. |
| 1951 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
| 1952 | LValue &LVal, QualType EltTy, |
| 1953 | bool Imag) { |
| 1954 | if (Imag) { |
| 1955 | CharUnits SizeOfComponent; |
| 1956 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
| 1957 | return false; |
| 1958 | LVal.Offset += SizeOfComponent; |
| 1959 | } |
| 1960 | LVal.addComplex(Info, E, EltTy, Imag); |
| 1961 | return true; |
| 1962 | } |
| 1963 | |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1964 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1965 | /// |
| 1966 | /// \param Info Information about the ongoing evaluation. |
| 1967 | /// \param E An expression to be used when printing diagnostics. |
| 1968 | /// \param VD The variable whose initializer should be obtained. |
| 1969 | /// \param Frame The frame in which the variable was created. Must be null |
| 1970 | /// if this variable is not local to the evaluation. |
| 1971 | /// \param Result Filled in with a pointer to the value of the variable. |
| 1972 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 1973 | const VarDecl *VD, CallStackFrame *Frame, |
| 1974 | APValue *&Result) { |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1975 | // If this is a parameter to an active constexpr function call, perform |
| 1976 | // argument substitution. |
| 1977 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1978 | // Assume arguments of a potential constant expression are unknown |
| 1979 | // constant expressions. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1980 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1981 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1982 | if (!Frame || !Frame->Arguments) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1983 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1984 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1985 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1986 | Result = &Frame->Arguments[PVD->getFunctionScopeIndex()]; |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1987 | return true; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1988 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1989 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1990 | // If this is a local variable, dig out its value. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1991 | if (Frame) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1992 | Result = Frame->getTemporary(VD); |
| 1993 | assert(Result && "missing value for local variable"); |
| 1994 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1997 | // Dig out the initializer, and use the declaration which it's attached to. |
| 1998 | const Expr *Init = VD->getAnyInitializer(VD); |
| 1999 | if (!Init || Init->isValueDependent()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2000 | // If we're checking a potential constant expression, the variable could be |
| 2001 | // initialized later. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2002 | if (!Info.checkingPotentialConstantExpression()) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2003 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2004 | return false; |
| 2005 | } |
| 2006 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2007 | // If we're currently evaluating the initializer of this declaration, use that |
| 2008 | // in-flight value. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2009 | if (Info.EvaluatingDecl.dyn_cast<const ValueDecl*>() == VD) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2010 | Result = Info.EvaluatingDeclValue; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2011 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2014 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 2015 | // this is the definition which will be used. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2016 | if (VD->isWeak()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2017 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2018 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2019 | } |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2020 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2021 | // Check that we can fold the initializer. In C++, we will have already done |
| 2022 | // this in the cases where it matters for conformance. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2023 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2024 | if (!VD->evaluateValue(Notes)) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2025 | Info.Diag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2026 | Notes.size() + 1) << VD; |
| 2027 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2028 | Info.addNotes(Notes); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2029 | return false; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2030 | } else if (!VD->checkInitIsICE()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2031 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2032 | Notes.size() + 1) << VD; |
| 2033 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2034 | Info.addNotes(Notes); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2035 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2036 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2037 | Result = VD->getEvaluatedValue(); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2038 | return true; |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2041 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2042 | Qualifiers Quals = T.getQualifiers(); |
| 2043 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 2044 | } |
| 2045 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2046 | /// Get the base index of the given base class within an APValue representing |
| 2047 | /// the given derived class. |
| 2048 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 2049 | const CXXRecordDecl *Base) { |
| 2050 | Base = Base->getCanonicalDecl(); |
| 2051 | unsigned Index = 0; |
| 2052 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 2053 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 2054 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 2055 | return Index; |
| 2056 | } |
| 2057 | |
| 2058 | llvm_unreachable("base class missing from derived class's bases list"); |
| 2059 | } |
| 2060 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2061 | /// Extract the value of a character from a string literal. |
| 2062 | static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
| 2063 | uint64_t Index) { |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2064 | // FIXME: Support ObjCEncodeExpr, MakeStringConstant |
| 2065 | if (auto PE = dyn_cast<PredefinedExpr>(Lit)) |
| 2066 | Lit = PE->getFunctionName(); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2067 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 2068 | const ConstantArrayType *CAT = |
| 2069 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 2070 | assert(CAT && "string literal isn't an array"); |
| 2071 | QualType CharType = CAT->getElementType(); |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 2072 | assert(CharType->isIntegerType() && "unexpected character type"); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2073 | |
| 2074 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 2075 | CharType->isUnsignedIntegerType()); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2076 | if (Index < S->getLength()) |
| 2077 | Value = S->getCodeUnit(Index); |
| 2078 | return Value; |
| 2079 | } |
| 2080 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2081 | // Expand a string literal into an array of characters. |
| 2082 | static void expandStringLiteral(EvalInfo &Info, const Expr *Lit, |
| 2083 | APValue &Result) { |
| 2084 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 2085 | const ConstantArrayType *CAT = |
| 2086 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 2087 | assert(CAT && "string literal isn't an array"); |
| 2088 | QualType CharType = CAT->getElementType(); |
| 2089 | assert(CharType->isIntegerType() && "unexpected character type"); |
| 2090 | |
| 2091 | unsigned Elts = CAT->getSize().getZExtValue(); |
| 2092 | Result = APValue(APValue::UninitArray(), |
| 2093 | std::min(S->getLength(), Elts), Elts); |
| 2094 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 2095 | CharType->isUnsignedIntegerType()); |
| 2096 | if (Result.hasArrayFiller()) |
| 2097 | Result.getArrayFiller() = APValue(Value); |
| 2098 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
| 2099 | Value = S->getCodeUnit(I); |
| 2100 | Result.getArrayInitializedElt(I) = APValue(Value); |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | // Expand an array so that it has more than Index filled elements. |
| 2105 | static void expandArray(APValue &Array, unsigned Index) { |
| 2106 | unsigned Size = Array.getArraySize(); |
| 2107 | assert(Index < Size); |
| 2108 | |
| 2109 | // Always at least double the number of elements for which we store a value. |
| 2110 | unsigned OldElts = Array.getArrayInitializedElts(); |
| 2111 | unsigned NewElts = std::max(Index+1, OldElts * 2); |
| 2112 | NewElts = std::min(Size, std::max(NewElts, 8u)); |
| 2113 | |
| 2114 | // Copy the data across. |
| 2115 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
| 2116 | for (unsigned I = 0; I != OldElts; ++I) |
| 2117 | NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I)); |
| 2118 | for (unsigned I = OldElts; I != NewElts; ++I) |
| 2119 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
| 2120 | if (NewValue.hasArrayFiller()) |
| 2121 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
| 2122 | Array.swap(NewValue); |
| 2123 | } |
| 2124 | |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2125 | /// Determine whether a type would actually be read by an lvalue-to-rvalue |
| 2126 | /// conversion. If it's of class type, we may assume that the copy operation |
| 2127 | /// is trivial. Note that this is never true for a union type with fields |
| 2128 | /// (because the copy always "reads" the active member) and always true for |
| 2129 | /// a non-class type. |
| 2130 | static bool isReadByLvalueToRvalueConversion(QualType T) { |
| 2131 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 2132 | if (!RD || (RD->isUnion() && !RD->field_empty())) |
| 2133 | return true; |
| 2134 | if (RD->isEmpty()) |
| 2135 | return false; |
| 2136 | |
| 2137 | for (auto *Field : RD->fields()) |
| 2138 | if (isReadByLvalueToRvalueConversion(Field->getType())) |
| 2139 | return true; |
| 2140 | |
| 2141 | for (auto &BaseSpec : RD->bases()) |
| 2142 | if (isReadByLvalueToRvalueConversion(BaseSpec.getType())) |
| 2143 | return true; |
| 2144 | |
| 2145 | return false; |
| 2146 | } |
| 2147 | |
| 2148 | /// Diagnose an attempt to read from any unreadable field within the specified |
| 2149 | /// type, which might be a class type. |
| 2150 | static bool diagnoseUnreadableFields(EvalInfo &Info, const Expr *E, |
| 2151 | QualType T) { |
| 2152 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 2153 | if (!RD) |
| 2154 | return false; |
| 2155 | |
| 2156 | if (!RD->hasMutableFields()) |
| 2157 | return false; |
| 2158 | |
| 2159 | for (auto *Field : RD->fields()) { |
| 2160 | // If we're actually going to read this field in some way, then it can't |
| 2161 | // be mutable. If we're in a union, then assigning to a mutable field |
| 2162 | // (even an empty one) can change the active member, so that's not OK. |
| 2163 | // FIXME: Add core issue number for the union case. |
| 2164 | if (Field->isMutable() && |
| 2165 | (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) { |
| 2166 | Info.Diag(E, diag::note_constexpr_ltor_mutable, 1) << Field; |
| 2167 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 2168 | return true; |
| 2169 | } |
| 2170 | |
| 2171 | if (diagnoseUnreadableFields(Info, E, Field->getType())) |
| 2172 | return true; |
| 2173 | } |
| 2174 | |
| 2175 | for (auto &BaseSpec : RD->bases()) |
| 2176 | if (diagnoseUnreadableFields(Info, E, BaseSpec.getType())) |
| 2177 | return true; |
| 2178 | |
| 2179 | // All mutable fields were empty, and thus not actually read. |
| 2180 | return false; |
| 2181 | } |
| 2182 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2183 | /// Kinds of access we can perform on an object, for diagnostics. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2184 | enum AccessKinds { |
| 2185 | AK_Read, |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2186 | AK_Assign, |
| 2187 | AK_Increment, |
| 2188 | AK_Decrement |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2189 | }; |
| 2190 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2191 | /// A handle to a complete object (an object that is not a subobject of |
| 2192 | /// another object). |
| 2193 | struct CompleteObject { |
| 2194 | /// The value of the complete object. |
| 2195 | APValue *Value; |
| 2196 | /// The type of the complete object. |
| 2197 | QualType Type; |
| 2198 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2199 | CompleteObject() : Value(nullptr) {} |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2200 | CompleteObject(APValue *Value, QualType Type) |
| 2201 | : Value(Value), Type(Type) { |
| 2202 | assert(Value && "missing value for complete object"); |
| 2203 | } |
| 2204 | |
Aaron Ballman | 6734766 | 2015-02-15 22:00:28 +0000 | [diff] [blame] | 2205 | explicit operator bool() const { return Value; } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2206 | }; |
| 2207 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2208 | /// Find the designated sub-object of an rvalue. |
| 2209 | template<typename SubobjectHandler> |
| 2210 | typename SubobjectHandler::result_type |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2211 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2212 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2213 | if (Sub.Invalid) |
| 2214 | // A diagnostic will have already been produced. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2215 | return handler.failed(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2216 | if (Sub.isOnePastTheEnd()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2217 | if (Info.getLangOpts().CPlusPlus11) |
| 2218 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 2219 | << handler.AccessKind; |
| 2220 | else |
| 2221 | Info.Diag(E); |
| 2222 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2223 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2224 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2225 | APValue *O = Obj.Value; |
| 2226 | QualType ObjType = Obj.Type; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2227 | const FieldDecl *LastField = nullptr; |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2228 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2229 | // Walk the designator's path to find the subobject. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2230 | for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) { |
| 2231 | if (O->isUninit()) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2232 | if (!Info.checkingPotentialConstantExpression()) |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2233 | Info.Diag(E, diag::note_constexpr_access_uninit) << handler.AccessKind; |
| 2234 | return handler.failed(); |
| 2235 | } |
| 2236 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2237 | if (I == N) { |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2238 | // If we are reading an object of class type, there may still be more |
| 2239 | // things we need to check: if there are any mutable subobjects, we |
| 2240 | // cannot perform this read. (This only happens when performing a trivial |
| 2241 | // copy or assignment.) |
| 2242 | if (ObjType->isRecordType() && handler.AccessKind == AK_Read && |
| 2243 | diagnoseUnreadableFields(Info, E, ObjType)) |
| 2244 | return handler.failed(); |
| 2245 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2246 | if (!handler.found(*O, ObjType)) |
| 2247 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2248 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2249 | // If we modified a bit-field, truncate it to the right width. |
| 2250 | if (handler.AccessKind != AK_Read && |
| 2251 | LastField && LastField->isBitField() && |
| 2252 | !truncateBitfieldValue(Info, E, *O, LastField)) |
| 2253 | return false; |
| 2254 | |
| 2255 | return true; |
| 2256 | } |
| 2257 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2258 | LastField = nullptr; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2259 | if (ObjType->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2260 | // Next subobject is an array element. |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2261 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2262 | assert(CAT && "vla in literal type?"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2263 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2264 | if (CAT->getSize().ule(Index)) { |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2265 | // Note, it should not be possible to form a pointer with a valid |
| 2266 | // designator which points more than one past the end of the array. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2267 | if (Info.getLangOpts().CPlusPlus11) |
| 2268 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 2269 | << handler.AccessKind; |
| 2270 | else |
| 2271 | Info.Diag(E); |
| 2272 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2273 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2274 | |
| 2275 | ObjType = CAT->getElementType(); |
| 2276 | |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2277 | // An array object is represented as either an Array APValue or as an |
| 2278 | // LValue which refers to a string literal. |
| 2279 | if (O->isLValue()) { |
| 2280 | assert(I == N - 1 && "extracting subobject of character?"); |
| 2281 | assert(!O->hasLValuePath() || O->getLValuePath().empty()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2282 | if (handler.AccessKind != AK_Read) |
| 2283 | expandStringLiteral(Info, O->getLValueBase().get<const Expr *>(), |
| 2284 | *O); |
| 2285 | else |
| 2286 | return handler.foundString(*O, ObjType, Index); |
| 2287 | } |
| 2288 | |
| 2289 | if (O->getArrayInitializedElts() > Index) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2290 | O = &O->getArrayInitializedElt(Index); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2291 | else if (handler.AccessKind != AK_Read) { |
| 2292 | expandArray(*O, Index); |
| 2293 | O = &O->getArrayInitializedElt(Index); |
| 2294 | } else |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2295 | O = &O->getArrayFiller(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2296 | } else if (ObjType->isAnyComplexType()) { |
| 2297 | // Next subobject is a complex number. |
| 2298 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 2299 | if (Index > 1) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2300 | if (Info.getLangOpts().CPlusPlus11) |
| 2301 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 2302 | << handler.AccessKind; |
| 2303 | else |
| 2304 | Info.Diag(E); |
| 2305 | return handler.failed(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2306 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2307 | |
| 2308 | bool WasConstQualified = ObjType.isConstQualified(); |
| 2309 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 2310 | if (WasConstQualified) |
| 2311 | ObjType.addConst(); |
| 2312 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2313 | assert(I == N - 1 && "extracting subobject of scalar?"); |
| 2314 | if (O->isComplexInt()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2315 | return handler.found(Index ? O->getComplexIntImag() |
| 2316 | : O->getComplexIntReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2317 | } else { |
| 2318 | assert(O->isComplexFloat()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2319 | return handler.found(Index ? O->getComplexFloatImag() |
| 2320 | : O->getComplexFloatReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2321 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2322 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2323 | if (Field->isMutable() && handler.AccessKind == AK_Read) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2324 | Info.Diag(E, diag::note_constexpr_ltor_mutable, 1) |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 2325 | << Field; |
| 2326 | Info.Note(Field->getLocation(), diag::note_declared_at); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2327 | return handler.failed(); |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2330 | // Next subobject is a class, struct or union field. |
| 2331 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 2332 | if (RD->isUnion()) { |
| 2333 | const FieldDecl *UnionField = O->getUnionField(); |
| 2334 | if (!UnionField || |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2335 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2336 | Info.Diag(E, diag::note_constexpr_access_inactive_union_member) |
| 2337 | << handler.AccessKind << Field << !UnionField << UnionField; |
| 2338 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2339 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2340 | O = &O->getUnionValue(); |
| 2341 | } else |
| 2342 | O = &O->getStructField(Field->getFieldIndex()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2343 | |
| 2344 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2345 | ObjType = Field->getType(); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2346 | if (WasConstQualified && !Field->isMutable()) |
| 2347 | ObjType.addConst(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2348 | |
| 2349 | if (ObjType.isVolatileQualified()) { |
| 2350 | if (Info.getLangOpts().CPlusPlus) { |
| 2351 | // FIXME: Include a description of the path to the volatile subobject. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2352 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2353 | << handler.AccessKind << 2 << Field; |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2354 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 2355 | } else { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2356 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2357 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2358 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2359 | } |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2360 | |
| 2361 | LastField = Field; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2362 | } else { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2363 | // Next subobject is a base class. |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2364 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 2365 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 2366 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2367 | |
| 2368 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2369 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2370 | if (WasConstQualified) |
| 2371 | ObjType.addConst(); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2372 | } |
| 2373 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 2376 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2377 | struct ExtractSubobjectHandler { |
| 2378 | EvalInfo &Info; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2379 | APValue &Result; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2380 | |
| 2381 | static const AccessKinds AccessKind = AK_Read; |
| 2382 | |
| 2383 | typedef bool result_type; |
| 2384 | bool failed() { return false; } |
| 2385 | bool found(APValue &Subobj, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2386 | Result = Subobj; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2387 | return true; |
| 2388 | } |
| 2389 | bool found(APSInt &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2390 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2391 | return true; |
| 2392 | } |
| 2393 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2394 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2395 | return true; |
| 2396 | } |
| 2397 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2398 | Result = APValue(extractStringLiteralCharacter( |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2399 | Info, Subobj.getLValueBase().get<const Expr *>(), Character)); |
| 2400 | return true; |
| 2401 | } |
| 2402 | }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2403 | } // end anonymous namespace |
| 2404 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2405 | const AccessKinds ExtractSubobjectHandler::AccessKind; |
| 2406 | |
| 2407 | /// Extract the designated sub-object of an rvalue. |
| 2408 | static bool extractSubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2409 | const CompleteObject &Obj, |
| 2410 | const SubobjectDesignator &Sub, |
| 2411 | APValue &Result) { |
| 2412 | ExtractSubobjectHandler Handler = { Info, Result }; |
| 2413 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2416 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2417 | struct ModifySubobjectHandler { |
| 2418 | EvalInfo &Info; |
| 2419 | APValue &NewVal; |
| 2420 | const Expr *E; |
| 2421 | |
| 2422 | typedef bool result_type; |
| 2423 | static const AccessKinds AccessKind = AK_Assign; |
| 2424 | |
| 2425 | bool checkConst(QualType QT) { |
| 2426 | // Assigning to a const object has undefined behavior. |
| 2427 | if (QT.isConstQualified()) { |
| 2428 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2429 | return false; |
| 2430 | } |
| 2431 | return true; |
| 2432 | } |
| 2433 | |
| 2434 | bool failed() { return false; } |
| 2435 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2436 | if (!checkConst(SubobjType)) |
| 2437 | return false; |
| 2438 | // We've been given ownership of NewVal, so just swap it in. |
| 2439 | Subobj.swap(NewVal); |
| 2440 | return true; |
| 2441 | } |
| 2442 | bool found(APSInt &Value, QualType SubobjType) { |
| 2443 | if (!checkConst(SubobjType)) |
| 2444 | return false; |
| 2445 | if (!NewVal.isInt()) { |
| 2446 | // Maybe trying to write a cast pointer value into a complex? |
| 2447 | Info.Diag(E); |
| 2448 | return false; |
| 2449 | } |
| 2450 | Value = NewVal.getInt(); |
| 2451 | return true; |
| 2452 | } |
| 2453 | bool found(APFloat &Value, QualType SubobjType) { |
| 2454 | if (!checkConst(SubobjType)) |
| 2455 | return false; |
| 2456 | Value = NewVal.getFloat(); |
| 2457 | return true; |
| 2458 | } |
| 2459 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 2460 | llvm_unreachable("shouldn't encounter string elements with ExpandArrays"); |
| 2461 | } |
| 2462 | }; |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 2463 | } // end anonymous namespace |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2464 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2465 | const AccessKinds ModifySubobjectHandler::AccessKind; |
| 2466 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2467 | /// Update the designated sub-object of an rvalue to the given value. |
| 2468 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2469 | const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2470 | const SubobjectDesignator &Sub, |
| 2471 | APValue &NewVal) { |
| 2472 | ModifySubobjectHandler Handler = { Info, NewVal, E }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2473 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2476 | /// Find the position where two subobject designators diverge, or equivalently |
| 2477 | /// the length of the common initial subsequence. |
| 2478 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 2479 | const SubobjectDesignator &A, |
| 2480 | const SubobjectDesignator &B, |
| 2481 | bool &WasArrayIndex) { |
| 2482 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 2483 | for (/**/; I != N; ++I) { |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2484 | if (!ObjType.isNull() && |
| 2485 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2486 | // Next subobject is an array element. |
| 2487 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
| 2488 | WasArrayIndex = true; |
| 2489 | return I; |
| 2490 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2491 | if (ObjType->isAnyComplexType()) |
| 2492 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 2493 | else |
| 2494 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2495 | } else { |
| 2496 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
| 2497 | WasArrayIndex = false; |
| 2498 | return I; |
| 2499 | } |
| 2500 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 2501 | // Next subobject is a field. |
| 2502 | ObjType = FD->getType(); |
| 2503 | else |
| 2504 | // Next subobject is a base class. |
| 2505 | ObjType = QualType(); |
| 2506 | } |
| 2507 | } |
| 2508 | WasArrayIndex = false; |
| 2509 | return I; |
| 2510 | } |
| 2511 | |
| 2512 | /// Determine whether the given subobject designators refer to elements of the |
| 2513 | /// same array object. |
| 2514 | static bool AreElementsOfSameArray(QualType ObjType, |
| 2515 | const SubobjectDesignator &A, |
| 2516 | const SubobjectDesignator &B) { |
| 2517 | if (A.Entries.size() != B.Entries.size()) |
| 2518 | return false; |
| 2519 | |
| 2520 | bool IsArray = A.MostDerivedArraySize != 0; |
| 2521 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 2522 | // A is a subobject of the array element. |
| 2523 | return false; |
| 2524 | |
| 2525 | // If A (and B) designates an array element, the last entry will be the array |
| 2526 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 2527 | // of length 1' case, and the entire path must match. |
| 2528 | bool WasArrayIndex; |
| 2529 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 2530 | return CommonLength >= A.Entries.size() - IsArray; |
| 2531 | } |
| 2532 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2533 | /// Find the complete object to which an LValue refers. |
Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 2534 | static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, |
| 2535 | AccessKinds AK, const LValue &LVal, |
| 2536 | QualType LValType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2537 | if (!LVal.Base) { |
| 2538 | Info.Diag(E, diag::note_constexpr_access_null) << AK; |
| 2539 | return CompleteObject(); |
| 2540 | } |
| 2541 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2542 | CallStackFrame *Frame = nullptr; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2543 | if (LVal.CallIndex) { |
| 2544 | Frame = Info.getCallFrame(LVal.CallIndex); |
| 2545 | if (!Frame) { |
| 2546 | Info.Diag(E, diag::note_constexpr_lifetime_ended, 1) |
| 2547 | << AK << LVal.Base.is<const ValueDecl*>(); |
| 2548 | NoteLValueLocation(Info, LVal.Base); |
| 2549 | return CompleteObject(); |
| 2550 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 2554 | // is not a constant expression (even if the object is non-volatile). We also |
| 2555 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 2556 | // semantics. |
| 2557 | if (LValType.isVolatileQualified()) { |
| 2558 | if (Info.getLangOpts().CPlusPlus) |
| 2559 | Info.Diag(E, diag::note_constexpr_access_volatile_type) |
| 2560 | << AK << LValType; |
| 2561 | else |
| 2562 | Info.Diag(E); |
| 2563 | return CompleteObject(); |
| 2564 | } |
| 2565 | |
| 2566 | // Compute value storage location and type of base object. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2567 | APValue *BaseVal = nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2568 | QualType BaseType = getType(LVal.Base); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2569 | |
| 2570 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
| 2571 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 2572 | // In C++11, constexpr, non-volatile variables initialized with constant |
| 2573 | // expressions are constant expressions too. Inside constexpr functions, |
| 2574 | // parameters are constant expressions even if they're non-const. |
| 2575 | // In C++1y, objects local to a constant expression (those with a Frame) are |
| 2576 | // both readable and writable inside constant expressions. |
| 2577 | // In C, such things can also be folded, although they are not ICEs. |
| 2578 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
| 2579 | if (VD) { |
| 2580 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
| 2581 | VD = VDef; |
| 2582 | } |
| 2583 | if (!VD || VD->isInvalidDecl()) { |
| 2584 | Info.Diag(E); |
| 2585 | return CompleteObject(); |
| 2586 | } |
| 2587 | |
| 2588 | // Accesses of volatile-qualified objects are not allowed. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2589 | if (BaseType.isVolatileQualified()) { |
| 2590 | if (Info.getLangOpts().CPlusPlus) { |
| 2591 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2592 | << AK << 1 << VD; |
| 2593 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2594 | } else { |
| 2595 | Info.Diag(E); |
| 2596 | } |
| 2597 | return CompleteObject(); |
| 2598 | } |
| 2599 | |
| 2600 | // Unless we're looking at a local variable or argument in a constexpr call, |
| 2601 | // the variable we're reading must be const. |
| 2602 | if (!Frame) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2603 | if (Info.getLangOpts().CPlusPlus14 && |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2604 | VD == Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()) { |
| 2605 | // OK, we can read and modify an object if we're in the process of |
| 2606 | // evaluating its initializer, because its lifetime began in this |
| 2607 | // evaluation. |
| 2608 | } else if (AK != AK_Read) { |
| 2609 | // All the remaining cases only permit reading. |
| 2610 | Info.Diag(E, diag::note_constexpr_modify_global); |
| 2611 | return CompleteObject(); |
| 2612 | } else if (VD->isConstexpr()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2613 | // OK, we can read this variable. |
| 2614 | } else if (BaseType->isIntegralOrEnumerationType()) { |
| 2615 | if (!BaseType.isConstQualified()) { |
| 2616 | if (Info.getLangOpts().CPlusPlus) { |
| 2617 | Info.Diag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
| 2618 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2619 | } else { |
| 2620 | Info.Diag(E); |
| 2621 | } |
| 2622 | return CompleteObject(); |
| 2623 | } |
| 2624 | } else if (BaseType->isFloatingType() && BaseType.isConstQualified()) { |
| 2625 | // We support folding of const floating-point types, in order to make |
| 2626 | // static const data members of such types (supported as an extension) |
| 2627 | // more useful. |
| 2628 | if (Info.getLangOpts().CPlusPlus11) { |
| 2629 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 2630 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2631 | } else { |
| 2632 | Info.CCEDiag(E); |
| 2633 | } |
| 2634 | } else { |
| 2635 | // FIXME: Allow folding of values of any literal type in all languages. |
| 2636 | if (Info.getLangOpts().CPlusPlus11) { |
| 2637 | Info.Diag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 2638 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2639 | } else { |
| 2640 | Info.Diag(E); |
| 2641 | } |
| 2642 | return CompleteObject(); |
| 2643 | } |
| 2644 | } |
| 2645 | |
| 2646 | if (!evaluateVarDeclInit(Info, E, VD, Frame, BaseVal)) |
| 2647 | return CompleteObject(); |
| 2648 | } else { |
| 2649 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
| 2650 | |
| 2651 | if (!Frame) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2652 | if (const MaterializeTemporaryExpr *MTE = |
| 2653 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 2654 | assert(MTE->getStorageDuration() == SD_Static && |
| 2655 | "should have a frame for a non-global materialized temporary"); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2656 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2657 | // Per C++1y [expr.const]p2: |
| 2658 | // an lvalue-to-rvalue conversion [is not allowed unless it applies to] |
| 2659 | // - a [...] glvalue of integral or enumeration type that refers to |
| 2660 | // a non-volatile const object [...] |
| 2661 | // [...] |
| 2662 | // - a [...] glvalue of literal type that refers to a non-volatile |
| 2663 | // object whose lifetime began within the evaluation of e. |
| 2664 | // |
| 2665 | // C++11 misses the 'began within the evaluation of e' check and |
| 2666 | // instead allows all temporaries, including things like: |
| 2667 | // int &&r = 1; |
| 2668 | // int x = ++r; |
| 2669 | // constexpr int k = r; |
| 2670 | // Therefore we use the C++1y rules in C++11 too. |
| 2671 | const ValueDecl *VD = Info.EvaluatingDecl.dyn_cast<const ValueDecl*>(); |
| 2672 | const ValueDecl *ED = MTE->getExtendingDecl(); |
| 2673 | if (!(BaseType.isConstQualified() && |
| 2674 | BaseType->isIntegralOrEnumerationType()) && |
| 2675 | !(VD && VD->getCanonicalDecl() == ED->getCanonicalDecl())) { |
| 2676 | Info.Diag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
| 2677 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
| 2678 | return CompleteObject(); |
| 2679 | } |
| 2680 | |
| 2681 | BaseVal = Info.Ctx.getMaterializedTemporaryValue(MTE, false); |
| 2682 | assert(BaseVal && "got reference to unevaluated temporary"); |
| 2683 | } else { |
| 2684 | Info.Diag(E); |
| 2685 | return CompleteObject(); |
| 2686 | } |
| 2687 | } else { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2688 | BaseVal = Frame->getTemporary(Base); |
| 2689 | assert(BaseVal && "missing value for temporary"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2690 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2691 | |
| 2692 | // Volatile temporary objects cannot be accessed in constant expressions. |
| 2693 | if (BaseType.isVolatileQualified()) { |
| 2694 | if (Info.getLangOpts().CPlusPlus) { |
| 2695 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2696 | << AK << 0; |
| 2697 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 2698 | } else { |
| 2699 | Info.Diag(E); |
| 2700 | } |
| 2701 | return CompleteObject(); |
| 2702 | } |
| 2703 | } |
| 2704 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2705 | // During the construction of an object, it is not yet 'const'. |
| 2706 | // FIXME: We don't set up EvaluatingDecl for local variables or temporaries, |
| 2707 | // and this doesn't do quite the right thing for const subobjects of the |
| 2708 | // object under construction. |
| 2709 | if (LVal.getLValueBase() == Info.EvaluatingDecl) { |
| 2710 | BaseType = Info.Ctx.getCanonicalType(BaseType); |
| 2711 | BaseType.removeLocalConst(); |
| 2712 | } |
| 2713 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2714 | // In C++1y, we can't safely access any mutable state when we might be |
| 2715 | // evaluating after an unmodeled side effect or an evaluation failure. |
| 2716 | // |
| 2717 | // FIXME: Not all local state is mutable. Allow local constant subobjects |
| 2718 | // to be read here (but take care with 'mutable' fields). |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2719 | if (Frame && Info.getLangOpts().CPlusPlus14 && |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2720 | (Info.EvalStatus.HasSideEffects || Info.keepEvaluatingAfterFailure())) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2721 | return CompleteObject(); |
| 2722 | |
| 2723 | return CompleteObject(BaseVal, BaseType); |
| 2724 | } |
| 2725 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2726 | /// \brief Perform an lvalue-to-rvalue conversion on the given glvalue. This |
| 2727 | /// can also be used for 'lvalue-to-lvalue' conversions for looking up the |
| 2728 | /// glvalue referred to by an entity of reference type. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2729 | /// |
| 2730 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2731 | /// \param Conv - The expression for which we are performing the conversion. |
| 2732 | /// Used for diagnostics. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2733 | /// \param Type - The type of the glvalue (before stripping cv-qualifiers in the |
| 2734 | /// case of a non-class type). |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2735 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 2736 | /// \param RVal - The produced value will be placed here. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2737 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2738 | QualType Type, |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2739 | const LValue &LVal, APValue &RVal) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2740 | if (LVal.Designator.Invalid) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2741 | return false; |
| 2742 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2743 | // Check for special cases where there is no existing APValue to look at. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2744 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 2745 | if (Base && !LVal.CallIndex && !Type.isVolatileQualified()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2746 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 2747 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 2748 | // initializer until now for such expressions. Such an expression can't be |
| 2749 | // an ICE in C, so this only matters for fold. |
| 2750 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 2751 | if (Type.isVolatileQualified()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2752 | Info.Diag(Conv); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2753 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2754 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2755 | APValue Lit; |
| 2756 | if (!Evaluate(Lit, Info, CLE->getInitializer())) |
| 2757 | return false; |
| 2758 | CompleteObject LitObj(&Lit, Base->getType()); |
| 2759 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal); |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2760 | } else if (isa<StringLiteral>(Base) || isa<PredefinedExpr>(Base)) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2761 | // We represent a string literal array as an lvalue pointing at the |
| 2762 | // corresponding expression, rather than building an array of chars. |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2763 | // FIXME: Support ObjCEncodeExpr, MakeStringConstant |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2764 | APValue Str(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0); |
| 2765 | CompleteObject StrObj(&Str, Base->getType()); |
| 2766 | return extractSubobject(Info, Conv, StrObj, LVal.Designator, RVal); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2767 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2770 | CompleteObject Obj = findCompleteObject(Info, Conv, AK_Read, LVal, Type); |
| 2771 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | /// Perform an assignment of Val to LVal. Takes ownership of Val. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2775 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2776 | QualType LValType, APValue &Val) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2777 | if (LVal.Designator.Invalid) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2778 | return false; |
| 2779 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2780 | if (!Info.getLangOpts().CPlusPlus14) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2781 | Info.Diag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2782 | return false; |
| 2783 | } |
| 2784 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2785 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 2786 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2789 | static bool isOverflowingIntegerType(ASTContext &Ctx, QualType T) { |
| 2790 | return T->isSignedIntegerType() && |
| 2791 | Ctx.getIntWidth(T) >= Ctx.getIntWidth(Ctx.IntTy); |
| 2792 | } |
| 2793 | |
| 2794 | namespace { |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2795 | struct CompoundAssignSubobjectHandler { |
| 2796 | EvalInfo &Info; |
| 2797 | const Expr *E; |
| 2798 | QualType PromotedLHSType; |
| 2799 | BinaryOperatorKind Opcode; |
| 2800 | const APValue &RHS; |
| 2801 | |
| 2802 | static const AccessKinds AccessKind = AK_Assign; |
| 2803 | |
| 2804 | typedef bool result_type; |
| 2805 | |
| 2806 | bool checkConst(QualType QT) { |
| 2807 | // Assigning to a const object has undefined behavior. |
| 2808 | if (QT.isConstQualified()) { |
| 2809 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2810 | return false; |
| 2811 | } |
| 2812 | return true; |
| 2813 | } |
| 2814 | |
| 2815 | bool failed() { return false; } |
| 2816 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2817 | switch (Subobj.getKind()) { |
| 2818 | case APValue::Int: |
| 2819 | return found(Subobj.getInt(), SubobjType); |
| 2820 | case APValue::Float: |
| 2821 | return found(Subobj.getFloat(), SubobjType); |
| 2822 | case APValue::ComplexInt: |
| 2823 | case APValue::ComplexFloat: |
| 2824 | // FIXME: Implement complex compound assignment. |
| 2825 | Info.Diag(E); |
| 2826 | return false; |
| 2827 | case APValue::LValue: |
| 2828 | return foundPointer(Subobj, SubobjType); |
| 2829 | default: |
| 2830 | // FIXME: can this happen? |
| 2831 | Info.Diag(E); |
| 2832 | return false; |
| 2833 | } |
| 2834 | } |
| 2835 | bool found(APSInt &Value, QualType SubobjType) { |
| 2836 | if (!checkConst(SubobjType)) |
| 2837 | return false; |
| 2838 | |
| 2839 | if (!SubobjType->isIntegerType() || !RHS.isInt()) { |
| 2840 | // We don't support compound assignment on integer-cast-to-pointer |
| 2841 | // values. |
| 2842 | Info.Diag(E); |
| 2843 | return false; |
| 2844 | } |
| 2845 | |
| 2846 | APSInt LHS = HandleIntToIntCast(Info, E, PromotedLHSType, |
| 2847 | SubobjType, Value); |
| 2848 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
| 2849 | return false; |
| 2850 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
| 2851 | return true; |
| 2852 | } |
| 2853 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2854 | return checkConst(SubobjType) && |
| 2855 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
| 2856 | Value) && |
| 2857 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
| 2858 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2859 | } |
| 2860 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 2861 | if (!checkConst(SubobjType)) |
| 2862 | return false; |
| 2863 | |
| 2864 | QualType PointeeType; |
| 2865 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 2866 | PointeeType = PT->getPointeeType(); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2867 | |
| 2868 | if (PointeeType.isNull() || !RHS.isInt() || |
| 2869 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2870 | Info.Diag(E); |
| 2871 | return false; |
| 2872 | } |
| 2873 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2874 | int64_t Offset = getExtValue(RHS.getInt()); |
| 2875 | if (Opcode == BO_Sub) |
| 2876 | Offset = -Offset; |
| 2877 | |
| 2878 | LValue LVal; |
| 2879 | LVal.setFrom(Info.Ctx, Subobj); |
| 2880 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
| 2881 | return false; |
| 2882 | LVal.moveInto(Subobj); |
| 2883 | return true; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2884 | } |
| 2885 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 2886 | llvm_unreachable("shouldn't encounter string elements here"); |
| 2887 | } |
| 2888 | }; |
| 2889 | } // end anonymous namespace |
| 2890 | |
| 2891 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
| 2892 | |
| 2893 | /// Perform a compound assignment of LVal <op>= RVal. |
| 2894 | static bool handleCompoundAssignment( |
| 2895 | EvalInfo &Info, const Expr *E, |
| 2896 | const LValue &LVal, QualType LValType, QualType PromotedLValType, |
| 2897 | BinaryOperatorKind Opcode, const APValue &RVal) { |
| 2898 | if (LVal.Designator.Invalid) |
| 2899 | return false; |
| 2900 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2901 | if (!Info.getLangOpts().CPlusPlus14) { |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2902 | Info.Diag(E); |
| 2903 | return false; |
| 2904 | } |
| 2905 | |
| 2906 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 2907 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
| 2908 | RVal }; |
| 2909 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 2910 | } |
| 2911 | |
| 2912 | namespace { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2913 | struct IncDecSubobjectHandler { |
| 2914 | EvalInfo &Info; |
| 2915 | const Expr *E; |
| 2916 | AccessKinds AccessKind; |
| 2917 | APValue *Old; |
| 2918 | |
| 2919 | typedef bool result_type; |
| 2920 | |
| 2921 | bool checkConst(QualType QT) { |
| 2922 | // Assigning to a const object has undefined behavior. |
| 2923 | if (QT.isConstQualified()) { |
| 2924 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2925 | return false; |
| 2926 | } |
| 2927 | return true; |
| 2928 | } |
| 2929 | |
| 2930 | bool failed() { return false; } |
| 2931 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2932 | // Stash the old value. Also clear Old, so we don't clobber it later |
| 2933 | // if we're post-incrementing a complex. |
| 2934 | if (Old) { |
| 2935 | *Old = Subobj; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2936 | Old = nullptr; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | switch (Subobj.getKind()) { |
| 2940 | case APValue::Int: |
| 2941 | return found(Subobj.getInt(), SubobjType); |
| 2942 | case APValue::Float: |
| 2943 | return found(Subobj.getFloat(), SubobjType); |
| 2944 | case APValue::ComplexInt: |
| 2945 | return found(Subobj.getComplexIntReal(), |
| 2946 | SubobjType->castAs<ComplexType>()->getElementType() |
| 2947 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 2948 | case APValue::ComplexFloat: |
| 2949 | return found(Subobj.getComplexFloatReal(), |
| 2950 | SubobjType->castAs<ComplexType>()->getElementType() |
| 2951 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 2952 | case APValue::LValue: |
| 2953 | return foundPointer(Subobj, SubobjType); |
| 2954 | default: |
| 2955 | // FIXME: can this happen? |
| 2956 | Info.Diag(E); |
| 2957 | return false; |
| 2958 | } |
| 2959 | } |
| 2960 | bool found(APSInt &Value, QualType SubobjType) { |
| 2961 | if (!checkConst(SubobjType)) |
| 2962 | return false; |
| 2963 | |
| 2964 | if (!SubobjType->isIntegerType()) { |
| 2965 | // We don't support increment / decrement on integer-cast-to-pointer |
| 2966 | // values. |
| 2967 | Info.Diag(E); |
| 2968 | return false; |
| 2969 | } |
| 2970 | |
| 2971 | if (Old) *Old = APValue(Value); |
| 2972 | |
| 2973 | // bool arithmetic promotes to int, and the conversion back to bool |
| 2974 | // doesn't reduce mod 2^n, so special-case it. |
| 2975 | if (SubobjType->isBooleanType()) { |
| 2976 | if (AccessKind == AK_Increment) |
| 2977 | Value = 1; |
| 2978 | else |
| 2979 | Value = !Value; |
| 2980 | return true; |
| 2981 | } |
| 2982 | |
| 2983 | bool WasNegative = Value.isNegative(); |
| 2984 | if (AccessKind == AK_Increment) { |
| 2985 | ++Value; |
| 2986 | |
| 2987 | if (!WasNegative && Value.isNegative() && |
| 2988 | isOverflowingIntegerType(Info.Ctx, SubobjType)) { |
| 2989 | APSInt ActualValue(Value, /*IsUnsigned*/true); |
| 2990 | HandleOverflow(Info, E, ActualValue, SubobjType); |
| 2991 | } |
| 2992 | } else { |
| 2993 | --Value; |
| 2994 | |
| 2995 | if (WasNegative && !Value.isNegative() && |
| 2996 | isOverflowingIntegerType(Info.Ctx, SubobjType)) { |
| 2997 | unsigned BitWidth = Value.getBitWidth(); |
| 2998 | APSInt ActualValue(Value.sext(BitWidth + 1), /*IsUnsigned*/false); |
| 2999 | ActualValue.setBit(BitWidth); |
| 3000 | HandleOverflow(Info, E, ActualValue, SubobjType); |
| 3001 | } |
| 3002 | } |
| 3003 | return true; |
| 3004 | } |
| 3005 | bool found(APFloat &Value, QualType SubobjType) { |
| 3006 | if (!checkConst(SubobjType)) |
| 3007 | return false; |
| 3008 | |
| 3009 | if (Old) *Old = APValue(Value); |
| 3010 | |
| 3011 | APFloat One(Value.getSemantics(), 1); |
| 3012 | if (AccessKind == AK_Increment) |
| 3013 | Value.add(One, APFloat::rmNearestTiesToEven); |
| 3014 | else |
| 3015 | Value.subtract(One, APFloat::rmNearestTiesToEven); |
| 3016 | return true; |
| 3017 | } |
| 3018 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 3019 | if (!checkConst(SubobjType)) |
| 3020 | return false; |
| 3021 | |
| 3022 | QualType PointeeType; |
| 3023 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 3024 | PointeeType = PT->getPointeeType(); |
| 3025 | else { |
| 3026 | Info.Diag(E); |
| 3027 | return false; |
| 3028 | } |
| 3029 | |
| 3030 | LValue LVal; |
| 3031 | LVal.setFrom(Info.Ctx, Subobj); |
| 3032 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
| 3033 | AccessKind == AK_Increment ? 1 : -1)) |
| 3034 | return false; |
| 3035 | LVal.moveInto(Subobj); |
| 3036 | return true; |
| 3037 | } |
| 3038 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 3039 | llvm_unreachable("shouldn't encounter string elements here"); |
| 3040 | } |
| 3041 | }; |
| 3042 | } // end anonymous namespace |
| 3043 | |
| 3044 | /// Perform an increment or decrement on LVal. |
| 3045 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
| 3046 | QualType LValType, bool IsIncrement, APValue *Old) { |
| 3047 | if (LVal.Designator.Invalid) |
| 3048 | return false; |
| 3049 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3050 | if (!Info.getLangOpts().CPlusPlus14) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3051 | Info.Diag(E); |
| 3052 | return false; |
| 3053 | } |
| 3054 | |
| 3055 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
| 3056 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
| 3057 | IncDecSubobjectHandler Handler = { Info, E, AK, Old }; |
| 3058 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 3059 | } |
| 3060 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3061 | /// Build an lvalue for the object argument of a member function call. |
| 3062 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 3063 | LValue &This) { |
| 3064 | if (Object->getType()->isPointerType()) |
| 3065 | return EvaluatePointer(Object, This, Info); |
| 3066 | |
| 3067 | if (Object->isGLValue()) |
| 3068 | return EvaluateLValue(Object, This, Info); |
| 3069 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3070 | if (Object->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3071 | return EvaluateTemporary(Object, This, Info); |
| 3072 | |
Richard Smith | 3e79a57 | 2014-06-11 19:53:12 +0000 | [diff] [blame] | 3073 | Info.Diag(Object, diag::note_constexpr_nonliteral) << Object->getType(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3074 | return false; |
| 3075 | } |
| 3076 | |
| 3077 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 3078 | /// lvalue referring to the result. |
| 3079 | /// |
| 3080 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3081 | /// \param LV - An lvalue referring to the base of the member pointer. |
| 3082 | /// \param RHS - The member pointer expression. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3083 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 3084 | /// the resulting LValue subobject designator. This is not possible when |
| 3085 | /// creating a bound member function. |
| 3086 | /// \return The field or method declaration to which the member pointer refers, |
| 3087 | /// or 0 if evaluation fails. |
| 3088 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3089 | QualType LVType, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3090 | LValue &LV, |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3091 | const Expr *RHS, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3092 | bool IncludeMember = true) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3093 | MemberPtr MemPtr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3094 | if (!EvaluateMemberPointer(RHS, MemPtr, Info)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3095 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3096 | |
| 3097 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 3098 | // member value, the behavior is undefined. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3099 | if (!MemPtr.getDecl()) { |
| 3100 | // FIXME: Specific diagnostic. |
| 3101 | Info.Diag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3102 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3103 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3104 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3105 | if (MemPtr.isDerivedMember()) { |
| 3106 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3107 | // The end of the derived-to-base path for the base object must match the |
| 3108 | // derived-to-base path for the member pointer. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3109 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3110 | LV.Designator.Entries.size()) { |
| 3111 | Info.Diag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3112 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3113 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3114 | unsigned PathLengthToMember = |
| 3115 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 3116 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 3117 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 3118 | LV.Designator.Entries[PathLengthToMember + I]); |
| 3119 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3120 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
| 3121 | Info.Diag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3122 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3123 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3127 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3128 | PathLengthToMember)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3129 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3130 | } else if (!MemPtr.Path.empty()) { |
| 3131 | // Extend the LValue path with the member pointer's path. |
| 3132 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 3133 | MemPtr.Path.size() + IncludeMember); |
| 3134 | |
| 3135 | // Walk down to the appropriate base class. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3136 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 3137 | LVType = PT->getPointeeType(); |
| 3138 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 3139 | assert(RD && "member pointer access on non-class-type expression"); |
| 3140 | // The first class in the path is that of the lvalue. |
| 3141 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 3142 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3143 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, Base)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3144 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3145 | RD = Base; |
| 3146 | } |
| 3147 | // Finally cast to the class containing the member. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3148 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, |
| 3149 | MemPtr.getContainingRecord())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3150 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | // Add the member. Note that we cannot build bound member functions here. |
| 3154 | if (IncludeMember) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3155 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3156 | if (!HandleLValueMember(Info, RHS, LV, FD)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3157 | return nullptr; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3158 | } else if (const IndirectFieldDecl *IFD = |
| 3159 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3160 | if (!HandleLValueIndirectMember(Info, RHS, LV, IFD)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3161 | return nullptr; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3162 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3163 | llvm_unreachable("can't construct reference to bound member function"); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3164 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | return MemPtr.getDecl(); |
| 3168 | } |
| 3169 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3170 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 3171 | const BinaryOperator *BO, |
| 3172 | LValue &LV, |
| 3173 | bool IncludeMember = true) { |
| 3174 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 3175 | |
| 3176 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) { |
| 3177 | if (Info.keepEvaluatingAfterFailure()) { |
| 3178 | MemberPtr MemPtr; |
| 3179 | EvaluateMemberPointer(BO->getRHS(), MemPtr, Info); |
| 3180 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3181 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | return HandleMemberPointerAccess(Info, BO->getLHS()->getType(), LV, |
| 3185 | BO->getRHS(), IncludeMember); |
| 3186 | } |
| 3187 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3188 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 3189 | /// the provided lvalue, which currently refers to the base object. |
| 3190 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 3191 | LValue &Result) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3192 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3193 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3194 | return false; |
| 3195 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3196 | QualType TargetQT = E->getType(); |
| 3197 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 3198 | TargetQT = PT->getPointeeType(); |
| 3199 | |
| 3200 | // Check this cast lands within the final derived-to-base subobject path. |
| 3201 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3202 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3203 | << D.MostDerivedType << TargetQT; |
| 3204 | return false; |
| 3205 | } |
| 3206 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3207 | // Check the type of the final cast. We don't need to check the path, |
| 3208 | // since a cast can only be formed if the path is unique. |
| 3209 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3210 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 3211 | const CXXRecordDecl *FinalType; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3212 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 3213 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 3214 | else |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3215 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3216 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3217 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3218 | << D.MostDerivedType << TargetQT; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3219 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3220 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3221 | |
| 3222 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3223 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 3226 | namespace { |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3227 | enum EvalStmtResult { |
| 3228 | /// Evaluation failed. |
| 3229 | ESR_Failed, |
| 3230 | /// Hit a 'return' statement. |
| 3231 | ESR_Returned, |
| 3232 | /// Evaluation succeeded. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3233 | ESR_Succeeded, |
| 3234 | /// Hit a 'continue' statement. |
| 3235 | ESR_Continue, |
| 3236 | /// Hit a 'break' statement. |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3237 | ESR_Break, |
| 3238 | /// Still scanning for 'case' or 'default' statement. |
| 3239 | ESR_CaseNotFound |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3240 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3241 | } |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3242 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3243 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
| 3244 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3245 | // We don't need to evaluate the initializer for a static local. |
| 3246 | if (!VD->hasLocalStorage()) |
| 3247 | return true; |
| 3248 | |
| 3249 | LValue Result; |
| 3250 | Result.set(VD, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3251 | APValue &Val = Info.CurrentCall->createTemporary(VD, true); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3252 | |
Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 3253 | const Expr *InitE = VD->getInit(); |
| 3254 | if (!InitE) { |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3255 | Info.Diag(D->getLocStart(), diag::note_constexpr_uninitialized) |
| 3256 | << false << VD->getType(); |
| 3257 | Val = APValue(); |
| 3258 | return false; |
| 3259 | } |
| 3260 | |
Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 3261 | if (InitE->isValueDependent()) |
| 3262 | return false; |
| 3263 | |
| 3264 | if (!EvaluateInPlace(Val, Info, Result, InitE)) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3265 | // Wipe out any partially-computed value, to allow tracking that this |
| 3266 | // evaluation failed. |
| 3267 | Val = APValue(); |
| 3268 | return false; |
| 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | return true; |
| 3273 | } |
| 3274 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3275 | /// Evaluate a condition (either a variable declaration or an expression). |
| 3276 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
| 3277 | const Expr *Cond, bool &Result) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3278 | FullExpressionRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3279 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
| 3280 | return false; |
| 3281 | return EvaluateAsBooleanCondition(Cond, Result, Info); |
| 3282 | } |
| 3283 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3284 | /// \brief A location where the result (returned value) of evaluating a |
| 3285 | /// statement should be stored. |
| 3286 | struct StmtResult { |
| 3287 | /// The APValue that should be filled in with the returned value. |
| 3288 | APValue &Value; |
| 3289 | /// The location containing the result, if any (used to support RVO). |
| 3290 | const LValue *Slot; |
| 3291 | }; |
| 3292 | |
| 3293 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3294 | const Stmt *S, |
| 3295 | const SwitchCase *SC = nullptr); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3296 | |
| 3297 | /// Evaluate the body of a loop, and translate the result as appropriate. |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3298 | static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3299 | const Stmt *Body, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3300 | const SwitchCase *Case = nullptr) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3301 | BlockScopeRAII Scope(Info); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3302 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, Body, Case)) { |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3303 | case ESR_Break: |
| 3304 | return ESR_Succeeded; |
| 3305 | case ESR_Succeeded: |
| 3306 | case ESR_Continue: |
| 3307 | return ESR_Continue; |
| 3308 | case ESR_Failed: |
| 3309 | case ESR_Returned: |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3310 | case ESR_CaseNotFound: |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3311 | return ESR; |
| 3312 | } |
Hans Wennborg | 9242bd1 | 2013-05-06 15:13:34 +0000 | [diff] [blame] | 3313 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3316 | /// Evaluate a switch statement. |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3317 | static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3318 | const SwitchStmt *SS) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3319 | BlockScopeRAII Scope(Info); |
| 3320 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3321 | // Evaluate the switch condition. |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3322 | APSInt Value; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3323 | { |
| 3324 | FullExpressionRAII Scope(Info); |
| 3325 | if (SS->getConditionVariable() && |
| 3326 | !EvaluateDecl(Info, SS->getConditionVariable())) |
| 3327 | return ESR_Failed; |
| 3328 | if (!EvaluateInteger(SS->getCond(), Value, Info)) |
| 3329 | return ESR_Failed; |
| 3330 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3331 | |
| 3332 | // Find the switch case corresponding to the value of the condition. |
| 3333 | // FIXME: Cache this lookup. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3334 | const SwitchCase *Found = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3335 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
| 3336 | SC = SC->getNextSwitchCase()) { |
| 3337 | if (isa<DefaultStmt>(SC)) { |
| 3338 | Found = SC; |
| 3339 | continue; |
| 3340 | } |
| 3341 | |
| 3342 | const CaseStmt *CS = cast<CaseStmt>(SC); |
| 3343 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx); |
| 3344 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx) |
| 3345 | : LHS; |
| 3346 | if (LHS <= Value && Value <= RHS) { |
| 3347 | Found = SC; |
| 3348 | break; |
| 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | if (!Found) |
| 3353 | return ESR_Succeeded; |
| 3354 | |
| 3355 | // Search the switch body for the switch case and evaluate it from there. |
| 3356 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, SS->getBody(), Found)) { |
| 3357 | case ESR_Break: |
| 3358 | return ESR_Succeeded; |
| 3359 | case ESR_Succeeded: |
| 3360 | case ESR_Continue: |
| 3361 | case ESR_Failed: |
| 3362 | case ESR_Returned: |
| 3363 | return ESR; |
| 3364 | case ESR_CaseNotFound: |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3365 | // This can only happen if the switch case is nested within a statement |
| 3366 | // expression. We have no intention of supporting that. |
| 3367 | Info.Diag(Found->getLocStart(), diag::note_constexpr_stmt_expr_unsupported); |
| 3368 | return ESR_Failed; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3369 | } |
Richard Smith | f8cf9d4 | 2013-05-13 20:33:30 +0000 | [diff] [blame] | 3370 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3373 | // Evaluate a statement. |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3374 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3375 | const Stmt *S, const SwitchCase *Case) { |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 3376 | if (!Info.nextStep(S)) |
| 3377 | return ESR_Failed; |
| 3378 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3379 | // If we're hunting down a 'case' or 'default' label, recurse through |
| 3380 | // substatements until we hit the label. |
| 3381 | if (Case) { |
| 3382 | // FIXME: We don't start the lifetime of objects whose initialization we |
| 3383 | // jump over. However, such objects must be of class type with a trivial |
| 3384 | // default constructor that initialize all subobjects, so must be empty, |
| 3385 | // so this almost never matters. |
| 3386 | switch (S->getStmtClass()) { |
| 3387 | case Stmt::CompoundStmtClass: |
| 3388 | // FIXME: Precompute which substatement of a compound statement we |
| 3389 | // would jump to, and go straight there rather than performing a |
| 3390 | // linear scan each time. |
| 3391 | case Stmt::LabelStmtClass: |
| 3392 | case Stmt::AttributedStmtClass: |
| 3393 | case Stmt::DoStmtClass: |
| 3394 | break; |
| 3395 | |
| 3396 | case Stmt::CaseStmtClass: |
| 3397 | case Stmt::DefaultStmtClass: |
| 3398 | if (Case == S) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3399 | Case = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3400 | break; |
| 3401 | |
| 3402 | case Stmt::IfStmtClass: { |
| 3403 | // FIXME: Precompute which side of an 'if' we would jump to, and go |
| 3404 | // straight there rather than scanning both sides. |
| 3405 | const IfStmt *IS = cast<IfStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3406 | |
| 3407 | // Wrap the evaluation in a block scope, in case it's a DeclStmt |
| 3408 | // preceded by our switch label. |
| 3409 | BlockScopeRAII Scope(Info); |
| 3410 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3411 | EvalStmtResult ESR = EvaluateStmt(Result, Info, IS->getThen(), Case); |
| 3412 | if (ESR != ESR_CaseNotFound || !IS->getElse()) |
| 3413 | return ESR; |
| 3414 | return EvaluateStmt(Result, Info, IS->getElse(), Case); |
| 3415 | } |
| 3416 | |
| 3417 | case Stmt::WhileStmtClass: { |
| 3418 | EvalStmtResult ESR = |
| 3419 | EvaluateLoopBody(Result, Info, cast<WhileStmt>(S)->getBody(), Case); |
| 3420 | if (ESR != ESR_Continue) |
| 3421 | return ESR; |
| 3422 | break; |
| 3423 | } |
| 3424 | |
| 3425 | case Stmt::ForStmtClass: { |
| 3426 | const ForStmt *FS = cast<ForStmt>(S); |
| 3427 | EvalStmtResult ESR = |
| 3428 | EvaluateLoopBody(Result, Info, FS->getBody(), Case); |
| 3429 | if (ESR != ESR_Continue) |
| 3430 | return ESR; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3431 | if (FS->getInc()) { |
| 3432 | FullExpressionRAII IncScope(Info); |
| 3433 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 3434 | return ESR_Failed; |
| 3435 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3436 | break; |
| 3437 | } |
| 3438 | |
| 3439 | case Stmt::DeclStmtClass: |
| 3440 | // FIXME: If the variable has initialization that can't be jumped over, |
| 3441 | // bail out of any immediately-surrounding compound-statement too. |
| 3442 | default: |
| 3443 | return ESR_CaseNotFound; |
| 3444 | } |
| 3445 | } |
| 3446 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3447 | switch (S->getStmtClass()) { |
| 3448 | default: |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3449 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3450 | // Don't bother evaluating beyond an expression-statement which couldn't |
| 3451 | // be evaluated. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3452 | FullExpressionRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3453 | if (!EvaluateIgnoredValue(Info, E)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3454 | return ESR_Failed; |
| 3455 | return ESR_Succeeded; |
| 3456 | } |
| 3457 | |
| 3458 | Info.Diag(S->getLocStart()); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3459 | return ESR_Failed; |
| 3460 | |
| 3461 | case Stmt::NullStmtClass: |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3462 | return ESR_Succeeded; |
| 3463 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3464 | case Stmt::DeclStmtClass: { |
| 3465 | const DeclStmt *DS = cast<DeclStmt>(S); |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 3466 | for (const auto *DclIt : DS->decls()) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3467 | // Each declaration initialization is its own full-expression. |
| 3468 | // FIXME: This isn't quite right; if we're performing aggregate |
| 3469 | // initialization, each braced subexpression is its own full-expression. |
| 3470 | FullExpressionRAII Scope(Info); |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 3471 | if (!EvaluateDecl(Info, DclIt) && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3472 | return ESR_Failed; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3473 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3474 | return ESR_Succeeded; |
| 3475 | } |
| 3476 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3477 | case Stmt::ReturnStmtClass: { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3478 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3479 | FullExpressionRAII Scope(Info); |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3480 | if (RetExpr && |
| 3481 | !(Result.Slot |
| 3482 | ? EvaluateInPlace(Result.Value, Info, *Result.Slot, RetExpr) |
| 3483 | : Evaluate(Result.Value, Info, RetExpr))) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3484 | return ESR_Failed; |
| 3485 | return ESR_Returned; |
| 3486 | } |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3487 | |
| 3488 | case Stmt::CompoundStmtClass: { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3489 | BlockScopeRAII Scope(Info); |
| 3490 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3491 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 3492 | for (const auto *BI : CS->body()) { |
| 3493 | EvalStmtResult ESR = EvaluateStmt(Result, Info, BI, Case); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3494 | if (ESR == ESR_Succeeded) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3495 | Case = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3496 | else if (ESR != ESR_CaseNotFound) |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3497 | return ESR; |
| 3498 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3499 | return Case ? ESR_CaseNotFound : ESR_Succeeded; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3500 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3501 | |
| 3502 | case Stmt::IfStmtClass: { |
| 3503 | const IfStmt *IS = cast<IfStmt>(S); |
| 3504 | |
| 3505 | // Evaluate the condition, as either a var decl or as an expression. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3506 | BlockScopeRAII Scope(Info); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3507 | bool Cond; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3508 | if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(), Cond)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3509 | return ESR_Failed; |
| 3510 | |
| 3511 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
| 3512 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt); |
| 3513 | if (ESR != ESR_Succeeded) |
| 3514 | return ESR; |
| 3515 | } |
| 3516 | return ESR_Succeeded; |
| 3517 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3518 | |
| 3519 | case Stmt::WhileStmtClass: { |
| 3520 | const WhileStmt *WS = cast<WhileStmt>(S); |
| 3521 | while (true) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3522 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3523 | bool Continue; |
| 3524 | if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(), |
| 3525 | Continue)) |
| 3526 | return ESR_Failed; |
| 3527 | if (!Continue) |
| 3528 | break; |
| 3529 | |
| 3530 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody()); |
| 3531 | if (ESR != ESR_Continue) |
| 3532 | return ESR; |
| 3533 | } |
| 3534 | return ESR_Succeeded; |
| 3535 | } |
| 3536 | |
| 3537 | case Stmt::DoStmtClass: { |
| 3538 | const DoStmt *DS = cast<DoStmt>(S); |
| 3539 | bool Continue; |
| 3540 | do { |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3541 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody(), Case); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3542 | if (ESR != ESR_Continue) |
| 3543 | return ESR; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3544 | Case = nullptr; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3545 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3546 | FullExpressionRAII CondScope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3547 | if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info)) |
| 3548 | return ESR_Failed; |
| 3549 | } while (Continue); |
| 3550 | return ESR_Succeeded; |
| 3551 | } |
| 3552 | |
| 3553 | case Stmt::ForStmtClass: { |
| 3554 | const ForStmt *FS = cast<ForStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3555 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3556 | if (FS->getInit()) { |
| 3557 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
| 3558 | if (ESR != ESR_Succeeded) |
| 3559 | return ESR; |
| 3560 | } |
| 3561 | while (true) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3562 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3563 | bool Continue = true; |
| 3564 | if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), |
| 3565 | FS->getCond(), Continue)) |
| 3566 | return ESR_Failed; |
| 3567 | if (!Continue) |
| 3568 | break; |
| 3569 | |
| 3570 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 3571 | if (ESR != ESR_Continue) |
| 3572 | return ESR; |
| 3573 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3574 | if (FS->getInc()) { |
| 3575 | FullExpressionRAII IncScope(Info); |
| 3576 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 3577 | return ESR_Failed; |
| 3578 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3579 | } |
| 3580 | return ESR_Succeeded; |
| 3581 | } |
| 3582 | |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3583 | case Stmt::CXXForRangeStmtClass: { |
| 3584 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3585 | BlockScopeRAII Scope(Info); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3586 | |
| 3587 | // Initialize the __range variable. |
| 3588 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt()); |
| 3589 | if (ESR != ESR_Succeeded) |
| 3590 | return ESR; |
| 3591 | |
| 3592 | // Create the __begin and __end iterators. |
| 3593 | ESR = EvaluateStmt(Result, Info, FS->getBeginEndStmt()); |
| 3594 | if (ESR != ESR_Succeeded) |
| 3595 | return ESR; |
| 3596 | |
| 3597 | while (true) { |
| 3598 | // Condition: __begin != __end. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3599 | { |
| 3600 | bool Continue = true; |
| 3601 | FullExpressionRAII CondExpr(Info); |
| 3602 | if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info)) |
| 3603 | return ESR_Failed; |
| 3604 | if (!Continue) |
| 3605 | break; |
| 3606 | } |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3607 | |
| 3608 | // User's variable declaration, initialized by *__begin. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3609 | BlockScopeRAII InnerScope(Info); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3610 | ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt()); |
| 3611 | if (ESR != ESR_Succeeded) |
| 3612 | return ESR; |
| 3613 | |
| 3614 | // Loop body. |
| 3615 | ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 3616 | if (ESR != ESR_Continue) |
| 3617 | return ESR; |
| 3618 | |
| 3619 | // Increment: ++__begin |
| 3620 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 3621 | return ESR_Failed; |
| 3622 | } |
| 3623 | |
| 3624 | return ESR_Succeeded; |
| 3625 | } |
| 3626 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3627 | case Stmt::SwitchStmtClass: |
| 3628 | return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S)); |
| 3629 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3630 | case Stmt::ContinueStmtClass: |
| 3631 | return ESR_Continue; |
| 3632 | |
| 3633 | case Stmt::BreakStmtClass: |
| 3634 | return ESR_Break; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3635 | |
| 3636 | case Stmt::LabelStmtClass: |
| 3637 | return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case); |
| 3638 | |
| 3639 | case Stmt::AttributedStmtClass: |
| 3640 | // As a general principle, C++11 attributes can be ignored without |
| 3641 | // any semantic impact. |
| 3642 | return EvaluateStmt(Result, Info, cast<AttributedStmt>(S)->getSubStmt(), |
| 3643 | Case); |
| 3644 | |
| 3645 | case Stmt::CaseStmtClass: |
| 3646 | case Stmt::DefaultStmtClass: |
| 3647 | return EvaluateStmt(Result, Info, cast<SwitchCase>(S)->getSubStmt(), Case); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3648 | } |
| 3649 | } |
| 3650 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3651 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 3652 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 3653 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 3654 | /// so we need special handling. |
| 3655 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3656 | const CXXConstructorDecl *CD, |
| 3657 | bool IsValueInitialization) { |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3658 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 3659 | return false; |
| 3660 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 3661 | // Value-initialization does not call a trivial default constructor, so such a |
| 3662 | // call is a core constant expression whether or not the constructor is |
| 3663 | // constexpr. |
| 3664 | if (!CD->isConstexpr() && !IsValueInitialization) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3665 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 3666 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 3667 | // we should be much more explicit about why it's not constexpr. |
| 3668 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 3669 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 3670 | Info.Note(CD->getLocation(), diag::note_declared_at); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3671 | } else { |
| 3672 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 3673 | } |
| 3674 | } |
| 3675 | return true; |
| 3676 | } |
| 3677 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3678 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 3679 | /// expression. |
| 3680 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 3681 | const FunctionDecl *Declaration, |
| 3682 | const FunctionDecl *Definition) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3683 | // Potential constant expressions can contain calls to declared, but not yet |
| 3684 | // defined, constexpr functions. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 3685 | if (Info.checkingPotentialConstantExpression() && !Definition && |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3686 | Declaration->isConstexpr()) |
| 3687 | return false; |
| 3688 | |
Richard Smith | 0838f3a | 2013-05-14 05:18:44 +0000 | [diff] [blame] | 3689 | // Bail out with no diagnostic if the function declaration itself is invalid. |
| 3690 | // We will have produced a relevant diagnostic while parsing it. |
| 3691 | if (Declaration->isInvalidDecl()) |
| 3692 | return false; |
| 3693 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3694 | // Can we evaluate this function call? |
| 3695 | if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) |
| 3696 | return true; |
| 3697 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3698 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3699 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 3700 | // FIXME: If DiagDecl is an implicitly-declared special member function, we |
| 3701 | // should be much more explicit about why it's not constexpr. |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3702 | Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) |
| 3703 | << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) |
| 3704 | << DiagDecl; |
| 3705 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 3706 | } else { |
| 3707 | Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| 3708 | } |
| 3709 | return false; |
| 3710 | } |
| 3711 | |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 3712 | /// Determine if a class has any fields that might need to be copied by a |
| 3713 | /// trivial copy or move operation. |
| 3714 | static bool hasFields(const CXXRecordDecl *RD) { |
| 3715 | if (!RD || RD->isEmpty()) |
| 3716 | return false; |
| 3717 | for (auto *FD : RD->fields()) { |
| 3718 | if (FD->isUnnamedBitfield()) |
| 3719 | continue; |
| 3720 | return true; |
| 3721 | } |
| 3722 | for (auto &Base : RD->bases()) |
| 3723 | if (hasFields(Base.getType()->getAsCXXRecordDecl())) |
| 3724 | return true; |
| 3725 | return false; |
| 3726 | } |
| 3727 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3728 | namespace { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3729 | typedef SmallVector<APValue, 8> ArgVector; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
| 3732 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 3733 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 3734 | EvalInfo &Info) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3735 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3736 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3737 | I != E; ++I) { |
| 3738 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
| 3739 | // If we're checking for a potential constant expression, evaluate all |
| 3740 | // initializers even if some of them fail. |
| 3741 | if (!Info.keepEvaluatingAfterFailure()) |
| 3742 | return false; |
| 3743 | Success = false; |
| 3744 | } |
| 3745 | } |
| 3746 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3747 | } |
| 3748 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3749 | /// Evaluate a function call. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3750 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 3751 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3752 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3753 | EvalInfo &Info, APValue &Result, |
| 3754 | const LValue *ResultSlot) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3755 | ArgVector ArgValues(Args.size()); |
| 3756 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 3757 | return false; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3758 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3759 | if (!Info.CheckCallLimit(CallLoc)) |
| 3760 | return false; |
| 3761 | |
| 3762 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 3763 | |
| 3764 | // For a trivial copy or move assignment, perform an APValue copy. This is |
| 3765 | // essential for unions, where the operations performed by the assignment |
| 3766 | // operator cannot be represented as statements. |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 3767 | // |
| 3768 | // Skip this for non-union classes with no fields; in that case, the defaulted |
| 3769 | // copy/move does not actually read the object. |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 3770 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee); |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 3771 | if (MD && MD->isDefaulted() && |
| 3772 | (MD->getParent()->isUnion() || |
| 3773 | (MD->isTrivial() && hasFields(MD->getParent())))) { |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 3774 | assert(This && |
| 3775 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
| 3776 | LValue RHS; |
| 3777 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
| 3778 | APValue RHSValue; |
| 3779 | if (!handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 3780 | RHS, RHSValue)) |
| 3781 | return false; |
| 3782 | if (!handleAssignment(Info, Args[0], *This, MD->getThisType(Info.Ctx), |
| 3783 | RHSValue)) |
| 3784 | return false; |
| 3785 | This->moveInto(Result); |
| 3786 | return true; |
| 3787 | } |
| 3788 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3789 | StmtResult Ret = {Result, ResultSlot}; |
| 3790 | EvalStmtResult ESR = EvaluateStmt(Ret, Info, Body); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3791 | if (ESR == ESR_Succeeded) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3792 | if (Callee->getReturnType()->isVoidType()) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3793 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3794 | Info.Diag(Callee->getLocEnd(), diag::note_constexpr_no_return); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3795 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3796 | return ESR == ESR_Returned; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3799 | /// Evaluate a constructor call. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3800 | static bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This, |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3801 | ArrayRef<const Expr*> Args, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3802 | const CXXConstructorDecl *Definition, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3803 | EvalInfo &Info, APValue &Result) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3804 | ArgVector ArgValues(Args.size()); |
| 3805 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 3806 | return false; |
| 3807 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3808 | if (!Info.CheckCallLimit(CallLoc)) |
| 3809 | return false; |
| 3810 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3811 | const CXXRecordDecl *RD = Definition->getParent(); |
| 3812 | if (RD->getNumVBases()) { |
| 3813 | Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
| 3814 | return false; |
| 3815 | } |
| 3816 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3817 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3818 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3819 | // FIXME: Creating an APValue just to hold a nonexistent return value is |
| 3820 | // wasteful. |
| 3821 | APValue RetVal; |
| 3822 | StmtResult Ret = {RetVal, nullptr}; |
| 3823 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3824 | // If it's a delegating constructor, just delegate. |
| 3825 | if (Definition->isDelegatingConstructor()) { |
| 3826 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
Richard Smith | 9ff62af | 2013-11-07 18:45:03 +0000 | [diff] [blame] | 3827 | { |
| 3828 | FullExpressionRAII InitScope(Info); |
| 3829 | if (!EvaluateInPlace(Result, Info, This, (*I)->getInit())) |
| 3830 | return false; |
| 3831 | } |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3832 | return EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3835 | // For a trivial copy or move constructor, perform an APValue copy. This is |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 3836 | // essential for unions (or classes with anonymous union members), where the |
| 3837 | // operations performed by the constructor cannot be represented by |
| 3838 | // ctor-initializers. |
| 3839 | // |
| 3840 | // Skip this for empty non-union classes; we should not perform an |
| 3841 | // lvalue-to-rvalue conversion on them because their copy constructor does not |
| 3842 | // actually read them. |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 3843 | if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() && |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 3844 | (Definition->getParent()->isUnion() || |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 3845 | (Definition->isTrivial() && hasFields(Definition->getParent())))) { |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3846 | LValue RHS; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3847 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3848 | return handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3849 | RHS, Result); |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | // Reserve space for the struct members. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3853 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3854 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 3855 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3856 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3857 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3858 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3859 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3860 | // A scope for temporaries lifetime-extended by reference members. |
| 3861 | BlockScopeRAII LifetimeExtendedScope(Info); |
| 3862 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3863 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3864 | unsigned BasesSeen = 0; |
| 3865 | #ifndef NDEBUG |
| 3866 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 3867 | #endif |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3868 | for (const auto *I : Definition->inits()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3869 | LValue Subobject = This; |
| 3870 | APValue *Value = &Result; |
| 3871 | |
| 3872 | // Determine the subobject to initialize. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3873 | FieldDecl *FD = nullptr; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3874 | if (I->isBaseInitializer()) { |
| 3875 | QualType BaseType(I->getBaseClass(), 0); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3876 | #ifndef NDEBUG |
| 3877 | // Non-virtual base classes are initialized in the order in the class |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3878 | // definition. We have already checked for virtual base classes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3879 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 3880 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 3881 | "base class initializers not in expected order"); |
| 3882 | ++BaseIt; |
| 3883 | #endif |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3884 | if (!HandleLValueDirectBase(Info, I->getInit(), Subobject, RD, |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3885 | BaseType->getAsCXXRecordDecl(), &Layout)) |
| 3886 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3887 | Value = &Result.getStructBase(BasesSeen++); |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3888 | } else if ((FD = I->getMember())) { |
| 3889 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3890 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3891 | if (RD->isUnion()) { |
| 3892 | Result = APValue(FD); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3893 | Value = &Result.getUnionValue(); |
| 3894 | } else { |
| 3895 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 3896 | } |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3897 | } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3898 | // Walk the indirect field decl's chain to find the object to initialize, |
| 3899 | // and make sure we've initialized every step along it. |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 3900 | for (auto *C : IFD->chain()) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 3901 | FD = cast<FieldDecl>(C); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3902 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 3903 | // Switch the union field if it differs. This happens if we had |
| 3904 | // preceding zero-initialization, and we're now initializing a union |
| 3905 | // subobject other than the first. |
| 3906 | // FIXME: In this case, the values of the other subobjects are |
| 3907 | // specified, since zero-initialization sets all padding bits to zero. |
| 3908 | if (Value->isUninit() || |
| 3909 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 3910 | if (CD->isUnion()) |
| 3911 | *Value = APValue(FD); |
| 3912 | else |
| 3913 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 3914 | std::distance(CD->field_begin(), CD->field_end())); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3915 | } |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3916 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3917 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3918 | if (CD->isUnion()) |
| 3919 | Value = &Value->getUnionValue(); |
| 3920 | else |
| 3921 | Value = &Value->getStructField(FD->getFieldIndex()); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3922 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3923 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3924 | llvm_unreachable("unknown base initializer kind"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3925 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3926 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3927 | FullExpressionRAII InitScope(Info); |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3928 | if (!EvaluateInPlace(*Value, Info, Subobject, I->getInit()) || |
| 3929 | (FD && FD->isBitField() && !truncateBitfieldValue(Info, I->getInit(), |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 3930 | *Value, FD))) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3931 | // If we're checking for a potential constant expression, evaluate all |
| 3932 | // initializers even if some of them fail. |
| 3933 | if (!Info.keepEvaluatingAfterFailure()) |
| 3934 | return false; |
| 3935 | Success = false; |
| 3936 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3939 | return Success && |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3940 | EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3943 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3944 | // Generic Evaluation |
| 3945 | //===----------------------------------------------------------------------===// |
| 3946 | namespace { |
| 3947 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3948 | template <class Derived> |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3949 | class ExprEvaluatorBase |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3950 | : public ConstStmtVisitor<Derived, bool> { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3951 | private: |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3952 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3953 | bool DerivedSuccess(const APValue &V, const Expr *E) { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3954 | return getDerived().Success(V, E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3955 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3956 | bool DerivedZeroInitialization(const Expr *E) { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3957 | return getDerived().ZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3958 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3959 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3960 | // Check whether a conditional operator with a non-constant condition is a |
| 3961 | // potential constant expression. If neither arm is a potential constant |
| 3962 | // expression, then the conditional operator is not either. |
| 3963 | template<typename ConditionalOperator> |
| 3964 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 3965 | assert(Info.checkingPotentialConstantExpression()); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3966 | |
| 3967 | // Speculatively evaluate both arms. |
| 3968 | { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3969 | SmallVector<PartialDiagnosticAt, 8> Diag; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3970 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
| 3971 | |
| 3972 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 3973 | if (Diag.empty()) |
| 3974 | return; |
| 3975 | |
| 3976 | Diag.clear(); |
| 3977 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 3978 | if (Diag.empty()) |
| 3979 | return; |
| 3980 | } |
| 3981 | |
| 3982 | Error(E, diag::note_constexpr_conditional_never_const); |
| 3983 | } |
| 3984 | |
| 3985 | |
| 3986 | template<typename ConditionalOperator> |
| 3987 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
| 3988 | bool BoolResult; |
| 3989 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 3990 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3991 | CheckPotentialConstantConditional(E); |
| 3992 | return false; |
| 3993 | } |
| 3994 | |
| 3995 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 3996 | return StmtVisitorTy::Visit(EvalExpr); |
| 3997 | } |
| 3998 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3999 | protected: |
| 4000 | EvalInfo &Info; |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4001 | typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4002 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 4003 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 4004 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4005 | return Info.CCEDiag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4008 | bool ZeroInitialization(const Expr *E) { return Error(E); } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4009 | |
| 4010 | public: |
| 4011 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 4012 | |
| 4013 | EvalInfo &getEvalInfo() { return Info; } |
| 4014 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4015 | /// Report an evaluation error. This should only be called when an error is |
| 4016 | /// first discovered. When propagating an error, just return false. |
| 4017 | bool Error(const Expr *E, diag::kind D) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4018 | Info.Diag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4019 | return false; |
| 4020 | } |
| 4021 | bool Error(const Expr *E) { |
| 4022 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 4023 | } |
| 4024 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4025 | bool VisitStmt(const Stmt *) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4026 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4027 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4028 | bool VisitExpr(const Expr *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4029 | return Error(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4032 | bool VisitParenExpr(const ParenExpr *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4033 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4034 | bool VisitUnaryExtension(const UnaryOperator *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4035 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4036 | bool VisitUnaryPlus(const UnaryOperator *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4037 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4038 | bool VisitChooseExpr(const ChooseExpr *E) |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 4039 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4040 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4041 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4042 | bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4043 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4044 | bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
Richard Smith | f8120ca | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 4045 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4046 | bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
Richard Smith | 17e3246 | 2013-09-13 20:51:45 +0000 | [diff] [blame] | 4047 | // The initializer may not have been parsed yet, or might be erroneous. |
| 4048 | if (!E->getExpr()) |
| 4049 | return Error(E); |
| 4050 | return StmtVisitorTy::Visit(E->getExpr()); |
| 4051 | } |
Richard Smith | 5894a91 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 4052 | // We cannot create any objects for which cleanups are required, so there is |
| 4053 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4054 | bool VisitExprWithCleanups(const ExprWithCleanups *E) |
Richard Smith | 5894a91 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 4055 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4056 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4057 | bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4058 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 4059 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 4060 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4061 | bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4062 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 4063 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 4064 | } |
| 4065 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4066 | bool VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4067 | switch (E->getOpcode()) { |
| 4068 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4069 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4070 | |
| 4071 | case BO_Comma: |
| 4072 | VisitIgnoredValue(E->getLHS()); |
| 4073 | return StmtVisitorTy::Visit(E->getRHS()); |
| 4074 | |
| 4075 | case BO_PtrMemD: |
| 4076 | case BO_PtrMemI: { |
| 4077 | LValue Obj; |
| 4078 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 4079 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4080 | APValue Result; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4081 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4082 | return false; |
| 4083 | return DerivedSuccess(Result, E); |
| 4084 | } |
| 4085 | } |
| 4086 | } |
| 4087 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4088 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 4089 | // Evaluate and cache the common expression. We treat it as a temporary, |
| 4090 | // even though it's not quite the same thing. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4091 | if (!Evaluate(Info.CurrentCall->createTemporary(E->getOpaqueValue(), false), |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 4092 | Info, E->getCommon())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4093 | return false; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4094 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4095 | return HandleConditionalOperator(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4098 | bool VisitConditionalOperator(const ConditionalOperator *E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4099 | bool IsBcpCall = false; |
| 4100 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 4101 | // the result is a constant expression if it can be folded without |
| 4102 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 4103 | // for discussion. |
| 4104 | if (const CallExpr *CallCE = |
| 4105 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 4106 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4107 | IsBcpCall = true; |
| 4108 | |
| 4109 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 4110 | // constant expression; we can't check whether it's potentially foldable. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4111 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4112 | return false; |
| 4113 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4114 | FoldConstant Fold(Info, IsBcpCall); |
| 4115 | if (!HandleConditionalOperator(E)) { |
| 4116 | Fold.keepDiagnostics(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4117 | return false; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4118 | } |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4119 | |
| 4120 | return true; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4123 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4124 | if (APValue *Value = Info.CurrentCall->getTemporary(E)) |
| 4125 | return DerivedSuccess(*Value, E); |
| 4126 | |
| 4127 | const Expr *Source = E->getSourceExpr(); |
| 4128 | if (!Source) |
| 4129 | return Error(E); |
| 4130 | if (Source == E) { // sanity checking. |
| 4131 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
| 4132 | return Error(E); |
Argyrios Kyrtzidis | fac35c0 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 4133 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4134 | return StmtVisitorTy::Visit(Source); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4135 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4136 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4137 | bool VisitCallExpr(const CallExpr *E) { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4138 | APValue Result; |
| 4139 | if (!handleCallExpr(E, Result, nullptr)) |
| 4140 | return false; |
| 4141 | return DerivedSuccess(Result, E); |
| 4142 | } |
| 4143 | |
| 4144 | bool handleCallExpr(const CallExpr *E, APValue &Result, |
| 4145 | const LValue *ResultSlot) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4146 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4147 | QualType CalleeType = Callee->getType(); |
| 4148 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4149 | const FunctionDecl *FD = nullptr; |
| 4150 | LValue *This = nullptr, ThisVal; |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 4151 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4152 | bool HasQualifier = false; |
Richard Smith | 656d49d | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 4153 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4154 | // Extract function decl and 'this' pointer from the callee. |
| 4155 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4156 | const ValueDecl *Member = nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4157 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 4158 | // Explicit bound member calls, such as x.f() or p->g(); |
| 4159 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4160 | return false; |
| 4161 | Member = ME->getMemberDecl(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4162 | This = &ThisVal; |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4163 | HasQualifier = ME->hasQualifier(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4164 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 4165 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4166 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 4167 | if (!Member) return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4168 | This = &ThisVal; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4169 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4170 | return Error(Callee); |
| 4171 | |
| 4172 | FD = dyn_cast<FunctionDecl>(Member); |
| 4173 | if (!FD) |
| 4174 | return Error(Callee); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4175 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4176 | LValue Call; |
| 4177 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4178 | return false; |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4179 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4180 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4181 | return Error(Callee); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4182 | FD = dyn_cast_or_null<FunctionDecl>( |
| 4183 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4184 | if (!FD) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4185 | return Error(Callee); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4186 | |
| 4187 | // Overloaded operator calls to member functions are represented as normal |
| 4188 | // calls with '*this' as the first argument. |
| 4189 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 4190 | if (MD && !MD->isStatic()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4191 | // FIXME: When selecting an implicit conversion for an overloaded |
| 4192 | // operator delete, we sometimes try to evaluate calls to conversion |
| 4193 | // operators without a 'this' parameter! |
| 4194 | if (Args.empty()) |
| 4195 | return Error(E); |
| 4196 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4197 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 4198 | return false; |
| 4199 | This = &ThisVal; |
| 4200 | Args = Args.slice(1); |
| 4201 | } |
| 4202 | |
| 4203 | // Don't call function pointers which have been cast to some other type. |
| 4204 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4205 | return Error(E); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4206 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4207 | return Error(E); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4208 | |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 4209 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
| 4210 | return false; |
| 4211 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4212 | // DR1358 allows virtual constexpr functions in some cases. Don't allow |
| 4213 | // calls to such functions in constant expressions. |
| 4214 | if (This && !HasQualifier && |
| 4215 | isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) |
| 4216 | return Error(E, diag::note_constexpr_virtual_call); |
| 4217 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4218 | const FunctionDecl *Definition = nullptr; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4219 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4220 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4221 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4222 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, Info, |
| 4223 | Result, ResultSlot)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4224 | return false; |
| 4225 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4226 | return true; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4229 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4230 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 4231 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4232 | bool VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 90dc175 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 4233 | if (E->getNumInits() == 0) |
| 4234 | return DerivedZeroInitialization(E); |
| 4235 | if (E->getNumInits() == 1) |
| 4236 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4237 | return Error(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4238 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4239 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4240 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4241 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4242 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4243 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4244 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4245 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4246 | return DerivedZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4247 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4248 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4249 | /// A member expression where the object is a prvalue is itself a prvalue. |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4250 | bool VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4251 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 4252 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4253 | APValue Val; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4254 | if (!Evaluate(Val, Info, E->getBase())) |
| 4255 | return false; |
| 4256 | |
| 4257 | QualType BaseTy = E->getBase()->getType(); |
| 4258 | |
| 4259 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4260 | if (!FD) return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4261 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 4262 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4263 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 4264 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4265 | CompleteObject Obj(&Val, BaseTy); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4266 | SubobjectDesignator Designator(BaseTy); |
| 4267 | Designator.addDeclUnchecked(FD); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4268 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4269 | APValue Result; |
| 4270 | return extractSubobject(Info, E, Obj, Designator, Result) && |
| 4271 | DerivedSuccess(Result, E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4274 | bool VisitCastExpr(const CastExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4275 | switch (E->getCastKind()) { |
| 4276 | default: |
| 4277 | break; |
| 4278 | |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4279 | case CK_AtomicToNonAtomic: { |
| 4280 | APValue AtomicVal; |
| 4281 | if (!EvaluateAtomic(E->getSubExpr(), AtomicVal, Info)) |
| 4282 | return false; |
| 4283 | return DerivedSuccess(AtomicVal, E); |
| 4284 | } |
| 4285 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4286 | case CK_NoOp: |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 4287 | case CK_UserDefinedConversion: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4288 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 4289 | |
| 4290 | case CK_LValueToRValue: { |
| 4291 | LValue LVal; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4292 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 4293 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4294 | APValue RVal; |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 4295 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4296 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 4297 | LVal, RVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4298 | return false; |
| 4299 | return DerivedSuccess(RVal, E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4300 | } |
| 4301 | } |
| 4302 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4303 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4304 | } |
| 4305 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4306 | bool VisitUnaryPostInc(const UnaryOperator *UO) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4307 | return VisitUnaryPostIncDec(UO); |
| 4308 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4309 | bool VisitUnaryPostDec(const UnaryOperator *UO) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4310 | return VisitUnaryPostIncDec(UO); |
| 4311 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4312 | bool VisitUnaryPostIncDec(const UnaryOperator *UO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 4313 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4314 | return Error(UO); |
| 4315 | |
| 4316 | LValue LVal; |
| 4317 | if (!EvaluateLValue(UO->getSubExpr(), LVal, Info)) |
| 4318 | return false; |
| 4319 | APValue RVal; |
| 4320 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
| 4321 | UO->isIncrementOp(), &RVal)) |
| 4322 | return false; |
| 4323 | return DerivedSuccess(RVal, UO); |
| 4324 | } |
| 4325 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4326 | bool VisitStmtExpr(const StmtExpr *E) { |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4327 | // We will have checked the full-expressions inside the statement expression |
| 4328 | // when they were completed, and don't need to check them again now. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4329 | if (Info.checkingForOverflow()) |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4330 | return Error(E); |
| 4331 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4332 | BlockScopeRAII Scope(Info); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4333 | const CompoundStmt *CS = E->getSubStmt(); |
Jonathan Roelofs | 104cbf9 | 2015-06-01 16:23:08 +0000 | [diff] [blame] | 4334 | if (CS->body_empty()) |
| 4335 | return true; |
| 4336 | |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4337 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 4338 | BE = CS->body_end(); |
| 4339 | /**/; ++BI) { |
| 4340 | if (BI + 1 == BE) { |
| 4341 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
| 4342 | if (!FinalExpr) { |
| 4343 | Info.Diag((*BI)->getLocStart(), |
| 4344 | diag::note_constexpr_stmt_expr_unsupported); |
| 4345 | return false; |
| 4346 | } |
| 4347 | return this->Visit(FinalExpr); |
| 4348 | } |
| 4349 | |
| 4350 | APValue ReturnValue; |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4351 | StmtResult Result = { ReturnValue, nullptr }; |
| 4352 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4353 | if (ESR != ESR_Succeeded) { |
| 4354 | // FIXME: If the statement-expression terminated due to 'return', |
| 4355 | // 'break', or 'continue', it would be nice to propagate that to |
| 4356 | // the outer statement evaluation rather than bailing out. |
| 4357 | if (ESR != ESR_Failed) |
| 4358 | Info.Diag((*BI)->getLocStart(), |
| 4359 | diag::note_constexpr_stmt_expr_unsupported); |
| 4360 | return false; |
| 4361 | } |
| 4362 | } |
Jonathan Roelofs | 104cbf9 | 2015-06-01 16:23:08 +0000 | [diff] [blame] | 4363 | |
| 4364 | llvm_unreachable("Return from function from the loop above."); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4367 | /// Visit a value which is evaluated, but whose value is ignored. |
| 4368 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4369 | EvaluateIgnoredValue(Info, E); |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4370 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4371 | }; |
| 4372 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4373 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4374 | |
| 4375 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4376 | // Common base class for lvalue and temporary evaluation. |
| 4377 | //===----------------------------------------------------------------------===// |
| 4378 | namespace { |
| 4379 | template<class Derived> |
| 4380 | class LValueExprEvaluatorBase |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4381 | : public ExprEvaluatorBase<Derived> { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4382 | protected: |
| 4383 | LValue &Result; |
| 4384 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4385 | typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4386 | |
| 4387 | bool Success(APValue::LValueBase B) { |
| 4388 | Result.set(B); |
| 4389 | return true; |
| 4390 | } |
| 4391 | |
| 4392 | public: |
| 4393 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : |
| 4394 | ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 4395 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4396 | bool Success(const APValue &V, const Expr *E) { |
| 4397 | Result.setFrom(this->Info.Ctx, V); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4398 | return true; |
| 4399 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4400 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4401 | bool VisitMemberExpr(const MemberExpr *E) { |
| 4402 | // Handle non-static data members. |
| 4403 | QualType BaseTy; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 4404 | bool EvalOK; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4405 | if (E->isArrow()) { |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 4406 | EvalOK = EvaluatePointer(E->getBase(), Result, this->Info); |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 4407 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4408 | } else if (E->getBase()->isRValue()) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 4409 | assert(E->getBase()->getType()->isRecordType()); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 4410 | EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4411 | BaseTy = E->getBase()->getType(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4412 | } else { |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 4413 | EvalOK = this->Visit(E->getBase()); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4414 | BaseTy = E->getBase()->getType(); |
| 4415 | } |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 4416 | if (!EvalOK) { |
| 4417 | if (!this->Info.allowInvalidBaseExpr()) |
| 4418 | return false; |
| 4419 | Result.setInvalid(E->getBase()); |
| 4420 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4421 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4422 | const ValueDecl *MD = E->getMemberDecl(); |
| 4423 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 4424 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 4425 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 4426 | (void)BaseTy; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4427 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
| 4428 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4429 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4430 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
| 4431 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4432 | } else |
| 4433 | return this->Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4434 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4435 | if (MD->getType()->isReferenceType()) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4436 | APValue RefValue; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4437 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4438 | RefValue)) |
| 4439 | return false; |
| 4440 | return Success(RefValue, E); |
| 4441 | } |
| 4442 | return true; |
| 4443 | } |
| 4444 | |
| 4445 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 4446 | switch (E->getOpcode()) { |
| 4447 | default: |
| 4448 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 4449 | |
| 4450 | case BO_PtrMemD: |
| 4451 | case BO_PtrMemI: |
| 4452 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 4453 | } |
| 4454 | } |
| 4455 | |
| 4456 | bool VisitCastExpr(const CastExpr *E) { |
| 4457 | switch (E->getCastKind()) { |
| 4458 | default: |
| 4459 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 4460 | |
| 4461 | case CK_DerivedToBase: |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4462 | case CK_UncheckedDerivedToBase: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4463 | if (!this->Visit(E->getSubExpr())) |
| 4464 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4465 | |
| 4466 | // Now figure out the necessary offset to add to the base LV to get from |
| 4467 | // the derived class to the base class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4468 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
| 4469 | Result); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4470 | } |
| 4471 | } |
| 4472 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4473 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4474 | |
| 4475 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4476 | // LValue Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4477 | // |
| 4478 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 4479 | // function designators (in C), decl references to void objects (in C), and |
| 4480 | // temporaries (if building with -Wno-address-of-temporary). |
| 4481 | // |
| 4482 | // LValue evaluation produces values comprising a base expression of one of the |
| 4483 | // following types: |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4484 | // - Declarations |
| 4485 | // * VarDecl |
| 4486 | // * FunctionDecl |
| 4487 | // - Literals |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4488 | // * CompoundLiteralExpr in C |
| 4489 | // * StringLiteral |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4490 | // * CXXTypeidExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4491 | // * PredefinedExpr |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4492 | // * ObjCStringLiteralExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4493 | // * ObjCEncodeExpr |
| 4494 | // * AddrLabelExpr |
| 4495 | // * BlockExpr |
| 4496 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4497 | // - Locals and temporaries |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4498 | // * MaterializeTemporaryExpr |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4499 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4500 | // was evaluated, for cases where the MaterializeTemporaryExpr is missing |
| 4501 | // from the AST (FIXME). |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4502 | // * A MaterializeTemporaryExpr that has static storage duration, with no |
| 4503 | // CallIndex, for a lifetime-extended temporary. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4504 | // plus an offset in bytes. |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4505 | //===----------------------------------------------------------------------===// |
| 4506 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4507 | class LValueExprEvaluator |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4508 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4509 | public: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4510 | LValueExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 4511 | LValueExprEvaluatorBaseTy(Info, Result) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4512 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4513 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4514 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4515 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4516 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 4517 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 4518 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4519 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 4520 | bool VisitMemberExpr(const MemberExpr *E); |
| 4521 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 4522 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4523 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 4524 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4525 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 4526 | bool VisitUnaryDeref(const UnaryOperator *E); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 4527 | bool VisitUnaryReal(const UnaryOperator *E); |
| 4528 | bool VisitUnaryImag(const UnaryOperator *E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4529 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
| 4530 | return VisitUnaryPreIncDec(UO); |
| 4531 | } |
| 4532 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
| 4533 | return VisitUnaryPreIncDec(UO); |
| 4534 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4535 | bool VisitBinAssign(const BinaryOperator *BO); |
| 4536 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4537 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4538 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4539 | switch (E->getCastKind()) { |
| 4540 | default: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4541 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4542 | |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 4543 | case CK_LValueBitCast: |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4544 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4545 | if (!Visit(E->getSubExpr())) |
| 4546 | return false; |
| 4547 | Result.Designator.setInvalid(); |
| 4548 | return true; |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 4549 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4550 | case CK_BaseToDerived: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4551 | if (!Visit(E->getSubExpr())) |
| 4552 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4553 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4554 | } |
| 4555 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4556 | }; |
| 4557 | } // end anonymous namespace |
| 4558 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4559 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
Richard Smith | 9f8400e | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 4560 | /// expressions which are not glvalues, in two cases: |
| 4561 | /// * function designators in C, and |
| 4562 | /// * "extern void" objects |
| 4563 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info) { |
| 4564 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
| 4565 | E->getType()->isVoidType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4566 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4569 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 4570 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4571 | return Success(FD); |
| 4572 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4573 | return VisitVarDecl(E, VD); |
| 4574 | return Error(E); |
| 4575 | } |
Richard Smith | 733237d | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 4576 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4577 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4578 | CallStackFrame *Frame = nullptr; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4579 | if (VD->hasLocalStorage() && Info.CurrentCall->Index > 1) |
| 4580 | Frame = Info.CurrentCall; |
| 4581 | |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4582 | if (!VD->getType()->isReferenceType()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4583 | if (Frame) { |
| 4584 | Result.set(VD, Frame->Index); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4585 | return true; |
| 4586 | } |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4587 | return Success(VD); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4588 | } |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 4589 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4590 | APValue *V; |
| 4591 | if (!evaluateVarDeclInit(Info, E, VD, Frame, V)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4592 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4593 | if (V->isUninit()) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4594 | if (!Info.checkingPotentialConstantExpression()) |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4595 | Info.Diag(E, diag::note_constexpr_use_uninit_reference); |
| 4596 | return false; |
| 4597 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4598 | return Success(*V, E); |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 4601 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 4602 | const MaterializeTemporaryExpr *E) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4603 | // Walk through the expression to find the materialized temporary itself. |
| 4604 | SmallVector<const Expr *, 2> CommaLHSs; |
| 4605 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 4606 | const Expr *Inner = E->GetTemporaryExpr()-> |
| 4607 | skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4608 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4609 | // If we passed any comma operators, evaluate their LHSs. |
| 4610 | for (unsigned I = 0, N = CommaLHSs.size(); I != N; ++I) |
| 4611 | if (!EvaluateIgnoredValue(Info, CommaLHSs[I])) |
| 4612 | return false; |
| 4613 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4614 | // A materialized temporary with static storage duration can appear within the |
| 4615 | // result of a constant expression evaluation, so we need to preserve its |
| 4616 | // value for use outside this evaluation. |
| 4617 | APValue *Value; |
| 4618 | if (E->getStorageDuration() == SD_Static) { |
| 4619 | Value = Info.Ctx.getMaterializedTemporaryValue(E, true); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4620 | *Value = APValue(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4621 | Result.set(E); |
| 4622 | } else { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4623 | Value = &Info.CurrentCall-> |
| 4624 | createTemporary(E, E->getStorageDuration() == SD_Automatic); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4625 | Result.set(E, Info.CurrentCall->Index); |
| 4626 | } |
| 4627 | |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 4628 | QualType Type = Inner->getType(); |
| 4629 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4630 | // Materialize the temporary itself. |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 4631 | if (!EvaluateInPlace(*Value, Info, Result, Inner) || |
| 4632 | (E->getStorageDuration() == SD_Static && |
| 4633 | !CheckConstantExpression(Info, E->getExprLoc(), Type, *Value))) { |
| 4634 | *Value = APValue(); |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4635 | return false; |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 4636 | } |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4637 | |
| 4638 | // Adjust our lvalue to refer to the desired subobject. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4639 | for (unsigned I = Adjustments.size(); I != 0; /**/) { |
| 4640 | --I; |
| 4641 | switch (Adjustments[I].Kind) { |
| 4642 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
| 4643 | if (!HandleLValueBasePath(Info, Adjustments[I].DerivedToBase.BasePath, |
| 4644 | Type, Result)) |
| 4645 | return false; |
| 4646 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
| 4647 | break; |
| 4648 | |
| 4649 | case SubobjectAdjustment::FieldAdjustment: |
| 4650 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
| 4651 | return false; |
| 4652 | Type = Adjustments[I].Field->getType(); |
| 4653 | break; |
| 4654 | |
| 4655 | case SubobjectAdjustment::MemberPointerAdjustment: |
| 4656 | if (!HandleMemberPointerAccess(this->Info, Type, Result, |
| 4657 | Adjustments[I].Ptr.RHS)) |
| 4658 | return false; |
| 4659 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
| 4660 | break; |
| 4661 | } |
| 4662 | } |
| 4663 | |
| 4664 | return true; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 4665 | } |
| 4666 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4667 | bool |
| 4668 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4669 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 4670 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 4671 | // only see this when folding in C, so there's no standard to follow here. |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4672 | return Success(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4673 | } |
| 4674 | |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4675 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 4676 | if (!E->isPotentiallyEvaluated()) |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4677 | return Success(E); |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 4678 | |
| 4679 | Info.Diag(E, diag::note_constexpr_typeid_polymorphic) |
| 4680 | << E->getExprOperand()->getType() |
| 4681 | << E->getExprOperand()->getSourceRange(); |
| 4682 | return false; |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 4685 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 4686 | return Success(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4687 | } |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 4688 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4689 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4690 | // Handle static data members. |
| 4691 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 4692 | VisitIgnoredValue(E->getBase()); |
| 4693 | return VisitVarDecl(E, VD); |
| 4694 | } |
| 4695 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4696 | // Handle static member functions. |
| 4697 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 4698 | if (MD->isStatic()) { |
| 4699 | VisitIgnoredValue(E->getBase()); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4700 | return Success(MD); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4701 | } |
| 4702 | } |
| 4703 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4704 | // Handle non-static data members. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4705 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4706 | } |
| 4707 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4708 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4709 | // FIXME: Deal with vectors as array subscript bases. |
| 4710 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4711 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4712 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4713 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4714 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4715 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4716 | APSInt Index; |
| 4717 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4718 | return false; |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4719 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 4720 | return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), |
| 4721 | getExtValue(Index)); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4722 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4723 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4724 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4725 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 4726 | } |
| 4727 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 4728 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 4729 | if (!Visit(E->getSubExpr())) |
| 4730 | return false; |
| 4731 | // __real is a no-op on scalar lvalues. |
| 4732 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
| 4733 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
| 4734 | return true; |
| 4735 | } |
| 4736 | |
| 4737 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 4738 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
| 4739 | "lvalue __imag__ on scalar?"); |
| 4740 | if (!Visit(E->getSubExpr())) |
| 4741 | return false; |
| 4742 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
| 4743 | return true; |
| 4744 | } |
| 4745 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4746 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 4747 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4748 | return Error(UO); |
| 4749 | |
| 4750 | if (!this->Visit(UO->getSubExpr())) |
| 4751 | return false; |
| 4752 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4753 | return handleIncDec( |
| 4754 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4755 | UO->isIncrementOp(), nullptr); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
| 4759 | const CompoundAssignOperator *CAO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 4760 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4761 | return Error(CAO); |
| 4762 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4763 | APValue RHS; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4764 | |
| 4765 | // The overall lvalue result is the result of evaluating the LHS. |
| 4766 | if (!this->Visit(CAO->getLHS())) { |
| 4767 | if (Info.keepEvaluatingAfterFailure()) |
| 4768 | Evaluate(RHS, this->Info, CAO->getRHS()); |
| 4769 | return false; |
| 4770 | } |
| 4771 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4772 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) |
| 4773 | return false; |
| 4774 | |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 4775 | return handleCompoundAssignment( |
| 4776 | this->Info, CAO, |
| 4777 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
| 4778 | CAO->getOpForCompoundAssignment(CAO->getOpcode()), RHS); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4779 | } |
| 4780 | |
| 4781 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 4782 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4783 | return Error(E); |
| 4784 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4785 | APValue NewVal; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4786 | |
| 4787 | if (!this->Visit(E->getLHS())) { |
| 4788 | if (Info.keepEvaluatingAfterFailure()) |
| 4789 | Evaluate(NewVal, this->Info, E->getRHS()); |
| 4790 | return false; |
| 4791 | } |
| 4792 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4793 | if (!Evaluate(NewVal, this->Info, E->getRHS())) |
| 4794 | return false; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4795 | |
| 4796 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4797 | NewVal); |
| 4798 | } |
| 4799 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4800 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4801 | // Pointer Evaluation |
| 4802 | //===----------------------------------------------------------------------===// |
| 4803 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 4804 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4805 | class PointerExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4806 | : public ExprEvaluatorBase<PointerExprEvaluator> { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4807 | LValue &Result; |
| 4808 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4809 | bool Success(const Expr *E) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4810 | Result.set(E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4811 | return true; |
| 4812 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 4813 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4814 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4815 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4816 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4817 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4818 | bool Success(const APValue &V, const Expr *E) { |
| 4819 | Result.setFrom(Info.Ctx, V); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4820 | return true; |
| 4821 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4822 | bool ZeroInitialization(const Expr *E) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4823 | return Success((Expr*)nullptr); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4824 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 4825 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4826 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4827 | bool VisitCastExpr(const CastExpr* E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4828 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4829 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4830 | { return Success(E); } |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 4831 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 4832 | { return Success(E); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4833 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4834 | { return Success(E); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4835 | bool VisitCallExpr(const CallExpr *E); |
| 4836 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4837 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4838 | return Success(E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4839 | return Error(E); |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 4840 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4841 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4842 | // Can't look at 'this' when checking a potential constant expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4843 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4844 | return false; |
Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 4845 | if (!Info.CurrentCall->This) { |
| 4846 | if (Info.getLangOpts().CPlusPlus11) |
| 4847 | Info.Diag(E, diag::note_constexpr_this) << E->isImplicit(); |
| 4848 | else |
| 4849 | Info.Diag(E); |
| 4850 | return false; |
| 4851 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4852 | Result = *Info.CurrentCall->This; |
| 4853 | return true; |
| 4854 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4855 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 4856 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4857 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4858 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4859 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4860 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4861 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4862 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4865 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4866 | if (E->getOpcode() != BO_Add && |
| 4867 | E->getOpcode() != BO_Sub) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4868 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4869 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4870 | const Expr *PExp = E->getLHS(); |
| 4871 | const Expr *IExp = E->getRHS(); |
| 4872 | if (IExp->getType()->isPointerType()) |
| 4873 | std::swap(PExp, IExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4874 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4875 | bool EvalPtrOK = EvaluatePointer(PExp, Result, Info); |
| 4876 | if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4877 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4878 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4879 | llvm::APSInt Offset; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4880 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4881 | return false; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 4882 | |
| 4883 | int64_t AdditionalOffset = getExtValue(Offset); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4884 | if (E->getOpcode() == BO_Sub) |
| 4885 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4886 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 4887 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4888 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, |
| 4889 | AdditionalOffset); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4890 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4891 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4892 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 4893 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4894 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4895 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4896 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 4897 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4898 | |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 4899 | switch (E->getCastKind()) { |
| 4900 | default: |
| 4901 | break; |
| 4902 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4903 | case CK_BitCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4904 | case CK_CPointerToObjCPointerCast: |
| 4905 | case CK_BlockPointerToObjCPointerCast: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4906 | case CK_AnyPointerToBlockPointerCast: |
Anastasia Stulova | 5d8ad8a | 2014-11-26 15:36:41 +0000 | [diff] [blame] | 4907 | case CK_AddressSpaceConversion: |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 4908 | if (!Visit(SubExpr)) |
| 4909 | return false; |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4910 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 4911 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 4912 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 4913 | if (!E->getType()->isVoidPointerType()) { |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 4914 | Result.Designator.setInvalid(); |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 4915 | if (SubExpr->getType()->isVoidPointerType()) |
| 4916 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 4917 | << 3 << SubExpr->getType(); |
| 4918 | else |
| 4919 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 4920 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4921 | return true; |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 4922 | |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4923 | case CK_DerivedToBase: |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4924 | case CK_UncheckedDerivedToBase: |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4925 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4926 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4927 | if (!Result.Base && Result.Offset.isZero()) |
| 4928 | return true; |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4929 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4930 | // Now figure out the necessary offset to add to the base LV to get from |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4931 | // the derived class to the base class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4932 | return HandleLValueBasePath(Info, E, E->getSubExpr()->getType()-> |
| 4933 | castAs<PointerType>()->getPointeeType(), |
| 4934 | Result); |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4935 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4936 | case CK_BaseToDerived: |
| 4937 | if (!Visit(E->getSubExpr())) |
| 4938 | return false; |
| 4939 | if (!Result.Base && Result.Offset.isZero()) |
| 4940 | return true; |
| 4941 | return HandleBaseToDerivedCast(Info, E, Result); |
| 4942 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4943 | case CK_NullToPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 4944 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4945 | return ZeroInitialization(E); |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4946 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4947 | case CK_IntegralToPointer: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4948 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 4949 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4950 | APValue Value; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4951 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 4952 | break; |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 4953 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4954 | if (Value.isInt()) { |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4955 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 4956 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4957 | Result.Base = (Expr*)nullptr; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 4958 | Result.InvalidBase = false; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4959 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4960 | Result.CallIndex = 0; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4961 | Result.Designator.setInvalid(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4962 | return true; |
| 4963 | } else { |
| 4964 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4965 | Result.setFrom(Info.Ctx, Value); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4966 | return true; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4967 | } |
| 4968 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4969 | case CK_ArrayToPointerDecay: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4970 | if (SubExpr->isGLValue()) { |
| 4971 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 4972 | return false; |
| 4973 | } else { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4974 | Result.set(SubExpr, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4975 | if (!EvaluateInPlace(Info.CurrentCall->createTemporary(SubExpr, false), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4976 | Info, Result, SubExpr)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4977 | return false; |
| 4978 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4979 | // The result is a pointer to the first element of the array. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4980 | if (const ConstantArrayType *CAT |
| 4981 | = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) |
| 4982 | Result.addArray(Info, E, CAT); |
| 4983 | else |
| 4984 | Result.Designator.setInvalid(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4985 | return true; |
Richard Smith | dd78544 | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 4986 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4987 | case CK_FunctionToPointerDecay: |
Richard Smith | dd78544 | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 4988 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4989 | } |
| 4990 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4991 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4992 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4993 | |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 4994 | static CharUnits GetAlignOfType(EvalInfo &Info, QualType T) { |
| 4995 | // C++ [expr.alignof]p3: |
| 4996 | // When alignof is applied to a reference type, the result is the |
| 4997 | // alignment of the referenced type. |
| 4998 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 4999 | T = Ref->getPointeeType(); |
| 5000 | |
| 5001 | // __alignof is defined to return the preferred alignment. |
| 5002 | return Info.Ctx.toCharUnitsFromBits( |
| 5003 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
| 5004 | } |
| 5005 | |
| 5006 | static CharUnits GetAlignOfExpr(EvalInfo &Info, const Expr *E) { |
| 5007 | E = E->IgnoreParens(); |
| 5008 | |
| 5009 | // The kinds of expressions that we have special-case logic here for |
| 5010 | // should be kept up to date with the special checks for those |
| 5011 | // expressions in Sema. |
| 5012 | |
| 5013 | // alignof decl is always accepted, even if it doesn't make sense: we default |
| 5014 | // to 1 in those cases. |
| 5015 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 5016 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 5017 | /*RefAsPointee*/true); |
| 5018 | |
| 5019 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 5020 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 5021 | /*RefAsPointee*/true); |
| 5022 | |
| 5023 | return GetAlignOfType(Info, E->getType()); |
| 5024 | } |
| 5025 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5026 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5027 | if (IsStringLiteralCall(E)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5028 | return Success(E); |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 5029 | |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 5030 | switch (E->getBuiltinCallee()) { |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 5031 | case Builtin::BI__builtin_addressof: |
| 5032 | return EvaluateLValue(E->getArg(0), Result, Info); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5033 | case Builtin::BI__builtin_assume_aligned: { |
| 5034 | // We need to be very careful here because: if the pointer does not have the |
| 5035 | // asserted alignment, then the behavior is undefined, and undefined |
| 5036 | // behavior is non-constant. |
| 5037 | if (!EvaluatePointer(E->getArg(0), Result, Info)) |
| 5038 | return false; |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 5039 | |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5040 | LValue OffsetResult(Result); |
| 5041 | APSInt Alignment; |
| 5042 | if (!EvaluateInteger(E->getArg(1), Alignment, Info)) |
| 5043 | return false; |
| 5044 | CharUnits Align = CharUnits::fromQuantity(getExtValue(Alignment)); |
| 5045 | |
| 5046 | if (E->getNumArgs() > 2) { |
| 5047 | APSInt Offset; |
| 5048 | if (!EvaluateInteger(E->getArg(2), Offset, Info)) |
| 5049 | return false; |
| 5050 | |
| 5051 | int64_t AdditionalOffset = -getExtValue(Offset); |
| 5052 | OffsetResult.Offset += CharUnits::fromQuantity(AdditionalOffset); |
| 5053 | } |
| 5054 | |
| 5055 | // If there is a base object, then it must have the correct alignment. |
| 5056 | if (OffsetResult.Base) { |
| 5057 | CharUnits BaseAlignment; |
| 5058 | if (const ValueDecl *VD = |
| 5059 | OffsetResult.Base.dyn_cast<const ValueDecl*>()) { |
| 5060 | BaseAlignment = Info.Ctx.getDeclAlign(VD); |
| 5061 | } else { |
| 5062 | BaseAlignment = |
| 5063 | GetAlignOfExpr(Info, OffsetResult.Base.get<const Expr*>()); |
| 5064 | } |
| 5065 | |
| 5066 | if (BaseAlignment < Align) { |
| 5067 | Result.Designator.setInvalid(); |
| 5068 | // FIXME: Quantities here cast to integers because the plural modifier |
| 5069 | // does not work on APSInts yet. |
| 5070 | CCEDiag(E->getArg(0), |
| 5071 | diag::note_constexpr_baa_insufficient_alignment) << 0 |
| 5072 | << (int) BaseAlignment.getQuantity() |
| 5073 | << (unsigned) getExtValue(Alignment); |
| 5074 | return false; |
| 5075 | } |
| 5076 | } |
| 5077 | |
| 5078 | // The offset must also have the correct alignment. |
| 5079 | if (OffsetResult.Offset.RoundUpToAlignment(Align) != OffsetResult.Offset) { |
| 5080 | Result.Designator.setInvalid(); |
| 5081 | APSInt Offset(64, false); |
| 5082 | Offset = OffsetResult.Offset.getQuantity(); |
| 5083 | |
| 5084 | if (OffsetResult.Base) |
| 5085 | CCEDiag(E->getArg(0), |
| 5086 | diag::note_constexpr_baa_insufficient_alignment) << 1 |
| 5087 | << (int) getExtValue(Offset) << (unsigned) getExtValue(Alignment); |
| 5088 | else |
| 5089 | CCEDiag(E->getArg(0), |
| 5090 | diag::note_constexpr_baa_value_insufficient_alignment) |
| 5091 | << Offset << (unsigned) getExtValue(Alignment); |
| 5092 | |
| 5093 | return false; |
| 5094 | } |
| 5095 | |
| 5096 | return true; |
| 5097 | } |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 5098 | default: |
| 5099 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 5100 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5101 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5102 | |
| 5103 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5104 | // Member Pointer Evaluation |
| 5105 | //===----------------------------------------------------------------------===// |
| 5106 | |
| 5107 | namespace { |
| 5108 | class MemberPointerExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5109 | : public ExprEvaluatorBase<MemberPointerExprEvaluator> { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5110 | MemberPtr &Result; |
| 5111 | |
| 5112 | bool Success(const ValueDecl *D) { |
| 5113 | Result = MemberPtr(D); |
| 5114 | return true; |
| 5115 | } |
| 5116 | public: |
| 5117 | |
| 5118 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 5119 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 5120 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5121 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5122 | Result.setFrom(V); |
| 5123 | return true; |
| 5124 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5125 | bool ZeroInitialization(const Expr *E) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5126 | return Success((const ValueDecl*)nullptr); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | bool VisitCastExpr(const CastExpr *E); |
| 5130 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 5131 | }; |
| 5132 | } // end anonymous namespace |
| 5133 | |
| 5134 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 5135 | EvalInfo &Info) { |
| 5136 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 5137 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 5138 | } |
| 5139 | |
| 5140 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 5141 | switch (E->getCastKind()) { |
| 5142 | default: |
| 5143 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5144 | |
| 5145 | case CK_NullToMemberPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 5146 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5147 | return ZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5148 | |
| 5149 | case CK_BaseToDerivedMemberPointer: { |
| 5150 | if (!Visit(E->getSubExpr())) |
| 5151 | return false; |
| 5152 | if (E->path_empty()) |
| 5153 | return true; |
| 5154 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 5155 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 5156 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 5157 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 5158 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 5159 | PathI != PathE; ++PathI) { |
| 5160 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 5161 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 5162 | if (!Result.castToDerived(Derived)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5163 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5164 | } |
| 5165 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 5166 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5167 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5168 | return true; |
| 5169 | } |
| 5170 | |
| 5171 | case CK_DerivedToBaseMemberPointer: |
| 5172 | if (!Visit(E->getSubExpr())) |
| 5173 | return false; |
| 5174 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 5175 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 5176 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 5177 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 5178 | if (!Result.castToBase(Base)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5179 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5180 | } |
| 5181 | return true; |
| 5182 | } |
| 5183 | } |
| 5184 | |
| 5185 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 5186 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 5187 | // member can be formed. |
| 5188 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 5189 | } |
| 5190 | |
| 5191 | //===----------------------------------------------------------------------===// |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5192 | // Record Evaluation |
| 5193 | //===----------------------------------------------------------------------===// |
| 5194 | |
| 5195 | namespace { |
| 5196 | class RecordExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5197 | : public ExprEvaluatorBase<RecordExprEvaluator> { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5198 | const LValue &This; |
| 5199 | APValue &Result; |
| 5200 | public: |
| 5201 | |
| 5202 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 5203 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 5204 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5205 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5206 | Result = V; |
| 5207 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5208 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5209 | bool ZeroInitialization(const Expr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5210 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 5211 | bool VisitCallExpr(const CallExpr *E) { |
| 5212 | return handleCallExpr(E, Result, &This); |
| 5213 | } |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 5214 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5215 | bool VisitInitListExpr(const InitListExpr *E); |
| 5216 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5217 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5218 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5219 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5220 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5221 | /// Perform zero-initialization on an object of non-union class type. |
| 5222 | /// C++11 [dcl.init]p5: |
| 5223 | /// To zero-initialize an object or reference of type T means: |
| 5224 | /// [...] |
| 5225 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 5226 | /// each non-static data member and each base-class subobject is |
| 5227 | /// zero-initialized |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5228 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 5229 | const RecordDecl *RD, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5230 | const LValue &This, APValue &Result) { |
| 5231 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 5232 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 5233 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 5234 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5235 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5236 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5237 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 5238 | |
| 5239 | if (CD) { |
| 5240 | unsigned Index = 0; |
| 5241 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5242 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5243 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 5244 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5245 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
| 5246 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5247 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5248 | Result.getStructBase(Index))) |
| 5249 | return false; |
| 5250 | } |
| 5251 | } |
| 5252 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5253 | for (const auto *I : RD->fields()) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5254 | // -- if T is a reference type, no initialization is performed. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5255 | if (I->getType()->isReferenceType()) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5256 | continue; |
| 5257 | |
| 5258 | LValue Subobject = This; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5259 | if (!HandleLValueMember(Info, E, Subobject, I, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5260 | return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5261 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5262 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5263 | if (!EvaluateInPlace( |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5264 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5265 | return false; |
| 5266 | } |
| 5267 | |
| 5268 | return true; |
| 5269 | } |
| 5270 | |
| 5271 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E) { |
| 5272 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 5273 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5274 | if (RD->isUnion()) { |
| 5275 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 5276 | // object's first non-static named data member is zero-initialized |
| 5277 | RecordDecl::field_iterator I = RD->field_begin(); |
| 5278 | if (I == RD->field_end()) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5279 | Result = APValue((const FieldDecl*)nullptr); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5280 | return true; |
| 5281 | } |
| 5282 | |
| 5283 | LValue Subobject = This; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5284 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5285 | return false; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5286 | Result = APValue(*I); |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5287 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5288 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5289 | } |
| 5290 | |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 5291 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 5292 | Info.Diag(E, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 5293 | return false; |
| 5294 | } |
| 5295 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5296 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5297 | } |
| 5298 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 5299 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 5300 | switch (E->getCastKind()) { |
| 5301 | default: |
| 5302 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5303 | |
| 5304 | case CK_ConstructorConversion: |
| 5305 | return Visit(E->getSubExpr()); |
| 5306 | |
| 5307 | case CK_DerivedToBase: |
| 5308 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5309 | APValue DerivedObject; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5310 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 5311 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5312 | if (!DerivedObject.isStruct()) |
| 5313 | return Error(E->getSubExpr()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 5314 | |
| 5315 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 5316 | APValue *Value = &DerivedObject; |
| 5317 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 5318 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 5319 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 5320 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 5321 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 5322 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 5323 | RD = Base; |
| 5324 | } |
| 5325 | Result = *Value; |
| 5326 | return true; |
| 5327 | } |
| 5328 | } |
| 5329 | } |
| 5330 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5331 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 5332 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 5333 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5334 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 5335 | |
| 5336 | if (RD->isUnion()) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5337 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 5338 | Result = APValue(Field); |
| 5339 | if (!Field) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5340 | return true; |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5341 | |
| 5342 | // If the initializer list for a union does not contain any elements, the |
| 5343 | // first element of the union is value-initialized. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 5344 | // FIXME: The element should be initialized from an initializer list. |
| 5345 | // Is this difference ever observable for initializer lists which |
| 5346 | // we don't build? |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5347 | ImplicitValueInitExpr VIE(Field->getType()); |
| 5348 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 5349 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5350 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5351 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
| 5352 | return false; |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 5353 | |
| 5354 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 5355 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 5356 | isa<CXXDefaultInitExpr>(InitExpr)); |
| 5357 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5358 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5359 | } |
| 5360 | |
| 5361 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 5362 | "initializer list for class with base classes"); |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 5363 | Result = APValue(APValue::UninitStruct(), 0, |
| 5364 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5365 | unsigned ElementNo = 0; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5366 | bool Success = true; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5367 | for (const auto *Field : RD->fields()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5368 | // Anonymous bit-fields are not considered members of the class for |
| 5369 | // purposes of aggregate initialization. |
| 5370 | if (Field->isUnnamedBitfield()) |
| 5371 | continue; |
| 5372 | |
| 5373 | LValue Subobject = This; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5374 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5375 | bool HaveInit = ElementNo < E->getNumInits(); |
| 5376 | |
| 5377 | // FIXME: Diagnostics here should point to the end of the initializer |
| 5378 | // list, not the start. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5379 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5380 | Subobject, Field, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5381 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5382 | |
| 5383 | // Perform an implicit value-initialization for members beyond the end of |
| 5384 | // the initializer list. |
| 5385 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 5386 | const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5387 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 5388 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 5389 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 5390 | isa<CXXDefaultInitExpr>(Init)); |
| 5391 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 5392 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
| 5393 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init) || |
| 5394 | (Field->isBitField() && !truncateBitfieldValue(Info, Init, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5395 | FieldVal, Field))) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5396 | if (!Info.keepEvaluatingAfterFailure()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5397 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5398 | Success = false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5399 | } |
| 5400 | } |
| 5401 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5402 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5403 | } |
| 5404 | |
| 5405 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 5406 | const CXXConstructorDecl *FD = E->getConstructor(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 5407 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
| 5408 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5409 | bool ZeroInit = E->requiresZeroInitialization(); |
| 5410 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5411 | // If we've already performed zero-initialization, we're already done. |
| 5412 | if (!Result.isUninit()) |
| 5413 | return true; |
| 5414 | |
Richard Smith | da3f4fd | 2014-03-05 23:32:50 +0000 | [diff] [blame] | 5415 | // We can get here in two different ways: |
| 5416 | // 1) We're performing value-initialization, and should zero-initialize |
| 5417 | // the object, or |
| 5418 | // 2) We're performing default-initialization of an object with a trivial |
| 5419 | // constexpr default constructor, in which case we should start the |
| 5420 | // lifetimes of all the base subobjects (there can be no data member |
| 5421 | // subobjects in this case) per [basic.life]p1. |
| 5422 | // Either way, ZeroInitialization is appropriate. |
| 5423 | return ZeroInitialization(E); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5424 | } |
| 5425 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5426 | const FunctionDecl *Definition = nullptr; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5427 | FD->getBody(Definition); |
| 5428 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5429 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 5430 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5431 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 5432 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5433 | if (E->isElidable() && !ZeroInit) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5434 | if (const MaterializeTemporaryExpr *ME |
| 5435 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 5436 | return Visit(ME->GetTemporaryExpr()); |
| 5437 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5438 | if (ZeroInit && !ZeroInitialization(E)) |
| 5439 | return false; |
| 5440 | |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 5441 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5442 | return HandleConstructorCall(E->getExprLoc(), This, Args, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5443 | cast<CXXConstructorDecl>(Definition), Info, |
| 5444 | Result); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5445 | } |
| 5446 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5447 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
| 5448 | const CXXStdInitializerListExpr *E) { |
| 5449 | const ConstantArrayType *ArrayType = |
| 5450 | Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); |
| 5451 | |
| 5452 | LValue Array; |
| 5453 | if (!EvaluateLValue(E->getSubExpr(), Array, Info)) |
| 5454 | return false; |
| 5455 | |
| 5456 | // Get a pointer to the first element of the array. |
| 5457 | Array.addArray(Info, E, ArrayType); |
| 5458 | |
| 5459 | // FIXME: Perform the checks on the field types in SemaInit. |
| 5460 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
| 5461 | RecordDecl::field_iterator Field = Record->field_begin(); |
| 5462 | if (Field == Record->field_end()) |
| 5463 | return Error(E); |
| 5464 | |
| 5465 | // Start pointer. |
| 5466 | if (!Field->getType()->isPointerType() || |
| 5467 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 5468 | ArrayType->getElementType())) |
| 5469 | return Error(E); |
| 5470 | |
| 5471 | // FIXME: What if the initializer_list type has base classes, etc? |
| 5472 | Result = APValue(APValue::UninitStruct(), 0, 2); |
| 5473 | Array.moveInto(Result.getStructField(0)); |
| 5474 | |
| 5475 | if (++Field == Record->field_end()) |
| 5476 | return Error(E); |
| 5477 | |
| 5478 | if (Field->getType()->isPointerType() && |
| 5479 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 5480 | ArrayType->getElementType())) { |
| 5481 | // End pointer. |
| 5482 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
| 5483 | ArrayType->getElementType(), |
| 5484 | ArrayType->getSize().getZExtValue())) |
| 5485 | return false; |
| 5486 | Array.moveInto(Result.getStructField(1)); |
| 5487 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
| 5488 | // Length. |
| 5489 | Result.getStructField(1) = APValue(APSInt(ArrayType->getSize())); |
| 5490 | else |
| 5491 | return Error(E); |
| 5492 | |
| 5493 | if (++Field != Record->field_end()) |
| 5494 | return Error(E); |
| 5495 | |
| 5496 | return true; |
| 5497 | } |
| 5498 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5499 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 5500 | APValue &Result, EvalInfo &Info) { |
| 5501 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5502 | "can't evaluate expression as a record rvalue"); |
| 5503 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 5504 | } |
| 5505 | |
| 5506 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5507 | // Temporary Evaluation |
| 5508 | // |
| 5509 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 5510 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 5511 | // materialized so that a reference can bind to it. |
| 5512 | //===----------------------------------------------------------------------===// |
| 5513 | namespace { |
| 5514 | class TemporaryExprEvaluator |
| 5515 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 5516 | public: |
| 5517 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 5518 | LValueExprEvaluatorBaseTy(Info, Result) {} |
| 5519 | |
| 5520 | /// Visit an expression which constructs the value of this temporary. |
| 5521 | bool VisitConstructExpr(const Expr *E) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5522 | Result.set(E, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5523 | return EvaluateInPlace(Info.CurrentCall->createTemporary(E, false), |
| 5524 | Info, Result, E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5525 | } |
| 5526 | |
| 5527 | bool VisitCastExpr(const CastExpr *E) { |
| 5528 | switch (E->getCastKind()) { |
| 5529 | default: |
| 5530 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5531 | |
| 5532 | case CK_ConstructorConversion: |
| 5533 | return VisitConstructExpr(E->getSubExpr()); |
| 5534 | } |
| 5535 | } |
| 5536 | bool VisitInitListExpr(const InitListExpr *E) { |
| 5537 | return VisitConstructExpr(E); |
| 5538 | } |
| 5539 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 5540 | return VisitConstructExpr(E); |
| 5541 | } |
| 5542 | bool VisitCallExpr(const CallExpr *E) { |
| 5543 | return VisitConstructExpr(E); |
| 5544 | } |
Richard Smith | 513955c | 2014-12-17 19:24:30 +0000 | [diff] [blame] | 5545 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) { |
| 5546 | return VisitConstructExpr(E); |
| 5547 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5548 | }; |
| 5549 | } // end anonymous namespace |
| 5550 | |
| 5551 | /// Evaluate an expression of record type as a temporary. |
| 5552 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 5553 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5554 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 5555 | } |
| 5556 | |
| 5557 | //===----------------------------------------------------------------------===// |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5558 | // Vector Evaluation |
| 5559 | //===----------------------------------------------------------------------===// |
| 5560 | |
| 5561 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5562 | class VectorExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5563 | : public ExprEvaluatorBase<VectorExprEvaluator> { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5564 | APValue &Result; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5565 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5566 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5567 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 5568 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5569 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5570 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 5571 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 5572 | // FIXME: remove this APValue copy. |
| 5573 | Result = APValue(V.data(), V.size()); |
| 5574 | return true; |
| 5575 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5576 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5577 | assert(V.isVector()); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5578 | Result = V; |
| 5579 | return true; |
| 5580 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5581 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5582 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5583 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5584 | { return Visit(E->getSubExpr()); } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5585 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5586 | bool VisitInitListExpr(const InitListExpr *E); |
| 5587 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5588 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 5589 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5590 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5591 | }; |
| 5592 | } // end anonymous namespace |
| 5593 | |
| 5594 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5595 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5596 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5599 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 5600 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 5601 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5602 | |
Richard Smith | 161f09a | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 5603 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 5604 | QualType SETy = SE->getType(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5605 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5606 | switch (E->getCastKind()) { |
| 5607 | case CK_VectorSplat: { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5608 | APValue Val = APValue(); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5609 | if (SETy->isIntegerType()) { |
| 5610 | APSInt IntResult; |
| 5611 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5612 | return false; |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5613 | Val = APValue(IntResult); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5614 | } else if (SETy->isRealFloatingType()) { |
| 5615 | APFloat F(0.0); |
| 5616 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5617 | return false; |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5618 | Val = APValue(F); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5619 | } else { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5620 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5621 | } |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 5622 | |
| 5623 | // Splat and create vector APValue. |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5624 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 5625 | return Success(Elts, E); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 5626 | } |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 5627 | case CK_BitCast: { |
| 5628 | // Evaluate the operand into an APInt we can extract from. |
| 5629 | llvm::APInt SValInt; |
| 5630 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 5631 | return false; |
| 5632 | // Extract the elements |
| 5633 | QualType EltTy = VTy->getElementType(); |
| 5634 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 5635 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 5636 | SmallVector<APValue, 4> Elts; |
| 5637 | if (EltTy->isRealFloatingType()) { |
| 5638 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 5639 | unsigned FloatEltSize = EltSize; |
| 5640 | if (&Sem == &APFloat::x87DoubleExtended) |
| 5641 | FloatEltSize = 80; |
| 5642 | for (unsigned i = 0; i < NElts; i++) { |
| 5643 | llvm::APInt Elt; |
| 5644 | if (BigEndian) |
| 5645 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 5646 | else |
| 5647 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
Tim Northover | 178723a | 2013-01-22 09:46:51 +0000 | [diff] [blame] | 5648 | Elts.push_back(APValue(APFloat(Sem, Elt))); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 5649 | } |
| 5650 | } else if (EltTy->isIntegerType()) { |
| 5651 | for (unsigned i = 0; i < NElts; i++) { |
| 5652 | llvm::APInt Elt; |
| 5653 | if (BigEndian) |
| 5654 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 5655 | else |
| 5656 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 5657 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 5658 | } |
| 5659 | } else { |
| 5660 | return Error(E); |
| 5661 | } |
| 5662 | return Success(Elts, E); |
| 5663 | } |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5664 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5665 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5666 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5667 | } |
| 5668 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5669 | bool |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5670 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5671 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5672 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5673 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5674 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5675 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5676 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5677 | |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5678 | // The number of initializers can be less than the number of |
| 5679 | // vector elements. For OpenCL, this can be due to nested vector |
| 5680 | // initialization. For GCC compatibility, missing trailing elements |
| 5681 | // should be initialized with zeroes. |
| 5682 | unsigned CountInits = 0, CountElts = 0; |
| 5683 | while (CountElts < NumElements) { |
| 5684 | // Handle nested vector initialization. |
| 5685 | if (CountInits < NumInits |
Eli Friedman | 1409e6e | 2013-09-17 04:07:02 +0000 | [diff] [blame] | 5686 | && E->getInit(CountInits)->getType()->isVectorType()) { |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5687 | APValue v; |
| 5688 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 5689 | return Error(E); |
| 5690 | unsigned vlen = v.getVectorLength(); |
| 5691 | for (unsigned j = 0; j < vlen; j++) |
| 5692 | Elements.push_back(v.getVectorElt(j)); |
| 5693 | CountElts += vlen; |
| 5694 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5695 | llvm::APSInt sInt(32); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5696 | if (CountInits < NumInits) { |
| 5697 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 5698 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5699 | } else // trailing integer zero. |
| 5700 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 5701 | Elements.push_back(APValue(sInt)); |
| 5702 | CountElts++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5703 | } else { |
| 5704 | llvm::APFloat f(0.0); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5705 | if (CountInits < NumInits) { |
| 5706 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 5707 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5708 | } else // trailing float zero. |
| 5709 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 5710 | Elements.push_back(APValue(f)); |
| 5711 | CountElts++; |
John McCall | 875679e | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 5712 | } |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5713 | CountInits++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5714 | } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5715 | return Success(Elements, E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5716 | } |
| 5717 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5718 | bool |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5719 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5720 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5721 | QualType EltTy = VT->getElementType(); |
| 5722 | APValue ZeroElement; |
| 5723 | if (EltTy->isIntegerType()) |
| 5724 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 5725 | else |
| 5726 | ZeroElement = |
| 5727 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 5728 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5729 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5730 | return Success(Elements, E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5731 | } |
| 5732 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5733 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5734 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5735 | return ZeroInitialization(E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5736 | } |
| 5737 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5738 | //===----------------------------------------------------------------------===// |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5739 | // Array Evaluation |
| 5740 | //===----------------------------------------------------------------------===// |
| 5741 | |
| 5742 | namespace { |
| 5743 | class ArrayExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5744 | : public ExprEvaluatorBase<ArrayExprEvaluator> { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5745 | const LValue &This; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5746 | APValue &Result; |
| 5747 | public: |
| 5748 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5749 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 5750 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5751 | |
| 5752 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 5753 | assert((V.isArray() || V.isLValue()) && |
| 5754 | "expected array or string literal"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5755 | Result = V; |
| 5756 | return true; |
| 5757 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5758 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5759 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5760 | const ConstantArrayType *CAT = |
| 5761 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 5762 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5763 | return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5764 | |
| 5765 | Result = APValue(APValue::UninitArray(), 0, |
| 5766 | CAT->getSize().getZExtValue()); |
| 5767 | if (!Result.hasArrayFiller()) return true; |
| 5768 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5769 | // Zero-initialize all elements. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5770 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5771 | Subobject.addArray(Info, E, CAT); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5772 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5773 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5774 | } |
| 5775 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 5776 | bool VisitCallExpr(const CallExpr *E) { |
| 5777 | return handleCallExpr(E, Result, &This); |
| 5778 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5779 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5780 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5781 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 5782 | const LValue &Subobject, |
| 5783 | APValue *Value, QualType Type); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5784 | }; |
| 5785 | } // end anonymous namespace |
| 5786 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5787 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 5788 | APValue &Result, EvalInfo &Info) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5789 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5790 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5791 | } |
| 5792 | |
| 5793 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 5794 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 5795 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5796 | return Error(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5797 | |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 5798 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 5799 | // an appropriately-typed string literal enclosed in braces. |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 5800 | if (E->isStringLiteralInit()) { |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 5801 | LValue LV; |
| 5802 | if (!EvaluateLValue(E->getInit(0), LV, Info)) |
| 5803 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5804 | APValue Val; |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 5805 | LV.moveInto(Val); |
| 5806 | return Success(Val, E); |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 5807 | } |
| 5808 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5809 | bool Success = true; |
| 5810 | |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5811 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
| 5812 | "zero-initialized array shouldn't have any initialized elts"); |
| 5813 | APValue Filler; |
| 5814 | if (Result.isArray() && Result.hasArrayFiller()) |
| 5815 | Filler = Result.getArrayFiller(); |
| 5816 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5817 | unsigned NumEltsToInit = E->getNumInits(); |
| 5818 | unsigned NumElts = CAT->getSize().getZExtValue(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5819 | const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : nullptr; |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5820 | |
| 5821 | // If the initializer might depend on the array index, run it for each |
| 5822 | // array element. For now, just whitelist non-class value-initialization. |
| 5823 | if (NumEltsToInit != NumElts && !isa<ImplicitValueInitExpr>(FillerExpr)) |
| 5824 | NumEltsToInit = NumElts; |
| 5825 | |
| 5826 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5827 | |
| 5828 | // If the array was previously zero-initialized, preserve the |
| 5829 | // zero-initialized values. |
| 5830 | if (!Filler.isUninit()) { |
| 5831 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
| 5832 | Result.getArrayInitializedElt(I) = Filler; |
| 5833 | if (Result.hasArrayFiller()) |
| 5834 | Result.getArrayFiller() = Filler; |
| 5835 | } |
| 5836 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5837 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5838 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5839 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
| 5840 | const Expr *Init = |
| 5841 | Index < E->getNumInits() ? E->getInit(Index) : FillerExpr; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5842 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5843 | Info, Subobject, Init) || |
| 5844 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5845 | CAT->getElementType(), 1)) { |
| 5846 | if (!Info.keepEvaluatingAfterFailure()) |
| 5847 | return false; |
| 5848 | Success = false; |
| 5849 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5850 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5851 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5852 | if (!Result.hasArrayFiller()) |
| 5853 | return Success; |
| 5854 | |
| 5855 | // If we get here, we have a trivial filler, which we can just evaluate |
| 5856 | // once and splat over the rest of the array elements. |
| 5857 | assert(FillerExpr && "no array filler for incomplete init list"); |
| 5858 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, |
| 5859 | FillerExpr) && Success; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5860 | } |
| 5861 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5862 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5863 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
| 5864 | } |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5865 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5866 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 5867 | const LValue &Subobject, |
| 5868 | APValue *Value, |
| 5869 | QualType Type) { |
| 5870 | bool HadZeroInit = !Value->isUninit(); |
| 5871 | |
| 5872 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) { |
| 5873 | unsigned N = CAT->getSize().getZExtValue(); |
| 5874 | |
| 5875 | // Preserve the array filler if we had prior zero-initialization. |
| 5876 | APValue Filler = |
| 5877 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
| 5878 | : APValue(); |
| 5879 | |
| 5880 | *Value = APValue(APValue::UninitArray(), N, N); |
| 5881 | |
| 5882 | if (HadZeroInit) |
| 5883 | for (unsigned I = 0; I != N; ++I) |
| 5884 | Value->getArrayInitializedElt(I) = Filler; |
| 5885 | |
| 5886 | // Initialize the elements. |
| 5887 | LValue ArrayElt = Subobject; |
| 5888 | ArrayElt.addArray(Info, E, CAT); |
| 5889 | for (unsigned I = 0; I != N; ++I) |
| 5890 | if (!VisitCXXConstructExpr(E, ArrayElt, &Value->getArrayInitializedElt(I), |
| 5891 | CAT->getElementType()) || |
| 5892 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
| 5893 | CAT->getElementType(), 1)) |
| 5894 | return false; |
| 5895 | |
| 5896 | return true; |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5897 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5898 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5899 | if (!Type->isRecordType()) |
Richard Smith | 9fce7bc | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 5900 | return Error(E); |
| 5901 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5902 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5903 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5904 | bool ZeroInit = E->requiresZeroInitialization(); |
| 5905 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5906 | if (HadZeroInit) |
| 5907 | return true; |
| 5908 | |
Richard Smith | da3f4fd | 2014-03-05 23:32:50 +0000 | [diff] [blame] | 5909 | // See RecordExprEvaluator::VisitCXXConstructExpr for explanation. |
| 5910 | ImplicitValueInitExpr VIE(Type); |
| 5911 | return EvaluateInPlace(*Value, Info, Subobject, &VIE); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5912 | } |
| 5913 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5914 | const FunctionDecl *Definition = nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5915 | FD->getBody(Definition); |
| 5916 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5917 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 5918 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5919 | |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5920 | if (ZeroInit && !HadZeroInit) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5921 | ImplicitValueInitExpr VIE(Type); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5922 | if (!EvaluateInPlace(*Value, Info, Subobject, &VIE)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5923 | return false; |
| 5924 | } |
| 5925 | |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 5926 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5927 | return HandleConstructorCall(E->getExprLoc(), Subobject, Args, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5928 | cast<CXXConstructorDecl>(Definition), |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5929 | Info, *Value); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5930 | } |
| 5931 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5932 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5933 | // Integer Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5934 | // |
| 5935 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 5936 | // types and back in constant folding. Integer values are thus represented |
| 5937 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5938 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5939 | |
| 5940 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5941 | class IntExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5942 | : public ExprEvaluatorBase<IntExprEvaluator> { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5943 | APValue &Result; |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 5944 | public: |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5945 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5946 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5947 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5948 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5949 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5950 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5951 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5952 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5953 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5954 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5955 | Result = APValue(SI); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5956 | return true; |
| 5957 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5958 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 5959 | return Success(SI, E, Result); |
| 5960 | } |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5961 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5962 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5963 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 5964 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 5965 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5966 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5967 | Result = APValue(APSInt(I)); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5968 | Result.getInt().setIsUnsigned( |
| 5969 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5970 | return true; |
| 5971 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5972 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 5973 | return Success(I, E, Result); |
| 5974 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5975 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5976 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5977 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 5978 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5979 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5980 | return true; |
| 5981 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5982 | bool Success(uint64_t Value, const Expr *E) { |
| 5983 | return Success(Value, E, Result); |
| 5984 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5985 | |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5986 | bool Success(CharUnits Size, const Expr *E) { |
| 5987 | return Success(Size.getQuantity(), E); |
| 5988 | } |
| 5989 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5990 | bool Success(const APValue &V, const Expr *E) { |
Eli Friedman | b1bc368 | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 5991 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 9c8d1c5 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 5992 | Result = V; |
| 5993 | return true; |
| 5994 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5995 | return Success(V.getInt(), E); |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 5996 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5997 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5998 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5999 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6000 | //===--------------------------------------------------------------------===// |
| 6001 | // Visitor Methods |
| 6002 | //===--------------------------------------------------------------------===// |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 6003 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 6004 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6005 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 6006 | } |
| 6007 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6008 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 6009 | } |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 6010 | |
| 6011 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 6012 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6013 | if (CheckReferencedDecl(E, E->getDecl())) |
| 6014 | return true; |
| 6015 | |
| 6016 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 6017 | } |
| 6018 | bool VisitMemberExpr(const MemberExpr *E) { |
| 6019 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6020 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 6021 | return true; |
| 6022 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6023 | |
| 6024 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 6025 | } |
| 6026 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6027 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 6028 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6029 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 6030 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 6031 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6032 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6033 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 6034 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 6035 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6036 | return Success(E->getValue(), E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 6037 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6038 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6039 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 6040 | return Success(E->getValue(), E); |
| 6041 | } |
| 6042 | |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 6043 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 6044 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6045 | return ZeroInitialization(E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 6046 | } |
| 6047 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 6048 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 6049 | return Success(E->getValue(), E); |
| 6050 | } |
| 6051 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 6052 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 6053 | return Success(E->getValue(), E); |
| 6054 | } |
| 6055 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 6056 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 6057 | return Success(E->getValue(), E); |
| 6058 | } |
| 6059 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 6060 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 6061 | bool VisitUnaryImag(const UnaryOperator *E); |
| 6062 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 6063 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 6064 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 6065 | |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 6066 | private: |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6067 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E, unsigned Type); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 6068 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 6069 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 6070 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 6071 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6072 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 6073 | /// produce either the integer value or a pointer. |
| 6074 | /// |
| 6075 | /// GCC has a heinous extension which folds casts between pointer types and |
| 6076 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 6077 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 6078 | /// Some simple arithmetic on such values is supported (they are treated much |
| 6079 | /// like char*). |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6080 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 6081 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6082 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6083 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 6084 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 6085 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6086 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6087 | APValue Val; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6088 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 6089 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6090 | if (!Val.isInt()) { |
| 6091 | // FIXME: It would be better to produce the diagnostic for casting |
| 6092 | // a pointer to an integer. |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6093 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6094 | return false; |
| 6095 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 6096 | Result = Val.getInt(); |
| 6097 | return true; |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 6098 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 6099 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6100 | /// Check whether the given declaration can be directly converted to an integral |
| 6101 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 6102 | /// try. |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 6103 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 6104 | // Enums are integer constant exprs. |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 6105 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 6106 | // Check for signedness/width mismatches between E type and ECD value. |
| 6107 | bool SameSign = (ECD->getInitVal().isSigned() |
| 6108 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 6109 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 6110 | == Info.Ctx.getIntWidth(E->getType())); |
| 6111 | if (SameSign && SameWidth) |
| 6112 | return Success(ECD->getInitVal(), E); |
| 6113 | else { |
| 6114 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 6115 | // by computing a new value matching the type of E. |
| 6116 | llvm::APSInt Val = ECD->getInitVal(); |
| 6117 | if (!SameSign) |
| 6118 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 6119 | if (!SameWidth) |
| 6120 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 6121 | return Success(Val, E); |
| 6122 | } |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 6123 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6124 | return false; |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 6125 | } |
| 6126 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 6127 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 6128 | /// as GCC. |
| 6129 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 6130 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6131 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 6132 | enum gcc_type_class { |
| 6133 | no_type_class = -1, |
| 6134 | void_type_class, integer_type_class, char_type_class, |
| 6135 | enumeral_type_class, boolean_type_class, |
| 6136 | pointer_type_class, reference_type_class, offset_type_class, |
| 6137 | real_type_class, complex_type_class, |
| 6138 | function_type_class, method_type_class, |
| 6139 | record_type_class, union_type_class, |
| 6140 | array_type_class, string_type_class, |
| 6141 | lang_type_class |
| 6142 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6143 | |
| 6144 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 6145 | // ideal, however it is what gcc does. |
| 6146 | if (E->getNumArgs() == 0) |
| 6147 | return no_type_class; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6148 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 6149 | QualType ArgTy = E->getArg(0)->getType(); |
| 6150 | if (ArgTy->isVoidType()) |
| 6151 | return void_type_class; |
| 6152 | else if (ArgTy->isEnumeralType()) |
| 6153 | return enumeral_type_class; |
| 6154 | else if (ArgTy->isBooleanType()) |
| 6155 | return boolean_type_class; |
| 6156 | else if (ArgTy->isCharType()) |
| 6157 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 6158 | else if (ArgTy->isIntegerType()) |
| 6159 | return integer_type_class; |
| 6160 | else if (ArgTy->isPointerType()) |
| 6161 | return pointer_type_class; |
| 6162 | else if (ArgTy->isReferenceType()) |
| 6163 | return reference_type_class; |
| 6164 | else if (ArgTy->isRealType()) |
| 6165 | return real_type_class; |
| 6166 | else if (ArgTy->isComplexType()) |
| 6167 | return complex_type_class; |
| 6168 | else if (ArgTy->isFunctionType()) |
| 6169 | return function_type_class; |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 6170 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 6171 | return record_type_class; |
| 6172 | else if (ArgTy->isUnionType()) |
| 6173 | return union_type_class; |
| 6174 | else if (ArgTy->isArrayType()) |
| 6175 | return array_type_class; |
| 6176 | else if (ArgTy->isUnionType()) |
| 6177 | return union_type_class; |
| 6178 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6179 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 6180 | } |
| 6181 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6182 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 6183 | /// __builtin_constant_p when applied to the given lvalue. |
| 6184 | /// |
| 6185 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 6186 | /// character of a string literal. |
| 6187 | template<typename LValue> |
| 6188 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
Douglas Gregor | f31cee6 | 2012-03-11 02:23:56 +0000 | [diff] [blame] | 6189 | const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>(); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6190 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 6191 | } |
| 6192 | |
| 6193 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 6194 | /// GCC as we can manage. |
| 6195 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 6196 | QualType ArgType = Arg->getType(); |
| 6197 | |
| 6198 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 6199 | // are not precisely documented, but are as follows: |
| 6200 | // |
| 6201 | // - If the operand is of integral, floating, complex or enumeration type, |
| 6202 | // and can be folded to a known value of that type, it returns 1. |
| 6203 | // - If the operand and can be folded to a pointer to the first character |
| 6204 | // of a string literal (or such a pointer cast to an integral type), it |
| 6205 | // returns 1. |
| 6206 | // |
| 6207 | // Otherwise, it returns 0. |
| 6208 | // |
| 6209 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 6210 | // its support for this does not currently work. |
| 6211 | if (ArgType->isIntegralOrEnumerationType()) { |
| 6212 | Expr::EvalResult Result; |
| 6213 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 6214 | return false; |
| 6215 | |
| 6216 | APValue &V = Result.Val; |
| 6217 | if (V.getKind() == APValue::Int) |
| 6218 | return true; |
| 6219 | |
| 6220 | return EvaluateBuiltinConstantPForLValue(V); |
| 6221 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 6222 | return Arg->isEvaluatable(Ctx); |
| 6223 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 6224 | LValue LV; |
| 6225 | Expr::EvalStatus Status; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 6226 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6227 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 6228 | : EvaluatePointer(Arg, LV, Info)) && |
| 6229 | !Status.HasSideEffects) |
| 6230 | return EvaluateBuiltinConstantPForLValue(LV); |
| 6231 | } |
| 6232 | |
| 6233 | // Anything else isn't considered to be sufficiently constant. |
| 6234 | return false; |
| 6235 | } |
| 6236 | |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6237 | /// Retrieves the "underlying object type" of the given expression, |
| 6238 | /// as used by __builtin_object_size. |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6239 | static QualType getObjectType(APValue::LValueBase B) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 6240 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 6241 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6242 | return VD->getType(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 6243 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 6244 | if (isa<CompoundLiteralExpr>(E)) |
| 6245 | return E->getType(); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6246 | } |
| 6247 | |
| 6248 | return QualType(); |
| 6249 | } |
| 6250 | |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 6251 | /// A more selective version of E->IgnoreParenCasts for |
| 6252 | /// TryEvaluateBuiltinObjectSize. This ignores casts/parens that serve only to |
| 6253 | /// change the type of E. |
| 6254 | /// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo` |
| 6255 | /// |
| 6256 | /// Always returns an RValue with a pointer representation. |
| 6257 | static const Expr *ignorePointerCastsAndParens(const Expr *E) { |
| 6258 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
| 6259 | |
| 6260 | auto *NoParens = E->IgnoreParens(); |
| 6261 | auto *Cast = dyn_cast<CastExpr>(NoParens); |
| 6262 | if (Cast == nullptr || Cast->getCastKind() == CK_DerivedToBase) |
| 6263 | return NoParens; |
| 6264 | |
| 6265 | auto *SubExpr = Cast->getSubExpr(); |
| 6266 | if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isRValue()) |
| 6267 | return NoParens; |
| 6268 | return ignorePointerCastsAndParens(SubExpr); |
| 6269 | } |
| 6270 | |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6271 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E, |
| 6272 | unsigned Type) { |
| 6273 | // Determine the denoted object. |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6274 | LValue Base; |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 6275 | { |
| 6276 | // The operand of __builtin_object_size is never evaluated for side-effects. |
| 6277 | // If there are any, but we can determine the pointed-to object anyway, then |
| 6278 | // ignore the side-effects. |
| 6279 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 6280 | FoldOffsetRAII Fold(Info, Type & 1); |
| 6281 | const Expr *Ptr = ignorePointerCastsAndParens(E->getArg(0)); |
| 6282 | if (!EvaluatePointer(Ptr, Base, Info)) |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 6283 | return false; |
| 6284 | } |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6285 | |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6286 | CharUnits BaseOffset = Base.getLValueOffset(); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 6287 | // If we point to before the start of the object, there are no accessible |
| 6288 | // bytes. |
| 6289 | if (BaseOffset.isNegative()) |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6290 | return Success(0, E); |
| 6291 | |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 6292 | // In the case where we're not dealing with a subobject, we discard the |
| 6293 | // subobject bit. |
| 6294 | if (!Base.Designator.Invalid && Base.Designator.Entries.empty()) |
| 6295 | Type = Type & ~1U; |
| 6296 | |
| 6297 | // If Type & 1 is 0, we need to be able to statically guarantee that the bytes |
| 6298 | // exist. If we can't verify the base, then we can't do that. |
| 6299 | // |
| 6300 | // As a special case, we produce a valid object size for an unknown object |
| 6301 | // with a known designator if Type & 1 is 1. For instance: |
| 6302 | // |
| 6303 | // extern struct X { char buff[32]; int a, b, c; } *p; |
| 6304 | // int a = __builtin_object_size(p->buff + 4, 3); // returns 28 |
| 6305 | // int b = __builtin_object_size(p->buff + 4, 2); // returns 0, not 40 |
| 6306 | // |
| 6307 | // This matches GCC's behavior. |
| 6308 | if ((Type & 1) == 0 && Base.InvalidBase) |
Nico Weber | 19999b4 | 2015-08-18 20:32:55 +0000 | [diff] [blame] | 6309 | return Error(E); |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6310 | |
| 6311 | // If Type & 1 is 0, the object in question is the complete object; reset to |
| 6312 | // a complete object designator in that case. |
| 6313 | // |
| 6314 | // If Type is 1 and we've lost track of the subobject, just find the complete |
| 6315 | // object instead. (If Type is 3, that's not correct behavior and we should |
| 6316 | // return 0 instead.) |
| 6317 | LValue End = Base; |
| 6318 | if (((Type & 1) == 0) || (End.Designator.Invalid && Type == 1)) { |
| 6319 | QualType T = getObjectType(End.getLValueBase()); |
| 6320 | if (T.isNull()) |
| 6321 | End.Designator.setInvalid(); |
| 6322 | else { |
| 6323 | End.Designator = SubobjectDesignator(T); |
| 6324 | End.Offset = CharUnits::Zero(); |
| 6325 | } |
Fariborz Jahanian | a3d8879 | 2014-09-22 17:11:59 +0000 | [diff] [blame] | 6326 | } |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6327 | |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6328 | // If it is not possible to determine which objects ptr points to at compile |
| 6329 | // time, __builtin_object_size should return (size_t) -1 for type 0 or 1 |
| 6330 | // and (size_t) 0 for type 2 or 3. |
| 6331 | if (End.Designator.Invalid) |
| 6332 | return false; |
| 6333 | |
| 6334 | // According to the GCC documentation, we want the size of the subobject |
| 6335 | // denoted by the pointer. But that's not quite right -- what we actually |
| 6336 | // want is the size of the immediately-enclosing array, if there is one. |
| 6337 | int64_t AmountToAdd = 1; |
| 6338 | if (End.Designator.MostDerivedArraySize && |
| 6339 | End.Designator.Entries.size() == End.Designator.MostDerivedPathLength) { |
| 6340 | // We got a pointer to an array. Step to its end. |
| 6341 | AmountToAdd = End.Designator.MostDerivedArraySize - |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 6342 | End.Designator.Entries.back().ArrayIndex; |
| 6343 | } else if (End.Designator.isOnePastTheEnd()) { |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6344 | // We're already pointing at the end of the object. |
| 6345 | AmountToAdd = 0; |
| 6346 | } |
| 6347 | |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 6348 | QualType PointeeType = End.Designator.MostDerivedType; |
| 6349 | assert(!PointeeType.isNull()); |
| 6350 | if (PointeeType->isIncompleteType() || PointeeType->isFunctionType()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6351 | return Error(E); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6352 | |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6353 | if (!HandleLValueArrayAdjustment(Info, E, End, End.Designator.MostDerivedType, |
| 6354 | AmountToAdd)) |
| 6355 | return false; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6356 | |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6357 | auto EndOffset = End.getLValueOffset(); |
| 6358 | if (BaseOffset > EndOffset) |
| 6359 | return Success(0, E); |
| 6360 | |
| 6361 | return Success(EndOffset - BaseOffset, E); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6362 | } |
| 6363 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6364 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 6365 | switch (unsigned BuiltinOp = E->getBuiltinCallee()) { |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6366 | default: |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6367 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 6368 | |
| 6369 | case Builtin::BI__builtin_object_size: { |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6370 | // The type was checked when we built the expression. |
| 6371 | unsigned Type = |
| 6372 | E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 6373 | assert(Type <= 3 && "unexpected type"); |
| 6374 | |
| 6375 | if (TryEvaluateBuiltinObjectSize(E, Type)) |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6376 | return true; |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 6377 | |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 6378 | // If evaluating the argument has side-effects, we can't determine the size |
| 6379 | // of the object, and so we lower it to unknown now. CodeGen relies on us to |
| 6380 | // handle all cases where the expression has side-effects. |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6381 | // Likewise, if Type is 3, we must handle this because CodeGen cannot give a |
| 6382 | // conservatively correct answer in that case. |
| 6383 | if (E->getArg(0)->HasSideEffects(Info.Ctx) || Type == 3) |
| 6384 | return Success((Type & 2) ? 0 : -1, E); |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 6385 | |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 6386 | // Expression had no side effects, but we couldn't statically determine the |
| 6387 | // size of the referenced object. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6388 | switch (Info.EvalMode) { |
| 6389 | case EvalInfo::EM_ConstantExpression: |
| 6390 | case EvalInfo::EM_PotentialConstantExpression: |
| 6391 | case EvalInfo::EM_ConstantFold: |
| 6392 | case EvalInfo::EM_EvaluateForOverflow: |
| 6393 | case EvalInfo::EM_IgnoreSideEffects: |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame^] | 6394 | case EvalInfo::EM_DesignatorFold: |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6395 | // Leave it to IR generation. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6396 | return Error(E); |
| 6397 | case EvalInfo::EM_ConstantExpressionUnevaluated: |
| 6398 | case EvalInfo::EM_PotentialConstantExpressionUnevaluated: |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 6399 | // Reduce it to a constant now. |
| 6400 | return Success((Type & 2) ? 0 : -1, E); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6401 | } |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 6402 | } |
| 6403 | |
Benjamin Kramer | a801f4a | 2012-10-06 14:42:22 +0000 | [diff] [blame] | 6404 | case Builtin::BI__builtin_bswap16: |
Richard Smith | 80ac9ef | 2012-09-28 20:20:52 +0000 | [diff] [blame] | 6405 | case Builtin::BI__builtin_bswap32: |
| 6406 | case Builtin::BI__builtin_bswap64: { |
| 6407 | APSInt Val; |
| 6408 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6409 | return false; |
| 6410 | |
| 6411 | return Success(Val.byteSwap(), E); |
| 6412 | } |
| 6413 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6414 | case Builtin::BI__builtin_classify_type: |
| 6415 | return Success(EvaluateBuiltinClassifyType(E), E); |
| 6416 | |
| 6417 | // FIXME: BI__builtin_clrsb |
| 6418 | // FIXME: BI__builtin_clrsbl |
| 6419 | // FIXME: BI__builtin_clrsbll |
| 6420 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6421 | case Builtin::BI__builtin_clz: |
| 6422 | case Builtin::BI__builtin_clzl: |
Anders Carlsson | 1a9fe3d | 2014-07-07 15:53:44 +0000 | [diff] [blame] | 6423 | case Builtin::BI__builtin_clzll: |
| 6424 | case Builtin::BI__builtin_clzs: { |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6425 | APSInt Val; |
| 6426 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6427 | return false; |
| 6428 | if (!Val) |
| 6429 | return Error(E); |
| 6430 | |
| 6431 | return Success(Val.countLeadingZeros(), E); |
| 6432 | } |
| 6433 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6434 | case Builtin::BI__builtin_constant_p: |
| 6435 | return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); |
| 6436 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6437 | case Builtin::BI__builtin_ctz: |
| 6438 | case Builtin::BI__builtin_ctzl: |
Anders Carlsson | 1a9fe3d | 2014-07-07 15:53:44 +0000 | [diff] [blame] | 6439 | case Builtin::BI__builtin_ctzll: |
| 6440 | case Builtin::BI__builtin_ctzs: { |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6441 | APSInt Val; |
| 6442 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6443 | return false; |
| 6444 | if (!Val) |
| 6445 | return Error(E); |
| 6446 | |
| 6447 | return Success(Val.countTrailingZeros(), E); |
| 6448 | } |
| 6449 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6450 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 6451 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 6452 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
| 6453 | return Success(Operand, E); |
| 6454 | } |
| 6455 | |
| 6456 | case Builtin::BI__builtin_expect: |
| 6457 | return Visit(E->getArg(0)); |
| 6458 | |
| 6459 | case Builtin::BI__builtin_ffs: |
| 6460 | case Builtin::BI__builtin_ffsl: |
| 6461 | case Builtin::BI__builtin_ffsll: { |
| 6462 | APSInt Val; |
| 6463 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6464 | return false; |
| 6465 | |
| 6466 | unsigned N = Val.countTrailingZeros(); |
| 6467 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
| 6468 | } |
| 6469 | |
| 6470 | case Builtin::BI__builtin_fpclassify: { |
| 6471 | APFloat Val(0.0); |
| 6472 | if (!EvaluateFloat(E->getArg(5), Val, Info)) |
| 6473 | return false; |
| 6474 | unsigned Arg; |
| 6475 | switch (Val.getCategory()) { |
| 6476 | case APFloat::fcNaN: Arg = 0; break; |
| 6477 | case APFloat::fcInfinity: Arg = 1; break; |
| 6478 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
| 6479 | case APFloat::fcZero: Arg = 4; break; |
| 6480 | } |
| 6481 | return Visit(E->getArg(Arg)); |
| 6482 | } |
| 6483 | |
| 6484 | case Builtin::BI__builtin_isinf_sign: { |
| 6485 | APFloat Val(0.0); |
Richard Smith | ab341c6 | 2013-06-13 06:31:13 +0000 | [diff] [blame] | 6486 | return EvaluateFloat(E->getArg(0), Val, Info) && |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6487 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
| 6488 | } |
| 6489 | |
Richard Smith | ea3019d | 2013-10-15 19:07:14 +0000 | [diff] [blame] | 6490 | case Builtin::BI__builtin_isinf: { |
| 6491 | APFloat Val(0.0); |
| 6492 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 6493 | Success(Val.isInfinity() ? 1 : 0, E); |
| 6494 | } |
| 6495 | |
| 6496 | case Builtin::BI__builtin_isfinite: { |
| 6497 | APFloat Val(0.0); |
| 6498 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 6499 | Success(Val.isFinite() ? 1 : 0, E); |
| 6500 | } |
| 6501 | |
| 6502 | case Builtin::BI__builtin_isnan: { |
| 6503 | APFloat Val(0.0); |
| 6504 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 6505 | Success(Val.isNaN() ? 1 : 0, E); |
| 6506 | } |
| 6507 | |
| 6508 | case Builtin::BI__builtin_isnormal: { |
| 6509 | APFloat Val(0.0); |
| 6510 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 6511 | Success(Val.isNormal() ? 1 : 0, E); |
| 6512 | } |
| 6513 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6514 | case Builtin::BI__builtin_parity: |
| 6515 | case Builtin::BI__builtin_parityl: |
| 6516 | case Builtin::BI__builtin_parityll: { |
| 6517 | APSInt Val; |
| 6518 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6519 | return false; |
| 6520 | |
| 6521 | return Success(Val.countPopulation() % 2, E); |
| 6522 | } |
| 6523 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6524 | case Builtin::BI__builtin_popcount: |
| 6525 | case Builtin::BI__builtin_popcountl: |
| 6526 | case Builtin::BI__builtin_popcountll: { |
| 6527 | APSInt Val; |
| 6528 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6529 | return false; |
| 6530 | |
| 6531 | return Success(Val.countPopulation(), E); |
| 6532 | } |
| 6533 | |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 6534 | case Builtin::BIstrlen: |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 6535 | // A call to strlen is not a constant expression. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6536 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6537 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 6538 | << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; |
| 6539 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6540 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 6541 | // Fall through. |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 6542 | case Builtin::BI__builtin_strlen: { |
| 6543 | // As an extension, we support __builtin_strlen() as a constant expression, |
| 6544 | // and support folding strlen() to a constant. |
| 6545 | LValue String; |
| 6546 | if (!EvaluatePointer(E->getArg(0), String, Info)) |
| 6547 | return false; |
| 6548 | |
| 6549 | // Fast path: if it's a string literal, search the string value. |
| 6550 | if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( |
| 6551 | String.getLValueBase().dyn_cast<const Expr *>())) { |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 6552 | // The string literal may have embedded null characters. Find the first |
| 6553 | // one and truncate there. |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 6554 | StringRef Str = S->getBytes(); |
| 6555 | int64_t Off = String.Offset.getQuantity(); |
| 6556 | if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && |
| 6557 | S->getCharByteWidth() == 1) { |
| 6558 | Str = Str.substr(Off); |
| 6559 | |
| 6560 | StringRef::size_type Pos = Str.find(0); |
| 6561 | if (Pos != StringRef::npos) |
| 6562 | Str = Str.substr(0, Pos); |
| 6563 | |
| 6564 | return Success(Str.size(), E); |
| 6565 | } |
| 6566 | |
| 6567 | // Fall through to slow path to issue appropriate diagnostic. |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 6568 | } |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 6569 | |
| 6570 | // Slow path: scan the bytes of the string looking for the terminating 0. |
| 6571 | QualType CharTy = E->getArg(0)->getType()->getPointeeType(); |
| 6572 | for (uint64_t Strlen = 0; /**/; ++Strlen) { |
| 6573 | APValue Char; |
| 6574 | if (!handleLValueToRValueConversion(Info, E, CharTy, String, Char) || |
| 6575 | !Char.isInt()) |
| 6576 | return false; |
| 6577 | if (!Char.getInt()) |
| 6578 | return Success(Strlen, E); |
| 6579 | if (!HandleLValueArrayAdjustment(Info, E, String, CharTy, 1)) |
| 6580 | return false; |
| 6581 | } |
| 6582 | } |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6583 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 6584 | case Builtin::BI__atomic_always_lock_free: |
Richard Smith | b1e36c6 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 6585 | case Builtin::BI__atomic_is_lock_free: |
| 6586 | case Builtin::BI__c11_atomic_is_lock_free: { |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6587 | APSInt SizeVal; |
| 6588 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 6589 | return false; |
| 6590 | |
| 6591 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 6592 | // of two less than the maximum inline atomic width, we know it is |
| 6593 | // lock-free. If the size isn't a power of two, or greater than the |
| 6594 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 6595 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 6596 | // the answer can only be determined at runtime; for example, 16-byte |
| 6597 | // atomics have lock-free implementations on some, but not all, |
| 6598 | // x86-64 processors. |
| 6599 | |
| 6600 | // Check power-of-two. |
| 6601 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 6602 | if (Size.isPowerOfTwo()) { |
| 6603 | // Check against inlining width. |
| 6604 | unsigned InlineWidthBits = |
| 6605 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 6606 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
| 6607 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
| 6608 | Size == CharUnits::One() || |
| 6609 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
| 6610 | Expr::NPC_NeverValueDependent)) |
| 6611 | // OK, we will inline appropriately-aligned operations of this size, |
| 6612 | // and _Atomic(T) is appropriately-aligned. |
| 6613 | return Success(1, E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6614 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 6615 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
| 6616 | castAs<PointerType>()->getPointeeType(); |
| 6617 | if (!PointeeType->isIncompleteType() && |
| 6618 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
| 6619 | // OK, we will inline operations on this object. |
| 6620 | return Success(1, E); |
| 6621 | } |
| 6622 | } |
| 6623 | } |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6624 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 6625 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
| 6626 | Success(0, E) : Error(E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6627 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6628 | } |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 6629 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 6630 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6631 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 6632 | if (!A.getLValueBase()) |
| 6633 | return !B.getLValueBase(); |
| 6634 | if (!B.getLValueBase()) |
| 6635 | return false; |
| 6636 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 6637 | if (A.getLValueBase().getOpaqueValue() != |
| 6638 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6639 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 6640 | if (!ADecl) |
| 6641 | return false; |
| 6642 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 6643 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6644 | return false; |
| 6645 | } |
| 6646 | |
| 6647 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6648 | A.getLValueCallIndex() == B.getLValueCallIndex(); |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6649 | } |
| 6650 | |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 6651 | /// \brief Determine whether this is a pointer past the end of the complete |
| 6652 | /// object referred to by the lvalue. |
| 6653 | static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx, |
| 6654 | const LValue &LV) { |
| 6655 | // A null pointer can be viewed as being "past the end" but we don't |
| 6656 | // choose to look at it that way here. |
| 6657 | if (!LV.getLValueBase()) |
| 6658 | return false; |
| 6659 | |
| 6660 | // If the designator is valid and refers to a subobject, we're not pointing |
| 6661 | // past the end. |
| 6662 | if (!LV.getLValueDesignator().Invalid && |
| 6663 | !LV.getLValueDesignator().isOnePastTheEnd()) |
| 6664 | return false; |
| 6665 | |
David Majnemer | c378ca5 | 2015-08-29 08:32:55 +0000 | [diff] [blame] | 6666 | // A pointer to an incomplete type might be past-the-end if the type's size is |
| 6667 | // zero. We cannot tell because the type is incomplete. |
| 6668 | QualType Ty = getType(LV.getLValueBase()); |
| 6669 | if (Ty->isIncompleteType()) |
| 6670 | return true; |
| 6671 | |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 6672 | // We're a past-the-end pointer if we point to the byte after the object, |
| 6673 | // no matter what our type or path is. |
David Majnemer | c378ca5 | 2015-08-29 08:32:55 +0000 | [diff] [blame] | 6674 | auto Size = Ctx.getTypeSizeInChars(Ty); |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 6675 | return LV.getLValueOffset() == Size; |
| 6676 | } |
| 6677 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6678 | namespace { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6679 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6680 | /// \brief Data recursive integer evaluator of certain binary operators. |
| 6681 | /// |
| 6682 | /// We use a data recursive algorithm for binary operators so that we are able |
| 6683 | /// to handle extreme cases of chained binary operators without causing stack |
| 6684 | /// overflow. |
| 6685 | class DataRecursiveIntBinOpEvaluator { |
| 6686 | struct EvalResult { |
| 6687 | APValue Val; |
| 6688 | bool Failed; |
| 6689 | |
| 6690 | EvalResult() : Failed(false) { } |
| 6691 | |
| 6692 | void swap(EvalResult &RHS) { |
| 6693 | Val.swap(RHS.Val); |
| 6694 | Failed = RHS.Failed; |
| 6695 | RHS.Failed = false; |
| 6696 | } |
| 6697 | }; |
| 6698 | |
| 6699 | struct Job { |
| 6700 | const Expr *E; |
| 6701 | EvalResult LHSResult; // meaningful only for binary operator expression. |
| 6702 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6703 | |
David Blaikie | 7372606 | 2015-08-12 23:09:24 +0000 | [diff] [blame] | 6704 | Job() = default; |
| 6705 | Job(Job &&J) |
| 6706 | : E(J.E), LHSResult(J.LHSResult), Kind(J.Kind), |
| 6707 | StoredInfo(J.StoredInfo), OldEvalStatus(J.OldEvalStatus) { |
| 6708 | J.StoredInfo = nullptr; |
| 6709 | } |
| 6710 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6711 | void startSpeculativeEval(EvalInfo &Info) { |
| 6712 | OldEvalStatus = Info.EvalStatus; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6713 | Info.EvalStatus.Diag = nullptr; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6714 | StoredInfo = &Info; |
| 6715 | } |
| 6716 | ~Job() { |
| 6717 | if (StoredInfo) { |
| 6718 | StoredInfo->EvalStatus = OldEvalStatus; |
| 6719 | } |
| 6720 | } |
| 6721 | private: |
David Blaikie | 7372606 | 2015-08-12 23:09:24 +0000 | [diff] [blame] | 6722 | EvalInfo *StoredInfo = nullptr; // non-null if status changed. |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6723 | Expr::EvalStatus OldEvalStatus; |
| 6724 | }; |
| 6725 | |
| 6726 | SmallVector<Job, 16> Queue; |
| 6727 | |
| 6728 | IntExprEvaluator &IntEval; |
| 6729 | EvalInfo &Info; |
| 6730 | APValue &FinalResult; |
| 6731 | |
| 6732 | public: |
| 6733 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
| 6734 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
| 6735 | |
| 6736 | /// \brief True if \param E is a binary operator that we are going to handle |
| 6737 | /// data recursively. |
| 6738 | /// We handle binary operators that are comma, logical, or that have operands |
| 6739 | /// with integral or enumeration type. |
| 6740 | static bool shouldEnqueue(const BinaryOperator *E) { |
| 6741 | return E->getOpcode() == BO_Comma || |
| 6742 | E->isLogicalOp() || |
| 6743 | (E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 6744 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6745 | } |
| 6746 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6747 | bool Traverse(const BinaryOperator *E) { |
| 6748 | enqueue(E); |
| 6749 | EvalResult PrevResult; |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6750 | while (!Queue.empty()) |
| 6751 | process(PrevResult); |
| 6752 | |
| 6753 | if (PrevResult.Failed) return false; |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6754 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6755 | FinalResult.swap(PrevResult.Val); |
| 6756 | return true; |
| 6757 | } |
| 6758 | |
| 6759 | private: |
| 6760 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| 6761 | return IntEval.Success(Value, E, Result); |
| 6762 | } |
| 6763 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
| 6764 | return IntEval.Success(Value, E, Result); |
| 6765 | } |
| 6766 | bool Error(const Expr *E) { |
| 6767 | return IntEval.Error(E); |
| 6768 | } |
| 6769 | bool Error(const Expr *E, diag::kind D) { |
| 6770 | return IntEval.Error(E, D); |
| 6771 | } |
| 6772 | |
| 6773 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
| 6774 | return Info.CCEDiag(E, D); |
| 6775 | } |
| 6776 | |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6777 | // \brief Returns true if visiting the RHS is necessary, false otherwise. |
| 6778 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6779 | bool &SuppressRHSDiags); |
| 6780 | |
| 6781 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 6782 | const BinaryOperator *E, APValue &Result); |
| 6783 | |
| 6784 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
| 6785 | Result.Failed = !Evaluate(Result.Val, Info, E); |
| 6786 | if (Result.Failed) |
| 6787 | Result.Val = APValue(); |
| 6788 | } |
| 6789 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6790 | void process(EvalResult &Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6791 | |
| 6792 | void enqueue(const Expr *E) { |
| 6793 | E = E->IgnoreParens(); |
| 6794 | Queue.resize(Queue.size()+1); |
| 6795 | Queue.back().E = E; |
| 6796 | Queue.back().Kind = Job::AnyExprKind; |
| 6797 | } |
| 6798 | }; |
| 6799 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6800 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6801 | |
| 6802 | bool DataRecursiveIntBinOpEvaluator:: |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6803 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6804 | bool &SuppressRHSDiags) { |
| 6805 | if (E->getOpcode() == BO_Comma) { |
| 6806 | // Ignore LHS but note if we could not evaluate it. |
| 6807 | if (LHSResult.Failed) |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6808 | return Info.noteSideEffect(); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6809 | return true; |
| 6810 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6811 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6812 | if (E->isLogicalOp()) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6813 | bool LHSAsBool; |
| 6814 | if (!LHSResult.Failed && HandleConversionToBool(LHSResult.Val, LHSAsBool)) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6815 | // We were able to evaluate the LHS, see if we can get away with not |
| 6816 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6817 | if (LHSAsBool == (E->getOpcode() == BO_LOr)) { |
| 6818 | Success(LHSAsBool, E, LHSResult.Val); |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6819 | return false; // Ignore RHS |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6820 | } |
| 6821 | } else { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6822 | LHSResult.Failed = true; |
| 6823 | |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6824 | // Since we weren't able to evaluate the left hand side, it |
| 6825 | // must have had side effects. |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6826 | if (!Info.noteSideEffect()) |
| 6827 | return false; |
| 6828 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6829 | // We can't evaluate the LHS; however, sometimes the result |
| 6830 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 6831 | // Don't ignore RHS and suppress diagnostics from this arm. |
| 6832 | SuppressRHSDiags = true; |
| 6833 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6834 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6835 | return true; |
| 6836 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6837 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6838 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 6839 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6840 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6841 | if (LHSResult.Failed && !Info.keepEvaluatingAfterFailure()) |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6842 | return false; // Ignore RHS; |
| 6843 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6844 | return true; |
| 6845 | } |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6846 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6847 | bool DataRecursiveIntBinOpEvaluator:: |
| 6848 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 6849 | const BinaryOperator *E, APValue &Result) { |
| 6850 | if (E->getOpcode() == BO_Comma) { |
| 6851 | if (RHSResult.Failed) |
| 6852 | return false; |
| 6853 | Result = RHSResult.Val; |
| 6854 | return true; |
| 6855 | } |
| 6856 | |
| 6857 | if (E->isLogicalOp()) { |
| 6858 | bool lhsResult, rhsResult; |
| 6859 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
| 6860 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
| 6861 | |
| 6862 | if (LHSIsOK) { |
| 6863 | if (RHSIsOK) { |
| 6864 | if (E->getOpcode() == BO_LOr) |
| 6865 | return Success(lhsResult || rhsResult, E, Result); |
| 6866 | else |
| 6867 | return Success(lhsResult && rhsResult, E, Result); |
| 6868 | } |
| 6869 | } else { |
| 6870 | if (RHSIsOK) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6871 | // We can't evaluate the LHS; however, sometimes the result |
| 6872 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 6873 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6874 | return Success(rhsResult, E, Result); |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6875 | } |
| 6876 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6877 | |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6878 | return false; |
| 6879 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6880 | |
| 6881 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 6882 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| 6883 | |
| 6884 | if (LHSResult.Failed || RHSResult.Failed) |
| 6885 | return false; |
| 6886 | |
| 6887 | const APValue &LHSVal = LHSResult.Val; |
| 6888 | const APValue &RHSVal = RHSResult.Val; |
| 6889 | |
| 6890 | // Handle cases like (unsigned long)&a + 4. |
| 6891 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
| 6892 | Result = LHSVal; |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 6893 | CharUnits AdditionalOffset = |
| 6894 | CharUnits::fromQuantity(RHSVal.getInt().getZExtValue()); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6895 | if (E->getOpcode() == BO_Add) |
| 6896 | Result.getLValueOffset() += AdditionalOffset; |
| 6897 | else |
| 6898 | Result.getLValueOffset() -= AdditionalOffset; |
| 6899 | return true; |
| 6900 | } |
| 6901 | |
| 6902 | // Handle cases like 4 + (unsigned long)&a |
| 6903 | if (E->getOpcode() == BO_Add && |
| 6904 | RHSVal.isLValue() && LHSVal.isInt()) { |
| 6905 | Result = RHSVal; |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 6906 | Result.getLValueOffset() += |
| 6907 | CharUnits::fromQuantity(LHSVal.getInt().getZExtValue()); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6908 | return true; |
| 6909 | } |
| 6910 | |
| 6911 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 6912 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
| 6913 | if (!LHSVal.getLValueOffset().isZero() || |
| 6914 | !RHSVal.getLValueOffset().isZero()) |
| 6915 | return false; |
| 6916 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 6917 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 6918 | if (!LHSExpr || !RHSExpr) |
| 6919 | return false; |
| 6920 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 6921 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 6922 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 6923 | return false; |
| 6924 | // Make sure both labels come from the same function. |
| 6925 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 6926 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 6927 | return false; |
| 6928 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
| 6929 | return true; |
| 6930 | } |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 6931 | |
| 6932 | // All the remaining cases expect both operands to be an integer |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6933 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
| 6934 | return Error(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 6935 | |
| 6936 | // Set up the width and signedness manually, in case it can't be deduced |
| 6937 | // from the operation we're performing. |
| 6938 | // FIXME: Don't do this in the cases where we can deduce it. |
| 6939 | APSInt Value(Info.Ctx.getIntWidth(E->getType()), |
| 6940 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
| 6941 | if (!handleIntIntBinOp(Info, E, LHSVal.getInt(), E->getOpcode(), |
| 6942 | RHSVal.getInt(), Value)) |
| 6943 | return false; |
| 6944 | return Success(Value, E, Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6945 | } |
| 6946 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6947 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6948 | Job &job = Queue.back(); |
| 6949 | |
| 6950 | switch (job.Kind) { |
| 6951 | case Job::AnyExprKind: { |
| 6952 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
| 6953 | if (shouldEnqueue(Bop)) { |
| 6954 | job.Kind = Job::BinOpKind; |
| 6955 | enqueue(Bop->getLHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6956 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6957 | } |
| 6958 | } |
| 6959 | |
| 6960 | EvaluateExpr(job.E, Result); |
| 6961 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6962 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6963 | } |
| 6964 | |
| 6965 | case Job::BinOpKind: { |
| 6966 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6967 | bool SuppressRHSDiags = false; |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6968 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6969 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6970 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6971 | } |
| 6972 | if (SuppressRHSDiags) |
| 6973 | job.startSpeculativeEval(Info); |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6974 | job.LHSResult.swap(Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6975 | job.Kind = Job::BinOpVisitedLHSKind; |
| 6976 | enqueue(Bop->getRHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6977 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6978 | } |
| 6979 | |
| 6980 | case Job::BinOpVisitedLHSKind: { |
| 6981 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
| 6982 | EvalResult RHS; |
| 6983 | RHS.swap(Result); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6984 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6985 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6986 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6987 | } |
| 6988 | } |
| 6989 | |
| 6990 | llvm_unreachable("Invalid Job::Kind!"); |
| 6991 | } |
| 6992 | |
| 6993 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Josh Magee | 4d1a79b | 2015-02-04 21:50:20 +0000 | [diff] [blame] | 6994 | if (!Info.keepEvaluatingAfterFailure() && E->isAssignmentOp()) |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6995 | return Error(E); |
| 6996 | |
| 6997 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
| 6998 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6999 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 7000 | QualType LHSTy = E->getLHS()->getType(); |
| 7001 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 7002 | |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 7003 | if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7004 | ComplexValue LHS, RHS; |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 7005 | bool LHSOK; |
Josh Magee | 4d1a79b | 2015-02-04 21:50:20 +0000 | [diff] [blame] | 7006 | if (E->isAssignmentOp()) { |
| 7007 | LValue LV; |
| 7008 | EvaluateLValue(E->getLHS(), LV, Info); |
| 7009 | LHSOK = false; |
| 7010 | } else if (LHSTy->isRealFloatingType()) { |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 7011 | LHSOK = EvaluateFloat(E->getLHS(), LHS.FloatReal, Info); |
| 7012 | if (LHSOK) { |
| 7013 | LHS.makeComplexFloat(); |
| 7014 | LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics()); |
| 7015 | } |
| 7016 | } else { |
| 7017 | LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 7018 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7019 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 7020 | return false; |
| 7021 | |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 7022 | if (E->getRHS()->getType()->isRealFloatingType()) { |
| 7023 | if (!EvaluateFloat(E->getRHS(), RHS.FloatReal, Info) || !LHSOK) |
| 7024 | return false; |
| 7025 | RHS.makeComplexFloat(); |
| 7026 | RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics()); |
| 7027 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 7028 | return false; |
| 7029 | |
| 7030 | if (LHS.isComplexFloat()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7031 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 7032 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7033 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 7034 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 7035 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7036 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7037 | return Success((CR_r == APFloat::cmpEqual && |
| 7038 | CR_i == APFloat::cmpEqual), E); |
| 7039 | else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7040 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7041 | "Invalid complex comparison."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7042 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 7043 | CR_r == APFloat::cmpLessThan || |
| 7044 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7045 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 7046 | CR_i == APFloat::cmpLessThan || |
| 7047 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7048 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 7049 | } else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7050 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7051 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 7052 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 7053 | else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7054 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7055 | "Invalid compex comparison."); |
| 7056 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 7057 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 7058 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 7059 | } |
| 7060 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7061 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 7062 | if (LHSTy->isRealFloatingType() && |
| 7063 | RHSTy->isRealFloatingType()) { |
| 7064 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7065 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7066 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
| 7067 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 7068 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7069 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7070 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 7071 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7072 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 7073 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 899c705 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 7074 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 7075 | switch (E->getOpcode()) { |
| 7076 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7077 | llvm_unreachable("Invalid binary operator!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7078 | case BO_LT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7079 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7080 | case BO_GT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7081 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7082 | case BO_LE: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7083 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7084 | case BO_GE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7085 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7086 | E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7087 | case BO_EQ: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7088 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7089 | case BO_NE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7090 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 7091 | || CR == APFloat::cmpLessThan |
| 7092 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 7093 | } |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 7094 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7095 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 7096 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 7097 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7098 | LValue LHSValue, RHSValue; |
| 7099 | |
| 7100 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 7101 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7102 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 7103 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7104 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7105 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 7106 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 7107 | // Reject differing bases from the normal codepath; we special-case |
| 7108 | // comparisons to null. |
| 7109 | if (!HasSameBase(LHSValue, RHSValue)) { |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 7110 | if (E->getOpcode() == BO_Sub) { |
| 7111 | // Handle &&A - &&B. |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 7112 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 7113 | return false; |
| 7114 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
Benjamin Kramer | daa09612 | 2012-10-03 14:15:39 +0000 | [diff] [blame] | 7115 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr*>(); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 7116 | if (!LHSExpr || !RHSExpr) |
| 7117 | return false; |
| 7118 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 7119 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 7120 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 7121 | return false; |
Eli Friedman | b1bc368 | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 7122 | // Make sure both labels come from the same function. |
| 7123 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 7124 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 7125 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7126 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 7127 | return true; |
| 7128 | } |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 7129 | // Inequalities and subtractions between unrelated pointers have |
| 7130 | // unspecified or undefined behavior. |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 7131 | if (!E->isEqualityOp()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7132 | return Error(E); |
Eli Friedman | c6be94b | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 7133 | // A constant address may compare equal to the address of a symbol. |
| 7134 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | 42fbd62 | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 7135 | // to a null pointer constant. |
Eli Friedman | c6be94b | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 7136 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 7137 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7138 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 7139 | // It's implementation-defined whether distinct literals will have |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 7140 | // distinct addresses. In clang, the result of such a comparison is |
| 7141 | // unspecified, so it is not a constant expression. However, we do know |
| 7142 | // that the address of a literal will be non-null. |
Richard Smith | e9e20dd3 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 7143 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 7144 | LHSValue.Base && RHSValue.Base) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7145 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 7146 | // We can't tell whether weak symbols will end up pointing to the same |
| 7147 | // object. |
| 7148 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7149 | return Error(E); |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 7150 | // We can't compare the address of the start of one object with the |
| 7151 | // past-the-end address of another object, per C++ DR1652. |
| 7152 | if ((LHSValue.Base && LHSValue.Offset.isZero() && |
| 7153 | isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue)) || |
| 7154 | (RHSValue.Base && RHSValue.Offset.isZero() && |
| 7155 | isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue))) |
| 7156 | return Error(E); |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 7157 | // We can't tell whether an object is at the same address as another |
| 7158 | // zero sized object. |
David Majnemer | 27db358 | 2014-12-11 19:36:24 +0000 | [diff] [blame] | 7159 | if ((RHSValue.Base && isZeroSized(LHSValue)) || |
| 7160 | (LHSValue.Base && isZeroSized(RHSValue))) |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 7161 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 7162 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | 42fbd62 | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 7163 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 7164 | // lead to inconsistent results for comparisons involving the address |
| 7165 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 7166 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 7167 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 7168 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 7169 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 7170 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 7171 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 7172 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 7173 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 7174 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7175 | if (E->getOpcode() == BO_Sub) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 7176 | // C++11 [expr.add]p6: |
| 7177 | // Unless both pointers point to elements of the same array object, or |
| 7178 | // one past the last element of the array object, the behavior is |
| 7179 | // undefined. |
| 7180 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 7181 | !AreElementsOfSameArray(getType(LHSValue.Base), |
| 7182 | LHSDesignator, RHSDesignator)) |
| 7183 | CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 7184 | |
Chris Lattner | 882bdf2 | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 7185 | QualType Type = E->getLHS()->getType(); |
| 7186 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7187 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7188 | CharUnits ElementSize; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 7189 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7190 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 7191 | |
Richard Smith | 84c6b3d | 2013-09-10 21:34:14 +0000 | [diff] [blame] | 7192 | // As an extension, a type may have zero size (empty struct or union in |
| 7193 | // C, array of zero length). Pointer subtraction in such cases has |
| 7194 | // undefined behavior, so is not constant. |
| 7195 | if (ElementSize.isZero()) { |
| 7196 | Info.Diag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
| 7197 | << ElementType; |
| 7198 | return false; |
| 7199 | } |
| 7200 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 7201 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 7202 | // and produce incorrect results when it overflows. Such behavior |
| 7203 | // appears to be non-conforming, but is common, so perhaps we should |
| 7204 | // assume the standard intended for such cases to be undefined behavior |
| 7205 | // and check for them. |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 7206 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 7207 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 7208 | // overflow in the final conversion to ptrdiff_t. |
| 7209 | APSInt LHS( |
| 7210 | llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 7211 | APSInt RHS( |
| 7212 | llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 7213 | APSInt ElemSize( |
| 7214 | llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); |
| 7215 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 7216 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 7217 | |
| 7218 | if (Result.extend(65) != TrueResult) |
| 7219 | HandleOverflow(Info, E, TrueResult, E->getType()); |
| 7220 | return Success(Result, E); |
| 7221 | } |
Richard Smith | de21b24 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 7222 | |
| 7223 | // C++11 [expr.rel]p3: |
| 7224 | // Pointers to void (after pointer conversions) can be compared, with a |
| 7225 | // result defined as follows: If both pointers represent the same |
| 7226 | // address or are both the null pointer value, the result is true if the |
| 7227 | // operator is <= or >= and false otherwise; otherwise the result is |
| 7228 | // unspecified. |
| 7229 | // We interpret this as applying to pointers to *cv* void. |
| 7230 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 7231 | E->isRelationalOp()) |
Richard Smith | de21b24 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 7232 | CCEDiag(E, diag::note_constexpr_void_comparison); |
| 7233 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 7234 | // C++11 [expr.rel]p2: |
| 7235 | // - If two pointers point to non-static data members of the same object, |
| 7236 | // or to subobjects or array elements fo such members, recursively, the |
| 7237 | // pointer to the later declared member compares greater provided the |
| 7238 | // two members have the same access control and provided their class is |
| 7239 | // not a union. |
| 7240 | // [...] |
| 7241 | // - Otherwise pointer comparisons are unspecified. |
| 7242 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 7243 | E->isRelationalOp()) { |
| 7244 | bool WasArrayIndex; |
| 7245 | unsigned Mismatch = |
| 7246 | FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, |
| 7247 | RHSDesignator, WasArrayIndex); |
| 7248 | // At the point where the designators diverge, the comparison has a |
| 7249 | // specified value if: |
| 7250 | // - we are comparing array indices |
| 7251 | // - we are comparing fields of a union, or fields with the same access |
| 7252 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 7253 | // constant expression. |
| 7254 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 7255 | Mismatch < RHSDesignator.Entries.size()) { |
| 7256 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 7257 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 7258 | if (!LF && !RF) |
| 7259 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 7260 | else if (!LF) |
| 7261 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 7262 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 7263 | << RF->getParent() << RF; |
| 7264 | else if (!RF) |
| 7265 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 7266 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 7267 | << LF->getParent() << LF; |
| 7268 | else if (!LF->getParent()->isUnion() && |
| 7269 | LF->getAccess() != RF->getAccess()) |
| 7270 | CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) |
| 7271 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 7272 | << LF->getParent(); |
| 7273 | } |
| 7274 | } |
| 7275 | |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 7276 | // The comparison here must be unsigned, and performed with the same |
| 7277 | // width as the pointer. |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 7278 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
| 7279 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
| 7280 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
| 7281 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
| 7282 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
| 7283 | CompareLHS &= Mask; |
| 7284 | CompareRHS &= Mask; |
| 7285 | |
Eli Friedman | 2f5b7c5 | 2012-04-16 19:23:57 +0000 | [diff] [blame] | 7286 | // If there is a base and this is a relational operator, we can only |
| 7287 | // compare pointers within the object in question; otherwise, the result |
| 7288 | // depends on where the object is located in memory. |
| 7289 | if (!LHSValue.Base.isNull() && E->isRelationalOp()) { |
| 7290 | QualType BaseTy = getType(LHSValue.Base); |
| 7291 | if (BaseTy->isIncompleteType()) |
| 7292 | return Error(E); |
| 7293 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
| 7294 | uint64_t OffsetLimit = Size.getQuantity(); |
| 7295 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
| 7296 | return Error(E); |
| 7297 | } |
| 7298 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 7299 | switch (E->getOpcode()) { |
| 7300 | default: llvm_unreachable("missing comparison operator"); |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 7301 | case BO_LT: return Success(CompareLHS < CompareRHS, E); |
| 7302 | case BO_GT: return Success(CompareLHS > CompareRHS, E); |
| 7303 | case BO_LE: return Success(CompareLHS <= CompareRHS, E); |
| 7304 | case BO_GE: return Success(CompareLHS >= CompareRHS, E); |
| 7305 | case BO_EQ: return Success(CompareLHS == CompareRHS, E); |
| 7306 | case BO_NE: return Success(CompareLHS != CompareRHS, E); |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 7307 | } |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7308 | } |
| 7309 | } |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 7310 | |
| 7311 | if (LHSTy->isMemberPointerType()) { |
| 7312 | assert(E->isEqualityOp() && "unexpected member pointer operation"); |
| 7313 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 7314 | |
| 7315 | MemberPtr LHSValue, RHSValue; |
| 7316 | |
| 7317 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
| 7318 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
| 7319 | return false; |
| 7320 | |
| 7321 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 7322 | return false; |
| 7323 | |
| 7324 | // C++11 [expr.eq]p2: |
| 7325 | // If both operands are null, they compare equal. Otherwise if only one is |
| 7326 | // null, they compare unequal. |
| 7327 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 7328 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
| 7329 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 7330 | } |
| 7331 | |
| 7332 | // Otherwise if either is a pointer to a virtual member function, the |
| 7333 | // result is unspecified. |
| 7334 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 7335 | if (MD->isVirtual()) |
| 7336 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 7337 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 7338 | if (MD->isVirtual()) |
| 7339 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 7340 | |
| 7341 | // Otherwise they compare equal if and only if they would refer to the |
| 7342 | // same member of the same most derived object or the same subobject if |
| 7343 | // they were dereferenced with a hypothetical object of the associated |
| 7344 | // class type. |
| 7345 | bool Equal = LHSValue == RHSValue; |
| 7346 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 7347 | } |
| 7348 | |
Richard Smith | ab44d9b | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 7349 | if (LHSTy->isNullPtrType()) { |
| 7350 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
| 7351 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
| 7352 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
| 7353 | // are compared, the result is true of the operator is <=, >= or ==, and |
| 7354 | // false otherwise. |
| 7355 | BinaryOperator::Opcode Opcode = E->getOpcode(); |
| 7356 | return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E); |
| 7357 | } |
| 7358 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7359 | assert((!LHSTy->isIntegralOrEnumerationType() || |
| 7360 | !RHSTy->isIntegralOrEnumerationType()) && |
| 7361 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
| 7362 | // We can't continue from here for non-integral types. |
| 7363 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7364 | } |
| 7365 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7366 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 7367 | /// a result as the expression's type. |
| 7368 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 7369 | const UnaryExprOrTypeTraitExpr *E) { |
| 7370 | switch(E->getKind()) { |
| 7371 | case UETT_AlignOf: { |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 7372 | if (E->isArgumentType()) |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 7373 | return Success(GetAlignOfType(Info, E->getArgumentType()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 7374 | else |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 7375 | return Success(GetAlignOfExpr(Info, E->getArgumentExpr()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 7376 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 7377 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7378 | case UETT_VecStep: { |
| 7379 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 7380 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7381 | if (Ty->isVectorType()) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7382 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 7383 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7384 | // The vec_step built-in functions that take a 3-component |
| 7385 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 7386 | if (n == 3) |
| 7387 | n = 4; |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 7388 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7389 | return Success(n, E); |
| 7390 | } else |
| 7391 | return Success(1, E); |
| 7392 | } |
| 7393 | |
| 7394 | case UETT_SizeOf: { |
| 7395 | QualType SrcTy = E->getTypeOfArgument(); |
| 7396 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 7397 | // the result is the size of the referenced type." |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7398 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 7399 | SrcTy = Ref->getPointeeType(); |
| 7400 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7401 | CharUnits Sizeof; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 7402 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7403 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7404 | return Success(Sizeof, E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7405 | } |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 7406 | case UETT_OpenMPRequiredSimdAlign: |
| 7407 | assert(E->isArgumentType()); |
| 7408 | return Success( |
| 7409 | Info.Ctx.toCharUnitsFromBits( |
| 7410 | Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType())) |
| 7411 | .getQuantity(), |
| 7412 | E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7413 | } |
| 7414 | |
| 7415 | llvm_unreachable("unknown expr/type trait"); |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 7416 | } |
| 7417 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7418 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7419 | CharUnits Result; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7420 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7421 | if (n == 0) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7422 | return Error(OOE); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7423 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7424 | for (unsigned i = 0; i != n; ++i) { |
| 7425 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 7426 | switch (ON.getKind()) { |
| 7427 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7428 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7429 | APSInt IdxResult; |
| 7430 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 7431 | return false; |
| 7432 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 7433 | if (!AT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7434 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7435 | CurrentType = AT->getElementType(); |
| 7436 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 7437 | Result += IdxResult.getSExtValue() * ElementSize; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 7438 | break; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7439 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7440 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7441 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 7442 | FieldDecl *MemberDecl = ON.getField(); |
| 7443 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7444 | if (!RT) |
| 7445 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7446 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 7447 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7448 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 7449 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7450 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 7451 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7452 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 7453 | break; |
| 7454 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7455 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7456 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 7457 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7458 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7459 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 7460 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 7461 | if (BaseSpec->isVirtual()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7462 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7463 | |
| 7464 | // Find the layout of the class whose base we are looking into. |
| 7465 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7466 | if (!RT) |
| 7467 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7468 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 7469 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7470 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 7471 | |
| 7472 | // Find the base class itself. |
| 7473 | CurrentType = BaseSpec->getType(); |
| 7474 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 7475 | if (!BaseRT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7476 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7477 | |
| 7478 | // Add the offset to the base. |
Ken Dyck | 02155cb | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 7479 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7480 | break; |
| 7481 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7482 | } |
| 7483 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7484 | return Success(Result, OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7485 | } |
| 7486 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7487 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7488 | switch (E->getOpcode()) { |
| 7489 | default: |
| 7490 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 7491 | // See C99 6.6p3. |
| 7492 | return Error(E); |
| 7493 | case UO_Extension: |
| 7494 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 7495 | // If so, we could clear the diagnostic ID. |
| 7496 | return Visit(E->getSubExpr()); |
| 7497 | case UO_Plus: |
| 7498 | // The result is just the value. |
| 7499 | return Visit(E->getSubExpr()); |
| 7500 | case UO_Minus: { |
| 7501 | if (!Visit(E->getSubExpr())) |
| 7502 | return false; |
| 7503 | if (!Result.isInt()) return Error(E); |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 7504 | const APSInt &Value = Result.getInt(); |
| 7505 | if (Value.isSigned() && Value.isMinSignedValue()) |
| 7506 | HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 7507 | E->getType()); |
| 7508 | return Success(-Value, E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7509 | } |
| 7510 | case UO_Not: { |
| 7511 | if (!Visit(E->getSubExpr())) |
| 7512 | return false; |
| 7513 | if (!Result.isInt()) return Error(E); |
| 7514 | return Success(~Result.getInt(), E); |
| 7515 | } |
| 7516 | case UO_LNot: { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 7517 | bool bres; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7518 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 7519 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7520 | return Success(!bres, E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 7521 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7522 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7523 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7524 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 7525 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 7526 | /// result type is integer. |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7527 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 7528 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 7529 | QualType DestType = E->getType(); |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 7530 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 7531 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7532 | switch (E->getCastKind()) { |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7533 | case CK_BaseToDerived: |
| 7534 | case CK_DerivedToBase: |
| 7535 | case CK_UncheckedDerivedToBase: |
| 7536 | case CK_Dynamic: |
| 7537 | case CK_ToUnion: |
| 7538 | case CK_ArrayToPointerDecay: |
| 7539 | case CK_FunctionToPointerDecay: |
| 7540 | case CK_NullToPointer: |
| 7541 | case CK_NullToMemberPointer: |
| 7542 | case CK_BaseToDerivedMemberPointer: |
| 7543 | case CK_DerivedToBaseMemberPointer: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 7544 | case CK_ReinterpretMemberPointer: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7545 | case CK_ConstructorConversion: |
| 7546 | case CK_IntegralToPointer: |
| 7547 | case CK_ToVoid: |
| 7548 | case CK_VectorSplat: |
| 7549 | case CK_IntegralToFloating: |
| 7550 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7551 | case CK_CPointerToObjCPointerCast: |
| 7552 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7553 | case CK_AnyPointerToBlockPointerCast: |
| 7554 | case CK_ObjCObjectLValueCast: |
| 7555 | case CK_FloatingRealToComplex: |
| 7556 | case CK_FloatingComplexToReal: |
| 7557 | case CK_FloatingComplexCast: |
| 7558 | case CK_FloatingComplexToIntegralComplex: |
| 7559 | case CK_IntegralRealToComplex: |
| 7560 | case CK_IntegralComplexCast: |
| 7561 | case CK_IntegralComplexToFloatingComplex: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 7562 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7563 | case CK_ZeroToOCLEvent: |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7564 | case CK_NonAtomicToAtomic: |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 7565 | case CK_AddressSpaceConversion: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7566 | llvm_unreachable("invalid cast kind for integral value"); |
| 7567 | |
Eli Friedman | 9faf2f9 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 7568 | case CK_BitCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7569 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7570 | case CK_LValueBitCast: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 7571 | case CK_ARCProduceObject: |
| 7572 | case CK_ARCConsumeObject: |
| 7573 | case CK_ARCReclaimReturnedObject: |
| 7574 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 7575 | case CK_CopyAndAutoreleaseBlockObject: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7576 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7577 | |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 7578 | case CK_UserDefinedConversion: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7579 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 7580 | case CK_AtomicToNonAtomic: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7581 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7582 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7583 | |
| 7584 | case CK_MemberPointerToBoolean: |
| 7585 | case CK_PointerToBoolean: |
| 7586 | case CK_IntegralToBoolean: |
| 7587 | case CK_FloatingToBoolean: |
| 7588 | case CK_FloatingComplexToBoolean: |
| 7589 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7590 | bool BoolResult; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7591 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7592 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7593 | return Success(BoolResult, E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7594 | } |
| 7595 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7596 | case CK_IntegralCast: { |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 7597 | if (!Visit(SubExpr)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7598 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 7599 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 7600 | if (!Result.isInt()) { |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 7601 | // Allow casts of address-of-label differences if they are no-ops |
| 7602 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 7603 | // be constant-evaluatable except in some narrow cases which are hard |
| 7604 | // to detect here. We let it through on the assumption the user knows |
| 7605 | // what they are doing.) |
| 7606 | if (Result.isAddrLabelDiff()) |
| 7607 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 7608 | // Only allow casts of lvalues if they are lossless. |
| 7609 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 7610 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 7611 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 7612 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 7613 | Result.getInt()), E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 7614 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7615 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7616 | case CK_PointerToIntegral: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 7617 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 7618 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7619 | LValue LV; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 7620 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7621 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7622 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 7623 | if (LV.getLValueBase()) { |
| 7624 | // Only allow based lvalue casts if they are lossless. |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 7625 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 7626 | // narrowing back down to pointer width in subsequent integral casts. |
| 7627 | // FIXME: Check integer type's active bits, not its type size. |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 7628 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7629 | return Error(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7630 | |
Richard Smith | cf74da7 | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 7631 | LV.Designator.setInvalid(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7632 | LV.moveInto(Result); |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 7633 | return true; |
| 7634 | } |
| 7635 | |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 7636 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 7637 | SrcType); |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 7638 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 7639 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7640 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7641 | case CK_IntegralComplexToReal: { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7642 | ComplexValue C; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 7643 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 7644 | return false; |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7645 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 7646 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 7647 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7648 | case CK_FloatingToIntegral: { |
| 7649 | APFloat F(0.0); |
| 7650 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 7651 | return false; |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 7652 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7653 | APSInt Value; |
| 7654 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 7655 | return false; |
| 7656 | return Success(Value, E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7657 | } |
| 7658 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7659 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7660 | llvm_unreachable("unknown cast resulting in integral value"); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7661 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 7662 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7663 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 7664 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7665 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7666 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 7667 | return false; |
| 7668 | if (!LV.isComplexInt()) |
| 7669 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7670 | return Success(LV.getComplexIntReal(), E); |
| 7671 | } |
| 7672 | |
| 7673 | return Visit(E->getSubExpr()); |
| 7674 | } |
| 7675 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7676 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7677 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7678 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7679 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 7680 | return false; |
| 7681 | if (!LV.isComplexInt()) |
| 7682 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7683 | return Success(LV.getComplexIntImag(), E); |
| 7684 | } |
| 7685 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 7686 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7687 | return Success(0, E); |
| 7688 | } |
| 7689 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 7690 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 7691 | return Success(E->getPackLength(), E); |
| 7692 | } |
| 7693 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 7694 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 7695 | return Success(E->getValue(), E); |
| 7696 | } |
| 7697 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7698 | //===----------------------------------------------------------------------===// |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7699 | // Float Evaluation |
| 7700 | //===----------------------------------------------------------------------===// |
| 7701 | |
| 7702 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 7703 | class FloatExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7704 | : public ExprEvaluatorBase<FloatExprEvaluator> { |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7705 | APFloat &Result; |
| 7706 | public: |
| 7707 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7708 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7709 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7710 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7711 | Result = V.getFloat(); |
| 7712 | return true; |
| 7713 | } |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7714 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7715 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7716 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 7717 | return true; |
| 7718 | } |
| 7719 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7720 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7721 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7722 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7723 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 7724 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7725 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 7726 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7727 | bool VisitUnaryReal(const UnaryOperator *E); |
| 7728 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 7729 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7730 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7731 | }; |
| 7732 | } // end anonymous namespace |
| 7733 | |
| 7734 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7735 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7736 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7737 | } |
| 7738 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7739 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 7740 | QualType ResultTy, |
| 7741 | const Expr *Arg, |
| 7742 | bool SNaN, |
| 7743 | llvm::APFloat &Result) { |
| 7744 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 7745 | if (!S) return false; |
| 7746 | |
| 7747 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 7748 | |
| 7749 | llvm::APInt fill; |
| 7750 | |
| 7751 | // Treat empty strings as if they were zero. |
| 7752 | if (S->getString().empty()) |
| 7753 | fill = llvm::APInt(32, 0); |
| 7754 | else if (S->getString().getAsInteger(0, fill)) |
| 7755 | return false; |
| 7756 | |
Petar Jovanovic | d55ae6b | 2015-02-26 18:19:22 +0000 | [diff] [blame] | 7757 | if (Context.getTargetInfo().isNan2008()) { |
| 7758 | if (SNaN) |
| 7759 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 7760 | else |
| 7761 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 7762 | } else { |
| 7763 | // Prior to IEEE 754-2008, architectures were allowed to choose whether |
| 7764 | // the first bit of their significand was set for qNaN or sNaN. MIPS chose |
| 7765 | // a different encoding to what became a standard in 2008, and for pre- |
| 7766 | // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as |
| 7767 | // sNaN. This is now known as "legacy NaN" encoding. |
| 7768 | if (SNaN) |
| 7769 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 7770 | else |
| 7771 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 7772 | } |
| 7773 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 7774 | return true; |
| 7775 | } |
| 7776 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7777 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 7778 | switch (E->getBuiltinCallee()) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7779 | default: |
| 7780 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 7781 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7782 | case Builtin::BI__builtin_huge_val: |
| 7783 | case Builtin::BI__builtin_huge_valf: |
| 7784 | case Builtin::BI__builtin_huge_vall: |
| 7785 | case Builtin::BI__builtin_inf: |
| 7786 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 7787 | case Builtin::BI__builtin_infl: { |
| 7788 | const llvm::fltSemantics &Sem = |
| 7789 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 7790 | Result = llvm::APFloat::getInf(Sem); |
| 7791 | return true; |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 7792 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7793 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 7794 | case Builtin::BI__builtin_nans: |
| 7795 | case Builtin::BI__builtin_nansf: |
| 7796 | case Builtin::BI__builtin_nansl: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7797 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 7798 | true, Result)) |
| 7799 | return Error(E); |
| 7800 | return true; |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 7801 | |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 7802 | case Builtin::BI__builtin_nan: |
| 7803 | case Builtin::BI__builtin_nanf: |
| 7804 | case Builtin::BI__builtin_nanl: |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 7805 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 7806 | // can't constant fold it. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7807 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 7808 | false, Result)) |
| 7809 | return Error(E); |
| 7810 | return true; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7811 | |
| 7812 | case Builtin::BI__builtin_fabs: |
| 7813 | case Builtin::BI__builtin_fabsf: |
| 7814 | case Builtin::BI__builtin_fabsl: |
| 7815 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 7816 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7817 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7818 | if (Result.isNegative()) |
| 7819 | Result.changeSign(); |
| 7820 | return true; |
| 7821 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7822 | // FIXME: Builtin::BI__builtin_powi |
| 7823 | // FIXME: Builtin::BI__builtin_powif |
| 7824 | // FIXME: Builtin::BI__builtin_powil |
| 7825 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7826 | case Builtin::BI__builtin_copysign: |
| 7827 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7828 | case Builtin::BI__builtin_copysignl: { |
| 7829 | APFloat RHS(0.); |
| 7830 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 7831 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 7832 | return false; |
| 7833 | Result.copySign(RHS); |
| 7834 | return true; |
| 7835 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7836 | } |
| 7837 | } |
| 7838 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7839 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 7840 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 7841 | ComplexValue CV; |
| 7842 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 7843 | return false; |
| 7844 | Result = CV.FloatReal; |
| 7845 | return true; |
| 7846 | } |
| 7847 | |
| 7848 | return Visit(E->getSubExpr()); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7849 | } |
| 7850 | |
| 7851 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 7852 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 7853 | ComplexValue CV; |
| 7854 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 7855 | return false; |
| 7856 | Result = CV.FloatImag; |
| 7857 | return true; |
| 7858 | } |
| 7859 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 7860 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 7861 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 7862 | Result = llvm::APFloat::getZero(Sem); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7863 | return true; |
| 7864 | } |
| 7865 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7866 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7867 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7868 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7869 | case UO_Plus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 7870 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7871 | case UO_Minus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 7872 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 7873 | return false; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7874 | Result.changeSign(); |
| 7875 | return true; |
| 7876 | } |
| 7877 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7878 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7879 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7880 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 7881 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 141fbf3 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 7882 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7883 | APFloat RHS(0.0); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7884 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
| 7885 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7886 | return false; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 7887 | return EvaluateFloat(E->getRHS(), RHS, Info) && LHSOK && |
| 7888 | handleFloatFloatBinOp(Info, E, Result, E->getOpcode(), RHS); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7889 | } |
| 7890 | |
| 7891 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 7892 | Result = E->getValue(); |
| 7893 | return true; |
| 7894 | } |
| 7895 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7896 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 7897 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7898 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7899 | switch (E->getCastKind()) { |
| 7900 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7901 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7902 | |
| 7903 | case CK_IntegralToFloating: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7904 | APSInt IntResult; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7905 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 7906 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 7907 | E->getType(), Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7908 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7909 | |
| 7910 | case CK_FloatingCast: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7911 | if (!Visit(SubExpr)) |
| 7912 | return false; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7913 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 7914 | Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7915 | } |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 7916 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7917 | case CK_FloatingComplexToReal: { |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 7918 | ComplexValue V; |
| 7919 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 7920 | return false; |
| 7921 | Result = V.getComplexFloatReal(); |
| 7922 | return true; |
| 7923 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7924 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7925 | } |
| 7926 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7927 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 7928 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7929 | //===----------------------------------------------------------------------===// |
| 7930 | |
| 7931 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 7932 | class ComplexExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7933 | : public ExprEvaluatorBase<ComplexExprEvaluator> { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7934 | ComplexValue &Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7935 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7936 | public: |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7937 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7938 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 7939 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7940 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7941 | Result.setFrom(V); |
| 7942 | return true; |
| 7943 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7944 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7945 | bool ZeroInitialization(const Expr *E); |
| 7946 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7947 | //===--------------------------------------------------------------------===// |
| 7948 | // Visitor Methods |
| 7949 | //===--------------------------------------------------------------------===// |
| 7950 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7951 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7952 | bool VisitCastExpr(const CastExpr *E); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7953 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 7954 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7955 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7956 | }; |
| 7957 | } // end anonymous namespace |
| 7958 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7959 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 7960 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7961 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7962 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7963 | } |
| 7964 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7965 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7966 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7967 | if (ElemTy->isRealFloatingType()) { |
| 7968 | Result.makeComplexFloat(); |
| 7969 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 7970 | Result.FloatReal = Zero; |
| 7971 | Result.FloatImag = Zero; |
| 7972 | } else { |
| 7973 | Result.makeComplexInt(); |
| 7974 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 7975 | Result.IntReal = Zero; |
| 7976 | Result.IntImag = Zero; |
| 7977 | } |
| 7978 | return true; |
| 7979 | } |
| 7980 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7981 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 7982 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7983 | |
| 7984 | if (SubExpr->getType()->isRealFloatingType()) { |
| 7985 | Result.makeComplexFloat(); |
| 7986 | APFloat &Imag = Result.FloatImag; |
| 7987 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 7988 | return false; |
| 7989 | |
| 7990 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 7991 | return true; |
| 7992 | } else { |
| 7993 | assert(SubExpr->getType()->isIntegerType() && |
| 7994 | "Unexpected imaginary literal."); |
| 7995 | |
| 7996 | Result.makeComplexInt(); |
| 7997 | APSInt &Imag = Result.IntImag; |
| 7998 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 7999 | return false; |
| 8000 | |
| 8001 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 8002 | return true; |
| 8003 | } |
| 8004 | } |
| 8005 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 8006 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 8007 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8008 | switch (E->getCastKind()) { |
| 8009 | case CK_BitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8010 | case CK_BaseToDerived: |
| 8011 | case CK_DerivedToBase: |
| 8012 | case CK_UncheckedDerivedToBase: |
| 8013 | case CK_Dynamic: |
| 8014 | case CK_ToUnion: |
| 8015 | case CK_ArrayToPointerDecay: |
| 8016 | case CK_FunctionToPointerDecay: |
| 8017 | case CK_NullToPointer: |
| 8018 | case CK_NullToMemberPointer: |
| 8019 | case CK_BaseToDerivedMemberPointer: |
| 8020 | case CK_DerivedToBaseMemberPointer: |
| 8021 | case CK_MemberPointerToBoolean: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 8022 | case CK_ReinterpretMemberPointer: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8023 | case CK_ConstructorConversion: |
| 8024 | case CK_IntegralToPointer: |
| 8025 | case CK_PointerToIntegral: |
| 8026 | case CK_PointerToBoolean: |
| 8027 | case CK_ToVoid: |
| 8028 | case CK_VectorSplat: |
| 8029 | case CK_IntegralCast: |
| 8030 | case CK_IntegralToBoolean: |
| 8031 | case CK_IntegralToFloating: |
| 8032 | case CK_FloatingToIntegral: |
| 8033 | case CK_FloatingToBoolean: |
| 8034 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 8035 | case CK_CPointerToObjCPointerCast: |
| 8036 | case CK_BlockPointerToObjCPointerCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8037 | case CK_AnyPointerToBlockPointerCast: |
| 8038 | case CK_ObjCObjectLValueCast: |
| 8039 | case CK_FloatingComplexToReal: |
| 8040 | case CK_FloatingComplexToBoolean: |
| 8041 | case CK_IntegralComplexToReal: |
| 8042 | case CK_IntegralComplexToBoolean: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 8043 | case CK_ARCProduceObject: |
| 8044 | case CK_ARCConsumeObject: |
| 8045 | case CK_ARCReclaimReturnedObject: |
| 8046 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8047 | case CK_CopyAndAutoreleaseBlockObject: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 8048 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 8049 | case CK_ZeroToOCLEvent: |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8050 | case CK_NonAtomicToAtomic: |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 8051 | case CK_AddressSpaceConversion: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8052 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 8053 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8054 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 8055 | case CK_AtomicToNonAtomic: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8056 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8057 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8058 | |
| 8059 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 8060 | case CK_LValueBitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8061 | case CK_UserDefinedConversion: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8062 | return Error(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8063 | |
| 8064 | case CK_FloatingRealToComplex: { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 8065 | APFloat &Real = Result.FloatReal; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8066 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 8067 | return false; |
| 8068 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8069 | Result.makeComplexFloat(); |
| 8070 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 8071 | return true; |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 8072 | } |
| 8073 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8074 | case CK_FloatingComplexCast: { |
| 8075 | if (!Visit(E->getSubExpr())) |
| 8076 | return false; |
| 8077 | |
| 8078 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 8079 | QualType From |
| 8080 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 8081 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 8082 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 8083 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8084 | } |
| 8085 | |
| 8086 | case CK_FloatingComplexToIntegralComplex: { |
| 8087 | if (!Visit(E->getSubExpr())) |
| 8088 | return false; |
| 8089 | |
| 8090 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 8091 | QualType From |
| 8092 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 8093 | Result.makeComplexInt(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 8094 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 8095 | To, Result.IntReal) && |
| 8096 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 8097 | To, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8098 | } |
| 8099 | |
| 8100 | case CK_IntegralRealToComplex: { |
| 8101 | APSInt &Real = Result.IntReal; |
| 8102 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 8103 | return false; |
| 8104 | |
| 8105 | Result.makeComplexInt(); |
| 8106 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 8107 | return true; |
| 8108 | } |
| 8109 | |
| 8110 | case CK_IntegralComplexCast: { |
| 8111 | if (!Visit(E->getSubExpr())) |
| 8112 | return false; |
| 8113 | |
| 8114 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 8115 | QualType From |
| 8116 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 8117 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 8118 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 8119 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8120 | return true; |
| 8121 | } |
| 8122 | |
| 8123 | case CK_IntegralComplexToFloatingComplex: { |
| 8124 | if (!Visit(E->getSubExpr())) |
| 8125 | return false; |
| 8126 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 8127 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8128 | QualType From |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 8129 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8130 | Result.makeComplexFloat(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 8131 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 8132 | To, Result.FloatReal) && |
| 8133 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 8134 | To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8135 | } |
| 8136 | } |
| 8137 | |
| 8138 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 8139 | } |
| 8140 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8141 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8142 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 10f4d06 | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 8143 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 8144 | |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8145 | // Track whether the LHS or RHS is real at the type system level. When this is |
| 8146 | // the case we can simplify our evaluation strategy. |
| 8147 | bool LHSReal = false, RHSReal = false; |
| 8148 | |
| 8149 | bool LHSOK; |
| 8150 | if (E->getLHS()->getType()->isRealFloatingType()) { |
| 8151 | LHSReal = true; |
| 8152 | APFloat &Real = Result.FloatReal; |
| 8153 | LHSOK = EvaluateFloat(E->getLHS(), Real, Info); |
| 8154 | if (LHSOK) { |
| 8155 | Result.makeComplexFloat(); |
| 8156 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 8157 | } |
| 8158 | } else { |
| 8159 | LHSOK = Visit(E->getLHS()); |
| 8160 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8161 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8162 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8163 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8164 | ComplexValue RHS; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8165 | if (E->getRHS()->getType()->isRealFloatingType()) { |
| 8166 | RHSReal = true; |
| 8167 | APFloat &Real = RHS.FloatReal; |
| 8168 | if (!EvaluateFloat(E->getRHS(), Real, Info) || !LHSOK) |
| 8169 | return false; |
| 8170 | RHS.makeComplexFloat(); |
| 8171 | RHS.FloatImag = APFloat(Real.getSemantics()); |
| 8172 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8173 | return false; |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 8174 | |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8175 | assert(!(LHSReal && RHSReal) && |
| 8176 | "Cannot have both operands of a complex operation be real."); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 8177 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8178 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8179 | case BO_Add: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 8180 | if (Result.isComplexFloat()) { |
| 8181 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 8182 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8183 | if (LHSReal) |
| 8184 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
| 8185 | else if (!RHSReal) |
| 8186 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 8187 | APFloat::rmNearestTiesToEven); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 8188 | } else { |
| 8189 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 8190 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 8191 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 8192 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8193 | case BO_Sub: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 8194 | if (Result.isComplexFloat()) { |
| 8195 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 8196 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8197 | if (LHSReal) { |
| 8198 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
| 8199 | Result.getComplexFloatImag().changeSign(); |
| 8200 | } else if (!RHSReal) { |
| 8201 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 8202 | APFloat::rmNearestTiesToEven); |
| 8203 | } |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 8204 | } else { |
| 8205 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 8206 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 8207 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 8208 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8209 | case BO_Mul: |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 8210 | if (Result.isComplexFloat()) { |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8211 | // This is an implementation of complex multiplication according to the |
| 8212 | // constraints laid out in C11 Annex G. The implemantion uses the |
| 8213 | // following naming scheme: |
| 8214 | // (a + ib) * (c + id) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8215 | ComplexValue LHS = Result; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8216 | APFloat &A = LHS.getComplexFloatReal(); |
| 8217 | APFloat &B = LHS.getComplexFloatImag(); |
| 8218 | APFloat &C = RHS.getComplexFloatReal(); |
| 8219 | APFloat &D = RHS.getComplexFloatImag(); |
| 8220 | APFloat &ResR = Result.getComplexFloatReal(); |
| 8221 | APFloat &ResI = Result.getComplexFloatImag(); |
| 8222 | if (LHSReal) { |
| 8223 | assert(!RHSReal && "Cannot have two real operands for a complex op!"); |
| 8224 | ResR = A * C; |
| 8225 | ResI = A * D; |
| 8226 | } else if (RHSReal) { |
| 8227 | ResR = C * A; |
| 8228 | ResI = C * B; |
| 8229 | } else { |
| 8230 | // In the fully general case, we need to handle NaNs and infinities |
| 8231 | // robustly. |
| 8232 | APFloat AC = A * C; |
| 8233 | APFloat BD = B * D; |
| 8234 | APFloat AD = A * D; |
| 8235 | APFloat BC = B * C; |
| 8236 | ResR = AC - BD; |
| 8237 | ResI = AD + BC; |
| 8238 | if (ResR.isNaN() && ResI.isNaN()) { |
| 8239 | bool Recalc = false; |
| 8240 | if (A.isInfinity() || B.isInfinity()) { |
| 8241 | A = APFloat::copySign( |
| 8242 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
| 8243 | B = APFloat::copySign( |
| 8244 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
| 8245 | if (C.isNaN()) |
| 8246 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
| 8247 | if (D.isNaN()) |
| 8248 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
| 8249 | Recalc = true; |
| 8250 | } |
| 8251 | if (C.isInfinity() || D.isInfinity()) { |
| 8252 | C = APFloat::copySign( |
| 8253 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
| 8254 | D = APFloat::copySign( |
| 8255 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
| 8256 | if (A.isNaN()) |
| 8257 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
| 8258 | if (B.isNaN()) |
| 8259 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
| 8260 | Recalc = true; |
| 8261 | } |
| 8262 | if (!Recalc && (AC.isInfinity() || BD.isInfinity() || |
| 8263 | AD.isInfinity() || BC.isInfinity())) { |
| 8264 | if (A.isNaN()) |
| 8265 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
| 8266 | if (B.isNaN()) |
| 8267 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
| 8268 | if (C.isNaN()) |
| 8269 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
| 8270 | if (D.isNaN()) |
| 8271 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
| 8272 | Recalc = true; |
| 8273 | } |
| 8274 | if (Recalc) { |
| 8275 | ResR = APFloat::getInf(A.getSemantics()) * (A * C - B * D); |
| 8276 | ResI = APFloat::getInf(A.getSemantics()) * (A * D + B * C); |
| 8277 | } |
| 8278 | } |
| 8279 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 8280 | } else { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8281 | ComplexValue LHS = Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8282 | Result.getComplexIntReal() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 8283 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 8284 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8285 | Result.getComplexIntImag() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 8286 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 8287 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 8288 | } |
| 8289 | break; |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 8290 | case BO_Div: |
| 8291 | if (Result.isComplexFloat()) { |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8292 | // This is an implementation of complex division according to the |
| 8293 | // constraints laid out in C11 Annex G. The implemantion uses the |
| 8294 | // following naming scheme: |
| 8295 | // (a + ib) / (c + id) |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 8296 | ComplexValue LHS = Result; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8297 | APFloat &A = LHS.getComplexFloatReal(); |
| 8298 | APFloat &B = LHS.getComplexFloatImag(); |
| 8299 | APFloat &C = RHS.getComplexFloatReal(); |
| 8300 | APFloat &D = RHS.getComplexFloatImag(); |
| 8301 | APFloat &ResR = Result.getComplexFloatReal(); |
| 8302 | APFloat &ResI = Result.getComplexFloatImag(); |
| 8303 | if (RHSReal) { |
| 8304 | ResR = A / C; |
| 8305 | ResI = B / C; |
| 8306 | } else { |
| 8307 | if (LHSReal) { |
| 8308 | // No real optimizations we can do here, stub out with zero. |
| 8309 | B = APFloat::getZero(A.getSemantics()); |
| 8310 | } |
| 8311 | int DenomLogB = 0; |
| 8312 | APFloat MaxCD = maxnum(abs(C), abs(D)); |
| 8313 | if (MaxCD.isFinite()) { |
| 8314 | DenomLogB = ilogb(MaxCD); |
| 8315 | C = scalbn(C, -DenomLogB); |
| 8316 | D = scalbn(D, -DenomLogB); |
| 8317 | } |
| 8318 | APFloat Denom = C * C + D * D; |
| 8319 | ResR = scalbn((A * C + B * D) / Denom, -DenomLogB); |
| 8320 | ResI = scalbn((B * C - A * D) / Denom, -DenomLogB); |
| 8321 | if (ResR.isNaN() && ResI.isNaN()) { |
| 8322 | if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) { |
| 8323 | ResR = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * A; |
| 8324 | ResI = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * B; |
| 8325 | } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() && |
| 8326 | D.isFinite()) { |
| 8327 | A = APFloat::copySign( |
| 8328 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
| 8329 | B = APFloat::copySign( |
| 8330 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
| 8331 | ResR = APFloat::getInf(ResR.getSemantics()) * (A * C + B * D); |
| 8332 | ResI = APFloat::getInf(ResI.getSemantics()) * (B * C - A * D); |
| 8333 | } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) { |
| 8334 | C = APFloat::copySign( |
| 8335 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
| 8336 | D = APFloat::copySign( |
| 8337 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
| 8338 | ResR = APFloat::getZero(ResR.getSemantics()) * (A * C + B * D); |
| 8339 | ResI = APFloat::getZero(ResI.getSemantics()) * (B * C - A * D); |
| 8340 | } |
| 8341 | } |
| 8342 | } |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 8343 | } else { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8344 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 8345 | return Error(E, diag::note_expr_divide_by_zero); |
| 8346 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 8347 | ComplexValue LHS = Result; |
| 8348 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 8349 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 8350 | Result.getComplexIntReal() = |
| 8351 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 8352 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 8353 | Result.getComplexIntImag() = |
| 8354 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 8355 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 8356 | } |
| 8357 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 8358 | } |
| 8359 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8360 | return true; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 8361 | } |
| 8362 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 8363 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 8364 | // Get the operand value into 'Result'. |
| 8365 | if (!Visit(E->getSubExpr())) |
| 8366 | return false; |
| 8367 | |
| 8368 | switch (E->getOpcode()) { |
| 8369 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8370 | return Error(E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 8371 | case UO_Extension: |
| 8372 | return true; |
| 8373 | case UO_Plus: |
| 8374 | // The result is always just the subexpr. |
| 8375 | return true; |
| 8376 | case UO_Minus: |
| 8377 | if (Result.isComplexFloat()) { |
| 8378 | Result.getComplexFloatReal().changeSign(); |
| 8379 | Result.getComplexFloatImag().changeSign(); |
| 8380 | } |
| 8381 | else { |
| 8382 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 8383 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 8384 | } |
| 8385 | return true; |
| 8386 | case UO_Not: |
| 8387 | if (Result.isComplexFloat()) |
| 8388 | Result.getComplexFloatImag().changeSign(); |
| 8389 | else |
| 8390 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 8391 | return true; |
| 8392 | } |
| 8393 | } |
| 8394 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 8395 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 8396 | if (E->getNumInits() == 2) { |
| 8397 | if (E->getType()->isComplexType()) { |
| 8398 | Result.makeComplexFloat(); |
| 8399 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 8400 | return false; |
| 8401 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 8402 | return false; |
| 8403 | } else { |
| 8404 | Result.makeComplexInt(); |
| 8405 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 8406 | return false; |
| 8407 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 8408 | return false; |
| 8409 | } |
| 8410 | return true; |
| 8411 | } |
| 8412 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 8413 | } |
| 8414 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 8415 | //===----------------------------------------------------------------------===// |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8416 | // Atomic expression evaluation, essentially just handling the NonAtomicToAtomic |
| 8417 | // implicit conversion. |
| 8418 | //===----------------------------------------------------------------------===// |
| 8419 | |
| 8420 | namespace { |
| 8421 | class AtomicExprEvaluator : |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 8422 | public ExprEvaluatorBase<AtomicExprEvaluator> { |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8423 | APValue &Result; |
| 8424 | public: |
| 8425 | AtomicExprEvaluator(EvalInfo &Info, APValue &Result) |
| 8426 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 8427 | |
| 8428 | bool Success(const APValue &V, const Expr *E) { |
| 8429 | Result = V; |
| 8430 | return true; |
| 8431 | } |
| 8432 | |
| 8433 | bool ZeroInitialization(const Expr *E) { |
| 8434 | ImplicitValueInitExpr VIE( |
| 8435 | E->getType()->castAs<AtomicType>()->getValueType()); |
| 8436 | return Evaluate(Result, Info, &VIE); |
| 8437 | } |
| 8438 | |
| 8439 | bool VisitCastExpr(const CastExpr *E) { |
| 8440 | switch (E->getCastKind()) { |
| 8441 | default: |
| 8442 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 8443 | case CK_NonAtomicToAtomic: |
| 8444 | return Evaluate(Result, Info, E->getSubExpr()); |
| 8445 | } |
| 8446 | } |
| 8447 | }; |
| 8448 | } // end anonymous namespace |
| 8449 | |
| 8450 | static bool EvaluateAtomic(const Expr *E, APValue &Result, EvalInfo &Info) { |
| 8451 | assert(E->isRValue() && E->getType()->isAtomicType()); |
| 8452 | return AtomicExprEvaluator(Info, Result).Visit(E); |
| 8453 | } |
| 8454 | |
| 8455 | //===----------------------------------------------------------------------===// |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 8456 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 8457 | // comma operator |
| 8458 | //===----------------------------------------------------------------------===// |
| 8459 | |
| 8460 | namespace { |
| 8461 | class VoidExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 8462 | : public ExprEvaluatorBase<VoidExprEvaluator> { |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 8463 | public: |
| 8464 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 8465 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8466 | bool Success(const APValue &V, const Expr *e) { return true; } |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 8467 | |
| 8468 | bool VisitCastExpr(const CastExpr *E) { |
| 8469 | switch (E->getCastKind()) { |
| 8470 | default: |
| 8471 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 8472 | case CK_ToVoid: |
| 8473 | VisitIgnoredValue(E->getSubExpr()); |
| 8474 | return true; |
| 8475 | } |
| 8476 | } |
Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 8477 | |
| 8478 | bool VisitCallExpr(const CallExpr *E) { |
| 8479 | switch (E->getBuiltinCallee()) { |
| 8480 | default: |
| 8481 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 8482 | case Builtin::BI__assume: |
Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 8483 | case Builtin::BI__builtin_assume: |
Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 8484 | // The argument is not evaluated! |
| 8485 | return true; |
| 8486 | } |
| 8487 | } |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 8488 | }; |
| 8489 | } // end anonymous namespace |
| 8490 | |
| 8491 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 8492 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 8493 | return VoidExprEvaluator(Info).Visit(E); |
| 8494 | } |
| 8495 | |
| 8496 | //===----------------------------------------------------------------------===// |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8497 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 8498 | //===----------------------------------------------------------------------===// |
| 8499 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8500 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8501 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 8502 | // are. |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8503 | QualType T = E->getType(); |
| 8504 | if (E->isGLValue() || T->isFunctionType()) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8505 | LValue LV; |
| 8506 | if (!EvaluateLValue(E, LV, Info)) |
| 8507 | return false; |
| 8508 | LV.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8509 | } else if (T->isVectorType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 8510 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 8511 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8512 | } else if (T->isIntegralOrEnumerationType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 8513 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 8514 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8515 | } else if (T->hasPointerRepresentation()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 8516 | LValue LV; |
| 8517 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 8518 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 8519 | LV.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8520 | } else if (T->isRealFloatingType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 8521 | llvm::APFloat F(0.0); |
| 8522 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 8523 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8524 | Result = APValue(F); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8525 | } else if (T->isAnyComplexType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 8526 | ComplexValue C; |
| 8527 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 8528 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 8529 | C.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8530 | } else if (T->isMemberPointerType()) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8531 | MemberPtr P; |
| 8532 | if (!EvaluateMemberPointer(E, P, Info)) |
| 8533 | return false; |
| 8534 | P.moveInto(Result); |
| 8535 | return true; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8536 | } else if (T->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8537 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8538 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 8539 | APValue &Value = Info.CurrentCall->createTemporary(E, false); |
| 8540 | if (!EvaluateArray(E, LV, Value, Info)) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 8541 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 8542 | Result = Value; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8543 | } else if (T->isRecordType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8544 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8545 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 8546 | APValue &Value = Info.CurrentCall->createTemporary(E, false); |
| 8547 | if (!EvaluateRecord(E, LV, Value, Info)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8548 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 8549 | Result = Value; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8550 | } else if (T->isVoidType()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8551 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 8552 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 8553 | << E->getType(); |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 8554 | if (!EvaluateVoid(E, Info)) |
| 8555 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8556 | } else if (T->isAtomicType()) { |
| 8557 | if (!EvaluateAtomic(E, Result, Info)) |
| 8558 | return false; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8559 | } else if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 8560 | Info.Diag(E, diag::note_constexpr_nonliteral) << E->getType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 8561 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8562 | } else { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 8563 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 7c282e4 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 8564 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8565 | } |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 8566 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 8567 | return true; |
| 8568 | } |
| 8569 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8570 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
| 8571 | /// cases, the in-place evaluation is essential, since later initializers for |
| 8572 | /// an object can indirectly refer to subobjects which were initialized earlier. |
| 8573 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8574 | const Expr *E, bool AllowNonLiteralTypes) { |
Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 8575 | assert(!E->isValueDependent()); |
| 8576 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8577 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, &This)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8578 | return false; |
| 8579 | |
| 8580 | if (E->isRValue()) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 8581 | // Evaluate arrays and record types in-place, so that later initializers can |
| 8582 | // refer to earlier-initialized members of the object. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8583 | if (E->getType()->isArrayType()) |
| 8584 | return EvaluateArray(E, This, Result, Info); |
| 8585 | else if (E->getType()->isRecordType()) |
| 8586 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 8587 | } |
| 8588 | |
| 8589 | // For any other type, in-place evaluation is unimportant. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8590 | return Evaluate(Result, Info, E); |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 8591 | } |
| 8592 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8593 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 8594 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 8595 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 8596 | if (E->getType().isNull()) |
| 8597 | return false; |
| 8598 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8599 | if (!CheckLiteralType(Info, E)) |
| 8600 | return false; |
| 8601 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8602 | if (!::Evaluate(Result, Info, E)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8603 | return false; |
| 8604 | |
| 8605 | if (E->isGLValue()) { |
| 8606 | LValue LV; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8607 | LV.setFrom(Info.Ctx, Result); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 8608 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8609 | return false; |
| 8610 | } |
| 8611 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8612 | // Check this core constant expression is a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8613 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8614 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8615 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8616 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
| 8617 | const ASTContext &Ctx, bool &IsConst) { |
| 8618 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 8619 | // containing vast quantities of these. |
| 8620 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Exp)) { |
| 8621 | Result.Val = APValue(APSInt(L->getValue(), |
| 8622 | L->getType()->isUnsignedIntegerType())); |
| 8623 | IsConst = true; |
| 8624 | return true; |
| 8625 | } |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 8626 | |
| 8627 | // This case should be rare, but we need to check it before we check on |
| 8628 | // the type below. |
| 8629 | if (Exp->getType().isNull()) { |
| 8630 | IsConst = false; |
| 8631 | return true; |
| 8632 | } |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8633 | |
| 8634 | // FIXME: Evaluating values of large array and record types can cause |
| 8635 | // performance problems. Only do so in C++11 for now. |
| 8636 | if (Exp->isRValue() && (Exp->getType()->isArrayType() || |
| 8637 | Exp->getType()->isRecordType()) && |
| 8638 | !Ctx.getLangOpts().CPlusPlus11) { |
| 8639 | IsConst = false; |
| 8640 | return true; |
| 8641 | } |
| 8642 | return false; |
| 8643 | } |
| 8644 | |
| 8645 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8646 | /// EvaluateAsRValue - Return true if this is a constant which we can fold using |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 8647 | /// any crazy technique (that has nothing to do with language standards) that |
| 8648 | /// we want to. If this function returns true, it returns the folded constant |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8649 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 8650 | /// will be applied to the result. |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8651 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8652 | bool IsConst; |
| 8653 | if (FastEvaluateAsRValue(this, Result, Ctx, IsConst)) |
| 8654 | return IsConst; |
| 8655 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 8656 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8657 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 8658 | } |
| 8659 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8660 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 8661 | const ASTContext &Ctx) const { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8662 | EvalResult Scratch; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8663 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8664 | HandleConversionToBool(Scratch.Val, Result); |
John McCall | 1be1c63 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 8665 | } |
| 8666 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 8667 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, |
| 8668 | SideEffectsKind AllowSideEffects) const { |
| 8669 | if (!getType()->isIntegralOrEnumerationType()) |
| 8670 | return false; |
| 8671 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8672 | EvalResult ExprResult; |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 8673 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || |
| 8674 | (!AllowSideEffects && ExprResult.HasSideEffects)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8675 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8676 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8677 | Result = ExprResult.Val.getInt(); |
| 8678 | return true; |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8679 | } |
| 8680 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8681 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 8682 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
Anders Carlsson | 4316812 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 8683 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 8684 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8685 | if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || |
| 8686 | !CheckLValueConstantExpression(Info, getExprLoc(), |
| 8687 | Ctx.getLValueReferenceType(getType()), LV)) |
| 8688 | return false; |
| 8689 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8690 | LV.moveInto(Result.Val); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8691 | return true; |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 8692 | } |
| 8693 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8694 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 8695 | const VarDecl *VD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8696 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 8697 | // FIXME: Evaluating initializers for large array and record types can cause |
| 8698 | // performance problems. Only do so in C++11 for now. |
| 8699 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8700 | !Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 8701 | return false; |
| 8702 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8703 | Expr::EvalStatus EStatus; |
| 8704 | EStatus.Diag = &Notes; |
| 8705 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 8706 | EvalInfo InitInfo(Ctx, EStatus, EvalInfo::EM_ConstantFold); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8707 | InitInfo.setEvaluatingDecl(VD, Value); |
| 8708 | |
| 8709 | LValue LVal; |
| 8710 | LVal.set(VD); |
| 8711 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8712 | // C++11 [basic.start.init]p2: |
| 8713 | // Variables with static storage duration or thread storage duration shall be |
| 8714 | // zero-initialized before any other initialization takes place. |
| 8715 | // This behavior is not present in C. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8716 | if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8717 | !VD->getType()->isReferenceType()) { |
| 8718 | ImplicitValueInitExpr VIE(VD->getType()); |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8719 | if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8720 | /*AllowNonLiteralTypes=*/true)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8721 | return false; |
| 8722 | } |
| 8723 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8724 | if (!EvaluateInPlace(Value, InitInfo, LVal, this, |
| 8725 | /*AllowNonLiteralTypes=*/true) || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8726 | EStatus.HasSideEffects) |
| 8727 | return false; |
| 8728 | |
| 8729 | return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), |
| 8730 | Value); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8731 | } |
| 8732 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8733 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 8734 | /// constant folded, but discard the result. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8735 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 8736 | EvalResult Result; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8737 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 8738 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 8739 | |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 8740 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8741 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 8742 | EvalResult EvalResult; |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 8743 | EvalResult.Diag = Diag; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8744 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 8745 | (void)Result; |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 8746 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 8747 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 8748 | |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 8749 | return EvalResult.Val.getInt(); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 8750 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8751 | |
Richard Smith | e9ff770 | 2013-11-05 22:23:30 +0000 | [diff] [blame] | 8752 | void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8753 | bool IsConst; |
| 8754 | EvalResult EvalResult; |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8755 | if (!FastEvaluateAsRValue(this, EvalResult, Ctx, IsConst)) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 8756 | EvalInfo Info(Ctx, EvalResult, EvalInfo::EM_EvaluateForOverflow); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8757 | (void)::EvaluateAsRValue(Info, this, EvalResult.Val); |
| 8758 | } |
| 8759 | } |
| 8760 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 8761 | bool Expr::EvalResult::isGlobalLValue() const { |
| 8762 | assert(Val.isLValue()); |
| 8763 | return IsGlobalLValue(Val.getLValueBase()); |
| 8764 | } |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 8765 | |
| 8766 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8767 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 8768 | /// an integer constant expression. |
| 8769 | |
| 8770 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 8771 | /// comma, etc |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8772 | |
| 8773 | // CheckICE - This function does the fundamental ICE checking: the returned |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8774 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
| 8775 | // and a (possibly null) SourceLocation indicating the location of the problem. |
| 8776 | // |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8777 | // Note that to reduce code duplication, this helper does no evaluation |
| 8778 | // itself; the caller checks whether the expression is evaluatable, and |
| 8779 | // in the rare cases where CheckICE actually cares about the evaluated |
| 8780 | // value, it calls into Evalute. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8781 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 8782 | namespace { |
| 8783 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8784 | enum ICEKind { |
| 8785 | /// This expression is an ICE. |
| 8786 | IK_ICE, |
| 8787 | /// This expression is not an ICE, but if it isn't evaluated, it's |
| 8788 | /// a legal subexpression for an ICE. This return value is used to handle |
| 8789 | /// the comma operator in C99 mode, and non-constant subexpressions. |
| 8790 | IK_ICEIfUnevaluated, |
| 8791 | /// This expression is not an ICE, and is not a legal subexpression for one. |
| 8792 | IK_NotICE |
| 8793 | }; |
| 8794 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8795 | struct ICEDiag { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8796 | ICEKind Kind; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8797 | SourceLocation Loc; |
| 8798 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8799 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8800 | }; |
| 8801 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8802 | } |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 8803 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8804 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
| 8805 | |
| 8806 | static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8807 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 8808 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8809 | Expr::EvalResult EVResult; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8810 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8811 | !EVResult.Val.isInt()) |
| 8812 | return ICEDiag(IK_NotICE, E->getLocStart()); |
| 8813 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8814 | return NoDiag(); |
| 8815 | } |
| 8816 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 8817 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8818 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8819 | if (!E->getType()->isIntegralOrEnumerationType()) |
| 8820 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8821 | |
| 8822 | switch (E->getStmtClass()) { |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 8823 | #define ABSTRACT_STMT(Node) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8824 | #define STMT(Node, Base) case Expr::Node##Class: |
| 8825 | #define EXPR(Node, Base) |
| 8826 | #include "clang/AST/StmtNodes.inc" |
| 8827 | case Expr::PredefinedExprClass: |
| 8828 | case Expr::FloatingLiteralClass: |
| 8829 | case Expr::ImaginaryLiteralClass: |
| 8830 | case Expr::StringLiteralClass: |
| 8831 | case Expr::ArraySubscriptExprClass: |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 8832 | case Expr::OMPArraySectionExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8833 | case Expr::MemberExprClass: |
| 8834 | case Expr::CompoundAssignOperatorClass: |
| 8835 | case Expr::CompoundLiteralExprClass: |
| 8836 | case Expr::ExtVectorElementExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8837 | case Expr::DesignatedInitExprClass: |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 8838 | case Expr::NoInitExprClass: |
| 8839 | case Expr::DesignatedInitUpdateExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8840 | case Expr::ImplicitValueInitExprClass: |
| 8841 | case Expr::ParenListExprClass: |
| 8842 | case Expr::VAArgExprClass: |
| 8843 | case Expr::AddrLabelExprClass: |
| 8844 | case Expr::StmtExprClass: |
| 8845 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 8846 | case Expr::CUDAKernelCallExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8847 | case Expr::CXXDynamicCastExprClass: |
| 8848 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 5cc0a67 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 8849 | case Expr::CXXUuidofExprClass: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 8850 | case Expr::MSPropertyRefExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8851 | case Expr::CXXNullPtrLiteralExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 8852 | case Expr::UserDefinedLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8853 | case Expr::CXXThisExprClass: |
| 8854 | case Expr::CXXThrowExprClass: |
| 8855 | case Expr::CXXNewExprClass: |
| 8856 | case Expr::CXXDeleteExprClass: |
| 8857 | case Expr::CXXPseudoDestructorExprClass: |
| 8858 | case Expr::UnresolvedLookupExprClass: |
Kaelyn Takata | e1f49d5 | 2014-10-27 18:07:20 +0000 | [diff] [blame] | 8859 | case Expr::TypoExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8860 | case Expr::DependentScopeDeclRefExprClass: |
| 8861 | case Expr::CXXConstructExprClass: |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8862 | case Expr::CXXStdInitializerListExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8863 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 8864 | case Expr::ExprWithCleanupsClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8865 | case Expr::CXXTemporaryObjectExprClass: |
| 8866 | case Expr::CXXUnresolvedConstructExprClass: |
| 8867 | case Expr::CXXDependentScopeMemberExprClass: |
| 8868 | case Expr::UnresolvedMemberExprClass: |
| 8869 | case Expr::ObjCStringLiteralClass: |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 8870 | case Expr::ObjCBoxedExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 8871 | case Expr::ObjCArrayLiteralClass: |
| 8872 | case Expr::ObjCDictionaryLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8873 | case Expr::ObjCEncodeExprClass: |
| 8874 | case Expr::ObjCMessageExprClass: |
| 8875 | case Expr::ObjCSelectorExprClass: |
| 8876 | case Expr::ObjCProtocolExprClass: |
| 8877 | case Expr::ObjCIvarRefExprClass: |
| 8878 | case Expr::ObjCPropertyRefExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 8879 | case Expr::ObjCSubscriptRefExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8880 | case Expr::ObjCIsaExprClass: |
| 8881 | case Expr::ShuffleVectorExprClass: |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 8882 | case Expr::ConvertVectorExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8883 | case Expr::BlockExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8884 | case Expr::NoStmtClass: |
John McCall | 8d69a21 | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 8885 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 8886 | case Expr::PackExpansionExprClass: |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 8887 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 8888 | case Expr::FunctionParmPackExprClass: |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 8889 | case Expr::AsTypeExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8890 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 8891 | case Expr::MaterializeTemporaryExprClass: |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 8892 | case Expr::PseudoObjectExprClass: |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 8893 | case Expr::AtomicExprClass: |
Douglas Gregor | e31e606 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 8894 | case Expr::LambdaExprClass: |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 8895 | case Expr::CXXFoldExprClass: |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8896 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 8897 | |
Richard Smith | f137f93 | 2014-01-25 20:50:08 +0000 | [diff] [blame] | 8898 | case Expr::InitListExprClass: { |
| 8899 | // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the |
| 8900 | // form "T x = { a };" is equivalent to "T x = a;". |
| 8901 | // Unless we're initializing a reference, T is a scalar as it is known to be |
| 8902 | // of integral or enumeration type. |
| 8903 | if (E->isRValue()) |
| 8904 | if (cast<InitListExpr>(E)->getNumInits() == 1) |
| 8905 | return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx); |
| 8906 | return ICEDiag(IK_NotICE, E->getLocStart()); |
| 8907 | } |
| 8908 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 8909 | case Expr::SizeOfPackExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8910 | case Expr::GNUNullExprClass: |
| 8911 | // GCC considers the GNU __null value to be an integral constant expression. |
| 8912 | return NoDiag(); |
| 8913 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 8914 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 8915 | return |
| 8916 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 8917 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8918 | case Expr::ParenExprClass: |
| 8919 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 8920 | case Expr::GenericSelectionExprClass: |
| 8921 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8922 | case Expr::IntegerLiteralClass: |
| 8923 | case Expr::CharacterLiteralClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 8924 | case Expr::ObjCBoolLiteralExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8925 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 8926 | case Expr::CXXScalarValueInitExprClass: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 8927 | case Expr::TypeTraitExprClass: |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 8928 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 8929 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 8930 | case Expr::CXXNoexceptExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8931 | return NoDiag(); |
| 8932 | case Expr::CallExprClass: |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 8933 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 8934 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 8935 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 8936 | // contain an operand of (pointer to) function type. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8937 | const CallExpr *CE = cast<CallExpr>(E); |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 8938 | if (CE->getBuiltinCallee()) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8939 | return CheckEvalInICE(E, Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8940 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8941 | } |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 8942 | case Expr::DeclRefExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8943 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 8944 | return NoDiag(); |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 8945 | const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl()); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8946 | if (Ctx.getLangOpts().CPlusPlus && |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 8947 | D && IsConstNonVolatile(D->getType())) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8948 | // Parameter variables are never constants. Without this check, |
| 8949 | // getAnyInitializer() can find a default argument, which leads |
| 8950 | // to chaos. |
| 8951 | if (isa<ParmVarDecl>(D)) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8952 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8953 | |
| 8954 | // C++ 7.1.5.1p2 |
| 8955 | // A variable of non-volatile const-qualified integral or enumeration |
| 8956 | // type initialized by an ICE can be used in ICEs. |
| 8957 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 8958 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8959 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 8960 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8961 | const VarDecl *VD; |
| 8962 | // Look for a declaration of this variable that has an initializer, and |
| 8963 | // check whether it is an ICE. |
| 8964 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 8965 | return NoDiag(); |
| 8966 | else |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8967 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8968 | } |
| 8969 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8970 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 8971 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8972 | case Expr::UnaryOperatorClass: { |
| 8973 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 8974 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8975 | case UO_PostInc: |
| 8976 | case UO_PostDec: |
| 8977 | case UO_PreInc: |
| 8978 | case UO_PreDec: |
| 8979 | case UO_AddrOf: |
| 8980 | case UO_Deref: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 8981 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 8982 | // subexpressions of constant expressions, but they can never be ICEs |
| 8983 | // because an ICE cannot contain an lvalue operand. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8984 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8985 | case UO_Extension: |
| 8986 | case UO_LNot: |
| 8987 | case UO_Plus: |
| 8988 | case UO_Minus: |
| 8989 | case UO_Not: |
| 8990 | case UO_Real: |
| 8991 | case UO_Imag: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8992 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8993 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8994 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8995 | // OffsetOf falls through here. |
| 8996 | } |
| 8997 | case Expr::OffsetOfExprClass: { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8998 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 8999 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
| 9000 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
| 9001 | // compliance: we should warn earlier for offsetof expressions with |
| 9002 | // array subscripts that aren't ICEs, and if the array subscripts |
| 9003 | // are ICEs, the value of the offsetof must be an integer constant. |
| 9004 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9005 | } |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9006 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 9007 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 9008 | if ((Exp->getKind() == UETT_SizeOf) && |
| 9009 | Exp->getTypeOfArgument()->isVariableArrayType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9010 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9011 | return NoDiag(); |
| 9012 | } |
| 9013 | case Expr::BinaryOperatorClass: { |
| 9014 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 9015 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9016 | case BO_PtrMemD: |
| 9017 | case BO_PtrMemI: |
| 9018 | case BO_Assign: |
| 9019 | case BO_MulAssign: |
| 9020 | case BO_DivAssign: |
| 9021 | case BO_RemAssign: |
| 9022 | case BO_AddAssign: |
| 9023 | case BO_SubAssign: |
| 9024 | case BO_ShlAssign: |
| 9025 | case BO_ShrAssign: |
| 9026 | case BO_AndAssign: |
| 9027 | case BO_XorAssign: |
| 9028 | case BO_OrAssign: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 9029 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 9030 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 9031 | // contain an lvalue operand. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9032 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9033 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9034 | case BO_Mul: |
| 9035 | case BO_Div: |
| 9036 | case BO_Rem: |
| 9037 | case BO_Add: |
| 9038 | case BO_Sub: |
| 9039 | case BO_Shl: |
| 9040 | case BO_Shr: |
| 9041 | case BO_LT: |
| 9042 | case BO_GT: |
| 9043 | case BO_LE: |
| 9044 | case BO_GE: |
| 9045 | case BO_EQ: |
| 9046 | case BO_NE: |
| 9047 | case BO_And: |
| 9048 | case BO_Xor: |
| 9049 | case BO_Or: |
| 9050 | case BO_Comma: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9051 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 9052 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9053 | if (Exp->getOpcode() == BO_Div || |
| 9054 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 9055 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9056 | // we don't evaluate one. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9057 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9058 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9059 | if (REval == 0) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9060 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9061 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9062 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9063 | if (LEval.isMinSignedValue()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9064 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9065 | } |
| 9066 | } |
| 9067 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9068 | if (Exp->getOpcode() == BO_Comma) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9069 | if (Ctx.getLangOpts().C99) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9070 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 9071 | // if it isn't evaluated. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9072 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
| 9073 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9074 | } else { |
| 9075 | // In both C89 and C++, commas in ICEs are illegal. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9076 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9077 | } |
| 9078 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9079 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9080 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9081 | case BO_LAnd: |
| 9082 | case BO_LOr: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9083 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 9084 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9085 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9086 | // Rare case where the RHS has a comma "side-effect"; we need |
| 9087 | // to actually check the condition to see whether the side |
| 9088 | // with the comma is evaluated. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9089 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9090 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9091 | return RHSResult; |
| 9092 | return NoDiag(); |
| 9093 | } |
| 9094 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9095 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9096 | } |
| 9097 | } |
| 9098 | } |
| 9099 | case Expr::ImplicitCastExprClass: |
| 9100 | case Expr::CStyleCastExprClass: |
| 9101 | case Expr::CXXFunctionalCastExprClass: |
| 9102 | case Expr::CXXStaticCastExprClass: |
| 9103 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | c3e31e7 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 9104 | case Expr::CXXConstCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9105 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9106 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 9107 | if (isa<ExplicitCastExpr>(E)) { |
| 9108 | if (const FloatingLiteral *FL |
| 9109 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 9110 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 9111 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 9112 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 9113 | bool Ignored; |
| 9114 | // If the value does not fit in the destination type, the behavior is |
| 9115 | // undefined, so we are not required to treat it as a constant |
| 9116 | // expression. |
| 9117 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 9118 | llvm::APFloat::rmTowardZero, |
| 9119 | &Ignored) & APFloat::opInvalidOp) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9120 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 9121 | return NoDiag(); |
| 9122 | } |
| 9123 | } |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 9124 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 9125 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 9126 | case CK_AtomicToNonAtomic: |
| 9127 | case CK_NonAtomicToAtomic: |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 9128 | case CK_NoOp: |
| 9129 | case CK_IntegralToBoolean: |
| 9130 | case CK_IntegralCast: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9131 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 9132 | default: |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9133 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 9134 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9135 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 9136 | case Expr::BinaryConditionalOperatorClass: { |
| 9137 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 9138 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9139 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 9140 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9141 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
| 9142 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
| 9143 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
Richard Smith | 74fc721 | 2012-12-28 12:53:55 +0000 | [diff] [blame] | 9144 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 9145 | return FalseResult; |
| 9146 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9147 | case Expr::ConditionalOperatorClass: { |
| 9148 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 9149 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 9150 | // then only the true side is actually considered in an integer constant |
| 9151 | // expression, and it is fully evaluated. This is an important GNU |
| 9152 | // extension. See GCC PR38377 for discussion. |
| 9153 | if (const CallExpr *CallCE |
| 9154 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 9155 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 9156 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9157 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9158 | if (CondResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9159 | return CondResult; |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 9160 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9161 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 9162 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 9163 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9164 | if (TrueResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9165 | return TrueResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9166 | if (FalseResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9167 | return FalseResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9168 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9169 | return CondResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9170 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9171 | return NoDiag(); |
| 9172 | // Rare case where the diagnostics depend on which side is evaluated |
| 9173 | // Note that if we get here, CondResult is 0, and at least one of |
| 9174 | // TrueResult and FalseResult is non-zero. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9175 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9176 | return FalseResult; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9177 | return TrueResult; |
| 9178 | } |
| 9179 | case Expr::CXXDefaultArgExprClass: |
| 9180 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 9181 | case Expr::CXXDefaultInitExprClass: |
| 9182 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9183 | case Expr::ChooseExprClass: { |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 9184 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9185 | } |
| 9186 | } |
| 9187 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 9188 | llvm_unreachable("Invalid StmtClass!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9189 | } |
| 9190 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9191 | /// Evaluate an expression as a C++11 integral constant expression. |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 9192 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9193 | const Expr *E, |
| 9194 | llvm::APSInt *Value, |
| 9195 | SourceLocation *Loc) { |
| 9196 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 9197 | if (Loc) *Loc = E->getExprLoc(); |
| 9198 | return false; |
| 9199 | } |
| 9200 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9201 | APValue Result; |
| 9202 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 9203 | return false; |
| 9204 | |
Richard Smith | 98710fc | 2014-11-13 23:03:19 +0000 | [diff] [blame] | 9205 | if (!Result.isInt()) { |
| 9206 | if (Loc) *Loc = E->getExprLoc(); |
| 9207 | return false; |
| 9208 | } |
| 9209 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9210 | if (Value) *Value = Result.getInt(); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 9211 | return true; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9212 | } |
| 9213 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 9214 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
| 9215 | SourceLocation *Loc) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9216 | if (Ctx.getLangOpts().CPlusPlus11) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9217 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, nullptr, Loc); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9218 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9219 | ICEDiag D = CheckICE(this, Ctx); |
| 9220 | if (D.Kind != IK_ICE) { |
| 9221 | if (Loc) *Loc = D.Loc; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9222 | return false; |
| 9223 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9224 | return true; |
| 9225 | } |
| 9226 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 9227 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9228 | SourceLocation *Loc, bool isEvaluated) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9229 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9230 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 9231 | |
| 9232 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 9233 | return false; |
| 9234 | if (!EvaluateAsInt(Value, Ctx)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9235 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9236 | return true; |
| 9237 | } |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9238 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 9239 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9240 | return CheckICE(this, Ctx).Kind == IK_ICE; |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 9241 | } |
| 9242 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 9243 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9244 | SourceLocation *Loc) const { |
| 9245 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 9246 | // issues. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9247 | assert(Ctx.getLangOpts().CPlusPlus); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9248 | |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 9249 | // Build evaluation settings. |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9250 | Expr::EvalStatus Status; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9251 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9252 | Status.Diag = &Diags; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 9253 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9254 | |
| 9255 | APValue Scratch; |
| 9256 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
| 9257 | |
| 9258 | if (!Diags.empty()) { |
| 9259 | IsConstExpr = false; |
| 9260 | if (Loc) *Loc = Diags[0].first; |
| 9261 | } else if (!IsConstExpr) { |
| 9262 | // FIXME: This shouldn't happen. |
| 9263 | if (Loc) *Loc = getExprLoc(); |
| 9264 | } |
| 9265 | |
| 9266 | return IsConstExpr; |
| 9267 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9268 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9269 | bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
| 9270 | const FunctionDecl *Callee, |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 9271 | ArrayRef<const Expr*> Args) const { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9272 | Expr::EvalStatus Status; |
| 9273 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated); |
| 9274 | |
| 9275 | ArgVector ArgValues(Args.size()); |
| 9276 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
| 9277 | I != E; ++I) { |
Nick Lewycky | f0202ca | 2014-12-16 06:12:01 +0000 | [diff] [blame] | 9278 | if ((*I)->isValueDependent() || |
| 9279 | !Evaluate(ArgValues[I - Args.begin()], Info, *I)) |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9280 | // If evaluation fails, throw away the argument entirely. |
| 9281 | ArgValues[I - Args.begin()] = APValue(); |
| 9282 | if (Info.EvalStatus.HasSideEffects) |
| 9283 | return false; |
| 9284 | } |
| 9285 | |
| 9286 | // Build fake call to Callee. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9287 | CallStackFrame Frame(Info, Callee->getLocation(), Callee, /*This*/nullptr, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9288 | ArgValues.data()); |
| 9289 | return Evaluate(Value, Info, this) && !Info.EvalStatus.HasSideEffects; |
| 9290 | } |
| 9291 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9292 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9293 | SmallVectorImpl< |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9294 | PartialDiagnosticAt> &Diags) { |
| 9295 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 9296 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 9297 | // ASTs which we build for dependent expressions. |
| 9298 | if (FD->isDependentContext()) |
| 9299 | return true; |
| 9300 | |
| 9301 | Expr::EvalStatus Status; |
| 9302 | Status.Diag = &Diags; |
| 9303 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 9304 | EvalInfo Info(FD->getASTContext(), Status, |
| 9305 | EvalInfo::EM_PotentialConstantExpression); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9306 | |
| 9307 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9308 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9309 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 9310 | // Fabricate an arbitrary expression on the stack and pretend that it |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9311 | // is a temporary being used as the 'this' pointer. |
| 9312 | LValue This; |
| 9313 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 9314 | This.set(&VIE, Info.CurrentCall->Index); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9315 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9316 | ArrayRef<const Expr*> Args; |
| 9317 | |
| 9318 | SourceLocation Loc = FD->getLocation(); |
| 9319 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9320 | APValue Scratch; |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 9321 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
| 9322 | // Evaluate the call as a constant initializer, to allow the construction |
| 9323 | // of objects of non-literal types. |
| 9324 | Info.setEvaluatingDecl(This.getLValueBase(), Scratch); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9325 | HandleConstructorCall(Loc, This, Args, CD, Info, Scratch); |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 9326 | } else |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9327 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : nullptr, |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 9328 | Args, FD->getBody(), Info, Scratch, nullptr); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9329 | |
| 9330 | return Diags.empty(); |
| 9331 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9332 | |
| 9333 | bool Expr::isPotentialConstantExprUnevaluated(Expr *E, |
| 9334 | const FunctionDecl *FD, |
| 9335 | SmallVectorImpl< |
| 9336 | PartialDiagnosticAt> &Diags) { |
| 9337 | Expr::EvalStatus Status; |
| 9338 | Status.Diag = &Diags; |
| 9339 | |
| 9340 | EvalInfo Info(FD->getASTContext(), Status, |
| 9341 | EvalInfo::EM_PotentialConstantExpressionUnevaluated); |
| 9342 | |
| 9343 | // Fabricate a call stack frame to give the arguments a plausible cover story. |
| 9344 | ArrayRef<const Expr*> Args; |
| 9345 | ArgVector ArgValues(0); |
| 9346 | bool Success = EvaluateArgs(Args, ArgValues, Info); |
| 9347 | (void)Success; |
| 9348 | assert(Success && |
| 9349 | "Failed to set up arguments for potential constant evaluation"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9350 | CallStackFrame Frame(Info, SourceLocation(), FD, nullptr, ArgValues.data()); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9351 | |
| 9352 | APValue ResultScratch; |
| 9353 | Evaluate(ResultScratch, Info, E); |
| 9354 | return Diags.empty(); |
| 9355 | } |