summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2012-04-20 22:28:53 +0300
committerZahary Karadjov <zahary@gmail.com>2012-04-20 22:28:53 +0300
commit064f29621397a985e5e344a1b3d9751df92ff52d (patch)
treebf151e387e38df345ddbf31406aaeebdae1657f5
parentc1d16c5a4da3f1af6b2e9ce82df29c5397e544ca (diff)
downloadNim-064f29621397a985e5e344a1b3d9751df92ff52d.tar.gz
allow the use of built-in type constraints in type sections
-rwxr-xr-xcompiler/semtypes.nim61
1 files changed, 27 insertions, 34 deletions
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index 28ef55274..a201d1f03 100755
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -16,8 +16,13 @@ proc newOrPrevType(kind: TTypeKind, prev: PType, c: PContext): PType =
   else: 
     result = prev
     if result.kind == tyForward: result.kind = kind
-  
-proc semEnum(c: PContext, n: PNode, prev: PType): PType = 
+
+proc newConstraint(c: PContext, k: TTypeKind): PType = 
+  result = newTypeS(tyTypeClass, c)
+  result.addSon(newTypeS(k, c))
+
+proc semEnum(c: PContext, n: PNode, prev: PType): PType =
+  if n.sonsLen == 0: return newConstraint(c, tyEnum)
   var 
     counter, x: BiggestInt
     e: PSym
@@ -92,29 +97,30 @@ proc semContainer(c: PContext, n: PNode, kind: TTypeKind, kindStr: string,
     addSon(result, base)
   else: 
     GlobalError(n.info, errXExpectsOneTypeParam, kindStr)
-  
-proc semAnyRef(c: PContext, n: PNode, kind: TTypeKind, kindStr: string, 
-               prev: PType): PType = 
-  result = newOrPrevType(kind, prev, c)
-  if sonsLen(n) == 1: 
+
+proc semAnyRef(c: PContext, n: PNode, kind: TTypeKind, prev: PType): PType = 
+  if sonsLen(n) == 1:
+    result = newOrPrevType(kind, prev, c)
     var base = semTypeNode(c, n.sons[0], nil)
     addSon(result, base)
-  else: 
-    GlobalError(n.info, errXExpectsOneTypeParam, kindStr)
+  else:
+    result = newConstraint(c, kind)
   
 proc semVarType(c: PContext, n: PNode, prev: PType): PType = 
-  result = newOrPrevType(tyVar, prev, c)
   if sonsLen(n) == 1: 
+    result = newOrPrevType(tyVar, prev, c)
     var base = semTypeNode(c, n.sons[0], nil)
     if base.kind == tyVar: GlobalError(n.info, errVarVarTypeNotAllowed)
     addSon(result, base)
-  else: 
-    GlobalError(n.info, errXExpectsOneTypeParam, "var")
+  else:
+    result = newConstraint(c, tyVar)
   
 proc semDistinct(c: PContext, n: PNode, prev: PType): PType = 
-  result = newOrPrevType(tyDistinct, prev, c)
-  if sonsLen(n) == 1: addSon(result, semTypeNode(c, n.sons[0], nil))
-  else: GlobalError(n.info, errXExpectsOneTypeParam, "distinct")
+  if sonsLen(n) == 1:
+    result = newOrPrevType(tyDistinct, prev, c)
+    addSon(result, semTypeNode(c, n.sons[0], nil))
+  else:
+    result = newConstraint(c, tyDistinct)
   
 proc semRangeAux(c: PContext, n: PNode, prev: PType): PType = 
   assert IsRange(n)
@@ -197,8 +203,8 @@ proc semTypeIdent(c: PContext, n: PNode): PSym =
       GlobalError(n.info, errIdentifierExpected)
   
 proc semTuple(c: PContext, n: PNode, prev: PType): PType = 
-  var 
-    typ: PType
+  if n.sonsLen == 0: return newConstraint(c, tyTuple)
+  var typ: PType
   result = newOrPrevType(tyTuple, prev, c)
   result.n = newNodeI(nkRecList, n.info)
   var check = initIntSet()
@@ -458,7 +464,8 @@ proc skipGenericInvokation(t: PType): PType {.inline.} =
   if result.kind == tyGenericBody:
     result = lastSon(result)
 
-proc semObjectNode(c: PContext, n: PNode, prev: PType): PType = 
+proc semObjectNode(c: PContext, n: PNode, prev: PType): PType =
+  if n.sonsLen == 0: return newConstraint(c, tyObject)
   var check = initIntSet()
   var pos = 0 
   var base: PType = nil
@@ -766,8 +773,8 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
       GlobalError(n.info, errTypeExpected)
   of nkObjectTy: result = semObjectNode(c, n, prev)
   of nkTupleTy: result = semTuple(c, n, prev)
-  of nkRefTy: result = semAnyRef(c, n, tyRef, "ref", prev)
-  of nkPtrTy: result = semAnyRef(c, n, tyPtr, "ptr", prev)
+  of nkRefTy: result = semAnyRef(c, n, tyRef, prev)
+  of nkPtrTy: result = semAnyRef(c, n, tyPtr, prev)
   of nkVarTy: result = semVarType(c, n, prev)
   of nkDistinctTy: result = semDistinct(c, n, prev)
   of nkProcTy: 
@@ -828,23 +835,9 @@ proc processMagicType(c: PContext, m: PSym) =
   of mPNimrodNode: nil
   else: GlobalError(m.info, errTypeExpected)
   
-proc newConstraint(c: PContext, k: TTypeKind): PType = 
-  result = newTypeS(tyTypeClass, c)
-  result.addSon(newTypeS(k, c))
-  
 proc semGenericConstraints(c: PContext, n: PNode, result: PType) = 
   case n.kind
   of nkProcTy: result.addSon(newConstraint(c, tyProc))
-  of nkEnumTy: result.addSon(newConstraint(c, tyEnum))
-  of nkObjectTy: result.addSon(newConstraint(c, tyObject))
-  of nkTupleTy: result.addSon(newConstraint(c, tyTuple))
-  of nkDistinctTy: result.addSon(newConstraint(c, tyDistinct))
-  of nkVarTy: result.addSon(newConstraint(c, tyVar))
-  of nkPtrTy: result.addSon(newConstraint(c, tyPtr))
-  of nkRefTy: result.addSon(newConstraint(c, tyRef))
-  of nkInfix: 
-    semGenericConstraints(c, n.sons[1], result)
-    semGenericConstraints(c, n.sons[2], result)
   else:
     var x = semTypeNode(c, n, nil)
     if x.kind in StructuralEquivTypes and (
ef='#n330'>330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549