Interp.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. //===--- Interp.h - Interpreter for the constexpr VM ------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Definition of the interpreter state and entry point.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_AST_INTERP_INTERP_H
  13. #define LLVM_CLANG_AST_INTERP_INTERP_H
  14. #include "Function.h"
  15. #include "InterpFrame.h"
  16. #include "InterpStack.h"
  17. #include "InterpState.h"
  18. #include "Opcode.h"
  19. #include "PrimType.h"
  20. #include "Program.h"
  21. #include "State.h"
  22. #include "clang/AST/ASTContext.h"
  23. #include "clang/AST/ASTDiagnostic.h"
  24. #include "clang/AST/CXXInheritance.h"
  25. #include "clang/AST/Expr.h"
  26. #include "llvm/ADT/APFloat.h"
  27. #include "llvm/ADT/APSInt.h"
  28. #include "llvm/Support/Endian.h"
  29. #include <limits>
  30. #include <type_traits>
  31. #include <vector>
  32. namespace clang {
  33. namespace interp {
  34. using APInt = llvm::APInt;
  35. using APSInt = llvm::APSInt;
  36. /// Convert a value to an APValue.
  37. template <typename T> bool ReturnValue(const T &V, APValue &R) {
  38. R = V.toAPValue();
  39. return true;
  40. }
  41. /// Checks if the variable has externally defined storage.
  42. bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
  43. /// Checks if the array is offsetable.
  44. bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
  45. /// Checks if a pointer is live and accessible.
  46. bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
  47. AccessKinds AK);
  48. /// Checks if a pointer is null.
  49. bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
  50. CheckSubobjectKind CSK);
  51. /// Checks if a pointer is in range.
  52. bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
  53. AccessKinds AK);
  54. /// Checks if a field from which a pointer is going to be derived is valid.
  55. bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
  56. CheckSubobjectKind CSK);
  57. /// Checks if a pointer points to const storage.
  58. bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
  59. /// Checks if a pointer points to a mutable field.
  60. bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
  61. /// Checks if a value can be loaded from a block.
  62. bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
  63. /// Checks if a value can be stored in a block.
  64. bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
  65. /// Checks if a method can be invoked on an object.
  66. bool CheckInvoke(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
  67. /// Checks if a value can be initialized.
  68. bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
  69. /// Checks if a method can be called.
  70. bool CheckCallable(InterpState &S, CodePtr OpPC, Function *F);
  71. /// Checks the 'this' pointer.
  72. bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This);
  73. /// Checks if a method is pure virtual.
  74. bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD);
  75. template <typename T> inline bool IsTrue(const T &V) { return !V.isZero(); }
  76. //===----------------------------------------------------------------------===//
  77. // Add, Sub, Mul
  78. //===----------------------------------------------------------------------===//
  79. template <typename T, bool (*OpFW)(T, T, unsigned, T *),
  80. template <typename U> class OpAP>
  81. bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS,
  82. const T &RHS) {
  83. // Fast path - add the numbers with fixed width.
  84. T Result;
  85. if (!OpFW(LHS, RHS, Bits, &Result)) {
  86. S.Stk.push<T>(Result);
  87. return true;
  88. }
  89. // If for some reason evaluation continues, use the truncated results.
  90. S.Stk.push<T>(Result);
  91. // Slow path - compute the result using another bit of precision.
  92. APSInt Value = OpAP<APSInt>()(LHS.toAPSInt(Bits), RHS.toAPSInt(Bits));
  93. // Report undefined behaviour, stopping if required.
  94. const Expr *E = S.Current->getExpr(OpPC);
  95. QualType Type = E->getType();
  96. if (S.checkingForUndefinedBehavior()) {
  97. SmallString<32> Trunc;
  98. Value.trunc(Result.bitWidth()).toString(Trunc, 10);
  99. auto Loc = E->getExprLoc();
  100. S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
  101. return true;
  102. } else {
  103. S.CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
  104. return S.noteUndefinedBehavior();
  105. }
  106. }
  107. template <PrimType Name, class T = typename PrimConv<Name>::T>
  108. bool Add(InterpState &S, CodePtr OpPC) {
  109. const T &RHS = S.Stk.pop<T>();
  110. const T &LHS = S.Stk.pop<T>();
  111. const unsigned Bits = RHS.bitWidth() + 1;
  112. return AddSubMulHelper<T, T::add, std::plus>(S, OpPC, Bits, LHS, RHS);
  113. }
  114. template <PrimType Name, class T = typename PrimConv<Name>::T>
  115. bool Sub(InterpState &S, CodePtr OpPC) {
  116. const T &RHS = S.Stk.pop<T>();
  117. const T &LHS = S.Stk.pop<T>();
  118. const unsigned Bits = RHS.bitWidth() + 1;
  119. return AddSubMulHelper<T, T::sub, std::minus>(S, OpPC, Bits, LHS, RHS);
  120. }
  121. template <PrimType Name, class T = typename PrimConv<Name>::T>
  122. bool Mul(InterpState &S, CodePtr OpPC) {
  123. const T &RHS = S.Stk.pop<T>();
  124. const T &LHS = S.Stk.pop<T>();
  125. const unsigned Bits = RHS.bitWidth() * 2;
  126. return AddSubMulHelper<T, T::mul, std::multiplies>(S, OpPC, Bits, LHS, RHS);
  127. }
  128. //===----------------------------------------------------------------------===//
  129. // EQ, NE, GT, GE, LT, LE
  130. //===----------------------------------------------------------------------===//
  131. using CompareFn = llvm::function_ref<bool(ComparisonCategoryResult)>;
  132. template <typename T>
  133. bool CmpHelper(InterpState &S, CodePtr OpPC, CompareFn Fn) {
  134. using BoolT = PrimConv<PT_Bool>::T;
  135. const T &RHS = S.Stk.pop<T>();
  136. const T &LHS = S.Stk.pop<T>();
  137. S.Stk.push<BoolT>(BoolT::from(Fn(LHS.compare(RHS))));
  138. return true;
  139. }
  140. template <typename T>
  141. bool CmpHelperEQ(InterpState &S, CodePtr OpPC, CompareFn Fn) {
  142. return CmpHelper<T>(S, OpPC, Fn);
  143. }
  144. template <>
  145. inline bool CmpHelper<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
  146. using BoolT = PrimConv<PT_Bool>::T;
  147. const Pointer &RHS = S.Stk.pop<Pointer>();
  148. const Pointer &LHS = S.Stk.pop<Pointer>();
  149. if (!Pointer::hasSameBase(LHS, RHS)) {
  150. const SourceInfo &Loc = S.Current->getSource(OpPC);
  151. S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
  152. return false;
  153. } else {
  154. unsigned VL = LHS.getByteOffset();
  155. unsigned VR = RHS.getByteOffset();
  156. S.Stk.push<BoolT>(BoolT::from(Fn(Compare(VL, VR))));
  157. return true;
  158. }
  159. }
  160. template <>
  161. inline bool CmpHelperEQ<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
  162. using BoolT = PrimConv<PT_Bool>::T;
  163. const Pointer &RHS = S.Stk.pop<Pointer>();
  164. const Pointer &LHS = S.Stk.pop<Pointer>();
  165. if (LHS.isZero() && RHS.isZero()) {
  166. S.Stk.push<BoolT>(BoolT::from(Fn(ComparisonCategoryResult::Equal)));
  167. return true;
  168. }
  169. if (!Pointer::hasSameBase(LHS, RHS)) {
  170. S.Stk.push<BoolT>(BoolT::from(Fn(ComparisonCategoryResult::Unordered)));
  171. return true;
  172. } else {
  173. unsigned VL = LHS.getByteOffset();
  174. unsigned VR = RHS.getByteOffset();
  175. S.Stk.push<BoolT>(BoolT::from(Fn(Compare(VL, VR))));
  176. return true;
  177. }
  178. }
  179. template <PrimType Name, class T = typename PrimConv<Name>::T>
  180. bool EQ(InterpState &S, CodePtr OpPC) {
  181. return CmpHelperEQ<T>(S, OpPC, [](ComparisonCategoryResult R) {
  182. return R == ComparisonCategoryResult::Equal;
  183. });
  184. }
  185. template <PrimType Name, class T = typename PrimConv<Name>::T>
  186. bool NE(InterpState &S, CodePtr OpPC) {
  187. return CmpHelperEQ<T>(S, OpPC, [](ComparisonCategoryResult R) {
  188. return R != ComparisonCategoryResult::Equal;
  189. });
  190. }
  191. template <PrimType Name, class T = typename PrimConv<Name>::T>
  192. bool LT(InterpState &S, CodePtr OpPC) {
  193. return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
  194. return R == ComparisonCategoryResult::Less;
  195. });
  196. }
  197. template <PrimType Name, class T = typename PrimConv<Name>::T>
  198. bool LE(InterpState &S, CodePtr OpPC) {
  199. return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
  200. return R == ComparisonCategoryResult::Less ||
  201. R == ComparisonCategoryResult::Equal;
  202. });
  203. }
  204. template <PrimType Name, class T = typename PrimConv<Name>::T>
  205. bool GT(InterpState &S, CodePtr OpPC) {
  206. return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
  207. return R == ComparisonCategoryResult::Greater;
  208. });
  209. }
  210. template <PrimType Name, class T = typename PrimConv<Name>::T>
  211. bool GE(InterpState &S, CodePtr OpPC) {
  212. return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
  213. return R == ComparisonCategoryResult::Greater ||
  214. R == ComparisonCategoryResult::Equal;
  215. });
  216. }
  217. //===----------------------------------------------------------------------===//
  218. // InRange
  219. //===----------------------------------------------------------------------===//
  220. template <PrimType Name, class T = typename PrimConv<Name>::T>
  221. bool InRange(InterpState &S, CodePtr OpPC) {
  222. const T RHS = S.Stk.pop<T>();
  223. const T LHS = S.Stk.pop<T>();
  224. const T Value = S.Stk.pop<T>();
  225. S.Stk.push<bool>(LHS <= Value && Value <= RHS);
  226. return true;
  227. }
  228. //===----------------------------------------------------------------------===//
  229. // Dup, Pop, Test
  230. //===----------------------------------------------------------------------===//
  231. template <PrimType Name, class T = typename PrimConv<Name>::T>
  232. bool Dup(InterpState &S, CodePtr OpPC) {
  233. S.Stk.push<T>(S.Stk.peek<T>());
  234. return true;
  235. }
  236. template <PrimType Name, class T = typename PrimConv<Name>::T>
  237. bool Pop(InterpState &S, CodePtr OpPC) {
  238. S.Stk.pop<T>();
  239. return true;
  240. }
  241. //===----------------------------------------------------------------------===//
  242. // Const
  243. //===----------------------------------------------------------------------===//
  244. template <PrimType Name, class T = typename PrimConv<Name>::T>
  245. bool Const(InterpState &S, CodePtr OpPC, const T &Arg) {
  246. S.Stk.push<T>(Arg);
  247. return true;
  248. }
  249. //===----------------------------------------------------------------------===//
  250. // Get/Set Local/Param/Global/This
  251. //===----------------------------------------------------------------------===//
  252. template <PrimType Name, class T = typename PrimConv<Name>::T>
  253. bool GetLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
  254. S.Stk.push<T>(S.Current->getLocal<T>(I));
  255. return true;
  256. }
  257. template <PrimType Name, class T = typename PrimConv<Name>::T>
  258. bool SetLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
  259. S.Current->setLocal<T>(I, S.Stk.pop<T>());
  260. return true;
  261. }
  262. template <PrimType Name, class T = typename PrimConv<Name>::T>
  263. bool GetParam(InterpState &S, CodePtr OpPC, uint32_t I) {
  264. if (S.checkingPotentialConstantExpression()) {
  265. return false;
  266. }
  267. S.Stk.push<T>(S.Current->getParam<T>(I));
  268. return true;
  269. }
  270. template <PrimType Name, class T = typename PrimConv<Name>::T>
  271. bool SetParam(InterpState &S, CodePtr OpPC, uint32_t I) {
  272. S.Current->setParam<T>(I, S.Stk.pop<T>());
  273. return true;
  274. }
  275. template <PrimType Name, class T = typename PrimConv<Name>::T>
  276. bool GetField(InterpState &S, CodePtr OpPC, uint32_t I) {
  277. const Pointer &Obj = S.Stk.peek<Pointer>();
  278. if (!CheckNull(S, OpPC, Obj, CSK_Field))
  279. return false;
  280. if (!CheckRange(S, OpPC, Obj, CSK_Field))
  281. return false;
  282. const Pointer &Field = Obj.atField(I);
  283. if (!CheckLoad(S, OpPC, Field))
  284. return false;
  285. S.Stk.push<T>(Field.deref<T>());
  286. return true;
  287. }
  288. template <PrimType Name, class T = typename PrimConv<Name>::T>
  289. bool SetField(InterpState &S, CodePtr OpPC, uint32_t I) {
  290. const T &Value = S.Stk.pop<T>();
  291. const Pointer &Obj = S.Stk.peek<Pointer>();
  292. if (!CheckNull(S, OpPC, Obj, CSK_Field))
  293. return false;
  294. if (!CheckRange(S, OpPC, Obj, CSK_Field))
  295. return false;
  296. const Pointer &Field = Obj.atField(I);
  297. if (!CheckStore(S, OpPC, Field))
  298. return false;
  299. Field.deref<T>() = Value;
  300. return true;
  301. }
  302. template <PrimType Name, class T = typename PrimConv<Name>::T>
  303. bool GetFieldPop(InterpState &S, CodePtr OpPC, uint32_t I) {
  304. const Pointer &Obj = S.Stk.pop<Pointer>();
  305. if (!CheckNull(S, OpPC, Obj, CSK_Field))
  306. return false;
  307. if (!CheckRange(S, OpPC, Obj, CSK_Field))
  308. return false;
  309. const Pointer &Field = Obj.atField(I);
  310. if (!CheckLoad(S, OpPC, Field))
  311. return false;
  312. S.Stk.push<T>(Field.deref<T>());
  313. return true;
  314. }
  315. template <PrimType Name, class T = typename PrimConv<Name>::T>
  316. bool GetThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
  317. if (S.checkingPotentialConstantExpression())
  318. return false;
  319. const Pointer &This = S.Current->getThis();
  320. if (!CheckThis(S, OpPC, This))
  321. return false;
  322. const Pointer &Field = This.atField(I);
  323. if (!CheckLoad(S, OpPC, Field))
  324. return false;
  325. S.Stk.push<T>(Field.deref<T>());
  326. return true;
  327. }
  328. template <PrimType Name, class T = typename PrimConv<Name>::T>
  329. bool SetThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
  330. if (S.checkingPotentialConstantExpression())
  331. return false;
  332. const T &Value = S.Stk.pop<T>();
  333. const Pointer &This = S.Current->getThis();
  334. if (!CheckThis(S, OpPC, This))
  335. return false;
  336. const Pointer &Field = This.atField(I);
  337. if (!CheckStore(S, OpPC, Field))
  338. return false;
  339. Field.deref<T>() = Value;
  340. return true;
  341. }
  342. template <PrimType Name, class T = typename PrimConv<Name>::T>
  343. bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
  344. auto *B = S.P.getGlobal(I);
  345. if (B->isExtern())
  346. return false;
  347. S.Stk.push<T>(B->deref<T>());
  348. return true;
  349. }
  350. template <PrimType Name, class T = typename PrimConv<Name>::T>
  351. bool SetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
  352. // TODO: emit warning.
  353. return false;
  354. }
  355. template <PrimType Name, class T = typename PrimConv<Name>::T>
  356. bool InitGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
  357. S.P.getGlobal(I)->deref<T>() = S.Stk.pop<T>();
  358. return true;
  359. }
  360. template <PrimType Name, class T = typename PrimConv<Name>::T>
  361. bool InitThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
  362. if (S.checkingPotentialConstantExpression())
  363. return false;
  364. const Pointer &This = S.Current->getThis();
  365. if (!CheckThis(S, OpPC, This))
  366. return false;
  367. const Pointer &Field = This.atField(I);
  368. Field.deref<T>() = S.Stk.pop<T>();
  369. Field.initialize();
  370. return true;
  371. }
  372. template <PrimType Name, class T = typename PrimConv<Name>::T>
  373. bool InitThisBitField(InterpState &S, CodePtr OpPC, const Record::Field *F) {
  374. if (S.checkingPotentialConstantExpression())
  375. return false;
  376. const Pointer &This = S.Current->getThis();
  377. if (!CheckThis(S, OpPC, This))
  378. return false;
  379. const Pointer &Field = This.atField(F->Offset);
  380. const auto &Value = S.Stk.pop<T>();
  381. Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue(S.getCtx()));
  382. Field.initialize();
  383. return true;
  384. }
  385. template <PrimType Name, class T = typename PrimConv<Name>::T>
  386. bool InitThisFieldActive(InterpState &S, CodePtr OpPC, uint32_t I) {
  387. if (S.checkingPotentialConstantExpression())
  388. return false;
  389. const Pointer &This = S.Current->getThis();
  390. if (!CheckThis(S, OpPC, This))
  391. return false;
  392. const Pointer &Field = This.atField(I);
  393. Field.deref<T>() = S.Stk.pop<T>();
  394. Field.activate();
  395. Field.initialize();
  396. return true;
  397. }
  398. template <PrimType Name, class T = typename PrimConv<Name>::T>
  399. bool InitField(InterpState &S, CodePtr OpPC, uint32_t I) {
  400. const T &Value = S.Stk.pop<T>();
  401. const Pointer &Field = S.Stk.pop<Pointer>().atField(I);
  402. Field.deref<T>() = Value;
  403. Field.activate();
  404. Field.initialize();
  405. return true;
  406. }
  407. template <PrimType Name, class T = typename PrimConv<Name>::T>
  408. bool InitBitField(InterpState &S, CodePtr OpPC, const Record::Field *F) {
  409. const T &Value = S.Stk.pop<T>();
  410. const Pointer &Field = S.Stk.pop<Pointer>().atField(F->Offset);
  411. Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue(S.getCtx()));
  412. Field.activate();
  413. Field.initialize();
  414. return true;
  415. }
  416. template <PrimType Name, class T = typename PrimConv<Name>::T>
  417. bool InitFieldActive(InterpState &S, CodePtr OpPC, uint32_t I) {
  418. const T &Value = S.Stk.pop<T>();
  419. const Pointer &Ptr = S.Stk.pop<Pointer>();
  420. const Pointer &Field = Ptr.atField(I);
  421. Field.deref<T>() = Value;
  422. Field.activate();
  423. Field.initialize();
  424. return true;
  425. }
  426. //===----------------------------------------------------------------------===//
  427. // GetPtr Local/Param/Global/Field/This
  428. //===----------------------------------------------------------------------===//
  429. inline bool GetPtrLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
  430. S.Stk.push<Pointer>(S.Current->getLocalPointer(I));
  431. return true;
  432. }
  433. inline bool GetPtrParam(InterpState &S, CodePtr OpPC, uint32_t I) {
  434. if (S.checkingPotentialConstantExpression()) {
  435. return false;
  436. }
  437. S.Stk.push<Pointer>(S.Current->getParamPointer(I));
  438. return true;
  439. }
  440. inline bool GetPtrGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
  441. S.Stk.push<Pointer>(S.P.getPtrGlobal(I));
  442. return true;
  443. }
  444. inline bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off) {
  445. const Pointer &Ptr = S.Stk.pop<Pointer>();
  446. if (!CheckNull(S, OpPC, Ptr, CSK_Field))
  447. return false;
  448. if (!CheckExtern(S, OpPC, Ptr))
  449. return false;
  450. if (!CheckRange(S, OpPC, Ptr, CSK_Field))
  451. return false;
  452. S.Stk.push<Pointer>(Ptr.atField(Off));
  453. return true;
  454. }
  455. inline bool GetPtrThisField(InterpState &S, CodePtr OpPC, uint32_t Off) {
  456. if (S.checkingPotentialConstantExpression())
  457. return false;
  458. const Pointer &This = S.Current->getThis();
  459. if (!CheckThis(S, OpPC, This))
  460. return false;
  461. S.Stk.push<Pointer>(This.atField(Off));
  462. return true;
  463. }
  464. inline bool GetPtrActiveField(InterpState &S, CodePtr OpPC, uint32_t Off) {
  465. const Pointer &Ptr = S.Stk.pop<Pointer>();
  466. if (!CheckNull(S, OpPC, Ptr, CSK_Field))
  467. return false;
  468. if (!CheckRange(S, OpPC, Ptr, CSK_Field))
  469. return false;
  470. Pointer Field = Ptr.atField(Off);
  471. Ptr.deactivate();
  472. Field.activate();
  473. S.Stk.push<Pointer>(std::move(Field));
  474. return true;
  475. }
  476. inline bool GetPtrActiveThisField(InterpState &S, CodePtr OpPC, uint32_t Off) {
  477. if (S.checkingPotentialConstantExpression())
  478. return false;
  479. const Pointer &This = S.Current->getThis();
  480. if (!CheckThis(S, OpPC, This))
  481. return false;
  482. Pointer Field = This.atField(Off);
  483. This.deactivate();
  484. Field.activate();
  485. S.Stk.push<Pointer>(std::move(Field));
  486. return true;
  487. }
  488. inline bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
  489. const Pointer &Ptr = S.Stk.pop<Pointer>();
  490. if (!CheckNull(S, OpPC, Ptr, CSK_Base))
  491. return false;
  492. S.Stk.push<Pointer>(Ptr.atField(Off));
  493. return true;
  494. }
  495. inline bool GetPtrThisBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
  496. if (S.checkingPotentialConstantExpression())
  497. return false;
  498. const Pointer &This = S.Current->getThis();
  499. if (!CheckThis(S, OpPC, This))
  500. return false;
  501. S.Stk.push<Pointer>(This.atField(Off));
  502. return true;
  503. }
  504. inline bool VirtBaseHelper(InterpState &S, CodePtr OpPC, const RecordDecl *Decl,
  505. const Pointer &Ptr) {
  506. Pointer Base = Ptr;
  507. while (Base.isBaseClass())
  508. Base = Base.getBase();
  509. auto *Field = Base.getRecord()->getVirtualBase(Decl);
  510. S.Stk.push<Pointer>(Base.atField(Field->Offset));
  511. return true;
  512. }
  513. inline bool GetPtrVirtBase(InterpState &S, CodePtr OpPC, const RecordDecl *D) {
  514. const Pointer &Ptr = S.Stk.pop<Pointer>();
  515. if (!CheckNull(S, OpPC, Ptr, CSK_Base))
  516. return false;
  517. return VirtBaseHelper(S, OpPC, D, Ptr);
  518. }
  519. inline bool GetPtrThisVirtBase(InterpState &S, CodePtr OpPC,
  520. const RecordDecl *D) {
  521. if (S.checkingPotentialConstantExpression())
  522. return false;
  523. const Pointer &This = S.Current->getThis();
  524. if (!CheckThis(S, OpPC, This))
  525. return false;
  526. return VirtBaseHelper(S, OpPC, D, S.Current->getThis());
  527. }
  528. //===----------------------------------------------------------------------===//
  529. // Load, Store, Init
  530. //===----------------------------------------------------------------------===//
  531. template <PrimType Name, class T = typename PrimConv<Name>::T>
  532. bool Load(InterpState &S, CodePtr OpPC) {
  533. const Pointer &Ptr = S.Stk.peek<Pointer>();
  534. if (!CheckLoad(S, OpPC, Ptr))
  535. return false;
  536. S.Stk.push<T>(Ptr.deref<T>());
  537. return true;
  538. }
  539. template <PrimType Name, class T = typename PrimConv<Name>::T>
  540. bool LoadPop(InterpState &S, CodePtr OpPC) {
  541. const Pointer &Ptr = S.Stk.pop<Pointer>();
  542. if (!CheckLoad(S, OpPC, Ptr))
  543. return false;
  544. S.Stk.push<T>(Ptr.deref<T>());
  545. return true;
  546. }
  547. template <PrimType Name, class T = typename PrimConv<Name>::T>
  548. bool Store(InterpState &S, CodePtr OpPC) {
  549. const T &Value = S.Stk.pop<T>();
  550. const Pointer &Ptr = S.Stk.peek<Pointer>();
  551. if (!CheckStore(S, OpPC, Ptr))
  552. return false;
  553. Ptr.deref<T>() = Value;
  554. return true;
  555. }
  556. template <PrimType Name, class T = typename PrimConv<Name>::T>
  557. bool StorePop(InterpState &S, CodePtr OpPC) {
  558. const T &Value = S.Stk.pop<T>();
  559. const Pointer &Ptr = S.Stk.pop<Pointer>();
  560. if (!CheckStore(S, OpPC, Ptr))
  561. return false;
  562. Ptr.deref<T>() = Value;
  563. return true;
  564. }
  565. template <PrimType Name, class T = typename PrimConv<Name>::T>
  566. bool StoreBitField(InterpState &S, CodePtr OpPC) {
  567. const T &Value = S.Stk.pop<T>();
  568. const Pointer &Ptr = S.Stk.peek<Pointer>();
  569. if (!CheckStore(S, OpPC, Ptr))
  570. return false;
  571. if (auto *FD = Ptr.getField()) {
  572. Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue(S.getCtx()));
  573. } else {
  574. Ptr.deref<T>() = Value;
  575. }
  576. return true;
  577. }
  578. template <PrimType Name, class T = typename PrimConv<Name>::T>
  579. bool StoreBitFieldPop(InterpState &S, CodePtr OpPC) {
  580. const T &Value = S.Stk.pop<T>();
  581. const Pointer &Ptr = S.Stk.pop<Pointer>();
  582. if (!CheckStore(S, OpPC, Ptr))
  583. return false;
  584. if (auto *FD = Ptr.getField()) {
  585. Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue(S.getCtx()));
  586. } else {
  587. Ptr.deref<T>() = Value;
  588. }
  589. return true;
  590. }
  591. template <PrimType Name, class T = typename PrimConv<Name>::T>
  592. bool InitPop(InterpState &S, CodePtr OpPC) {
  593. const T &Value = S.Stk.pop<T>();
  594. const Pointer &Ptr = S.Stk.pop<Pointer>();
  595. if (!CheckInit(S, OpPC, Ptr))
  596. return false;
  597. Ptr.initialize();
  598. new (&Ptr.deref<T>()) T(Value);
  599. return true;
  600. }
  601. template <PrimType Name, class T = typename PrimConv<Name>::T>
  602. bool InitElem(InterpState &S, CodePtr OpPC, uint32_t Idx) {
  603. const T &Value = S.Stk.pop<T>();
  604. const Pointer &Ptr = S.Stk.peek<Pointer>().atIndex(Idx);
  605. if (!CheckInit(S, OpPC, Ptr))
  606. return false;
  607. Ptr.initialize();
  608. new (&Ptr.deref<T>()) T(Value);
  609. return true;
  610. }
  611. template <PrimType Name, class T = typename PrimConv<Name>::T>
  612. bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx) {
  613. const T &Value = S.Stk.pop<T>();
  614. const Pointer &Ptr = S.Stk.pop<Pointer>().atIndex(Idx);
  615. if (!CheckInit(S, OpPC, Ptr))
  616. return false;
  617. Ptr.initialize();
  618. new (&Ptr.deref<T>()) T(Value);
  619. return true;
  620. }
  621. //===----------------------------------------------------------------------===//
  622. // AddOffset, SubOffset
  623. //===----------------------------------------------------------------------===//
  624. template <class T, bool Add> bool OffsetHelper(InterpState &S, CodePtr OpPC) {
  625. // Fetch the pointer and the offset.
  626. const T &Offset = S.Stk.pop<T>();
  627. const Pointer &Ptr = S.Stk.pop<Pointer>();
  628. if (!CheckNull(S, OpPC, Ptr, CSK_ArrayIndex))
  629. return false;
  630. if (!CheckRange(S, OpPC, Ptr, CSK_ArrayToPointer))
  631. return false;
  632. // Get a version of the index comparable to the type.
  633. T Index = T::from(Ptr.getIndex(), Offset.bitWidth());
  634. // A zero offset does not change the pointer, but in the case of an array
  635. // it has to be adjusted to point to the first element instead of the array.
  636. if (Offset.isZero()) {
  637. S.Stk.push<Pointer>(Index.isZero() ? Ptr.atIndex(0) : Ptr);
  638. return true;
  639. }
  640. // Arrays of unknown bounds cannot have pointers into them.
  641. if (!CheckArray(S, OpPC, Ptr))
  642. return false;
  643. // Compute the largest index into the array.
  644. unsigned MaxIndex = Ptr.getNumElems();
  645. // Helper to report an invalid offset, computed as APSInt.
  646. auto InvalidOffset = [&]() {
  647. const unsigned Bits = Offset.bitWidth();
  648. APSInt APOffset(Offset.toAPSInt().extend(Bits + 2), false);
  649. APSInt APIndex(Index.toAPSInt().extend(Bits + 2), false);
  650. APSInt NewIndex = Add ? (APIndex + APOffset) : (APIndex - APOffset);
  651. S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
  652. << NewIndex
  653. << /*array*/ static_cast<int>(!Ptr.inArray())
  654. << static_cast<unsigned>(MaxIndex);
  655. return false;
  656. };
  657. // If the new offset would be negative, bail out.
  658. if (Add && Offset.isNegative() && (Offset.isMin() || -Offset > Index))
  659. return InvalidOffset();
  660. if (!Add && Offset.isPositive() && Index < Offset)
  661. return InvalidOffset();
  662. // If the new offset would be out of bounds, bail out.
  663. unsigned MaxOffset = MaxIndex - Ptr.getIndex();
  664. if (Add && Offset.isPositive() && Offset > MaxOffset)
  665. return InvalidOffset();
  666. if (!Add && Offset.isNegative() && (Offset.isMin() || -Offset > MaxOffset))
  667. return InvalidOffset();
  668. // Offset is valid - compute it on unsigned.
  669. int64_t WideIndex = static_cast<int64_t>(Index);
  670. int64_t WideOffset = static_cast<int64_t>(Offset);
  671. int64_t Result = Add ? (WideIndex + WideOffset) : (WideIndex - WideOffset);
  672. S.Stk.push<Pointer>(Ptr.atIndex(static_cast<unsigned>(Result)));
  673. return true;
  674. }
  675. template <PrimType Name, class T = typename PrimConv<Name>::T>
  676. bool AddOffset(InterpState &S, CodePtr OpPC) {
  677. return OffsetHelper<T, true>(S, OpPC);
  678. }
  679. template <PrimType Name, class T = typename PrimConv<Name>::T>
  680. bool SubOffset(InterpState &S, CodePtr OpPC) {
  681. return OffsetHelper<T, false>(S, OpPC);
  682. }
  683. //===----------------------------------------------------------------------===//
  684. // Destroy
  685. //===----------------------------------------------------------------------===//
  686. inline bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I) {
  687. S.Current->destroy(I);
  688. return true;
  689. }
  690. //===----------------------------------------------------------------------===//
  691. // Cast, CastFP
  692. //===----------------------------------------------------------------------===//
  693. template <PrimType TIn, PrimType TOut> bool Cast(InterpState &S, CodePtr OpPC) {
  694. using T = typename PrimConv<TIn>::T;
  695. using U = typename PrimConv<TOut>::T;
  696. S.Stk.push<U>(U::from(S.Stk.pop<T>()));
  697. return true;
  698. }
  699. //===----------------------------------------------------------------------===//
  700. // Zero, Nullptr
  701. //===----------------------------------------------------------------------===//
  702. template <PrimType Name, class T = typename PrimConv<Name>::T>
  703. bool Zero(InterpState &S, CodePtr OpPC) {
  704. S.Stk.push<T>(T::zero());
  705. return true;
  706. }
  707. template <PrimType Name, class T = typename PrimConv<Name>::T>
  708. inline bool Null(InterpState &S, CodePtr OpPC) {
  709. S.Stk.push<T>();
  710. return true;
  711. }
  712. //===----------------------------------------------------------------------===//
  713. // This, ImplicitThis
  714. //===----------------------------------------------------------------------===//
  715. inline bool This(InterpState &S, CodePtr OpPC) {
  716. // Cannot read 'this' in this mode.
  717. if (S.checkingPotentialConstantExpression()) {
  718. return false;
  719. }
  720. const Pointer &This = S.Current->getThis();
  721. if (!CheckThis(S, OpPC, This))
  722. return false;
  723. S.Stk.push<Pointer>(This);
  724. return true;
  725. }
  726. //===----------------------------------------------------------------------===//
  727. // Shr, Shl
  728. //===----------------------------------------------------------------------===//
  729. template <PrimType TR, PrimType TL, class T = typename PrimConv<TR>::T>
  730. unsigned Trunc(InterpState &S, CodePtr OpPC, unsigned Bits, const T &V) {
  731. // C++11 [expr.shift]p1: Shift width must be less than the bit width of
  732. // the shifted type.
  733. if (Bits > 1 && V >= T::from(Bits, V.bitWidth())) {
  734. const Expr *E = S.Current->getExpr(OpPC);
  735. const APSInt Val = V.toAPSInt();
  736. QualType Ty = E->getType();
  737. S.CCEDiag(E, diag::note_constexpr_large_shift) << Val << Ty << Bits;
  738. return Bits;
  739. } else {
  740. return static_cast<unsigned>(V);
  741. }
  742. }
  743. template <PrimType TL, PrimType TR, typename T = typename PrimConv<TL>::T>
  744. inline bool ShiftRight(InterpState &S, CodePtr OpPC, const T &V, unsigned RHS) {
  745. if (RHS >= V.bitWidth()) {
  746. S.Stk.push<T>(T::from(0, V.bitWidth()));
  747. } else {
  748. S.Stk.push<T>(T::from(V >> RHS, V.bitWidth()));
  749. }
  750. return true;
  751. }
  752. template <PrimType TL, PrimType TR, typename T = typename PrimConv<TL>::T>
  753. inline bool ShiftLeft(InterpState &S, CodePtr OpPC, const T &V, unsigned RHS) {
  754. if (V.isSigned() && !S.getLangOpts().CPlusPlus20) {
  755. // C++11 [expr.shift]p2: A signed left shift must have a non-negative
  756. // operand, and must not overflow the corresponding unsigned type.
  757. // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to
  758. // E1 x 2^E2 module 2^N.
  759. if (V.isNegative()) {
  760. const Expr *E = S.Current->getExpr(OpPC);
  761. S.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << V.toAPSInt();
  762. } else if (V.countLeadingZeros() < RHS) {
  763. S.CCEDiag(S.Current->getExpr(OpPC), diag::note_constexpr_lshift_discards);
  764. }
  765. }
  766. if (V.bitWidth() == 1) {
  767. S.Stk.push<T>(V);
  768. } else if (RHS >= V.bitWidth()) {
  769. S.Stk.push<T>(T::from(0, V.bitWidth()));
  770. } else {
  771. S.Stk.push<T>(T::from(V.toUnsigned() << RHS, V.bitWidth()));
  772. }
  773. return true;
  774. }
  775. template <PrimType TL, PrimType TR>
  776. inline bool Shr(InterpState &S, CodePtr OpPC) {
  777. const auto &RHS = S.Stk.pop<typename PrimConv<TR>::T>();
  778. const auto &LHS = S.Stk.pop<typename PrimConv<TL>::T>();
  779. const unsigned Bits = LHS.bitWidth();
  780. if (RHS.isSigned() && RHS.isNegative()) {
  781. const SourceInfo &Loc = S.Current->getSource(OpPC);
  782. S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
  783. return ShiftLeft<TL, TR>(S, OpPC, LHS, Trunc<TR, TL>(S, OpPC, Bits, -RHS));
  784. } else {
  785. return ShiftRight<TL, TR>(S, OpPC, LHS, Trunc<TR, TL>(S, OpPC, Bits, RHS));
  786. }
  787. }
  788. template <PrimType TL, PrimType TR>
  789. inline bool Shl(InterpState &S, CodePtr OpPC) {
  790. const auto &RHS = S.Stk.pop<typename PrimConv<TR>::T>();
  791. const auto &LHS = S.Stk.pop<typename PrimConv<TL>::T>();
  792. const unsigned Bits = LHS.bitWidth();
  793. if (RHS.isSigned() && RHS.isNegative()) {
  794. const SourceInfo &Loc = S.Current->getSource(OpPC);
  795. S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
  796. return ShiftRight<TL, TR>(S, OpPC, LHS, Trunc<TR, TL>(S, OpPC, Bits, -RHS));
  797. } else {
  798. return ShiftLeft<TL, TR>(S, OpPC, LHS, Trunc<TR, TL>(S, OpPC, Bits, RHS));
  799. }
  800. }
  801. //===----------------------------------------------------------------------===//
  802. // NoRet
  803. //===----------------------------------------------------------------------===//
  804. inline bool NoRet(InterpState &S, CodePtr OpPC) {
  805. SourceLocation EndLoc = S.Current->getCallee()->getEndLoc();
  806. S.FFDiag(EndLoc, diag::note_constexpr_no_return);
  807. return false;
  808. }
  809. //===----------------------------------------------------------------------===//
  810. // NarrowPtr, ExpandPtr
  811. //===----------------------------------------------------------------------===//
  812. inline bool NarrowPtr(InterpState &S, CodePtr OpPC) {
  813. const Pointer &Ptr = S.Stk.pop<Pointer>();
  814. S.Stk.push<Pointer>(Ptr.narrow());
  815. return true;
  816. }
  817. inline bool ExpandPtr(InterpState &S, CodePtr OpPC) {
  818. const Pointer &Ptr = S.Stk.pop<Pointer>();
  819. S.Stk.push<Pointer>(Ptr.expand());
  820. return true;
  821. }
  822. //===----------------------------------------------------------------------===//
  823. // Read opcode arguments
  824. //===----------------------------------------------------------------------===//
  825. template <typename T>
  826. inline std::enable_if_t<!std::is_pointer<T>::value, T> ReadArg(InterpState &S,
  827. CodePtr OpPC) {
  828. return OpPC.read<T>();
  829. }
  830. template <typename T>
  831. inline std::enable_if_t<std::is_pointer<T>::value, T> ReadArg(InterpState &S,
  832. CodePtr OpPC) {
  833. uint32_t ID = OpPC.read<uint32_t>();
  834. return reinterpret_cast<T>(S.P.getNativePointer(ID));
  835. }
  836. /// Interpreter entry point.
  837. bool Interpret(InterpState &S, APValue &Result);
  838. } // namespace interp
  839. } // namespace clang
  840. #endif