OSDN Git Service

Update to current version of Go library (revision 94d654be2064).
[pf3gnuchains/gcc-fork.git] / libgo / go / go / printer / testdata / expressions.golden
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package expressions
6
7 type T struct {
8         x, y, z int
9 }
10
11 var (
12         a, b, c, d, e                                           int
13         under_bar                                               int
14         longIdentifier1, longIdentifier2, longIdentifier3       int
15         t0, t1, t2                                              T
16         s                                                       string
17         p                                                       *int
18 )
19
20
21 func _() {
22         // no spaces around simple or parenthesized expressions
23         _ = (a + 0)
24         _ = a + b
25         _ = a + b + c
26         _ = a + b - c
27         _ = a - b - c
28         _ = a + (b * c)
29         _ = a + (b / c)
30         _ = a - (b % c)
31         _ = 1 + a
32         _ = a + 1
33         _ = a + b + 1
34         _ = s[a]
35         _ = s[a:]
36         _ = s[:b]
37         _ = s[1:2]
38         _ = s[a:b]
39         _ = s[0:len(s)]
40         _ = s[0] << 1
41         _ = (s[0] << 1) & 0xf
42         _ = s[0]<<2 | s[1]>>4
43         _ = "foo" + s
44         _ = s + "foo"
45         _ = 'a' + 'b'
46         _ = len(s) / 2
47         _ = len(t0.x) / a
48
49         // spaces around expressions of different precedence or expressions containing spaces
50         _ = a + -b
51         _ = a - ^b
52         _ = a / *p
53         _ = a + b*c
54         _ = 1 + b*c
55         _ = a + 2*c
56         _ = a + c*2
57         _ = 1 + 2*3
58         _ = s[1 : 2*3]
59         _ = s[a : b-c]
60         _ = s[0:]
61         _ = s[a+b]
62         _ = s[:b-c]
63         _ = s[a+b:]
64         _ = a[a<<b+1]
65         _ = a[a<<b+1:]
66         _ = s[a+b : len(s)]
67         _ = s[len(s):-a]
68         _ = s[a : len(s)+1]
69         _ = s[a:len(s)+1] + s
70
71         // spaces around operators with equal or lower precedence than comparisons
72         _ = a == b
73         _ = a != b
74         _ = a > b
75         _ = a >= b
76         _ = a < b
77         _ = a <= b
78         _ = a < b && c > d
79         _ = a < b || c > d
80
81         // spaces around "long" operands
82         _ = a + longIdentifier1
83         _ = longIdentifier1 + a
84         _ = longIdentifier1 + longIdentifier2*longIdentifier3
85         _ = s + "a longer string"
86
87         // some selected cases
88         _ = a + t0.x
89         _ = a + t0.x + t1.x*t2.x
90         _ = a + b + c + d + e + 2*3
91         _ = a + b + c + 2*3 + d + e
92         _ = (a + b + c) * 2
93         _ = a - b + c - d + (a + b + c) + d&e
94         _ = under_bar - 1
95         _ = Open(dpath+"/file", O_WRONLY|O_CREAT, 0666)
96         _ = int(c0&_Mask4)<<18 | int(c1&_Maskx)<<12 | int(c2&_Maskx)<<6 | int(c3&_Maskx)
97 }
98
99
100 func _() {
101         a + b
102         a + b + c
103         a + b*c
104         a + (b * c)
105         (a + b) * c
106         a + (b * c * d)
107         a + (b*c + d)
108
109         1 << x
110         -1 << x
111         1<<x - 1
112         -1<<x - 1
113
114         f(a + b)
115         f(a + b + c)
116         f(a + b*c)
117         f(a + (b * c))
118         f(1<<x-1, 1<<x-2)
119
120         1<<d.logWindowSize - 1
121
122         buf = make(x, 2*cap(b.buf)+n)
123
124         dst[i*3+2] = dbuf[0] << 2
125         dst[i*3+2] = dbuf[0]<<2 | dbuf[1]>>4
126
127         b.buf = b.buf[0 : b.off+m+n]
128         b.buf = b.buf[0 : b.off+m*n]
129         f(b.buf[0 : b.off+m+n])
130
131         signed += ' ' * 8
132         tw.octal(header[148:155], chksum)
133
134         x > 0 && i >= 0
135
136         x1, x0 := x>>w2, x&m2
137         z0 = t1<<w2 + t0
138         z1 = (t1 + t0>>w2) >> w2
139         q1, r1 := x1/d1, x1%d1
140         r1 = r1*b2 | x0>>w2
141         x1 = (x1 << z) | (x0 >> (uint(w) - z))
142         x1 = x1<<z | x0>>(uint(w)-z)
143
144         buf[0 : len(buf)+1]
145         buf[0 : n+1]
146
147         a, b = b, a
148         a = b + c
149         a = b*c + d
150         a*b + c
151         a - b - c
152         a - (b - c)
153         a - b*c
154         a - (b * c)
155         a * b / c
156         a / *b
157         x[a|^b]
158         x[a / *b]
159         a & ^b
160         a + +b
161         a - -b
162         x[a*-b]
163         x[a + +b]
164         x ^ y ^ z
165         b[a>>24] ^ b[(a>>16)&0xFF] ^ b[(a>>8)&0xFF] ^ b[a&0xFF]
166         len(longVariableName) * 2
167
168         token(matchType + xlength<<lengthShift + xoffset)
169 }
170
171
172 func f(x int, args ...int) {
173         f(0, args...)
174         f(1, args)
175         f(2, args[0])
176
177         // make sure syntactically legal code remains syntactically legal
178         f(3, 42 ...)    // a blank must remain between 42 and ...
179         f(4, 42....)
180         f(5, 42....)
181         f(6, 42.0...)
182         f(7, 42.0...)
183         f(8, .42...)
184         f(9, .42...)
185         f(10, 42e0...)
186         f(11, 42e0...)
187
188         _ = 42 .x       // a blank must remain between 42 and .x
189         _ = 42..x
190         _ = 42..x
191         _ = 42.0.x
192         _ = 42.0.x
193         _ = .42.x
194         _ = .42.x
195         _ = 42e0.x
196         _ = 42e0.x
197
198         // a blank must remain between the binary operator and the 2nd operand
199         _ = x / *y
200         _ = x < -1
201         _ = x < <-1
202         _ = x + +1
203         _ = x - -1
204         _ = x & &x
205         _ = x & ^x
206
207         _ = f(x / *y, x < -1, x < <-1, x + +1, x - -1, x & &x, x & ^x)
208 }
209
210
211 func _() {
212         _ = T{}
213         _ = struct{}{}
214         _ = [10]T{}
215         _ = [...]T{}
216         _ = []T{}
217         _ = map[int]T{}
218 }
219
220
221 // one-line structs/interfaces in composite literals (up to a threshold)
222 func _() {
223         _ = struct{}{}
224         _ = struct{ x int }{0}
225         _ = struct{ x, y, z int }{0, 1, 2}
226         _ = struct{ int }{0}
227         _ = struct {
228                 s struct {
229                         int
230                 }
231         }{struct{ int }{0}}     // compositeLit context not propagated => multiLine result
232 }
233
234
235 func _() {
236         // do not modify literals
237         _ = "tab1       tab2    tab3    end"    // string contains 3 tabs
238         _ = "tab1 tab2 tab3 end"        // same string with 3 blanks - may be unaligned because editors see tabs in strings
239         _ = ""                          // this comment should be aligned with the one on the previous line
240         _ = ``
241         _ = `
242 `
243         _ = `foo
244                 bar`
245         _ = `three spaces before the end of the line starting here:   
246 they must not be removed`
247 }
248
249
250 func _() {
251         // smart handling of indentation for multi-line raw strings
252         var _ = ``
253         var _ = `foo`
254         var _ = `foo
255 bar`
256
257         var _ = ``
258         var _ = `foo`
259         var _ =
260         // the next line should not be indented
261 `foo
262 bar`
263
264         var _ = // comment
265         ``
266         var _ = // comment
267         `foo`
268         var _ = // comment
269         // the next line should not be indented
270 `foo
271 bar`
272
273         var _ = /* comment */ ``
274         var _ = /* comment */ `foo`
275         var _ = /* comment */ `foo
276 bar`
277
278         var _ = /* comment */
279         ``
280         var _ = /* comment */
281         `foo`
282         var _ = /* comment */
283         // the next line should not be indented
284 `foo
285 bar`
286
287         var board = []int(
288 `...........
289 ...........
290 ....●●●....
291 ....●●●....
292 ..●●●●●●●..
293 ..●●●○●●●..
294 ..●●●●●●●..
295 ....●●●....
296 ....●●●....
297 ...........
298 ...........
299 `)
300
301         var state = S{
302                 "foo",
303                 // the next line should not be indented
304 `...........
305 ...........
306 ....●●●....
307 ....●●●....
308 ..●●●●●●●..
309 ..●●●○●●●..
310 ..●●●●●●●..
311 ....●●●....
312 ....●●●....
313 ...........
314 ...........
315 `,
316                 "bar",
317         }
318 }
319
320
321 func _() {
322         // one-line function literals (body is on a single line)
323         _ = func() {}
324         _ = func() int { return 0 }
325         _ = func(x, y int) bool { m := (x + y) / 2; return m < 0 }
326
327         // multi-line function literals (body is not on one line)
328         _ = func() {
329         }
330         _ = func() int {
331                 return 0
332         }
333         _ = func(x, y int) bool {
334                 m := (x + y) / 2
335                 return x < y
336         }
337
338         f(func() {
339         })
340         f(func() int {
341                 return 0
342         })
343         f(func(x, y int) bool {
344                 m := (x + y) / 2
345                 return x < y
346         })
347 }
348
349
350 func _() {
351         _ = [][]int{
352                 []int{1},
353                 []int{1, 2},
354                 []int{1, 2, 3},
355         }
356         _ = [][]int{
357                 {1},
358                 []int{1, 2},
359                 []int{1, 2, 3},
360         }
361         _ = [][]int{
362                 {1},
363                 {1, 2},
364                 {1, 2, 3},
365         }
366         _ = [][]int{{1}, {1, 2}, {1, 2, 3}}
367 }
368
369
370 // various multi-line expressions
371 func _() {
372         // do not add extra indentation to multi-line string lists
373         _ = "foo" + "bar"
374         _ = "foo" +
375                 "bar" +
376                 "bah"
377         _ = []string{
378                 "abc" +
379                         "def",
380                 "foo" +
381                         "bar",
382         }
383 }
384
385
386 const _ = F1 +
387         `string = "%s";` +
388         `ptr = *;` +
389         `datafmt.T2 = s ["-" p "-"];`
390
391
392 const _ = `datafmt "datafmt";` +
393         `default = "%v";` +
394         `array = *;` +
395         `datafmt.T3 = s  {" " a a / ","};`
396
397
398 const _ = `datafmt "datafmt";` +
399         `default = "%v";` +
400         `array = *;` +
401         `datafmt.T3 = s  {" " a a / ","};`
402
403
404 func _() {
405         _ = F1 +
406                 `string = "%s";` +
407                 `ptr = *;` +
408                 `datafmt.T2 = s ["-" p "-"];`
409
410         _ =
411                 `datafmt "datafmt";` +
412                         `default = "%v";` +
413                         `array = *;` +
414                         `datafmt.T3 = s  {" " a a / ","};`
415
416         _ = `datafmt "datafmt";` +
417                 `default = "%v";` +
418                 `array = *;` +
419                 `datafmt.T3 = s  {" " a a / ","};`
420 }
421
422
423 func _() {
424         // respect source lines in multi-line expressions
425         _ = a +
426                 b +
427                 c
428         _ = a < b ||
429                 b < a
430         _ = "933262154439441526816992388562667004907159682643816214685929" +
431                 "638952175999932299156089414639761565182862536979208272237582" +
432                 "51185210916864000000000000000000000000"        // 100!
433         _ = "170141183460469231731687303715884105727"   // prime
434 }
435
436
437 // Alignment after overlong lines
438 const (
439         _       = "991"
440         _       = "2432902008176640000" // 20!
441         _       = "933262154439441526816992388562667004907159682643816214685929" +
442                 "638952175999932299156089414639761565182862536979208272237582" +
443                 "51185210916864000000000000000000000000"        // 100!
444         _       = "170141183460469231731687303715884105727"     // prime
445 )
446
447
448 // Correct placement of operators and comments in multi-line expressions
449 func _() {
450         _ = a + // comment
451                 b +     // comment
452                 c
453         _ = "a" +
454                 "b" +   // comment
455                 "c"
456         _ = "ba0408" + "7265717569726564"       // field 71, encoding 2, string "required"
457 }
458
459
460 // Correct placement of terminating comma/closing parentheses in multi-line calls.
461 func _() {
462         f(1,
463                 2,
464                 3)
465         f(1,
466                 2,
467                 3,
468         )
469         f(1,
470                 2,
471                 3)      // comment
472         f(1,
473                 2,
474                 3,      // comment
475         )
476         f(1,
477                 2,
478                 3)      // comment
479         f(1,
480                 2,
481                 3,      // comment
482         )
483 }
484
485
486 // Align comments in multi-line lists of single-line expressions.
487 var txpix = [NCOL]draw.Color{
488         draw.Yellow,            // yellow
489         draw.Cyan,              // cyan
490         draw.Green,             // lime green
491         draw.GreyBlue,          // slate
492         draw.Red,               /* red */
493         draw.GreyGreen,         /* olive green */
494         draw.Blue,              /* blue */
495         draw.Color(0xFF55AAFF), /* pink */
496         draw.Color(0xFFAAFFFF), /* lavender */
497         draw.Color(0xBB005DFF), /* maroon */
498 }
499
500
501 func same(t, u *Time) bool {
502         // respect source lines in multi-line expressions
503         return t.Year == u.Year &&
504                 t.Month == u.Month &&
505                 t.Day == u.Day &&
506                 t.Hour == u.Hour &&
507                 t.Minute == u.Minute &&
508                 t.Second == u.Second &&
509                 t.Weekday == u.Weekday &&
510                 t.ZoneOffset == u.ZoneOffset &&
511                 t.Zone == u.Zone
512 }
513
514
515 func (p *parser) charClass() {
516         // respect source lines in multi-line expressions
517         if cc.negate && len(cc.ranges) == 2 &&
518                 cc.ranges[0] == '\n' && cc.ranges[1] == '\n' {
519                 nl := new(_NotNl)
520                 p.re.add(nl)
521         }
522 }
523
524
525 func addState(s []state, inst instr, match []int) {
526         // handle comments correctly in multi-line expressions
527         for i := 0; i < l; i++ {
528                 if s[i].inst.index() == index &&        // same instruction
529                         s[i].match[0] < pos {   // earlier match already going; leftmost wins
530                         return s
531                 }
532         }
533 }
534
535 func (self *T) foo(x int) *T    { return self }
536
537 func _()        { module.Func1().Func2() }
538
539 func _() {
540         _ = new(T).
541                 foo(1).
542                 foo(2).
543                 foo(3)
544
545         _ = new(T).
546                 foo(1).
547                 foo(2). // inline comments
548                 foo(3)
549
550         _ = new(T).foo(1).foo(2).foo(3)
551
552         // handle multiline argument list correctly
553         _ = new(T).
554                 foo(
555                         1).
556                 foo(2)
557
558         _ = new(T).foo(
559                 1).foo(2)
560
561         _ = Array[3+
562                 4]
563
564         _ = Method(1, 2,
565                 3)
566
567         _ = new(T).
568                 foo().
569                 bar().(*Type)
570
571         _ = new(T).
572                 foo().
573                 bar().(*Type).
574                 baz()
575
576         _ = new(T).
577                 foo().
578                 bar()["idx"]
579
580         _ = new(T).
581                 foo().
582                 bar()["idx"].
583                 baz()
584
585         _ = new(T).
586                 foo().
587                 bar()[1:2]
588
589         _ = new(T).
590                 foo().
591                 bar()[1:2].
592                 baz()
593
594         _ = new(T).
595                 Field.
596                 Array[3+
597                         4].
598                 Table["foo"].
599                 Blob.(*Type).
600                 Slices[1:4].
601                 Method(1, 2,
602                         3).
603                 Thingy
604
605         _ = a.b.c
606         _ = a.
607                 b.
608                 c
609         _ = a.b().c
610         _ = a.
611                 b().
612                 c
613         _ = a.b[0].c
614         _ = a.
615                 b[0].
616                 c
617         _ = a.b[0:].c
618         _ = a.
619                 b[0:].
620                 c
621         _ = a.b.(T).c
622         _ = a.
623                 b.(T).
624                 c
625 }