summary refs log tree commit diff stats
path: root/doc/manual/about.txt
Commit message (Expand)AuthorAgeFilesLines
* doc: Trim .txt files trailing whitespaceAdam Strzelecki2015-09-041-6/+6
* Change to hard word wrap at 80.Oscar Campbell2015-05-251-4/+5
* Change wording in some parts. Fix some typos.Oscar Campbell2015-05-251-4/+3
* Fix typosFederico Ceratto2015-02-151-2/+2
* manual split up into multiple files; documented the new concurrency systemAraq2014-10-021-0/+37
> 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 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
#
#
#           The Nimrod Compiler
#        (c) Copyright 2009 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This module implements semantic checking for pragmas

import 
  os, platform, condsyms, ast, astalgo, idents, semdata, msgs, rnimsyn, 
  wordrecg, ropes, options, strutils, lists, extccomp, math, magicsys

const 
  FirstCallConv* = wNimcall
  LastCallConv* = wNoconv

const 
  procPragmas* = {FirstCallConv..LastCallConv, wImportc, wExportc, wNodecl, 
    wMagic, wNosideEffect, wSideEffect, wNoreturn, wDynLib, wHeader, 
    wCompilerProc, wPure, wProcVar, wDeprecated, wVarargs, wCompileTime, wMerge, 
    wBorrow}
  converterPragmas* = procPragmas
  methodPragmas* = procPragmas
  macroPragmas* = {FirstCallConv..LastCallConv, wImportc, wExportc, wNodecl, 
    wMagic, wNosideEffect, wCompilerProc, wDeprecated, wTypeCheck}
  iteratorPragmas* = {FirstCallConv..LastCallConv, wNosideEffect, wSideEffect, 
    wImportc, wExportc, wNodecl, wMagic, wDeprecated, wBorrow}
  stmtPragmas* = {wChecks, wObjChecks, wFieldChecks, wRangechecks, wBoundchecks, 
    wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints, wLinedir, 
    wStacktrace, wLinetrace, wOptimization, wHint, wWarning, wError, wFatal, 
    wDefine, wUndef, wCompile, wLink, wLinkSys, wPure, wPush, wPop, wBreakpoint, 
    wCheckpoint, wPassL, wPassC, wDeadCodeElim, wDeprecated}
  lambdaPragmas* = {FirstCallConv..LastCallConv, wImportc, wExportc, wNodecl, 
    wNosideEffect, wSideEffect, wNoreturn, wDynLib, wHeader, wPure, wDeprecated}
  typePragmas* = {wImportc, wExportc, wDeprecated, wMagic, wAcyclic, wNodecl, 
    wPure, wHeader, wCompilerProc, wFinal}
  fieldPragmas* = {wImportc, wExportc, wDeprecated}
  varPragmas* = {wImportc, wExportc, wVolatile, wRegister, wThreadVar, wNodecl, 
    wMagic, wHeader, wDeprecated, wCompilerProc, wDynLib}
  constPragmas* = {wImportc, wExportc, wHeader, wDeprecated, wMagic, wNodecl}
  procTypePragmas* = {FirstCallConv..LastCallConv, wVarargs, wNosideEffect}

proc pragma*(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords)
proc pragmaAsm*(c: PContext, n: PNode): char
# implementation

proc invalidPragma(n: PNode) = 
  liMessage(n.info, errInvalidPragmaX, renderTree(n, {renderNoComments}))

proc pragmaAsm(c: PContext, n: PNode): char = 
  result = '\0'
  if n != nil: 
    for i in countup(0, sonsLen(n) - 1): 
      var it = n.sons[i]
      if (it.kind == nkExprColonExpr) and (it.sons[0].kind == nkIdent): 
        case whichKeyword(it.sons[0].ident)
        of wSubsChar: 
          if it.sons[1].kind == nkCharLit: result = chr(int(it.sons[1].intVal))
          else: invalidPragma(it)
        else: invalidPragma(it)
      else: 
        invalidPragma(it)
  
const 
  FirstPragmaWord = wMagic
  LastPragmaWord = wNoconv

proc MakeExternImport(s: PSym, extname: string) = 
  s.loc.r = toRope(extname)
  incl(s.flags, sfImportc)
  excl(s.flags, sfForward)

