# # # The Nimrod Compiler # (c) Copyright 2010 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # # this module does the semantic checking of statements proc semExprNoType(c: PContext, n: PNode): PNode = result = semExpr(c, n) if result.typ != nil and result.typ.kind != tyStmt: liMessage(n.info, errDiscardValue) proc semWhen(c: PContext, n: PNode): PNode = result = nil for i in countup(0, sonsLen(n) - 1): var it = n.sons[i] if it == nil: illFormedAst(n) case it.kind of nkElifBranch: checkSonsLen(it, 2) var e = semConstExpr(c, it.sons[0]) checkBool(e) if (e.kind != nkIntLit): InternalError(n.info, "semWhen") if (e.intVal != 0) and (result == nil): result = semStmt(c, it.sons[1]) # do not open a new scope! of nkElse: checkSonsLen(it, 1) if result == nil: result = semStmt(c, it.sons[0]) # do not open a new scope! else: illFormedAst(n) if result == nil: result = newNodeI(nkNilLit, n.info) # The ``when`` statement implements the mechanism for platform dependant # code. Thus we try to ensure here consistent ID allocation after the # ``when`` statement. IDsynchronizationPoint(200) proc semIf(c: PContext, n: PNode): PNode = result = n for i in countup(0, sonsLen(n) - 1): var it = n.sons[i] if it == nil: illFormedAst(n) case it.kind of nkElifBranch: checkSonsLen(it, 2) openScope(c.tab) it.sons[0] = semExprWithType(c, it.sons[0]) checkBool(it.sons[0]) it.sons[1] = semStmt(c, it.sons[1]) closeScope(c.tab) of nkElse: if sonsLen(it) == 1: it.sons[0] = semStmtScope(c, it.sons[0]) else: illFormedAst(it) else: illFormedAst(n) proc semDiscard(c: PContext, n: PNode): PNode = result = n checkSonsLen(n, 1) n.sons[0] = semExprWithType(c, n.sons[0]) if n.sons[0].typ == nil: liMessage(n.info, errInvalidDiscard) proc semBreakOrContinue(c: PContext, n: PNode): PNode = result = n checkSonsLen(n, 1) if n.sons[0] != nil: var s: PSym case n.sons[0].kind of nkIdent: s = lookUp(c, n.sons[0]) of nkSym: s = n.sons[0].sym else: illFormedAst(n) if (s.kind == skLabel) and (s.owner.id == c.p.owner.id): var x = newSymNode(s) x.info = n.info incl(s.flags, sfUsed) n.sons[0] = x else: liMessage(n.info, errInvalidControlFlowX, s.name.s) elif (c.p.nestedLoopCounter <= 0) and (c.p.nestedBlockCounter <= 0): liMessage(n.info, errInvalidControlFlowX, renderTree(n, {renderNoComments})) proc semBlock(c: PContext, n: PNode): PNode = result = n Inc(c.p.nestedBlockCounter) checkSonsLen(n, 2) openScope(c.tab) # BUGFIX: label is in the scope of block! if n.sons[0] != nil: var labl = newSymS(skLabel, n.sons[0], c) addDecl(c, labl) n.sons[0] = newSymNode(labl) # BUGFIX n.sons[1] = semStmt(c, n.sons[1]) closeScope(c.tab) Dec(c.p.nestedBlockCounter) proc semAsm(con: PContext, n: PNode): PNode = result = n checkSonsLen(n, 2) var marker = pragmaAsm(con, n.sons[0]) if marker == '\0': marker = '`' # default marker case n.sons[1].kind of nkStrLit, nkRStrLit, nkTripleStrLit: result = copyNode(n) var str = n.sons[1].strVal if str == "": liMessage(n.info, errEmptyAsm) # now parse the string literal and substitute symbols: var a = 0 while true: var b = strutils.find(str, marker, a) var sub = if b < 0: copy(str, a) else: copy(str, a, b - 1) if sub != "": addSon(result, newStrNode(nkStrLit, sub)) if b < 0: break var c = strutils.find(str, marker, b + 1) if c < 0: sub = copy(str, b + 1) else: sub = copy(str, b + 1, c - 1) if sub != "": var e = SymtabGet(con.tab, getIdent(sub)) if e != nil: if e.kind == skStub: loadStub(e) addSon(result, newSymNode(e)) else: addSon(result, newStrNode(nkStrLit, sub)) if c < 0: break a = c + 1 else: illFormedAst(n) proc semWhile(c: PContext, n: PNode): PNode = result = n checkSonsLen(n, 2) openScope(c.tab) n.sons[0] = semExprWithType(c, n.sons[0]) CheckBool(n.sons[0]) inc(c.p.nestedLoopCounter) n.sons[1] = semStmt(c, n.sons[1]) dec(c.p.nestedLoopCounter) closeScope(c.tab) proc semCase(c: PContext, n: PNode): PNode = # check selector: result = n checkMinSonsLen(n, 2) openScope(c.tab) n.sons[0] = semExprWithType(c, n.sons[0]) var chckCovered = false var covered: biggestint = 0 case skipTypes(n.sons[0].Typ, abstractVarRange).Kind of tyInt..tyInt64, tyChar, tyEnum: chckCovered = true of tyFloat..tyFloat128, tyString: nil else: liMessage(n.info, errSelectorMustBeOfCertainTypes) for i in countup(1, sonsLen(n) - 1): var
# gridwm - grid window manager
#   (C)opyright MMVI Anselm R. Garbe

