Example #1
0
        public PhpClassDefinition FindOrCreateClass(PhpQualifiedName phpClassName, PhpQualifiedName baseClass)
        {
            var c = Classes.FirstOrDefault(i => phpClassName == i.Name);

            if (c != null)
            {
                return(c);
            }
            c = new PhpClassDefinition(phpClassName, baseClass);
            Classes.Add(c);
            return(c);
        }
Example #2
0
 private void TranslateProperty(PhpClassDefinition phpClassDefinition, CsharpPropertyDeclaration propertyDeclaration)
 {
     var pi = _state.Principles.CurrentType.GetProperty(propertyDeclaration.PropertyName);
     var pti = PropertyTranslationInfo.FromPropertyInfo(pi);
     if (pti.GetSetByMethod)
     {
         CsharpPropertyDeclarationAccessor accessor;
         if (!string.IsNullOrEmpty(pti.GetMethodName))
         {
             accessor = propertyDeclaration.Accessors.FirstOrDefault(u => u.Name == "get");
             if (accessor != null)
                 Tranlate_MethodOrProperty(phpClassDefinition, pi.GetGetMethod(), accessor.Statement, pti.GetMethodName);
         }
         if (string.IsNullOrEmpty(pti.SetMethodName)) return;
         accessor = propertyDeclaration.Accessors.FirstOrDefault(u => u.Name == "set");
         if (accessor != null)
             Tranlate_MethodOrProperty(phpClassDefinition, pi.GetSetMethod(), accessor.Statement, pti.SetMethodName);
     }
     else
         phpClassDefinition.Fields.Add(new PhpClassFieldDefinition
         {
             Name = pti.FieldScriptName,
             IsStatic = pti.IsStatic
         });
 }
Example #3
0
 private void TranslateMethod(PhpClassDefinition phpClass, MethodDeclaration md)
 {
     Tranlate_MethodOrProperty(phpClass, md.Info, md.Body, null);
 }
Example #4
0
        private void TranslateField(PhpCodeModule module, PhpClassDefinition phpClass, FieldDeclaration field)
        {
            PhpValueTranslator phpValueTranslator = null;
            foreach (var item in field.Items)
            {
                if (item.OptionalFieldInfo == null) continue;
                var fti = _info.GetOrMakeTranslationInfo(item.OptionalFieldInfo);
                switch (fti.Destination)
                {
                    case FieldTranslationDestionations.DefinedConst:
                        if (item.Value == null)
                            throw new NotSupportedException();
                        if (phpValueTranslator == null)
                            phpValueTranslator = new PhpValueTranslator(_state);
                        var definedValue = phpValueTranslator.TransValue(item.Value);
                        {
                            if (fti.IncludeModule != module.Name)
                            {
                                module = GetOrMakeModuleByName(fti.IncludeModule);
                            }
                        }
                        module.DefinedConsts.Add(new KeyValuePair<string, IPhpValue>(fti.ScriptName, definedValue));
                        break;
                    case FieldTranslationDestionations.GlobalVariable:
                        if (item.Value != null)
                        {

                            IPhpValue value;
                            // muszę na chwilę wyłączyć current type, bo to jes poza klasą generowane
                            {
                                var saveCurrentType = _state.Principles.CurrentType;
                                _state.Principles.CurrentType = null;
                                try
                                {
                                    if (phpValueTranslator == null)
                                        phpValueTranslator = new PhpValueTranslator(_state);
                                    value = phpValueTranslator.TransValue(item.Value);
                                }
                                finally
                                {
                                    _state.Principles.CurrentType = saveCurrentType;
                                }
                            }

                            #region Tworzenie kodu
                            var assign = new PhpAssignExpression(PhpVariableExpression.MakeGlobal(fti.ScriptName), value);
                            module.TopCode.Statements.Add(new PhpExpressionStatement(assign));
                            #endregion
                        }
                        break;
                    case FieldTranslationDestionations.JustValue:
                        continue; // don't define
                    case FieldTranslationDestionations.NormalField:
                    case FieldTranslationDestionations.ClassConst:
                        {
                            var def = new PhpClassFieldDefinition();
                            var cti = _state.Principles.GetTi(_state.Principles.CurrentType, true);
                            if (cti.IsArray)
                                continue;
                            if (field.Modifiers.Has("const") ^ fti.Destination == FieldTranslationDestionations.ClassConst)
                                throw new Exception("beige lion");

                            def.IsConst = fti.Destination == FieldTranslationDestionations.ClassConst;// field.Modifiers.Has("const");
                            def.Name = fti.ScriptName;

                            def.IsStatic = def.IsConst || field.Modifiers.Has("static");
                            if (field.Modifiers.Has("public"))
                                def.Visibility = Visibility.Public;
                            else if (field.Modifiers.Has("protected"))
                                def.Visibility = Visibility.Protected;
                            else
                                def.Visibility = Visibility.Private;

                            if (item.Value != null)
                            {
                                if (phpValueTranslator == null)
                                    phpValueTranslator = new PhpValueTranslator(_state);
                                def.ConstValue = phpValueTranslator.TransValue(item.Value);
                            }
                            phpClass.Fields.Add(def);
                            break;
                        }
                    default:
                        throw new NotSupportedException();
                }
            }
        }
