private bool TryShortenClassName(IdentifierListNode node, out AssociativeNode shortNameNode) { shortNameNode = null; string qualifiedName = CoreUtils.GetIdentifierExceptMethodName(node); // if it is a global method with no class if (string.IsNullOrEmpty(qualifiedName)) { return(false); } // Make sure qualifiedName is not a property var matchingClasses = classTable.GetAllMatchingClasses(qualifiedName); if (matchingClasses.Length == 0) { return(false); } string className = qualifiedName.Split('.').Last(); var symbol = new ProtoCore.Namespace.Symbol(qualifiedName); if (!symbol.Matches(node.ToString())) { return(false); } shortNameNode = CreateNodeFromShortName(className, qualifiedName); return(shortNameNode != null); }
private bool Test_GetIdentifierExceptMethodName(string input, string expected) { var astList = CoreUtils.BuildASTList(core, input); var identList = (astList[0] as BinaryExpressionNode).RightNode as IdentifierListNode; // Verify expected string string identListString = CoreUtils.GetIdentifierExceptMethodName(identList); return(identListString.Equals(expected)); }
internal void LookupResolvedNameAndRewriteAst(ElementResolver elementResolver, ref AssociativeNode astNode) { Debug.Assert(elementResolver != null); // Get partial class identifier/identifier lists var classIdentifiers = GetClassIdentifiers(astNode); var resolvedNames = new Queue <string>(); foreach (var identifier in classIdentifiers) { string partialName = string.Empty; var identifierList = identifier as IdentifierListNode; if (identifierList != null) { partialName = CoreUtils.GetIdentifierExceptMethodName(identifierList); } else { partialName = identifier.Name; } var resolvedName = elementResolver.LookupResolvedName(partialName); if (string.IsNullOrEmpty(resolvedName)) { // If namespace resolution map does not contain entry for partial name, // back up on compiler to resolve the namespace from partial name var matchingClasses = CoreUtils.GetResolvedClassName(classTable, CoreUtils.CreateNodeFromString(partialName)); if (matchingClasses.Length == 1) { resolvedName = matchingClasses[0]; var assemblyName = CoreUtils.GetAssemblyFromClassName(classTable, resolvedName); elementResolver.AddToResolutionMap(partialName, resolvedName, assemblyName); } else { // Class name could not be resolved - Possible namespace conflict // This will be reported subsequently in the pre-compilation stage if there's a conflict // Enter an empty resolved name to the list and continue resolvedNames.Enqueue(string.Empty); continue; } } resolvedNames.Enqueue(resolvedName); } if (resolvedNames.Any()) { RewriteAstWithResolvedName(ref astNode, resolvedNames); } }
private string ResolveClassName(AssociativeNode identifierList) { var identListNode = identifierList as IdentifierListNode; string partialName = identListNode != null? CoreUtils.GetIdentifierExceptMethodName(identListNode) : identifierList.Name; if (string.IsNullOrEmpty(partialName)) { return(String.Empty); } var resolvedName = elementResolver.LookupResolvedName(partialName); if (!string.IsNullOrEmpty(resolvedName)) { return(resolvedName); } // If namespace resolution map does not contain entry for partial name, // back up on compiler to resolve the namespace from partial name var matchingClasses = CoreUtils.GetResolvedClassName(classTable, identifierList); if (matchingClasses.Length == 1) { resolvedName = matchingClasses[0]; var classNode = classTable.ClassNodes.FirstOrDefault(x => x.Name == resolvedName); var isHiddenInLibrary = classNode.ClassAttributes?.HiddenInLibrary ?? false; // Do not add a hidden class to element resolver. if (!isHiddenInLibrary) { var assemblyName = CoreUtils.GetAssemblyFromClassName(classTable, resolvedName); elementResolver.AddToResolutionMap(partialName, resolvedName, assemblyName); } } else if (matchingClasses.Length > 1) { OnLogSymbolConflictWarning(partialName, matchingClasses); } return(resolvedName); }
private IdentifierListNode RewriteNodeWithShortName(IdentifierListNode node) { // Get class name from AST string qualifiedName = CoreUtils.GetIdentifierExceptMethodName(node); // if it is a global method if (string.IsNullOrEmpty(qualifiedName)) { return(node); } // Make sure qualifiedName is not a property var lNode = node.LeftNode; var matchingClasses = classTable.GetAllMatchingClasses(qualifiedName); while (matchingClasses.Length == 0 && lNode is IdentifierListNode) { qualifiedName = lNode.ToString(); matchingClasses = classTable.GetAllMatchingClasses(qualifiedName); lNode = ((IdentifierListNode)lNode).LeftNode; } qualifiedName = lNode.ToString(); string className = qualifiedName.Split('.').Last(); var newIdentList = CreateNodeFromShortName(className, qualifiedName); if (newIdentList == null) { return(node); } // Replace class name in input node with short name (newIdentList) node = new IdentifierListNode { LeftNode = newIdentList, RightNode = node.RightNode, Optr = Operator.dot }; return(node); }
/// <summary> /// Return possible type of the output at specified output port. /// </summary> /// <param name="index"></param> /// <returns></returns> public override ProtoCore.Type GetTypeHintForOutput(int index) { ProtoCore.Type type = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.kTypeVar); var statement = GetStatementForOutput(index); if (statement == null) { return(type); } BinaryExpressionNode expr = statement.AstNode as BinaryExpressionNode; if (expr == null || expr.Optr != Operator.assign) { return(type); } var core = libraryServices.LibraryManagementCore; if (expr.RightNode is IdentifierListNode) { var identListNode = expr.RightNode as IdentifierListNode; var funcNode = identListNode.RightNode as FunctionCallNode; if (funcNode == null) { return(type); } string fullyQualitifiedName = CoreUtils.GetIdentifierExceptMethodName(identListNode); if (string.IsNullOrEmpty(fullyQualitifiedName)) { return(type); } var classIndex = core.ClassTable.IndexOf(fullyQualitifiedName); if (classIndex == ProtoCore.DSASM.Constants.kInvalidIndex) { return(type); } var targetClass = core.ClassTable.ClassNodes[classIndex]; var func = targetClass.GetFirstMemberFunctionBy(funcNode.Function.Name); type = func.ReturnType; return(type); } else if (expr.RightNode is FunctionCallNode) { var functionCallNode = expr.RightNode as FunctionCallNode; ProtoCore.FunctionGroup funcGroup; var funcTable = core.FunctionTable.GlobalFuncTable[0]; if (funcTable.TryGetValue(functionCallNode.Function.Name, out funcGroup)) { var func = funcGroup.FunctionEndPoints.FirstOrDefault(); if (func != null) { return(func.procedureNode.ReturnType); } } } else if (expr.RightNode is IntNode) { return(TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.kTypeInt)); } else if (expr.RightNode is StringNode) { return(TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.kTypeString)); } else if (expr.RightNode is DoubleNode) { return(TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.kTypeDouble)); } else if (expr.RightNode is StringNode) { return(TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.kTypeString)); } return(type); }
private AssociativeNode RewriteIdentifierListNode(AssociativeNode identifierList) { var identListNode = identifierList as IdentifierListNode; string partialName = identListNode != null? CoreUtils.GetIdentifierExceptMethodName(identListNode) : identifierList.Name; var resolvedName = elementResolver.LookupResolvedName(partialName); if (string.IsNullOrEmpty(resolvedName)) { // If namespace resolution map does not contain entry for partial name, // back up on compiler to resolve the namespace from partial name var matchingClasses = CoreUtils.GetResolvedClassName(classTable, identifierList); if (matchingClasses.Length == 1) { resolvedName = matchingClasses[0]; var assemblyName = CoreUtils.GetAssemblyFromClassName(classTable, resolvedName); elementResolver.AddToResolutionMap(partialName, resolvedName, assemblyName); } } if (string.IsNullOrEmpty(resolvedName)) { return(identifierList); } var newIdentList = CoreUtils.CreateNodeFromString(resolvedName); Validity.Assert(newIdentList is IdentifierListNode); // If the original input node matches with the resolved name, simply return // the identifier list constructed from the resolved name var symbol = new Symbol(resolvedName); if (symbol.Matches(identifierList.ToString())) { return(newIdentList); } // Remove partialName from identListNode and replace with newIdentList AssociativeNode leftNode = identListNode != null ? identListNode.LeftNode : identifierList; AssociativeNode rightNode = identListNode != null ? identListNode.RightNode : identifierList; var intermediateNodes = new List <AssociativeNode>(); while (leftNode is IdentifierListNode && !symbol.Matches(leftNode.ToString())) { intermediateNodes.Insert(0, ((IdentifierListNode)leftNode).RightNode); leftNode = ((IdentifierListNode)leftNode).LeftNode; } intermediateNodes.Insert(0, newIdentList); var lNode = CoreUtils.CreateNodeByCombiningIdentifiers(intermediateNodes); Validity.Assert(lNode is IdentifierListNode); // The last ident list for the functioncall or identifier rhs var lastIdentList = new IdentifierListNode { LeftNode = lNode, RightNode = rightNode, Optr = Operator.dot }; return(lastIdentList); }