public override string GetNamespaceName(Namespace space) { using (TextWriter writer = new StringWriter()) { writer.Write("N:"); WriteNamespace(space, writer); return (writer.ToString()); } }
private void ParseNamespaceMemberDeclarations(Namespace/*!*/ parentNamespace, TokenSet followers){ TokenSet attributeOrNamespaceOrTypeDeclarationStartOrLastIdentifier = Parser.AttributeOrNamespaceOrTypeDeclarationStart|Token.LastIdentifier; TokenSet followersOrAttributeOrNamespaceOrTypeDeclarationStartOrLastIdentifier = followers|attributeOrNamespaceOrTypeDeclarationStartOrLastIdentifier; while (attributeOrNamespaceOrTypeDeclarationStartOrLastIdentifier[this.currentToken] || this.currentToken == Token.Identifier) this.ParseNamespaceOrTypeDeclarations(parentNamespace, followersOrAttributeOrNamespaceOrTypeDeclarationStartOrLastIdentifier); //if (this.sink != null && this.currentToken == Token.EndOfFile){ // TypeAlias ta = new TypeAlias(new TypeExpression(new Identifier(" ", this.scanner.CurrentSourceContext)), Identifier.Empty); // parentNamespace.Types.Add(ta); //} Debug.Assert(followers[this.currentToken]); }
public override Namespace VisitNamespace(Namespace nspace) { this.VisitTypeNodeList(nspace.Types); return nspace; }
CompilationUnit CreateCompilationUnit(Module module, TypeNode type) { CompilationUnit cu = new CompilationUnit(); cu.Namespaces = new NamespaceList(); cu.TargetModule = module; this.cunit = cu; this.module = module; Namespace ns = new Namespace(Identifier.Empty, Identifier.Empty, new AliasDefinitionList(), new UsedNamespaceList(), new NamespaceList(), new TypeNodeList()); cu.Namespaces.Add(ns); ns.UsedNamespaces.Add(new UsedNamespace(Identifier.For("System"))); ns.UsedNamespaces.Add(new UsedNamespace(Identifier.For("System.Xml"))); ns.UsedNamespaces.Add(new UsedNamespace(Identifier.For("Microsoft.Comega"))); // XmlSerializationWriter CreateSerializerFor(type); return cu; }
/// <summary> /// Store the type in the catalog indexed by namespace /// </summary> /// <param name="type">The type to store in the catalog</param> private void StoreType(TypeNode type) { string spaceName = GetNamespaceName(type); Namespace space; if(!catalog.TryGetValue(spaceName, out space)) { space = new Namespace(new Identifier(spaceName)); catalog.Add(spaceName, space); } if(space.Types == null) throw new InvalidOperationException("Null type list encountered"); space.Types.Add(type); // Store nested types as well foreach(var member in type.Members) { TypeNode nestedType = member as TypeNode; if(nestedType != null) this.StoreType(nestedType); } }
public override bool IsExposedNamespace(Namespace space) { return (true); }
// Members private void WriteNamespaceElements(Namespace space) { TypeNodeList types = space.Types; if (types.Count == 0) return; writer.WriteStartElement("elements"); for (int i = 0; i < types.Count; i++) { TypeNode type = types[i]; //skip hidden types, but if a type is not exposed and has exposed members we must add it if (!ApiFilter.IsExposedType(type) && !ApiFilter.HasExposedMembers(type)) continue; writer.WriteStartElement("element"); writer.WriteAttributeString("api", namer.GetTypeName(type)); writer.WriteEndElement(); } writer.WriteEndElement(); }
protected override void VisitNamespace(Namespace space) { parsedNamespaces.Add(space); WriteNamespace(space); base.VisitNamespace(space); }
public virtual void FindTypesToBeDuplicated(Namespace nspace) { if (nspace == null) return; this.FindTypesToBeDuplicated(nspace.Types); this.FindTypesToBeDuplicated(nspace.NestedNamespaces); }
private void ParseTypeDeclaration(Namespace ns, TypeNode parentType, AttributeList attributes, TokenList modifierTokens, SourceContextList modifierContexts, TypeFlags flags, bool isPartial, SourceContext sctx, TokenSet followers){ if (parentType is Interface){ this.HandleError(Error.InterfacesCannotContainTypes); modifierTokens = null; } TypeNode t = null; InvariantCt = 0; switch(this.currentToken){ case Token.Class: Class c = new Class(); t = c; if (parentType == null) t.DeclaringNamespace = ns; else t.DeclaringType = parentType; if (modifierTokens != null) t.Flags |= this.NestedTypeFlags(modifierTokens, modifierContexts, t, isPartial)|TypeFlags.BeforeFieldInit; else{ t.IsUnsafe = this.inUnsafeCode; t.Flags |= flags|TypeFlags.BeforeFieldInit; } if (t.IsAbstract && t.IsSealed && t.IsSpecialName){ c.IsAbstractSealedContainerForStatics = true; c.Flags &= ~TypeFlags.SpecialName; } break; case Token.Interface: t = new Interface(); if (parentType == null) t.DeclaringNamespace = ns; else t.DeclaringType = parentType; if (modifierTokens != null) t.Flags |= this.NestedTypeFlags(modifierTokens, modifierContexts, t, isPartial); else{ if ((flags & TypeFlags.Abstract) != 0){ if ((flags & TypeFlags.Sealed) != 0 && (flags & TypeFlags.SpecialName) != 0){ this.HandleError(Error.InvalidModifier, "static"); flags &= ~(TypeFlags.Abstract|TypeFlags.Sealed|TypeFlags.SpecialName); }else{ this.HandleError(Error.InvalidModifier, "abstract"); flags &= ~TypeFlags.Abstract; } }else if ((flags & TypeFlags.Sealed) != 0){ this.HandleError(Error.InvalidModifier, "sealed"); flags &= ~TypeFlags.Sealed; } t.IsUnsafe = this.inUnsafeCode; t.Flags |= flags|TypeFlags.BeforeFieldInit; } break; case Token.Struct: t = new Struct(); if (parentType == null) t.DeclaringNamespace = ns; else t.DeclaringType = parentType; if (modifierTokens != null) t.Flags |= this.NestedTypeFlags(modifierTokens, modifierContexts, t, isPartial)|TypeFlags.BeforeFieldInit; else{ if ((flags & TypeFlags.Abstract) != 0){ if ((flags & TypeFlags.Sealed) != 0 && (flags & TypeFlags.SpecialName) != 0){ this.HandleError(Error.InvalidModifier, "static"); flags &= ~(TypeFlags.Abstract|TypeFlags.Sealed|TypeFlags.SpecialName); }else{ this.HandleError(Error.InvalidModifier, "abstract"); flags &= ~TypeFlags.Abstract; } }else if ((flags & TypeFlags.Sealed) != 0){ this.HandleError(Error.InvalidModifier, "sealed"); } t.IsUnsafe = this.inUnsafeCode; t.Flags |= flags|TypeFlags.BeforeFieldInit; } break; default: Debug.Assert(false); break; } t.Attributes = attributes; t.SourceContext = sctx; t.DeclaringModule = this.module; t.Documentation = this.LastDocComment; this.GetNextToken(); t.Name = this.scanner.GetIdentifier(); if (Parser.IdentifierOrNonReservedKeyword[this.currentToken]) this.GetNextToken(); else{ this.SkipIdentifierOrNonReservedKeyword(); if (Parser.IdentifierOrNonReservedKeyword[this.currentToken]){ t.Name = this.scanner.GetIdentifier(); this.GetNextToken(); } } if (this.currentToken == Token.LessThan) this.ParseTypeParameters(t, followers|Token.Colon|Token.LeftBrace|Token.Where); if (parentType != null){ t.Namespace = Identifier.Empty; if (parentType.IsGeneric) t.IsGeneric = true; }else t.Namespace = ns.FullNameId; Identifier mangledName = t.Name; if (Cci.TargetPlatform.GenericTypeNamesMangleChar != 0) { int numPars = t.TemplateParameters == null ? 0 : t.TemplateParameters.Count; if (numPars > 0){ mangledName = new Identifier(t.Name.ToString() + Cci.TargetPlatform.GenericTypeNamesMangleChar + numPars.ToString(), t.Name.SourceContext); t.IsGeneric = this.useGenerics; } } t.PartiallyDefines = this.GetCompleteType(t, mangledName, isPartial); if (isPartial){ isPartial = t.PartiallyDefines != null; if (!isPartial) t.Name = new Identifier(t.Name+" "+t.UniqueKey, t.Name.SourceContext); }else isPartial = t.PartiallyDefines != null; if (parentType != null){ if (!isPartial || parentType.PartiallyDefines != null) parentType.Members.Add(t); }else{ ns.Types.Add(t); if (!isPartial) this.AddTypeToModule(t); } if (this.currentToken == Token.Colon){ this.GetNextToken(); t.Interfaces = this.ParseInterfaceList(followers|Token.LeftBrace|Token.Where, true); //The first of these might be the base class, but that is a semantic issue }else t.Interfaces = new InterfaceList(); //TODO: omit this? t.InterfaceExpressions = t.Interfaces; while (this.currentToken == Token.Where) this.ParseTypeParameterConstraint(t, followers|Token.LeftBrace|Token.Where); t.SourceContext.EndPos = this.scanner.endPos; SourceContext typeBodyCtx = this.scanner.CurrentSourceContext; this.Skip(Token.LeftBrace); tryAgain: this.ParseTypeMembers(t, followers|Token.RightBrace); if (this.currentToken == Token.Namespace){ this.HandleError(Error.InvalidMemberDecl, this.scanner.CurrentSourceContext.SourceText); this.currentToken = Token.Class; goto tryAgain; } int endCol = this.scanner.endPos; this.ParseBracket(t.SourceContext, Token.RightBrace, followers|Token.Semicolon, Error.ExpectedRightBrace); t.SourceContext.EndPos = endCol; t.Name = mangledName; if (this.currentToken == Token.Semicolon) this.GetNextToken(); if (this.sink != null){ typeBodyCtx.EndPos = endCol; this.sink.AddCollapsibleRegion(typeBodyCtx, false); } this.SkipTo(followers|Parser.TypeMemberStart); if (!followers[this.currentToken]) this.SkipTo(followers, Error.NamespaceUnexpected); if (isPartial) this.MergeWithCompleteType(t); }
private void ParseTypeDeclaration(Namespace ns, TypeNode parentType, AttributeList attributes, TokenList modifierTokens, SourceContextList modifierContexts, bool isPartial, SourceContext sctx, TokenSet followers){ this.ParseTypeDeclaration(ns, parentType, attributes, modifierTokens, modifierContexts, TypeFlags.None, isPartial, sctx, followers); }
private void ParseTypeDeclaration(Namespace ns, TypeNode parentType, AttributeList attributes, TypeFlags flags, bool isPartial, SourceContext sctx, TokenSet followers){ this.ParseTypeDeclaration(ns, parentType, attributes, null, null, flags, isPartial, sctx, followers); }
private AttributeList ParseAttributes(Namespace ns, TokenSet followers){ if (this.currentToken != Token.LeftBracket) return null; AttributeList attributes = new AttributeList(); bool allowGlobalAttributes = ns != null && ns.Name == Identifier.Empty && (ns.NestedNamespaces == null || ns.NestedNamespaces.Count == 0) && (ns.Types == null || ns.Types.Count == 0); while(this.currentToken == Token.LeftBracket){ SourceContext sctx = this.scanner.CurrentSourceContext; this.GetNextToken(); AttributeTargets flags = (AttributeTargets)0; Identifier id = this.scanner.GetIdentifier(); SourceContext attrCtx = this.scanner.CurrentSourceContext; switch(this.currentToken){ case Token.Event: case Token.Identifier: case Token.Return: this.GetNextToken(); if (this.currentToken == Token.Colon){ this.GetNextToken(); int key = id.UniqueIdKey; if (key == Parser.assemblyId.UniqueIdKey) flags = AttributeTargets.Assembly; else if (key == Parser.eventId.UniqueIdKey) flags = AttributeTargets.Event; else if (key == Parser.fieldId.UniqueIdKey) flags = AttributeTargets.Field; else if (key == Parser.methodId.UniqueIdKey) flags = AttributeTargets.Method|AttributeTargets.Constructor; else if (key == Parser.moduleId.UniqueIdKey) flags = AttributeTargets.Module; else if (key == Parser.paramId.UniqueIdKey) flags = AttributeTargets.Parameter; else if (key == Parser.propertyId.UniqueIdKey) flags = AttributeTargets.Property; else if (key == Parser.returnId.UniqueIdKey) flags = AttributeTargets.ReturnValue; else if (key == Parser.typeId.UniqueIdKey) flags = AttributeTargets.Class|AttributeTargets.Delegate|AttributeTargets.Enum|AttributeTargets.Interface|AttributeTargets.Struct; else{ flags = (AttributeTargets)int.MaxValue; this.HandleError(Error.InvalidAttributeLocation, id.ToString()); } id = this.scanner.GetIdentifier(); this.SkipIdentifierOrNonReservedKeyword(); } break; default: this.SkipIdentifierOrNonReservedKeyword(); break; } for(;;){ AttributeNode attr = new AttributeNode(); attr.SourceContext = attrCtx; attr.Target = flags; if (this.currentToken == Token.DoubleColon){ Identifier prefix = id; this.GetNextToken(); id = this.scanner.GetIdentifier(); id.Prefix = prefix; id.SourceContext.StartPos = prefix.SourceContext.StartPos; this.SkipIdentifierOrNonReservedKeyword(); } if (this.sink != null) this.sink.StartName(id); if (this.currentToken == Token.Dot) attr.Constructor = this.ParseQualifiedIdentifier(id, followers|Token.Comma|Token.LeftParenthesis|Token.RightBracket); else attr.Constructor = id; this.ParseAttributeArguments(attr, followers|Token.Comma|Token.RightBracket); if (flags != (AttributeTargets)int.MaxValue){ if (allowGlobalAttributes && (flags == AttributeTargets.Assembly || flags == AttributeTargets.Module)){ if (ns.Attributes == null) ns.Attributes = new AttributeList(); ns.Attributes.Add(attr); }else attributes.Add(attr); } if (this.currentToken != Token.Comma) break; this.GetNextToken(); if (this.currentToken == Token.RightBracket) break; id = this.scanner.GetIdentifier(); this.SkipIdentifierOrNonReservedKeyword(); } this.ParseBracket(sctx, Token.RightBracket, followers, Error.ExpectedRightBracket); } this.SkipTo(followers); return attributes; }
private void ParseTypeDeclarations(Namespace ns, TokenSet followers){ for(;;){ SourceContext sctx = this.scanner.CurrentSourceContext; AttributeList attributes = this.ParseAttributes(ns, followers|Parser.AttributeOrTypeDeclarationStart); Token tok = this.currentToken; this.inUnsafeCode = false; TypeFlags flags = this.ParseTypeModifiers(); switch (this.currentToken){ case Token.Class: case Token.Interface: case Token.Struct: this.ParseTypeDeclaration(ns, null, attributes, flags, false, sctx, followers); break; case Token.Delegate: this.ParseDelegateDeclaration(ns, null, attributes, flags, sctx, followers); break; case Token.Enum: this.ParseEnumDeclaration(ns, null, attributes, flags, sctx, followers); break; case Token.Namespace: if (tok != this.currentToken || (attributes != null && attributes.Count > 0)) this.HandleError(Error.BadTokenInType); return; case Token.LeftBracket: this.HandleError(Error.BadTokenInType); this.GetNextToken(); return; case Token.MultiLineDocCommentStart: case Token.SingleLineDocCommentStart: this.ParseDocComment(followers); break; case Token.Partial: SourceContext pctx = this.scanner.CurrentSourceContext; this.GetNextToken(); switch (this.currentToken){ case Token.Class: case Token.Struct: case Token.Interface: this.ParseTypeDeclaration(ns, null, attributes, flags, true, sctx, followers); break; case Token.Enum: this.HandleError(Error.PartialMisplaced); this.ParseEnumDeclaration(ns, null, attributes, flags, sctx, followers); break; default: if (this.currentToken == Token.Namespace) this.HandleError(Error.BadModifiersOnNamespace); else this.HandleError(Error.PartialMisplaced); this.SkipTo(followers, Error.None); return; } break; default: if (!followers[this.currentToken]) this.SkipTo(followers, Error.BadTokenInType); else if (this.currentToken == Token.EndOfFile && attributes != null){ Class dummy = new Class(this.module, null, attributes, TypeFlags.Public, ns.Name, Identifier.Empty, null, null, new MemberList(0)); ns.Types.Add(dummy); //this.sink = null; } return; } this.inUnsafeCode = false; } }
private void ParseNamespaceOrTypeDeclarations(Namespace parentNamespace, TokenSet followers){ if (this.currentToken == Token.Identifier){ TypeAlias ta = new TypeAlias(new TypeExpression(this.scanner.GetIdentifier()), Identifier.Empty); parentNamespace.Types.Add(ta); ta.SourceContext = this.scanner.CurrentSourceContext; this.GetNextToken(); return; } if (this.currentToken == Token.Private || this.currentToken == Token.Protected){ this.HandleError(Error.PrivateOrProtectedNamespaceElement); this.GetNextToken(); } if (this.currentToken == Token.Namespace){ SourceContext openContext = this.scanner.CurrentSourceContext; this.GetNextToken(); TokenSet nsFollowers = followers|Token.RightBrace; Identifier nsId = this.ParseNamespaceName(Identifier.Empty, false, nsFollowers|Token.LeftBrace); Identifier nsFullId = parentNamespace.Name == Identifier.Empty ? nsId : Identifier.For(parentNamespace.FullName+"."+nsId); Namespace ns = new Namespace(nsId, nsFullId, new AliasDefinitionList(), new UsedNamespaceList(), new NamespaceList(), new TypeNodeList()); ns.DeclaringNamespace = parentNamespace; ns.SourceContext = openContext; if (this.currentToken == Token.LeftBrace) openContext.EndPos = this.scanner.endPos; SourceContext nsBodyCtx = this.scanner.CurrentSourceContext; this.Skip(Token.LeftBrace); parentNamespace.NestedNamespaces.Add(ns); this.ParseExternalAliasDirectives(ns, nsFollowers|Parser.NamespaceOrTypeDeclarationStart|Token.Using|Token.LastIdentifier); this.ParseUsingDirectives(ns, nsFollowers|Token.LastIdentifier|Parser.NamespaceOrTypeDeclarationStart); this.ParseNamespaceMemberDeclarations(ns, nsFollowers); if (this.currentToken == Token.RightBrace && this.sink != null) this.sink.MatchPair(openContext, this.scanner.CurrentSourceContext); ns.SourceContext.EndPos = this.scanner.endPos; this.Skip(Token.RightBrace); if (this.currentToken == Token.Semicolon) this.GetNextToken(); if (this.sink != null){ nsBodyCtx.EndPos = this.scanner.endPos; this.sink.AddCollapsibleRegion(nsBodyCtx, false); } }else this.ParseTypeDeclarations(parentNamespace, followers); if (!followers[this.currentToken]) this.SkipTo(followers, Error.EOFExpected); }
private static int CompareNamespaces(Namespace a, Namespace b) { return (String.Compare(a.Name.Name, b.Name.Name)); }
private void StoreType(TypeNode type) { //Console.WriteLine("type: {0} ", type.Name); /* if (type.Name == null) { // CCI seems to occasionally construct corrupted, phantom types, which we should reject // Console.WriteLine("unidentified type rejected"); return; } */ string spaceName = GetNamespaceName(type); //Console.WriteLine("in space: {0}", spaceName); Namespace space; if (!catalog.TryGetValue(spaceName, out space)) { space = new Namespace(new Identifier(spaceName)); catalog.Add(spaceName, space); } if (space.Types == null) Console.WriteLine("null type list"); space.Types.Add(type); //Console.WriteLine("getting members"); MemberList members = type.Members; //Console.WriteLine("got {0} members", members.Count); for (int i = 0; i < members.Count; i++) { TypeNode nestedType = members[i] as TypeNode; if (nestedType != null) { //Console.WriteLine("nested type {0}", type.FullName); StoreType(nestedType); } } //Console.WriteLine("done storing type"); }
public override Namespace VisitNamespace(Namespace nspace) { if (nspace == null) return null; return base.VisitNamespace((Namespace)nspace.Clone()); }
private void WriteNamespace(Namespace space) { writer.WriteStartElement("api"); writer.WriteAttributeString("id", namer.GetNamespaceName(space)); StartElementCallbacks("api", space); WriteApiData(space); WriteNamespaceElements(space); EndElementCallbacks("api", space); writer.WriteEndElement(); }
/// <summary> /// This is used to see if a namespace is exposed /// </summary> /// <param name="space">The namespace to check</param> /// <returns>True if the namespace contains any exposed types or types with exposed members, false if not</returns> public virtual bool IsExposedNamespace(Namespace space) { if(space == null) throw new ArgumentNullException("space"); // !EFW - Bug fix. Some obfuscated assemblies have mangled names containing characters that // are not valid in XML. Exclude those by default. if(space.FullName.HasInvalidXmlCharacters()) return false; string name = space.Name.Name; // Look in cache to see if namespace exposure is already determined bool exposed; if(!namespaceCache.TryGetValue(name, out exposed)) { // The namespace is exposed if any types in it are exposed exposed = this.NamespaceContainsExposedTypes(space) ?? false; // the namespace is also exposed if it contains exposed members, even if all types are hidden if(!exposed) exposed = this.NamespaceContainsExposedMembers(space); // Cache the result namespaceCache.Add(name, exposed); } return exposed; }
private void WriteNamespaceReference(Namespace space) { writer.WriteStartElement("namespace"); writer.WriteAttributeString("api", namer.GetNamespaceName(space)); writer.WriteEndElement(); }
/// <summary> /// Check for any exposed types in a namespace /// </summary> /// <param name="space">The namespace to check</param> /// <returns>True if the namespace contains exposed types, false if it does not, or null if there are no /// exposed types but the API filter is empty.</returns> private bool? NamespaceContainsExposedTypes(Namespace space) { foreach(var type in space.Types) if(this.IsExposedType(type)) return true; return (apiFilter.NamespaceFilterCount == 0) ? (bool?)null : false; }
public virtual Namespace VisitNamespace(Namespace nspace) { if (nspace == null) return null; nspace.AliasDefinitions = this.VisitAliasDefinitionList(nspace.AliasDefinitions); nspace.UsedNamespaces = this.VisitUsedNamespaceList(nspace.UsedNamespaces); nspace.Attributes = this.VisitAttributeList(nspace.Attributes); nspace.Types = this.VisitTypeNodeList(nspace.Types); nspace.NestedNamespaces = this.VisitNamespaceList(nspace.NestedNamespaces); return nspace; }
/// <summary> /// Check for any exposed members in any of the types /// </summary> /// <param name="space">The namespace to check</param> /// <returns>True if the type has an exposed member filter and it is matched are set to true.</returns> /// <remarks>This is used to determine if the namespace should be visited. If the namespace and all /// types are set to false for exposed, we still want to visit them if any members are visible.</remarks> private bool NamespaceContainsExposedMembers(Namespace space) { foreach(var type in space.Types) if(this.HasExposedMembers(type)) return true; return false; }
/// <summary> /// This is used to visit a namespace /// </summary> /// <param name="space">The namespace to visit</param> protected virtual void VisitNamespace(Namespace space) { this.VisitEntity(space); this.VisitTypes(space.Types); }
/// <summary> /// Check to see if the namespace is exposed or not by this entry /// </summary> /// <param name="space">The namespace to check</param> /// <returns>Null if the namespace is not represented by this entry, true if it is and it is exposed or /// false if it is and it is not exposed.</returns> public bool? IsExposedNamespace(Namespace space) { return (space.Name.Name == name) ? exposed : (bool?)null; }
//===================================================================== /// <inheritdoc /> public override string GetNamespaceName(Namespace space) { return "N:" + space.Name; }
protected virtual void VisitNamespace(Namespace space) { //Console.WriteLine("Visit Entity {0}",space.FullName); VisitEntity(space); TypeNodeList types = space.Types; VisitTypes(types); }
/// <summary> /// This method is used to get the namespace name /// </summary> /// <param name="space">The namespace for which to get the name</param> /// <returns>The namespace name</returns> public abstract string GetNamespaceName(Namespace space);
protected virtual void VisitNamespaces(NamespaceList spaces) { // sort namespaces by name Namespace[] sorted_spaces = new Namespace[spaces.Count]; for (int i = 0; i < spaces.Count; i++) sorted_spaces[i] = spaces[i]; Array.Sort < Namespace >(sorted_spaces, namespaceComparison); // visit them foreach (Namespace space in sorted_spaces) { if (filter.IsExposedNamespace(space)) VisitNamespace(space); } }