Example #5
0
        private void TranslateConstructor(PhpClassDefinition phpClass, ConstructorDeclaration md)
        {
            //   state.Principles.CurrentMethod = md.Info;
            try
            {
                // MethodTranslationInfo mti = MethodTranslationInfo.FromMethodInfo(md.Info);
                // state.Principles.CurrentMethod = 
                var phpMethod = new PhpClassMethodDefinition("__construct");
                phpClass.Methods.Add(phpMethod);

                if (md.Info.IsPublic)
                    phpMethod.Visibility = Visibility.Public;
                else if (md.Info.IsPrivate)
                    phpMethod.Visibility = Visibility.Private;
                else
                    phpMethod.Visibility = Visibility.Protected;

                phpMethod.IsStatic = md.Info.IsStatic;
                {
                    var declaredParameters = md.Info.GetParameters();
                    foreach (var parameter in declaredParameters)
                    {
                        var phpParameter = new PhpMethodArgument();
                        phpParameter.Name = parameter.Name;
                        phpMethod.Arguments.Add(phpParameter);
                        if (parameter.HasDefaultValue)
                        {
                            phpParameter.DefaultValue = new PhpConstValue(parameter.DefaultValue);
                        }
                    }
                }

                if (md.Body != null)
                    phpMethod.Statements.AddRange(TranslateStatement(md.Body));
            }
            finally
            {
                _state.Principles.CurrentMethod = null;
            }
        }
Example #6
0
        private void Tranlate_MethodOrProperty(PhpClassDefinition phpClass, MethodInfo info, IStatement body, string overrideName)
        {
            _state.Principles.CurrentMethod = info;
            try
            {
                var mti = _state.Principles.GetOrMakeTranslationInfo(info);
                var phpMethod = new PhpClassMethodDefinition(string.IsNullOrEmpty(overrideName) ? mti.ScriptName : overrideName);
                phpClass.Methods.Add(phpMethod);

                if (info.IsPublic)
                    phpMethod.Visibility = Visibility.Public;
                else if (info.IsPrivate)
                    phpMethod.Visibility = Visibility.Private;
                else
                    phpMethod.Visibility = Visibility.Protected;

                phpMethod.IsStatic = info.IsStatic;
                {
                    var declaredParameters = info.GetParameters();
                    foreach (var parameter in declaredParameters)
                    {
                        var phpParameter = new PhpMethodArgument();
                        phpParameter.Name = parameter.Name;
                        phpMethod.Arguments.Add(phpParameter);
                        if (parameter.HasDefaultValue)
                        {
                            phpParameter.DefaultValue = new PhpConstValue(parameter.DefaultValue);
                        }
                    }
                }

                if (body != null)
                    phpMethod.Statements.AddRange(TranslateStatement(body));
            }
            finally
            {
                _state.Principles.CurrentMethod = null;
            }
        }
Example #7
0
 public PhpClassDefinition FindOrCreateClass(PhpQualifiedName phpClassName, PhpQualifiedName baseClass)
 {
     var c = _classes.FirstOrDefault(i => phpClassName == i.Name);
     if (c != null) return c;
     c = new PhpClassDefinition(phpClassName, baseClass);
     _classes.Add(c);
     return c;
 }