authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-20 15:52:24-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-21 10:46:47-07:00
log1ac28eed839663a7d12bbbf131596b6abd8b0744
treedb29d85aae9025ad2e6c1606636500d33e76ed53
parent1cfe43d563bd1418706d64733474b43c823d677b

stage2 AST: rename OptionalUnwrap to OrElse

preparing to flatten suffix operations AST

4 files changed, 134 insertions(+), 124 deletions(-)

lib/std/zig/ast.zig+2-2
...@@ -453,7 +453,7 @@ pub const Node = struct {...@@ -453,7 +453,7 @@ pub const Node = struct {
453 Range,453 Range,
454 Sub,454 Sub,
455 SubWrap,455 SubWrap,
456 UnwrapOptional,456 OrElse,
457457
458 // SimplePrefixOp458 // SimplePrefixOp
459 AddressOf,459 AddressOf,
...@@ -582,7 +582,7 @@ pub const Node = struct {...@@ -582,7 +582,7 @@ pub const Node = struct {
582 .Range,582 .Range,
583 .Sub,583 .Sub,
584 .SubWrap,584 .SubWrap,
585 .UnwrapOptional,585 .OrElse,
586 => SimpleInfixOp,586 => SimpleInfixOp,
587587
588 .AddressOf,588 .AddressOf,
lib/std/zig/parse.zig+3-3
...@@ -1486,7 +1486,7 @@ const Parser = struct {...@@ -1486,7 +1486,7 @@ const Parser = struct {
1486 .Range,1486 .Range,
1487 .Sub,1487 .Sub,
1488 .SubWrap,1488 .SubWrap,
1489 .UnwrapOptional,1489 .OrElse,
1490 => node.cast(Node.SimpleInfixOp).?.lhs = res,1490 => node.cast(Node.SimpleInfixOp).?.lhs = res,
14911491
1492 else => unreachable,1492 else => unreachable,
...@@ -1563,7 +1563,7 @@ const Parser = struct {...@@ -1563,7 +1563,7 @@ const Parser = struct {
1563 .Range,1563 .Range,
1564 .Sub,1564 .Sub,
1565 .SubWrap,1565 .SubWrap,
1566 .UnwrapOptional,1566 .OrElse,
1567 => node.cast(Node.SimpleInfixOp).?.lhs = res,1567 => node.cast(Node.SimpleInfixOp).?.lhs = res,
1568 else => unreachable,1568 else => unreachable,
1569 }1569 }
...@@ -2425,7 +2425,7 @@ const Parser = struct {...@@ -2425,7 +2425,7 @@ const Parser = struct {
2425 .Ampersand => .BitAnd,2425 .Ampersand => .BitAnd,
2426 .Caret => .BitXor,2426 .Caret => .BitXor,
2427 .Pipe => .BitOr,2427 .Pipe => .BitOr,
2428 .Keyword_orelse => .UnwrapOptional,2428 .Keyword_orelse => .OrElse,
2429 .Keyword_catch => {2429 .Keyword_catch => {
2430 const payload = try p.parsePayload();2430 const payload = try p.parsePayload();
2431 const node = try p.arena.allocator.create(Node.Catch);2431 const node = try p.arena.allocator.create(Node.Catch);
lib/std/zig/render.zig+2-2
...@@ -503,7 +503,7 @@ fn renderExpression(...@@ -503,7 +503,7 @@ fn renderExpression(
503 .Range,503 .Range,
504 .Sub,504 .Sub,
505 .SubWrap,505 .SubWrap,
506 .UnwrapOptional,506 .OrElse,
507 => {507 => {
508 const infix_op_node = @fieldParentPtr(ast.Node.SimpleInfixOp, "base", base);508 const infix_op_node = @fieldParentPtr(ast.Node.SimpleInfixOp, "base", base);
509509
...@@ -2656,7 +2656,7 @@ fn nodeCausesSliceOpSpace(base: *ast.Node) bool {...@@ -2656,7 +2656,7 @@ fn nodeCausesSliceOpSpace(base: *ast.Node) bool {
2656 .Range,2656 .Range,
2657 .Sub,2657 .Sub,
2658 .SubWrap,2658 .SubWrap,
2659 .UnwrapOptional,2659 .OrElse,
2660 => true,2660 => true,
26612661
2662 else => false,2662 else => false,
src-self-hosted/astgen.zig+127-117
...@@ -88,7 +88,7 @@ fn varDecl(mod: *Module, scope: *Scope, node: *ast.Node.VarDecl) InnerError!Scop...@@ -88,7 +88,7 @@ fn varDecl(mod: *Module, scope: *Scope, node: *ast.Node.VarDecl) InnerError!Scop
88 // or an rvalue as a result location. If it is an rvalue, we can use the instruction as88 // or an rvalue as a result location. If it is an rvalue, we can use the instruction as
89 // the variable, no memory location needed.89 // the variable, no memory location needed.
90 const init_node = node.getTrailer("init_node").?;90 const init_node = node.getTrailer("init_node").?;
91 if (nodeNeedsMemoryLocation(init_node)) {91 if (nodeMayNeedMemoryLocation(init_node)) {
92 return mod.failNode(scope, init_node, "TODO implement result locations", .{});92 return mod.failNode(scope, init_node, "TODO implement result locations", .{});
93 }93 }
94 const init_inst = try expr(mod, scope, init_node);94 const init_inst = try expr(mod, scope, init_node);
...@@ -579,120 +579,130 @@ fn getSimplePrimitiveValue(name: []const u8) ?TypedValue {...@@ -579,120 +579,130 @@ fn getSimplePrimitiveValue(name: []const u8) ?TypedValue {
579 return null;579 return null;
580}580}
581581
582fn nodeNeedsMemoryLocation(node: *ast.Node) bool {582fn nodeMayNeedMemoryLocation(start_node: *ast.Node) bool {
583 return switch (node.tag) {583 var node = start_node;
584 .Root,584 while (true) {
585 .Use,585 switch (node.tag) {
586 .TestDecl,586 .Root,
587 .DocComment,587 .Use,
588 .SwitchCase,588 .TestDecl,
589 .SwitchElse,589 .DocComment,
590 .Else,590 .SwitchCase,
591 .Payload,591 .SwitchElse,
592 .PointerPayload,592 .Else,
593 .PointerIndexPayload,593 .Payload,
594 .ContainerField,594 .PointerPayload,
595 .ErrorTag,595 .PointerIndexPayload,
596 .FieldInitializer,596 .ContainerField,
597 => unreachable,597 .ErrorTag,
598598 .FieldInitializer,
599 .ControlFlowExpression,599 => unreachable,
600 .BitNot,600
601 .BoolNot,601 .ControlFlowExpression,
602 .VarDecl,602 .BitNot,
603 .Defer,603 .BoolNot,
604 .AddressOf,604 .VarDecl,
605 .OptionalType,605 .Defer,
606 .Negation,606 .AddressOf,
607 .NegationWrap,607 .OptionalType,
608 .Resume,608 .Negation,
609 .ArrayType,609 .NegationWrap,
610 .ArrayTypeSentinel,610 .Resume,
611 .PtrType,611 .ArrayType,
612 .SliceType,612 .ArrayTypeSentinel,
613 .Suspend,613 .PtrType,
614 .AnyType,614 .SliceType,
615 .ErrorType,615 .Suspend,
616 .FnProto,616 .AnyType,
617 .AnyFrameType,617 .ErrorType,
618 .IntegerLiteral,618 .FnProto,
619 .FloatLiteral,619 .AnyFrameType,
620 .EnumLiteral,620 .IntegerLiteral,
621 .StringLiteral,621 .FloatLiteral,
622 .MultilineStringLiteral,622 .EnumLiteral,
623 .CharLiteral,623 .StringLiteral,
624 .BoolLiteral,624 .MultilineStringLiteral,
625 .NullLiteral,625 .CharLiteral,
626 .UndefinedLiteral,626 .BoolLiteral,
627 .Unreachable,627 .NullLiteral,
628 .Identifier,628 .UndefinedLiteral,
629 .ErrorSetDecl,629 .Unreachable,
630 .ContainerDecl,630 .Identifier,
631 .Asm,631 .ErrorSetDecl,
632 .Add,632 .ContainerDecl,
633 .AddWrap,633 .Asm,
634 .ArrayCat,634 .Add,
635 .ArrayMult,635 .AddWrap,
636 .Assign,636 .ArrayCat,
637 .AssignBitAnd,637 .ArrayMult,
638 .AssignBitOr,638 .Assign,
639 .AssignBitShiftLeft,639 .AssignBitAnd,
640 .AssignBitShiftRight,640 .AssignBitOr,
641 .AssignBitXor,641 .AssignBitShiftLeft,
642 .AssignDiv,642 .AssignBitShiftRight,
643 .AssignSub,643 .AssignBitXor,
644 .AssignSubWrap,644 .AssignDiv,
645 .AssignMod,645 .AssignSub,
646 .AssignAdd,646 .AssignSubWrap,
647 .AssignAddWrap,647 .AssignMod,
648 .AssignMul,648 .AssignAdd,
649 .AssignMulWrap,649 .AssignAddWrap,
650 .BangEqual,650 .AssignMul,
651 .BitAnd,651 .AssignMulWrap,
652 .BitOr,652 .BangEqual,
653 .BitShiftLeft,653 .BitAnd,
654 .BitShiftRight,654 .BitOr,
655 .BitXor,655 .BitShiftLeft,
656 .BoolAnd,656 .BitShiftRight,
657 .BoolOr,657 .BitXor,
658 .Div,658 .BoolAnd,
659 .EqualEqual,659 .BoolOr,
660 .ErrorUnion,660 .Div,
661 .GreaterOrEqual,661 .EqualEqual,
662 .GreaterThan,662 .ErrorUnion,
663 .LessOrEqual,663 .GreaterOrEqual,
664 .LessThan,664 .GreaterThan,
665 .MergeErrorSets,665 .LessOrEqual,
666 .Mod,666 .LessThan,
667 .Mul,667 .MergeErrorSets,
668 .MulWrap,668 .Mod,
669 .Range,669 .Mul,
670 .Period,670 .MulWrap,
671 .Sub,671 .Range,
672 .SubWrap,672 .Period,
673 => false,673 .Sub,
674674 .SubWrap,
675 .ArrayInitializer,675 => return false,
676 .ArrayInitializerDot,676
677 .StructInitializer,677 // Forward the question to a sub-expression.
678 .StructInitializerDot,678 .GroupedExpression => node = node.castTag(.GroupedExpression).?.expr,
679 => true,679 .Try => node = node.castTag(.Try).?.rhs,
680680 .Await => node = node.castTag(.Await).?.rhs,
681 .GroupedExpression => nodeNeedsMemoryLocation(node.castTag(.GroupedExpression).?.expr),681 .Catch => node = node.castTag(.Catch).?.rhs,
682682 .OrElse => node = node.castTag(.OrElse).?.rhs,
683 .UnwrapOptional => @panic("TODO nodeNeedsMemoryLocation for UnwrapOptional"),683 .Comptime => node = node.castTag(.Comptime).?.expr,
684 .Catch => @panic("TODO nodeNeedsMemoryLocation for Catch"),684 .Nosuspend => node = node.castTag(.Nosuspend).?.expr,
685 .Await => @panic("TODO nodeNeedsMemoryLocation for Await"),685
686 .Try => @panic("TODO nodeNeedsMemoryLocation for Try"),686 // True because these are exactly the expressions we need memory locations for.
687 .If => @panic("TODO nodeNeedsMemoryLocation for If"),687 .ArrayInitializer,
688 .SuffixOp => @panic("TODO nodeNeedsMemoryLocation for SuffixOp"),688 .ArrayInitializerDot,
689 .Call => @panic("TODO nodeNeedsMemoryLocation for Call"),689 .StructInitializer,
690 .Switch => @panic("TODO nodeNeedsMemoryLocation for Switch"),690 .StructInitializerDot,
691 .While => @panic("TODO nodeNeedsMemoryLocation for While"),691 => return true,
692 .For => @panic("TODO nodeNeedsMemoryLocation for For"),692
693 .BuiltinCall => @panic("TODO nodeNeedsMemoryLocation for BuiltinCall"),693 // True because depending on comptime conditions, sub-expressions
694 .Comptime => @panic("TODO nodeNeedsMemoryLocation for Comptime"),694 // may be the kind that need memory locations.
695 .Nosuspend => @panic("TODO nodeNeedsMemoryLocation for Nosuspend"),695 .While,
696 .Block => @panic("TODO nodeNeedsMemoryLocation for Block"),696 .For,
697 };697 .Switch,
698 .Call,
699 .BuiltinCall, // TODO some of these can return false
700 .SuffixOp, // TODO this should be split up
701 => return true,
702
703 // Depending on AST properties, they may need memory locations.
704 .If => return node.castTag(.If).?.@"else" != null,
705 .Block => return node.castTag(.Block).?.label != null,
706 }
707 }
698}708}