Add structure for custom type 94/160894/3
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 20 Nov 2017 10:48:06 +0000 (19:48 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 21 Nov 2017 01:45:49 +0000 (10:45 +0900)
Change-Id: I2848a9e44d9a5a1151b44b5d8c30c73f0aa3d4c1
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
22 files changed:
idlc/CMakeLists.txt
idlc/inc/attribute.h [new file with mode: 0644]
idlc/inc/block.h [moved from idlc/inc/Interface.h with 72% similarity]
idlc/inc/document.h
idlc/inc/generator.h
idlc/inc/interface.h
idlc/inc/parser.h
idlc/inc/proxy_gen.h
idlc/inc/structure.h [new file with mode: 0644]
idlc/inc/stub_gen.h
idlc/src/attribute.cc [new file with mode: 0644]
idlc/src/block.cc [new file with mode: 0644]
idlc/src/document.cc
idlc/src/generator.cc
idlc/src/interface.cc
idlc/src/main.cc
idlc/src/parser.cc
idlc/src/proxy_gen.cc
idlc/src/structure.cc [new file with mode: 0644]
idlc/src/stub_gen.cc
idlc/tidlc.ll
idlc/tidlc.yy

index 7e93fd8..5341487 100644 (file)
@@ -19,6 +19,9 @@ SET(SOURCES
        src/interface.cc
        src/type.cc
        src/parameter.cc
+       src/attribute.cc
+       src/structure.cc
+       src/block.cc
        src/generator.cc
        src/proxy_gen.cc
        src/stub_gen.cc
diff --git a/idlc/inc/attribute.h b/idlc/inc/attribute.h
new file mode 100644 (file)
index 0000000..f66ef28
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef IDLC_INC_ATTRIBUTE_H_
+#define IDLC_INC_ATTRIBUTE_H_
+
+#include <string>
+#include <list>
+#include <memory>
+
+#include "type.h"
+#include "parameter.h"
+
+namespace tidl {
+
+class Attribute {
+ public:
+  Attribute(const std::string& id, BaseType* type);
+
+  const std::string& GetID() const;
+  const BaseType& GetType() const;
+
+ private:
+  std::string id_;
+  std::unique_ptr<BaseType> type_;
+};
+
+class Attributes {
+ public:
+  void Add(Attribute* decl);
+  const std::list<std::unique_ptr<Attribute>>& GetAttrs() const;
+
+ private:
+  std::list<std::unique_ptr<Attribute>> attrs_;
+};
+
+}  // namespace tidl
+
+#endif  // IDLC_INC_ATTRIBUTE_H_
similarity index 72%
rename from idlc/inc/Interface.h
rename to idlc/inc/block.h
index 17b9f8a..48ce0d9 100644 (file)
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef IDLC_INC_INTERFACE_H_
-#define IDLC_INC_INTERFACE_H_
+#ifndef IDLC_INC_BLOCK_H_
+#define IDLC_INC_BLOCK_H_
 
 #include <string>
 #include <memory>
 
 namespace tidl {
 
-class Interface {
+class Block {
  public:
-  Interface(const std::string& id, Declarations* decls);
-  
+  enum Type {
+    TYPE_INTERFACE = 0,
+    TYPE_STRUCTURE,
+  };
+
+  Block(const std::string& id, Block::Type type);
+  virtual ~Block() = default;
+
   const std::string& GetID() const;
-  const Declarations& GetDeclarations() const;
+  const Block::Type& GetType() const;
 
  private:
   std::string id_;
-  std::unique_ptr<Declarations> decls_;
+  Block::Type type_;
 };
 
 }  // namespace tidl
 
-#endif  // IDLC_INC_INTERFACE_H_
+#endif  // IDLC_INC_BLOCK_H_
 
index d7750dd..56893db 100644 (file)
@@ -26,11 +26,13 @@ namespace tidl {
 
 class Document {
  public:
-  void AddInterface(Interface* interface);
-  const std::list<std::unique_ptr<Interface>>& GetInterfaces() const;
+  Document();
+
+  void AddBlock(Block* block);
+  const std::list<std::unique_ptr<Block>>& GetBlocks() const;
 
  private:
-  std::list<std::unique_ptr<Interface>> interfaces_;
+  std::list<std::unique_ptr<Block>> blocks_;
 };
 
 }  // namespace tidl
index 05e4722..7d6c81f 100644 (file)
@@ -37,7 +37,12 @@ class Generator {
   virtual void OnInterfaceBegin(const std::string& id) = 0;
   virtual void OnInterfaceEnd(const std::string& id) = 0;
   virtual void OnDeclartionGen(std::ofstream& stream, const std::string& id,
-  const Parameters& args, const BaseType& ret) = 0;
+                               const Parameters& args, const BaseType& ret) = 0;
+  virtual void OnStructureBegin(std::ofstream& stream,
+                                const std::string& id) = 0;
+  virtual void OnStructureEnd(std::ofstream& stream, const std::string& id) = 0;
+  virtual void OnAttributeGen(std::ofstream& stream, const std::string& id,
+                              const BaseType& type) = 0;
   virtual void OnInitGen(std::ofstream& stream) = 0;
   virtual void OnFiniGen(std::ofstream& stream) = 0;
 
index 17b9f8a..510f1fe 100644 (file)
 #include <string>
 #include <memory>
 
+#include "block.h"
 #include "declaration.h"
 
 namespace tidl {
 
-class Interface {
+class Interface : public Block {
  public:
   Interface(const std::string& id, Declarations* decls);
-  
-  const std::string& GetID() const;
+
   const Declarations& GetDeclarations() const;
 
  private:
-  std::string id_;
   std::unique_ptr<Declarations> decls_;
 };
 
index f9117a7..e1ef422 100644 (file)
@@ -32,8 +32,8 @@ class Parser {
   void* Scanner() const { return scanner_; }
   bool Parse(const std::string& input);
   bool ParseFromFile(const std::string& path);
-  void AttachDoc(Document* doc);
-  std::shared_ptr<Document> DetachDoc();
+  void SetDoc(Document* doc);
+  std::shared_ptr<Document> GetDoc();
 
  private:
   void* scanner_;
index 7a1d315..ac59f1c 100644 (file)
@@ -30,11 +30,15 @@ class ProxyGen : public Generator {
   virtual ~ProxyGen() = default;
 
   void OnDeclartionGen(std::ofstream& stream, const std::string& id,
-    const Parameters& args, const BaseType& ret) override;
+                       const Parameters& args, const BaseType& ret) override;
   void OnInitGen(std::ofstream& stream) override;
   void OnFiniGen(std::ofstream& stream) override;
   void OnInterfaceBegin(const std::string& id) override;
   void OnInterfaceEnd(const std::string& id) override;
+  void OnAttributeGen(std::ofstream& stream, const std::string& id,
+                      const BaseType& type) override;
+  void OnStructureBegin(std::ofstream& stream, const std::string& id) override;
+  void OnStructureEnd(std::ofstream& stream, const std::string& id) override;
 
  private:
   std::string ConvertTypeToCType(const BaseType& type);
diff --git a/idlc/inc/structure.h b/idlc/inc/structure.h
new file mode 100644 (file)
index 0000000..4022bb9
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef IDLC_INC_STRUCTURE_H_
+#define IDLC_INC_STRUCTURE_H_
+
+#include <string>
+#include <memory>
+
+#include "block.h"
+#include "attribute.h"
+
+namespace tidl {
+
+class Structure : public Block {
+ public:
+  Structure(const std::string& id, Attributes* attrs);
+
+  const Attributes& GetAttributes() const;
+
+ private:
+  std::unique_ptr<Attributes> attrs_;
+};
+
+}  // namespace tidl
+
+#endif  // IDLC_INC_STRUCTURE_H_
+
index 331467e..bf54bc1 100644 (file)
@@ -35,6 +35,10 @@ class StubGen : public Generator {
   void OnFiniGen(std::ofstream& stream) override;
   void OnInterfaceBegin(const std::string& id) override;
   void OnInterfaceEnd(const std::string& id) override;
+  void OnAttributeGen(std::ofstream& stream, const std::string& id,
+                      const BaseType& type) override;
+  void OnStructureBegin(std::ofstream& stream, const std::string& id) override;
+  void OnStructureEnd(std::ofstream& stream, const std::string& id) override;
 };
 
 }  // namespace tidl
diff --git a/idlc/src/attribute.cc b/idlc/src/attribute.cc
new file mode 100644 (file)
index 0000000..9083e15
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "attribute.h"
+#include "type.h"
+
+namespace tidl {
+
+Attribute::Attribute(const std::string& id, BaseType* type)
+    : id_(id), type_(type) {}
+
+const std::string& Attribute::GetID() const {
+  return id_;
+}
+
+const BaseType& Attribute::GetType() const {
+  return *type_;
+}
+
+void Attributes::Add(Attribute* attr) {
+  attrs_.emplace_back(attr);
+}
+
+const std::list<std::unique_ptr<Attribute>>& Attributes::GetAttrs() const {
+  return attrs_;
+}
+
+}  // namespace tidl
diff --git a/idlc/src/block.cc b/idlc/src/block.cc
new file mode 100644 (file)
index 0000000..4557795
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "type.h"
+#include "block.h"
+
+namespace tidl {
+
+Block::Block(const std::string& id, Block::Type type)
+    : id_(id), type_(type) {
+}
+
+const std::string& Block::GetID() const {
+  return id_;
+}
+
+const Block::Type& Block::GetType() const {
+  return type_;
+}
+
+}  // namespace tidl
index c8cf5ff..2fd0372 100644 (file)
 
 namespace tidl {
 
-void Document::AddInterface(Interface* interface) {
-  interfaces_.emplace_back(interface);
+Document::Document() {}
+
+void Document::AddBlock(Block* block) {
+  blocks_.emplace_back(block);
 }
 
-const std::list<std::unique_ptr<Interface>>& Document::GetInterfaces() const {
-  return interfaces_;
+const std::list<std::unique_ptr<Block>>& Document::GetBlocks() const {
+  return blocks_;
 }
 
 }  // namespace tidl
index 549fc09..6274910 100644 (file)
@@ -15,7 +15,9 @@
  */
 
 #include "generator.h"
+#include "block.h"
 #include "interface.h"
+#include "structure.h"
 #include "declaration.h"
 
 namespace tidl {
@@ -28,13 +30,23 @@ void Generator::Run(const std::string& file_name) {
   out_file_.open(FileName);
 
   OnInitGen(out_file_);
-  for (auto& interface : doc_->GetInterfaces()) {
-    OnInterfaceBegin(interface->GetID());
-    for (auto& decl : interface->GetDeclarations().GetDecls()) {
-      OnDeclartionGen(out_file_, decl->GetID(), decl->GetParameters(),
-          decl->GetType());
+  for (auto& block : doc_->GetBlocks()) {
+    if (block->GetType() == Block::TYPE_INTERFACE) {
+      Interface* interface = static_cast<Interface*>(block.get());
+      OnInterfaceBegin(interface->GetID());
+      for (auto& decl : interface->GetDeclarations().GetDecls()) {
+        OnDeclartionGen(out_file_, decl->GetID(), decl->GetParameters(),
+            decl->GetType());
+      }
+      OnInterfaceEnd(interface->GetID());
+    } else if (block->GetType() == Block::TYPE_STRUCTURE) {
+      Structure* structure = static_cast<Structure*>(block.get());
+      OnStructureBegin(out_file_, structure->GetID());
+      for (auto& attr : structure->GetAttributes().GetAttrs()) {
+        OnAttributeGen(out_file_, attr->GetID(), attr->GetType());
+      }
+      OnStructureEnd(out_file_, structure->GetID());
     }
-    OnInterfaceEnd(interface->GetID());
   }
 
   OnFiniGen(out_file_);
index 85fb37d..77069f9 100644 (file)
 
 #include "declaration.h"
 #include "interface.h"
+#include "block.h"
 
 namespace tidl {
 
 Interface::Interface(const std::string& id, Declarations* decls)
-    : id_(id), decls_(decls) {
-}
-
-const std::string& Interface::GetID() const {
-  return id_;
+    : Block::Block(id, Block::TYPE_INTERFACE), decls_(decls) {
 }
 
 const Declarations& Interface::GetDeclarations() const {
index 0a62a0b..96ac258 100644 (file)
@@ -63,10 +63,10 @@ int main(int argc, char** argv) {
 
   ps.ParseFromFile(path);
   if (opt == ::Option::PROXY) {
-    tidl::ProxyGen proxy(ps.DetachDoc());
+    tidl::ProxyGen proxy(ps.GetDoc());
     proxy.Run(argv[3]);
   } else {
-    tidl::StubGen stub(ps.DetachDoc());
+    tidl::StubGen stub(ps.GetDoc());
     stub.Run(argv[3]);
   }
 
index 3432c39..934b1da 100644 (file)
@@ -22,6 +22,8 @@
 #include "declaration.h"
 #include "parameter.h"
 #include "type.h"
+#include "attribute.h"
+#include "structure.h"
 #include "../tidlc_y.hpp"
 
 struct yy_buffer_state;
@@ -39,11 +41,11 @@ Parser::~Parser() {
   yylex_destroy(scanner_);
 }
 
-void Parser::AttachDoc(Document* doc) {
+void Parser::SetDoc(Document* doc) {
   doc_ = std::shared_ptr<Document>(doc);
 }
 
-std::shared_ptr<Document> Parser::DetachDoc() {
+std::shared_ptr<Document> Parser::GetDoc() {
   return doc_;
 }
 
index c1fd16f..2c0919f 100644 (file)
@@ -63,4 +63,18 @@ void ProxyGen::OnInterfaceEnd(const std::string& id) {
   interface_name_ = "";
 }
 
+void ProxyGen::OnAttributeGen(std::ofstream& stream, const std::string& id,
+                              const BaseType& type) {
+  stream << "\t" << type.GetFullName() << " " << id << ";" << std::endl;
+}
+
+void ProxyGen::OnStructureBegin(std::ofstream& stream, const std::string& id) {
+  stream << "typedef struct" << " " << id + "_t" << " " << "{" << std::endl;
+}
+
+void ProxyGen::OnStructureEnd(std::ofstream& stream, const std::string& id) {
+  stream << "}" << " " << id << ";" << std::endl;
+  stream << std::endl;
+}
+
 }  // namespace tidl
diff --git a/idlc/src/structure.cc b/idlc/src/structure.cc
new file mode 100644 (file)
index 0000000..5ebc5ee
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "attribute.h"
+#include "structure.h"
+#include "block.h"
+
+namespace tidl {
+
+Structure::Structure(const std::string& id, Attributes* attrs)
+    : Block::Block(id, Block::TYPE_STRUCTURE), attrs_(attrs) {
+}
+
+const Attributes& Structure::GetAttributes() const {
+  return *attrs_;
+}
+
+}  // namespace tidl
index 03287c2..4a40015 100644 (file)
@@ -32,4 +32,11 @@ void StubGen::OnInterfaceBegin(const std::string& id) {}
 
 void StubGen::OnInterfaceEnd(const std::string& id) {}
 
+void StubGen::OnAttributeGen(std::ofstream& stream, const std::string& id,
+                             const BaseType& type) {}
+
+void StubGen::OnStructureBegin(std::ofstream& stream, const std::string& id) {}
+
+void StubGen::OnStructureEnd(std::ofstream& stream, const std::string& id) {}
+
 }  // namespace tidl
index 0f4e157..9b85a6e 100644 (file)
@@ -7,6 +7,9 @@
 #include "type.h"
 #include "parameter.h"
 #include "interface.h"
+#include "attribute.h"
+#include "structure.h"
+#include "block.h"
 #include "tidlc_y.hpp"
 
 #define YY_USER_ACTION yylloc->columns(yyleng);
@@ -45,6 +48,7 @@
 "<"               {return yy::parser::token::T_META_OPEN;}
 ">"               {return yy::parser::token::T_META_CLOSE;}
 "list"            {return yy::parser::token::T_LIST;}
+"struct"          {return yy::parser::token::T_STRUCTURE;}
 
 [A-Za-z_][A-Za-z0-9_]* {
     yylval->token = new tidl::Token(yytext);
index 8f1fa70..15ad238 100644 (file)
@@ -9,6 +9,9 @@
 #include "type.h"
 #include "parameter.h"
 #include "interface.h"
+#include "attribute.h"
+#include "structure.h"
+#include "block.h"
 #include "tidlc_y.hpp"
 
 int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *);
@@ -22,7 +25,7 @@ int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *);
 
 %token T_LEFT T_RIGHT T_ID T_COMMA T_SEMICOLON T_BRACE_OPEN T_BRACE_CLOSE
 %token T_CHAR T_SHORT T_INT T_LONG T_FLOAT T_DOUBLE T_VOID T_BUNDLE T_STRING T_BOOL
-%token T_IN T_OUT T_REF T_ASYNC T_INTERFACE
+%token T_IN T_OUT T_REF T_ASYNC T_INTERFACE T_STRUCTURE
 %token T_META_OPEN T_META_CLOSE T_LIST
 
 
@@ -42,11 +45,17 @@ int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *);
   tidl::Parameter* param;
   tidl::Parameters* params;
   tidl::Token* token;
+  tidl::Structure* structure;
+  tidl::Attribute* attr;
+  tidl::Attributes* attrs;
+  tidl::Block* blk;
 }
 
 %token<token> T_ID
 
-%type<doc> interface_blocks
+%type<doc> blocks
+%type<blk> block
+%type<structure> structure_block
 %type<interf> interface_block
 %type<decls> declarations
 %type<decl> declaration
@@ -57,21 +66,53 @@ int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *);
 %type<params> parameter_list
 %type<direction> direction_specifier
 %type<token> container_type_name
+%type<attr> attribute
+%type<attrs> attributes
 
 %%
 
-start: interface_blocks {
-    ps->AttachDoc($1);
+start: blocks {
+     ps->SetDoc($1);
   }
 ;
 
-interface_blocks: interface_block {
+blocks: block {
     $$ = new tidl::Document();
-    $$->AddInterface($1);
+    $$->AddBlock($1);
   }
-  | interface_blocks interface_block {
+  | blocks block {
     $$ = $1;
-    $$->AddInterface($2);
+    $$->AddBlock($2);
+  }
+;
+
+block: interface_block {
+    $$ = $1;
+  }
+  | structure_block {
+    $$ = $1;
+  }
+;
+
+structure_block: T_STRUCTURE T_ID T_BRACE_OPEN attributes T_BRACE_CLOSE {
+    $$ = new tidl::Structure($2->ToString(), $4);
+    delete $2;
+  }
+;
+
+attributes: attribute {
+    $$ = new tidl::Attributes();
+    $$->Add($1);
+  }
+  | attributes attribute {
+    $$ = $1;
+    $$->Add($2);
+  }
+;
+
+attribute: base_type T_ID T_SEMICOLON {
+    $$ = new tidl::Attribute($2->ToString(), $1);
+    delete $2;
   }
 ;