add pointer dereferencing operator

This commit is contained in:
Andrew Kelley 2016-01-04 19:10:52 -07:00
parent 22c52f1eb6
commit e0aa0736be
6 changed files with 77 additions and 6 deletions

View File

@ -154,7 +154,7 @@ FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen)
ArrayAccessExpression : token(LBracket) Expression token(RBracket)
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)))
PrimaryExpression : token(Number) | token(String) | token(CharLiteral) | KeywordLiteral | GroupedExpression | Goto | token(Break) | token(Continue) | BlockExpression | token(Symbol) | StructValueExpression | CompilerFnType
@ -173,7 +173,7 @@ KeywordLiteral : token(Unreachable) | token(Void) | token(True) | token(False)
```
x() x[] x.y
!x -x ~x &x &const x
!x -x ~x *x &x &const x
as
* / %
+ -

View File

@ -1271,31 +1271,51 @@ static TypeTableEntry *analyze_lvalue(CodeGen *g, ImportTableEntry *import, Bloc
if (purpose == LValPurposeAssign && var->is_const) {
add_node_error(g, lhs_node,
buf_sprintf("cannot assign to constant"));
expected_rhs_type = g->builtin_types.entry_invalid;
} else if (purpose == LValPurposeAddressOf && var->is_const && !is_ptr_const) {
add_node_error(g, lhs_node,
buf_sprintf("must use &const to get address of constant"));
expected_rhs_type = g->builtin_types.entry_invalid;
} else {
expected_rhs_type = var->type;
}
} else {
add_node_error(g, lhs_node,
buf_sprintf("use of undeclared identifier '%s'", buf_ptr(name)));
expected_rhs_type = g->builtin_types.entry_invalid;
}
} else if (lhs_node->type == NodeTypeArrayAccessExpr) {
expected_rhs_type = analyze_array_access_expr(g, import, block_context, lhs_node);
} else if (lhs_node->type == NodeTypeFieldAccessExpr) {
alloc_codegen_node(lhs_node);
expected_rhs_type = analyze_field_access_expr(g, import, block_context, lhs_node);
} else if (lhs_node->type == NodeTypePrefixOpExpr &&
lhs_node->data.prefix_op_expr.prefix_op == PrefixOpDereference)
{
assert(purpose == LValPurposeAssign);
AstNode *target_node = lhs_node->data.prefix_op_expr.primary_expr;
TypeTableEntry *type_entry = analyze_expression(g, import, block_context, nullptr, target_node);
if (type_entry->id == TypeTableEntryIdInvalid) {
expected_rhs_type = type_entry;
} else if (type_entry->id == TypeTableEntryIdPointer) {
expected_rhs_type = type_entry->data.pointer.child_type;
} else {
add_node_error(g, target_node,
buf_sprintf("indirection requires pointer operand ('%s' invalid)",
buf_ptr(&type_entry->name)));
expected_rhs_type = g->builtin_types.entry_invalid;
}
} else {
if (purpose == LValPurposeAssign) {
add_node_error(g, lhs_node,
buf_sprintf("assignment target must be variable, field, or array element"));
buf_sprintf("invalid assignment target"));
} else if (purpose == LValPurposeAddressOf) {
add_node_error(g, lhs_node,
buf_sprintf("addressof target must be variable, field, or array element"));
buf_sprintf("invalid addressof target"));
}
expected_rhs_type = g->builtin_types.entry_invalid;
}
assert(expected_rhs_type);
return expected_rhs_type;
}
@ -1940,6 +1960,22 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
return_type = get_pointer_to_type(g, child_type, is_const);
break;
}
case PrefixOpDereference:
{
TypeTableEntry *type_entry = analyze_expression(g, import, context, nullptr,
node->data.prefix_op_expr.primary_expr);
if (type_entry->id == TypeTableEntryIdInvalid) {
return_type = type_entry;
} else if (type_entry->id == TypeTableEntryIdPointer) {
return_type = type_entry->data.pointer.child_type;
} else {
add_node_error(g, node->data.prefix_op_expr.primary_expr,
buf_sprintf("indirection requires pointer operand ('%s' invalid)",
buf_ptr(&type_entry->name)));
return_type = g->builtin_types.entry_invalid;
}
break;
}
}
break;
case NodeTypeIfBoolExpr:

View File

@ -359,6 +359,13 @@ static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node,
}
} else if (node->type == NodeTypeFieldAccessExpr) {
target_ref = gen_field_ptr(g, node, out_type_entry);
} else if (node->type == NodeTypePrefixOpExpr) {
assert(node->data.prefix_op_expr.prefix_op == PrefixOpDereference);
AstNode *target_expr = node->data.prefix_op_expr.primary_expr;
TypeTableEntry *type_entry = get_expr_type(target_expr);
assert(type_entry->id == TypeTableEntryIdPointer);
*out_type_entry = type_entry->data.pointer.child_type;
return gen_expr(g, target_expr);
} else {
zig_panic("bad assign target");
}
@ -397,11 +404,16 @@ static LLVMValueRef gen_prefix_op_expr(CodeGen *g, AstNode *node) {
case PrefixOpAddressOf:
case PrefixOpConstAddressOf:
{
add_debug_source_node(g, node);
TypeTableEntry *lvalue_type;
return gen_lvalue(g, node, expr_node, &lvalue_type);
}
case PrefixOpDereference:
{
LLVMValueRef expr = gen_expr(g, expr_node);
add_debug_source_node(g, node);
return LLVMBuildLoad(g->builder, expr, "");
}
}
zig_unreachable();
}

View File

@ -60,6 +60,7 @@ static const char *prefix_op_str(PrefixOp prefix_op) {
case PrefixOpBinNot: return "~";
case PrefixOpAddressOf: return "&";
case PrefixOpConstAddressOf: return "&const";
case PrefixOpDereference: return "*";
}
zig_unreachable();
}
@ -1422,6 +1423,7 @@ static PrefixOp tok_to_prefix_op(Token *token) {
case TokenIdDash: return PrefixOpNegation;
case TokenIdTilde: return PrefixOpBinNot;
case TokenIdAmpersand: return PrefixOpAddressOf;
case TokenIdStar: return PrefixOpDereference;
default: return PrefixOpInvalid;
}
}

View File

@ -212,6 +212,7 @@ enum PrefixOp {
PrefixOpNegation,
PrefixOpAddressOf,
PrefixOpConstAddressOf,
PrefixOpDereference,
};
struct AstNodePrefixOpExpr {

View File

@ -755,6 +755,26 @@ pub fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
}
print_str("OK\n");
return 0;
}
)SOURCE", "OK\n");
add_simple_case("pointer dereferencing", R"SOURCE(
use "std.zig";
pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
var x = 3 as i32;
const y = &x;
*y += 1;
if (x != 4) {
print_str("BAD\n");
}
if (*y != 4) {
print_str("BAD\n");
}
print_str("OK\n");
return 0;
}
)SOURCE", "OK\n");
}
@ -904,7 +924,7 @@ a_label:
fn f() {
3 = 3;
}
)SOURCE", 1, ".tmp_source.zig:3:5: error: assignment target must be variable, field, or array element");
)SOURCE", 1, ".tmp_source.zig:3:5: error: invalid assignment target");
add_compile_fail_case("assign to constant variable", R"SOURCE(
fn f() {