Repository: Konstantin8105/c4go
Pull request #343 (2019-03-18 19:52:47)

operation |= for enum

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/343?src=pr&el=h1) Report
> Merging [#343](https://codecov.io/gh/Konstantin8105/c4go/pull/343?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/10bedeeda0c75930d87573b5c66c5353657beae1?src=pr&el=desc) will **increase** coverage by `0.05%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/Konstantin8105/c4go/pull/343/graphs/tree.svg?width=650&token=esH2rHXlZ2&height=150&src=pr)](https://codecov.io/gh/Konstantin8105/c4go/pull/343?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #343      +/-   ##
==========================================
+ Coverage   86.32%   86.38%   +0.05%     
==========================================
  Files         185      185              
  Lines       10459    10473      +14     
==========================================
+ Hits         9029     9047      +18     
+ Misses       1021     1017       -4     
  Partials      409      409
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/343?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/binary.go](https://codecov.io/gh/Konstantin8105/c4go/pull/343/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9iaW5hcnkuZ28=) | `75.53% <100%> (+1.99%)` | :arrow_up: |
| [transpiler/enum.go](https://codecov.io/gh/Konstantin8105/c4go/pull/343/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9lbnVtLmdv) | `89.05% <100%> (-0.16%)` | :arrow_down: |
| [ast/ast.go](https://codecov.io/gh/Konstantin8105/c4go/pull/343/diff?src=pr&el=tree#diff-YXN0L2FzdC5nbw==) | `98.64% <0%> (-0.3%)` | :arrow_down: |
| [ast/position.go](https://codecov.io/gh/Konstantin8105/c4go/pull/343/diff?src=pr&el=tree#diff-YXN0L3Bvc2l0aW9uLmdv) | `93.47% <0%> (-0.26%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/343?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/343?src=pr&el=footer). Last update [10bedee...ef45873](https://codecov.io/gh/Konstantin8105/c4go/pull/343?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/transpiler/binary.go b/transpiler/binary.go
index 6bcbba4..dbba7f4 100644
--- a/transpiler/binary.go
+++ b/transpiler/binary.go
@@ -398,6 +398,13 @@ func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program, exprIsSt
 		operator == token.MUL_ASSIGN || // *=
 		operator == token.QUO_ASSIGN || // /=
 		operator == token.REM_ASSIGN || // %=
+
+		operator == token.AND_ASSIGN || // &=
+		operator == token.OR_ASSIGN || // |=
+		operator == token.XOR_ASSIGN || // ^=
+		operator == token.SHL_ASSIGN || // <<=
+		operator == token.SHR_ASSIGN || // >>=
+		operator == token.AND_NOT_ASSIGN || // &^=
 		false {
 
 		if rightType == types.NullPointer && leftType == types.NullPointer {
@@ -502,14 +509,6 @@ func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program, exprIsSt
 		p.AddMessage(p.GenerateWarningMessage(err, n))
 	}
 
-	// Enum casting
-	if operator != token.ASSIGN && strings.Contains(leftType, "enum") {
-		left, err = types.CastExpr(p, left, leftType, "int")
-		if err != nil {
-			p.AddMessage(p.GenerateWarningMessage(err, n))
-		}
-	}
-
 	// Enum casting
 	if operator != token.ASSIGN && strings.Contains(rightType, "enum") {
 		right, err = types.CastExpr(p, right, rightType, "int")
diff --git a/transpiler/enum.go b/transpiler/enum.go
index 033ba65..68d32b0 100644
--- a/transpiler/enum.go
+++ b/transpiler/enum.go
@@ -61,12 +61,10 @@ func transpileEnumDecl(p *program.Program, n *ast.EnumDecl) (
 		Tok: token.TYPE,
 		Specs: []goast.Spec{
 			&goast.TypeSpec{
-				Name: &goast.Ident{
-					Name: n.Name,
-					Obj:  goast.NewObj(goast.Typ, n.Name),
-				},
+				Name:   util.NewIdent(n.Name),
+				Assign: 1,
 				// by defaults enum in C is INT
-				Type: util.NewTypeIdent("int"),
+				Type: util.NewTypeIdent("int32"),
 			},
 		},
 	})


Test patch:
diff --git a/tests/operators.c b/tests/operators.c
index fff2aed..eea014b 100644
--- a/tests/operators.c
+++ b/tests/operators.c
@@ -64,7 +64,7 @@ void view(int c)
 
 int main()
 {
-    plan(116);
+    plan(119);
 
     int i = 10;
     signed char j = 1;
@@ -511,6 +511,20 @@ int main()
 		view(-c);
 		is_eq(c,90);
 	}
+	diag("operation |= for enum");
+	{
+		enum Sflags {
+		  SGG = 0x01,
+		  SGP = 0x02,
+		  SGR = 0x04,
+		  SGF = 0x08
+		  } sflags = 0;
+		is_eq(sflags, 0);
+        sflags |= SGG;
+		is_eq(sflags, 1);
+        sflags |= SGR;
+		is_eq(sflags, 5);
+	}
 
     done_testing();
 }