Ejemplo n.º 1
0
        /// <summary>
        /// Converts C++ function parameter or struct member definition (type, name, const etc) to C# typename/name/etc.
        /// _i must be at its start. Finally it will be after the parsed part. Does not check whether it is followed by ',' etc.
        /// </summary>
        /// <param name="context">Member, Parameter or DelegateParameter.</param>
        /// <param name="t">Receives C# typename, name etc.</param>
        /// <param name="parentName">The function/struct name. Used to auto-create names for nameless parameters.</param>
        /// <param name="iParam">1-based index of parameter/member. Used to auto-create names for nameless parameters.</param>
        void _ParseParamOrMember(_TypeContext context, out _PARAMDATA t, string parentName, int iParam)
        {
            bool isMember = context == _TypeContext.Member;

            t = new _PARAMDATA();

            int iSAL = 0;

            if (_TokIsChar(_i, '^') && _TokIsChar(_i + 1, '\"'))
            {
                _i++;
                if (!isMember)
                {
                    iSAL       = _i;
                    _tok[iSAL] = new _Token(_tok[iSAL].s + 1, _tok[iSAL].len - 2);
                }
                _i++;
            }

            var d = new _FINDTYPEDATA();

            _FindTypename(isMember, ref d);
            if (d.outIsNestedTypeDefinition)
            {
                if (_TokIsChar(_i, ';'))
                {
                    t.isNestedTypeDefinitionWithoutVariables    = true;
                    t.isAnonymousTypeDefinitionWithoutVariables = d.outIsAnonymousTypeDefinition;
                    t.typeName = d.outSym.csTypename;
                    return;
                }
            }
            else
            {
                _i++;
            }

            //pointer
            int ptr = 0;

            while (_TokIsChar(_i, '*', '&'))
            {
                _i++; ptr++;
            }

            //suport inline function type definition
            bool isFunc = !d.outIsNestedTypeDefinition && _DetectIsFuncType(_i);

            if (isFunc)
            {
                var f = new _INLINEFUNCTYPEDATA(parentName, iParam);
                _DeclareTypedefFunc(d.outSym, ptr, d.outIsConst, d.outTypenameToken, f);
                t.typeName = f.typeName;
                t.name     = f.paramName;
            }
            else
            {
                //typename, param/member name
                t.typeName = _ConvertTypeName(d.outSym, ref ptr, d.outIsConst, d.outTypenameToken, context, out t.attributes, iSAL);

                if (_TokIsIdent(_i))
                {
                    t.name = _TokToString(_i++);
                }
                else if (!isMember && _TokIsChar(_i, ",)[="))
                {
                    t.name = "param" + iParam;
                }
                else
                {
                    _Err(_i, "no name");
                }
            }

            if (_TokIsChar(_i, '['))
            {
                _ConvertCArray(ref t.typeName, ref t.name, ref t.attributes, !isMember);
            }

            //escape names that are C# keywords
            if (_csKeywords.Contains(t.name))
            {
                //AOutput.Write(t.name);
                t.name = "@" + t.name;
            }
        }
Ejemplo n.º 2
0
        void _DefineUndef()
        {
            char *s = T(++_i);
            char  c = *s;            //was like `d$$$_REALNAME, now s is without `

            if (c == 'c')            //`cx "C# code converted by the preprocessor script", where x tells what it is
            {
                if (!_TokIsChar(++_i, '\x2'))
                {
                    _Err(_i, "unexpected 1");
                }
                _tok[_i] = new _Token(T(_i) + 1, _tok[_i].len - 2);
                if (s[1] == 'p')
                {
                    _sbVar.AppendLine(_TokToString(_i));
                }
                else
                {
                    _Err(_i - 1, "unexpected 2");
                }
                return;
            }

            s       += 5; int lenName = _tok[_i].len - 5;       //skip prefix 'd$$$_' that was added to avoid unexpanding names
            _tok[_i] = new _Token(s, lenName);
            int iName = _i;

            //is function-style?
            char *s2     = T(++_i);
            bool  isFunc = c == 'd' && *s2 == '(' && s2 == s + lenName;

            //find value
            int iValue = _i, iParamOrValue = _i;

            if (isFunc)
            {
                iValue = _SkipEnclosed(_i) + 1;                    //name(parameters)[ value]
            }
            //find next line
            int iNext = iValue;

            for (; ; iNext++)
            {
                char k = *T(iNext);
                if (k == '`' || k == '\x0')
                {
                    break;
                }
            }

            string name = new string(s, 0, lenName);

            if (c == 'u')              //#undef
            {
                if (!_defineConst.Remove(name) && !_defineOther.Remove(name))
                {
                    _defineW.Remove(name);
                }
                //AOutput.Write($"#undef {name}");
            }
            else if (iValue < iNext)         //preprocessor removes some #define values, it's ok
            {
                if (isFunc)                  //info: for func-style get parameters as part of value
                {
                    __DefineAddToOther(iName, name, _TokToString(iParamOrValue, iNext));
                }
                else if (*s2 == '\x2')                    //ANSI string constant, when tokenizing replaced the first '\"' to '\x2'
                {
                    *s2 = '\"';
                    __DefineAddToOther(iName, name, " " + _TokToString(iParamOrValue, iNext) + " //ANSI string");
                }
                else
                {
                    _ExpressionResult r = _Expression(iParamOrValue, iNext, name);
                    if (r.typeS == null)
                    {
                        bool isFuncWA = (iNext - iValue == 1) && __DefineWA(name, r.valueS);
                        if (!isFuncWA)
                        {
                            //don't add '#define NAME1 NAME2'
                            if (iNext - iValue == 1 && _TokIsIdent(iValue))
                            {
                                //OutList(name, r.valueS);
                            }
                            else
                            {
                                //OutList(name, iNext-iValue);
                                __DefineAddToOther(iName, name, " " + r.valueS);
                            }
                        }
                    }
                    else
                    {
                        __sbDef.Clear();
                        __sbDef.AppendFormat("internal {2} {3} {0} = {1};", name, r.valueS, r.notConst ? "readonly" : "const", r.typeS);
                        _defineConst[name] = __sbDef.ToString();
                    }
                }
            }

            _i = iNext - 1;
        }