Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(286)

Side by Side Diff: mojom/mojom_tool/mojom/types.go

Issue 1915413002: Mojom frontend: Detect Ill-founded Types (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Fix comments as per code review. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package mojom 5 package mojom
6 6
7 import ( 7 import (
8 "fmt" 8 "fmt"
9 "math" 9 "math"
10 "mojom/mojom_tool/lexer" 10 "mojom/mojom_tool/lexer"
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 124
125 // SerializationSize() is invoked after the resolution and validation ph ases. 125 // SerializationSize() is invoked after the resolution and validation ph ases.
126 // It returns the number of bytes necessary to serialize an instance of the type 126 // It returns the number of bytes necessary to serialize an instance of the type
127 // to which this type reference resolves in Mojo serialization. 127 // to which this type reference resolves in Mojo serialization.
128 SerializationSize() uint32 128 SerializationSize() uint32
129 129
130 // SerializationAlignment() is invoked after the resolution and validati on phases. 130 // SerializationAlignment() is invoked after the resolution and validati on phases.
131 // It returns the number of bytes on which an instance of the type to wh ich this 131 // It returns the number of bytes on which an instance of the type to wh ich this
132 // type reference resolves must be aligned during Mojo serialization. 132 // type reference resolves must be aligned during Mojo serialization.
133 SerializationAlignment() uint32 133 SerializationAlignment() uint32
134
135 // NonAvoidableUserType is invoked after the resolution and validation p hases.
136 // It returns a non-nil UserDefinedType just in case this type
137 // reference resolves to a type the serialization of which will
138 // include a non-nullable pointer to a struct or union. In that case a
139 // non-nil UserDefinedType representing that struct or union is returnd.
140 //
141 // This method is used during the analysis of ill-founded types. The goa l of
142 // that analysis is to find user-defined types that are unserializable b ecause
143 // every instance of the type would involve a cycle.
144 //
145 // For example, suppose there is a struct Foo that contains a field |x|
146 // and the type of x is Foo. An instance of Foo could not be serialized because
147 // the cycle would cause an infinite recursion.
148 //
149 // However a cycle in the type graph is allowed if it is avoidable.
150 // For example if the type of x were instead Foo? then an instance
151 // of Foo could be serialized by setting x to null at some level
152 // of the recursion.
153 //
154 // Similarly if the type of x were array<Foo, 1> then an instance of
155 // Foo would not be serializable. However it allowed for the type of
156 // x to be any of the following:
157 // - array<Foo>
158 // - array<Foo?, 1>
159 // - array<Foo, 1>?
160 NonAvoidableUserType() UserDefinedType
134 } 161 }
135 162
136 ///////////////////////////////////////////////////////////// 163 /////////////////////////////////////////////////////////////
137 // SimpleType 164 // SimpleType
138 ///////////////////////////////////////////////////////////// 165 /////////////////////////////////////////////////////////////
139 type SimpleType int 166 type SimpleType int
140 167
141 const ( 168 const (
142 SimpleTypeBool SimpleType = iota 169 SimpleTypeBool SimpleType = iota
143 SimpleTypeDouble 170 SimpleTypeDouble
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 return 8 381 return 8
355 default: 382 default:
356 panic(fmt.Sprintf("unexpected type: &d", t)) 383 panic(fmt.Sprintf("unexpected type: &d", t))
357 } 384 }
358 } 385 }
359 386
360 func (t SimpleType) SerializationAlignment() uint32 { 387 func (t SimpleType) SerializationAlignment() uint32 {
361 return t.SerializationSize() 388 return t.SerializationSize()
362 } 389 }
363 390
391 func (SimpleType) NonAvoidableUserType() UserDefinedType {
392 return nil
393 }
394
364 func (t SimpleType) String() string { 395 func (t SimpleType) String() string {
365 switch t { 396 switch t {
366 case SimpleTypeBool: 397 case SimpleTypeBool:
367 return "bool" 398 return "bool"
368 case SimpleTypeDouble: 399 case SimpleTypeDouble:
369 return "double" 400 return "double"
370 case SimpleTypeFloat: 401 case SimpleTypeFloat:
371 return "float" 402 return "float"
372 case SimpleTypeInt8: 403 case SimpleTypeInt8:
373 return "int8" 404 return "int8"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 } 445 }
415 446
416 func (StringType) SerializationSize() uint32 { 447 func (StringType) SerializationSize() uint32 {
417 return 8 448 return 8
418 } 449 }
419 450
420 func (StringType) SerializationAlignment() uint32 { 451 func (StringType) SerializationAlignment() uint32 {
421 return 8 452 return 8
422 } 453 }
423 454
455 func (StringType) NonAvoidableUserType() UserDefinedType {
456 return nil
457 }
458
424 // StringLiteralType is a global singleton representing the unique LiteralType s tring. 459 // StringLiteralType is a global singleton representing the unique LiteralType s tring.
425 var StringLiteralType LiteralType = StringType{} 460 var StringLiteralType LiteralType = StringType{}
426 461
427 // A StringType is a LiteralType: 462 // A StringType is a LiteralType:
428 463
429 func (StringType) LiteralTypeKind() TypeKind { 464 func (StringType) LiteralTypeKind() TypeKind {
430 return TypeKindString 465 return TypeKindString
431 } 466 }
432 467
433 // A StringType is a ConcreteType: 468 // A StringType is a ConcreteType:
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 } 546 }
512 547
513 func (HandleTypeRef) SerializationSize() uint32 { 548 func (HandleTypeRef) SerializationSize() uint32 {
514 return 4 549 return 4
515 } 550 }
516 551
517 func (HandleTypeRef) SerializationAlignment() uint32 { 552 func (HandleTypeRef) SerializationAlignment() uint32 {
518 return 4 553 return 4
519 } 554 }
520 555
556 func (HandleTypeRef) NonAvoidableUserType() UserDefinedType {
557 return nil
558 }
559
521 func (h HandleTypeRef) HandleKind() HandleKind { 560 func (h HandleTypeRef) HandleKind() HandleKind {
522 return h.kind 561 return h.kind
523 } 562 }
524 563
525 func (HandleTypeRef) TypeRefKind() TypeKind { 564 func (HandleTypeRef) TypeRefKind() TypeKind {
526 return TypeKindHandle 565 return TypeKindHandle
527 } 566 }
528 567
529 func (HandleTypeRef) MarkUsedAsMapKey() bool { 568 func (HandleTypeRef) MarkUsedAsMapKey() bool {
530 return false 569 return false
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 } 695 }
657 696
658 func (a ArrayTypeRef) FixedLength() int32 { 697 func (a ArrayTypeRef) FixedLength() int32 {
659 return a.fixedLength 698 return a.fixedLength
660 } 699 }
661 700
662 func (a ArrayTypeRef) ElementType() TypeRef { 701 func (a ArrayTypeRef) ElementType() TypeRef {
663 return a.elementType 702 return a.elementType
664 } 703 }
665 704
705 func (a ArrayTypeRef) NonAvoidableUserType() UserDefinedType {
706 if a.nullable || a.fixedLength <= 0 {
707 return nil
708 }
709 return a.elementType.NonAvoidableUserType()
710 }
711
666 // An ArrayTypeRef is a TypeRef: 712 // An ArrayTypeRef is a TypeRef:
667 713
668 func (ArrayTypeRef) TypeRefKind() TypeKind { 714 func (ArrayTypeRef) TypeRefKind() TypeKind {
669 return TypeKindArray 715 return TypeKindArray
670 } 716 }
671 717
672 func (ArrayTypeRef) MarkUsedAsMapKey() bool { 718 func (ArrayTypeRef) MarkUsedAsMapKey() bool {
673 return false 719 return false
674 } 720 }
675 721
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
740 } 786 }
741 787
742 func (MapTypeRef) SerializationSize() uint32 { 788 func (MapTypeRef) SerializationSize() uint32 {
743 return 8 789 return 8
744 } 790 }
745 791
746 func (MapTypeRef) SerializationAlignment() uint32 { 792 func (MapTypeRef) SerializationAlignment() uint32 {
747 return 8 793 return 8
748 } 794 }
749 795
796 func (MapTypeRef) NonAvoidableUserType() UserDefinedType {
797 return nil
798 }
799
750 func (m MapTypeRef) KeyType() TypeRef { 800 func (m MapTypeRef) KeyType() TypeRef {
751 return m.keyType 801 return m.keyType
752 } 802 }
753 803
754 func (m MapTypeRef) ValueType() TypeRef { 804 func (m MapTypeRef) ValueType() TypeRef {
755 return m.valueType 805 return m.valueType
756 } 806 }
757 807
758 // A MapTypeRef is a TypeRef: 808 // A MapTypeRef is a TypeRef:
759 809
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
878 return t.resolvedType.SerializationSize() 928 return t.resolvedType.SerializationSize()
879 } 929 }
880 930
881 func (t *UserTypeRef) SerializationAlignment() uint32 { 931 func (t *UserTypeRef) SerializationAlignment() uint32 {
882 if t.resolvedType == nil { 932 if t.resolvedType == nil {
883 panic("This method should only be invoked after successful resol ution.") 933 panic("This method should only be invoked after successful resol ution.")
884 } 934 }
885 return t.resolvedType.SerializationAlignment() 935 return t.resolvedType.SerializationAlignment()
886 } 936 }
887 937
938 func (t *UserTypeRef) NonAvoidableUserType() UserDefinedType {
939 if t.nullable || t.interfaceRequest {
940 return nil
941 }
942 if t.resolvedType == nil {
943 panic("This method should only be invoked after successful resol ution.")
944 }
945 switch udt := t.resolvedType.(type) {
946 case *MojomStruct, *MojomUnion:
947 return udt
948 }
949 return nil
950 }
951
888 func (t *UserTypeRef) IsInterfaceRequest() bool { 952 func (t *UserTypeRef) IsInterfaceRequest() bool {
889 return t.interfaceRequest 953 return t.interfaceRequest
890 } 954 }
891 955
892 func (t *UserTypeRef) Identifier() string { 956 func (t *UserTypeRef) Identifier() string {
893 return t.identifier 957 return t.identifier
894 } 958 }
895 959
896 // A UserTypeRef is a TypeRef: 960 // A UserTypeRef is a TypeRef:
897 961
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after
1422 default: 1486 default:
1423 panic(fmt.Sprintf("Unknown LiteralAssignmentKind %d", k)) 1487 panic(fmt.Sprintf("Unknown LiteralAssignmentKind %d", k))
1424 } 1488 }
1425 } 1489 }
1426 1490
1427 type LiteralAssignment struct { 1491 type LiteralAssignment struct {
1428 assignedValue LiteralValue 1492 assignedValue LiteralValue
1429 variableName string 1493 variableName string
1430 kind LiteralAssignmentKind 1494 kind LiteralAssignmentKind
1431 } 1495 }
OLDNEW
« no previous file with comments | « mojom/mojom_tool/mojom/mojom_descriptor.go ('k') | mojom/mojom_tool/mojom/user_defined_types.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698