include config.mk

WMSRC = bar.c client.c draw.c event.c util.c wm.c
WMOBJ = ${WMSRC:.c=.o}
MENSRC = menu.c draw.c util.c
MENOBJ = ${MENSRC:.c=.o}
MAN1 = gridwm.1 gridmenu.1
BIN = gridwm gridmenu     

all: config gridwm gridmenu
	@echo finished

config:
	@echo gridwm build options:
	@echo "LIBS     = ${LIBS}"
	@echo "CFLAGS   = ${CFLAGS}"
	@echo "LDFLAGS  = ${LDFLAGS}"
	@echo "CC       = ${CC}"

.c.o:
	@echo CC $<
	@${CC} -c ${CFLAGS} $<

${WMOBJ}: wm.h draw.h config.h util.h

gridmenu: ${MENOBJ}
	@echo LD $@
	@${CC} -o $@ ${MENOBJ} ${LDFLAGS}

gridwm: ${WMOBJ}
	@echo LD $@
	@${CC} -o $@ ${WMOBJ} ${LDFLAGS}

clean:
	rm -f gridwm gridmenu *.o core

dist: clean
	mkdir -p gridwm-${VERSION}
	cp -R Makefile README LICENSE config.mk *.h *.c ${MAN} gridwm-${VERSION}
	tar -cf gridwm-${VERSION}.tar gridwm-${VERSION}
	gzip gridwm-${VERSION}.tar
	rm -rf gridwm-${VERSION}

install: all
	@mkdir -p ${DESTDIR}${PREFIX}/bin
	@cp -f ${BIN} ${DESTDIR}${PREFIX}/bin
	@echo installed executable files to ${DESTDIR}${PREFIX}/bin
	@mkdir -p ${DESTDIR}${MANPREFIX}/man1
	@cp -f ${MAN1} ${DESTDIR}${MANPREFIX}/man1
	@echo installed manual pages to ${DESTDIR}${MANPREFIX}/man1

uninstall:
	for i in ${BIN}; do \
		rm -f ${DESTDIR}${PREFIX}/bin/`basename $$i`; \
	done
	for i in ${MAN1}; do \
		rm -f ${DESTDIR}${MANPREFIX}/man1/`basename $$i`; \
	done