proc MakeExternExport(s: PSym, extname: string) = 
  s.loc.r = toRope(extname)
  incl(s.flags, sfExportc)

proc expectStrLit(c: PContext, n: PNode): string = 
  if n.kind != nkExprColonExpr: 
    liMessage(n.info, errStringLiteralExpected)
    result = ""
  else: 
    n.sons[1] = c.semConstExpr(c, n.sons[1])
    case n.sons[1].kind
    of nkStrLit, nkRStrLit, nkTripleStrLit: result = n.sons[1].strVal
    else: 
      liMessage(n.info, errStringLiteralExpected)
      result = ""

proc expectIntLit(c: PContext, n: PNode): int = 
  if n.kind != nkExprColonExpr: 
    liMessage(n.info, errIntLiteralExpected)
    result = 0
  else: 
    n.sons[1] = c.semConstExpr(c, n.sons[1])
    case n.sons[1].kind
    of nkIntLit..nkInt64Lit: result = int(n.sons[1].intVal)
    else: 
      liMessage(n.info, errIntLiteralExpected)
      result = 0

proc getOptionalStr(c: PContext, n: PNode, defaultStr: string): string = 
  if n.kind == nkExprColonExpr: result = expectStrLit(c, n)
  else: result = defaultStr
  
proc processMagic(c: PContext, n: PNode, s: PSym) = 
  var v: string
  #if not (sfSystemModule in c.module.flags) then
  #  liMessage(n.info, errMagicOnlyInSystem);
  if n.kind != nkExprColonExpr: liMessage(n.info, errStringLiteralExpected)
  if n.sons[1].kind == nkIdent: v = n.sons[1].ident.s
  else: v = expectStrLit(c, n)
  incl(s.flags, sfImportc) 
  # magics don't need an implementation, so we
  # treat them as imported, instead of modifing a lot of working code
  # BUGFIX: magic does not imply ``lfNoDecl`` anymore!
  for m in countup(low(TMagic), high(TMagic)): 
    if copy($m, 1) == v: 
      s.magic = m
      return 
  liMessage(n.info, warnUnknownMagic, v)

proc wordToCallConv(sw: TSpecialWord): TCallingConvention = 
  # this assumes that the order of special words and calling conventions is
  # the same
  result = TCallingConvention(ord(ccDefault) + ord(sw) - ord(wNimcall))

proc onOff(c: PContext, n: PNode, op: TOptions) = 
  if (n.kind == nkExprColonExpr) and (n.sons[1].kind == nkIdent): 
    case whichKeyword(n.sons[1].ident)
    of wOn: gOptions = gOptions + op
    of wOff: gOptions = gOptions - op
    else: liMessage(n.info, errOnOrOffExpected)
  else: 
    liMessage(n.info, errOnOrOffExpected)
  
proc pragmaDeadCodeElim(c: PContext, n: PNode) = 
  if (n.kind == nkExprColonExpr) and (n.sons[1].kind == nkIdent): 
    case whichKeyword(n.sons[1].ident)
    of wOn: incl(c.module.flags, sfDeadCodeElim)
    of wOff: excl(c.module.flags, sfDeadCodeElim)
    else: liMessage(n.info, errOnOrOffExpected)
  else: 
    liMessage(n.info, errOnOrOffExpected)
  
proc processCallConv(c: PContext, n: PNode) = 
  if (n.kind == nkExprColonExpr) and (n.sons[1].kind == nkIdent): 
    var sw = whichKeyword(n.sons[1].ident)
    case sw
    of firstCallConv..lastCallConv: 
      POptionEntry(c.optionStack.tail).defaultCC = wordToCallConv(sw)
    else: liMessage(n.info, errCallConvExpected)
  else: 
    liMessage(n.info, errCallConvExpected)
  
proc getLib(c: PContext, kind: TLibKind, path: string): PLib = 
  var it = PLib(c.libs.head)
  while it != nil: 
    if it.kind == kind: 
      if ospCaseInsensitive in platform.OS[targetOS].props: 
        if cmpIgnoreCase(it.path, path) == 0: 
          return it
      else: 
        if it.path == path: 
          return it
    it = PLib(it.next)
  result = newLib(kind)
  result.path = path
  Append(c.libs, result)

