/// <summary> /// Reflect global constants in <script> class. /// </summary> /// <param name="scriptType">The type representing single script.</param> /// <param name="constants">Dictionary for constants.</param> private void ReflectScriptTypeConstants(Type scriptType, DualDictionary <string, DConstantDesc> /*!*/ constants) { foreach (var field in scriptType.GetFields(BindingFlags.Public | BindingFlags.Static)) { // TODO: namespaces!! GlobalConstant constant = new GlobalConstant(this, QualifiedName.FromClrNotation(field.Name, true), field); constant.SetValue(Convert.ClrLiteralToPhpLiteral(field.GetValue(null))); constants.Add(field.Name, constant.ConstantDesc, false); } }
static GlobalConstant() { if (UnknownModule.RuntimeModule == null) { UnknownModule.RuntimeModule = new UnknownModule(); } Null = new GlobalConstant(QualifiedName.Null, Fields.PhpVariable_LiteralNull); Null.SetValue(null); False = new GlobalConstant(QualifiedName.False, Fields.PhpVariable_LiteralFalse); False.SetValue(false); True = new GlobalConstant(QualifiedName.True, Fields.PhpVariable_LiteralTrue); True.SetValue(true); PhpIntSize = new GlobalConstant(new QualifiedName(new Name("PHP_INT_SIZE")), typeof(PhpVariable).GetField("LiteralIntSize")); PhpIntSize.SetValue(PhpVariable.LiteralIntSize); PhpIntMax = new GlobalConstant(new QualifiedName(new Name("PHP_INT_MAX")), typeof(int).GetField("MaxValue")); PhpIntMax.SetValue(int.MaxValue); }
private void ReflectConstants(FieldInfo[] /*!!*/ realFields, DualDictionary <string, DConstantDesc> /*!*/ constants) { foreach (FieldInfo field in realFields) { // reflect only fields with [ImplementsConstant] attribute: ImplementsConstantAttribute impl_const = ImplementsConstantAttribute.Reflect(field); if (impl_const != null) { object value; try { // expect the constant have literal value, otherwise crash value = Convert.ClrLiteralToPhpLiteral(field.GetValue(null)); } catch (Exception) { throw new InvalidCastException(); } GlobalConstant constant = new GlobalConstant(this, new QualifiedName(new Name(impl_const.Name)), field); constant.SetValue(value); constants[impl_const.Name, impl_const.CaseInsensitive] = constant.ConstantDesc; } //// accepts literals of PHP/CLR primitive types only: //if (field.IsLiteral && (PhpVariable.IsLiteralPrimitiveType(field.FieldType) || field.FieldType.IsEnum)) //{ // if (impl_const != null) // { // // ... // } //} } }
private void ReflectConstants(FieldInfo[]/*!!*/ realFields, DualDictionary<string, DConstantDesc>/*!*/ constants) { foreach (FieldInfo field in realFields) { // reflect only fields with [ImplementsConstant] attribute: ImplementsConstantAttribute impl_const = ImplementsConstantAttribute.Reflect(field); if (impl_const != null) { object value; try { // expect the constant have literal value, otherwise crash value = Convert.ClrLiteralToPhpLiteral(field.GetValue(null)); } catch(Exception) { throw new InvalidCastException(); } GlobalConstant constant = new GlobalConstant(this ,new QualifiedName(new Name(impl_const.Name)), field); constant.SetValue(value); constants[impl_const.Name, impl_const.CaseInsensitive] = constant.ConstantDesc; } //// accepts literals of PHP/CLR primitive types only: //if (field.IsLiteral && (PhpVariable.IsLiteralPrimitiveType(field.FieldType) || field.FieldType.IsEnum)) //{ // if (impl_const != null) // { // // ... // } //} } }
private void Bake() { if (types != null) { bakedTypes = new KeyValuePair <string, PhpTypeDesc> [types.Count]; int i = 0; foreach (Declaration declaration in types.Values) { PhpType type = (PhpType)declaration.Declaree; // store full name before calling Bake() as it nulls the PhpType: string full_name = type.FullName; PhpTypeDesc baked = type.Bake(); // baked is null if the type is indefinite // (its base class may be evaluated when the module's main method is executed): if (baked != null && !declaration.IsConditional) { bakedTypes[i++] = new KeyValuePair <string, PhpTypeDesc>(full_name, baked); } } // trim: Array.Resize(ref bakedTypes, i); types = null; } if (functions != null) { bakedFunctions = new KeyValuePair <string, PhpRoutineDesc> [functions.Count]; int i = 0; foreach (Declaration declaration in functions.Values) { PhpFunction function = (PhpFunction)declaration.Declaree; string full_name = function.FullName; PhpRoutineDesc baked = function.Bake(); if (!declaration.IsConditional) { bakedFunctions[i++] = new KeyValuePair <string, PhpRoutineDesc>(full_name, baked); } } // trim: Array.Resize(ref bakedFunctions, i); functions = null; } if (constants != null) { bakedConstants = new KeyValuePair <string, DConstantDesc> [constants.Count]; int i = 0; foreach (Declaration declaration in constants.Values) { GlobalConstant constant = (GlobalConstant)declaration.Declaree; string full_name = constant.FullName; DConstantDesc baked = constant.Bake(); if (!declaration.IsConditional) { bakedConstants[i++] = new KeyValuePair <string, DConstantDesc>(full_name, baked); } } // trim: Array.Resize(ref bakedConstants, i); constants = null; } }
public GlobalConstantDecl(SourceUnit/*!*/ sourceUnit, Position position, bool isConditional, Scope scope, string/*!*/ name, NamespaceDecl ns, Expression/*!*/ initializer) : base(position, name, initializer) { this.ns = ns; QualifiedName qn = (ns != null) ? new QualifiedName(new Name(name), ns.QualifiedName) : new QualifiedName(new Name(name)); constant = new GlobalConstant(qn, PhpMemberAttributes.Public, sourceUnit, isConditional, scope, position); constant.SetNode(this); }
/// <summary> /// Reflect global constants in <script> class. /// </summary> /// <param name="scriptType">The type representing single script.</param> /// <param name="constants">Dictionary for constants.</param> private void ReflectScriptTypeConstants(Type scriptType, DualDictionary<string, DConstantDesc>/*!*/ constants) { foreach (var field in scriptType.GetFields(BindingFlags.Public | BindingFlags.Static)) { // TODO: namespaces!! GlobalConstant constant = new GlobalConstant(this, ClrNotationUtils.FromClrNotation(field.Name, true), field); constant.SetValue(Convert.ClrLiteralToPhpLiteral(field.GetValue(null))); constants.Add(field.Name, constant.ConstantDesc, false); } }
static GlobalConstant() { if (UnknownModule.RuntimeModule == null) UnknownModule.RuntimeModule = new UnknownModule(); Null = new GlobalConstant(QualifiedName.Null, Fields.PhpVariable_LiteralNull); Null.SetValue(null); False = new GlobalConstant(QualifiedName.False, Fields.PhpVariable_LiteralFalse); False.SetValue(false); True = new GlobalConstant(QualifiedName.True, Fields.PhpVariable_LiteralTrue); True.SetValue(true); PhpIntSize = new GlobalConstant(new QualifiedName(new Name("PHP_INT_SIZE")), typeof(PhpVariable).GetField("LiteralIntSize")); PhpIntSize.SetValue(PhpVariable.LiteralIntSize); PhpIntMax = new GlobalConstant(new QualifiedName(new Name("PHP_INT_MAX")), typeof(int).GetField("MaxValue")); PhpIntMax.SetValue(int.MaxValue); }