diff --git a/include/ast.h b/include/ast.h index 48e2f7852..ecabb3dd0 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,6 +6,12 @@ #include #include +namespace llvm{ + +class LLVMType; + +} + namespace tdl{ class module; @@ -197,7 +203,8 @@ class no_op: public statement { }; // Types class declarator: public node{ - +public: + virtual llvm::LLVMType llvm_type(TYPE_T spec) const = 0; }; class pointer_declarator: public declarator{ @@ -210,35 +217,33 @@ public: return this; } + llvm::LLVMType llvm_type(TYPE_T spec) const; + private: unsigned order_; }; class tile_declarator: public declarator{ public: - tile_declarator(node *shapes) - : shapes_((list*)(shapes)) { } + tile_declarator(node *decl, node *shapes) + : decl_(decl), shapes_((list*)(shapes)) { } + + llvm::LLVMType llvm_type(TYPE_T spec) const; public: + const node* decl_; const list* shapes_; }; -class parameter: public declarator { -public: - parameter(TYPE_T spec, node *decl) - : spec_(spec), decl_(decl) { } - -public: - const TYPE_T spec_; - const node *decl_; -}; - class function_declarator: public declarator{ public: - function_declarator(node *args) - : args_((list*)args) { } + function_declarator(node *decl, node *args) + : decl_(decl), args_((list*)args) { } + + llvm::LLVMType llvm_type(TYPE_T spec) const; public: + const node* decl_; const list* args_; }; @@ -247,6 +252,8 @@ public: compound_declarator(node *ptr, node *tile) : ptr_(ptr), tile_(tile) { } + llvm::LLVMType llvm_type(TYPE_T spec) const; + public: const node *ptr_; const node *tile_; @@ -257,11 +264,24 @@ public: init_declarator(node *decl, node *initializer) : decl_(decl), initializer_(initializer){ } + llvm::LLVMType llvm_type(TYPE_T spec) const; + public: const node *decl_; const node *initializer_; }; +class parameter: public node { +public: + parameter(TYPE_T spec, node *decl) + : spec_(spec), decl_(decl) { } + + llvm::LLVMType* llvm_type() const; + +public: + const TYPE_T spec_; + const node *decl_; +}; class type: public node{ public: @@ -276,10 +296,11 @@ public: /* Function definition */ class function_definition: public node{ public: - function_definition(node *header, node *body) - : header_((function_declarator *)header), body_((compound_statement*)body) { } + function_definition(TYPE_T spec, node *header, node *body) + : spec_(spec), header_((function_declarator *)header), body_((compound_statement*)body) { } public: + const TYPE_T spec_; const function_declarator *header_; const compound_statement *body_; }; diff --git a/include/parser.y b/include/parser.y index 73ef31ad6..50e89ce3f 100644 --- a/include/parser.y +++ b/include/parser.y @@ -84,7 +84,7 @@ abstract_declarator ; direct_abstract_declarator - : '[' constant_list ']' { $$ = new tile_declarator($1); } + : '[' constant_list ']' { $$ = new tile_declarator(nullptr, $1); } constant : CONSTANT { $$ = new constant(atoi(yytext)); } @@ -279,17 +279,12 @@ iteration_statement direct_declarator : identifier { $$ = $1; } - | direct_declarator '[' constant_list ']' { $$ = new tile_declarator($2); } - | direct_declarator '(' parameter_list ')' { $$ = new function_declarator($2); } - | direct_declarator '(' identifier_list ')' { $$ = new function_declarator($2); } - | direct_declarator '(' ')' { $$ = new function_declarator(nullptr); } - ; - -identifier_list - : identifier { $$ = new list((identifier*)$1); } - | identifier_list ',' identifier { $$ = append_ptr_list($1, $2); } + | identifier '[' constant_list ']' { $$ = new tile_declarator($1, $2); } + | identifier '(' parameter_list ')' { $$ = new function_declarator($1, $2); } + | identifier '(' ')' { $$ = new function_declarator($1, nullptr); } ; + parameter_list : parameter_declaration { $$ = new list((parameter*)$1); } | parameter_list ',' parameter_declaration { $$ = append_ptr_list($1, $2); } @@ -298,7 +293,6 @@ parameter_list parameter_declaration : declaration_specifiers declarator { $$ = new parameter(get_type_spec($1), $2); } | declaration_specifiers abstract_declarator { $$ = new parameter(get_type_spec($1), $2); } - | declaration_specifiers { $$ = new parameter(get_type_spec($1), nullptr); } ; @@ -346,7 +340,6 @@ external_declaration ; function_definition - : declaration_specifiers declarator compound_statement - | declarator compound_statement { $$ = new function_definition($1, $2); } + : type_specifier declarator compound_statement { $$ = new function_definition(get_type_spec($1), $2, $3); } ; diff --git a/lib/codegen.cpp b/lib/codegen.cpp index f3d61d59c..7372cc272 100644 --- a/lib/codegen.cpp +++ b/lib/codegen.cpp @@ -1,4 +1,3 @@ -#include "codegen.h" #include "ast.h" namespace tdl{