BfSource.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. void BfSource::AddErrorNode(BfAstNode* astNode)
  54. {
  55. mPendingErrorNodes.push_back(CreateErrorNode(astNode));
  56. }
  57. int BfSource::AllocChars(int charCount)
  58. {
  59. if (mSrcLength + charCount > mSrcAllocSize)
  60. {
  61. int newAllocSize = std::max(mSrcLength + charCount, mSrcAllocSize * 2);
  62. char* newSrc = new char[newAllocSize + 1];
  63. memset(newSrc + mSrcAllocSize, 0, newAllocSize - mSrcAllocSize);
  64. if (mSrc != NULL)
  65. {
  66. memcpy(newSrc, mSrc, mSrcLength);
  67. delete [] mSrc;
  68. }
  69. mSrc = newSrc;
  70. mSrcAllocSize = newAllocSize;
  71. BF_ASSERT(mSourceData->ToParser() != NULL);
  72. mSourceData->mSrc = mSrc;
  73. HadSrcRealloc();
  74. }
  75. int retVal = mSrcLength;
  76. mSrcLength += charCount;
  77. return retVal;
  78. }
  79. /*void BfSource::AddReplaceNode(BfAstNode* astNode, const StringImpl& replaceStr)
  80. {
  81. int srcStart = AllocChars((int)replaceStr.length());
  82. memcpy((char*)mSrc + srcStart, replaceStr.c_str(), (int)replaceStr.length());
  83. auto replaceNode = mAlloc.Alloc<BfReplaceNode>();
  84. replaceNode->mSource = this;
  85. replaceNode->mSrcStart = srcStart;
  86. replaceNode->mSrcEnd = srcStart + (int)replaceStr.length();
  87. astNode->Add(replaceNode);
  88. }
  89. */
  90. int NodeCompare(const void* lhs, const void* rhs)
  91. {
  92. //BfAstNode* leftNode = *((ASTREF(BfAstNode*)*)lhs);
  93. //BfAstNode* rightNode = *((ASTREF(BfAstNode*)*)rhs);
  94. BfAstNode* leftNode = *((ASTREF(BfAstNode*)*)lhs);
  95. BfAstNode* rightNode = *((ASTREF(BfAstNode*)*)rhs);
  96. return leftNode->GetSrcStart() - rightNode->GetSrcStart();
  97. }
  98. void BfSource::FinishSideNodes()
  99. {
  100. if (!mPendingSideNodes.IsEmpty())
  101. {
  102. mSidechannelRootNode->Init(mPendingSideNodes, mAlloc);
  103. qsort(mSidechannelRootNode->mChildArr.mVals, mSidechannelRootNode->mChildArr.mSize, sizeof(ASTREF(BfAstNode*)), NodeCompare);
  104. mPendingSideNodes.clear();
  105. }
  106. }
  107. void BfSource::Close()
  108. {
  109. // if (mAlloc->mSource == NULL)
  110. // {
  111. // BF_ASSERT(mErrorRootNode == NULL);
  112. // BF_ASSERT(mPendingErrorNodes.size() == 0);
  113. // return;
  114. // }
  115. mAstScratch = mAlloc->AllocBytes(SCRATCH_SIZE, sizeof(void*));
  116. FinishSideNodes();
  117. if (!mPendingErrorNodes.IsEmpty())
  118. {
  119. mErrorRootNode->Init(mPendingErrorNodes, mAlloc);
  120. qsort(mErrorRootNode->mChildArr.mVals, mErrorRootNode->mChildArr.mSize, sizeof(ASTREF(BfAstNode*)), NodeCompare);
  121. mPendingErrorNodes.clear();
  122. }
  123. mIsClosed = true;
  124. }