private TypeSymbol CreateArrayTypeCore(TypeSymbol itemTypeSymbol) { TypeSymbol arrayTypeSymbol = (TypeSymbol)((ISymbolTable)_systemNamespace).FindSymbol("Array", null, SymbolFilter.Types); Debug.Assert(arrayTypeSymbol != null); TypeSymbol specificArrayTypeSymbol = new ClassSymbol("Array", _systemNamespace); foreach (MemberSymbol memberSymbol in arrayTypeSymbol.Members) { specificArrayTypeSymbol.AddMember(memberSymbol); } IndexerSymbol indexerSymbol = new IndexerSymbol(specificArrayTypeSymbol, itemTypeSymbol, MemberVisibility.Public); indexerSymbol.SetIntrinsic(); specificArrayTypeSymbol.AddMember(indexerSymbol); specificArrayTypeSymbol.SetIgnoreNamespace(); specificArrayTypeSymbol.SetArray(); return(specificArrayTypeSymbol); }
private IndexerSymbol BuildIndexer(IndexerDeclarationNode indexerNode, TypeSymbol typeSymbol) { TypeSymbol indexerType = typeSymbol.SymbolSet.ResolveType(indexerNode.Type, _symbolTable, typeSymbol); Debug.Assert(indexerType != null); if (indexerType != null) { IndexerSymbol indexer = new IndexerSymbol(typeSymbol, indexerType); BuildMemberDetails(indexer, typeSymbol, indexerNode, indexerNode.Attributes); if (AttributeNode.FindAttribute(indexerNode.Attributes, "IntrinsicProperty") != null) { indexer.SetIntrinsic(); } SymbolImplementationFlags implFlags = SymbolImplementationFlags.Regular; if (indexerNode.SetAccessor == null) { implFlags |= SymbolImplementationFlags.ReadOnly; } if ((indexerNode.Modifiers & Modifiers.Abstract) != 0) { implFlags |= SymbolImplementationFlags.Abstract; } else if ((indexerNode.Modifiers & Modifiers.Override) != 0) { implFlags |= SymbolImplementationFlags.Override; } indexer.SetImplementationState(implFlags); Debug.Assert(indexerNode.Parameters.Count != 0); foreach (ParameterNode parameterNode in indexerNode.Parameters) { ParameterSymbol paramSymbol = BuildParameter(parameterNode, indexer); if (paramSymbol != null) { paramSymbol.SetParseContext(parameterNode); indexer.AddParameter(paramSymbol); } } indexer.AddParameter(new ParameterSymbol("value", indexer, indexerType, ParameterMode.In)); return indexer; } return null; }
private MemberSymbol CreateGenericMember(MemberSymbol templateMember, IList<TypeSymbol> typeArguments) { TypeSymbol parentType = (TypeSymbol)templateMember.Parent; TypeSymbol instanceAssociatedType; if (templateMember.AssociatedType.Type == SymbolType.GenericParameter) { GenericParameterSymbol genericParameter = (GenericParameterSymbol)templateMember.AssociatedType; instanceAssociatedType = typeArguments[genericParameter.Index]; } else { instanceAssociatedType = typeArguments[0]; } if (templateMember.Type == SymbolType.Indexer) { IndexerSymbol templateIndexer = (IndexerSymbol)templateMember; IndexerSymbol instanceIndexer = new IndexerSymbol(parentType, instanceAssociatedType); if (templateIndexer.IsIntrinsic) { instanceIndexer.SetIntrinsic(); } instanceIndexer.SetVisibility(templateIndexer.Visibility); return instanceIndexer; } else if (templateMember.Type == SymbolType.Property) { PropertySymbol templateProperty = (PropertySymbol)templateMember; PropertySymbol instanceProperty = new PropertySymbol(templateProperty.Name, parentType, instanceAssociatedType); if (templateProperty.IsTransformed) { instanceProperty.SetTransformedName(templateProperty.GeneratedName); } instanceProperty.SetNameCasing(templateProperty.IsCasePreserved); instanceProperty.SetVisibility(templateProperty.Visibility); return instanceProperty; } else if (templateMember.Type == SymbolType.Field) { FieldSymbol templateField = (FieldSymbol)templateMember; FieldSymbol instanceField = new FieldSymbol(templateField.Name, parentType, instanceAssociatedType); if (templateField.IsTransformed) { instanceField.SetTransformedName(templateField.GeneratedName); } instanceField.SetNameCasing(templateField.IsCasePreserved); instanceField.SetVisibility(templateField.Visibility); return instanceField; } else if (templateMember.Type == SymbolType.Method) { MethodSymbol templateMethod = (MethodSymbol)templateMember; MethodSymbol instanceMethod = new MethodSymbol(templateMethod.Name, parentType, instanceAssociatedType); if (templateMethod.IsTransformed) { instanceMethod.SetTransformedName(templateMethod.GeneratedName); } if (templateMethod.InterfaceMember != null) { instanceMethod.SetInterfaceMember(templateMethod.InterfaceMember); } instanceMethod.SetNameCasing(templateMethod.IsCasePreserved); instanceMethod.SetVisibility(templateMethod.Visibility); return instanceMethod; } Debug.Fail("Unexpected generic member '" + templateMember.Name + " on type '" + ((TypeSymbol)templateMember.Parent).FullName + "'."); return null; }
private TypeSymbol CreateArrayTypeCore(TypeSymbol itemTypeSymbol) { TypeSymbol arrayTypeSymbol = (TypeSymbol)((ISymbolTable)_systemNamespace).FindSymbol("Array", null, SymbolFilter.Types); Debug.Assert(arrayTypeSymbol != null); TypeSymbol specificArrayTypeSymbol = new ClassSymbol("Array", _systemNamespace); foreach (MemberSymbol memberSymbol in arrayTypeSymbol.Members) { specificArrayTypeSymbol.AddMember(memberSymbol); } IndexerSymbol indexerSymbol = new IndexerSymbol(specificArrayTypeSymbol, itemTypeSymbol, MemberVisibility.Public); indexerSymbol.SetIntrinsic(); specificArrayTypeSymbol.AddMember(indexerSymbol); specificArrayTypeSymbol.SetIgnoreNamespace(); specificArrayTypeSymbol.SetArray(); return specificArrayTypeSymbol; }
private void ImportPseudoMembers(PseudoClassMembers memberSet, ClassSymbol classSymbol) { // Import pseudo members that go on the class but aren't defined in mscorlib.dll // These are meant to be used by internal compiler-generated transformations etc. // and aren't meant to be referenced directly in C# code. if (memberSet == PseudoClassMembers.Script) { TypeSymbol boolType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Boolean", null, SymbolFilter.Types); Debug.Assert(boolType != null); TypeSymbol intType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Int32", null, SymbolFilter.Types); Debug.Assert(intType != null); TypeSymbol floatType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Single", null, SymbolFilter.Types); Debug.Assert(floatType != null); TypeSymbol stringType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("String", null, SymbolFilter.Types); Debug.Assert(stringType != null); // Define the Escape, Unescape, encodeURI, decodeURI, encodeURIComponent, decodeURIComponent methods MethodSymbol escapeMethod = new MethodSymbol("Escape", classSymbol, stringType, MemberVisibility.Public | MemberVisibility.Static); classSymbol.AddMember(escapeMethod); MethodSymbol unescapeMethod = new MethodSymbol("Unescape", classSymbol, stringType, MemberVisibility.Public | MemberVisibility.Static); classSymbol.AddMember(unescapeMethod); MethodSymbol encodeURIMethod = new MethodSymbol("EncodeUri", classSymbol, stringType, MemberVisibility.Public | MemberVisibility.Static); encodeURIMethod.SetTransformedName("encodeURI"); classSymbol.AddMember(encodeURIMethod); MethodSymbol decodeURIMethod = new MethodSymbol("DecodeUri", classSymbol, stringType, MemberVisibility.Public | MemberVisibility.Static); decodeURIMethod.SetTransformedName("decodeURI"); classSymbol.AddMember(decodeURIMethod); MethodSymbol encodeURIComponentMethod = new MethodSymbol("EncodeUriComponent", classSymbol, stringType, MemberVisibility.Public | MemberVisibility.Static); encodeURIComponentMethod.SetTransformedName("encodeURIComponent"); classSymbol.AddMember(encodeURIComponentMethod); MethodSymbol decodeURIComponentMethod = new MethodSymbol("DecodeUriComponent", classSymbol, stringType, MemberVisibility.Public | MemberVisibility.Static); decodeURIComponentMethod.SetTransformedName("decodeURIComponent"); classSymbol.AddMember(decodeURIComponentMethod); return; } if (memberSet == PseudoClassMembers.Arguments) { TypeSymbol objectType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Object", null, SymbolFilter.Types); Debug.Assert(objectType != null); IndexerSymbol indexer = new IndexerSymbol(classSymbol, objectType, MemberVisibility.Public | MemberVisibility.Static); indexer.SetIntrinsic(); classSymbol.AddMember(indexer); return; } if (memberSet == PseudoClassMembers.Type) { // Define the Type.GetInstanceType static method which provides the functionality of // Object.GetType instance method. We don't extend Object.prototype in script to add // GetType, since we want to keep Object's protoype clean of any extensions. // // We create this symbol here, so that later the ExpressionBuilder can transform // calls to Object.GetType to this. TypeSymbol objectType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Object", null, SymbolFilter.Types); Debug.Assert(objectType != null); TypeSymbol typeType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Type", null, SymbolFilter.Types); Debug.Assert(objectType != null); MethodSymbol getTypeMethod = new MethodSymbol("GetInstanceType", classSymbol, typeType, MemberVisibility.Public | MemberVisibility.Static); getTypeMethod.AddParameter(new ParameterSymbol("instance", getTypeMethod, objectType, ParameterMode.In)); classSymbol.AddMember(getTypeMethod); return; } if (memberSet == PseudoClassMembers.Dictionary) { TypeSymbol intType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Int32", null, SymbolFilter.Types); Debug.Assert(intType != null); TypeSymbol boolType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Boolean", null, SymbolFilter.Types); Debug.Assert(boolType != null); TypeSymbol voidType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("Void", null, SymbolFilter.Types); Debug.Assert(boolType != null); TypeSymbol stringType = (TypeSymbol)((ISymbolTable)_symbols.SystemNamespace).FindSymbol("String", null, SymbolFilter.Types); Debug.Assert(boolType != null); // Define Dictionary.Keys MethodSymbol getKeysMethod = new MethodSymbol("GetKeys", classSymbol, _symbols.CreateArrayTypeSymbol(stringType), MemberVisibility.Public | MemberVisibility.Static); getKeysMethod.SetTransformedName("keys"); classSymbol.AddMember(getKeysMethod); // Define Dictionary.GetCount MethodSymbol countMethod = new MethodSymbol("GetKeyCount", classSymbol, intType, MemberVisibility.Public | MemberVisibility.Static); classSymbol.AddMember(countMethod); // Define Dictionary.ClearKeys MethodSymbol clearMethod = new MethodSymbol("ClearKeys", classSymbol, voidType, MemberVisibility.Public | MemberVisibility.Static); classSymbol.AddMember(clearMethod); // Define Dictionary.DeleteKey MethodSymbol deleteMethod = new MethodSymbol("DeleteKey", classSymbol, voidType, MemberVisibility.Public | MemberVisibility.Static); classSymbol.AddMember(deleteMethod); // Define Dictionary.KeyExists MethodSymbol existsMethod = new MethodSymbol("KeyExists", classSymbol, boolType, MemberVisibility.Public | MemberVisibility.Static); classSymbol.AddMember(existsMethod); return; } if (memberSet == PseudoClassMembers.String) { // In script, String.replace replaces only the first occurrence of a string // whereas in C# all occurrences are replaced. // Replace becomes replaceAll (a method we add) in generated script // ReplaceFirst becomes replace in generated script. // ReplaceRegex also becomes replace in generated script. (We added ReplaceRegex so // it could be mapped to the native replace method, rather than out replaceAll // extension) MethodSymbol replaceFirstMethod = (MethodSymbol)classSymbol.GetMember("ReplaceFirst"); Debug.Assert(replaceFirstMethod != null); replaceFirstMethod.SetTransformedName("replace"); MethodSymbol replaceMethod = (MethodSymbol)classSymbol.GetMember("Replace"); Debug.Assert(replaceMethod != null); replaceMethod.SetTransformedName("replaceAll"); MethodSymbol replaceRegexMethod = (MethodSymbol)classSymbol.GetMember("ReplaceRegex"); Debug.Assert(replaceRegexMethod != null); replaceRegexMethod.SetTransformedName("replace"); } }
private void ImportProperties(TypeSymbol typeSymbol) { TypeDefinition type = (TypeDefinition)typeSymbol.MetadataReference; foreach (PropertyDefinition property in type.Properties) { if (property.IsSpecialName) { continue; } if (property.GetMethod == null) { continue; } if (property.GetMethod.IsPrivate || property.GetMethod.IsAssembly || property.GetMethod.IsFamilyAndAssembly) { continue; } string propertyName = property.Name; bool preserveCase = MetadataHelpers.ShouldPreserveCase(property); bool intrinsicProperty = MetadataHelpers.ShouldTreatAsIntrinsicProperty(property); TypeSymbol propertyType = ResolveType(property.PropertyType); if (propertyType == null) { continue; } PropertySymbol propertySymbol = null; if (property.Parameters.Count != 0) { IndexerSymbol indexerSymbol = new IndexerSymbol(typeSymbol, propertyType); ImportMemberDetails(indexerSymbol, property.GetMethod, property); if (intrinsicProperty) { indexerSymbol.SetIntrinsic(); } propertySymbol = indexerSymbol; propertySymbol.SetNameCasing(preserveCase); } else { if (intrinsicProperty) { // Properties marked with this attribute are to be thought of as // fields. If they are read-only, the C# compiler will enforce that, // so we don't have to worry about making them read-write via a field // instead of a property FieldSymbol fieldSymbol = new FieldSymbol(propertyName, typeSymbol, propertyType); ImportMemberDetails(fieldSymbol, property.GetMethod, property); string alias = MetadataHelpers.GetScriptAlias(property); if (String.IsNullOrEmpty(alias) == false) { fieldSymbol.SetAlias(alias); } typeSymbol.AddMember(fieldSymbol); } else { propertySymbol = new PropertySymbol(propertyName, typeSymbol, propertyType); ImportMemberDetails(propertySymbol, property.GetMethod, property); } } if (propertySymbol != null) { SymbolImplementationFlags implFlags = SymbolImplementationFlags.Regular; if (property.SetMethod == null) { implFlags |= SymbolImplementationFlags.ReadOnly; } if (property.GetMethod.IsAbstract) { implFlags |= SymbolImplementationFlags.Abstract; } propertySymbol.SetImplementationState(implFlags); typeSymbol.AddMember(propertySymbol); } } }
private MemberSymbol CreateGenericMember(MemberSymbol templateMember, IList <TypeSymbol> typeArguments) { TypeSymbol parentType = (TypeSymbol)templateMember.Parent; TypeSymbol instanceAssociatedType; if (templateMember.AssociatedType.Type == SymbolType.GenericParameter) { GenericParameterSymbol genericParameter = (GenericParameterSymbol)templateMember.AssociatedType; instanceAssociatedType = typeArguments[genericParameter.Index]; } else { instanceAssociatedType = typeArguments[0]; } if (templateMember.Type == SymbolType.Indexer) { IndexerSymbol templateIndexer = (IndexerSymbol)templateMember; IndexerSymbol instanceIndexer = new IndexerSymbol(parentType, instanceAssociatedType); if (templateIndexer.IsIntrinsic) { instanceIndexer.SetIntrinsic(); } instanceIndexer.SetVisibility(templateIndexer.Visibility); return(instanceIndexer); } else if (templateMember.Type == SymbolType.Property) { PropertySymbol templateProperty = (PropertySymbol)templateMember; PropertySymbol instanceProperty = new PropertySymbol(templateProperty.Name, parentType, instanceAssociatedType); if (templateProperty.IsTransformed) { instanceProperty.SetTransformedName(templateProperty.GeneratedName); } instanceProperty.SetNameCasing(templateProperty.IsCasePreserved); instanceProperty.SetVisibility(templateProperty.Visibility); return(instanceProperty); } else if (templateMember.Type == SymbolType.Field) { FieldSymbol templateField = (FieldSymbol)templateMember; FieldSymbol instanceField = new FieldSymbol(templateField.Name, parentType, instanceAssociatedType); if (templateField.IsTransformed) { instanceField.SetTransformedName(templateField.GeneratedName); } instanceField.SetNameCasing(templateField.IsCasePreserved); instanceField.SetVisibility(templateField.Visibility); return(instanceField); } else if (templateMember.Type == SymbolType.Method) { MethodSymbol templateMethod = (MethodSymbol)templateMember; MethodSymbol instanceMethod = new MethodSymbol(templateMethod.Name, parentType, instanceAssociatedType); if (templateMethod.IsTransformed) { instanceMethod.SetTransformedName(templateMethod.GeneratedName); } if (templateMethod.InterfaceMember != null) { instanceMethod.SetInterfaceMember(templateMethod.InterfaceMember); } instanceMethod.SetNameCasing(templateMethod.IsCasePreserved); instanceMethod.SetVisibility(templateMethod.Visibility); return(instanceMethod); } Debug.Fail("Unexpected generic member '" + templateMember.Name + " on type '" + ((TypeSymbol)templateMember.Parent).FullName + "'."); return(null); }