1*91e13788Srillig# $NetBSD: varmod-loop.mk,v 1.2 2020/08/16 12:30:45 rillig Exp $
21fb97e4bSrillig#
3*91e13788Srillig# Tests for the :@var@...${var}...@ variable modifier.
41fb97e4bSrillig
5*91e13788Srilligall: mod-loop-varname
6*91e13788Srilligall: mod-loop-resolve
7*91e13788Srilligall: mod-loop-varname-dollar
8*91e13788Srilligall: mod-loop-dollar
91fb97e4bSrillig
10*91e13788Srillig# In the :@ modifier, the name of the loop variable can even be generated
11*91e13788Srillig# dynamically.  There's no practical use-case for this, and hopefully nobody
12*91e13788Srillig# will ever depend on this, but technically it's possible.
13*91e13788Srillig# Therefore, in -dL mode, this is forbidden, see lint.mk.
14*91e13788Srilligmod-loop-varname:
15*91e13788Srillig	@echo :${:Uone two three:@${:Ubar:S,b,v,}@+${var}+@:Q}:
16*91e13788Srillig	# ":::" is a very creative variable name, unlikely in practice
17*91e13788Srillig	# The expression ${\:\:\:} would not work since backslashes can only
18*91e13788Srillig	# be escaped in the modifiers, but not in the variable name.
19*91e13788Srillig	@echo :${:U1 2 3:@:::@x${${:U\:\:\:}}y@}:
20*91e13788Srillig	# "@@" is another creative variable name.
21*91e13788Srillig	@echo :${:U1 2 3:@\@\@@x${@@}y@}:
22*91e13788Srillig	# Even "@" works as a variable name since the variable is installed
23*91e13788Srillig	# in the "current" scope, which in this case is the one from the
24*91e13788Srillig	# target.
25*91e13788Srillig	@echo :$@: :${:U1 2 3:@\@@x${@}y@}: :$@:
26*91e13788Srillig	# In extreme cases, even the backslash can be used as variable name.
27*91e13788Srillig	# It needs to be doubled though.
28*91e13788Srillig	@echo :${:U1 2 3:@\\@x${${:Ux:S,x,\\,}}y@}:
29*91e13788Srillig
30*91e13788Srillig# The :@ modifier resolves the variables a little more often than expected.
31*91e13788Srillig# In particular, it resolves _all_ variables from the context, and not only
32*91e13788Srillig# the loop variable (in this case v).
33*91e13788Srillig#
34*91e13788Srillig# The d means direct reference, the i means indirect reference.
35*91e13788SrilligRESOLVE=	${RES1} $${RES1}
36*91e13788SrilligRES1=		1d${RES2} 1i$${RES2}
37*91e13788SrilligRES2=		2d${RES3} 2i$${RES3}
38*91e13788SrilligRES3=		3
39*91e13788Srillig
40*91e13788Srilligmod-loop-resolve:
41*91e13788Srillig	@echo $@:${RESOLVE:@v@w${v}w@:Q}:
42*91e13788Srillig
43*91e13788Srillig# Until 2020-07-20, the variable name of the :@ modifier could end with one
44*91e13788Srillig# or two dollar signs, which were silently ignored.
45*91e13788Srillig# There's no point in allowing a dollar sign in that position.
46*91e13788Srilligmod-loop-varname-dollar:
47*91e13788Srillig	@echo $@:${1 2 3:L:@v$@($v)@:Q}.
48*91e13788Srillig	@echo $@:${1 2 3:L:@v$$@($v)@:Q}.
49*91e13788Srillig	@echo $@:${1 2 3:L:@v$$$@($v)@:Q}.
50*91e13788Srillig
51*91e13788Srillig# Demonstrate that it is possible to generate dollar characters using the
52*91e13788Srillig# :@ modifier.
53*91e13788Srillig#
54*91e13788Srillig# These are edge cases that could have resulted in a parse error as well
55*91e13788Srillig# since the $@ at the end could have been interpreted as a variable, which
56*91e13788Srillig# would mean a missing closing @ delimiter.
57*91e13788Srilligmod-loop-dollar:
58*91e13788Srillig	@echo $@:${:U1:@word@${word}$@:Q}:
59*91e13788Srillig	@echo $@:${:U2:@word@$${word}$$@:Q}:
60*91e13788Srillig	@echo $@:${:U3:@word@$$${word}$$$@:Q}:
61*91e13788Srillig	@echo $@:${:U4:@word@$$$${word}$$$$@:Q}:
62*91e13788Srillig	@echo $@:${:U5:@word@$$$$${word}$$$$$@:Q}:
63*91e13788Srillig	@echo $@:${:U6:@word@$$$$$${word}$$$$$$@:Q}:
64