proc processDynLib(c: PContext, n: PNode, sym: PSym) = 
  if (sym == nil) or (sym.kind == skModule): 
    POptionEntry(c.optionStack.tail).dynlib = getLib(c, libDynamic, 
        expectStrLit(c, n))
  elif n.kind == nkExprColonExpr: 
    var lib = getLib(c, libDynamic, expectStrLit(c, n))
    addToLib(lib, sym)
    incl(sym.loc.flags, lfDynamicLib)
  else: 
    incl(sym.loc.flags, lfExportLib)
  
proc processNote(c: PContext, n: PNode) = 
  if (n.kind == nkExprColonExpr) and (sonsLen(n) == 2) and
      (n.sons[0].kind == nkBracketExpr) and
      (n.sons[0].sons[1].kind == nkIdent) and
      (n.sons[0].sons[0].kind == nkIdent) and (n.sons[1].kind == nkIdent): 
    var nk: TNoteKind
    case whichKeyword(n.sons[0].sons[0].ident)
    of wHint: 
      var x = findStr(msgs.HintsToStr, n.sons[0].sons[1].ident.s)
      if x >= 0: nk = TNoteKind(x + ord(hintMin))
      else: invalidPragma(n)
    of wWarning: 
      var x = findStr(msgs.WarningsToStr, n.sons[0].sons[1].ident.s)
      if x >= 0: nk = TNoteKind(x + ord(warnMin))
      else: InvalidPragma(n)
    else: 
      invalidPragma(n)
      return 
    case whichKeyword(n.sons[1].ident)
    of wOn: incl(gNotes, nk)
    of wOff: excl(gNotes, nk)
    else: liMessage(n.info, errOnOrOffExpected)
  else: 
    invalidPragma(n)
  
proc processOption(c: PContext, n: PNode) = 
  if n.kind != nkExprColonExpr: invalidPragma(n)
  elif n.sons[0].kind == nkBracketExpr: processNote(c, n)
  elif n.sons[0].kind != nkIdent: invalidPragma(n)
  else: 
    var sw = whichKeyword(n.sons[0].ident)
    case sw
    of wChecks: OnOff(c, n, checksOptions)
    of wObjChecks: OnOff(c, n, {optObjCheck})
    of wFieldchecks: OnOff(c, n, {optFieldCheck})
    of wRangechecks: OnOff(c, n, {optRangeCheck})
    of wBoundchecks: OnOff(c, n, {optBoundsCheck})
    of wOverflowchecks: OnOff(c, n, {optOverflowCheck})
    of wNilchecks: OnOff(c, n, {optNilCheck})
    of wAssertions: OnOff(c, n, {optAssert})
    of wWarnings: OnOff(c, n, {optWarns})
    of wHints: OnOff(c, n, {optHints})
    of wCallConv: processCallConv(c, n)   
    of wLinedir: OnOff(c, n, {optLineDir})
    of wStacktrace: OnOff(c, n, {optStackTrace})
    of wLinetrace: OnOff(c, n, {optLineTrace})
    of wDebugger: OnOff(c, n, {optEndb})
    of wProfiler: OnOff(c, n, {optProfiler})
    of wByRef: OnOff(c, n, {optByRef})
    of wDynLib: processDynLib(c, n, nil) 
    of wOptimization: 
      if n.sons[1].kind != nkIdent: 
        invalidPragma(n)
      else: 
        case whichKeyword(n.sons[1].ident)
        of wSpeed: 
          incl(gOptions, optOptimizeSpeed)
          excl(gOptions, optOptimizeSize)
        of wSize: 
          excl(gOptions, optOptimizeSpeed)
          incl(gOptions, optOptimizeSize)
        of wNone: 
          excl(gOptions, optOptimizeSpeed)
          excl(gOptions, optOptimizeSize)
        else: liMessage(n.info, errNoneSpeedOrSizeExpected)
    else: liMessage(n.info, errOptionExpected)
  
proc processPush(c: PContext, n: PNode, start: int) = 
  var x = newOptionEntry()
  var y = POptionEntry(c.optionStack.tail)
  x.options = gOptions
  x.defaultCC = y.defaultCC
  x.dynlib = y.dynlib
  x.notes = gNotes
  append(c.optionStack, x)
  for i in countup(start, sonsLen(n) - 1): 
    processOption(c, n.sons[i]) 
    #liMessage(n.info, warnUser, ropeToStr(optionsToStr(gOptions)));
  
