public ANamedType(TIdentifier name, TIdentifier ns) { AAName aName = new AAName(); if (ns != null) aName.GetIdentifier().Add(ns); aName.GetIdentifier().Add(name); SetName(aName); }
public AAmbiguousNameLvalue(ASimpleName simpleName) { AAName name = new AAName(); name.GetIdentifier().Add(simpleName.Identifier); SetAmbiguous(name); }
public override void CaseAAName(AAName node) { InAAName(node); { Object[] temp = new Object[node.GetIdentifier().Count]; node.GetIdentifier().CopyTo(temp, 0); for (int i = temp.Length - 1; i >= 0; i--) { ((TIdentifier)temp[i]).Apply(this); } } OutAAName(node); }
public static PLvalue Link(AAName name, Node refNode, List<Node> list, SharedData data) { List<TIdentifier> identifierList = new List<TIdentifier>(); { int count = name.GetIdentifier().Count; if (count < list.Count) { for (int i = 0; i < list.Count - count; i++) { if (list[i] is AStructDecl) identifierList.Add(((AStructDecl)list[i]).GetName()); } for (int i = 0; i < count; i++) { TIdentifier iden = (TIdentifier)name.GetIdentifier()[i]; identifierList.Add(iden); } } else for (int i = count - list.Count; i < count; i++) { TIdentifier iden = (TIdentifier)name.GetIdentifier()[i]; identifierList.Add(iden); } } PLvalue baseLvalue = null; Node node = list[0]; list.RemoveAt(0); TIdentifier identifier = identifierList[0]; identifierList.RemoveAt(0); if (node is AALocalDecl) { AALocalDecl aNode = (AALocalDecl)node; if (node.Parent() is AStructDecl) {//Struct local //Make it this->var or this.var AStructDecl pStruct = Util.GetAncestor<AStructDecl>(node); if (pStruct.GetClassToken() != null || Util.HasAncestor<AConstructorDecl>(refNode) || Util.HasAncestor<ADeconstructorDecl>(refNode)) {//(*this). baseLvalue = new AThisLvalue(new TThis("this")); baseLvalue = new APointerLvalue(new TStar("*"), new ALvalueExp(baseLvalue)); baseLvalue = new AStructLvalue(new ALvalueExp(baseLvalue), new ADotDotType(new TDot(".")), identifier); data.StructFieldLinks[(AStructLvalue)baseLvalue] = aNode; } else {//struct. baseLvalue = new AStructFieldLvalue(identifier); data.StructMethodFieldLinks[(AStructFieldLvalue)baseLvalue] = aNode; } } else {//Method/constructor/deconstructor local ALocalLvalue replaceNode = new ALocalLvalue(identifier); data.LocalLinks[replaceNode] = aNode; baseLvalue = replaceNode; } } else if (node is APropertyDecl) { APropertyDecl aNode = (APropertyDecl)node; if (Util.HasAncestor<AStructDecl>(node)) {//Property in current struct AStructDecl pStruct = Util.GetAncestor<AStructDecl>(node); if (pStruct.GetClassToken() != null || Util.HasAncestor<AConstructorDecl>(refNode) || Util.HasAncestor<ADeconstructorDecl>(refNode)) {//(*this). baseLvalue = new AThisLvalue(new TThis("this")); baseLvalue = new APointerLvalue(new TStar("*"), new ALvalueExp(baseLvalue)); baseLvalue = new AStructLvalue(new ALvalueExp(baseLvalue), new ADotDotType(new TDot(".")), identifier); data.StructPropertyLinks[(AStructLvalue)baseLvalue] = aNode; } else {//struct. baseLvalue = new AStructFieldLvalue(identifier); data.StructMethodPropertyLinks[(AStructFieldLvalue)baseLvalue] = aNode; } } else {//Global property baseLvalue = new APropertyLvalue(identifier); data.PropertyLinks[(APropertyLvalue)baseLvalue] = aNode; } } else if (node is AFieldDecl) { baseLvalue = new AFieldLvalue(identifier); data.FieldLinks[(AFieldLvalue)baseLvalue] = (AFieldDecl)node; } else if (node is AStructDecl) { AStructDecl targetStruct = (AStructDecl)node; node = list[0]; list.RemoveAt(0); identifier = identifierList[0]; identifierList.RemoveAt(0); AStructFieldLvalue lvalue = new AStructFieldLvalue(identifier); if (node is AALocalDecl) data.StructMethodFieldLinks[lvalue] = (AALocalDecl)node; else data.StructMethodPropertyLinks[lvalue] = (APropertyDecl)node; baseLvalue = lvalue; } while (list.Count > 0) { node = list[0]; list.RemoveAt(0); identifier = identifierList[0]; identifierList.RemoveAt(0); baseLvalue = new AStructLvalue(new ALvalueExp(baseLvalue), new ADotDotType(new TDot(".")), identifier); if (node is AALocalDecl) {//Struct local data.StructFieldLinks[(AStructLvalue) baseLvalue] = (AALocalDecl) node; } else if (node is APropertyDecl) {//Struct property data.StructPropertyLinks[(AStructLvalue) baseLvalue] = (APropertyDecl) node; } //Don't link array length stuff } return baseLvalue; }
private static void GetTargets(AAName node, List<string> names, out List<List<Node>>[] targets, List<ANamespaceDecl> namespaces, SharedData data, ErrorCollection errors, out bool reportedError, bool first = false) { targets = new [] { new List<List<Node>>(),//0: Stuff starting with a local variable new List<List<Node>>(),//1: Stuff starting with a struct field/property new List<List<Node>>() //2: Stuff starting with a global declaration }; reportedError = false; string name = names[names.Count - 1]; if (names.Count == 1) { //Locals AConstructorDecl pConstructor = Util.GetAncestor<AConstructorDecl>(node); AMethodDecl pMethod = Util.GetAncestor<AMethodDecl>(node); AABlock pBlock = Util.GetAncestor<AABlock>(node); if (pBlock != null) { if (data.Locals.ContainsKey(pBlock)) { foreach (AALocalDecl local in data.Locals[pBlock]) { if (local.GetName().Text == name && Util.IsBefore(local, node)) targets[0].Add(new List<Node>(){local}); } } } else if (pConstructor != null) { foreach (AALocalDecl formal in pConstructor.GetFormals()) { if (formal.GetName().Text == name) targets[0].Add(new List<Node>(){formal}); } } //Fields/properties in current struct AStructDecl currentStruct = Util.GetAncestor<AStructDecl>(node); if (currentStruct != null) { bool isStaticContext = false; if (Util.HasAncestor<AMethodDecl>(node)) isStaticContext = Util.GetAncestor<AMethodDecl>(node).GetStatic() != null; else if (Util.HasAncestor<APropertyDecl>(node)) isStaticContext = Util.GetAncestor<APropertyDecl>(node).GetStatic() != null; else if (Util.HasAncestor<AALocalDecl>(node)) isStaticContext = Util.GetAncestor<AALocalDecl>(node).GetStatic() != null; foreach (AALocalDecl local in data.StructFields[currentStruct]) { if (local.GetName().Text != name) continue; //If it's an enherited private variable, you can't refer to it. if (local.GetVisibilityModifier() is APrivateVisibilityModifier && data.EnheritanceLocalMap.ContainsKey(local)) { continue; } if (local.GetStatic() != null) { //Add it to the dotted map targets[1].Add(new List<Node>(){currentStruct, local}); continue; } if (isStaticContext) continue;//Can't refference non static stuff from static context targets[1].Add(new List<Node>(){local}); } foreach (APropertyDecl local in data.StructProperties[currentStruct]) { if (local.GetName().Text != name) continue; //If it's an enherited private variable, you can't refer to it. if (local.GetVisibilityModifier() is APrivateVisibilityModifier && Util.GetAncestor<AStructDecl>(local) != currentStruct) { continue; } if (local.GetStatic() != null) { //Add it to the dotted map targets[1].Add(new List<Node>() { currentStruct, local }); continue; } if (isStaticContext) continue;//Can't refference non static stuff from static context targets[1].Add(new List<Node>(){local}); } } //Global field/property List<IList> visibleDecls = Util.GetVisibleDecls(node, true); AASourceFile currentFile = Util.GetAncestor<AASourceFile>(node); List<string> currentNamespace = Util.GetFullNamespace(node); foreach (IList declList in visibleDecls) { bool isSameFile = false; if (declList.Count > 0) isSameFile = currentFile == Util.GetAncestor<AASourceFile>((PDecl) declList[0]); foreach (PDecl decl in declList) { if (decl is AFieldDecl) { AFieldDecl aDecl = (AFieldDecl)decl; if (aDecl.GetName().Text != name) continue; bool isSameNamespace = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace(decl)); if (!isSameNamespace && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier || !isSameFile && aDecl.GetStatic() != null) continue; targets[2].Add(new List<Node>(){decl}); } else if (decl is APropertyDecl) { APropertyDecl aDecl = (APropertyDecl)decl; if (aDecl.GetName().Text != name) continue; bool isSameNamespace = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace(decl)); if (!isSameNamespace && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier || !isSameFile && aDecl.GetStatic() != null) continue; targets[2].Add(new List<Node>() { decl }); } else if (decl is AStructDecl && !first) { AStructDecl aDecl = (AStructDecl)decl; if (aDecl.GetName().Text != name) continue; bool isSameNamespace = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace(decl)); if (!isSameNamespace && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier) continue; targets[2].Add(new List<Node>() { decl }); } } } //Look in lib fields foreach (AFieldDecl field in data.Libraries.Fields) { if (field.GetName().Text == name) { targets[2].Add(new List<Node>() { field }); } } //Namespaces visibleDecls = Util.GetVisibleDecls(node, false); foreach (IList declList in visibleDecls) { foreach (PDecl decl in declList) { if (decl is ANamespaceDecl) { ANamespaceDecl aDecl = (ANamespaceDecl)decl; if (aDecl.GetName().Text != name) continue; namespaces.Add(aDecl); } } } } else { /*private static void GetTargets(AAName node, List<string> names, List<AALocalDecl> locals, List<Node> structDecls, //<AAlocaldecl/APropertyDecl> List<PDecl> globalDecls, List<AStructDecl> structType, List<List<Node>> dotted, //<any of the above>.<AAlocaldecl/APropertyDecl> List<ANamespaceDecl> namespaces, SharedData data, ErrorCollection errors, out bool reportedError)*/ List<string> baseNames = new List<string>(); baseNames.AddRange(names); baseNames.RemoveAt(baseNames.Count - 1); List<List<Node>>[] baseTargets; List<ANamespaceDecl> baseNamespaces = new List<ANamespaceDecl>(); GetTargets(node, baseNames, out baseTargets, baseNamespaces, data, errors, out reportedError); AStructDecl currentStruct = Util.GetAncestor<AStructDecl>(node); for (int i = 0; i < baseTargets.Length; i++) { foreach (List<Node> list in baseTargets[i]) { Node last = list[list.Count - 1]; PType type = null; if (last is AALocalDecl) { type = ((AALocalDecl) last).GetType(); } else if (last is APropertyDecl) { type = ((APropertyDecl) last).GetType(); } else if (last is AFieldDecl) { type = ((AFieldDecl) last).GetType(); } else if (last is TIdentifier) { type = new ANamedType(new TIdentifier("int"), null); } if (last is AStructDecl) { //Special. Static only AStructDecl structDecl = ((AStructDecl) last); foreach (AALocalDecl local in data.StructFields[structDecl]) { if (local.GetName().Text != name) continue; //Must be public if we are outside the struct //If it's an enherited private variable, you can't refer to it. if (currentStruct != structDecl && !(local.GetVisibilityModifier() is APublicVisibilityModifier) || currentStruct == structDecl && local.GetVisibilityModifier() is APrivateVisibilityModifier && data.EnheritanceLocalMap.ContainsKey(local)) { continue; } if (local.GetStatic() == null) { //non Static types doesn't work in this context continue; } List<Node> nodeList = new List<Node>(); nodeList.Add(structDecl); nodeList.Add(local); targets[i].Add(nodeList); } foreach (APropertyDecl local in data.StructProperties[structDecl]) { if (local.GetName().Text != name) continue; //Must be public if we are outside the struct //If it's an enherited private variable, you can't refer to it. if (currentStruct != structDecl && !(local.GetVisibilityModifier() is APublicVisibilityModifier)) { continue; } if (local.GetStatic() == null) { //non Static types doesn't work in this context continue; } List<Node> nodeList = new List<Node>(); nodeList.Add(structDecl); nodeList.Add(local); targets[i].Add(nodeList); } } else { if (type is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType)type) && !(data.Enums.ContainsKey(data.StructTypeLinks[(ANamedType)type]))) { AStructDecl targetStruct = data.StructTypeLinks[(ANamedType)type]; foreach (AALocalDecl local in data.StructFields[targetStruct]) { if (local.GetName().Text != name) continue; //Must be public if we are outside the struct //If it's an enherited private variable, you can't refer to it. if (currentStruct != targetStruct && !(local.GetVisibilityModifier() is APublicVisibilityModifier) || currentStruct == targetStruct && local.GetVisibilityModifier() is APrivateVisibilityModifier && data.EnheritanceLocalMap.ContainsKey(local)) { continue; } if (local.GetStatic() != null) { //Static types doesn't work in this context continue; } List<Node> nodeList = new List<Node>(); nodeList.AddRange(list); nodeList.Add(local); targets[i].Add(nodeList); } foreach (APropertyDecl local in data.StructProperties[targetStruct]) { if (local.GetName().Text != name) continue; //Must be public if we are outside the struct //If it's an enherited private variable, you can't refer to it. if (currentStruct != targetStruct && !(local.GetVisibilityModifier() is APublicVisibilityModifier)) { continue; } if (local.GetStatic() != null) { //Static types doesn't work in this context continue; } List<Node> nodeList = new List<Node>(); nodeList.AddRange(list); nodeList.Add(local); targets[i].Add(nodeList); } } else//Find matching enrichment { List<IList> visibleDecls = Util.GetVisibleDecls(node, true); AEnrichmentDecl currentEnrichment = Util.GetAncestor<AEnrichmentDecl>(node); foreach (IList declList in visibleDecls) { foreach (PDecl decl in declList) { if (decl is AEnrichmentDecl) { AEnrichmentDecl aDecl = (AEnrichmentDecl)decl; if (Util.TypesEqual(aDecl.GetType(), type, data)) { foreach (PDecl enrichmentDecl in aDecl.GetDecl()) { if (enrichmentDecl is APropertyDecl) { APropertyDecl local = (APropertyDecl)enrichmentDecl; if (local.GetName().Text != name) continue; //Must be public if we are outside the struct if (currentEnrichment != aDecl && !(local.GetVisibilityModifier() is APublicVisibilityModifier)) { continue; } if (local.GetStatic() != null) { //Static types doesn't work in this context continue; } List<Node> nodeList = new List<Node>(); nodeList.AddRange(list); nodeList.Add(local); targets[i].Add(nodeList); } } } } } } //Could be array.length if ((type is AArrayTempType || type is ADynamicArrayType) && name == "length") { List<Node> nodeList = new List<Node>(); nodeList.AddRange(list); nodeList.Add((TIdentifier)node.GetIdentifier()[names.Count - 1]); targets[i].Add(nodeList); } } } } } AASourceFile currentFile = Util.GetAncestor<AASourceFile>(node); List<string> currentNamespace = Util.GetFullNamespace(node); foreach (ANamespaceDecl ns in baseNamespaces) { bool isSameFile = currentFile == Util.GetAncestor<AASourceFile>(ns); foreach (PDecl decl in ns.GetDecl()) { if (decl is AFieldDecl) { AFieldDecl aDecl = (AFieldDecl)decl; if (aDecl.GetName().Text != name) continue; bool isSameNamespace = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace(decl)); if (!isSameNamespace && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier || !isSameFile && aDecl.GetStatic() != null) continue; targets[2].Add(new List<Node>(){decl}); } else if (decl is APropertyDecl) { APropertyDecl aDecl = (APropertyDecl)decl; if (aDecl.GetName().Text != name) continue; bool isSameNamespace = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace(decl)); if (!isSameNamespace && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier || !isSameFile && aDecl.GetStatic() != null) continue; targets[2].Add(new List<Node>() { decl }); } else if (decl is AStructDecl) { AStructDecl aDecl = (AStructDecl)decl; if (aDecl.GetName().Text != name) continue; bool isSameNamespace = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace(decl)); if (!isSameNamespace && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier) continue; targets[2].Add(new List<Node>() { decl }); } else if (decl is ANamespaceDecl) { ANamespaceDecl aDecl = (ANamespaceDecl)decl; if (aDecl.GetName().Text != name) continue; namespaces.Add(aDecl); } } } } //If we got no matches, and we are not last, report error if (errors != null && node.GetIdentifier().Count > names.Count && targets[0].Count + targets[1].Count + targets[2].Count + namespaces.Count == 0) { string dotList = ""; foreach (string s in names) { if (dotList != "") dotList += "."; dotList += s; } errors.Add(new ErrorCollection.Error((TIdentifier)node.GetIdentifier()[names.Count - 1], dotList + LocRM.GetString("ErrorText174"))); reportedError = true; } }
/* * Local variable (including formals) * Special case: Formals from base constructor calls * * Struct fields/properties in current struct * Not enherited private fields * Note: If it was a class field, it must be replaced by this->... when used * * Struct fields/properties in target struct * * Global field/property * * NamedType (primitive and structs/classes) * * enriched primitive * * Namespace */ public static void GetTargets(AAName name, out List<List<Node>>[] targets, List<ANamespaceDecl> namespaces, SharedData data, ErrorCollection errors, out bool reportedError, bool allowStructs = true) { List<string> names = new List<string>(); foreach (TIdentifier identifier in name.GetIdentifier()) { names.Add(identifier.Text); } GetTargets(name, names, out targets, namespaces, data, errors, out reportedError, !allowStructs); }