authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-07 19:37:34-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-07 19:37:34-05:00
logb8379b4c5b702badf15f668ad30369f4370e4490
tree183b8ca639fab1c39931f14f256fa5aa4a13328f
parent05de70017d8bf15a37f1d1be4e23f5d3f3f9b146

IR: support pointer types


1 files changed, 63 insertions(+), 32 deletions(-)

src/ir.cpp+63-32
......@@ -1309,7 +1309,6 @@ static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, AstNode *node) {
13091309 assert(node->type == NodeTypePrefixOpExpr);
13101310
13111311 PrefixOp prefix_op = node->data.prefix_op_expr.prefix_op;
1312 //AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
13131312
13141313 switch (prefix_op) {
13151314 case PrefixOpInvalid:
......@@ -3013,6 +3012,68 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct
30133012 zig_unreachable();
30143013}
30153014
3015static TypeTableEntry *ir_analyze_unary_address_of(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {
3016 IrInstruction *value = un_op_instruction->value->other;
3017 if (value->type_entry->id == TypeTableEntryIdInvalid)
3018 return ira->codegen->builtin_types.entry_invalid;
3019
3020 bool is_const;
3021 if (un_op_instruction->op_id == IrUnOpAddressOf) {
3022 is_const = false;
3023 } else if (un_op_instruction->op_id == IrUnOpConstAddressOf) {
3024 is_const = true;
3025 } else {
3026 zig_unreachable();
3027 }
3028
3029 TypeTableEntry *child_type = value->type_entry;
3030 TypeTableEntry *canon_child_type = get_underlying_type(child_type);
3031 switch (canon_child_type->id) {
3032 case TypeTableEntryIdTypeDecl:
3033 zig_unreachable();
3034 case TypeTableEntryIdInvalid:
3035 return ira->codegen->builtin_types.entry_invalid;
3036 case TypeTableEntryIdNumLitFloat:
3037 case TypeTableEntryIdNumLitInt:
3038 case TypeTableEntryIdUndefLit:
3039 case TypeTableEntryIdNullLit:
3040 case TypeTableEntryIdNamespace:
3041 case TypeTableEntryIdBlock:
3042 case TypeTableEntryIdUnreachable:
3043 case TypeTableEntryIdVar:
3044 add_node_error(ira->codegen, un_op_instruction->base.source_node,
3045 buf_sprintf("unable to get address of type '%s'", buf_ptr(&child_type->name)));
3046 // TODO if type decl, add note pointing to type decl declaration
3047 return ira->codegen->builtin_types.entry_invalid;
3048 case TypeTableEntryIdMetaType:
3049 {
3050 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base,
3051 value->static_value.depends_on_compile_var);
3052 out_val->data.x_type = get_pointer_to_type(ira->codegen, child_type, is_const);
3053 return ira->codegen->builtin_types.entry_type;
3054 }
3055 case TypeTableEntryIdVoid:
3056 case TypeTableEntryIdBool:
3057 case TypeTableEntryIdInt:
3058 case TypeTableEntryIdFloat:
3059 case TypeTableEntryIdPointer:
3060 case TypeTableEntryIdArray:
3061 case TypeTableEntryIdStruct:
3062 case TypeTableEntryIdMaybe:
3063 case TypeTableEntryIdErrorUnion:
3064 case TypeTableEntryIdPureError:
3065 case TypeTableEntryIdEnum:
3066 case TypeTableEntryIdUnion:
3067 case TypeTableEntryIdFn:
3068 case TypeTableEntryIdGenericFn:
3069 {
3070 zig_panic("TODO address of operation");
3071 break;
3072 }
3073 }
3074 zig_unreachable();
3075}
3076
30163077static TypeTableEntry *ir_analyze_instruction_un_op(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {
30173078 IrUnOp op_id = un_op_instruction->op_id;
30183079 switch (op_id) {
......@@ -3085,37 +3146,7 @@ static TypeTableEntry *ir_analyze_instruction_un_op(IrAnalyze *ira, IrInstructio
30853146 //}
30863147 case IrUnOpAddressOf:
30873148 case IrUnOpConstAddressOf:
3088 zig_panic("TODO analyze PrefixOpAddressOf and PrefixOpConstAddressOf");
3089 //{
3090 // bool is_const = (prefix_op == PrefixOpConstAddressOf);
3091
3092 // TypeTableEntry *child_type = analyze_lvalue(g, import, context,
3093 // *expr_node, LValPurposeAddressOf, is_const);
3094
3095 // if (child_type->id == TypeTableEntryIdInvalid) {
3096 // return g->builtin_types.entry_invalid;
3097 // } else if (child_type->id == TypeTableEntryIdMetaType) {
3098 // TypeTableEntry *meta_type = analyze_type_expr_pointer_only(g, import, context,
3099 // *expr_node, true);
3100 // if (meta_type->id == TypeTableEntryIdInvalid) {
3101 // return g->builtin_types.entry_invalid;
3102 // } else if (meta_type->id == TypeTableEntryIdUnreachable) {
3103 // add_node_error(g, node, buf_create_from_str("pointer to unreachable not allowed"));
3104 // return g->builtin_types.entry_invalid;
3105 // } else {
3106 // return resolve_expr_const_val_as_type(g, node,
3107 // get_pointer_to_type(g, meta_type, is_const), false);
3108 // }
3109 // } else if (child_type->id == TypeTableEntryIdNumLitInt ||
3110 // child_type->id == TypeTableEntryIdNumLitFloat)
3111 // {
3112 // add_node_error(g, *expr_node,
3113 // buf_sprintf("unable to get address of type '%s'", buf_ptr(&child_type->name)));
3114 // return g->builtin_types.entry_invalid;
3115 // } else {
3116 // return get_pointer_to_type(g, child_type, is_const);
3117 // }
3118 //}
3149 return ir_analyze_unary_address_of(ira, un_op_instruction);
31193150 case IrUnOpDereference:
31203151 zig_panic("TODO remove this IrUnOp item");
31213152 case IrUnOpMaybe: