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

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

Issue 1514173002: New Mojom parser: Don't allow enum variables to be assigned int values. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: working on stuff Created 5 years 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
« no previous file with comments | « no previous file | mojom/mojom_parser/mojom/types_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_parser/lexer" 10 "mojom/mojom_parser/lexer"
(...skipping 762 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 case BuiltInConstantValue: 773 case BuiltInConstantValue:
774 return false 774 return false
775 case *EnumValue: 775 case *EnumValue:
776 return ref.resolvedType == assignedValue.EnumType() 776 return ref.resolvedType == assignedValue.EnumType()
777 default: 777 default:
778 panic(fmt.Sprintf("Unexpected type %T.", assignedValue)) 778 panic(fmt.Sprintf("Unexpected type %T.", assignedValue))
779 } 779 }
780 } 780 }
781 781
782 func (t *UserTypeRef) MarkTypeCompatible(assignment LiteralAssignment) bool { 782 func (t *UserTypeRef) MarkTypeCompatible(assignment LiteralAssignment) bool {
783 » switch assignment.assignedValue.LiteralValueType() { 783 » // Just mark the assignment attempt and return true. We will validate it
784 » case SimpleTypeDouble, SimpleTypeFloat, SimpleTypeBool: 784 » // during the validation phase.
785 » » return false
786 » case StringLiteralType:
787 » » if !assignment.assignedValue.IsDefault() {
788 » » » return false
789 » » }
790 » }
791
792 t.literalAssignment = &assignment 785 t.literalAssignment = &assignment
793 return true 786 return true
794 } 787 }
795 788
796 func (ref *UserTypeRef) validateAfterResolution() error { 789 func (ref *UserTypeRef) validateAfterResolution() error {
797 var file *MojomFile = nil 790 var file *MojomFile = nil
798 if ref.scope != nil && ref.scope.file != nil { 791 if ref.scope != nil && ref.scope.file != nil {
799 file = ref.scope.file 792 file = ref.scope.file
800 } 793 }
801 if ref.resolvedType.Kind() != UserDefinedTypeKindEnum { 794 if ref.resolvedType.Kind() != UserDefinedTypeKindEnum {
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
947 v.identifier, concreteValue) 940 v.identifier, concreteValue)
948 message = UserErrorMessage(v.scope.file, v.token , message) 941 message = UserErrorMessage(v.scope.file, v.token , message)
949 return fmt.Errorf(message) 942 return fmt.Errorf(message)
950 } 943 }
951 case BuiltInConstantValue: 944 case BuiltInConstantValue:
952 message := fmt.Sprintf("Illegal assignment: %s cannot be used as an enum value initializer.", v.identifier) 945 message := fmt.Sprintf("Illegal assignment: %s cannot be used as an enum value initializer.", v.identifier)
953 message = UserErrorMessage(v.scope.file, v.token, messag e) 946 message = UserErrorMessage(v.scope.file, v.token, messag e)
954 return fmt.Errorf(message) 947 return fmt.Errorf(message)
955 case *EnumValue: 948 case *EnumValue:
956 // An EnumValue is being used as an EnumValue initialize r. 949 // An EnumValue is being used as an EnumValue initialize r.
957 » » » // Below we will check that the two EnumTypes match. 950 » » » // We will only check that the two EnumTypes match.
958 // In ComputeEnumValueIntegers() in computed_data.go we will further validate. 951 // In ComputeEnumValueIntegers() in computed_data.go we will further validate.
952 if !v.assigneeSpec.Type.IsAssignmentCompatible(v.resolve dConcreteValue) {
953 var message string
954 switch v.resolvedDeclaredValue.(type) {
955 case *EnumValue:
956 // An enum value is being used directly as an initializer.
957 message = fmt.Sprintf("Illegal assignmen t: The enum value %s of type %s may not be used as an initializer for %s of type %s.",
958 v.identifier, concreteValue.enum Type.fullyQualifiedName, v.assigneeSpec.Name, v.assigneeSpec.Type.TypeName())
959 default:
960 // A user-defined constant whose value i s an enum value is being used as an initializer.
961 message = fmt.Sprintf("Illegal assignmen t: %s with the value %v may not be used as an initializer for %s of type %s.",
962 v.identifier, concreteValue.full yQualifiedName, v.assigneeSpec.Name, v.assigneeSpec.Type.TypeName())
963 }
964 return fmt.Errorf(UserErrorMessage(v.scope.file, v.token, message))
965 }
959 default: 966 default:
960 panic(fmt.Sprintf("Unexpected type %T", concreteValue)) 967 panic(fmt.Sprintf("Unexpected type %T", concreteValue))
961 } 968 }
962 969 » » return nil
963 } 970 }
964 971
965 if !v.assigneeSpec.Type.IsAssignmentCompatible(v.resolvedConcreteValue) { 972 if !v.assigneeSpec.Type.IsAssignmentCompatible(v.resolvedConcreteValue) {
966 var message string 973 var message string
967 switch assigneeType := v.assigneeSpec.Type.(type) { 974 switch assigneeType := v.assigneeSpec.Type.(type) {
968 case SimpleType, StringType, *UserTypeRef: 975 case SimpleType, StringType, *UserTypeRef:
969 switch concreteValue := v.resolvedConcreteValue.(type) { 976 switch concreteValue := v.resolvedConcreteValue.(type) {
970 case LiteralValue: 977 case LiteralValue:
971 // A user-defined constant whose value is a lite ral value is being assigned to a variable. 978 // A user-defined constant whose value is a lite ral value is being assigned to a variable.
972 message = fmt.Sprintf("Illegal assignment: %s wi th the value %v of type %s may not be assigned to %s of type %s.", 979 message = fmt.Sprintf("Illegal assignment: %s wi th the value %v of type %s may not be assigned to %s of type %s.",
973 v.identifier, concreteValue, concreteVal ue.LiteralValueType(), v.assigneeSpec.Name, assigneeType.TypeName()) 980 v.identifier, concreteValue, concreteVal ue.LiteralValueType(), v.assigneeSpec.Name, assigneeType.TypeName())
974 case BuiltInConstantValue: 981 case BuiltInConstantValue:
975 switch v.resolvedDeclaredValue.(type) { 982 switch v.resolvedDeclaredValue.(type) {
976 case BuiltInConstantValue: 983 case BuiltInConstantValue:
977 // A built-in float constant is being as signed directly to a variable. 984 // A built-in float constant is being as signed directly to a variable.
978 message = fmt.Sprintf("Illegal assignmen t: %s may not be assigned to %s of type %s.", 985 message = fmt.Sprintf("Illegal assignmen t: %s may not be assigned to %s of type %s.",
979 v.identifier, v.assigneeSpec.Nam e, assigneeType.TypeName()) 986 v.identifier, v.assigneeSpec.Nam e, assigneeType.TypeName())
980 default: 987 default:
981 // A user-defined constant whose value i s a built-in float constant is being assigned to a variable. 988 // A user-defined constant whose value i s a built-in float constant is being assigned to a variable.
982 message = fmt.Sprintf("Illegal assignmen t: %s with the value %v may not be assigned to %s of type %s.", 989 message = fmt.Sprintf("Illegal assignmen t: %s with the value %v may not be assigned to %s of type %s.",
983 v.identifier, concreteValue, v.a ssigneeSpec.Name, assigneeType.TypeName()) 990 v.identifier, concreteValue, v.a ssigneeSpec.Name, assigneeType.TypeName())
984 } 991 }
985 case *EnumValue: 992 case *EnumValue:
986 switch v.resolvedDeclaredValue.(type) { 993 switch v.resolvedDeclaredValue.(type) {
987 case *EnumValue: 994 case *EnumValue:
988 // An enum value is being assigned direc tly to a variable. 995 // An enum value is being assigned direc tly to a variable.
989 » » » » » message = "Illegal assignment: The enum value %s of type %s may not be assigned to %s of type %s." 996 » » » » » message = fmt.Sprintf("Illegal assignmen t: The enum value %s of type %s may not be assigned to %s of type %s.",
990 » » » » » if v.usedAsEnumValueInitializer { 997 » » » » » » v.identifier, concreteValue.enum Type.fullyQualifiedName, v.assigneeSpec.Name, assigneeType.TypeName())
991 » » » » » » // An enum value is being used d irectly as an initializer.
992 » » » » » » message = "Illegal assignment: T he enum value %s of type %s may not be used as an initializer for %s of type %s. "
993 » » » » » }
994 » » » » » message = fmt.Sprintf(message, v.identif ier, concreteValue.enumType.fullyQualifiedName, v.assigneeSpec.Name, assigneeTyp e.TypeName())
995 default: 998 default:
996 // A user-defined constant whose value i s an enum value is being assigned to a variable. 999 // A user-defined constant whose value i s an enum value is being assigned to a variable.
997 message = "Illegal assignment: %s with t he value %v may not be assigned to %s of type %s." 1000 message = "Illegal assignment: %s with t he value %v may not be assigned to %s of type %s."
998 if v.usedAsEnumValueInitializer { 1001 if v.usedAsEnumValueInitializer {
999 » » » » » » // A user-defined constant whose value is an enum valu is being used as an initializer. 1002 » » » » » » // A user-defined constant whose value is an enum value is being used as an initializer.
1000 message = "Illegal assignment: % s with the value %v may not be used as an initializer for %s of type %s." 1003 message = "Illegal assignment: % s with the value %v may not be used as an initializer for %s of type %s."
1001 } 1004 }
1002 message = fmt.Sprintf(message, v.identif ier, concreteValue.fullyQualifiedName, v.assigneeSpec.Name, assigneeType.TypeNam e()) 1005 message = fmt.Sprintf(message, v.identif ier, concreteValue.fullyQualifiedName, v.assigneeSpec.Name, assigneeType.TypeNam e())
1003 } 1006 }
1004 default: 1007 default:
1005 panic(fmt.Sprintf("Unexpected type %T", concrete Value)) 1008 panic(fmt.Sprintf("Unexpected type %T", concrete Value))
1006 } 1009 }
1007 default: 1010 default:
1008 panic(fmt.Sprintf("Unexpected type %T", assigneeType)) 1011 panic(fmt.Sprintf("Unexpected type %T", assigneeType))
1009 } 1012 }
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
1214 default: 1217 default:
1215 panic(fmt.Sprintf("Unknown LiteralAssignmentKind %d", k)) 1218 panic(fmt.Sprintf("Unknown LiteralAssignmentKind %d", k))
1216 } 1219 }
1217 } 1220 }
1218 1221
1219 type LiteralAssignment struct { 1222 type LiteralAssignment struct {
1220 assignedValue LiteralValue 1223 assignedValue LiteralValue
1221 variableName string 1224 variableName string
1222 kind LiteralAssignmentKind 1225 kind LiteralAssignmentKind
1223 } 1226 }
OLDNEW
« no previous file with comments | « no previous file | mojom/mojom_parser/mojom/types_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698