| 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 // TODO(vardhan): The generated names for type tables may need to be reworked | 5 // TODO(vardhan): Occurrances of "type table" and "pointer table" should be |
| 6 // to keep within C's identifier-length limit. | 6 // "type descriptors". |
| 7 | 7 |
| 8 package cgen | 8 package cgen |
| 9 | 9 |
| 10 import ( | 10 import ( |
| 11 "fmt" | 11 "fmt" |
| 12 "log" | 12 "log" |
| 13 "mojom/generated/mojom_files" | 13 "mojom/generated/mojom_files" |
| 14 "mojom/generated/mojom_types" | 14 "mojom/generated/mojom_types" |
| 15 ) | 15 ) |
| 16 | 16 |
| 17 type StructPointerTableEntry struct { | 17 type StructPointerTableEntry struct { |
| 18 ElemTable string | 18 ElemTable string |
| 19 Offset uint32 | 19 Offset uint32 |
| 20 MinVersion uint32 | 20 MinVersion uint32 |
| 21 ElemType string | 21 ElemType string |
| 22 Nullable bool | 22 Nullable bool |
| 23 KeepGoing bool | |
| 24 } | 23 } |
| 25 | 24 |
| 26 type UnionPointerTableEntry struct { | 25 type UnionPointerTableEntry struct { |
| 27 ElemTable string | 26 ElemTable string |
| 28 Tag uint32 | 27 Tag uint32 |
| 29 Nullable bool | 28 Nullable bool |
| 30 ElemType string | 29 ElemType string |
| 31 KeepGoing bool | |
| 32 } | 30 } |
| 33 | 31 |
| 34 type ArrayPointerTableEntry struct { | 32 type ArrayPointerTableEntry struct { |
| 35 Name string | 33 Name string |
| 36 ElemTable string | 34 ElemTable string |
| 37 NumElements uint32 | 35 NumElements uint32 |
| 38 Nullable bool | 36 Nullable bool |
| 39 ElemType string | 37 ElemType string |
| 40 } | 38 } |
| 41 | 39 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 61 counter uint32 | 59 counter uint32 |
| 62 | 60 |
| 63 // Used to look up user-defined references. | 61 // Used to look up user-defined references. |
| 64 fileGraph *mojom_files.MojomFileGraph | 62 fileGraph *mojom_files.MojomFileGraph |
| 65 } | 63 } |
| 66 | 64 |
| 67 func (table *TypeTableTemplate) getTableForUDT(typeRef mojom_types.TypeReference
) (elemTable string, elemType string, nullable bool) { | 65 func (table *TypeTableTemplate) getTableForUDT(typeRef mojom_types.TypeReference
) (elemTable string, elemType string, nullable bool) { |
| 68 nullable = typeRef.Nullable | 66 nullable = typeRef.Nullable |
| 69 if typeRef.IsInterfaceRequest { | 67 if typeRef.IsInterfaceRequest { |
| 70 elemTable = "NULL" | 68 elemTable = "NULL" |
| 71 » » elemType = "MOJOM_ELEMENT_TYPE_HANDLE" | 69 » » elemType = "MOJOM_TYPE_DESCRIPTOR_TYPE_HANDLE" |
| 72 return | 70 return |
| 73 } | 71 } |
| 74 if typeRef.TypeKey == nil { | 72 if typeRef.TypeKey == nil { |
| 75 log.Fatalf("Unresolved type reference %s", typeRef.Identifier) | 73 log.Fatalf("Unresolved type reference %s", typeRef.Identifier) |
| 76 } | 74 } |
| 77 udt, _ := table.fileGraph.ResolvedTypes[*typeRef.TypeKey] | 75 udt, _ := table.fileGraph.ResolvedTypes[*typeRef.TypeKey] |
| 78 switch udt.(type) { | 76 switch udt.(type) { |
| 79 case *mojom_types.UserDefinedTypeStructType: | 77 case *mojom_types.UserDefinedTypeStructType: |
| 80 structName := *udt.Interface().(mojom_types.MojomStruct).DeclDat
a.FullIdentifier | 78 structName := *udt.Interface().(mojom_types.MojomStruct).DeclDat
a.FullIdentifier |
| 81 » » elemTable = mojomToCName(structName) + "__PointerTable" | 79 » » elemTable = "&" + mojomToCName(structName) + "__TypeDesc" |
| 82 » » elemType = "MOJOM_ELEMENT_TYPE_STRUCT" | 80 » » elemType = "MOJOM_TYPE_DESCRIPTOR_TYPE_STRUCT" |
| 83 case *mojom_types.UserDefinedTypeUnionType: | 81 case *mojom_types.UserDefinedTypeUnionType: |
| 84 unionName := *udt.Interface().(mojom_types.MojomUnion).DeclData.
FullIdentifier | 82 unionName := *udt.Interface().(mojom_types.MojomUnion).DeclData.
FullIdentifier |
| 85 » » elemTable = mojomToCName(unionName) + "__PointerTable" | 83 » » elemTable = "&" + mojomToCName(unionName) + "__TypeDesc" |
| 86 » » elemType = "MOJOM_ELEMENT_TYPE_UNION" | 84 » » elemType = "MOJOM_TYPE_DESCRIPTOR_TYPE_UNION" |
| 87 case *mojom_types.UserDefinedTypeInterfaceType: | 85 case *mojom_types.UserDefinedTypeInterfaceType: |
| 88 elemTable = "NULL" | 86 elemTable = "NULL" |
| 89 » » elemType = "MOJOM_ELEMENT_TYPE_INTERFACE" | 87 » » elemType = "MOJOM_TYPE_DESCRIPTOR_TYPE_INTERFACE" |
| 90 default: | 88 default: |
| 91 elemTable = "NULL" | 89 elemTable = "NULL" |
| 92 » » elemType = "MOJOM_ELEMENT_TYPE_POD" | 90 » » elemType = "MOJOM_TYPE_DESCRIPTOR_TYPE_POD" |
| 93 } | 91 } |
| 94 | 92 |
| 95 return | 93 return |
| 96 } | 94 } |
| 97 | 95 |
| 98 func (table *TypeTableTemplate) makeTableForType(prefix string, dataType mojom_t
ypes.Type) (elemTable string, elemType string, nullable bool) { | 96 func (table *TypeTableTemplate) makeTableForType(prefix string, dataType mojom_t
ypes.Type) (elemTable string, elemType string, nullable bool) { |
| 99 switch dataType.(type) { | 97 switch dataType.(type) { |
| 100 case *mojom_types.TypeStringType: | 98 case *mojom_types.TypeStringType: |
| 101 » » elemTable = "(void*)&MojomStringPointerEntry" | 99 » » elemTable = "&g_mojom_string_type_description" |
| 102 » » elemType = "MOJOM_ELEMENT_TYPE_ARRAY" | 100 » » elemType = "MOJOM_TYPE_DESCRIPTOR_TYPE_ARRAY" |
| 103 nullable = dataType.Interface().(mojom_types.StringType).Nullabl
e | 101 nullable = dataType.Interface().(mojom_types.StringType).Nullabl
e |
| 104 case *mojom_types.TypeArrayType: | 102 case *mojom_types.TypeArrayType: |
| 105 arrayTableName := fmt.Sprintf("%s_%d", prefix, table.counter) | 103 arrayTableName := fmt.Sprintf("%s_%d", prefix, table.counter) |
| 106 table.counter++ | 104 table.counter++ |
| 107 typ := dataType.Interface().(mojom_types.ArrayType) | 105 typ := dataType.Interface().(mojom_types.ArrayType) |
| 108 elemTable = "&" + table.makeArrayPointerEntry(arrayTableName, ty
p) | 106 elemTable = "&" + table.makeArrayPointerEntry(arrayTableName, ty
p) |
| 109 » » elemType = "MOJOM_ELEMENT_TYPE_ARRAY" | 107 » » elemType = "MOJOM_TYPE_DESCRIPTOR_TYPE_ARRAY" |
| 110 nullable = typ.Nullable | 108 nullable = typ.Nullable |
| 111 case *mojom_types.TypeMapType: | 109 case *mojom_types.TypeMapType: |
| 112 mapTableName := fmt.Sprintf("%s_%d", prefix, table.counter) | 110 mapTableName := fmt.Sprintf("%s_%d", prefix, table.counter) |
| 113 table.counter++ | 111 table.counter++ |
| 114 typ := dataType.Interface().(mojom_types.MapType) | 112 typ := dataType.Interface().(mojom_types.MapType) |
| 115 elemTable = "&" + table.makeMapPointerTable(mapTableName, typ) | 113 elemTable = "&" + table.makeMapPointerTable(mapTableName, typ) |
| 116 » » elemType = "MOJOM_ELEMENT_TYPE_STRUCT" | 114 » » elemType = "MOJOM_TYPE_DESCRIPTOR_TYPE_STRUCT" |
| 117 nullable = typ.Nullable | 115 nullable = typ.Nullable |
| 118 case *mojom_types.TypeHandleType: | 116 case *mojom_types.TypeHandleType: |
| 119 typ := dataType.Interface().(mojom_types.HandleType) | 117 typ := dataType.Interface().(mojom_types.HandleType) |
| 120 elemTable = "NULL" | 118 elemTable = "NULL" |
| 121 » » elemType = "MOJOM_ELEMENT_TYPE_HANDLE" | 119 » » elemType = "MOJOM_TYPE_DESCRIPTOR_TYPE_HANDLE" |
| 122 nullable = typ.Nullable | 120 nullable = typ.Nullable |
| 123 case *mojom_types.TypeTypeReference: | 121 case *mojom_types.TypeTypeReference: |
| 124 return table.getTableForUDT(dataType.Interface().(mojom_types.Ty
peReference)) | 122 return table.getTableForUDT(dataType.Interface().(mojom_types.Ty
peReference)) |
| 125 case *mojom_types.TypeSimpleType: | 123 case *mojom_types.TypeSimpleType: |
| 126 elemTable = "NULL" | 124 elemTable = "NULL" |
| 127 » » elemType = "MOJOM_ELEMENT_TYPE_POD" | 125 » » elemType = "MOJOM_TYPE_DESCRIPTOR_TYPE_POD" |
| 128 default: | 126 default: |
| 129 log.Fatal("uhoh, should not be here.") | 127 log.Fatal("uhoh, should not be here.") |
| 130 } | 128 } |
| 131 | 129 |
| 132 return | 130 return |
| 133 } | 131 } |
| 134 | 132 |
| 135 // Returns the name of the array type table. | 133 // Returns the name of the array type table. |
| 136 // Takes in |prefix|, the name this array table entry should be. | 134 // Takes in |prefix|, the name this array table entry should be. |
| 137 func (table *TypeTableTemplate) makeArrayPointerEntry(prefix string, f mojom_typ
es.ArrayType) string { | 135 func (table *TypeTableTemplate) makeArrayPointerEntry(prefix string, f mojom_typ
es.ArrayType) string { |
| 138 numElements := uint32(0) | 136 numElements := uint32(0) |
| 139 if f.FixedLength > 0 { | 137 if f.FixedLength > 0 { |
| 140 numElements = uint32(f.FixedLength) | 138 numElements = uint32(f.FixedLength) |
| 141 } | 139 } |
| 142 entry := ArrayPointerTableEntry{ | 140 entry := ArrayPointerTableEntry{ |
| 143 » » Name: prefix + "__PointerEntry", | 141 » » Name: prefix + "__TypeDesc", |
| 144 NumElements: numElements, | 142 NumElements: numElements, |
| 145 Nullable: f.Nullable, | 143 Nullable: f.Nullable, |
| 146 } | 144 } |
| 147 entry.ElemTable, entry.ElemType, entry.Nullable = table.makeTableForType
(prefix, f.ElementType) | 145 entry.ElemTable, entry.ElemType, entry.Nullable = table.makeTableForType
(prefix, f.ElementType) |
| 148 | 146 |
| 149 table.Arrays = append(table.Arrays, entry) | 147 table.Arrays = append(table.Arrays, entry) |
| 150 return entry.Name | 148 return entry.Name |
| 151 } | 149 } |
| 152 | 150 |
| 153 func (table *TypeTableTemplate) makeMapPointerTable(prefix string, f mojom_types
.MapType) string { | 151 func (table *TypeTableTemplate) makeMapPointerTable(prefix string, f mojom_types
.MapType) string { |
| 154 structTable := StructPointerTable{ | 152 structTable := StructPointerTable{ |
| 155 » » Name: prefix + "__PointerTable", | 153 » » Name: prefix + "__TypeDesc", |
| 156 } | 154 } |
| 157 // The key array has offset 0. | 155 // The key array has offset 0. |
| 158 // The value array has offset 8. | 156 // The value array has offset 8. |
| 159 structTable.Entries = append(structTable.Entries, | 157 structTable.Entries = append(structTable.Entries, |
| 160 table.makeStructPointerTableEntry(fmt.Sprintf("%s_%d", prefix, 0
), 0, 0, f.KeyType)) | 158 table.makeStructPointerTableEntry(fmt.Sprintf("%s_%d", prefix, 0
), 0, 0, f.KeyType)) |
| 161 structTable.Entries = append(structTable.Entries, | 159 structTable.Entries = append(structTable.Entries, |
| 162 table.makeStructPointerTableEntry(fmt.Sprintf("%s_%d", prefix, 8
), 8, 0, f.ValueType)) | 160 table.makeStructPointerTableEntry(fmt.Sprintf("%s_%d", prefix, 8
), 8, 0, f.ValueType)) |
| 163 structTable.Entries[1].KeepGoing = false | |
| 164 | 161 |
| 165 table.Structs = append(table.Structs, structTable) | 162 table.Structs = append(table.Structs, structTable) |
| 166 return structTable.Name | 163 return structTable.Name |
| 167 } | 164 } |
| 168 | 165 |
| 169 // A union in a struct is inlined. It could possibly have a pointer type in | 166 // A union in a struct is inlined. It could possibly have a pointer type in |
| 170 // there, so we consider unions to be pointers for the purposes of this method. | 167 // there, so we consider unions to be pointers for the purposes of this method. |
| 171 // TODO(vardhan): To optimize, check that, if union, there is a reference | 168 // TODO(vardhan): To optimize, check that, if union, there is a reference |
| 172 // type inside the union before deeming the union a pointer type. | 169 // type inside the union before deeming the union a pointer type. |
| 173 func (table *TypeTableTemplate) isPointerOrHandle(typ mojom_types.Type) bool { | 170 func (table *TypeTableTemplate) isPointerOrHandle(typ mojom_types.Type) bool { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 204 // create type tables for sub types of |fieldType| (e.g. if fieldType is a map). | 201 // create type tables for sub types of |fieldType| (e.g. if fieldType is a map). |
| 205 func (table *TypeTableTemplate) makeStructPointerTableEntry(prefix string, offse
t uint32, minVersion uint32, fieldType mojom_types.Type) StructPointerTableEntry
{ | 202 func (table *TypeTableTemplate) makeStructPointerTableEntry(prefix string, offse
t uint32, minVersion uint32, fieldType mojom_types.Type) StructPointerTableEntry
{ |
| 206 elemTableName := fmt.Sprintf("%s_%d", prefix, offset) | 203 elemTableName := fmt.Sprintf("%s_%d", prefix, offset) |
| 207 elemTable, elemType, nullable := table.makeTableForType(elemTableName, f
ieldType) | 204 elemTable, elemType, nullable := table.makeTableForType(elemTableName, f
ieldType) |
| 208 return StructPointerTableEntry{ | 205 return StructPointerTableEntry{ |
| 209 ElemTable: elemTable, | 206 ElemTable: elemTable, |
| 210 Offset: offset, | 207 Offset: offset, |
| 211 MinVersion: minVersion, | 208 MinVersion: minVersion, |
| 212 ElemType: elemType, | 209 ElemType: elemType, |
| 213 Nullable: nullable, | 210 Nullable: nullable, |
| 214 KeepGoing: true, | |
| 215 } | 211 } |
| 216 } | 212 } |
| 217 | 213 |
| 218 // Given a MojomStruct, creates the templates required to make the type table | 214 // Given a MojomStruct, creates the templates required to make the type table |
| 219 // for it, and inserts it into |table|. | 215 // for it, and inserts it into |table|. |
| 220 func (table *TypeTableTemplate) insertStructPointerTable(s mojom_types.MojomStru
ct) { | 216 func (table *TypeTableTemplate) insertStructPointerTable(s mojom_types.MojomStru
ct) { |
| 221 structTablePrefix := mojomToCName(*s.DeclData.FullIdentifier) | 217 structTablePrefix := mojomToCName(*s.DeclData.FullIdentifier) |
| 222 structTable := StructPointerTable{ | 218 structTable := StructPointerTable{ |
| 223 » » Name: structTablePrefix + "__PointerTable", | 219 » » Name: structTablePrefix + "__TypeDesc", |
| 224 } | 220 } |
| 225 for _, field := range s.Fields { | 221 for _, field := range s.Fields { |
| 226 if table.isPointerOrHandle(field.Type) { | 222 if table.isPointerOrHandle(field.Type) { |
| 227 structTable.Entries = append(structTable.Entries, table.
makeStructPointerTableEntry( | 223 structTable.Entries = append(structTable.Entries, table.
makeStructPointerTableEntry( |
| 228 structTablePrefix, uint32(field.Offset), field.M
inVersion, field.Type)) | 224 structTablePrefix, uint32(field.Offset), field.M
inVersion, field.Type)) |
| 229 } | 225 } |
| 230 } | 226 } |
| 231 if len(structTable.Entries) > 0 { | |
| 232 structTable.Entries[len(structTable.Entries)-1].KeepGoing = fals
e | |
| 233 } | |
| 234 table.PublicStructNames = append(table.PublicStructNames, structTable.Na
me) | 227 table.PublicStructNames = append(table.PublicStructNames, structTable.Na
me) |
| 235 table.Structs = append(table.Structs, structTable) | 228 table.Structs = append(table.Structs, structTable) |
| 236 } | 229 } |
| 237 | 230 |
| 238 func (table *TypeTableTemplate) makeUnionPointerTableEntry(prefix string, tag ui
nt32, fieldType mojom_types.Type) UnionPointerTableEntry { | 231 func (table *TypeTableTemplate) makeUnionPointerTableEntry(prefix string, tag ui
nt32, fieldType mojom_types.Type) UnionPointerTableEntry { |
| 239 elemTableName := fmt.Sprintf("%s_%d", prefix, tag) | 232 elemTableName := fmt.Sprintf("%s_%d", prefix, tag) |
| 240 elemTable, elemType, nullable := table.makeTableForType(elemTableName, f
ieldType) | 233 elemTable, elemType, nullable := table.makeTableForType(elemTableName, f
ieldType) |
| 241 return UnionPointerTableEntry{ | 234 return UnionPointerTableEntry{ |
| 242 ElemTable: elemTable, | 235 ElemTable: elemTable, |
| 243 Tag: tag, | 236 Tag: tag, |
| 244 Nullable: nullable, | 237 Nullable: nullable, |
| 245 ElemType: elemType, | 238 ElemType: elemType, |
| 246 KeepGoing: true, | |
| 247 } | 239 } |
| 248 } | 240 } |
| 249 | 241 |
| 250 // Given a MojomUnion, creates the templates required to make the type table | 242 // Given a MojomUnion, creates the templates required to make the type table |
| 251 // for it, and inserts it into |table|. | 243 // for it, and inserts it into |table|. |
| 252 func (table *TypeTableTemplate) insertUnionPointerTable(u mojom_types.MojomUnion
) { | 244 func (table *TypeTableTemplate) insertUnionPointerTable(u mojom_types.MojomUnion
) { |
| 253 unionTablePrefix := mojomToCName(*u.DeclData.FullIdentifier) | 245 unionTablePrefix := mojomToCName(*u.DeclData.FullIdentifier) |
| 254 unionTable := UnionPointerTable{ | 246 unionTable := UnionPointerTable{ |
| 255 » » Name: unionTablePrefix + "__PointerTable", | 247 » » Name: unionTablePrefix + "__TypeDesc", |
| 256 } | 248 } |
| 257 for _, field := range u.Fields { | 249 for _, field := range u.Fields { |
| 258 if table.isPointerOrHandle(field.Type) { | 250 if table.isPointerOrHandle(field.Type) { |
| 259 unionTable.Entries = append(unionTable.Entries, table.ma
keUnionPointerTableEntry(unionTablePrefix, uint32(field.Tag), field.Type)) | 251 unionTable.Entries = append(unionTable.Entries, table.ma
keUnionPointerTableEntry(unionTablePrefix, uint32(field.Tag), field.Type)) |
| 260 } | 252 } |
| 261 } | 253 } |
| 262 if len(unionTable.Entries) > 0 { | |
| 263 unionTable.Entries[len(unionTable.Entries)-1].KeepGoing = false | |
| 264 } | |
| 265 table.PublicUnionNames = append(table.PublicUnionNames, unionTable.Name) | 254 table.PublicUnionNames = append(table.PublicUnionNames, unionTable.Name) |
| 266 table.Unions = append(table.Unions, unionTable) | 255 table.Unions = append(table.Unions, unionTable) |
| 267 } | 256 } |
| 268 | 257 |
| 269 func NewTypeTableTemplate(fileGraph *mojom_files.MojomFileGraph, file *mojom_fil
es.MojomFile) TypeTableTemplate { | 258 func NewTypeTableTemplate(fileGraph *mojom_files.MojomFileGraph, file *mojom_fil
es.MojomFile) TypeTableTemplate { |
| 270 table := TypeTableTemplate{ | 259 table := TypeTableTemplate{ |
| 271 fileGraph: fileGraph, | 260 fileGraph: fileGraph, |
| 272 } | 261 } |
| 273 if file.DeclaredMojomObjects.Structs != nil { | 262 if file.DeclaredMojomObjects.Structs != nil { |
| 274 for _, mojomStructKey := range *(file.DeclaredMojomObjects.Struc
ts) { | 263 for _, mojomStructKey := range *(file.DeclaredMojomObjects.Struc
ts) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 294 params := *mojomMethod.ResponseParams | 283 params := *mojomMethod.ResponseParams |
| 295 fullId := responseMethodToCName(&mojomIf
ace, ¶ms) | 284 fullId := responseMethodToCName(&mojomIf
ace, ¶ms) |
| 296 params.DeclData.FullIdentifier = &fullId | 285 params.DeclData.FullIdentifier = &fullId |
| 297 table.insertStructPointerTable(params) | 286 table.insertStructPointerTable(params) |
| 298 } | 287 } |
| 299 } | 288 } |
| 300 } | 289 } |
| 301 } | 290 } |
| 302 return table | 291 return table |
| 303 } | 292 } |
| OLD | NEW |