From 14b9cbd43c21ad2ba75b38ef5fc681c044e7662e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 8 Jan 2016 20:59:47 -0700 Subject: add restrict qualifier on pointer arguments --- src/parser.cpp | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/parser.cpp b/src/parser.cpp index e8da95bb3d..0a1fb654b3 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -224,16 +224,18 @@ void ast_print(AstNode *node, int indent) { } case AstNodeTypeTypePointer: { - const char *const_or_mut_str = node->data.type.is_const ? "const" : "var"; - fprintf(stderr, "'%s' PointerType\n", const_or_mut_str); + const char *const_or_mut_str = node->data.type.is_const ? "const " : ""; + const char *restrict_or_not_str = node->data.type.is_restrict ? "restrict " : ""; + fprintf(stderr, "%s%s PointerType\n", const_or_mut_str, restrict_or_not_str); ast_print(node->data.type.child_type, indent + 2); break; } case AstNodeTypeTypeArray: { - const char *const_or_mut_str = node->data.type.is_const ? "const" : "var"; - fprintf(stderr, "'%s' ArrayType\n", const_or_mut_str); + const char *const_or_mut_str = node->data.type.is_const ? "const " : ""; + const char *restrict_or_not_str = node->data.type.is_restrict ? "restrict " : ""; + fprintf(stderr, "%s%s ArrayType\n", const_or_mut_str, restrict_or_not_str); if (node->data.type.array_size) ast_print(node->data.type.array_size, indent + 2); ast_print(node->data.type.child_type, indent + 2); @@ -1022,6 +1024,13 @@ static void ast_parse_type_assume_amp(ParseContext *pc, int *token_index, AstNod node->data.type.is_const = true; *token_index += 1; first_type_token = &pc->tokens->at(*token_index); + if (first_type_token->id == TokenIdKeywordRestrict) { + node->data.type.is_restrict = true; + *token_index += 1; + } + } else if (first_type_token->id == TokenIdKeywordRestrict) { + node->data.type.is_restrict = true; + *token_index += 1; } node->data.type.child_type = ast_parse_type(pc, token_index); @@ -1079,8 +1088,8 @@ static AstNode *ast_parse_compiler_fn_call(ParseContext *pc, int *token_index, b /* Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType | MaybeType | CompilerFnExpr -PointerType : token(Ampersand) option(token(Const)) Type -ArrayType : token(LBracket) option(Expression) token(RBracket) Type +PointerType : token(Ampersand) option(token(Const)) option(token(Restrict)) Type +ArrayType : token(LBracket) option(Expression) token(RBracket) option(token(Const)) option(token(Restrict)) Type */ static AstNode *ast_parse_type(ParseContext *pc, int *token_index) { Token *token = &pc->tokens->at(*token_index); @@ -1129,6 +1138,15 @@ static AstNode *ast_parse_type(ParseContext *pc, int *token_index) { if (const_tok->id == TokenIdKeywordConst) { *token_index += 1; node->data.type.is_const = true; + + Token *next_tok = &pc->tokens->at(*token_index); + if (next_tok->id == TokenIdKeywordRestrict) { + *token_index += 1; + node->data.type.is_restrict = true; + } + } else if (const_tok->id == TokenIdKeywordRestrict) { + *token_index += 1; + node->data.type.is_restrict = true; } node->data.type.child_type = ast_parse_type(pc, token_index); @@ -1476,7 +1494,7 @@ static PrefixOp tok_to_prefix_op(Token *token) { } /* -PrefixOp : token(Not) | token(Dash) | token(Tilde) | (token(Ampersand) option(token(Const))) +PrefixOp : token(Not) | token(Dash) | token(Tilde) | token(Star) | (token(Ampersand) option(token(Const))) */ static PrefixOp ast_parse_prefix_op(ParseContext *pc, int *token_index, bool mandatory) { Token *token = &pc->tokens->at(*token_index); -- cgit v1.2.3