private PLvalue DetypeArray(PType type) { if (type is AArrayTempType) { AArrayTempType atype = (AArrayTempType)type; return(new AArrayLvalue(atype.GetToken(), new ALvalueExp(DetypeArray(atype.GetType())), atype.GetDimention())); } if (type is ANamedType) { ANamedType atype = (ANamedType)type; return(new AAmbiguousNameLvalue(atype.GetName())); } if (type is AVoidType) { AVoidType atype = (AVoidType)type; throw new Exception(Util.TokenToStringPos(atype.GetToken()) + " (Weeder)Unexpected type: void. It should not be possible for this error to occur"); } /*if (type is AArrayType) * { * AArrayType atype = (AArrayType)type; * throw new Exception(Util.TokenToStringPos(atype.GetToken()) + " (Weeder)Unexpected type: ArrayType. It should not be possible for this error to occur"); * }*/ throw new Exception("(Weeder)Unexpected type: none. It should not be possible for this error to occur"); }
public static void SetPrimitive(this ANamedType node, string primitive) { AAName name = (AAName)node.GetName(); TIdentifier lastIdentifier = (TIdentifier)name.GetIdentifier()[name.GetIdentifier().Count - 1]; name.GetIdentifier().Clear(); lastIdentifier.Text = primitive; name.GetIdentifier().Add(lastIdentifier); }
public static bool IsPrimitive(this ANamedType node) { AAName name = (AAName)node.GetName(); if (name.GetIdentifier().Count != 1) { return(false); } return(GalaxyKeywords.Primitives.words.Contains(((TIdentifier)name.GetIdentifier()[0]).Text)); }
public static bool IsPrimitive(this ANamedType node, string[] primitives) { AAName name = (AAName)node.GetName(); if (name.GetIdentifier().Count != 1) { return(false); } return(primitives.Contains(((TIdentifier)name.GetIdentifier()[0]).Text)); }
public static void GetMatchingTypes(ANamedType node, List <ATypedefDecl> typeDefs, List <AStructDecl> structs, List <AMethodDecl> delegates, List <TIdentifier> generics, out bool matchPrimitive) { List <string> names = new List <string>(); foreach (TIdentifier identifier in ((AAName)node.GetName()).GetIdentifier()) { names.Add(identifier.Text); } matchPrimitive = names.Count == 1 && GalaxyKeywords.Primitives.words.Contains(names[0]); GetMatchingTypes(node, names, typeDefs, structs, delegates, new List <ANamespaceDecl>(), generics); }
public static string TypeToIdentifierString(PType type) { if (type is AVoidType) { return("void"); } /*if (type is AArrayType) * { * AArrayType aType = (AArrayType)type; * return TypeToString(aType.GetType()) + "[" + aType.GetDimention().Text + "]"; * }*/ if (type is AArrayTempType) { AArrayTempType aType = (AArrayTempType)type; return(TypeToIdentifierString(aType.GetType()) + "Ar" + (aType.GetIntDim() == null ? "" : aType.GetIntDim().Text)); } if (type is ANamedType) { ANamedType aType = (ANamedType)type; return(((AAName)aType.GetName()).AsString().Replace('.', '_')); } if (type is APointerType) { APointerType aType = (APointerType)type; return("p" + TypeToIdentifierString(aType.GetType())); } if (type is ADynamicArrayType) { ADynamicArrayType aType = (ADynamicArrayType)type; return(TypeToIdentifierString(aType.GetType()) + "DAr"); } if (type is AGenericType) { AGenericType aType = (AGenericType)type; string ret = TypeToIdentifierString(aType.GetBase()) + "G_"; bool first = true; foreach (PType t in aType.GetGenericTypes()) { if (first) { first = false; } else { ret += "_"; } ret += TypeToIdentifierString(t); } return(ret + "_G"); } throw new Exception("Unknown type"); }
public override void CaseANamedType(ANamedType node) { AAName name = (AAName)node.GetName(); if (name.GetIdentifier().Count > 2) { return; } if (name.GetIdentifier().Count == 2 && ((TIdentifier)name.GetIdentifier()[0]).Text != "Dialogs") { return; } if (name.GetIdentifier().Count == 1) { bool foundDialogs = false; AASourceFile file = Util.GetAncestor <AASourceFile>(node); foreach (AUsingDecl usingDecl in file.GetUsings()) { if (usingDecl.GetNamespace().Count == 1) { TIdentifier identifer = (TIdentifier)usingDecl.GetNamespace()[0]; if (identifer.Text == "Dialogs") { foundDialogs = true; break; } } } if (!foundDialogs) { ANamespaceDecl ns = Util.GetAncestor <ANamespaceDecl>(node); if (!Util.HasAncestor <ANamespaceDecl>(ns.Parent()) && ns.GetName().Text == "Dialogs") { foundDialogs = true; } } if (!foundDialogs) { return; } } if (((TIdentifier)name.GetIdentifier()[name.GetIdentifier().Count - 1]).Text == oldName) { types.Add((TIdentifier)name.GetIdentifier()[name.GetIdentifier().Count - 1]); } }
public static bool IsSame(this ANamedType node1, ANamedType node2, bool primitiveOnly) { AAName name1 = (AAName)node1.GetName(); AAName name2 = (AAName)node2.GetName(); if (primitiveOnly && (name1.GetIdentifier().Count > 1 || name2.GetIdentifier().Count > 1)) { return(false); } if (name1.GetIdentifier().Count != name2.GetIdentifier().Count) { return(false); } for (int i = 0; i < name2.GetIdentifier().Count; i++) { TIdentifier identifier1 = (TIdentifier)name1.GetIdentifier()[i]; TIdentifier identifier2 = (TIdentifier)name2.GetIdentifier()[i]; if (identifier1.Text != identifier2.Text) { return(false); } } return(true); }
public override void OutAAProgram(AAProgram node) { foreach (var pair in Refferences) { if (structsWithGenerics.Contains(pair.Key) && pair.Value.Count > 0) { needAnotherPass = true; } } foreach (var pair in Refferences) { AStructDecl str = pair.Key; if (!copies.ContainsKey(str)) { copies[str] = new List <List <PType> >(); } IList declList; Node parent = str.Parent(); if (parent is AASourceFile) { declList = ((AASourceFile)parent).GetDecl(); } else { declList = ((ANamespaceDecl)parent).GetDecl(); } //AASourceFile pFile = (AASourceFile) str.Parent(); foreach (AGenericType refference in pair.Value) { AStructDecl clone = null; bool addList = true; foreach (List <PType> list in copies[str]) { bool listEqual = true; for (int i = 0; i < list.Count; i++) { if (!Util.TypesEqual(list[i], (PType)refference.GetGenericTypes()[i], data)) { listEqual = false; break; } } if (listEqual) { addList = false; clone = clones[list]; break; } } if (addList) { List <PType> list = new List <PType>(); foreach (PType type in refference.GetGenericTypes()) { list.Add(type); } copies[str].Add(list); clone = (AStructDecl)str.Clone(); declList.Insert(declList.IndexOf(str), clone); clone.Apply(new EnviromentBuilding(errors, data)); clone.Apply(new EnviromentChecking(errors, data, false)); clone.Apply(new LinkNamedTypes(errors, data)); /* * data.Structs.Add(new SharedData.DeclItem<AStructDecl>(pFile, clone)); * data.StructFields[clone] = new List<AALocalDecl>(); * data.StructMethods[clone] = new List<AMethodDecl>(); * data.StructProperties[clone] = new List<APropertyDecl>(); * data.StructConstructors[clone] = new List<AConstructorDecl>(); * foreach (PLocalDecl localDecl in clone.GetLocals()) * { * if (localDecl is AALocalDecl) * { * data.StructFields[clone].Add((AALocalDecl)localDecl); * } * else * { * PDecl decl = ((ADeclLocalDecl) localDecl).GetDecl(); * if (decl is AMethodDecl) * { * data.StructMethods[clone].Add((AMethodDecl) decl); * } * else if (decl is APropertyDecl) * { * data.StructProperties[clone].Add((APropertyDecl)decl); * } * else * { * data.StructConstructors[clone].Add((AConstructorDecl) decl); * } * } * }*/ clones[list] = clone; clone.setGenericVars(new ArrayList()); FixGenericLinks.Parse(str, clone, list, data); clone.GetName().Text = Util.TypeToIdentifierString(refference); } //Change refference to clone ANamedType baseRef = (ANamedType)refference.GetBase(); List <string> cloneNs = Util.GetFullNamespace(clone); cloneNs.Add(clone.GetName().Text); AAName aName = (AAName)baseRef.GetName(); aName.GetIdentifier().Clear(); foreach (var n in cloneNs) { aName.GetIdentifier().Add(new TIdentifier(n)); } data.StructTypeLinks[baseRef] = clone; refference.ReplaceBy(baseRef); } if (!needAnotherPass) { parent.RemoveChild(str); } } if (needAnotherPass) { Refferences.Clear(); structsWithGenerics.Clear(); needAnotherPass = false; CaseAAProgram(node); } }
private static void GetMatchingTypes(ANamedType node, List <string> names, List <ATypedefDecl> typeDefs, List <AStructDecl> structs, List <AMethodDecl> delegates, List <ANamespaceDecl> namespaces, List <TIdentifier> generics) { List <IList> decls = new List <IList>(); List <string> currentNamespace = Util.GetFullNamespace(node); AASourceFile currentSourceFile = Util.GetAncestor <AASourceFile>(node); if (names.Count == 1) { string name = names[0]; //Check generic vars AStructDecl currentStruct = Util.GetAncestor <AStructDecl>(node); if (currentStruct != null) { foreach (TIdentifier genericVar in currentStruct.GetGenericVars()) { if (genericVar.Text == name) { generics.Add(genericVar); } } } //Get all type decls and namespaces matching this name, visible from this location List <IList> visibleDecls = Util.GetVisibleDecls(node, ((AAName)node.GetName()).GetIdentifier().Count == 1); foreach (IList declList in visibleDecls) { bool sameFile = false; if (declList.Count > 0) { sameFile = currentSourceFile == Util.GetAncestor <AASourceFile>((PDecl)declList[0]); } foreach (PDecl decl in declList) { bool sameNS = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace(decl)); if (decl is ANamespaceDecl) { ANamespaceDecl aDecl = (ANamespaceDecl)decl; if (aDecl.GetName().Text == name) { namespaces.Add(aDecl); } continue; } if (decl is ATypedefDecl) { if (Util.IsAncestor(node, decl)) { continue; } ATypedefDecl aDecl = (ATypedefDecl)decl; if (aDecl.GetStatic() != null && !sameFile || aDecl.GetVisibilityModifier() is APrivateVisibilityModifier && !sameNS) { continue; } ANamedType namedType = (ANamedType)aDecl.GetName(); AAName aName = (AAName)namedType.GetName(); string n = ((TIdentifier)aName.GetIdentifier()[0]).Text; if (n == name) { typeDefs.Add(aDecl); } continue; } if (decl is AStructDecl) { AStructDecl aDecl = (AStructDecl)decl; if (!sameNS && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier) { continue; } if (aDecl.GetName().Text == name) { structs.Add(aDecl); } continue; } if (decl is AMethodDecl) { AMethodDecl aDecl = (AMethodDecl)decl; if (!sameNS && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier || !sameFile && aDecl.GetStatic() != null) { continue; } if (aDecl.GetDelegate() != null && aDecl.GetName().Text == name) { delegates.Add(aDecl); } continue; } } } } else { string name = names[names.Count - 1]; List <ANamespaceDecl> baseNamespaces = new List <ANamespaceDecl>(); List <string> baseNames = new List <string>(); baseNames.AddRange(names); baseNames.RemoveAt(baseNames.Count - 1); GetMatchingTypes(node, baseNames, new List <ATypedefDecl>(), new List <AStructDecl>(), new List <AMethodDecl>(), baseNamespaces, generics); foreach (ANamespaceDecl ns in baseNamespaces) { bool sameFile = currentSourceFile == Util.GetAncestor <AASourceFile>(ns); foreach (PDecl decl in ns.GetDecl()) { bool sameNS = Util.NamespacesEquals(currentNamespace, Util.GetFullNamespace(decl)); if (decl is ANamespaceDecl) { ANamespaceDecl aDecl = (ANamespaceDecl)decl; if (aDecl.GetName().Text == name) { namespaces.Add(aDecl); } continue; } if (decl is ATypedefDecl) { ATypedefDecl aDecl = (ATypedefDecl)decl; ANamedType namedType = (ANamedType)aDecl.GetName(); AAName aName = (AAName)namedType.GetName(); string n = ((TIdentifier)aName.GetIdentifier()[0]).Text; if (n == name) { typeDefs.Add(aDecl); } continue; } if (decl is AStructDecl) { AStructDecl aDecl = (AStructDecl)decl; if (!sameNS && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier) { continue; } if (aDecl.GetName().Text == name) { structs.Add(aDecl); } continue; } if (decl is AMethodDecl) { AMethodDecl aDecl = (AMethodDecl)decl; if (!sameNS && aDecl.GetVisibilityModifier() is APrivateVisibilityModifier || !sameFile && aDecl.GetStatic() != null) { continue; } if (aDecl.GetDelegate() != null && aDecl.GetName().Text == name) { delegates.Add(aDecl); } continue; } } } } }
public override void OutANamedType(ANamedType node) { if (node.Parent() is ATypedefDecl && ((ATypedefDecl)node.Parent()).GetName() == node) { return; } //Link named type to their definition (structs) List <ATypedefDecl> typeDefs = new List <ATypedefDecl>(); List <AStructDecl> structs = new List <AStructDecl>(); List <AMethodDecl> delegates = new List <AMethodDecl>(); List <TIdentifier> generics = new List <TIdentifier>(); bool matchPrimitive; GetMatchingTypes(node, typeDefs, structs, delegates, generics, out matchPrimitive); int matches = typeDefs.Count + structs.Count + delegates.Count + (matchPrimitive ? 1 : 0) + generics.Count; if (matches == 0) { errors.Add(new ErrorCollection.Error(node.GetToken(), "Could not find any types matching " + ((AAName)node.GetName()).AsString()), true); } else if (generics.Count != 1 && matches > 1) { List <ErrorCollection.Error> subError = new List <ErrorCollection.Error>(); if (matchPrimitive) { subError.Add(new ErrorCollection.Error(node.GetToken(), "Matches primitive " + ((AAName)node.GetName()).AsString())); } foreach (ATypedefDecl typeDef in typeDefs) { subError.Add(new ErrorCollection.Error(typeDef.GetToken(), "Matching typedef")); } foreach (AStructDecl structDecl in structs) { subError.Add(new ErrorCollection.Error(structDecl.GetName(), "Matching " + Util.GetTypeName(structDecl))); } foreach (AMethodDecl methodDecl in delegates) { subError.Add(new ErrorCollection.Error(methodDecl.GetName(), "Matching delegate")); } foreach (TIdentifier identifier in generics) { subError.Add(new ErrorCollection.Error(identifier, "Matching generic")); } errors.Add( new ErrorCollection.Error(node.GetToken(), "Found multiple types matching " + ((AAName)node.GetName()).AsString(), false, subError.ToArray()), true); } else { if (generics.Count == 1) { data.GenericLinks[node] = generics[0]; return; } if (typeDefs.Count == 1) { ATypedefDecl typeDef = typeDefs[0]; //data.TypeDefLinks[node] = typeDef; PType type = (PType)typeDef.GetType().Clone(); node.ReplaceBy(type); type.Apply(this); return; } if (structs.Count == 1) { data.StructTypeLinks[node] = structs[0]; } else if (delegates.Count == 1) { data.DelegateTypeLinks[node] = delegates[0]; } if (!matchPrimitive && !(structs.Count == 1 && data.Enums.ContainsKey(structs[0])) && node.Parent() is AEnrichmentDecl) //Not allowed to enrich a struct, class or delegate { errors.Add(new ErrorCollection.Error(node.GetToken(), "You can not enrich this type.")); } } }
private AStructDecl Lookup(ANamedType type) { //Look for structs List <AStructDecl> structs = new List <AStructDecl>(); bool matchingPrimitive; LinkNamedTypes.GetMatchingTypes(type, new List <ATypedefDecl>(), structs, new List <AMethodDecl>(), new List <TIdentifier>(), out matchingPrimitive); if (structs.Count == 0) { errors.Add(new ErrorCollection.Error(type.GetToken(), "No type found named " + ((AAName)type.GetName()).AsString()), true); throw new ParserException(null, null); } else if (structs.Count > 1) { List <ErrorCollection.Error> subError = new List <ErrorCollection.Error>(); foreach (AStructDecl structDecl in structs) { subError.Add(new ErrorCollection.Error(structDecl.GetName(), "Matching " + Util.GetTypeName(structDecl))); } errors.Add( new ErrorCollection.Error(type.GetToken(), "Found multiple structs/classes matching " + ((AAName)type.GetName()).AsString(), false, subError.ToArray()), true); } return(structs[0]); }
public static StringBuilder TypeToStringBuilder(PType type) { if (type is AVoidType) { return(new StringBuilder("void")); } /*if (type is AArrayType) * { * AArrayType aType = (AArrayType)type; * return TypeToString(aType.GetType()) + "[" + aType.GetDimention().Text + "]"; * }*/ if (type is AArrayTempType) { AArrayTempType aType = (AArrayTempType)type; StringBuilder builder = new StringBuilder(); builder.Append(TypeToStringBuilder(aType.GetType())); builder.Append("["); if (aType.GetIntDim() != null) { builder.Append(aType.GetIntDim().Text); } builder.Append("]"); return(builder); } if (type is ANamedType) { ANamedType aType = (ANamedType)type; StringBuilder builder = new StringBuilder(); foreach (TIdentifier identifier in ((AAName)aType.GetName()).GetIdentifier()) { if (builder.Length != 0) { builder.Append("."); } builder.Append(identifier.Text); } return(builder); } if (type is APointerType) { APointerType aType = (APointerType)type; StringBuilder builder = new StringBuilder(); builder.Append(TypeToStringBuilder(aType.GetType())); builder.Append("*"); return(builder); } if (type is ADynamicArrayType) { ADynamicArrayType aType = (ADynamicArrayType)type; StringBuilder builder = new StringBuilder(); builder.Append(TypeToStringBuilder(aType.GetType())); builder.Append("[]"); return(builder); } if (type is AGenericType) { AGenericType aType = (AGenericType)type; StringBuilder builder = new StringBuilder(); builder.Append(TypeToStringBuilder(aType.GetBase())); builder.Append("<"); bool first = true; foreach (PType t in aType.GetGenericTypes()) { if (first) { first = false; } else { builder.Append(", "); } builder.Append(TypeToStringBuilder(t)); } builder.Append(">"); return(builder); } throw new Exception("Unknown type. Got " + type); }
public static bool TypesEqual(PType t1, PType t2, SharedData data) { if (t1.GetType() != t2.GetType()) { return(false); } if (t1 is AVoidType) { return(true); } if (t1 is AArrayTempType) { AArrayTempType aT1 = (AArrayTempType)t1; AArrayTempType aT2 = (AArrayTempType)t2; return(TypesEqual(aT1.GetType(), aT2.GetType(), data) && ReturnsTheSame(aT1.GetDimention(), aT2.GetDimention(), data)); } if (t1 is ADynamicArrayType) { ADynamicArrayType aT1 = (ADynamicArrayType)t1; ADynamicArrayType aT2 = (ADynamicArrayType)t2; return(TypesEqual(aT1.GetType(), aT2.GetType(), data)); } if (t1 is ANamedType) { ANamedType aT1 = (ANamedType)t1; ANamedType aT2 = (ANamedType)t2; if (aT1.IsPrimitive() && aT2.IsPrimitive()) { return(((AAName)aT1.GetName()).AsString() == ((AAName)aT2.GetName()).AsString()); } if (data.StructTypeLinks.ContainsKey(aT1) != data.StructTypeLinks.ContainsKey(aT2)) { return(false); } if (data.StructTypeLinks.ContainsKey(aT1) && data.StructTypeLinks[aT1] != data.StructTypeLinks[aT2]) { return(false); } if (data.DelegateTypeLinks.ContainsKey(aT1) != data.DelegateTypeLinks.ContainsKey(aT2)) { return(false); } if (data.DelegateTypeLinks.ContainsKey(aT1) && data.DelegateTypeLinks[aT1] != data.DelegateTypeLinks[aT2]) { return(false); } return(true); } if (t1 is ANullType) { return(true); } if (t1 is APointerType) { APointerType aT1 = (APointerType)t1; APointerType aT2 = (APointerType)t2; return(TypesEqual(aT1.GetType(), aT2.GetType(), data)); } if (t1 is AGenericType) { AGenericType aT1 = (AGenericType)t1; AGenericType aT2 = (AGenericType)t2; if (!TypesEqual(aT1.GetBase(), aT2.GetBase(), data)) { return(false); } if (aT1.GetGenericTypes().Count != aT2.GetGenericTypes().Count) { return(false); } for (int i = 0; i < aT1.GetGenericTypes().Count; i++) { if (!TypesEqual((PType)aT1.GetGenericTypes()[i], (PType)aT2.GetGenericTypes()[i], data)) { return(false); } } return(true); } throw new Exception("Util.TypesEqual: Unexpected type. Got + " + t1.GetType()); }
public static string AsIdentifierString(this ANamedType node) { return(((AAName)node.GetName()).AsIdentifierString()); }
public static Token GetToken(this ANamedType node) { return(GetToken((AAName)node.GetName())); }