summary refs log tree commit diff stats
path: root/lib/core
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2018-09-17 18:19:44 +0300
committerzah <zahary@gmail.com>2018-09-17 22:17:39 +0300
commit97fefa3f50a7a594d57b25dcda4ef36f88df8364 (patch)
tree31e6f1a4d09ee87873615b975eb1439f63157821 /lib/core
parent95072fbcc3379a3dfa2356f6c1ed1bfad5b6a656 (diff)
downloadNim-97fefa3f50a7a594d57b25dcda4ef36f88df8364.tar.gz
some simple sub-expression elimination not applied by the previos author
Diffstat (limited to 'lib/core')
-rw-r--r--lib/core/macros.nim24
1 files changed, 13 insertions, 11 deletions
diff --git a/lib/core/macros.nim b/lib/core/macros.nim
index 0ac46b54d..e7ef89551 100644
--- a/lib/core/macros.nim
+++ b/lib/core/macros.nim
@@ -1325,13 +1325,14 @@ proc customPragmaNode(n: NimNode): NimNode =
     var typDef = getImpl(getTypeInst(if n.kind == nnkCheckedFieldExpr or n[0].kind == nnkHiddenDeref: n[0][0] else: n[0]))
     while typDef != nil:
       typDef.expectKind(nnkTypeDef)
-      typDef[2].expectKind({nnkRefTy, nnkPtrTy, nnkObjectTy})
-      let isRef = typDef[2].kind in {nnkRefTy, nnkPtrTy}
-      if isRef and typDef[2][0].kind in {nnkSym, nnkBracketExpr}: # defines ref type for another object(e.g. X = ref X)
-        typDef = getImpl(typDef[2][0])
+      let typ = typDef[2]
+      typ.expectKind({nnkRefTy, nnkPtrTy, nnkObjectTy})
+      let isRef = typ.kind in {nnkRefTy, nnkPtrTy}
+      if isRef and typ[0].kind in {nnkSym, nnkBracketExpr}: # defines ref type for another object(e.g. X = ref X)
+        typDef = getImpl(typ[0])
       else: # object definition, maybe an object directly defined as a ref type
         let
-          obj = (if isRef: typDef[2][0] else: typDef[2])
+          obj = (if isRef: typ[0] else: typ)
         var identDefsStack = newSeq[NimNode](obj[2].len)
         for i in 0..<identDefsStack.len: identDefsStack[i] = obj[2][i]
         while identDefsStack.len > 0:
@@ -1339,13 +1340,14 @@ proc customPragmaNode(n: NimNode): NimNode =
           if identDefs.kind == nnkRecCase:
             identDefsStack.add(identDefs[0])
             for i in 1..<identDefs.len:
+              let varNode = identDefs[i]
               # if it is and empty branch, skip
-              if identDefs[i][0].kind == nnkNilLit: continue
-              if identDefs[i][1].kind == nnkIdentDefs:
-                identDefsStack.add(identDefs[i][1])
+              if varNode[0].kind == nnkNilLit: continue
+              if varNode[1].kind == nnkIdentDefs:
+                identDefsStack.add(varNode[1])
               else: # nnkRecList
-                for j in 0..<identDefs[i][1].len:
-                  identDefsStack.add(identDefs[i][1][j])
+                for j in 0 ..< varNode[1].len:
+                  identDefsStack.add(varNode[1][j])
 
           else:
             for i in 0 .. identDefs.len - 3:
@@ -1356,7 +1358,7 @@ proc customPragmaNode(n: NimNode): NimNode =
                   # This is a public field. We are skipping the postfix *
                   varName = varName[1]
                 if eqIdent(varName.strVal, name):
-                  return identDefs[i][1]
+                  return varNode[1]
 
         if obj[1].kind == nnkOfInherit: # explore the parent object
           typDef = getImpl(obj[1][0])