public static string DefaultGetCSharpName(CSharpConverter converter, CppElement element, CSharpElement context) { var name = string.Empty; // Try to get the name directly from the CppElement if (element is ICppMember member) { name = member.Name; } // If it is null, try to get a contextual name from the context if (string.IsNullOrEmpty(name)) { var contextName = string.Empty; if (context is ICSharpMember csMember) { contextName = csMember.Name; } if (!string.IsNullOrEmpty(contextName)) { name = contextName; } } // If the name is null, we create an anonymous type name that includes the type, file name, and file offset if (string.IsNullOrEmpty(name)) { var fileName = Path.GetFileNameWithoutExtension(element.Span.Start.File); name = $"__Anonymous{element.GetType().Name}_{fileName}_{element.Span.Start.Offset}"; } else if (element is CppType cppType && (!(element is ICppMember cppMember) || string.IsNullOrEmpty(cppMember.Name))) { switch (cppType) { case CppClass cppClass: name = CSharpHelper.AppendWithCasing(name, CSharpHelper.GetCSharpCasingKind(name), cppClass.ClassKind.ToString().ToLowerInvariant(), CSharpCasingKind.Lower); break; case CppFunctionType cppFunctionType: name = CSharpHelper.AppendWithCasing(name, CSharpHelper.GetCSharpCasingKind(name), "delegate", CSharpCasingKind.Lower); break; } } return(name); }
public static CSharpType GetCSharpType(CSharpConverter converter, CppType cppType, CSharpElement context, bool nested) { // Early exit for primitive types if (cppType is CppPrimitiveType cppPrimitiveType) { // Special case for bool return(cppPrimitiveType.Kind == CppPrimitiveKind.Bool ? GetBoolType(converter) : CSharpHelper.GetCSharpPrimitive(cppPrimitiveType)); } // Check if a particular CppType has been already converted var csType = converter.FindCSharpType(cppType); if (csType != null) { return(csType); } // Pre-decode the type by extracting any const/pointer and get the element type directly DecodeSimpleType(cppType, out var isConst, out var isPointer, out var elementType); if (isPointer) { if (isConst && elementType.Equals(CppPrimitiveType.Char)) { // const char* => string (with marshal) csType = GetStringType(converter); } else { var pointedCSharpType = converter.GetCSharpType(elementType, context, true); var isParam = context is CSharpParameter; var isReturn = context is CSharpMethod; if (!nested && (isParam || isReturn)) { switch (elementType.TypeKind) { case CppTypeKind.Array: break; case CppTypeKind.Reference: break; case CppTypeKind.Qualified: var qualifiedType = (CppQualifiedType)elementType; csType = new CSharpRefType(qualifiedType.Qualifier == CppTypeQualifier.Const ? (isParam ? CSharpRefKind.In : CSharpRefKind.RefReadOnly) : CSharpRefKind.Ref, converter.GetCSharpType(qualifiedType.ElementType, context, true)); break; case CppTypeKind.Function: csType = pointedCSharpType; break; case CppTypeKind.Typedef: csType = new CSharpRefType(CSharpRefKind.Ref, pointedCSharpType); break; case CppTypeKind.StructOrClass: // Is the struct is an opaque definition (which can is transformed into passing the struct directly as // the struct contains the pointer) if (pointedCSharpType is CSharpStruct csStruct && csStruct.IsOpaque) { csType = csStruct; } else { csType = new CSharpRefType(isConst ? (isParam ? CSharpRefKind.In : CSharpRefKind.RefReadOnly) : CSharpRefKind.Ref, pointedCSharpType); } break;