private void GetGlobalTypeForType(FieldNameFinder inNameFinder, ConstantValue tmpConstant, string tmpVal) { //Check for in Variable List var tmpVar = inNameFinder.VariableList?.FirstOrDefault(inItem => inItem.Name == tmpVal); if (tmpVar != null) { tmpConstant.Value = tmpVar; tmpConstant.Type = tmpVar.Type; //TODO load class for Type inNameFinder.Class = ProjectInformation.ClassFromBaseType(tmpVar.Type); } else { //TODO Check for Parent CLass FieldList var tmpField = inNameFinder.Class?.FieldList?.FirstOrDefault(inItem => inItem.Name == tmpVal); if (tmpField != null) { tmpConstant.Value = tmpField; tmpConstant.Type = tmpField.Type; //TODO load class for Type inNameFinder.Class = ProjectInformation.ClassFromBaseType(tmpField.Type); } else { //Check for Static Types, System aliases or other Unknown Type var tmpStaticClassOrUnknown = ProjectInformation.GetClassOrUnknownForType(tmpVal, inNameFinder.Class ?? inNameFinder.MethodeParentClass ?? new ClassContainer()); //tmpConstant.Value = tmpStaticClassOrUnknown.Type; if (tmpConstant.Type != null) { tmpConstant.Type.Type = tmpStaticClassOrUnknown.Type.Type; } else { tmpConstant.Type = tmpStaticClassOrUnknown.Type; } inNameFinder.Class = tmpStaticClassOrUnknown; } } }
/// <summary> /// Write Code-Entry into C# Code /// </summary> /// <param name="inOutput"></param> /// <param name="inCodeEntry"></param> private TypeContainer CodeEntryHandling(ICodeEntry inCodeEntry, FieldNameFinder inNameFinder, TypeContainer inReturnType = null) { if (inCodeEntry == null) { return(null); } TypeContainer tmpReturnType = null; if (inCodeEntry is VariableDeclaration) { var tmpVarDecl = inCodeEntry as VariableDeclaration; inNameFinder.VariableList.Add(tmpVarDecl); var tmpConstant = new ConstantValue() { Type = tmpVarDecl.Type }; GetGlobalTypeForType(new FieldNameFinder(inNameFinder), tmpConstant, tmpVarDecl.Type.Name); tmpVarDecl.Type = tmpConstant.Type; } else if (inCodeEntry is ConstantValue) { var tmpConstant = (inCodeEntry as ConstantValue); var tmpVal = tmpConstant.Value?.ToString() ?? tmpConstant.Type.Name; if (tmpConstant.Value is FieldContainer) { tmpVal = (tmpConstant.Value as FieldContainer).Name; } else if (tmpConstant.Value is VariableDeclaration) { tmpVal = (tmpConstant.Value as VariableDeclaration).Name; } else if (tmpVal.EndsWith("\"")) { //It's a number, nothing to do tmpReturnType = ProjectInformation.GetAliasType("string")?.Type ?? tmpReturnType; } else if (RegexHelper.NumberCheck.IsMatch(tmpVal)) { //It's a number, nothing to do tmpReturnType = ProjectInformation.GetAliasType("int")?.Type ?? tmpReturnType; } else { //Access to Variable, Field, Param or static Class, so we need to do a lot here for Code-link if (tmpVal == "this") { inNameFinder.VariableList = new List <VariableDeclaration>(); tmpConstant.Type = inNameFinder.MethodeParentClass.Type; } else if (tmpVal == "base") { inNameFinder.Class = inNameFinder.Class.InterfaceList .Select(inItem => ProjectInformation.GetClassForType(inItem.Type.Name, new List <string> { inItem.Type.Namespace })) .FirstOrDefault(inItem => !inItem.IsInterface()); inNameFinder.VariableList = new List <VariableDeclaration>(); tmpConstant.Type = inNameFinder.Class.GetParentClass().Type; } else if (tmpVal.StartsWith("\"") && tmpVal.EndsWith("\"")) { //GetGlobalTypeForType(inNameFinder, tmpConstant, "String"); } else if (new Regex("^\\-?[0-9]*(\\.[0-9]*)?(S|D|F|L)?$").IsMatch(tmpVal)) { var tmpConstValue = tmpConstant.Value; if (tmpVal.EndsWith("L")) { GetGlobalTypeForType(inNameFinder, tmpConstant, "long"); } else if (tmpVal.EndsWith("D")) { GetGlobalTypeForType(inNameFinder, tmpConstant, "double"); } else if (tmpVal.EndsWith("S")) { GetGlobalTypeForType(inNameFinder, tmpConstant, "short"); } else if (tmpVal.EndsWith("F")) { GetGlobalTypeForType(inNameFinder, tmpConstant, "float"); } else { GetGlobalTypeForType(inNameFinder, tmpConstant, "int"); } tmpConstant.Value = tmpConstValue; } else { GetGlobalTypeForType(inNameFinder, tmpConstant, tmpVal); } tmpReturnType = tmpConstant.Type; } } else if (inCodeEntry is StatementCode) { var tmpStatement = inCodeEntry as StatementCode; if (tmpStatement.StatementType == Enum.StatementTypeEnum.If || tmpStatement.StatementType == Enum.StatementTypeEnum.Else || tmpStatement.StatementType == Enum.StatementTypeEnum.For || tmpStatement.StatementType == Enum.StatementTypeEnum.While) { if (tmpStatement.InnerContent != null) { foreach (var tmpEntry in tmpStatement.InnerContent.CodeEntries) { CodeEntryHandling(tmpEntry, new FieldNameFinder(inNameFinder)); } } if (tmpStatement.StatementCodeBlocks != null) { foreach (var tmpEntry in tmpStatement.StatementCodeBlocks.SelectMany(inItem => inItem.CodeEntries)) { CodeEntryHandling(tmpEntry, new FieldNameFinder(inNameFinder)); } } } else if (tmpStatement.StatementType == Enum.StatementTypeEnum.Assert || tmpStatement.StatementType == Enum.StatementTypeEnum.Elvis) { foreach (var tmpCodeBlock in tmpStatement.StatementCodeBlocks) { foreach (var tmpEntry in tmpCodeBlock.CodeEntries) { CodeEntryHandling(tmpEntry, new FieldNameFinder(inNameFinder)); } } } else { throw new NotImplementedException("CodeEntryHandling: StatementCode Handling not Implemented"); } } else if (inCodeEntry is SetFieldWithValue) { var tmpFieldVal = inCodeEntry as SetFieldWithValue; foreach (var tmpEntry in tmpFieldVal.VariableToAccess.CodeEntries) { CodeEntryHandling(tmpEntry, new FieldNameFinder(inNameFinder)); } foreach (var tmpEntry in tmpFieldVal.ValueToSet.CodeEntries) { CodeEntryHandling(tmpEntry, new FieldNameFinder(inNameFinder)); } } else if (inCodeEntry is VariableAccess) { var tmpVarAccess = inCodeEntry as VariableAccess; inNameFinder.StackVariables(true, true); ClassContainer tmpPrevClass = null; if (!(tmpVarAccess.Access is VariableDeclaration)) { tmpReturnType = CodeEntryHandling(tmpVarAccess.Access, inNameFinder); tmpPrevClass = inNameFinder.Class; if (tmpReturnType != null) { inNameFinder.Class = ProjectInformation.ClassFromBaseType(tmpReturnType); } else { inNameFinder.Class = null; } } if (tmpVarAccess.Child != null) { tmpReturnType = CodeEntryHandling(tmpVarAccess.Child, inNameFinder, inReturnType); } inNameFinder.Class = tmpPrevClass; inNameFinder.UnstackVariableList(); if (tmpVarAccess.BaseDataSource != null) { CodeEntryHandling(tmpVarAccess.BaseDataSource, inNameFinder, inReturnType); } } else if (inCodeEntry is ReturnCodeEntry) { foreach (var tmpEntry in (inCodeEntry as ReturnCodeEntry).CodeEntries) { CodeEntryHandling(tmpEntry, inNameFinder); } } else if (inCodeEntry is NewObjectDeclaration) { var tmpObjectDecl = (inCodeEntry as NewObjectDeclaration); CodeEntryHandling(tmpObjectDecl.InnerCode, inNameFinder); if (tmpObjectDecl.ArgumentList != null) { foreach (var tmpArgument in tmpObjectDecl.ArgumentList) { foreach (var tmpEntry in tmpArgument.CodeEntries) { CodeEntryHandling(tmpEntry, inNameFinder); } } } } else if (inCodeEntry is MethodeCall) { var tmpMethodeCall = inCodeEntry as MethodeCall; var tmpParentClass = inNameFinder.Class; while (tmpParentClass != null) { tmpMethodeCall.MethodeLink = tmpParentClass.MethodeList.FirstOrDefault(inItem => inItem.Name == tmpMethodeCall.Name); if (tmpMethodeCall.MethodeLink != null) { break; } tmpParentClass = tmpParentClass.GetParentClass(); foreach (var tmpParam in tmpMethodeCall.Parameter) { foreach (var tmpEntry in tmpParam.CodeEntries) { CodeEntryHandling(tmpEntry, new FieldNameFinder() { VariableList = inNameFinder.GetMethodeVariableList(), Class = inNameFinder.MethodeParentClass ?? inNameFinder.Class, MethodeParentClass = inNameFinder.MethodeParentClass }); } } } if (tmpMethodeCall.MethodeLink == null) { //TODO Implement Extension Methode finding if (inNameFinder.Class is UnknownTypeClass || inNameFinder.Class is ClassContainer) { if (tmpMethodeCall.Parameter.Count > 0) { foreach (var tmpParam in tmpMethodeCall.Parameter) { for (var tmpI = 0; tmpI < tmpParam.CodeEntries.Count; tmpI++) { var tmpCodeBlock = tmpParam.CodeEntries[tmpI]; CodeEntryHandling(tmpCodeBlock, new FieldNameFinder() { VariableList = inNameFinder.GetMethodeVariableList(), Class = inNameFinder.MethodeParentClass ?? inNameFinder.Class, MethodeParentClass = inNameFinder.MethodeParentClass }); } } } var tmpMethode = Create.AddMethode(inNameFinder.Class, tmpMethodeCall.Name, inReturnType ?? TypeContainer.Void); tmpMethode.ReturnType = /*inReturnType ??*/ TypeContainer.Void; tmpReturnType = tmpMethode.ReturnType; } else if (inNameFinder.Class == null) { } else { throw new NotImplementedException("Unknown Methode on Class"); } } else { if (tmpMethodeCall.Parameter.Count > 0) { foreach (var tmpParam in tmpMethodeCall.Parameter) { for (var tmpI = 0; tmpI < tmpParam.CodeEntries.Count; tmpI++) { var tmpCodeBlock = tmpParam.CodeEntries[tmpI]; CodeEntryHandling(tmpCodeBlock, new FieldNameFinder() { VariableList = inNameFinder.GetMethodeVariableList(), Class = inNameFinder.MethodeParentClass ?? inNameFinder.Class, MethodeParentClass = inNameFinder.MethodeParentClass }); } } } tmpReturnType = tmpMethodeCall.MethodeLink.ReturnType; } } else if (inCodeEntry is CodeExpression) { var tmpExpr = inCodeEntry as CodeExpression; tmpReturnType = inReturnType; foreach (var tmpSubClause in tmpExpr.SubClauseEntries) { foreach (var tmpCodeEntry in tmpSubClause.CodeEntries) { tmpReturnType = CodeEntryHandling(tmpCodeEntry, inNameFinder, tmpReturnType); } } } else if (inCodeEntry is CodeBlockContainer) { var tmpExpr = inCodeEntry as CodeBlockContainer; tmpReturnType = inReturnType; foreach (var tmpEntry in tmpExpr.InnerBlock.CodeEntries) { tmpReturnType = CodeEntryHandling(tmpEntry, inNameFinder); } } else if (inCodeEntry is TypeConversion) { var tmpExpr = inCodeEntry as TypeConversion; tmpReturnType = tmpExpr.Type; foreach (var tmpEntry in tmpExpr.PreconversionValue.CodeEntries) { CodeEntryHandling(tmpEntry, inNameFinder); } } else { throw new NotImplementedException("Code Entry Type not Implement"); } return(tmpReturnType); }