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


                               
                                         





                                                                
                       













                                                                   
                                


                                                    
















                                                                  








                                                  




                                                              
                                             


                                                                 
                                                            
                  
 
#
#
#           The Nimrod Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This include file implements the semantic checking for magics.
# included from sem.nim

proc semIsPartOf(c: PContext, n: PNode, flags: TExprFlags): PNode =
  var r = isPartOf(n[1], n[2])
  result = newIntNodeT(ord(r), n)
  
proc semSlurp(c: PContext, n: PNode, flags: TExprFlags): PNode = 
  assert sonsLen(n) == 2
  var a = expectStringArg(c, n, 0)
  try:
    var filename = a.strVal.FindFile
    var content = readFile(filename)
    result = newStrNode(nkStrLit, content)
    result.typ = getSysType(tyString)
    result.info = n.info
    c.slurpedFiles.add(a.strVal)
  except EIO:
    GlobalError(a.info, errCannotOpenFile, a.strVal)

proc expectIntLit(c: PContext, n: PNode): int =
  let x = c.semConstExpr(c, n)
  case x.kind
  of nkIntLit..nkInt64Lit: result = int(x.intVal)
  else: GlobalError(n.info, errIntLiteralExpected)

proc semInstantiationInfo(c: PContext, n: PNode): PNode =
  result = newNodeIT(nkPar, n.info, n.typ)
  let idx = expectIntLit(c, n.sons[1])
  let info = getInfoContext(idx)
  var filename = newNodeIT(nkStrLit, n.info, getSysType(tyString))
  filename.strVal = ToFilename(info)
  var line = newNodeIT(nkIntLit, n.info, getSysType(tyInt))
  line.intVal = ToLinenumber(info)
  result.add(filename)
  result.add(line)

proc semTypeTraits(c: PContext, n: PNode): PNode =
  checkMinSonsLen(n, 2)
  internalAssert n.sons[1].kind == nkSym
  if n.sons[1].sym.kind == skType:
    result = evalTypeTrait(n, GetCurrOwner())
  else:
    # pass unmodified to evals
    result = n

proc magicsAfterOverloadResolution(c: PContext, n: PNode, 
                                   flags: TExprFlags): PNode =
  case n[0].sym.magic
  of mSlurp: result = semSlurp(c, n, flags)
  of mIsPartOf: result = semIsPartOf(c, n, flags)
  of mTypeTrait: result = semTypeTraits(c, n)
  of mAstToStr:
    result = newStrNodeT(renderTree(n[1], {renderNoComments}), n)
    result.typ = getSysType(tyString)
  of mInstantiationInfo: result = semInstantiationInfo(c, n)
  else: result = n