~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/reflect/type.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
        // method signature, without a receiver, and the Func field is nil.
48
48
        Method(int) Method
49
49
 
 
50
        // MethodByName returns the method with that name in the type's
 
51
        // method set and a boolean indicating if the method was found.
 
52
        //
 
53
        // For a non-interface type T or *T, the returned Method's Type and Func
 
54
        // fields describe a function whose first argument is the receiver.
 
55
        //
 
56
        // For an interface type, the returned Method's Type field gives the
 
57
        // method signature, without a receiver, and the Func field is nil.
 
58
        MethodByName(string) (Method, bool)
 
59
 
50
60
        // NumMethod returns the number of methods in the type's method set.
51
61
        NumMethod() int
52
62
 
264
274
 
265
275
// arrayType represents a fixed array type.
266
276
type arrayType struct {
267
 
        commonType "array"
 
277
        commonType `reflect:"array"`
268
278
        elem       *runtime.Type
269
279
        slice      *runtime.Type
270
280
        len        uintptr
272
282
 
273
283
// chanType represents a channel type.
274
284
type chanType struct {
275
 
        commonType "chan"
 
285
        commonType `reflect:"chan"`
276
286
        elem       *runtime.Type
277
287
        dir        uintptr
278
288
}
279
289
 
280
290
// funcType represents a function type.
281
291
type funcType struct {
282
 
        commonType "func"
 
292
        commonType `reflect:"func"`
283
293
        dotdotdot  bool
284
294
        in         []*runtime.Type
285
295
        out        []*runtime.Type
294
304
 
295
305
// interfaceType represents an interface type.
296
306
type interfaceType struct {
297
 
        commonType "interface"
 
307
        commonType `reflect:"interface"`
298
308
        methods    []imethod
299
309
}
300
310
 
301
311
// mapType represents a map type.
302
312
type mapType struct {
303
 
        commonType "map"
 
313
        commonType `reflect:"map"`
304
314
        key        *runtime.Type
305
315
        elem       *runtime.Type
306
316
}
307
317
 
308
318
// ptrType represents a pointer type.
309
319
type ptrType struct {
310
 
        commonType "ptr"
 
320
        commonType `reflect:"ptr"`
311
321
        elem       *runtime.Type
312
322
}
313
323
 
314
324
// sliceType represents a slice type.
315
325
type sliceType struct {
316
 
        commonType "slice"
 
326
        commonType `reflect:"slice"`
317
327
        elem       *runtime.Type
318
328
}
319
329
 
328
338
 
329
339
// structType represents a struct type.
330
340
type structType struct {
331
 
        commonType "struct"
 
341
        commonType `reflect:"struct"`
332
342
        fields     []structField
333
343
}
334
344
 
344
354
        Name    string
345
355
        Type    Type
346
356
        Func    Value
 
357
        Index   int
347
358
}
348
359
 
349
360
// High bit says whether type has
451
462
        m.Type = toType(p.typ)
452
463
        fn := p.tfn
453
464
        m.Func = valueFromIword(flag, m.Type, iword(fn))
 
465
        m.Index = i
454
466
        return
455
467
}
456
468
 
461
473
        return len(t.methods)
462
474
}
463
475
 
 
476
func (t *uncommonType) MethodByName(name string) (m Method, ok bool) {
 
477
        if t == nil {
 
478
                return
 
479
        }
 
480
        var p *method
 
481
        for i := range t.methods {
 
482
                p = &t.methods[i]
 
483
                if p.name != nil && *p.name == name {
 
484
                        return t.Method(i), true
 
485
                }
 
486
        }
 
487
        return
 
488
}
 
489
 
464
490
// TODO(rsc): 6g supplies these, but they are not
465
491
// as efficient as they could be: they have commonType
466
492
// as the receiver instead of *commonType.
480
506
        return t.uncommonType.Method(i)
481
507
}
482
508
 
 
509
func (t *commonType) MethodByName(name string) (m Method, ok bool) {
 
510
        if t.Kind() == Interface {
 
511
                tt := (*interfaceType)(unsafe.Pointer(t))
 
512
                return tt.MethodByName(name)
 
513
        }
 
514
        return t.uncommonType.MethodByName(name)
 
515
}
 
516
 
483
517
func (t *commonType) PkgPath() string {
484
518
        return t.uncommonType.PkgPath()
485
519
}
636
670
                m.PkgPath = *p.pkgPath
637
671
        }
638
672
        m.Type = toType(p.typ)
 
673
        m.Index = i
639
674
        return
640
675
}
641
676
 
642
677
// NumMethod returns the number of interface methods in the type's method set.
643
678
func (t *interfaceType) NumMethod() int { return len(t.methods) }
644
679
 
 
680
// MethodByName method with the given name in the type's method set.
 
