BfSource.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "BfSource.h"
  2. #include "BfSystem.h"
  3. #include "BfParser.h"
  4. USING_NS_BF;
  5. static int gSourceCount = 0;
  6. BfSource::BfSource(BfSystem* bfSystem)
  7. {
  8. gSourceCount++;
  9. mProject = NULL;
  10. mSystem = bfSystem;
  11. mAstAllocManager = NULL;
  12. if (bfSystem != NULL)
  13. mAstAllocManager = &gBfParserCache->mAstAllocManager;
  14. mSrc = NULL;
  15. mSrcLength = 0;
  16. mSrcAllocSize = -1;
  17. mErrorRootNode = NULL;
  18. mSidechannelRootNode = NULL;
  19. mRootNode = NULL;
  20. mNextRevision = NULL;
  21. mPrevRevision = NULL;
  22. mSourceData = NULL;
  23. mAstScratch = NULL;
  24. mIsClosed = false;
  25. mRefCount = 0;
  26. mParsingFailed = false;
  27. }
  28. BfSource::~BfSource()
  29. {
  30. int sourceCount = gSourceCount--;
  31. delete mSourceData;
  32. if (mSrcAllocSize >= 0)
  33. delete [] mSrc;
  34. }
  35. bool BfSource::WantsStats()
  36. {
  37. auto parser = ToParser();
  38. if (parser == NULL)
  39. return false;
  40. return ((int)parser->mFileName.IndexOf("main2.cs") != -1);
  41. }
  42. BfErrorNode* BfSource::CreateErrorNode(BfAstNode* astNode)
  43. {
  44. BfErrorNode* errorNode = BfNodeDynCast<BfErrorNode>(astNode);
  45. if (errorNode == NULL)
  46. {
  47. errorNode = mAlloc->Alloc<BfErrorNode>();
  48. errorNode->Init(astNode->GetSrcStart(), astNode->GetSrcStart(), astNode->GetSrcEnd());
  49. errorNode->mRefNode = astNode;
  50. }
  51. return errorNode;
  52. }
  53. bool BfSource::HasPendingError(BfAstNode* astNode)
  54. {
  55. for (auto errorNode : mPendingErrorNodes)
  56. {
  57. if ((astNode->mSrcStart >= errorNode->mSrcStart) && (astNode->mSrcEnd <= errorNode->mSrcEnd))
  58. return true;
  59. }
  60. return false;
  61. }
  62. void BfSource::AddErrorNode(BfAstNode* astNode)
  63. {
  64. mPendingErrorNodes.push_back(CreateErrorNode(astNode));
  65. }
  66. int BfSource::AllocChars(int charCount)
  67. {
  68. if (mSrcLength + charCount > mSrcAllocSize)
  69. {
  70. int newAllocSize = std::max(mSrcLength + charCount, mSrcAllocSize * 2);
  71. char* newSrc = new char[newAllocSize + 1];
  72. memset(newSrc + mSrcAllocSize, 0, newAllocSize - mSrcAllocSize);
  73. if (mSrc != NULL)
  74. {
  75. memcpy(newSrc, mSrc, mSrcLength);
  76. delete [] mSrc;
  77. }
  78. mSrc = newSrc;
  79. mSrcAllocSize = newAllocSize;
  80. BF_ASSERT(mSourceData->ToParser() != NULL);
  81. mSourceData->mSrc = mSrc;
  82. HadSrcRealloc();
  83. }
  84. int retVal = mSrcLength;
  85. mSrcLength += charCount;
  86. return retVal;
  87. }
  88. /*void BfSource::AddReplaceNode(BfAstNode* astNode, const StringImpl& replaceStr)
  89. {
  90. int srcStart = AllocChars((int)replaceStr.length());
  91. memcpy((char*)mSrc + srcStart, replaceStr.c_str(), (int)replaceStr.length());
  92. auto replaceNode = mAlloc.Alloc<BfReplaceNode>();
  93. replaceNode->mSource = this;
  94. replaceNode->mSrcStart = srcStart;
  95. replaceNode->mSrcEnd = srcStart + (int)replaceStr.length();
  96. astNode->Add(replaceNode);
  97. }
  98. */
  99. int NodeCompare(const void* lhs, const void* rhs)
  100. {
  101. //BfAstNode* leftNode = *((ASTREF(BfAstNode*)*)lhs);
  102. //BfAstNode* rightNode = *((ASTREF(BfAstNode*)*)rhs);
  103. BfAstNode* leftNode = *((ASTREF(BfAstNode*)*)lhs);
  104. BfAstNode* rightNode = *((ASTREF(BfAstNode*)*)rhs);
  105. return leftNode->GetSrcStart() - rightNode->GetSrcStart();
  106. }
  107. void BfSource::FinishSideNodes()
  108. {
  109. if (!mPendingSideNodes.IsEmpty())
  110. {
  111. mSidechannelRootNode->Init(mPendingSideNodes, mAlloc);
  112. qsort(mSidechannelRootNode->mChildArr.mVals, mSidechannelRootNode->mChildArr.mSize, sizeof(ASTREF(BfAstNode*)), NodeCompare);
  113. mPendingSideNodes.clear();
  114. }
  115. }
  116. void BfSource::Close()
  117. {
  118. // if (mAlloc->mSource == NULL)
  119. // {
  120. // BF_ASSERT(mErrorRootNode == NULL);
  121. // BF_ASSERT(mPendingErrorNodes.size() == 0);
  122. // return;
  123. // }
  124. mAstScratch = mAlloc->AllocBytes(SCRATCH_SIZE, sizeof(void*));
  125. FinishSideNodes();
  126. if (!mPendingErrorNodes.IsEmpty())
  127. {
  128. mErrorRootNode->Init(mPendingErrorNodes, mAlloc);
  129. qsort(mErrorRootNode->mChildArr.mVals, mErrorRootNode->mChildArr.mSize, sizeof(ASTREF(BfAstNode*)), NodeCompare);
  130. mPendingErrorNodes.clear();
  131. }
  132. mIsClosed = true;
  133. }