JSBPreprocessVisitor.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include <Atomic/IO/Log.h>
  24. #include "JSBNameVisitor.h"
  25. #include "JSBEnum.h"
  26. #include "JSBModule.h"
  27. #include "JSBHeader.h"
  28. namespace ToolCore
  29. {
  30. class JSBPreprocessVisitor : public SymbolVisitor
  31. {
  32. public:
  33. JSBPreprocessVisitor(JSBHeader* header, TranslationUnit *unit, Namespace* globalNamespace) :
  34. header_(header),
  35. globalNamespace_(globalNamespace)
  36. {
  37. module_ = header_->GetModule();
  38. accept(globalNamespace_);
  39. }
  40. String getNameString(const Name* name)
  41. {
  42. JSBNameVisitor nvisitor;
  43. return nvisitor(name);
  44. }
  45. // reject template types
  46. virtual bool visit(Template *t)
  47. {
  48. return false;
  49. }
  50. virtual bool visit(Enum *penum)
  51. {
  52. // don't want enum's in classes
  53. if (classes_.Size())
  54. return true;
  55. JSBModule* module = header_->GetModule();
  56. JSBEnum* jenum = new JSBEnum(header_->GetContext(), module_, getNameString(penum->name()));
  57. jenum->SetHeader(header_);
  58. for (unsigned i = 0; i < penum->memberCount(); i++)
  59. {
  60. Symbol* symbol = penum->memberAt(i);
  61. String name = getNameString(symbol->name());
  62. String value = "";
  63. Declaration* decl = symbol->asDeclaration();
  64. if (decl)
  65. {
  66. EnumeratorDeclaration* enumDecl = decl->asEnumeratorDeclarator();
  67. const StringLiteral* constantValue = enumDecl->constantValue();
  68. if (constantValue)
  69. {
  70. value = constantValue->chars();
  71. }
  72. }
  73. jenum->AddValue(name, value);
  74. }
  75. jenum->Preprocess();
  76. module->RegisterEnum(jenum);
  77. return true;
  78. }
  79. virtual bool visit(Class *klass)
  80. {
  81. classes_.Push(klass);
  82. String name = getNameString(klass->name());
  83. JSBModule* module = header_->GetModule();
  84. module->RegisterClass(name);
  85. return true;
  86. }
  87. void postVisit(Symbol *symbol)
  88. {
  89. if (symbol->asClass())
  90. {
  91. classes_.Remove((Class*) symbol);
  92. }
  93. }
  94. private:
  95. SharedPtr<JSBHeader> header_;
  96. SharedPtr<JSBModule> module_;
  97. PODVector<Class*> classes_;
  98. Namespace* globalNamespace_;
  99. };
  100. }