Skip to content

Commit

Permalink
Stage 1 for a workaround for ooc-lang#239/ooc-lang#311: A warning for…
Browse files Browse the repository at this point in the history
… varargs-inside-ternary-expressions-or-boolean-binaryops.
  • Loading branch information
fredreichbier committed Sep 3, 2011
1 parent d8af9d8 commit 6347c02
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion source/rock/middle/FunctionCall.ooc
Expand Up @@ -4,7 +4,7 @@ import Visitor, Expression, FunctionDecl, Argument, Type, VariableAccess,
TypeDecl, Node, VariableDecl, AddressOf, CommaSequence, BinaryOp,
InterfaceDecl, Cast, NamespaceDecl, BaseType, FuncType, Return,
TypeList, Scope, Block, InlineContext, StructLiteral, NullLiteral,
IntLiteral
IntLiteral, Ternary
import tinker/[Response, Resolver, Trail, Errors]

/**
Expand Down Expand Up @@ -816,6 +816,24 @@ FunctionCall: class extends Expression {
Response OK
}

/** print an appropriate warning if the user tries to use vararg functions in binary/ternary expressions.
See bug #311 for details. */
printVarargExpressionWarning: func (trail: Trail) {
i := trail getSize() - 1
while(i >= 0) {
node := trail data get(i) as Node
// boolean binary ops and ternary ops are the problem!
if((node instanceOf?(BinaryOp) && node as BinaryOp isBooleanOp()) \
|| node instanceOf?(Ternary)) {
token formatMessage("Found a vararg function call inside a binary/ternary expression. Please unwrap this expression. See https://github.com/nddrylliog/rock/issues/311 for details", "WARNING") println()
} else if(node instanceOf?(Scope)) {
// we're not part of the same expression anymore!
break;
}
i -= 1
}
}

/**
* Resolve ooc variable arguments
*/
Expand All @@ -826,6 +844,7 @@ FunctionCall: class extends Expression {
match (lastArg := ref args last()) {
case vararg: VarArg =>
if(vararg name != null) {
// ooc varargs have names!
numVarArgs := (args size - (ref args size - 1))

if(!args empty?()) {
Expand Down Expand Up @@ -874,6 +893,8 @@ FunctionCall: class extends Expression {
args removeAt(args lastIndex())
)
args add(VariableAccess new(vaDecl, token))
// print a warning if needed
printVarargExpressionWarning(trail)
}
}

Expand Down

0 comments on commit 6347c02

Please sign in to comment.