# # # The Nim Compiler # (c) Copyright 2012 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # # This module declares some helpers for the C code generator. import ast, types, msgs, wordrecg, platform, trees, options, cgendata, mangleutils import std/[hashes, strutils, formatfloat] when defined(nimPreviewSlimSystem): import std/assertions proc getPragmaStmt*(n: PNode, w: TSpecialWord): PNode = case n.kind of nkStmtList: result = nil for i in 0.. conf.target.floatSize * 3): result = true # requested anyway elif (tfFinal in pt.flags) and (pt[0] == nil): result = false # no need, because no subtyping possible else: result = true # ordinary objects are always passed by reference, # otherwise casting doesn't work of tyTuple: result = (getSize(conf, pt) > conf.target.floatSize*3) or (optByRef in s.options) else: result = false # first parameter and return type is 'lent T'? --> use pass by pointer if s.position == 0 and retType != nil and retType.kind == tyLent: result = not (pt.kind in {tyVar, tyArray, tyOpenArray, tyVarargs, tyRef, tyPtr, tyPointer} or pt.kind == tySet and mapSetType(conf, pt) == ctArray) proc encodeName*(name: string): string = result = mangle(name) result = $result.len & result proc makeUnique(m: BModule; s: PSym, name: string = ""): string = result = if name == "": s.name.s else: name result.add "__" result.add m.g.graph.ifaces[s.itemId.module].uniqueName result.add "_u" result.add $s.itemId.item proc encodeSym*(m: BModule; s: PSym; makeUnique: bool = false): string = #Module::Type var name = s.name.s if makeUnique: name = makeUnique(m, s, name) "N" & encodeName(s.skipGenericOwner.name.s) & encodeName(name) & "E" proc encodeType*(m: BModule; t: PType): string = result = "" var kindName = ($t.kind)[2..^1] kindName[0] = toLower($kindName[0])[0] case t.kind of tyObject, tyEnum, tyDistinct, tyUserTypeClass, tyGenericParam: result = encodeSym(m, t.sym) of tyGenericInst, tyUserTypeClassInst, tyGenericBody: result = encodeName(t[0].sym.name.s) result.add "I" for i in 1..