hqlwcpp.ipp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef HQLWCPP_IPP
  14. #define HQLWCPP_IPP
  15. class HQLCPP_API CppWriterTemplate : public CInterface, public ITemplateExpander
  16. {
  17. public:
  18. CppWriterTemplate(const char * codeTemplate);
  19. IMPLEMENT_IINTERFACE
  20. virtual void generate(ISectionWriter & writer, unsigned pass, IProperties * properties = NULL);
  21. void outputQuoted(ISectionWriter & writer, size32_t len, const char * str)
  22. {
  23. writer.noteLines(memcount(len, str, '\n'));
  24. outStream->write(len, str);
  25. }
  26. void setOutput(IFile * _output)
  27. {
  28. Owned<IFileIO> io = _output->open(IFOcreate);
  29. if (!io)
  30. throwError1(HQLERR_CouldNotCreateOutputX, _output->queryFilename());
  31. out.set(_output);
  32. outStream.setown(createIOStream(io));
  33. }
  34. private:
  35. void loadTemplate(const char * codeTemplate);
  36. enum TplSectionType { TplEmbed, TplExpand, TplCondition, TplEndCondition };
  37. struct CppTemplateSection : public CInterface
  38. {
  39. TplSectionType type;
  40. IAtom * id;
  41. const char * position;
  42. unsigned len;
  43. unsigned indent;
  44. };
  45. protected:
  46. const char * text;
  47. unsigned len;
  48. CIArray sections;
  49. Owned<IFile> out;
  50. Owned<IIOStream> outStream;
  51. };
  52. class HQLCPP_API HqlCppWriter
  53. {
  54. public:
  55. HqlCppWriter(CompilerType _compiler);
  56. HqlCppWriter(StringBuffer & _out, CompilerType _compiler);
  57. StringBuffer & generateExprCpp(IHqlExpression * expr);
  58. bool generateFunctionPrototype(IHqlExpression * funcdef);
  59. void generateFunctionReturnType(StringBuffer & params, ITypeInfo * retType, IHqlExpression * attrs);
  60. void generateStatementsForPass(HqlStmts & stmts, unsigned indent, unsigned pass);
  61. void generateType(ITypeInfo * type, const char * name);
  62. void noteLines(size32_t count) { outputLineNum += count; }
  63. void setOutput(IFile * out, IIOStream * outStream);
  64. protected:
  65. void flush();
  66. void generate(HqlStmtArray & stmts);
  67. void generateChildren(IHqlStmt * stmt, bool addBraces);
  68. StringBuffer & generateChildExpr(StringBuffer & out, IHqlExpression * expr, unsigned childIndex);
  69. bool generateFunctionPrototype(IHqlExpression * funcdef, const char * name);
  70. void generateInitializer(IHqlExpression * expr);
  71. void generateParamCpp(IHqlExpression * expr, IHqlExpression * attrs);
  72. void generateSimpleAssign(IHqlExpression * target, IHqlExpression * source);
  73. void generateStmt(IHqlStmt * stmt);
  74. void generateStmtAssign(IHqlStmt * assign, bool link);
  75. void generateStmtAssignModify(IHqlStmt * assign);
  76. void generateStmtCase(IHqlStmt * stmt);
  77. void generateStmtCatch(IHqlStmt * stmt);
  78. void generateStmtDeclare(IHqlStmt * declare);
  79. void generateStmtFilter(IHqlStmt * stmt);
  80. void generateStmtFunction(IHqlStmt * stmt);
  81. void generateStmtLine(IHqlStmt * stmt);
  82. void generateStmtLoop(IHqlStmt * stmt);
  83. void generateStmtSwitch(IHqlStmt * stmt);
  84. void generateStmtForPass(IHqlStmt * stmt, unsigned pass);
  85. //Wrappers around recursive calls.
  86. StringBuffer & generateExprCpp(StringBuffer & out, IHqlExpression * expr);
  87. void generateType(StringBuffer & result, ITypeInfo * type, const char * name);
  88. StringBuffer & indent();
  89. void indent(int delta) { curIndent += delta; }
  90. StringBuffer & newline();
  91. StringBuffer & queryBreakLine();
  92. StringBuffer & queryIndent();
  93. void queryNewline();
  94. StringBuffer & generateChildExpr(IHqlExpression * expr, unsigned childIndex);
  95. StringBuffer & generateCommaChildren(IHqlExpression * expr);
  96. void generateOrderExpr(IHqlExpression * left, IHqlExpression * right);
  97. StringBuffer & generateExprAsChar(IHqlExpression * expr);
  98. protected:
  99. Linked<IFile> targetFile;
  100. Linked<IIOStream> target;
  101. StringBuffer & out;
  102. StringBuffer defaultOut;
  103. unsigned curIndent;
  104. unsigned startOffset;
  105. unsigned outputLineNum;
  106. CompilerType compiler;
  107. };
  108. class HQLCPP_API HqlCppSectionWriter : implements ISectionWriter, public CInterface
  109. {
  110. public:
  111. HqlCppSectionWriter(IHqlCppInstance & _instance, CompilerType _compiler) : instance(_instance), writer(_compiler)
  112. {
  113. }
  114. IMPLEMENT_IINTERFACE
  115. virtual void generateSection(unsigned indent, IAtom * section, unsigned pass);
  116. virtual void noteLines(size32_t count) { writer.noteLines(count); }
  117. virtual void setOutput(IFile * out, IIOStream * outStream) { writer.setOutput(out, outStream); }
  118. IHqlCppInstance & instance;
  119. HqlCppWriter writer;
  120. };
  121. #endif