discover/grub2: Use subclass-style structs for AST
authorJeremy Kerr <jk@ozlabs.org>
Thu, 12 Sep 2013 07:54:36 +0000 (15:54 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Tue, 24 Sep 2013 05:14:59 +0000 (13:14 +0800)
Use embedded 'struct grub2_statement' data to create a subclass-style
syntax tree of statements.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/grub2/grub2.h
discover/grub2/parser-api.c

index cc817ff4b9eae7e29fb6ebbcebf5775d3376f293..0643a7293d8aba9025e742d5c822f7ff8901d6fc 100644 (file)
@@ -18,45 +18,43 @@ struct grub2_argv {
        struct list             words;
 };
 
+struct grub2_statements {
+       struct list             list;
+};
+
+struct grub2_statement {
+       struct list_item        list;
+       enum {
+               STMT_TYPE_SIMPLE,
+               STMT_TYPE_MENUENTRY,
+               STMT_TYPE_IF,
+               STMT_TYPE_BLOCK,
+       } type;
+};
+
 struct grub2_statement_simple {
+       struct grub2_statement  st;
        struct grub2_argv       *argv;
 };
 
 struct grub2_statement_menuentry {
+       struct grub2_statement  st;
        struct grub2_argv       *argv;
        struct grub2_statements *statements;
 };
 
 struct grub2_statement_if {
+       struct grub2_statement  st;
        struct grub2_statement  *condition;
        struct grub2_statements *true_case;
        struct grub2_statements *false_case;
 };
 
 struct grub2_statement_block {
+       struct grub2_statement  st;
        struct grub2_statements *statements;
 };
 
-struct grub2_statement {
-       struct list_item        list;
-       enum {
-               STMT_TYPE_SIMPLE,
-               STMT_TYPE_MENUENTRY,
-               STMT_TYPE_IF,
-               STMT_TYPE_BLOCK,
-       } type;
-       union {
-               struct grub2_statement_simple           simple;
-               struct grub2_statement_menuentry        menuentry;
-               struct grub2_statement_if               ifstmt;
-               struct grub2_statement_block            block;
-       };
-};
-
-struct grub2_statements {
-       struct list             list;
-};
-
 struct grub2_parser {
        void                    *scanner;
        struct grub2_statements *statements;
index 37c88a264d750590f282bf0bfa39262ec9b7c2f5..d3bec3d31029b60689546b68c84884d70d7ab63b 100644 (file)
@@ -14,20 +14,22 @@ struct grub2_statements *create_statements(struct grub2_parser *parser)
 struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
                struct grub2_argv *argv)
 {
-       struct grub2_statement *stmt = talloc(parser, struct grub2_statement);
-       stmt->type = STMT_TYPE_SIMPLE;
-       stmt->simple.argv = argv;
-       return stmt;
+       struct grub2_statement_simple *stmt =
+               talloc(parser, struct grub2_statement_simple);
+       stmt->st.type = STMT_TYPE_SIMPLE;
+       stmt->argv = argv;
+       return &stmt->st;
 }
 
 struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
                struct grub2_argv *argv, struct grub2_statements *stmts)
 {
-       struct grub2_statement *stmt = talloc(parser, struct grub2_statement);
-       stmt->type = STMT_TYPE_MENUENTRY;
-       stmt->menuentry.argv = argv;
-       stmt->menuentry.statements = stmts;
-       return stmt;
+       struct grub2_statement_menuentry *stmt =
+               talloc(parser, struct grub2_statement_menuentry);
+       stmt->st.type = STMT_TYPE_MENUENTRY;
+       stmt->argv = argv;
+       stmt->statements = stmts;
+       return &stmt->st;
 }
 
 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
@@ -35,21 +37,23 @@ struct grub2_statement *create_statement_if(struct grub2_parser *parser,
                struct grub2_statements *true_case,
                struct grub2_statements *false_case)
 {
-       struct grub2_statement *stmt = talloc(parser, struct grub2_statement);
-       stmt->type = STMT_TYPE_IF;
-       stmt->ifstmt.condition = condition;
-       stmt->ifstmt.true_case = true_case;
-       stmt->ifstmt.false_case = false_case;
-       return stmt;
+       struct grub2_statement_if *stmt =
+               talloc(parser, struct grub2_statement_if);
+       stmt->st.type = STMT_TYPE_IF;
+       stmt->condition = condition;
+       stmt->true_case = true_case;
+       stmt->false_case = false_case;
+       return &stmt->st;
 }
 
 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
                struct grub2_statements *stmts)
 {
-       struct grub2_statement *stmt = talloc(parser, struct grub2_statement);
-       stmt->type = STMT_TYPE_BLOCK;
-       stmt->block.statements = stmts;
-       return stmt;
+       struct grub2_statement_block *stmt =
+               talloc(parser, struct grub2_statement_block);
+       stmt->st.type = STMT_TYPE_BLOCK;
+       stmt->statements = stmts;
+       return &stmt->st;
 }
 
 void statement_append(struct grub2_statements *stmts,