public static CSharpElement ConvertEnum(CSharpConverter converter, CppEnum cppEnum, CSharpElement context) { var enumName = converter.GetCSharpName(cppEnum, context); var csEnum = new CSharpEnum(enumName) { CppElement = cppEnum }; if (cppEnum.IsAnonymous) { csEnum.Name += "AnonymousEnum"; } var container = converter.GetCSharpContainer(cppEnum, context); container.Members.Add(csEnum); converter.ApplyDefaultVisibility(csEnum, container); csEnum.Comment = converter.GetCSharpComment(cppEnum, csEnum); csEnum.BaseTypes.Add(converter.GetCSharpType(cppEnum.IntegerType, csEnum)); return(csEnum); }
public static CSharpType GetCSharpType(CSharpConverter converter, CppType cppType, CSharpElement context, bool nested) { // Check if a particular CppType has been already converted var csType = converter.FindCSharpType(cppType); if (csType != null) { return(csType); } switch (cppType.TypeKind) { case CppTypeKind.Pointer: if (context is CSharpField) { return(null); } var pointerType = (CppPointerType)cppType; var pointerElementType = pointerType.ElementType; if (IsConst(pointerElementType, out var eltType)) { pointerElementType = eltType; } pointerElementType = pointerElementType.GetCanonicalType(); return(pointerElementType.TypeKind == CppTypeKind.Primitive ? new CSharpPrimitiveType(CSharpPrimitiveKind.IntPtr) : null); case CppTypeKind.Array: if (context is CSharpField) { return(null); } var arrayType = (CppArrayType)cppType; var arrayElementType = arrayType.ElementType; if (arrayType.Size <= 0) { return(null); } bool isConst = IsConst(arrayElementType, out _); var csArrayElementType = converter.GetCSharpType(arrayElementType, context, true); var elementTypeName = csArrayElementType.ToString(); elementTypeName = char.ToUpper(elementTypeName[0]) + elementTypeName.Substring(1); var freeType = new CSharpArrayLikeType($"{elementTypeName}{arrayType.Size}", arrayType.Size); csType = new CSharpRefType(isConst ? CSharpRefKind.In : CSharpRefKind.Ref, freeType); return(csType); } return(null); }
public static CSharpElement ConvertEnumItem(CSharpConverter converter, CppEnumItem cppEnumItem, CSharpElement context) { // If the context is not an enum, we don't support this scenario. if (!(context is CSharpEnum csEnum)) { return(null); } var enumItemName = converter.GetCSharpName(cppEnumItem, context); var csEnumItem = new CSharpEnumItem(enumItemName) { CppElement = cppEnumItem }; csEnum.Members.Add(csEnumItem); csEnumItem.Comment = converter.GetCSharpComment(cppEnumItem, context); // Process any enum item value expression (e.g ENUM_ITEM = 1 << 2) if (cppEnumItem.ValueExpression != null) { var integerValue = converter.ConvertExpression(cppEnumItem.ValueExpression); csEnumItem.Value = $"unchecked(({csEnum.IntegerBaseType}){(string.IsNullOrEmpty(integerValue) ? cppEnumItem.Value + "" : integerValue)})"; // Tag the enum has flags if (!csEnum.IsFlags && csEnumItem.Value.Contains("<<")) { csEnum.IsFlags = true; } if (csEnum.IsFlags) { csEnumItem.Value = csEnumItem.Value.Replace("<<", $" << ({csEnum.IntegerBaseType})"); } } if (converter.Options.GenerateEnumItemAsFields && context.Parent is CSharpClass csClass) { var csEnumItemAsField = new CSharpField(enumItemName) { Modifiers = CSharpModifiers.Const, FieldType = csEnum, Comment = csEnumItem.Comment, InitValue = $"{csEnum.Name}.{csEnumItem.Name}" }; converter.ApplyDefaultVisibility(csEnumItemAsField, csClass); csClass.Members.Add(csEnumItemAsField); } return(csEnumItem); }