Mojom C Generator: Small fixes to get generated C bindings to compile. The changes are: - Suffix int literals with "ul", "ull", etc. - Include MinVersion attribute in the type tables (the corresponding type table struct lives in mojo/public/c/bindings/, not yet committed) and reorder the fields for better packing. - Declare type tables before defining them (since definitions may have references to things that aren't defined yet.) R=azani@chromium.org BUG= Review URL: https://codereview.chromium.org/2074923002 .
diff --git a/mojo/public/tools/bindings/mojom_tool/bin/linux64/generators/c.sha1 b/mojo/public/tools/bindings/mojom_tool/bin/linux64/generators/c.sha1 index ad71ac9..5babe9a 100644 --- a/mojo/public/tools/bindings/mojom_tool/bin/linux64/generators/c.sha1 +++ b/mojo/public/tools/bindings/mojom_tool/bin/linux64/generators/c.sha1
@@ -1 +1 @@ -c8f6edc0fff955009ff0c08055efb8fb49474b58 \ No newline at end of file +87b90fb043951f91e484b17e0ad9008616eb2e38 \ No newline at end of file
diff --git a/mojo/public/tools/bindings/mojom_tool/bin/mac64/generators/c.sha1 b/mojo/public/tools/bindings/mojom_tool/bin/mac64/generators/c.sha1 index 7e8799d..5995c04 100644 --- a/mojo/public/tools/bindings/mojom_tool/bin/mac64/generators/c.sha1 +++ b/mojo/public/tools/bindings/mojom_tool/bin/mac64/generators/c.sha1
@@ -1 +1 @@ -b065117d05d3b1ade388bec5bfefd41130830b23 \ No newline at end of file +a0cb07388a910e207bdd1c27d6cacc77c4121bb1 \ No newline at end of file
diff --git a/mojom/generators/c/cgen/header.go b/mojom/generators/c/cgen/header.go index 17d1798..4814c5c 100644 --- a/mojom/generators/c/cgen/header.go +++ b/mojom/generators/c/cgen/header.go
@@ -189,8 +189,8 @@ func NewConstantTemplate(fileGraph *mojom_files.MojomFileGraph, mojomConstant mojom_types.DeclaredConstant) ConstantTemplate { var type_text string switch mojomConstant.Type.(type) { - // We can't type constants as a 'union MojomStringPtr' since it involves - // some setup and prevents immediate consumption. const char* should + // We can't type string constants as a 'union MojomStringPtr' since it + // involves some setup and prevents immediate consumption. const char* should // suffice. case *mojom_types.TypeStringType: type_text = "char*"
diff --git a/mojom/generators/c/cgen/type_table.go b/mojom/generators/c/cgen/type_table.go index 9b25a29..4a913eb 100644 --- a/mojom/generators/c/cgen/type_table.go +++ b/mojom/generators/c/cgen/type_table.go
@@ -2,6 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +// TODO(vardhan): The generated names for type tables may need to be reworked +// to keep within C's identifier-length limit. + package cgen import ( @@ -12,11 +15,12 @@ ) type StructPointerTableEntry struct { - ElemTable string - Offset uint32 - Nullable bool - ElemType string - KeepGoing bool + ElemTable string + Offset uint32 + MinVersion uint32 + ElemType string + Nullable bool + KeepGoing bool } type UnionPointerTableEntry struct { @@ -153,9 +157,9 @@ // The key array has offset 0. // The value array has offset 8. structTable.Entries = append(structTable.Entries, - table.makeStructPointerTableEntry(fmt.Sprintf("%s_%d", prefix, 0), 0, f.KeyType)) + table.makeStructPointerTableEntry(fmt.Sprintf("%s_%d", prefix, 0), 0, 0, f.KeyType)) structTable.Entries = append(structTable.Entries, - table.makeStructPointerTableEntry(fmt.Sprintf("%s_%d", prefix, 8), 8, f.ValueType)) + table.makeStructPointerTableEntry(fmt.Sprintf("%s_%d", prefix, 8), 8, 0, f.ValueType)) structTable.Entries[1].KeepGoing = false table.Structs = append(table.Structs, structTable) @@ -198,15 +202,16 @@ // but won't insert it into the `table`; it is the caller's responsibility to // insert it. However, this operation is NOT totally immutable, since it may // create type tables for sub types of |fieldType| (e.g. if fieldType is a map). -func (table *TypeTableTemplate) makeStructPointerTableEntry(prefix string, offset uint32, fieldType mojom_types.Type) StructPointerTableEntry { +func (table *TypeTableTemplate) makeStructPointerTableEntry(prefix string, offset uint32, minVersion uint32, fieldType mojom_types.Type) StructPointerTableEntry { elemTableName := fmt.Sprintf("%s_%d", prefix, offset) elemTable, elemType, nullable := table.makeTableForType(elemTableName, fieldType) return StructPointerTableEntry{ - ElemTable: elemTable, - Offset: offset, - Nullable: nullable, - ElemType: elemType, - KeepGoing: true, + ElemTable: elemTable, + Offset: offset, + MinVersion: minVersion, + ElemType: elemType, + Nullable: nullable, + KeepGoing: true, } } @@ -219,7 +224,8 @@ } for _, field := range s.Fields { if table.isPointerOrHandle(field.Type) { - structTable.Entries = append(structTable.Entries, table.makeStructPointerTableEntry(structTablePrefix, uint32(field.Offset), field.Type)) + structTable.Entries = append(structTable.Entries, table.makeStructPointerTableEntry( + structTablePrefix, uint32(field.Offset), field.MinVersion, field.Type)) } } if len(structTable.Entries) > 0 {
diff --git a/mojom/generators/c/cgen/type_translation.go b/mojom/generators/c/cgen/type_translation.go index 2fe92d6..ce0d6e2 100644 --- a/mojom/generators/c/cgen/type_translation.go +++ b/mojom/generators/c/cgen/type_translation.go
@@ -83,13 +83,13 @@ return found } -// Translates: import "file.mojom" -> #include 'rel/path/file.mojom.c.h' +// Translates: import "file.mojom" -> #include 'rel/path/file.mojom-c.h' func mojomToCFilePath(srcRootPath string, mojomImport string) string { rel_import, err := filepath.Rel(srcRootPath, mojomImport) if err != nil { log.Fatalf("Cannot determine relative path for '%s'", mojomImport) } - return rel_import + ".c.h" + return rel_import + "-c.h" } // Given an interface + method name, return a name-mangled name in C. @@ -145,7 +145,7 @@ case *mojom_types.TypeTypeReference: typ_ref := t.Interface().(mojom_types.TypeReference) if typ_ref.IsInterfaceRequest { - return "MojoInterfaceRequestHandle" + return "MojoHandle" } resolved_type := fileGraph.ResolvedTypes[*typ_ref.TypeKey] return userDefinedTypeToCType(&resolved_type) @@ -170,17 +170,17 @@ case *mojom_types.LiteralValueInt16Value: return strconv.FormatInt(int64(val.(int16)), 10) case *mojom_types.LiteralValueInt32Value: - return strconv.FormatInt(int64(val.(int32)), 10) + return strconv.FormatInt(int64(val.(int32)), 10) + "l" case *mojom_types.LiteralValueInt64Value: - return strconv.FormatInt(int64(val.(int64)), 10) + return strconv.FormatInt(int64(val.(int64)), 10) + "ll" case *mojom_types.LiteralValueUint8Value: return strconv.FormatUint(uint64(val.(uint8)), 10) case *mojom_types.LiteralValueUint16Value: return strconv.FormatUint(uint64(val.(uint16)), 10) case *mojom_types.LiteralValueUint32Value: - return strconv.FormatUint(uint64(val.(uint32)), 10) + return strconv.FormatUint(uint64(val.(uint32)), 10) + "ul" case *mojom_types.LiteralValueUint64Value: - return strconv.FormatUint(uint64(val.(uint64)), 10) + return strconv.FormatUint(uint64(val.(uint64)), 10) + "ull" case *mojom_types.LiteralValueStringValue: // TODO(vardhan): Do we have to do any string escaping here? return "\"" + val.(string) + "\""
diff --git a/mojom/generators/c/templates/header.tmpl.go b/mojom/generators/c/templates/header.tmpl.go index 3543bd5..448aae7 100644 --- a/mojom/generators/c/templates/header.tmpl.go +++ b/mojom/generators/c/templates/header.tmpl.go
@@ -18,12 +18,13 @@ #include <stdbool.h> #include <stdint.h> -#include "mojo/public/c/bindings/mojom/array.h" -#include "mojo/public/c/bindings/mojom/buffer.h" -#include "mojo/public/c/bindings/mojom/map.h" -#include "mojo/public/c/bindings/mojom/string.h" -#include "mojo/public/c/bindings/mojom/table.h" -#include "mojo/public/c/bindings/mojom/types.h" +#include "mojo/public/c/bindings/array.h" +#include "mojo/public/c/bindings/buffer.h" +#include "mojo/public/c/bindings/interface.h" +#include "mojo/public/c/bindings/lib/type_table.h" +#include "mojo/public/c/bindings/map.h" +#include "mojo/public/c/bindings/string.h" +#include "mojo/public/c/bindings/validation.h" #include "mojo/public/c/system/handle.h" // Imports.
diff --git a/mojom/generators/c/templates/source.tmpl.go b/mojom/generators/c/templates/source.tmpl.go index aafcd3c..64bd4a0 100644 --- a/mojom/generators/c/templates/source.tmpl.go +++ b/mojom/generators/c/templates/source.tmpl.go
@@ -12,12 +12,7 @@ #include <stdbool.h> -#include "mojo/public/c/bindings/mojom/buffer.h" -#include "mojo/public/c/bindings/mojom/array.h" -#include "mojo/public/c/bindings/mojom/map.h" -#include "mojo/public/c/bindings/mojom/string.h" -#include "mojo/public/c/bindings/mojom/table.h" -#include "mojo/public/c/bindings/mojom/types.h" +#include "mojo/public/c/bindings/lib/type_table.h" #include "mojo/public/c/system/handle.h" // Imports.
diff --git a/mojom/generators/c/templates/type_table.tmpl.go b/mojom/generators/c/templates/type_table.tmpl.go index c8cbe53..c20d7e8 100644 --- a/mojom/generators/c/templates/type_table.tmpl.go +++ b/mojom/generators/c/templates/type_table.tmpl.go
@@ -4,6 +4,9 @@ package templates +// These declarations go in the header file so that we can avoid some +// circular-dependencies. We call these "public" to say that other types can +// refer to them. const GenerateTypeTableDeclarations = ` {{define "GenerateTypeTableDeclarations"}} {{range $union := .PublicUnionNames -}} @@ -18,34 +21,52 @@ const GenerateTypeTableDefinitions = ` {{define "GenerateTypeTableDefinitions"}} +// Declarations for array type entries. +{{range $array := .Arrays -}} +static struct MojomPointerTableArrayEntry {{$array.Name}}; +{{end -}} + +// Declarations for struct type tables. +{{range $struct := .Structs -}} +struct MojomPointerTableStructEntry {{$struct.Name}}[]; +{{end -}} + +// Declarations for union type tables. +{{range $union := .Unions -}} +struct MojomPointerTableUnionEntry {{$union.Name}}[]; +{{end -}} + +// Array type entry definitions. {{range $array := .Arrays -}} static struct MojomPointerTableArrayEntry {{$array.Name}} = { - {{$array.ElemTable}}, {{$array.NumElements}}, {{$array.Nullable}}, - {{$array.ElemType}}, + {{$array.ElemTable}}, {{$array.NumElements}}, + {{$array.ElemType}}, {{$array.Nullable}}, }; {{end -}} -{{range $union := .Unions -}} -struct MojomPointerTableUnionEntry {{$union.Name}}[] = { -{{- range $entry := $union.Entries}} - { - {{$entry.ElemTable}}, {{$entry.Tag}}, - {{$entry.Nullable}}, {{$entry.ElemType}}, {{$entry.KeepGoing}}, - }, -{{end -}} -}; -{{end}} - +// Struct type table definitions. {{range $struct := .Structs -}} struct MojomPointerTableStructEntry {{$struct.Name}}[] = { {{- range $entry := $struct.Entries}} { - {{$entry.ElemTable}}, {{$entry.Offset}}, - {{$entry.Nullable}}, {{$entry.ElemType}}, {{$entry.KeepGoing}}, + {{$entry.ElemTable}}, {{$entry.Offset}}, {{$entry.MinVersion}}, + {{$entry.ElemType}}, {{$entry.Nullable}}, {{$entry.KeepGoing}}, }, {{end -}} }; {{end -}} +// Union type table definitions. +{{range $union := .Unions -}} +struct MojomPointerTableUnionEntry {{$union.Name}}[] = { +{{- range $entry := $union.Entries}} + { + {{$entry.ElemTable}}, {{$entry.Tag}}, {{$entry.ElemType}}, + {{$entry.Nullable}}, {{$entry.KeepGoing}}, + }, +{{end -}} +}; +{{end}} + {{end}} `