summary refs log blame commit diff stats
path: root/lib/system/channels.nim
blob: fe93d6840ed24d3ca28322f239a55759d1a1bc90 (plain) (tree)
s="p">.id == forLoop[0].ident.id: if tupleType.n == nil: # ugh, there are no field names: result = newStrNode(nkStrLit, "") else: result = newStrNode(nkStrLit, tupleType.n.sons[tupleIndex].sym.name.s) return # other fields: for i in first..L-3: if n.ident.id == forLoop[i].ident.id: var call = forLoop.sons[L-2] var tupl = call.sons[i+1-first] result = newNodeI(nkBracketExpr, n.info) result.add(tupl) result.add(newIntNode(nkIntLit, tupleIndex)) break else: result = copyNode(n) newSons(result, sonsLen(n)) for i in countup(0, sonsLen(n)-1): result.sons[i] = transfFieldLoopBody(n.sons[i], forLoop, tupleType, tupleIndex, first) proc semForFields(c: PContext, n: PNode, m: TMagic): PNode = # so that 'break' etc. work as expected, we produce # a 'while true: stmt; break' loop ... result = newNodeI(nkWhileStmt, n.info) var trueSymbol = StrTableGet(magicsys.systemModule.Tab, getIdent"true") if trueSymbol == nil: GlobalError(n.info, errSystemNeeds, "true") result.add(newSymNode(trueSymbol, n.info)) var stmts = newNodeI(nkStmtList, n.info) result.add(stmts) var length = sonsLen(n) var call = n.sons[length-2] if length-2 != sonsLen(call)-1 + ord(m==mFieldPairs): GlobalError(n.info, errWrongNumberOfVariables) var tupleTypeA = skipTypes(call.sons[1].typ, abstractVar) if tupleTypeA.kind != tyTuple: InternalError(n.info, "no tuple type!") for i in 1..call.len-1: var tupleTypeB = skipTypes(call.sons[i].typ, abstractVar) if not SameType(tupleTypeA, tupleTypeB): typeMismatch(call.sons[i], tupleTypeA, tupleTypeB) Inc(c.p.nestedLoopCounter) var loopBody = n.sons[length-1] for i in 0..sonsLen(tupleTypeA)-1: openScope(c.tab) var body = transfFieldLoopBody(loopBody, n, tupleTypeA, i, ord(m==mFieldPairs)) stmts.add(SemStmt(c, body)) closeScope(c.tab) Dec(c.p.nestedLoopCounter) var b = newNodeI(nkBreakStmt, n.info) b.add(ast.emptyNode) stmts.add(b) proc semForVars(c: PContext, n: PNode): PNode = result = n var length = sonsLen(n) var iter = skipTypes(n.sons[length-2].typ, {tyGenericInst}) # length == 3 means that there is one for loop variable # and thus no tuple unpacking: if iter.kind != tyTuple or length == 3: if length != 3: GlobalError(n.info, errWrongNumberOfVariables) var v = newSymS(skForVar, n.sons[0], c) # BUGFIX: don't use `iter` here as that would strip away # the ``tyGenericInst``! See ``tests/compile/tgeneric.nim`` # for an example: v.typ = n.sons[length-2].typ n.sons[0] = newSymNode(v) addDecl(c, v) else: if length-2 != sonsLen(iter): GlobalError(n.info, errWrongNumberOfVariables) for i in countup(0, length - 3): var v = newSymS(skForVar, n.sons[i], c) v.typ = iter.sons[i] n.sons[i] = newSymNode(v) addDecl(c, v) Inc(c.p.nestedLoopCounter) n.sons[length-1] = SemStmt(c, n.sons[length-1]) Dec(c.p.nestedLoopCounter) proc implicitIterator(c: PContext, it: string, arg: PNode): PNode = result = newNodeI(nkCall, arg.info) result.add(newIdentNode(it.getIdent, arg.info)) result.add(arg) result = semExprNoDeref(c, result, {efWantIterator}) proc semFor(c: PContext, n: PNode): PNode = result = n checkMinSonsLen(n, 3) var length = sonsLen(n) openScope(c.tab) n.sons[length-2] = semExprNoDeref(c, n.sons[length-2], {efWantIterator}) var call = n.sons[length-2] if call.kind notin nkCallKinds or call.sons[0].kind != nkSym or call.sons[0].sym.kind != skIterator: if length == 3: n.sons[length-2] = implicitIterator(c, "items", n.sons[length-2]) result = semForVars(c, n) elif length == 4: n.sons[length-2] = implicitIterator(c, "pairs", n.sons[length-2]) result = semForVars(c, n) else: GlobalError(n.sons[length - 2].info, errIteratorExpected) elif call.sons[0].sym.magic != mNone: result = semForFields(c, n, call.sons[0].sym.magic) else: result = semForVars(c, n) closeScope(c.tab) proc semRaise(c: PContext, n: PNode): PNode = result = n checkSonsLen(n, 1) if n.sons[0].kind != nkEmpty: n.sons[0] = semExprWithType(c, n.sons[0]) var typ = n.sons[0].typ if typ.kind != tyRef or typ.sons[0].kind != tyObject: localError(n.info, errExprCannotBeRaised) proc semTry(c: PContext, n: PNode): PNode = result = n checkMinSonsLen(n, 2) n.sons[0] = semStmtScope(c, n.sons[0]) var check = initIntSet() for i in countup(1, sonsLen(n) - 1): var a = n.sons[i] checkMinSonsLen(a, 1) var length = sonsLen(a) if a.kind == nkExceptBranch: if length == 2 and a.sons[0].kind == nkBracket: a.sons[0..0] = a.sons[0].sons length = a.sonsLen for j in countup(0, length - 2): var typ = semTypeNode(c, a.sons[j], nil) if typ.kind == tyRef: typ = typ.sons[0] if typ.kind != tyObject: GlobalError(a.sons[j].info, errExprCannotBeRaised) a.sons[j] = newNodeI(nkType, a.sons[j].info) a.sons[j].typ = typ if ContainsOrIncl(check, typ.id): localError(a.sons[j].info, errExceptionAlreadyHandled) elif a.kind != nkFinally: illFormedAst(n) # last child of an nkExcept/nkFinally branch is a statement: a.sons[length - 1] = semStmtScope(c, a.sons[length - 1]) proc addGenericParamListToScope(c: PContext, n: PNode) = if n.kind != nkGenericParams: InternalError(n.info, "addGenericParamListToScope") for i in countup(0, sonsLen(n)-1): var a = n.sons[i] if a.kind != nkSym: internalError(a.info, "addGenericParamListToScope") addDecl(c, a.sym) proc typeSectionLeftSidePass(c: PContext, n: PNode) = # process the symbols on the left side for the whole type section, before # we even look at the type definitions on the right for i in countup(0, sonsLen(n) - 1): var a = n.sons[i] if gCmd == cmdIdeTools: suggestStmt(c, a) if a.kind == nkCommentStmt: continue if a.kind != nkTypeDef: IllFormedAst(a) checkSonsLen(a, 3) var s = semIdentDef(c, a.sons[0], skType) s.typ = newTypeS(tyForward, c) s.typ.sym = s # process pragmas: if a.sons[0].kind == nkPragmaExpr: pragma(c, s, a.sons[0].sons[1], typePragmas) # add it here, so that recursive types are possible: addInterfaceDecl(c, s) a.sons[0] = newSymNode(s) proc typeSectionRightSidePass(c: PContext, n: PNode) = for i in countup(0, sonsLen(n) - 1): var a = n.sons[i] if a.kind == nkCommentStmt: continue if (a.kind != nkTypeDef): IllFormedAst(a) checkSonsLen(a, 3) if (a.sons[0].kind != nkSym): IllFormedAst(a) var s = a.sons[0].sym if s.magic == mNone and a.sons[2].kind == nkEmpty: GlobalError(a.info, errImplOfXexpected, s.name.s) if s.magic != mNone: processMagicType(c, s) if a.sons[1].kind != nkEmpty: # We have a generic type declaration here. In generic types, # symbol lookup needs to be done here. openScope(c.tab) pushOwner(s) if s.magic == mNone: s.typ.kind = tyGenericBody if s.typ.containerID != 0: InternalError(a.info, "semTypeSection: containerID") s.typ.containerID = s.typ.id # XXX for generic type aliases this is not correct! We need the # underlying Id really: # # type # TGObj[T] = object # TAlias[T] = TGObj[T] # a.sons[1] = semGenericParamList(c, a.sons[1], s.typ) s.typ.size = -1 # could not be computed properly # we fill it out later. For magic generics like 'seq', it won't be filled # so we use tyEmpty instead of nil to not crash for strange conversions # like: mydata.seq addSon(s.typ, newTypeS(tyEmpty, c)) s.ast = a inc c.InGenericContext var body = semTypeNode(c, a.sons[2], nil) dec c.InGenericContext if body != nil: body.sym = s body.size = -1 # could not be computed properly s.typ.sons[sonsLen(s.typ) - 1] = body popOwner() closeScope(c.tab) elif a.sons[2].kind != nkEmpty: # process the type's body: pushOwner(s) var t = semTypeNode(c, a.sons[2], s.typ) if s.typ == nil: s.typ = t elif t != s.typ: # this can happen for e.g. tcan_alias_specialised_generic: assignType(s.typ, t) #debug s.typ s.ast = a popOwner() proc typeSectionFinalPass(c: PContext, n: PNode) = for i in countup(0, sonsLen(n) - 1): var a = n.sons[i] if a.kind == nkCommentStmt: continue if a.sons[0].kind != nkSym: IllFormedAst(a) var s = a.sons[0].sym # compute the type's size and check for illegal recursions: if a.sons[1].kind == nkEmpty: if a.sons[2].kind in {nkSym, nkIdent, nkAccQuoted}: # type aliases are hard: #MessageOut('for type ' + typeToString(s.typ)); var t = semTypeNode(c, a.sons[2], nil) if t.kind in {tyObject, tyEnum}: assignType(s.typ, t) s.typ.id = t.id # same id checkConstructedType(s.info, s.typ) proc SemTypeSection(c: PContext, n: PNode): PNode = typeSectionLeftSidePass(c, n) typeSectionRightSidePass(c, n) typeSectionFinalPass(c, n) result = n proc semParamList(c: PContext, n, genericParams: PNode, s: PSym) = s.typ = semProcTypeNode(c, n, genericParams, nil) proc addParams(c: PContext, n: PNode) = for i in countup(1, sonsLen(n)-1): if (n.sons[i].kind != nkSym): InternalError(n.info, "addParams") addDecl(c, n.sons[i].sym) proc semBorrow(c: PContext, n: PNode, s: PSym) = # search for the correct alias: var b = SearchForBorrowProc(c, s, c.tab.tos - 2) if b != nil: # store the alias: n.sons[bodyPos] = newSymNode(b) else: LocalError(n.info, errNoSymbolToBorrowFromFound) proc addResult(c: PContext, t: PType, info: TLineInfo) = if t != nil: var s = newSym(skResult, getIdent"result", getCurrOwner()) s.info = info s.typ = t incl(s.flags, sfUsed) addDecl(c, s) c.p.resultSym = s proc addResultNode(c: PContext, n: PNode) = if c.p.resultSym != nil: addSon(n, newSymNode(c.p.resultSym)) proc semLambda(c: PContext, n: PNode): PNode = result = n checkSonsLen(n, bodyPos + 1) var s = newSym(skProc, getIdent":anonymous", getCurrOwner()) s.info = n.info s.ast = n n.sons[namePos] = newSymNode(s) pushOwner(s) openScope(c.tab) if n.sons[genericParamsPos].kind != nkEmpty: illFormedAst(n) # process parameters: if n.sons[paramsPos].kind != nkEmpty: semParamList(c, n.sons[ParamsPos], nil, s) # XXX: obsoleted - happens in semParamList # addParams(c, s.typ.n) ParamsTypeCheck(c, s.typ) else: s.typ = newTypeS(tyProc, c) addSon(s.typ, nil) if n.sons[pragmasPos].kind != nkEmpty: pragma(c, s, n.sons[pragmasPos], lambdaPragmas) s.options = gOptions if n.sons[bodyPos].kind != nkEmpty: if sfImportc in s.flags: LocalError(n.sons[bodyPos].info, errImplOfXNotAllowed, s.name.s) pushProcCon(c, s) addResult(c, s.typ.sons[0], n.info) n.sons[bodyPos] = semStmtScope(c, n.sons[bodyPos]) addResultNode(c, n) popProcCon(c) else: LocalError(n.info, errImplOfXexpected, s.name.s) sideEffectsCheck(c, s) closeScope(c.tab) # close scope for parameters popOwner() result.typ = s.typ proc semProcAux(c: PContext, n: PNode, kind: TSymKind, validPragmas: TSpecialWords): PNode = result = n checkSonsLen(n, bodyPos + 1) var s = semIdentDef(c, n.sons[0], kind) n.sons[namePos] = newSymNode(s) s.ast = n pushOwner(s) openScope(c.tab) var gp: PNode if n.sons[genericParamsPos].kind != nkEmpty: n.sons[genericParamsPos] = semGenericParamList(c, n.sons[genericParamsPos]) gp = n.sons[genericParamsPos] else: gp = newNodeI(nkGenericParams, n.info) # process parameters: if n.sons[paramsPos].kind != nkEmpty: semParamList(c, n.sons[ParamsPos], gp, s) if sonsLen(gp) > 0: if n.sons[genericParamsPos].kind == nkEmpty: # we have a list of implicit type parameters: n.sons[genericParamsPos] = gp # check for semantics again: semParamList(c, n.sons[ParamsPos], nil, s) # XXX: obsoleted - happens in semParamList # addParams(c, s.typ.n) else: s.typ = newTypeS(tyProc, c) addSon(s.typ, nil) var proto = SearchForProc(c, s, c.tab.tos-2) # -2 because we have a scope # open for parameters if proto == nil: s.typ.callConv = lastOptionEntry(c).defaultCC # add it here, so that recursive procs are possible: # -2 because we have a scope open for parameters if kind in OverloadableSyms: addInterfaceOverloadableSymAt(c, s, c.tab.tos - 2) else: addInterfaceDeclAt(c, s, c.tab.tos - 2) if n.sons[pragmasPos].kind != nkEmpty: pragma(c, s, n.sons[pragmasPos], validPragmas) else: if n.sons[pragmasPos].kind != nkEmpty: LocalError(n.sons[pragmasPos].info, errPragmaOnlyInHeaderOfProc) if sfForward notin proto.flags: LocalError(n.info, errAttemptToRedefine, proto.name.s) excl(proto.flags, sfForward) closeScope(c.tab) # close scope with wrong parameter symbols openScope(c.tab) # open scope for old (correct) parameter symbols if proto.ast.sons[genericParamsPos].kind != nkEmpty: addGenericParamListToScope(c, proto.ast.sons[genericParamsPos]) addParams(c, proto.typ.n) proto.info = s.info # more accurate line information s.typ = proto.typ s = proto n.sons[genericParamsPos] = proto.ast.sons[genericParamsPos] n.sons[paramsPos] = proto.ast.sons[paramsPos] if n.sons[namePos].kind != nkSym: InternalError(n.info, "semProcAux") n.sons[namePos].sym = proto proto.ast = n # needed for code generation popOwner() pushOwner(s) s.options = gOptions if n.sons[bodyPos].kind != nkEmpty: # for DLL generation it is annoying to check for sfImportc! if sfBorrow in s.flags: LocalError(n.sons[bodyPos].info, errImplOfXNotAllowed, s.name.s) if n.sons[genericParamsPos].kind == nkEmpty: ParamsTypeCheck(c, s.typ) pushProcCon(c, s) if s.typ.sons[0] != nil and kind != skIterator: addResult(c, s.typ.sons[0], n.info) if sfImportc notin s.flags: # no semantic checking for importc: n.sons[bodyPos] = semStmtScope(c, n.sons[bodyPos]) if s.typ.sons[0] != nil and kind != skIterator: addResultNode(c, n) popProcCon(c) else: if s.typ.sons[0] != nil and kind != skIterator: addDecl(c, newSym(skUnknown, getIdent"result", nil)) var toBind = initIntSet() n.sons[bodyPos] = semGenericStmtScope(c, n.sons[bodyPos], {}, toBind) fixupInstantiatedSymbols(c, s) if sfImportc in s.flags: # so we just ignore the body after semantic checking for importc: n.sons[bodyPos] = ast.emptyNode else: if proto != nil: LocalError(n.info, errImplOfXexpected, proto.name.s) if {sfImportc, sfBorrow} * s.flags == {} and s.magic == mNone: incl(s.flags, sfForward) elif sfBorrow in s.flags: semBorrow(c, n, s) sideEffectsCheck(c, s) closeScope(c.tab) # close scope for parameters popOwner() proc semIterator(c: PContext, n: PNode): PNode = result = semProcAux(c, n, skIterator, iteratorPragmas) var s = result.sons[namePos].sym var t = s.typ if t.sons[0] == nil: LocalError(n.info, errXNeedsReturnType, "iterator") if n.sons[bodyPos].kind == nkEmpty and s.magic == mNone: LocalError(n.info, errImplOfXexpected, s.name.s) proc semProc(c: PContext, n: PNode): PNode = result = semProcAux(c, n, skProc, procPragmas) proc semMethod(c: PContext, n: PNode): PNode = if not isTopLevel(c): LocalError(n.info, errXOnlyAtModuleScope, "method") result = semProcAux(c, n, skMethod, methodPragmas) var s = result.sons[namePos].sym var t = s.typ var hasObjParam = false for col in countup(1, sonsLen(t)-1): if skipTypes(t.sons[col], skipPtrs).kind == tyObject: hasObjParam = true break # XXX this not really correct way to do it: Perhaps it should be done after # generic instantiation. Well it's good enough for now: if not hasObjParam: LocalError(n.info, errXNeedsParamObjectType, "method") proc semConverterDef(c: PContext, n: PNode): PNode = if not isTopLevel(c): LocalError(n.info, errXOnlyAtModuleScope, "converter") checkSonsLen(n, bodyPos + 1) if n.sons[genericParamsPos].kind != nkEmpty: LocalError(n.info, errNoGenericParamsAllowedForX, "converter") result = semProcAux(c, n, skConverter, converterPragmas) var s = result.sons[namePos].sym var t = s.typ if t.sons[0] == nil: LocalError(n.info, errXNeedsReturnType, "converter") if sonsLen(t) != 2: LocalError(n.info, errXRequiresOneArgument, "converter") addConverter(c, s) proc semMacroDef(c: PContext, n: PNode): PNode = checkSonsLen(n, bodyPos + 1) if n.sons[genericParamsPos].kind != nkEmpty: LocalError(n.info, errNoGenericParamsAllowedForX, "macro") result = semProcAux(c, n, skMacro, macroPragmas) var s = result.sons[namePos].sym var t = s.typ if t.sons[0] == nil: LocalError(n.info, errXNeedsReturnType, "macro") if sonsLen(t) != 2: LocalError(n.info, errXRequiresOneArgument, "macro") if n.sons[bodyPos].kind == nkEmpty: LocalError(n.info, errImplOfXexpected, s.name.s) proc evalInclude(c: PContext, n: PNode): PNode = result = newNodeI(nkStmtList, n.info) addSon(result, n) for i in countup(0, sonsLen(n) - 1): var f = checkModuleName(n.sons[i]) var fileIndex = f.fileInfoIdx if ContainsOrIncl(c.includedFiles, fileIndex): GlobalError(n.info, errRecursiveDependencyX, f.extractFilename) addSon(result, semStmt(c, gIncludeFile(f))) Excl(c.includedFiles, fileIndex) proc setLine(n: PNode, info: TLineInfo) = for i in 0 .. <safeLen(n): setLine(n.sons[i], info) n.info = info proc semPragmaBlock(c: PContext, n: PNode): PNode = let pragmaList = n.sons[0] pragma(c, nil, pragmaList, exprPragmas) result = semStmt(c, n.sons[1]) for i in 0 .. <pragmaList.len: if whichPragma(pragmaList.sons[i]) == wLine: setLine(result, pragmaList.sons[i].info) proc SemStmt(c: PContext, n: PNode): PNode = const # must be last statements in a block: LastBlockStmts = {nkRaiseStmt, nkReturnStmt, nkBreakStmt, nkContinueStmt} result = n if gCmd == cmdIdeTools: suggestStmt(c, n) if nfSem in n.flags: return case n.kind of nkAsgn: result = semAsgn(c, n) of nkCall, nkInfix, nkPrefix, nkPostfix, nkCommand, nkMacroStmt, nkCallStrLit: result = semCommand(c, n) of nkEmpty, nkCommentStmt, nkNilLit: nil of nkBlockStmt: result = semBlock(c, n) of nkStmtList: var length = sonsLen(n) for i in countup(0, length - 1): n.sons[i] = semStmt(c, n.sons[i]) if n.sons[i].kind in LastBlockStmts: for j in countup(i + 1, length - 1): case n.sons[j].kind of nkPragma, nkCommentStmt, nkNilLit, nkEmpty: nil else: localError(n.sons[j].info, errStmtInvalidAfterReturn) of nkRaiseStmt: result = semRaise(c, n) of nkVarSection: result = semVarOrLet(c, n, skVar) of nkLetSection: result = semVarOrLet(c, n, skLet) of nkConstSection: result = semConst(c, n) of nkTypeSection: result = SemTypeSection(c, n) of nkIfStmt: result = SemIf(c, n) of nkWhenStmt: result = semWhen(c, n) of nkDiscardStmt: result = semDiscard(c, n) of nkWhileStmt: result = semWhile(c, n) of nkTryStmt: result = semTry(c, n) of nkBreakStmt, nkContinueStmt: result = semBreakOrContinue(c, n) of nkForStmt: result = semFor(c, n) of nkCaseStmt: result = semCase(c, n) of nkReturnStmt: result = semReturn(c, n) of nkAsmStmt: result = semAsm(c, n) of nkYieldStmt: result = semYield(c, n) of nkPragma: pragma(c, c.p.owner, n, stmtPragmas) of nkIteratorDef: result = semIterator(c, n) of nkProcDef: result = semProc(c, n) of nkMethodDef: result = semMethod(c, n) of nkConverterDef: result = semConverterDef(c, n) of nkMacroDef: result = semMacroDef(c, n) of nkTemplateDef: result = semTemplateDef(c, n) of nkImportStmt: if not isTopLevel(c): LocalError(n.info, errXOnlyAtModuleScope, "import") result = evalImport(c, n) of nkFromStmt: if not isTopLevel(c): LocalError(n.info, errXOnlyAtModuleScope, "from") result = evalFrom(c, n) of nkIncludeStmt: if not isTopLevel(c): LocalError(n.info, errXOnlyAtModuleScope, "include") result = evalInclude(c, n) of nkPragmaBlock: result = semPragmaBlock(c, n) else: # in interactive mode, we embed the expression in an 'echo': if gCmd == cmdInteractive: result = buildEchoStmt(c, semExpr(c, n)) else: LocalError(n.info, errStmtExpected) result = ast.emptyNode if result == nil: InternalError(n.info, "SemStmt: result = nil") incl(result.flags, nfSem) proc semStmtScope(c: PContext, n: PNode): PNode = openScope(c.tab) result = semStmt(c, n) closeScope(c.tab)