DbgExprEvaluator.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. #pragma once
  2. #include "X86Target.h"
  3. #include "Compiler/BfAst.h"
  4. #include "DwAutoComplete.h"
  5. #include "DbgModule.h"
  6. namespace Beefy
  7. {
  8. class BfPassInstance;
  9. class BfExpression;
  10. }
  11. NS_BF_DBG_BEGIN
  12. class WinDebugger;
  13. class WdStackFrame;
  14. typedef WdStackFrame CPUStackFrame;
  15. enum DbgEvalExprFlags
  16. {
  17. DbgEvalExprFlags_None = 0,
  18. DbgEvalExprFlags_AllowTypeResult = 1,
  19. DbgEvalExprFlags_NoCast = 2
  20. };
  21. class DbgTypedValue
  22. {
  23. public:
  24. DbgType* mType;
  25. union
  26. {
  27. bool mBool;
  28. int8 mInt8;
  29. uint8 mUInt8;
  30. int16 mInt16;
  31. uint16 mUInt16;
  32. int32 mInt32;
  33. uint32 mUInt32;
  34. int64 mInt64;
  35. uint64 mUInt64;
  36. float mSingle;
  37. double mDouble;
  38. const char* mCharPtr;
  39. addr_target mPtr;
  40. const void* mLocalPtr;
  41. intptr mLocalIntPtr;
  42. DbgVariable* mVariable;
  43. };
  44. bool mIsLiteral;
  45. bool mHasNoValue;
  46. bool mIsReadOnly;
  47. union
  48. {
  49. int mRegNum;
  50. int mDataLen;
  51. };
  52. addr_target mSrcAddress;
  53. public:
  54. DbgTypedValue()
  55. {
  56. mType = NULL;
  57. mUInt64 = 0;
  58. mIsLiteral = false;
  59. mSrcAddress = 0;
  60. mHasNoValue = false;
  61. mIsReadOnly = false;
  62. mRegNum = -1;
  63. }
  64. DbgType* ResolveTypeDef() const
  65. {
  66. auto typeDef = mType;
  67. while ((typeDef != NULL) && (typeDef->mTypeCode == DbgType_TypeDef))
  68. typeDef = typeDef->mTypeParam;
  69. return typeDef;
  70. }
  71. int64 GetSExtInt() const
  72. {
  73. auto resolvedType = mType->RemoveModifiers();
  74. switch (resolvedType->mTypeCode)
  75. {
  76. case DbgType_Bool:
  77. case DbgType_i8:
  78. return (int64) mInt8;
  79. case DbgType_i16:
  80. return (int64) mInt16;
  81. case DbgType_i32:
  82. return (int64) mInt32;
  83. case DbgType_i64:
  84. return (int64) mInt64;
  85. default:
  86. BF_FATAL("Invalid type");
  87. }
  88. return 0;
  89. }
  90. int64 GetInt64() const
  91. {
  92. auto resolvedType = mType->RemoveModifiers();
  93. if (resolvedType->mTypeCode == DbgType_Enum)
  94. {
  95. if (resolvedType->mTypeParam->GetByteCount() == 0)
  96. resolvedType = resolvedType->GetPrimaryType();
  97. resolvedType = resolvedType->mTypeParam;
  98. }
  99. else if (resolvedType->mTypeCode == DbgType_Struct)
  100. {
  101. resolvedType = resolvedType->GetPrimaryType();
  102. if (resolvedType->IsTypedPrimitive())
  103. resolvedType = resolvedType->GetUnderlyingType();
  104. }
  105. switch (resolvedType->mTypeCode)
  106. {
  107. case DbgType_Bool:
  108. case DbgType_i8:
  109. case DbgType_SChar:
  110. return (int64) mInt8;
  111. case DbgType_i16:
  112. case DbgType_SChar16:
  113. return (int64) mInt16;
  114. case DbgType_i32:
  115. case DbgType_SChar32:
  116. return (int64) mInt32;
  117. case DbgType_i64:
  118. return (int64) mInt64;
  119. case DbgType_UChar:
  120. case DbgType_u8:
  121. return (int64) mUInt8;
  122. case DbgType_UChar16:
  123. case DbgType_u16:
  124. return (int64) mUInt16;
  125. case DbgType_UChar32:
  126. case DbgType_u32:
  127. return (int64) mUInt32;
  128. case DbgType_u64:
  129. return (int64) mUInt64;
  130. case DbgType_Ptr:
  131. return (int64) mUInt64;
  132. default:
  133. return 0;
  134. //BF_FATAL("Invalid type");
  135. }
  136. return 0;
  137. }
  138. addr_target GetPointer() const
  139. {
  140. if (mType->IsSizedArray())
  141. return mSrcAddress;
  142. if (mType->IsInteger())
  143. return GetInt64();
  144. return mPtr;
  145. }
  146. String GetString() const
  147. {
  148. if (!mType->IsPointer())
  149. return "";
  150. if ((mType->mTypeParam->mTypeCode != DbgType_SChar) && (mType->mTypeParam->mTypeCode != DbgType_UChar))
  151. return "";
  152. if (mIsLiteral)
  153. return mCharPtr;
  154. //return (const char*)mPtr;
  155. return "";
  156. }
  157. operator bool() const
  158. {
  159. return (mType != NULL) && (!mHasNoValue);
  160. }
  161. static DbgTypedValue GetValueless(DbgType* dbgType)
  162. {
  163. DbgTypedValue dbgTypedValue;
  164. dbgTypedValue.mType = dbgType;
  165. dbgTypedValue.mHasNoValue = true;
  166. return dbgTypedValue;
  167. }
  168. };
  169. struct DwFormatInfo
  170. {
  171. int mCallStackIdx;
  172. bool mHidePointers;
  173. bool mIgnoreDerivedClassInfo;
  174. bool mNoVisualizers;
  175. bool mNoMembers;
  176. bool mRawString;
  177. bool mAllowStringView;
  178. bool mNoEdit;
  179. String mStackSearchStr;
  180. DbgTypeKindFlags mTypeKindFlags;
  181. intptr mArrayLength;
  182. intptr mOverrideCount;
  183. intptr mMaxCount;
  184. DwDisplayType mDisplayType;
  185. DbgTypedValue mExplicitThis;
  186. int mTotalSummaryLength;
  187. String mReferenceId;
  188. String mAction;
  189. String mSubjectExpr;
  190. String mExpectedType;
  191. String mNamespaceSearch;
  192. int mExpandItemDepth;
  193. DbgLanguage mLanguage;
  194. DwFormatInfo()
  195. {
  196. mCallStackIdx = -1;
  197. mHidePointers = false;
  198. mIgnoreDerivedClassInfo = false;
  199. mRawString = false;
  200. mAllowStringView = false;
  201. mNoVisualizers = false;
  202. mNoMembers = false;
  203. mNoEdit = false;
  204. mTypeKindFlags = DbgTypeKindFlag_None;
  205. mArrayLength = -1;
  206. mOverrideCount = -1;
  207. mMaxCount = -1;
  208. mTotalSummaryLength = 0;
  209. mDisplayType = DwDisplayType_NotSpecified;
  210. mExpandItemDepth = 0;
  211. mLanguage = DbgLanguage_Unknown;
  212. }
  213. };
  214. struct DbgMethodArgument
  215. {
  216. DbgTypedValue mTypedValue;
  217. bool mWantsRef;
  218. DbgMethodArgument()
  219. {
  220. mWantsRef = false;
  221. }
  222. };
  223. typedef Array<DbgType*> DwTypeVector;
  224. class DbgExprEvaluator;
  225. class DwMethodMatcher
  226. {
  227. public:
  228. BfAstNode* mTargetSrc;
  229. DbgExprEvaluator* mExprEvaluator;
  230. String mMethodName;
  231. SizedArrayImpl<DbgTypedValue>& mArguments;
  232. bool mHadExplicitGenericArguments;
  233. BfType* mExplicitInterfaceCheck;
  234. bool mTargetIsConst;
  235. DwTypeVector mCheckMethodGenericArguments;
  236. DbgSubprogram* mBackupMethodDef;
  237. DbgSubprogram* mBestMethodDef;
  238. DbgType* mBestMethodTypeInstance;
  239. SizedArray<int, 4> mBestMethodGenericArgumentSrcs;
  240. DwTypeVector mBestMethodGenericArguments;
  241. public:
  242. void CompareMethods(DbgSubprogram* prevMethodInstance, DwTypeVector* prevGenericArgumentsSubstitute,
  243. DbgSubprogram* newMethodInstance, DwTypeVector* genericArgumentsSubstitute,
  244. bool* outNewIsBetter, bool* outNewIsWorse, bool allowSpecializeFail);
  245. public:
  246. DwMethodMatcher(BfAstNode* targetSrc, DbgExprEvaluator* exprEvaluator, const StringImpl& methodName, SizedArrayImpl<DbgTypedValue>& arguments, BfSizedArray<ASTREF(BfAstNode*)>* methodGenericArguments);
  247. bool CheckType(DbgType* typeInstance, bool isFailurePass);
  248. bool CheckMethod(DbgType* typeInstance, DbgSubprogram* checkMethod);
  249. };
  250. class DbgCallResult
  251. {
  252. public:
  253. DbgSubprogram* mSubProgram;
  254. DbgTypedValue mResult;
  255. DbgTypedValue mStructRetVal;
  256. Array<uint8> mSRetData;
  257. };
  258. class DbgStackSearch
  259. {
  260. public:
  261. String mSearchStr;
  262. BfAstNode* mIdentifier;
  263. HashSet<DbgType*> mSearchedTypes;
  264. HashSet<DbgType*> mAutocompleteSearchedTypes;
  265. int mStartingStackIdx;
  266. public:
  267. DbgStackSearch()
  268. {
  269. mIdentifier = NULL;
  270. mStartingStackIdx = -1;
  271. }
  272. };
  273. class DbgExprEvaluator : public BfStructuralVisitor
  274. {
  275. public:
  276. struct NodeReplaceRecord
  277. {
  278. BfAstNode** mNodeRef;
  279. BfAstNode* mNode;
  280. bool mForceTypeRef;
  281. NodeReplaceRecord(BfAstNode* node, BfAstNode** nodeRef, bool forceTypeRef = false)
  282. {
  283. mNode = node;
  284. mNodeRef = nodeRef;
  285. mForceTypeRef = forceTypeRef;
  286. }
  287. };
  288. struct SplatLookupEntry
  289. {
  290. String mFindName;
  291. bool mIsConst;
  292. DbgTypedValue mResult;
  293. SplatLookupEntry()
  294. {
  295. mIsConst = false;
  296. }
  297. };
  298. public:
  299. DebugTarget* mDebugTarget;
  300. DbgModule* mOrigDbgModule;
  301. DbgModule* mDbgModule;
  302. DbgCompileUnit* mDbgCompileUnit;
  303. DbgLanguage mLanguage;
  304. BfPassInstance* mPassInstance;
  305. WinDebugger* mDebugger;
  306. String mExpectingTypeName;
  307. String mSubjectExpr;
  308. DbgTypedValue mSubjectValue;
  309. DbgType* mExpectingType;
  310. DbgSubprogram* mCurMethod;
  311. DbgTypedValue mResult;
  312. DbgTypedValue* mReceivingValue;
  313. intptr mCountResultOverride;
  314. DbgTypedValue mExplicitThis;
  315. BfExpression* mExplicitThisExpr;
  316. Array<DbgCallResult>* mCallResults;
  317. addr_target mCallStackPreservePos;
  318. int mCallResultIdx;
  319. String mNamespaceSearchStr;
  320. Array<DbgType*> mNamespaceSearch;
  321. Dictionary<BfAstNode*, SplatLookupEntry> mSplatLookupMap;
  322. DbgTypedValue mPropTarget;
  323. DbgSubprogram* mPropSet;
  324. DbgSubprogram* mPropGet;
  325. BfAstNode* mPropSrc;
  326. SizedArray<BfExpression*, 2> mIndexerExprValues;
  327. SizedArray<DbgTypedValue, 2> mIndexerValues;
  328. bool mIsEmptyTarget;
  329. DwEvalExpressionFlags mExpressionFlags;
  330. bool mHadSideEffects;
  331. bool mBlockedSideEffects;
  332. bool mIgnoreErrors;
  333. bool mCreatedPendingCall;
  334. bool mValidateOnly;
  335. int mCallStackIdx;
  336. int mCursorPos;
  337. DwAutoComplete* mAutoComplete;
  338. DbgStackSearch* mStackSearch;
  339. Array<Array<uint8>> mTempStorage;
  340. Array<NodeReplaceRecord> mDeferredInsertExplicitThisVector;
  341. String* mReferenceId;
  342. bool mHadMemberReference;
  343. bool mIsComplexExpression;
  344. public:
  345. DbgTypedValue ReadTypedValue(BfAstNode* targetSrc, DbgType* type, uint64 valAddr, DbgAddrType addrType);
  346. bool CheckTupleCreation(addr_target receiveAddr, BfAstNode* targetSrc, DbgType* tupleType, const BfSizedArray<BfExpression*>& argValues, BfSizedArray<BfTupleNameNode*>* names);
  347. DbgTypedValue CheckEnumCreation(BfAstNode* targetSrc, DbgType* enumType, const StringImpl& caseName, const BfSizedArray<BfExpression*>& argValues);
  348. void DoInvocation(BfAstNode* target, BfSizedArray<ASTREF(BfExpression*)>& args, BfSizedArray<ASTREF(BfAstNode*)>* methodGenericArguments, bool& failed);
  349. bool ResolveArgValues(const BfSizedArray<ASTREF(BfExpression*)>& arguments, SizedArrayImpl<DbgTypedValue>& outArgValues);
  350. DbgTypedValue CreateCall(DbgSubprogram* method, DbgTypedValue thisVal, DbgTypedValue structRetVal, bool bypassVirtual, CPURegisters* registers);
  351. DbgTypedValue CreateCall(DbgSubprogram* method, SizedArrayImpl<DbgMethodArgument>& argPushQueue, bool bypassVirtual);
  352. DbgTypedValue CreateCall(BfAstNode* targetSrc, DbgTypedValue target, DbgSubprogram* methodDef, bool bypassVirtual, const BfSizedArray<ASTREF(BfExpression*)>& arguments, SizedArrayImpl<DbgTypedValue>& argValues);
  353. DbgTypedValue MatchMethod(BfAstNode* targetSrc, DbgTypedValue target, bool allowImplicitThis, bool bypassVirtual, const StringImpl& methodName,
  354. const BfSizedArray<ASTREF(BfExpression*)>& arguments, BfSizedArray<ASTREF(BfAstNode*)>* methodGenericArguments, bool& failed);
  355. DbgType* ResolveSubTypeRef(DbgType* checkType, const StringImpl& name);
  356. void PerformBinaryOperation(ASTREF(BfExpression*)& leftExpression, ASTREF(BfExpression*)& rightExpression, BfBinaryOp binaryOp, BfTokenNode* opToken, bool forceLeftType);
  357. void PerformBinaryOperation(DbgType* resultType, DbgTypedValue convLeftValue, DbgTypedValue convRightValue, BfBinaryOp binaryOp, BfTokenNode* opToken);
  358. void PerformUnaryExpression(BfAstNode* opToken, BfUnaryOp unaryOp, ASTREF(BfExpression*)& expr);
  359. DbgTypedValue CreateValueFromExpression(ASTREF(BfExpression*)& expr, DbgType* castToType = NULL, DbgEvalExprFlags flags = DbgEvalExprFlags_None);
  360. const char* GetTypeName(DbgType* type);
  361. DbgTypedValue GetResult();
  362. bool HasPropResult();
  363. DbgLanguage GetLanguage();
  364. DbgFlavor GetFlavor();
  365. public:
  366. DbgExprEvaluator(WinDebugger* winDebugger, DbgModule* dbgModule, BfPassInstance* passInstance, int callStackIdx, int cursorPos);
  367. ~DbgExprEvaluator();
  368. DbgTypedValue GetInt(int value);
  369. DbgTypedValue GetString(const StringImpl& str);
  370. void Fail(const StringImpl& error, BfAstNode* node);
  371. void Warn(const StringImpl& error, BfAstNode* node);
  372. DbgType* GetExpectingType();
  373. void GetNamespaceSearch();
  374. DbgType* FixType(DbgType* dbgType);
  375. DbgTypedValue FixThis(const DbgTypedValue& thisVal);
  376. DbgType* ResolveTypeRef(BfTypeReference* typeRef);
  377. DbgType* ResolveTypeRef(BfAstNode* typeRef, BfAstNode** parentChildRef = NULL);
  378. DbgType* ResolveTypeRef(const StringImpl& typeRef);
  379. static bool TypeIsSubTypeOf(DbgType* srcType, DbgType* wantType, int* thisOffset = NULL, addr_target* thisAddr = NULL);
  380. DbgTypedValue GetBeefTypeById(int typeId);
  381. void BeefStringToString(addr_target addr, String& outStr);
  382. void BeefStringToString(const DbgTypedValue& val, String& outStr);
  383. void BeefTypeToString(const DbgTypedValue& val, String& outStr);
  384. CPUStackFrame* GetStackFrame();
  385. CPURegisters* GetRegisters();
  386. DbgTypedValue GetRegister(const StringImpl& regName);
  387. DbgSubprogram* GetCurrentMethod();
  388. DbgType* GetCurrentType();
  389. DbgTypedValue GetThis();
  390. DbgTypedValue GetDefaultTypedValue(DbgType* srcType);
  391. bool IsAutoCompleteNode(BfAstNode* node, int lengthAdd = 0);
  392. void AutocompleteCheckType(BfTypeReference* typeReference);
  393. void AutocompleteAddTopLevelTypes(const StringImpl& filter);
  394. void AutocompleteAddMethod(const char* methodName, const StringImpl& filter);
  395. void AutocompleteAddMembers(DbgType* dbgType, bool wantsStatic, bool wantsNonStatic, const StringImpl& filter, bool isCapture = false);
  396. void AutocompleteCheckMemberReference(BfAstNode* target, BfAstNode* dotToken, BfAstNode* memberName);
  397. DbgTypedValue RemoveRef(DbgTypedValue typedValue);
  398. bool StoreValue(DbgTypedValue& ptr, DbgTypedValue& value, BfAstNode* refNode);
  399. bool StoreValue(DbgTypedValue& ptr, BfExpression* expr);
  400. String TypeToString(DbgType* type);
  401. bool CheckHasValue(DbgTypedValue typedValue, BfAstNode* refNode);
  402. bool CanCast(DbgTypedValue typedVal, DbgType* toType, BfCastFlags castFlags = BfCastFlags_None);
  403. DbgTypedValue Cast(BfAstNode* srcNode, const DbgTypedValue& val, DbgType* toType, bool explicitCast = false, bool silentFail = false);
  404. bool HasField(DbgType* type, const StringImpl& fieldName);
  405. DbgTypedValue DoLookupField(BfAstNode* targetSrc, DbgTypedValue target, DbgType* curCheckType, const StringImpl& fieldName, CPUStackFrame* stackFrame, bool allowImplicitThis);
  406. DbgTypedValue LookupField(BfAstNode* targetSrc, DbgTypedValue target, const StringImpl& fieldName);
  407. DbgTypedValue DoLookupIdentifier(BfAstNode* identifierNode, bool ignoreInitialError, bool* hadError);
  408. DbgTypedValue LookupIdentifier(BfAstNode* identifierNode, bool ignoreInitialError = false, bool* hadError = NULL);
  409. void LookupSplatMember(const DbgTypedValue& target, const StringImpl& fieldName);
  410. void LookupSplatMember(BfAstNode* srcNode, BfAstNode* lookupNode, const DbgTypedValue& target, const StringImpl& fieldName, String* outFindName = NULL, bool* outIsConst = NULL, StringImpl* forceName = NULL);
  411. void LookupQualifiedName(BfQualifiedNameNode* nameNode, bool ignoreInitialError = false, bool* hadError = NULL);
  412. DbgType* FindSubtype(DbgType* type, const StringImpl& name);
  413. void LookupQualifiedStaticField(BfQualifiedNameNode* nameNode, bool ignoreIdentifierNotFoundError = false);
  414. bool EnsureRunning(BfAstNode* astNode);
  415. virtual void Visit(BfAssignmentExpression* assignExpr) override;
  416. virtual void Visit(BfParenthesizedExpression* parenExpr) override;
  417. virtual void Visit(BfMemberReferenceExpression* memberRefExpr) override;
  418. virtual void Visit(BfIndexerExpression* indexerExpr) override;
  419. virtual void Visit(BfQualifiedNameNode* nameNode) override;
  420. virtual void Visit(BfThisExpression* thisExpr) override;
  421. virtual void Visit(BfIdentifierNode* node) override;
  422. virtual void Visit(BfAttributedIdentifierNode* node) override;
  423. virtual void Visit(BfMixinExpression* mixinExpr) override;
  424. virtual void Visit(BfAstNode* node) override;
  425. virtual void Visit(BfDefaultExpression* defaultExpr) override;
  426. virtual void Visit(BfLiteralExpression* literalExpr) override;
  427. virtual void Visit(BfCastExpression* castExpr) override;
  428. virtual void Visit(BfBinaryOperatorExpression* binOpExpr) override;
  429. virtual void Visit(BfUnaryOperatorExpression* unaryOpExpr) override;
  430. virtual void Visit(BfInvocationExpression* invocationExpr) override;
  431. virtual void Visit(BfConditionalExpression* condExpr) override;
  432. virtual void Visit(BfTypeAttrExpression* typeAttrExpr) override;
  433. virtual void Visit(BfTupleExpression* tupleExpr) override;
  434. DbgTypedValue Resolve(BfExpression* expr, DbgType* wantType = NULL);
  435. BfAstNode* FinalizeExplicitThisReferences(BfAstNode* headNode);
  436. };
  437. NS_BF_DBG_END