hqlwcpp.ipp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #ifndef HQLWCPP_IPP
  15. #define HQLWCPP_IPP
  16. class HQLCPP_API CppWriterTemplate : public CInterface, public ITemplateExpander
  17. {
  18. public:
  19. CppWriterTemplate();
  20. ~CppWriterTemplate();
  21. IMPLEMENT_IINTERFACE
  22. virtual void generate(ISectionWriter & writer, unsigned pass, IProperties * properties = NULL);
  23. bool loadTemplate(const char * filename, const char *dir);
  24. void outputQuoted(ISectionWriter & writer, size32_t len, const char * str)
  25. {
  26. writer.noteLines(memcount(len, str, '\n'));
  27. outStream->write(len, str);
  28. }
  29. void setOutput(IFile * _output)
  30. {
  31. Owned<IFileIO> io = _output->open(IFOcreate);
  32. if (!io)
  33. throwError1(HQLERR_CouldNotCreateOutputX, _output->queryFilename());
  34. out.set(_output);
  35. outStream.setown(createIOStream(io));
  36. }
  37. private:
  38. enum TplSectionType { TplEmbed, TplExpand, TplCondition, TplEndCondition };
  39. struct CppTemplateSection : public CInterface
  40. {
  41. TplSectionType type;
  42. _ATOM id;
  43. const char * position;
  44. unsigned len;
  45. unsigned indent;
  46. };
  47. protected:
  48. char * text;
  49. unsigned len;
  50. CIArray sections;
  51. Owned<IFile> out;
  52. Owned<IIOStream> outStream;
  53. };
  54. class HQLCPP_API HqlCppWriter
  55. {
  56. public:
  57. HqlCppWriter(CompilerType _compiler);
  58. HqlCppWriter(StringBuffer & _out, CompilerType _compiler);
  59. StringBuffer & generateExprCpp(IHqlExpression * expr);
  60. bool generateFunctionPrototype(IHqlExpression * funcdef);
  61. void generateFunctionReturnType(StringBuffer & params, ITypeInfo * retType, IHqlExpression * attrs);
  62. void generateStatementsForPass(HqlStmts & stmts, unsigned indent, unsigned pass);
  63. void generateType(ITypeInfo * type, const char * name);
  64. void noteLines(size32_t count) { outputLineNum += count; }
  65. void setOutput(IFile * out, IIOStream * outStream);
  66. protected:
  67. void flush();
  68. void generate(HqlStmtArray & stmts);
  69. void generateChildren(IHqlStmt * stmt, bool addBraces);
  70. StringBuffer & generateChildExpr(StringBuffer & out, IHqlExpression * expr, unsigned childIndex);
  71. bool generateFunctionPrototype(IHqlExpression * funcdef, const char * name);
  72. void generateInitializer(IHqlExpression * expr);
  73. void generateParamCpp(IHqlExpression * expr);
  74. void generateSimpleAssign(IHqlExpression * target, IHqlExpression * source);
  75. void generateStmt(IHqlStmt * stmt);
  76. void generateStmtAssign(IHqlStmt * assign);
  77. void generateStmtAssignModify(IHqlStmt * assign);
  78. void generateStmtCase(IHqlStmt * stmt);
  79. void generateStmtDeclare(IHqlStmt * declare);
  80. void generateStmtFilter(IHqlStmt * stmt);
  81. void generateStmtFunction(IHqlStmt * stmt);
  82. void generateStmtLine(IHqlStmt * stmt);
  83. void generateStmtLoop(IHqlStmt * stmt);
  84. void generateStmtSwitch(IHqlStmt * stmt);
  85. void generateStmtForPass(IHqlStmt * stmt, unsigned pass);
  86. //Wrappers around recursive calls.
  87. StringBuffer & generateExprCpp(StringBuffer & out, IHqlExpression * expr);
  88. void generateType(StringBuffer & result, ITypeInfo * type, const char * name);
  89. StringBuffer & indent();
  90. void indent(int delta) { curIndent += delta; }
  91. StringBuffer & newline();
  92. StringBuffer & queryBreakLine();
  93. StringBuffer & queryIndent();
  94. void queryNewline();
  95. StringBuffer & generateChildExpr(IHqlExpression * expr, unsigned childIndex);
  96. StringBuffer & generateCommaChildren(IHqlExpression * expr);
  97. void generateOrderExpr(IHqlExpression * left, IHqlExpression * right);
  98. StringBuffer & generateExprAsChar(IHqlExpression * expr);
  99. protected:
  100. Linked<IFile> targetFile;
  101. Linked<IIOStream> target;
  102. StringBuffer & out;
  103. StringBuffer defaultOut;
  104. unsigned curIndent;
  105. unsigned startOffset;
  106. unsigned outputLineNum;
  107. CompilerType compiler;
  108. };
  109. class HQLCPP_API HqlCppSectionWriter : public CInterface, implements ISectionWriter
  110. {
  111. public:
  112. HqlCppSectionWriter(IHqlCppInstance & _instance, CompilerType _compiler) : instance(_instance), writer(_compiler)
  113. {
  114. }
  115. IMPLEMENT_IINTERFACE
  116. virtual void generateSection(unsigned indent, _ATOM section, unsigned pass);
  117. virtual void noteLines(size32_t count) { writer.noteLines(count); }
  118. virtual void setOutput(IFile * out, IIOStream * outStream) { writer.setOutput(out, outStream); }
  119. IHqlCppInstance & instance;
  120. HqlCppWriter writer;
  121. };
  122. #endif