proc processPop(c: PContext, n: PNode) = 
  if c.optionStack.counter <= 1: 
    liMessage(n.info, errAtPopWithoutPush)
  else: 
    gOptions = POptionEntry(c.optionStack.tail).options 
    #liMessage(n.info, warnUser, ropeToStr(optionsToStr(gOptions)));
    gNotes = POptionEntry(c.optionStack.tail).notes
    remove(c.optionStack, c.optionStack.tail)

proc processDefine(c: PContext, n: PNode) = 
  if (n.kind == nkExprColonExpr) and (n.sons[1].kind == nkIdent): 
    DefineSymbol(n.sons[1].ident.s)
    liMessage(n.info, warnDeprecated, "define")
  else: 
    invalidPragma(n)
  
proc processUndef(c: PContext, n: PNode) = 
  if (n.kind == nkExprColonExpr) and (n.sons[1].kind == nkIdent): 
    UndefSymbol(n.sons[1].ident.s)
    liMessage(n.info, warnDeprecated, "undef")
  else: 
    invalidPragma(n)
  
type 
  TLinkFeature = enum 
    linkNormal, linkSys

proc processCompile(c: PContext, n: PNode) = 
  var s = expectStrLit(c, n)
  var found = findFile(s)
  if found == "": found = s
  var trunc = ChangeFileExt(found, "")
  extccomp.addExternalFileToCompile(trunc)
  extccomp.addFileToLink(completeCFilePath(trunc, false))

proc processCommonLink(c: PContext, n: PNode, feature: TLinkFeature) = 
  var f, found: string
  f = expectStrLit(c, n)
  if splitFile(f).ext == "": f = toObjFile(f)
  found = findFile(f)
  if found == "": found = f # use the default
  case feature
  of linkNormal: extccomp.addFileToLink(found)
  of linkSys: 
    extccomp.addFileToLink(joinPath(libpath, completeCFilePath(found, false)))
  else: internalError(n.info, "processCommonLink")
  
proc PragmaBreakpoint(c: PContext, n: PNode) = 
  discard getOptionalStr(c, n, "")

proc PragmaCheckpoint(c: PContext, n: PNode) = 
  # checkpoints can be used to debug the compiler; they are not documented
  var info = n.info
  inc(info.line)              # next line is affected!
  msgs.addCheckpoint(info)

proc noVal(n: PNode) = 
  if n.kind == nkExprColonExpr: invalidPragma(n)
  