or the correct alias: var b = SearchForBorrowProc(c, s, c.tab.tos - 2) if b == nil: liMessage(n.info, errNoSymbolToBorrowFromFound) # store the alias: n.sons[codePos] = newSymNode(b) proc sideEffectsCheck(c: PContext, s: PSym) = if {sfNoSideEffect, sfSideEffect} * s.flags == {sfNoSideEffect, sfSideEffect}: liMessage(s.info, errXhasSideEffects, s.name.s) proc addResult(c: PContext, t: PType, info: TLineInfo) = if t != nil: var s = newSym(skVar, getIdent("result"), getCurrOwner()) s.info = info s.typ = t incl(s.flags, sfResult) 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, codePos + 1) var s = newSym(skProc, getIdent(":anonymous"), getCurrOwner()) s.info = n.info var oldP = c.p # restore later s.ast = n n.sons[namePos] = newSymNode(s) pushOwner(s) openScope(c.tab) if (n.sons[genericParamsPos] != nil): illFormedAst(n) # process parameters: if n.sons[paramsPos] != nil: semParamList(c, n.sons[ParamsPos], nil, s) addParams(c, s.typ.n) else: s.typ = newTypeS(tyProc, c) addSon(s.typ, nil) s.typ.callConv = ccClosure if n.sons[pragmasPos] != nil: pragma(c, s, n.sons[pragmasPos], lambdaPragmas) s.options = gOptions if n.sons[codePos] != nil: if sfImportc in s.flags: liMessage(n.sons[codePos].info, errImplOfXNotAllowed, s.name.s) c.p = newProcCon(s) addResult(c, s.typ.sons[0], n.info) n.sons[codePos] = semStmtScope(c, n.sons[codePos]) addResultNode(c, n) else: liMessage(n.info, errImplOfXexpected, s.name.s) closeScope(c.tab) # close scope for parameters popOwner() c.p = oldP # restore result.typ = s.typ proc semProcAux(c: PContext, n: PNode, kind: TSymKind, validPragmas: TSpecialWords): PNode = var s, proto: PSym gp: PNode result = n checkSonsLen(n, codePos + 1) if c.p.owner.kind == skModule: s = semIdentVis(c, kind, n.sons[0], {sfStar}) incl(s.flags, sfGlobal) else: s = semIdentVis(c, kind, n.sons[0], {}) n.sons[namePos] = newSymNode(s) var oldP = c.p # restore later if sfStar in s.flags: incl(s.flags, sfInInterface) s.ast = n pushOwner(s) openScope(c.tab) if n.sons[genericParamsPos] != nil: n.sons[genericParamsPos] = semGenericParamList(c, n.sons[genericParamsPos]) gp = n.sons[genericParamsPos] else: gp = newNodeI(nkGenericParams, n.info) # process parameters: if n.sons[paramsPos] != nil: semParamList(c, n.sons[ParamsPos], gp, s) if sonsLen(gp) > 0: n.sons[genericParamsPos] = gp addParams(c, s.typ.n) else: s.typ = newTypeS(tyProc, c) addSon(s.typ, nil) proto = SearchForProc(c, s, c.tab.tos - 2) # -2 because we have a scope open # for parameters if proto == nil: if oldP.owner.kind != skModule: s.typ.callConv = ccClosure else: 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] != nil: pragma(c, s, n.sons[pragmasPos], validPragmas) else: if n.sons[pragmasPos] != nil: liMessage(n.sons[pragmasPos].info, errPragmaOnlyInHeaderOfProc) if not (sfForward in proto.flags): liMessage(n.info, errAttemptToRedefineX, 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] != nil: 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[codePos] != nil: if {sfImportc, sfBorrow} * s.flags != {}: liMessage(n.sons[codePos].info, errImplOfXNotAllowed, s.name.s) if (n.sons[genericParamsPos] == nil): c.p = newProcCon(s) if (s.typ.sons[0] != nil) and (kind != skIterator): addResult(c, s.typ.sons[0], n.info) n.sons[codePos] = semStmtScope(c, n.sons[codePos]) if (s.typ.sons[0] != nil) and (kind != skIterator): addResultNode(c, n) else: if (s.typ.sons[0] != nil) and (kind != skIterator): addDecl(c, newSym(skUnknown, getIdent("result"), nil)) n.sons[codePos] = semGenericStmtScope(c, n.sons[codePos]) else: if proto != nil: liMessage(n.info, errImplOfXexpected, proto.name.s) if {sfImportc, sfBorrow} * s.flags == {}: incl(s.flags, sfForward) elif sfBorrow in s.flags: semBorrow(c, n, s) sideEffectsCheck(c, s) closeScope(c.tab) # close scope for parameters popOwner() c.p = oldP # restore 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: liMessage(n.info, errXNeedsReturnType, "iterator") if n.sons[codePos] == nil: liMessage(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): liMessage(n.info, errXOnlyAtModuleScope, "method") result = semProcAux(c, n, skMethod, methodPragmas) proc semConverterDef(c: PContext, n: PNode): PNode = if not isTopLevel(c): liMessage(n.info, errXOnlyAtModuleScope, "converter") checkSonsLen(n, codePos + 1) if n.sons[genericParamsPos] != nil: liMessage(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: liMessage(n.info, errXNeedsReturnType, "converter") if sonsLen(t) != 2: liMessage(n.info, errXRequiresOneArgument, "converter") addConverter(c, s) proc semMacroDef(c: PContext, n: PNode): PNode = checkSonsLen(n, codePos + 1) if n.sons[genericParamsPos] != nil: liMessage(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: liMessage(n.info, errXNeedsReturnType, "macro") if sonsLen(t) != 2: liMessage(n.info, errXRequiresOneArgument, "macro") if n.sons[codePos] == nil: liMessage(n.info, errImplOfXexpected, s.name.s) proc evalInclude(c: PContext, n: PNode): PNode = result = newNodeI(nkStmtList, n.info) addSon(result, n) # the rodwriter needs include information! for i in countup(0, sonsLen(n) - 1): var f = getModuleFile(n.sons[i]) var fileIndex = includeFilename(f) if IntSetContainsOrIncl(c.includedFiles, fileIndex): liMessage(n.info, errRecursiveDependencyX, f) addSon(result, semStmt(c, gIncludeFile(f))) IntSetExcl(c.includedFiles, fileIndex) proc semCommand(c: PContext, n: PNode): PNode = result = semExprNoType(c, n) proc SemStmt(c: PContext, n: PNode): PNode = const # must be last statements in a block: LastBlockStmts = {nkRaiseStmt, nkReturnStmt, nkBreakStmt, nkContinueStmt} result = n if n == nil: return 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: liMessage(n.sons[j].info, errStmtInvalidAfterReturn) of nkRaiseStmt: result = semRaise(c, n) of nkVarSection: result = semVar(c, n) 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): liMessage(n.info, errXOnlyAtModuleScope, "import") result = evalImport(c, n) of nkFromStmt: if not isTopLevel(c): liMessage(n.info, errXOnlyAtModuleScope, "from") result = evalFrom(c, n) of nkIncludeStmt: if not isTopLevel(c): liMessage(n.info, errXOnlyAtModuleScope, "include") result = evalInclude(c, n) else: liMessage(n.info, errStmtExpected) 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)