681
func (t *interfaceType) MethodByName(name string) (m Method, ok bool) {
 
682
        if t == nil {
 
683
                return
 
684
        }
 
685
        var p *imethod
 
686
        for i := range t.methods {
 
687
                p = &t.methods[i]
 
688
                if *p.name == name {
 
689
                        return t.Method(i), true
 
690
                }
 
691
        }
 
692
        return
 
693
}
 
694
 
645
695
type StructField struct {
646
696
        PkgPath   string // empty for uppercase Name
647
697
        Name      string
648
698
        Type      Type
649
 
        Tag       string
 
699
        Tag       StructTag
650
700
        Offset    uintptr
651
701
        Index     []int
652
702
        Anonymous bool
653
703
}
654
704
 
 
705
// A StructTag is the tag string in a struct field.
 
706
//
 
707
// By convention, tag strings are a concatenation of
 
708
// optionally space-separated key:"value" pairs.
 
709
// Each key is a non-empty string consisting of non-control
 
710
// characters other than space (U+0020 ' '), quote (U+0022 '"'),
 
711
// and colon (U+003A ':').  Each value is quoted using U+0022 '"'
 
712
// characters and Go string literal syntax.
 
713
type StructTag string
 
714
 
 
715
// Get returns the value associated with key in the tag string.
 
716
// If there is no such key in the tag, Get returns the empty string.
 
717
// If the tag does not have the conventional format, the value
 
718
// returned by Get is unspecified, 
 
719
func (tag StructTag) Get(key string) string {
 
720
        for tag != "" {
 
721
                // skip leading space
 
722
                i := 0
 
723
                for i < len(tag) && tag[i] == ' ' {
 
724
                        i++
 
725
                }
 
726
                tag = tag[i:]
 
727
                if tag == "" {
 
728
                        break
 
729
                }
 
730
 
 
731
                // scan to colon.
 
732
                // a space or a quote is a syntax error
 
733
                i = 0
 
734
                for i < len(tag) && tag[i] != ' ' && tag[i] != ':' && tag[i] != '"' {
 
735
                        i++
 
736
                }
 
737
                if i+1 >= len(tag) || tag[i] != ':' || tag[i+1] != '"' {
 
738
                        break
 
739
                }
 
740
                name := string(tag[:i])
 
741
                tag = tag[i+1:]
 
742
 
 
743
                // scan quoted string to find value
 
744
                i = 1
 
745
                for i < len(tag) && tag[i] != '"' {
 
746
                        if tag[i] == '\\' {
 
747
                                i++
 
748
                        }
 
749
                        i++
 
750
                }
 
751
                if i >= len(tag) {
 
752
                        break
 
753
                }
 
754
                qvalue := string(tag[:i+1])
 
755
                tag = tag[i+1:]
 
756
 
 
757
                if key == name {
 
758
                        value, _ := strconv.Unquote(qvalue)
 
759
                        return value
 
760
                }
 
761
        }
 
762
        return ""
 
763
}
 
764
 
655
765
// Field returns the i'th struct field.
656
766
func (t *structType) Field(i int) (f StructField) {
657
767
        if i < 0 || i >= len(t.fields) {
673
783
                f.PkgPath = *p.pkgPath
674
784
        }
675
785
        if p.tag != nil {
676
 
                f.Tag = *p.tag
 
786
                f.Tag = StructTag(*p.tag)
677
787
        }
678
788
        f.Offset = p.offset
679
789
        f.Index = []int{i}
827
937
                i  runtime.Type
828
938
                ct commonType
829
939
        }
830
 
        return (*runtime.Type)(unsafe.Pointer(uintptr(unsafe.Pointer(t)) - uintptr(unsafe.Offsetof(rt.ct))))
 
940
        return (*runtime.Type)(unsafe.Pointer(uintptr(unsafe.Pointer(t)) - unsafe.Offsetof(rt.ct)))
831
941
}
832
942
 
833
943
// PtrTo returns the pointer type with element t.
888
998
 
889
999
        p.uncommonType = nil
890
1000
        p.ptrToThis = nil
891
 
        p.elem = (*runtime.Type)(unsafe.Pointer(uintptr(unsafe.Pointer(ct)) - uintptr(unsafe.Offsetof(rt.ptrType))))
 
1001
        p.elem = (*runtime.Type)(unsafe.Pointer(uintptr(unsafe.Pointer(ct)) - unsafe.Offsetof(rt.ptrType)))
892
1002
 
893
1003
        ptrMap.m[ct] = p
894
1004
        ptrMap.Unlock()