// Delegates are really just blessed Types. void CreateProxyType(ISemanticResolver s) { Debug.Assert(m_nodeProxy == null, "only create proxy once"); // The delegate R F(A) (where R is a return type, and A is a parameter list) // Can be converted into the type: // sealed class F : System.MulticastDelegate { // F(object, native int) { } // BeginInvoke() { } // EndInvoke() { } // R Invoke(A) { } // } BlockStatement stmtEmpty = new BlockStatement(null, new Statement[0]); Modifiers modsPublic = new Modifiers(); modsPublic.SetPublic(); Modifiers modsVirtual = modsPublic; modsVirtual.SetVirtual(); //System.Type tNativeInt = typeof(int); System.Type tNativeInt = Type.GetType("System.IntPtr"); TypeEntry t_IAsyncResult = s.ResolveCLRTypeToBlueType(typeof(System.IAsyncResult)); // Create the parameters for the BeginInvoke() ParamVarDecl [] paramBeginInvoke = new ParamVarDecl[m_arParams.Length + 2]; m_arParams.CopyTo(paramBeginInvoke, 0); paramBeginInvoke[m_arParams.Length]= new ParamVarDecl( new Identifier("cb"), new ResolvedTypeSig(typeof(System.AsyncCallback), s), EArgFlow.cIn ); paramBeginInvoke[m_arParams.Length + 1] = new ParamVarDecl( new Identifier("state"), new ResolvedTypeSig(typeof(System.Object), s), EArgFlow.cIn ); m_nodeProxy = new ClassDecl( m_idName, new TypeSig[] { new ResolvedTypeSig(typeof(System.MulticastDelegate), s) }, new MethodDecl[] { // Ctor new MethodDecl( m_idName, null, new ParamVarDecl[] { new ParamVarDecl(new Identifier("instance"), new ResolvedTypeSig(typeof(object), s), EArgFlow.cIn), new ParamVarDecl(new Identifier("func"), new ResolvedTypeSig(tNativeInt, s), EArgFlow.cIn) }, stmtEmpty, modsPublic ), // Invoke, new MethodDecl( new Identifier("Invoke"), this.m_tRetType, this.m_arParams, stmtEmpty, modsVirtual), // Begin Invoke new MethodDecl( new Identifier("BeginInvoke"), new ResolvedTypeSig(t_IAsyncResult), paramBeginInvoke, stmtEmpty, modsVirtual), // End Invoke new MethodDecl( new Identifier("EndInvoke"), this.m_tRetType, new ParamVarDecl[] { new ParamVarDecl(new Identifier("result"), new ResolvedTypeSig(t_IAsyncResult), EArgFlow.cIn) }, stmtEmpty, modsVirtual) }, new PropertyDecl[0], new FieldDecl[0], new EventDecl[0], new TypeDeclBase[0], m_mods, true); // isClass }
void FixCtors( ISemanticResolver s, ICLRtypeProvider provider ) { // Add default ctor bool fFoundCtor = m_symbol.HasMethodHeader(Name); // Note that structs don't have a default ctor, but can have other ctors // But add a default ctor for classes if we don't have one. if (!fFoundCtor && IsClass) { CtorChainStatement stmtChain = new CtorChainStatement(); Modifiers mods = new Modifiers(); mods.SetPublic(); MethodDecl mdecl = new MethodDecl( new Identifier(Name, this.Location), null, null, new BlockStatement(new LocalVarDecl[0], new Statement[] { stmtChain }), //new AST.Modifiers(AST.Modifiers.EFlags.Public) mods ); stmtChain.FinishInit(mdecl); mdecl.ResolveMember(m_symbol,s, null); mdecl.Symbol.SetInfo(provider); // Add to the end of the m_alMethods array so that we get codegen'ed! AddMethodToList(mdecl); Debug.Assert(m_symbol.HasMethodHeader(Name)); } // @todo - perhaps we could just make the static initializer a static-ctor.. // If we don't have a static ctor, but we do have static data, then add // a static ctor if (m_nodeStaticInit != null) { bool fFoundStaticCtor = false; foreach(MethodDecl m in m_alMethods) { if (m.Mods.IsStatic && m.IsCtor) { fFoundStaticCtor = true; break; } } if (!fFoundStaticCtor) { Modifiers mods = new Modifiers(); mods.SetStatic(); mods.SetPublic(); MethodDecl mdecl2 = new MethodDecl( new Identifier(Name, this.Location), null, null, new BlockStatement(null, new Statement[]{}), //new Modifiers(AST.Modifiers.EFlags.Static | Modifiers.EFlags.Public) mods ); mdecl2.ResolveMember(m_symbol, s, null); mdecl2.Symbol.SetInfo(provider); AddMethodToList(mdecl2); } } // end check static ctor } // fix ctors
//----------------------------------------------------------------------------- // Parse enum declaration // --> 'enum' id:name '{' enum_decl_list '}' //----------------------------------------------------------------------------- protected EnumDecl ParseEnum(Modifiers modsEnums) { ReadExpectedToken(Token.Type.cEnum); Identifier idName = ReadExpectedIdentifier(); FileRange f2 = this.BeginRange(); ReadExpectedToken(Token.Type.cLCurly); ArrayList a = new ArrayList(); // All enum fields are Static, Public, Literal // and have fieldtype set to type of the enum //Modifiers mods = new Modifiers(Modifiers.EFlags.Public | Modifiers.EFlags.Static); Modifiers mods = new Modifiers(); mods.SetPublic(); mods.SetStatic(); TypeSig tSig = new SimpleTypeSig(new SimpleObjExp(idName)); Identifier idPrev = null; Token t = m_lexer.PeekNextToken(); while(t.TokenType != Token.Type.cRCurly) { // Parse fields Identifier id = ReadExpectedIdentifier(); Exp expInit = null; t = m_lexer.PeekNextToken(); if (t.TokenType == Token.Type.cAssign) { ConsumeNextToken(); expInit = ParseExp(); } else { #if false // If no explicit assignment, then we must create one // first field -> '=0' if (idPrev == null) { expInit = new IntExp(0, id.Location); } // all other fields -> '= <prevfield> + '1' ' else { expInit = new BinaryExp( new SimpleObjExp(idPrev), new IntExp(1, id.Location), BinaryExp.BinaryOp.cAdd); } #endif } //EnumField e = new EnumField(id); FieldDecl e = new FieldDecl(id, tSig, mods, expInit); a.Add(e); // If no comma, then this had better be our last one t = m_lexer.PeekNextToken(); if (t.TokenType != Token.Type.cComma) { break; } ReadExpectedToken(Token.Type.cComma); idPrev = id; t = m_lexer.PeekNextToken(); } // while parsing fields ReadExpectedToken(Token.Type.cRCurly); // Convert array list to EnumField[] FieldDecl [] f = new FieldDecl[a.Count]; for(int i = 0; i < f.Length; i++) f[i] = (FieldDecl) a[i]; EnumDecl node = new EnumDecl(idName, f, modsEnums); node.SetLocation(this.EndRange(f2)); return node; }
//----------------------------------------------------------------------------- // Parse an interface // // ** rules ** // InterfaceDecl-> 'interface' id:name '{' body '}' // InterfaceDecl-> 'interface' id:name ':' base_list '{' body '}' //----------------------------------------------------------------------------- protected ClassDecl ParseInterface(Modifiers modsInterface) { Token t; ReadExpectedToken(Token.Type.cInterface); Identifier idName = ReadExpectedIdentifier(); TypeSig[] arBase = null; //if (!modsInterface.IsPublic) //modsInterface.FlagSetter |= Modifiers.EFlags.Private; if (modsInterface.VisibilityNotSet) modsInterface.SetPrivate(); ArrayList alMethods = new ArrayList(); ArrayList alProperties = new ArrayList(); // Read list of base interfaces that we derive from t = m_lexer.PeekNextToken(); if (t.TokenType == Token.Type.cColon) { ConsumeNextToken(); // ':' arBase = ParseIdNameList(); } ReadExpectedToken(Token.Type.cLCurly); // Read members t = m_lexer.PeekNextToken(); while(t.TokenType != Token.Type.cRCurly) { // member: // method -> rettype id:name '(' param_list ')' ';' // property -> rettype id:name '{' set ';' get ';' '}' TypeSig rettype = ParseTypeSig(); Identifier idMember = ReadExpectedIdentifier(); t = m_lexer.PeekNextToken(); // All interface members have these attributes /* AST.Modifiers mods = new AST.Modifiers( AST.Modifiers.EFlags.Abstract | AST.Modifiers.EFlags.Virtual | AST.Modifiers.EFlags.Public ); */ Modifiers mods = new Modifiers(); mods.SetAbstract(); mods.SetVirtual(); mods.SetPublic(); // Method if (t.TokenType == Token.Type.cLParen) { MemberDecl m = this.PartialParseMethodDecl(mods, rettype, idMember, Genre.cInterface); alMethods.Add(m); } // Property else if (t.TokenType == Token.Type.cLCurly) { PropertyDecl p = PartialParsePropertyDecl(mods, rettype, idMember); alProperties.Add(p); } // Indexer else if (t.TokenType == Token.Type.cLSquare) { PropertyDecl p = PartialParseIndexerDecl(mods, rettype, idMember); alProperties.Add(p); } // Error else { //this.ThrowError_UnexpectedToken(t); ThrowError(E_UnexpectedToken(t)); } t = m_lexer.PeekNextToken(); } ReadExpectedToken(Token.Type.cRCurly); // '}' MethodDecl [] arMethods = this.MethodDeclFromArray(alMethods); PropertyDecl [] arProperties = this.PropertyDeclFromArray(alProperties); ClassDecl d = new ClassDecl(idName, arBase, arMethods, arProperties, modsInterface); return d; }
//----------------------------------------------------------------------------- // Parse Member attributes and return the bit flag // // ** rules ** // MemberAttr -> <any subset of {public, static, etc } > //----------------------------------------------------------------------------- protected Modifiers ParseModifiers() { AST.Modifiers mods = new Modifiers(); while(true) { Token t = m_lexer.PeekNextToken(); switch(t.TokenType) { case Token.Type.cAttrPublic: mods.SetPublic(); break; case Token.Type.cAttrProtected: mods.SetProtected(); break; case Token.Type.cAttrPrivate: mods.SetPrivate(); break; case Token.Type.cAttrStatic: mods.SetStatic(); break; case Token.Type.cAttrAbstract: mods.SetAbstract(); break; case Token.Type.cAttrVirtual: mods.SetVirtual(); break; case Token.Type.cAttrOverride: mods.SetOverride(); break; case Token.Type.cAttrInternal: mods.SetInternal(); break; case Token.Type.cAttrReadOnly: mods.SetReadOnly(); break; case Token.Type.cNew: mods.SetNew(); break; case Token.Type.cAttrSealed: mods.SetSealed(); break; // Return once we encounter something that's not a modifier default: { return mods; } } ConsumeNextToken(); } // We exit once we find a token that's not a modifier (or we find a duplicate modifier) }