OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 cgen | 5 package cgen |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "log" | 9 "log" |
10 "os" | 10 "os" |
11 "path/filepath" | 11 "path/filepath" |
12 "strconv" | 12 "strconv" |
13 "strings" | 13 "strings" |
14 | 14 |
15 "mojom/generated/mojom_files" | 15 "mojom/generated/mojom_files" |
16 "mojom/generated/mojom_types" | 16 "mojom/generated/mojom_types" |
17 ) | 17 ) |
18 | 18 |
19 // TODO(vardhan): Make this file unittestable? This involves making it not crash | 19 // TODO(vardhan): Make this file unittestable? This involves making it not crash |
20 // on failure (so that we can test failure). | 20 // on failure (so that we can test failure). |
21 | |
22 // Translates: path/to/file.mojom -> PATH_TO_FILE_MOJOM_C_H_ | 21 // Translates: path/to/file.mojom -> PATH_TO_FILE_MOJOM_C_H_ |
23 func toHeaderGuard(path string) string { | 22 func toHeaderGuard(path string) string { |
24 return strings.Replace(strings.Replace(strings.ToUpper(path), string(os.
PathSeparator), "_", -1), ".", "_", -1) + "_C_H_" | 23 return strings.Replace(strings.Replace(strings.ToUpper(path), string(os.
PathSeparator), "_", -1), ".", "_", -1) + "_C_H_" |
25 } | 24 } |
26 | 25 |
27 var reservedCKeywords map[string]bool = map[string]bool{ | 26 var reservedCKeywords map[string]bool = map[string]bool{ |
28 "_Alignas": true, | 27 "_Alignas": true, |
29 "_Alignof": true, | 28 "_Alignof": true, |
30 "_Atomic": true, | 29 "_Atomic": true, |
31 "_Bool": true, | 30 "_Bool": true, |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 log.Fatal("Unknown builtin constant", val) | 127 log.Fatal("Unknown builtin constant", val) |
129 return "" | 128 return "" |
130 } | 129 } |
131 | 130 |
132 // A mojom type name-mangled to C type. | 131 // A mojom type name-mangled to C type. |
133 func mojomToCType(t mojom_types.Type, fileGraph *mojom_files.MojomFileGraph) str
ing { | 132 func mojomToCType(t mojom_types.Type, fileGraph *mojom_files.MojomFileGraph) str
ing { |
134 switch t.(type) { | 133 switch t.(type) { |
135 case *mojom_types.TypeSimpleType: | 134 case *mojom_types.TypeSimpleType: |
136 return simpleTypeToCType(t.Interface().(mojom_types.SimpleType)) | 135 return simpleTypeToCType(t.Interface().(mojom_types.SimpleType)) |
137 case *mojom_types.TypeArrayType: | 136 case *mojom_types.TypeArrayType: |
138 » » return "union MojomArrayPtr" | 137 » » return "union MojomArrayHeaderPtr" |
139 case *mojom_types.TypeMapType: | 138 case *mojom_types.TypeMapType: |
140 » » return "union MojomMapPtr" | 139 » » return "union MojomMapHeaderPtr" |
141 case *mojom_types.TypeStringType: | 140 case *mojom_types.TypeStringType: |
142 » » return "union MojomStringPtr" | 141 » » return "union MojomStringHeaderPtr" |
143 case *mojom_types.TypeHandleType: | 142 case *mojom_types.TypeHandleType: |
144 return "MojoHandle" | 143 return "MojoHandle" |
145 case *mojom_types.TypeTypeReference: | 144 case *mojom_types.TypeTypeReference: |
146 typ_ref := t.Interface().(mojom_types.TypeReference) | 145 typ_ref := t.Interface().(mojom_types.TypeReference) |
147 if typ_ref.IsInterfaceRequest { | 146 if typ_ref.IsInterfaceRequest { |
148 return "MojoHandle" | 147 return "MojoHandle" |
149 } | 148 } |
150 resolved_type := fileGraph.ResolvedTypes[*typ_ref.TypeKey] | 149 resolved_type := fileGraph.ResolvedTypes[*typ_ref.TypeKey] |
151 return userDefinedTypeToCType(&resolved_type) | 150 return userDefinedTypeToCType(&resolved_type) |
152 default: | 151 default: |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 case *mojom_types.UserDefinedTypeStructType: | 324 case *mojom_types.UserDefinedTypeStructType: |
326 return 8 | 325 return 8 |
327 case *mojom_types.UserDefinedTypeUnionType: | 326 case *mojom_types.UserDefinedTypeUnionType: |
328 return 16 | 327 return 16 |
329 case *mojom_types.UserDefinedTypeInterfaceType: | 328 case *mojom_types.UserDefinedTypeInterfaceType: |
330 return 8 | 329 return 8 |
331 } | 330 } |
332 log.Fatal("Uhoh, should not be here.", udt.Interface()) | 331 log.Fatal("Uhoh, should not be here.", udt.Interface()) |
333 return 0 | 332 return 0 |
334 } | 333 } |
OLD | NEW |