123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790 |
- #
- #
- # The Nim Compiler
- # (c) Copyright 2015 Andreas Rumpf
- #
- # See the file "copying.txt", included in this
- # distribution, for details about the copyright.
- #
- # this module folds constants; used by semantic checking phase
- # and evaluation phase
- import
- strutils, options, ast, astalgo, trees, treetab, nimsets, times,
- nversion, platform, math, msgs, os, condsyms, idents, renderer, types,
- commands, magicsys, saturate
- proc getConstExpr*(m: PSym, n: PNode): PNode
- # evaluates the constant expression or returns nil if it is no constant
- # expression
- proc evalOp*(m: TMagic, n, a, b, c: PNode): PNode
- proc leValueConv*(a, b: PNode): bool
- proc newIntNodeT*(intVal: BiggestInt, n: PNode): PNode
- proc newFloatNodeT(floatVal: BiggestFloat, n: PNode): PNode
- proc newStrNodeT*(strVal: string, n: PNode): PNode
- # implementation
- proc newIntNodeT(intVal: BiggestInt, n: PNode): PNode =
- case skipTypes(n.typ, abstractVarRange).kind
- of tyInt:
- result = newIntNode(nkIntLit, intVal)
- result.typ = getIntLitType(result)
- # hrm, this is not correct: 1 + high(int) shouldn't produce tyInt64 ...
- #setIntLitType(result)
- of tyChar:
- result = newIntNode(nkCharLit, intVal)
- result.typ = n.typ
- else:
- result = newIntNode(nkIntLit, intVal)
- result.typ = n.typ
- result.info = n.info
- proc newFloatNodeT(floatVal: BiggestFloat, n: PNode): PNode =
- result = newFloatNode(nkFloatLit, floatVal)
- if skipTypes(n.typ, abstractVarRange).kind == tyFloat:
- result.typ = getFloatLitType(result)
- else:
- result.typ = n.typ
- result.info = n.info
- proc newStrNodeT(strVal: string, n: PNode): PNode =
- result = newStrNode(nkStrLit, strVal)
- result.typ = n.typ
- result.info = n.info
- proc ordinalValToString*(a: PNode): string =
- # because $ has the param ordinal[T], `a` is not necessarily an enum, but an
- # ordinal
- var x = getInt(a)
- var t = skipTypes(a.typ, abstractRange)
- case t.kind
- of tyChar:
- result = $chr(int(x) and 0xff)
- of tyEnum:
- var n = t.n
- for i in countup(0, sonsLen(n) - 1):
- if n.sons[i].kind != nkSym: internalError(a.info, "ordinalValToString")
- var field = n.sons[i].sym
- if field.position == x:
- if field.ast == nil:
- return field.name.s
- else:
- return field.ast.strVal
- internalError(a.info, "no symbol for ordinal value: " & $x)
- else:
- result = $x
- proc isFloatRange(t: PType): bool {.inline.} =
- result = t.kind == tyRange and t.sons[0].kind in {tyFloat..tyFloat128}
- proc isIntRange(t: PType): bool {.inline.} =
- result = t.kind == tyRange and t.sons[0].kind in {
- tyInt..tyInt64, tyUInt8..tyUInt32}
- proc pickIntRange(a, b: PType): PType =
- if isIntRange(a): result = a
- elif isIntRange(b): result = b
- else: result = a
- proc isIntRangeOrLit(t: PType): bool =
- result = isIntRange(t) or isIntLit(t)
- proc pickMinInt(n: PNode): BiggestInt =
- if n.kind in {nkIntLit..nkUInt64Lit}:
- result = n.intVal
- elif isIntLit(n.typ):
- result = n.typ.n.intVal
- elif isIntRange(n.typ):
- result = firstOrd(n.typ)
- else:
- internalError(n.info, "pickMinInt")
- proc pickMaxInt(n: PNode): BiggestInt =
- if n.kind in {nkIntLit..nkUInt64Lit}:
- result = n.intVal
- elif isIntLit(n.typ):
- result = n.typ.n.intVal
- elif isIntRange(n.typ):
- result = lastOrd(n.typ)
- else:
- internalError(n.info, "pickMaxInt")
- proc makeRange(typ: PType, first, last: BiggestInt): PType =
- let minA = min(first, last)
- let maxA = max(first, last)
- let lowerNode = newIntNode(nkIntLit, minA)
- if typ.kind == tyInt and minA == maxA:
- result = getIntLitType(lowerNode)
- elif typ.kind in {tyUint, tyUInt64}:
- # these are not ordinal types, so you get no subrange type for these:
- result = typ
- else:
- var n = newNode(nkRange)
- addSon(n, lowerNode)
- addSon(n, newIntNode(nkIntLit, maxA))
- result = newType(tyRange, typ.owner)
- result.n = n
- addSonSkipIntLit(result, skipTypes(typ, {tyRange}))
- proc makeRangeF(typ: PType, first, last: BiggestFloat): PType =
- var n = newNode(nkRange)
- addSon(n, newFloatNode(nkFloatLit, min(first.float, last.float)))
- addSon(n, newFloatNode(nkFloatLit, max(first.float, last.float)))
- result = newType(tyRange, typ.owner)
- result.n = n
- addSonSkipIntLit(result, skipTypes(typ, {tyRange}))
- proc getIntervalType*(m: TMagic, n: PNode): PType =
- # Nim requires interval arithmetic for ``range`` types. Lots of tedious
- # work but the feature is very nice for reducing explicit conversions.
- const ordIntLit = {nkIntLit..nkUInt64Lit}
- result = n.typ
- template commutativeOp(opr: untyped) =
- let a = n.sons[1]
- let b = n.sons[2]
- if isIntRangeOrLit(a.typ) and isIntRangeOrLit(b.typ):
- result = makeRange(pickIntRange(a.typ, b.typ),
- opr(pickMinInt(a), pickMinInt(b)),
- opr(pickMaxInt(a), pickMaxInt(b)))
- template binaryOp(opr: untyped) =
- let a = n.sons[1]
- let b = n.sons[2]
- if isIntRange(a.typ) and b.kind in {nkIntLit..nkUInt64Lit}:
- result = makeRange(a.typ,
- opr(pickMinInt(a), pickMinInt(b)),
- opr(pickMaxInt(a), pickMaxInt(b)))
- case m
- of mUnaryMinusI, mUnaryMinusI64:
- let a = n.sons[1].typ
- if isIntRange(a):
- # (1..3) * (-1) == (-3.. -1)
- result = makeRange(a, 0|-|lastOrd(a), 0|-|firstOrd(a))
- of mUnaryMinusF64:
- let a = n.sons[1].typ
- if isFloatRange(a):
- result = makeRangeF(a, -getFloat(a.n.sons[1]),
- -getFloat(a.n.sons[0]))
- of mAbsF64:
- let a = n.sons[1].typ
- if isFloatRange(a):
- # abs(-5.. 1) == (1..5)
- if a.n[0].floatVal <= 0.0:
- result = makeRangeF(a, 0.0, abs(getFloat(a.n.sons[0])))
- else:
- result = makeRangeF(a, abs(getFloat(a.n.sons[1])),
- abs(getFloat(a.n.sons[0])))
- of mAbsI:
- let a = n.sons[1].typ
- if isIntRange(a):
- if a.n[0].intVal <= 0:
- result = makeRange(a, 0, `|abs|`(getInt(a.n.sons[0])))
- else:
- result = makeRange(a, `|abs|`(getInt(a.n.sons[1])),
- `|abs|`(getInt(a.n.sons[0])))
- of mSucc:
- let a = n.sons[1].typ
- let b = n.sons[2].typ
- if isIntRange(a) and isIntLit(b):
- # (-5.. 1) + 6 == (-5 + 6)..(-1 + 6)
- result = makeRange(a, pickMinInt(n.sons[1]) |+| pickMinInt(n.sons[2]),
- pickMaxInt(n.sons[1]) |+| pickMaxInt(n.sons[2]))
- of mPred:
- let a = n.sons[1].typ
- let b = n.sons[2].typ
- if isIntRange(a) and isIntLit(b):
- result = makeRange(a, pickMinInt(n.sons[1]) |-| pickMinInt(n.sons[2]),
- pickMaxInt(n.sons[1]) |-| pickMaxInt(n.sons[2]))
- of mAddI, mAddU:
- commutativeOp(`|+|`)
- of mMulI, mMulU:
- commutativeOp(`|*|`)
- of mSubI, mSubU:
- binaryOp(`|-|`)
- of mBitandI:
- # since uint64 is still not even valid for 'range' (since it's no ordinal
- # yet), we exclude it from the list (see bug #1638) for now:
- var a = n.sons[1]
- var b = n.sons[2]
- # symmetrical:
- if b.kind notin ordIntLit: swap(a, b)
- if b.kind in ordIntLit:
- let x = b.intVal|+|1
- if (x and -x) == x and x >= 0:
- result = makeRange(n.typ, 0, b.intVal)
- of mModU:
- let a = n.sons[1]
- let b = n.sons[2]
- if b.kind in ordIntLit:
- if b.intVal >= 0:
- result = makeRange(n.typ, 0, b.intVal-1)
- else:
- result = makeRange(n.typ, b.intVal+1, 0)
- of mModI:
- # so ... if you ever wondered about modulo's signedness; this defines it:
- let a = n.sons[1]
- let b = n.sons[2]
- if b.kind in {nkIntLit..nkUInt64Lit}:
- if b.intVal >= 0:
- result = makeRange(n.typ, -(b.intVal-1), b.intVal-1)
- else:
- result = makeRange(n.typ, b.intVal+1, -(b.intVal+1))
- of mDivI, mDivU:
- binaryOp(`|div|`)
- of mMinI:
- commutativeOp(min)
- of mMaxI:
- commutativeOp(max)
- else: discard
- discard """
- mShlI,
- mShrI, mAddF64, mSubF64, mMulF64, mDivF64, mMaxF64, mMinF64
- """
- proc evalIs(n, a: PNode): PNode =
- # XXX: This should use the standard isOpImpl
- internalAssert a.kind == nkSym and a.sym.kind == skType
- internalAssert n.sonsLen == 3 and
- n[2].kind in {nkStrLit..nkTripleStrLit, nkType}
- let t1 = a.sym.typ
- if n[2].kind in {nkStrLit..nkTripleStrLit}:
- case n[2].strVal.normalize
- of "closure":
- let t = skipTypes(t1, abstractRange)
- result = newIntNode(nkIntLit, ord(t.kind == tyProc and
- t.callConv == ccClosure and
- tfIterator notin t.flags))
- of "iterator":
- let t = skipTypes(t1, abstractRange)
- result = newIntNode(nkIntLit, ord(t.kind == tyProc and
- t.callConv == ccClosure and
- tfIterator in t.flags))
- else: discard
- else:
- # XXX semexprs.isOpImpl is slightly different and requires a context. yay.
- let t2 = n[2].typ
- var match = sameType(t1, t2)
- result = newIntNode(nkIntLit, ord(match))
- result.typ = n.typ
- proc evalOp(m: TMagic, n, a, b, c: PNode): PNode =
- # b and c may be nil
- result = nil
- case m
- of mOrd: result = newIntNodeT(getOrdValue(a), n)
- of mChr: result = newIntNodeT(getInt(a), n)
- of mUnaryMinusI, mUnaryMinusI64: result = newIntNodeT(- getInt(a), n)
- of mUnaryMinusF64: result = newFloatNodeT(- getFloat(a), n)
- of mNot: result = newIntNodeT(1 - getInt(a), n)
- of mCard: result = newIntNodeT(nimsets.cardSet(a), n)
- of mBitnotI: result = newIntNodeT(not getInt(a), n)
- of mLengthArray: result = newIntNodeT(lengthOrd(a.typ), n)
- of mLengthSeq, mLengthOpenArray, mXLenSeq, mLengthStr, mXLenStr:
- if a.kind == nkNilLit:
- result = newIntNodeT(0, n)
- elif a.kind in {nkStrLit..nkTripleStrLit}:
- result = newIntNodeT(len a.strVal, n)
- else:
- result = newIntNodeT(sonsLen(a), n) # BUGFIX
- of mUnaryPlusI, mUnaryPlusF64: result = a # throw `+` away
- of mToFloat, mToBiggestFloat:
- result = newFloatNodeT(toFloat(int(getInt(a))), n)
- of mToInt, mToBiggestInt: result = newIntNodeT(system.toInt(getFloat(a)), n)
- of mAbsF64: result = newFloatNodeT(abs(getFloat(a)), n)
- of mAbsI:
- if getInt(a) >= 0: result = a
- else: result = newIntNodeT(- getInt(a), n)
- of mZe8ToI, mZe8ToI64, mZe16ToI, mZe16ToI64, mZe32ToI64, mZeIToI64:
- # byte(-128) = 1...1..1000_0000'64 --> 0...0..1000_0000'64
- result = newIntNodeT(getInt(a) and (`shl`(1, getSize(a.typ) * 8) - 1), n)
- of mToU8: result = newIntNodeT(getInt(a) and 0x000000FF, n)
- of mToU16: result = newIntNodeT(getInt(a) and 0x0000FFFF, n)
- of mToU32: result = newIntNodeT(getInt(a) and 0x00000000FFFFFFFF'i64, n)
- of mUnaryLt: result = newIntNodeT(getOrdValue(a) |-| 1, n)
- of mSucc: result = newIntNodeT(getOrdValue(a) |+| getInt(b), n)
- of mPred: result = newIntNodeT(getOrdValue(a) |-| getInt(b), n)
- of mAddI: result = newIntNodeT(getInt(a) |+| getInt(b), n)
- of mSubI: result = newIntNodeT(getInt(a) |-| getInt(b), n)
- of mMulI: result = newIntNodeT(getInt(a) |*| getInt(b), n)
- of mMinI:
- if getInt(a) > getInt(b): result = newIntNodeT(getInt(b), n)
- else: result = newIntNodeT(getInt(a), n)
- of mMaxI:
- if getInt(a) > getInt(b): result = newIntNodeT(getInt(a), n)
- else: result = newIntNodeT(getInt(b), n)
- of mShlI:
- case skipTypes(n.typ, abstractRange).kind
- of tyInt8: result = newIntNodeT(int8(getInt(a)) shl int8(getInt(b)), n)
- of tyInt16: result = newIntNodeT(int16(getInt(a)) shl int16(getInt(b)), n)
- of tyInt32: result = newIntNodeT(int32(getInt(a)) shl int32(getInt(b)), n)
- of tyInt64, tyInt, tyUInt..tyUInt64:
- result = newIntNodeT(`shl`(getInt(a), getInt(b)), n)
- else: internalError(n.info, "constant folding for shl")
- of mShrI:
- case skipTypes(n.typ, abstractRange).kind
- of tyInt8: result = newIntNodeT(int8(getInt(a)) shr int8(getInt(b)), n)
- of tyInt16: result = newIntNodeT(int16(getInt(a)) shr int16(getInt(b)), n)
- of tyInt32: result = newIntNodeT(int32(getInt(a)) shr int32(getInt(b)), n)
- of tyInt64, tyInt, tyUInt..tyUInt64:
- result = newIntNodeT(`shr`(getInt(a), getInt(b)), n)
- else: internalError(n.info, "constant folding for shr")
- of mDivI:
- let y = getInt(b)
- if y != 0:
- result = newIntNodeT(`|div|`(getInt(a), y), n)
- of mModI:
- let y = getInt(b)
- if y != 0:
- result = newIntNodeT(`|mod|`(getInt(a), y), n)
- of mAddF64: result = newFloatNodeT(getFloat(a) + getFloat(b), n)
- of mSubF64: result = newFloatNodeT(getFloat(a) - getFloat(b), n)
- of mMulF64: result = newFloatNodeT(getFloat(a) * getFloat(b), n)
- of mDivF64:
- if getFloat(b) == 0.0:
- if getFloat(a) == 0.0: result = newFloatNodeT(NaN, n)
- else: result = newFloatNodeT(Inf, n)
- else:
- result = newFloatNodeT(getFloat(a) / getFloat(b), n)
- of mMaxF64:
- if getFloat(a) > getFloat(b): result = newFloatNodeT(getFloat(a), n)
- else: result = newFloatNodeT(getFloat(b), n)
- of mMinF64:
- if getFloat(a) > getFloat(b): result = newFloatNodeT(getFloat(b), n)
- else: result = newFloatNodeT(getFloat(a), n)
- of mIsNil: result = newIntNodeT(ord(a.kind == nkNilLit), n)
- of mLtI, mLtB, mLtEnum, mLtCh:
- result = newIntNodeT(ord(getOrdValue(a) < getOrdValue(b)), n)
- of mLeI, mLeB, mLeEnum, mLeCh:
- result = newIntNodeT(ord(getOrdValue(a) <= getOrdValue(b)), n)
- of mEqI, mEqB, mEqEnum, mEqCh:
- result = newIntNodeT(ord(getOrdValue(a) == getOrdValue(b)), n)
- of mLtF64: result = newIntNodeT(ord(getFloat(a) < getFloat(b)), n)
- of mLeF64: result = newIntNodeT(ord(getFloat(a) <= getFloat(b)), n)
- of mEqF64: result = newIntNodeT(ord(getFloat(a) == getFloat(b)), n)
- of mLtStr: result = newIntNodeT(ord(getStr(a) < getStr(b)), n)
- of mLeStr: result = newIntNodeT(ord(getStr(a) <= getStr(b)), n)
- of mEqStr: result = newIntNodeT(ord(getStr(a) == getStr(b)), n)
- of mLtU, mLtU64:
- result = newIntNodeT(ord(`<%`(getOrdValue(a), getOrdValue(b))), n)
- of mLeU, mLeU64:
- result = newIntNodeT(ord(`<=%`(getOrdValue(a), getOrdValue(b))), n)
- of mBitandI, mAnd: result = newIntNodeT(a.getInt and b.getInt, n)
- of mBitorI, mOr: result = newIntNodeT(getInt(a) or getInt(b), n)
- of mBitxorI, mXor: result = newIntNodeT(a.getInt xor b.getInt, n)
- of mAddU: result = newIntNodeT(`+%`(getInt(a), getInt(b)), n)
- of mSubU: result = newIntNodeT(`-%`(getInt(a), getInt(b)), n)
- of mMulU: result = newIntNodeT(`*%`(getInt(a), getInt(b)), n)
- of mModU:
- let y = getInt(b)
- if y != 0:
- result = newIntNodeT(`%%`(getInt(a), y), n)
- of mDivU:
- let y = getInt(b)
- if y != 0:
- result = newIntNodeT(`/%`(getInt(a), y), n)
- of mLeSet: result = newIntNodeT(ord(containsSets(a, b)), n)
- of mEqSet: result = newIntNodeT(ord(equalSets(a, b)), n)
- of mLtSet:
- result = newIntNodeT(ord(containsSets(a, b) and not equalSets(a, b)), n)
- of mMulSet:
- result = nimsets.intersectSets(a, b)
- result.info = n.info
- of mPlusSet:
- result = nimsets.unionSets(a, b)
- result.info = n.info
- of mMinusSet:
- result = nimsets.diffSets(a, b)
- result.info = n.info
- of mSymDiffSet:
- result = nimsets.symdiffSets(a, b)
- result.info = n.info
- of mConStrStr: result = newStrNodeT(getStrOrChar(a) & getStrOrChar(b), n)
- of mInSet: result = newIntNodeT(ord(inSet(a, b)), n)
- of mRepr:
- # BUGFIX: we cannot eval mRepr here for reasons that I forgot.
- discard
- of mIntToStr, mInt64ToStr: result = newStrNodeT($(getOrdValue(a)), n)
- of mBoolToStr:
- if getOrdValue(a) == 0: result = newStrNodeT("false", n)
- else: result = newStrNodeT("true", n)
- of mCopyStr: result = newStrNodeT(substr(getStr(a), int(getOrdValue(b))), n)
- of mCopyStrLast:
- result = newStrNodeT(substr(getStr(a), int(getOrdValue(b)),
- int(getOrdValue(c))), n)
- of mFloatToStr: result = newStrNodeT($getFloat(a), n)
- of mCStrToStr, mCharToStr:
- if a.kind == nkBracket:
- var s = ""
- for b in a.sons:
- s.add b.getStrOrChar
- result = newStrNodeT(s, n)
- else:
- result = newStrNodeT(getStrOrChar(a), n)
- of mStrToStr: result = a
- of mEnumToStr: result = newStrNodeT(ordinalValToString(a), n)
- of mArrToSeq:
- result = copyTree(a)
- result.typ = n.typ
- of mCompileOption:
- result = newIntNodeT(ord(commands.testCompileOption(a.getStr, n.info)), n)
- of mCompileOptionArg:
- result = newIntNodeT(ord(
- testCompileOptionArg(getStr(a), getStr(b), n.info)), n)
- of mEqProc:
- result = newIntNodeT(ord(
- exprStructuralEquivalent(a, b, strictSymEquality=true)), n)
- else: discard
- proc getConstIfExpr(c: PSym, n: PNode): PNode =
- result = nil
- for i in countup(0, sonsLen(n) - 1):
- var it = n.sons[i]
- if it.len == 2:
- var e = getConstExpr(c, it.sons[0])
- if e == nil: return nil
- if getOrdValue(e) != 0:
- if result == nil:
- result = getConstExpr(c, it.sons[1])
- if result == nil: return
- elif it.len == 1:
- if result == nil: result = getConstExpr(c, it.sons[0])
- else: internalError(it.info, "getConstIfExpr()")
- proc leValueConv(a, b: PNode): bool =
- result = false
- case a.kind
- of nkCharLit..nkUInt64Lit:
- case b.kind
- of nkCharLit..nkUInt64Lit: result = a.intVal <= b.intVal
- of nkFloatLit..nkFloat128Lit: result = a.intVal <= round(b.floatVal).int
- else: internalError(a.info, "leValueConv")
- of nkFloatLit..nkFloat128Lit:
- case b.kind
- of nkFloatLit..nkFloat128Lit: result = a.floatVal <= b.floatVal
- of nkCharLit..nkUInt64Lit: result = a.floatVal <= toFloat(int(b.intVal))
- else: internalError(a.info, "leValueConv")
- else: internalError(a.info, "leValueConv")
- proc magicCall(m: PSym, n: PNode): PNode =
- if sonsLen(n) <= 1: return
- var s = n.sons[0].sym
- var a = getConstExpr(m, n.sons[1])
- var b, c: PNode
- if a == nil: return
- if sonsLen(n) > 2:
- b = getConstExpr(m, n.sons[2])
- if b == nil: return
- if sonsLen(n) > 3:
- c = getConstExpr(m, n.sons[3])
- if c == nil: return
- result = evalOp(s.magic, n, a, b, c)
- proc getAppType(n: PNode): PNode =
- if gGlobalOptions.contains(optGenDynLib):
- result = newStrNodeT("lib", n)
- elif gGlobalOptions.contains(optGenStaticLib):
- result = newStrNodeT("staticlib", n)
- elif gGlobalOptions.contains(optGenGuiApp):
- result = newStrNodeT("gui", n)
- else:
- result = newStrNodeT("console", n)
- proc rangeCheck(n: PNode, value: BiggestInt) =
- if value < firstOrd(n.typ) or value > lastOrd(n.typ):
- localError(n.info, errGenerated, "cannot convert " & $value &
- " to " & typeToString(n.typ))
- proc foldConv*(n, a: PNode; check = false): PNode =
- # XXX range checks?
- case skipTypes(n.typ, abstractRange).kind
- of tyInt..tyInt64, tyUInt..tyUInt64:
- case skipTypes(a.typ, abstractRange).kind
- of tyFloat..tyFloat64:
- result = newIntNodeT(int(getFloat(a)), n)
- of tyChar: result = newIntNodeT(getOrdValue(a), n)
- else:
- result = a
- result.typ = n.typ
- if check and result.kind in {nkCharLit..nkUInt64Lit}:
- rangeCheck(n, result.intVal)
- of tyFloat..tyFloat64:
- case skipTypes(a.typ, abstractRange).kind
- of tyInt..tyInt64, tyEnum, tyBool, tyChar:
- result = newFloatNodeT(toBiggestFloat(getOrdValue(a)), n)
- else:
- result = a
- result.typ = n.typ
- of tyOpenArray, tyVarargs, tyProc:
- discard
- else:
- result = a
- result.typ = n.typ
- proc getArrayConstr(m: PSym, n: PNode): PNode =
- if n.kind == nkBracket:
- result = n
- else:
- result = getConstExpr(m, n)
- if result == nil: result = n
- proc foldArrayAccess(m: PSym, n: PNode): PNode =
- var x = getConstExpr(m, n.sons[0])
- if x == nil or x.typ.skipTypes({tyGenericInst, tyAlias}).kind == tyTypeDesc:
- return
- var y = getConstExpr(m, n.sons[1])
- if y == nil: return
- var idx = getOrdValue(y)
- case x.kind
- of nkPar:
- if idx >= 0 and idx < sonsLen(x):
- result = x.sons[int(idx)]
- if result.kind == nkExprColonExpr: result = result.sons[1]
- else:
- localError(n.info, errIndexOutOfBounds)
- of nkBracket:
- idx = idx - x.typ.firstOrd
- if idx >= 0 and idx < x.len: result = x.sons[int(idx)]
- else: localError(n.info, errIndexOutOfBounds)
- of nkStrLit..nkTripleStrLit:
- result = newNodeIT(nkCharLit, x.info, n.typ)
- if idx >= 0 and idx < len(x.strVal):
- result.intVal = ord(x.strVal[int(idx)])
- elif idx == len(x.strVal):
- discard
- else:
- localError(n.info, errIndexOutOfBounds)
- else: discard
- proc foldFieldAccess(m: PSym, n: PNode): PNode =
- # a real field access; proc calls have already been transformed
- var x = getConstExpr(m, n.sons[0])
- if x == nil or x.kind notin {nkObjConstr, nkPar}: return
- var field = n.sons[1].sym
- for i in countup(ord(x.kind == nkObjConstr), sonsLen(x) - 1):
- var it = x.sons[i]
- if it.kind != nkExprColonExpr:
- # lookup per index:
- result = x.sons[field.position]
- if result.kind == nkExprColonExpr: result = result.sons[1]
- return
- if it.sons[0].sym.name.id == field.name.id:
- result = x.sons[i].sons[1]
- return
- localError(n.info, errFieldXNotFound, field.name.s)
- proc foldConStrStr(m: PSym, n: PNode): PNode =
- result = newNodeIT(nkStrLit, n.info, n.typ)
- result.strVal = ""
- for i in countup(1, sonsLen(n) - 1):
- let a = getConstExpr(m, n.sons[i])
- if a == nil: return nil
- result.strVal.add(getStrOrChar(a))
- proc newSymNodeTypeDesc*(s: PSym; info: TLineInfo): PNode =
- result = newSymNode(s, info)
- if s.typ.kind != tyTypeDesc:
- result.typ = newType(tyTypeDesc, s.owner)
- result.typ.addSonSkipIntLit(s.typ)
- else:
- result.typ = s.typ
- proc getConstExpr(m: PSym, n: PNode): PNode =
- result = nil
- case n.kind
- of nkSym:
- var s = n.sym
- case s.kind
- of skEnumField:
- result = newIntNodeT(s.position, n)
- of skConst:
- case s.magic
- of mIsMainModule: result = newIntNodeT(ord(sfMainModule in m.flags), n)
- of mCompileDate: result = newStrNodeT(times.getDateStr(), n)
- of mCompileTime: result = newStrNodeT(times.getClockStr(), n)
- of mCpuEndian: result = newIntNodeT(ord(CPU[targetCPU].endian), n)
- of mHostOS: result = newStrNodeT(toLowerAscii(platform.OS[targetOS].name), n)
- of mHostCPU: result = newStrNodeT(platform.CPU[targetCPU].name.toLowerAscii, n)
- of mBuildOS: result = newStrNodeT(toLowerAscii(platform.OS[platform.hostOS].name), n)
- of mBuildCPU: result = newStrNodeT(platform.CPU[platform.hostCPU].name.toLowerAscii, n)
- of mAppType: result = getAppType(n)
- of mNaN: result = newFloatNodeT(NaN, n)
- of mInf: result = newFloatNodeT(Inf, n)
- of mNegInf: result = newFloatNodeT(NegInf, n)
- of mIntDefine:
- if isDefined(s.name):
- result = newIntNodeT(lookupSymbol(s.name).parseInt, n)
- of mStrDefine:
- if isDefined(s.name):
- result = newStrNodeT(lookupSymbol(s.name), n)
- else:
- result = copyTree(s.ast)
- of {skProc, skFunc, skMethod}:
- result = n
- of skType:
- # XXX gensym'ed symbols can come here and cannot be resolved. This is
- # dirty, but correct.
- if s.typ != nil:
- result = newSymNodeTypeDesc(s, n.info)
- of skGenericParam:
- if s.typ.kind == tyStatic:
- if s.typ.n != nil and tfUnresolved notin s.typ.flags:
- result = s.typ.n
- result.typ = s.typ.base
- elif s.typ.isIntLit:
- result = s.typ.n
- else:
- result = newSymNodeTypeDesc(s, n.info)
- else: discard
- of nkCharLit..nkNilLit:
- result = copyNode(n)
- of nkIfExpr:
- result = getConstIfExpr(m, n)
- of nkCallKinds:
- if n.sons[0].kind != nkSym: return
- var s = n.sons[0].sym
- if s.kind != skProc and s.kind != skFunc: return
- try:
- case s.magic
- of mNone:
- # If it has no sideEffect, it should be evaluated. But not here.
- return
- of mSizeOf:
- var a = n.sons[1]
- if computeSize(a.typ) < 0:
- localError(a.info, errCannotEvalXBecauseIncompletelyDefined,
- "sizeof")
- result = nil
- elif skipTypes(a.typ, typedescInst+{tyRange}).kind in
- IntegralTypes+NilableTypes+{tySet}:
- #{tyArray,tyObject,tyTuple}:
- result = newIntNodeT(getSize(a.typ), n)
- else:
- result = nil
- # XXX: size computation for complex types is still wrong
- of mLow:
- result = newIntNodeT(firstOrd(n.sons[1].typ), n)
- of mHigh:
- if skipTypes(n.sons[1].typ, abstractVar).kind notin
- {tySequence, tyString, tyCString, tyOpenArray, tyVarargs}:
- result = newIntNodeT(lastOrd(skipTypes(n[1].typ, abstractVar)), n)
- else:
- var a = getArrayConstr(m, n.sons[1])
- if a.kind == nkBracket:
- # we can optimize it away:
- result = newIntNodeT(sonsLen(a)-1, n)
- of mLengthOpenArray:
- var a = getArrayConstr(m, n.sons[1])
- if a.kind == nkBracket:
- # we can optimize it away! This fixes the bug ``len(134)``.
- result = newIntNodeT(sonsLen(a), n)
- else:
- result = magicCall(m, n)
- of mLengthArray:
- # It doesn't matter if the argument is const or not for mLengthArray.
- # This fixes bug #544.
- result = newIntNodeT(lengthOrd(n.sons[1].typ), n)
- of mAstToStr:
- result = newStrNodeT(renderTree(n[1], {renderNoComments}), n)
- of mConStrStr:
- result = foldConStrStr(m, n)
- of mIs:
- let a = getConstExpr(m, n[1])
- if a != nil and a.kind == nkSym and a.sym.kind == skType:
- result = evalIs(n, a)
- else:
- result = magicCall(m, n)
- except OverflowError:
- localError(n.info, errOverOrUnderflow)
- except DivByZeroError:
- localError(n.info, errConstantDivisionByZero)
- of nkAddr:
- var a = getConstExpr(m, n.sons[0])
- if a != nil:
- result = n
- n.sons[0] = a
- of nkBracket:
- result = copyTree(n)
- for i in countup(0, sonsLen(n) - 1):
- var a = getConstExpr(m, n.sons[i])
- if a == nil: return nil
- result.sons[i] = a
- incl(result.flags, nfAllConst)
- of nkRange:
- var a = getConstExpr(m, n.sons[0])
- if a == nil: return
- var b = getConstExpr(m, n.sons[1])
- if b == nil: return
- result = copyNode(n)
- addSon(result, a)
- addSon(result, b)
- of nkCurly:
- result = copyTree(n)
- for i in countup(0, sonsLen(n) - 1):
- var a = getConstExpr(m, n.sons[i])
- if a == nil: return nil
- result.sons[i] = a
- incl(result.flags, nfAllConst)
- of nkObjConstr:
- result = copyTree(n)
- for i in countup(1, sonsLen(n) - 1):
- var a = getConstExpr(m, n.sons[i].sons[1])
- if a == nil: return nil
- result.sons[i].sons[1] = a
- incl(result.flags, nfAllConst)
- of nkPar:
- # tuple constructor
- result = copyTree(n)
- if (sonsLen(n) > 0) and (n.sons[0].kind == nkExprColonExpr):
- for i in countup(0, sonsLen(n) - 1):
- var a = getConstExpr(m, n.sons[i].sons[1])
- if a == nil: return nil
- result.sons[i].sons[1] = a
- else:
- for i in countup(0, sonsLen(n) - 1):
- var a = getConstExpr(m, n.sons[i])
- if a == nil: return nil
- result.sons[i] = a
- incl(result.flags, nfAllConst)
- of nkChckRangeF, nkChckRange64, nkChckRange:
- var a = getConstExpr(m, n.sons[0])
- if a == nil: return
- if leValueConv(n.sons[1], a) and leValueConv(a, n.sons[2]):
- result = a # a <= x and x <= b
- result.typ = n.typ
- else:
- localError(n.info, errGenerated, `%`(
- msgKindToString(errIllegalConvFromXtoY),
- [typeToString(n.sons[0].typ), typeToString(n.typ)]))
- of nkStringToCString, nkCStringToString:
- var a = getConstExpr(m, n.sons[0])
- if a == nil: return
- result = a
- result.typ = n.typ
- of nkHiddenStdConv, nkHiddenSubConv, nkConv:
- var a = getConstExpr(m, n.sons[1])
- if a == nil: return
- result = foldConv(n, a, check=n.kind == nkHiddenStdConv)
- of nkCast:
- var a = getConstExpr(m, n.sons[1])
- if a == nil: return
- if n.typ != nil and n.typ.kind in NilableTypes:
- # we allow compile-time 'cast' for pointer types:
- result = a
- result.typ = n.typ
- of nkBracketExpr: result = foldArrayAccess(m, n)
- of nkDotExpr: result = foldFieldAccess(m, n)
- else:
- discard
|