proc pragma(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords) = 
  if n == nil: return 
  for i in countup(0, sonsLen(n) - 1): 
    var it = n.sons[i]
    var key = if it.kind == nkExprColonExpr: it.sons[0] else: it
    if key.kind == nkIdent: 
      var k = whichKeyword(key.ident)
      if k in validPragmas: 
        case k
        of wExportc: 
          makeExternExport(sym, getOptionalStr(c, it, sym.name.s))
          incl(sym.flags, sfUsed) # avoid wrong hints
        of wImportc: makeExternImport(sym, getOptionalStr(c, it, sym.name.s))
        of wAlign: 
          if sym.typ == nil: invalidPragma(it)
          sym.typ.align = expectIntLit(c, it)
          if not IsPowerOfTwo(sym.typ.align) and (sym.typ.align != 0): 
            liMessage(it.info, errPowerOfTwoExpected)
        of wNodecl: 
          noVal(it)
          incl(sym.loc.Flags, lfNoDecl)
        of wPure: 
          noVal(it)
          if sym != nil: incl(sym.flags, sfPure)
        of wVolatile: 
          noVal(it)
          incl(sym.flags, sfVolatile)
        of wRegister: 
          noVal(it)
          incl(sym.flags, sfRegister)
        of wThreadVar: 
          noVal(it)
          incl(sym.flags, sfThreadVar)
        of wDeadCodeElim: pragmaDeadCodeElim(c, it)
        of wMagic: processMagic(c, it, sym)
        of wCompileTime: 
          noVal(it)
          incl(sym.flags, sfCompileTime)
          incl(sym.loc.Flags, lfNoDecl)
        of wMerge: 
          noval(it)
          incl(sym.flags, sfMerge)
        of wHeader: 
          var lib = getLib(c, libHeader, expectStrLit(c, it))
          addToLib(lib, sym)
          incl(sym.flags, sfImportc)
          incl(sym.loc.flags, lfHeader)
          incl(sym.loc.Flags, lfNoDecl) # implies nodecl, because
                                        # otherwise header would not make sense
          if sym.loc.r == nil: sym.loc.r = toRope(sym.name.s)
        of wNosideeffect: 
          noVal(it)
          incl(sym.flags, sfNoSideEffect)
          if sym.typ != nil: incl(sym.typ.flags, tfNoSideEffect)
        of wSideEffect: 
          noVal(it)
          incl(sym.flags, sfSideEffect)
        of wNoReturn: 
          noVal(it)
          incl(sym.flags, sfNoReturn)
        of wDynLib: 
          processDynLib(c, it, sym)
        of wCompilerProc: 
          noVal(it)           # compilerproc may not get a string!
          makeExternExport(sym, sym.name.s)
          incl(sym.flags, sfCompilerProc)
          incl(sym.flags, sfUsed) # suppress all those stupid warnings
          registerCompilerProc(sym)
        of wProcvar: 
          noVal(it)
          incl(sym.flags, sfProcVar)
        of wDeprecated: 
          noVal(it)
          if sym != nil: incl(sym.flags, sfDeprecated)
          else: incl(c.module.flags, sfDeprecated)
        of wVarargs: 
          noVal(it)
          if sym.typ == nil: invalidPragma(it)
          incl(sym.typ.flags, tfVarargs)
        of wBorrow: 
          noVal(it)
          incl(sym.flags, sfBorrow)
        of wFinal: 
          noVal(it)
          if sym.typ == nil: invalidPragma(it)
          incl(sym.typ.flags, tfFinal)
        of wAcyclic: 
          noVal(it)
          if sym.typ == nil: invalidPragma(it)
          incl(sym.typ.flags, tfAcyclic)
        of wTypeCheck: 
          noVal(it)
          incl(sym.flags, sfTypeCheck)
        of wHint: liMessage(it.info, hintUser, expectStrLit(c, it))
        of wWarning: liMessage(it.info, warnUser, expectStrLit(c, it))
        of wError: liMessage(it.info, errUser, expectStrLit(c, it))
        of wFatal: 
          liMessage(it.info, errUser, expectStrLit(c, it))
          quit(1)
        of wDefine: processDefine(c, it)
        of wUndef: processUndef(c, it)
        of wCompile: processCompile(c, it)
        of wLink: processCommonLink(c, it, linkNormal)
        of wLinkSys: processCommonLink(c, it, linkSys)
        of wPassL: extccomp.addLinkOption(expectStrLit(c, it))
        of wPassC: extccomp.addCompileOption(expectStrLit(c, it))
        of wBreakpoint: PragmaBreakpoint(c, it)
        of wCheckpoint: PragmaCheckpoint(c, it)
        of wPush: 
          processPush(c, n, i + 1)
          break 
        of wPop: processPop(c, it)
        of wChecks, wObjChecks, wFieldChecks, wRangechecks, wBoundchecks, 
           wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints, 
           wLinedir, wStacktrace, wLinetrace, wOptimization, wByRef, wCallConv, 
           wDebugger, wProfiler: 
          processOption(c, it) # calling conventions (boring...):
        of firstCallConv..lastCallConv: 
          assert(sym != nil)
          if sym.typ == nil: invalidPragma(it)
          sym.typ.callConv = wordToCallConv(k)
        else: invalidPragma(it)
      else: invalidPragma(it)
    else: processNote(c, it)
  if (sym != nil) and (sym.kind != skModule): 
    if (lfExportLib in sym.loc.flags) and not (sfExportc in sym.flags): 
      liMessage(n.info, errDynlibRequiresExportc)
    var lib = POptionEntry(c.optionstack.tail).dynlib
    if ({lfDynamicLib, lfHeader} * sym.loc.flags == {}) and
        (sfImportc in sym.flags) and (lib != nil): 
      incl(sym.loc.flags, lfDynamicLib)
      addToLib(lib, sym)
      if sym.loc.r == nil: sym.loc.r = toRope(sym.name.s)