Interp.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. //===--- InterpState.cpp - 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. #include "Interp.h"
  9. #include <limits>
  10. #include <vector>
  11. #include "Function.h"
  12. #include "InterpFrame.h"
  13. #include "InterpStack.h"
  14. #include "Opcode.h"
  15. #include "PrimType.h"
  16. #include "Program.h"
  17. #include "State.h"
  18. #include "clang/AST/ASTContext.h"
  19. #include "clang/AST/ASTDiagnostic.h"
  20. #include "clang/AST/CXXInheritance.h"
  21. #include "clang/AST/Expr.h"
  22. #include "clang/AST/ExprCXX.h"
  23. #include "llvm/ADT/APSInt.h"
  24. using namespace clang;
  25. using namespace clang::interp;
  26. //===----------------------------------------------------------------------===//
  27. // Ret
  28. //===----------------------------------------------------------------------===//
  29. template <PrimType Name, class T = typename PrimConv<Name>::T>
  30. static bool Ret(InterpState &S, CodePtr &PC, APValue &Result) {
  31. S.CallStackDepth--;
  32. const T &Ret = S.Stk.pop<T>();
  33. assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
  34. if (!S.checkingPotentialConstantExpression())
  35. S.Current->popArgs();
  36. if (InterpFrame *Caller = S.Current->Caller) {
  37. PC = S.Current->getRetPC();
  38. delete S.Current;
  39. S.Current = Caller;
  40. S.Stk.push<T>(Ret);
  41. } else {
  42. delete S.Current;
  43. S.Current = nullptr;
  44. if (!ReturnValue<T>(Ret, Result))
  45. return false;
  46. }
  47. return true;
  48. }
  49. static bool RetVoid(InterpState &S, CodePtr &PC, APValue &Result) {
  50. S.CallStackDepth--;
  51. assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
  52. if (!S.checkingPotentialConstantExpression())
  53. S.Current->popArgs();
  54. if (InterpFrame *Caller = S.Current->Caller) {
  55. PC = S.Current->getRetPC();
  56. delete S.Current;
  57. S.Current = Caller;
  58. } else {
  59. delete S.Current;
  60. S.Current = nullptr;
  61. }
  62. return true;
  63. }
  64. static bool RetValue(InterpState &S, CodePtr &Pt, APValue &Result) {
  65. llvm::report_fatal_error("Interpreter cannot return values");
  66. }
  67. //===----------------------------------------------------------------------===//
  68. // Jmp, Jt, Jf
  69. //===----------------------------------------------------------------------===//
  70. static bool Jmp(InterpState &S, CodePtr &PC, int32_t Offset) {
  71. PC += Offset;
  72. return true;
  73. }
  74. static bool Jt(InterpState &S, CodePtr &PC, int32_t Offset) {
  75. if (S.Stk.pop<bool>()) {
  76. PC += Offset;
  77. }
  78. return true;
  79. }
  80. static bool Jf(InterpState &S, CodePtr &PC, int32_t Offset) {
  81. if (!S.Stk.pop<bool>()) {
  82. PC += Offset;
  83. }
  84. return true;
  85. }
  86. static bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
  87. AccessKinds AK) {
  88. if (Ptr.isInitialized())
  89. return true;
  90. if (!S.checkingPotentialConstantExpression()) {
  91. const SourceInfo &Loc = S.Current->getSource(OpPC);
  92. S.FFDiag(Loc, diag::note_constexpr_access_uninit) << AK << false;
  93. }
  94. return false;
  95. }
  96. static bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
  97. AccessKinds AK) {
  98. if (Ptr.isActive())
  99. return true;
  100. // Get the inactive field descriptor.
  101. const FieldDecl *InactiveField = Ptr.getField();
  102. // Walk up the pointer chain to find the union which is not active.
  103. Pointer U = Ptr.getBase();
  104. while (!U.isActive()) {
  105. U = U.getBase();
  106. }
  107. // Find the active field of the union.
  108. Record *R = U.getRecord();
  109. assert(R && R->isUnion() && "Not a union");
  110. const FieldDecl *ActiveField = nullptr;
  111. for (unsigned I = 0, N = R->getNumFields(); I < N; ++I) {
  112. const Pointer &Field = U.atField(R->getField(I)->Offset);
  113. if (Field.isActive()) {
  114. ActiveField = Field.getField();
  115. break;
  116. }
  117. }
  118. const SourceInfo &Loc = S.Current->getSource(OpPC);
  119. S.FFDiag(Loc, diag::note_constexpr_access_inactive_union_member)
  120. << AK << InactiveField << !ActiveField << ActiveField;
  121. return false;
  122. }
  123. static bool CheckTemporary(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
  124. AccessKinds AK) {
  125. if (auto ID = Ptr.getDeclID()) {
  126. if (!Ptr.isStaticTemporary())
  127. return true;
  128. if (Ptr.getDeclDesc()->getType().isConstQualified())
  129. return true;
  130. if (S.P.getCurrentDecl() == ID)
  131. return true;
  132. const SourceInfo &E = S.Current->getSource(OpPC);
  133. S.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK;
  134. S.Note(Ptr.getDeclLoc(), diag::note_constexpr_temporary_here);
  135. return false;
  136. }
  137. return true;
  138. }
  139. static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
  140. if (auto ID = Ptr.getDeclID()) {
  141. if (!Ptr.isStatic())
  142. return true;
  143. if (S.P.getCurrentDecl() == ID)
  144. return true;
  145. S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_modify_global);
  146. return false;
  147. }
  148. return true;
  149. }
  150. namespace clang {
  151. namespace interp {
  152. bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
  153. if (!Ptr.isExtern())
  154. return true;
  155. if (!S.checkingPotentialConstantExpression()) {
  156. auto *VD = Ptr.getDeclDesc()->asValueDecl();
  157. const SourceInfo &Loc = S.Current->getSource(OpPC);
  158. S.FFDiag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD;
  159. S.Note(VD->getLocation(), diag::note_declared_at);
  160. }
  161. return false;
  162. }
  163. bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
  164. if (!Ptr.isUnknownSizeArray())
  165. return true;
  166. const SourceInfo &E = S.Current->getSource(OpPC);
  167. S.FFDiag(E, diag::note_constexpr_unsized_array_indexed);
  168. return false;
  169. }
  170. bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
  171. AccessKinds AK) {
  172. const auto &Src = S.Current->getSource(OpPC);
  173. if (Ptr.isZero()) {
  174. if (Ptr.isField())
  175. S.FFDiag(Src, diag::note_constexpr_null_subobject) << CSK_Field;
  176. else
  177. S.FFDiag(Src, diag::note_constexpr_access_null) << AK;
  178. return false;
  179. }
  180. if (!Ptr.isLive()) {
  181. bool IsTemp = Ptr.isTemporary();
  182. S.FFDiag(Src, diag::note_constexpr_lifetime_ended, 1) << AK << !IsTemp;
  183. if (IsTemp)
  184. S.Note(Ptr.getDeclLoc(), diag::note_constexpr_temporary_here);
  185. else
  186. S.Note(Ptr.getDeclLoc(), diag::note_declared_at);
  187. return false;
  188. }
  189. return true;
  190. }
  191. bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
  192. CheckSubobjectKind CSK) {
  193. if (!Ptr.isZero())
  194. return true;
  195. const SourceInfo &Loc = S.Current->getSource(OpPC);
  196. S.FFDiag(Loc, diag::note_constexpr_null_subobject) << CSK;
  197. return false;
  198. }
  199. bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
  200. AccessKinds AK) {
  201. if (!Ptr.isOnePastEnd())
  202. return true;
  203. const SourceInfo &Loc = S.Current->getSource(OpPC);
  204. S.FFDiag(Loc, diag::note_constexpr_access_past_end) << AK;
  205. return false;
  206. }
  207. bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
  208. CheckSubobjectKind CSK) {
  209. if (!Ptr.isElementPastEnd())
  210. return true;
  211. const SourceInfo &Loc = S.Current->getSource(OpPC);
  212. S.FFDiag(Loc, diag::note_constexpr_past_end_subobject) << CSK;
  213. return false;
  214. }
  215. bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
  216. assert(Ptr.isLive() && "Pointer is not live");
  217. if (!Ptr.isConst()) {
  218. return true;
  219. }
  220. const QualType Ty = Ptr.getType();
  221. const SourceInfo &Loc = S.Current->getSource(OpPC);
  222. S.FFDiag(Loc, diag::note_constexpr_modify_const_type) << Ty;
  223. return false;
  224. }
  225. bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
  226. assert(Ptr.isLive() && "Pointer is not live");
  227. if (!Ptr.isMutable()) {
  228. return true;
  229. }
  230. const SourceInfo &Loc = S.Current->getSource(OpPC);
  231. const FieldDecl *Field = Ptr.getField();
  232. S.FFDiag(Loc, diag::note_constexpr_access_mutable, 1) << AK_Read << Field;
  233. S.Note(Field->getLocation(), diag::note_declared_at);
  234. return false;
  235. }
  236. bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
  237. if (!CheckLive(S, OpPC, Ptr, AK_Read))
  238. return false;
  239. if (!CheckExtern(S, OpPC, Ptr))
  240. return false;
  241. if (!CheckRange(S, OpPC, Ptr, AK_Read))
  242. return false;
  243. if (!CheckInitialized(S, OpPC, Ptr, AK_Read))
  244. return false;
  245. if (!CheckActive(S, OpPC, Ptr, AK_Read))
  246. return false;
  247. if (!CheckTemporary(S, OpPC, Ptr, AK_Read))
  248. return false;
  249. if (!CheckMutable(S, OpPC, Ptr))
  250. return false;
  251. return true;
  252. }
  253. bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
  254. if (!CheckLive(S, OpPC, Ptr, AK_Assign))
  255. return false;
  256. if (!CheckExtern(S, OpPC, Ptr))
  257. return false;
  258. if (!CheckRange(S, OpPC, Ptr, AK_Assign))
  259. return false;
  260. if (!CheckGlobal(S, OpPC, Ptr))
  261. return false;
  262. if (!CheckConst(S, OpPC, Ptr))
  263. return false;
  264. return true;
  265. }
  266. bool CheckInvoke(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
  267. if (!CheckLive(S, OpPC, Ptr, AK_MemberCall))
  268. return false;
  269. if (!CheckExtern(S, OpPC, Ptr))
  270. return false;
  271. if (!CheckRange(S, OpPC, Ptr, AK_MemberCall))
  272. return false;
  273. return true;
  274. }
  275. bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
  276. if (!CheckLive(S, OpPC, Ptr, AK_Assign))
  277. return false;
  278. if (!CheckRange(S, OpPC, Ptr, AK_Assign))
  279. return false;
  280. return true;
  281. }
  282. bool CheckCallable(InterpState &S, CodePtr OpPC, Function *F) {
  283. const SourceLocation &Loc = S.Current->getLocation(OpPC);
  284. if (F->isVirtual()) {
  285. if (!S.getLangOpts().CPlusPlus20) {
  286. S.CCEDiag(Loc, diag::note_constexpr_virtual_call);
  287. return false;
  288. }
  289. }
  290. if (!F->isConstexpr()) {
  291. if (S.getLangOpts().CPlusPlus11) {
  292. const FunctionDecl *DiagDecl = F->getDecl();
  293. // If this function is not constexpr because it is an inherited
  294. // non-constexpr constructor, diagnose that directly.
  295. auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl);
  296. if (CD && CD->isInheritingConstructor()) {
  297. auto *Inherited = CD->getInheritedConstructor().getConstructor();
  298. if (!Inherited->isConstexpr())
  299. DiagDecl = CD = Inherited;
  300. }
  301. // FIXME: If DiagDecl is an implicitly-declared special member function
  302. // or an inheriting constructor, we should be much more explicit about why
  303. // it's not constexpr.
  304. if (CD && CD->isInheritingConstructor())
  305. S.FFDiag(Loc, diag::note_constexpr_invalid_inhctor, 1)
  306. << CD->getInheritedConstructor().getConstructor()->getParent();
  307. else
  308. S.FFDiag(Loc, diag::note_constexpr_invalid_function, 1)
  309. << DiagDecl->isConstexpr() << (bool)CD << DiagDecl;
  310. S.Note(DiagDecl->getLocation(), diag::note_declared_at);
  311. } else {
  312. S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
  313. }
  314. return false;
  315. }
  316. return true;
  317. }
  318. bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This) {
  319. if (!This.isZero())
  320. return true;
  321. const SourceInfo &Loc = S.Current->getSource(OpPC);
  322. bool IsImplicit = false;
  323. if (auto *E = dyn_cast_or_null<CXXThisExpr>(Loc.asExpr()))
  324. IsImplicit = E->isImplicit();
  325. if (S.getLangOpts().CPlusPlus11)
  326. S.FFDiag(Loc, diag::note_constexpr_this) << IsImplicit;
  327. else
  328. S.FFDiag(Loc);
  329. return false;
  330. }
  331. bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD) {
  332. if (!MD->isPure())
  333. return true;
  334. const SourceInfo &E = S.Current->getSource(OpPC);
  335. S.FFDiag(E, diag::note_constexpr_pure_virtual_call, 1) << MD;
  336. S.Note(MD->getLocation(), diag::note_declared_at);
  337. return false;
  338. }
  339. bool Interpret(InterpState &S, APValue &Result) {
  340. CodePtr PC = S.Current->getPC();
  341. for (;;) {
  342. auto Op = PC.read<Opcode>();
  343. CodePtr OpPC = PC;
  344. switch (Op) {
  345. #define GET_INTERP
  346. #include "Opcodes.inc"
  347. #undef GET_INTERP
  348. }
  349. }
  350. }
  351. } // namespace interp
  352. } // namespace clang