summary refs log blame commit diff stats
path: root/tests/pragmas/tdeprecated.nim
blob: a5d07f7275771a3114058579c6adff69a040f296 (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                                         
# bug #6436
proc foo(size: int, T: typedesc): seq[T]  {.deprecated.}=
  result = newSeq[T](size)

proc foo[T](size: int): seq[T]=
  result = newSeq[T](size)

let bar = foo[int](3) # Warning foo is deprecated

doAssert bar == @[0, 0, 0]
#n288'>288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
The AST in Nimrod
=================
This section describes how the AST is modelled with Nimrod's type system.
The AST consists of nodes (``PNimrodNode``) with a variable number of
children. Each node has a field named ``kind`` which describes what the node
contains:

.. code-block:: nimrod

  type
    TNimrodNodeKind = enum ## kind of a node; only explanatory
      nnkNone,             ## invalid node kind
      nnkEmpty,            ## empty node
      nnkIdent,            ## node contains an identifier
      nnkIntLit,           ## node contains an int literal (example: 10)
      nnkStrLit,           ## node contains a string literal (example: "abc")
      nnkNilLit,           ## node contains a nil literal (example: nil)
      nnkCaseStmt,         ## node represents a case statement
      ...                  ## many more

    PNimrodNode = ref TNimrodNode
    TNimrodNode {.final.} = object
      case kind: TNimrodNodeKind       ## the node's kind
      of nnkNone, nnkEmpty, nnkNilLit:
        nil                            ## node contains no additional fields
      of nnkCharLit..nnkInt64Lit:
        intVal: biggestInt             ## the int literal
      of nnkFloatLit..nnkFloat64Lit:
        floatVal: biggestFloat         ## the float literal
      of nnkStrLit..nnkTripleStrLit:
        strVal: string                 ## the string literal
      of nnkIdent:
        ident: TNimrodIdent            ## the identifier
      of nnkSym:
        symbol: PNimrodSymbol          ## the symbol (after symbol lookup phase)
      else:
        sons: seq[PNimrodNode]         ## the node's sons (or children)

For the ``PNimrodNode`` type, the ``[]`` operator has been overloaded:
``n[i]`` is ``n``'s ``i``-th child.

To specify the AST for the different Nimrod constructs, the notation
``nodekind(son1, son2, ...)`` or ``nodekind(value)`` or 
``nodekind(field=value)`` is used.

Some child may be missing. Then it is ``nil``. A nil child is equivalent to 
a node of kind ``nnkEmpty``. This is more or less a (useful) artifact from 
the implementation. (In the compiler's implementation ``nil`` is of course 
not the same as ``nnkEmpty``!)


Leaf nodes/Atoms
================
A leaf of the AST often corresponds to a terminal symbol in the concrete 
syntax.

-----------------                ---------------------------------------------
Nimrod expression                corresponding AST
-----------------                ---------------------------------------------
``42``                           ``nnkIntLit(intVal = 42)``
``42'i8``                        ``nnkInt8Lit(intVal = 42)``
``42'i16``                       ``nnkInt16Lit(intVal = 42)``
``42'i32``                       ``nnkInt32Lit(intVal = 42)``
``42'i64``                       ``nnkInt64Lit(intVal = 42)``
``42.0``                         ``nnkFloatLit(floatVal = 42.0)``
``42.0'f32``                     ``nnkFloat32Lit(floatVal = 42.0)``
``42.0'f64``                     ``nnkFloat64Lit(floatVal = 42.0)``
``"abc"``                        ``nnkStrLit(strVal = "abc")``
``r"abc"``                       ``nnkRStrLit(strVal = "abc")``
``"""abc"""``                    ``nnkTripleStrLit(strVal = "abc")``
``' '``                          ``nnkCharLit(intVal = 32)``
``nil``                          ``nnkNilLit()``
``myIdentifier``                 ``nnkIdent(ident = !"myIdentifier")``
``myIdentifier``                 after lookup pass: ``nnkSym(symbol = ...)``
-----------------                ---------------------------------------------

Identifiers are ``nnkIdent`` nodes. After the name lookup pass these nodes
get transferred into ``nnkSym`` nodes. However, a macro receives an AST that
has not been checked for semantics and thus the identifiers have not been
looked up. Macros should deal with ``nnkIdent`` nodes and do not need to deal
with ``nnkSym`` nodes.


Calls/expressions
=================

Command call
------------

Concrete syntax:

.. code-block:: nimrod
  echo "abc", "xyz"

AST:

.. code-block:: nimrod
  nnkCommand(nnkIdent(!"echo"), nnkStrLit("abc"), nnkStrLit("xyz"))


Call with ``()``
----------------

Concrete syntax:

.. code-block:: nimrod
  echo("abc", "xyz")

AST:

.. code-block:: nimrod
  nnkCall(nnkIdent(!"echo"), nnkStrLit("abc"), nnkStrLit("xyz"))


Infix operator call
-------------------

Concrete syntax:

.. code-block:: nimrod
  "abc" & "xyz"

AST:

.. code-block:: nimrod
  nnkInfix(nnkIdent(!"&"), nnkStrLit("abc"), nnkStrLit("xyz"))


Prefix operator call
--------------------

Concrete syntax:

.. code-block:: nimrod
  ? "xyz"

AST:

.. code-block:: nimrod
  nnkPrefix(nnkIdent(!"?"), nnkStrLit("abc"))


Postfix operator call
---------------------

**Note:** There are no postfix operators in Nimrod. However, the 
``nnkPostfix`` node is used for the *asterisk export marker* ``*``:

Concrete syntax:

.. code-block:: nimrod
  identifier*

AST:

.. code-block:: nimrod
  nnkPostfix(nnkIdent(!"*"), nnkIdent(!"identifier"))


Call with named arguments
-------------------------

Concrete syntax:

.. code-block:: nimrod
  writeln(file=stdout, "hallo")

AST:

.. code-block:: nimrod
  nnkCall(nnkIdent(!"writeln"), 
          nnkExprEqExpr(nnkIdent(!"file"), nnkIdent(!"stdout")), 
          nnkStrLit("hallo"))


Dereference operator ``^``
--------------------------

Concrete syntax:

.. code-block:: nimrod
  x^

AST:

.. code-block:: nimrod
  nnkDerefExpr(nnkIdent(!"x"))


Addr operator
-------------

Concrete syntax:

.. code-block:: nimrod
  addr(x)

AST:

.. code-block:: nimrod
  nnkAddr(nnkIdent(!"x"))


Cast operator
-------------

Concrete syntax:

.. code-block:: nimrod
  cast[T](x)

AST:

.. code-block:: nimrod
  nnkCast(nnkIdent(!"T"), nnkIdent(!"x"))


Object access operator ``.``
----------------------------

Concrete syntax:

.. code-block:: nimrod
  x.y

AST:

.. code-block:: nimrod
  nnkDotExpr(nnkIdent(!"x"), nnkIdent(!"y"))


Array access operator ``[]``
----------------------------

Concrete syntax:

.. code-block:: nimrod
  x[y]

AST:

.. code-block:: nimrod
  nnkBracketExpr(nnkIdent(!"x"), nnkIdent(!"y"))


Parentheses
-----------

Parentheses for affecting operator precedence or tuple construction 
are built with the ``nnkPar`` node.

Concrete syntax:

.. code-block:: nimrod
  (1, 2, (3))

AST:

.. code-block:: nimrod
  nnkPar(nnkIntLit(1), nnkIntLit(2), nnkPar(nnkIntLit(3)))
  
  
Curly braces
------------

Curly braces are used as the set constructor. 

Concrete syntax:

.. code-block:: nimrod
  {1, 2, 3}

AST:

.. code-block:: nimrod
  nnkCurly(nnkIntLit(1), nnkIntLit(2), nnkIntLit(3))


Brackets
--------

Brackets are used as the array constructor.  

Concrete syntax:

.. code-block:: nimrod
  [1, 2, 3]

AST:

.. code-block:: nimrod
  nnkBracket(nnkIntLit(1), nnkIntLit(2), nnkIntLit(3))


Ranges
------

Ranges occur in set constructors, case statement branches or array slices.

Concrete syntax:

.. code-block:: nimrod
  1..3

AST:

.. code-block:: nimrod
  nnkRange(nnkIntLit(1), nnkIntLit(3))


If expression
-------------

The representation of the if expression is subtle, but easy to traverse.

Concrete syntax:

.. code-block:: nimrod
  if cond1: expr1 elif cond2: expr2 else: expr3

AST:

.. code-block:: nimrod
  nnkIfExpr(
    nnkElifExpr(cond1, expr1),
    nnkElifExpr(cond2, expr2),
    nnkElseExpr(expr3)
  )


Statements
==========

If statement
------------

The representation of the if statement is subtle, but easy to traverse. If
there is no ``else`` branch, no ``nnkElse`` child exists.

Concrete syntax:

.. code-block:: nimrod
  if cond1: 
    stmt1
  elif cond2:
    stmt2
  elif cond3:
    stmt3
  else:
    stmt4

AST:

.. code-block:: nimrod
  nnkIfStmt(
    nnkElifBranch(cond1, stmt1),
    nnkElifBranch(cond2, stmt2),
    nnkElifBranch(cond3, stmt3),
    nnkElse(stmt4)
  )


When statement
--------------

Like the ``if`` statement, but the root has the kind ``nnkWhenStmt``.
  

Assignment
----------

Concrete syntax:

.. code-block:: nimrod
  x = 42

AST:

.. code-block:: nimrod
  nnkAsgn(nnkIdent(!"x"), nnkIntLit(42))


Statement list
--------------

Concrete syntax:

.. code-block:: nimrod
  stmt1
  stmt2
  stmt3

AST:

.. code-block:: nimrod
  nnkStmtList(stmt1, stmt2, stmt3)

  
Case statement
--------------

Concrete syntax:

.. code-block:: nimrod
  case expr1
  of expr2, expr3..expr4: 
    stmt1
  of expr5:
    stmt2
  elif cond1:
    stmt3
  else:
    stmt4

AST:

.. code-block:: nimrod
  nnkCaseStmt(
    expr1,
    nnkOfBranch(expr2, nnkRange(expr3, expr4), stmt1),
    nnkOfBranch(expr5, stmt2),
    nnkElifBranch(cond1, stmt3),
    nnkElse(stmt4)
  )

The ``nnkElifBranch`` and ``nnkElse`` parts may be missing.


While statement
---------------

Concrete syntax:

.. code-block:: nimrod
  while expr1:
    stmt1

AST:

.. code-block:: nimrod
  nnkWhileStmt(expr1, stmt1)


For statement
-------------

Concrete syntax:

.. code-block:: nimrod
  for ident1, ident2 in expr1:
    stmt1

AST:

.. code-block:: nimrod
  nnkForStmt(ident1, ident2, expr1, stmt1)


Try statement
-------------

Concrete syntax:

.. code-block:: nimrod
  try:
    stmt1
  except e1, e2: 
    stmt2
  except e3:
    stmt3
  except: 
    stmt4
  finally:
    stmt5

AST:

.. code-block:: nimrod
  nnkTryStmt(
    stmt1, 
    nnkExceptBranch(e1, e2, stmt2), 
    nnkExceptBranch(e3, stmt3), 
    nnkExceptBranch(stmt4),
    nnkFinally(stmt5)
  )


Return statement
----------------

Concrete syntax:

.. code-block:: nimrod
  return expr1

AST:

.. code-block:: nimrod
  nnkReturnStmt(expr1)


Yield statement
---------------

Like ``return``, but with ``nnkYieldStmt`` kind.


Discard statement
-----------------

Like ``return``, but with ``nnkDiscardStmt`` kind.


Continue statement
------------------

Concrete syntax:

.. code-block:: nimrod
  continue

AST:

.. code-block:: nimrod
  nnkContinueStmt()

Var section
-----------

To be written.


Const section
-------------

To be written.


Type section
------------

To be written.


Procedure declaration
---------------------

To be written.


Iterator declaration
--------------------

To be written.


Template declaration
--------------------

To be written.


Macro declaration
-----------------

To be written.


Special node kinds
==================

There are several node kinds that are used for semantic checking or code 
generation. These are accessible from this module, but should not be used.
Other node kinds are especially designed to make AST manipulations easier.
These are explained here. 

To be written.