compiler/libec; ide/debugger: Fixed sizeof((char *)"abc")
[sdk] / compiler / libec / src / pass15.ec
1 import "ecdefs"
2
3 #define uint _uint
4 #include <stdlib.h>  // For strtoll
5 #undef uint
6
7 // UNTIL IMPLEMENTED IN GRAMMAR
8 #define ACCESS_CLASSDATA(_class, baseClass) \
9    (_class ? ((void *)(((char *)_class.data) + baseClass.offsetClass)) : null)
10
11 #define YYLTYPE Location
12 #include "grammar.h"
13
14 extern OldList * ast;
15 extern int returnCode;
16 extern Expression parsedExpression;
17 extern bool yydebug;
18 public void SetYydebug(bool b) { yydebug = b; }
19 extern bool echoOn;
20
21 void resetScanner();
22
23 // TODO: Reset this to 0 on reinitialization
24 int propWatcherID;
25
26 int expression_yyparse();
27 static Statement curCompound;
28 External curExternal, afterExternal;
29 static Type curSwitchType;
30 static Class currentClass;
31 Class thisClass;
32 public void SetThisClass(Class c) { thisClass = c; } public Class GetThisClass() { return thisClass; }
33 static char * thisNameSpace;
34 /*static */Class containerClass;
35 bool thisClassParams = true;
36
37 uint internalValueCounter;
38
39 #ifdef _DEBUG
40 Time findSymbolTotalTime;
41 #endif
42
43 // WARNING: PrintExpression CONCATENATES to string. Please initialize.
44 /*static */public void PrintExpression(Expression exp, char * string)
45 {
46    //if(inCompiler)
47    {
48       TempFile f { };
49       int count;
50
51       if(exp)
52          OutputExpression(exp, f);
53       f.Seek(0, start);
54       count = strlen(string);
55       count += f.Read(string + count, 1, 1023);
56       string[count] = '\0';
57       delete f;
58    }
59 }
60
61 Type ProcessTemplateParameterType(TemplateParameter param)
62 {
63    if(param && param.type == TemplateParameterType::type && (param.dataType || param.dataTypeString))
64    {
65       // TOFIX: Will need to free this Type
66       if(!param.baseType)
67       {
68          if(param.dataTypeString)
69             param.baseType = ProcessTypeString(param.dataTypeString, false);
70          else
71             param.baseType = ProcessType(param.dataType.specifiers, param.dataType.decl);
72       }
73       return param.baseType;
74    }
75    return null;
76 }
77
78 bool NeedCast(Type type1, Type type2)
79 {
80    if(!type1 || !type2 || type1.keepCast || type2.keepCast) return true;
81
82    if(type1.kind == templateType && type2.kind == int64Type && type2.passAsTemplate == false)
83    {
84       return false;
85    }
86
87    if(type1.kind == type2.kind)
88    {
89       switch(type1.kind)
90       {
91          case _BoolType:
92          case charType:
93          case shortType:
94          case intType:
95          case int64Type:
96          case intPtrType:
97          case intSizeType:
98             if(type1.passAsTemplate && !type2.passAsTemplate)
99                return true;
100             return type1.isSigned != type2.isSigned;
101          case classType:
102             return type1._class != type2._class;
103          case pointerType:
104             return NeedCast(type1.type, type2.type);
105          default:
106             return true; //false; ????
107       }
108    }
109    return true;
110 }
111
112 static void ReplaceClassMembers(Expression exp, Class _class)
113 {
114    if(exp.type == identifierExp && exp.identifier)
115    {
116       Identifier id = exp.identifier;
117       Context ctx;
118       Symbol symbol = null;
119       if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
120       {
121          // First, check if the identifier is declared inside the function
122          for(ctx = curContext; ctx != topContext.parent && !symbol; ctx = ctx.parent)
123          {
124             symbol = (Symbol)ctx.symbols.FindString(id.string);
125             if(symbol) break;
126          }
127       }
128
129       // If it is not, check if it is a member of the _class
130       if(!symbol && ((!id._class || (id._class.name && !strcmp(id._class.name, "property"))) || (id.classSym && eClass_IsDerived(_class, id.classSym.registered))))
131       {
132          Property prop = eClass_FindProperty(_class, id.string, privateModule);
133          Method method = null;
134          DataMember member = null;
135          ClassProperty classProp = null;
136          if(!prop)
137          {
138             method = eClass_FindMethod(_class, id.string, privateModule);
139          }
140          if(!prop && !method)
141             member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
142          if(!prop && !method && !member)
143          {
144             classProp = eClass_FindClassProperty(_class, id.string);
145          }
146          if(prop || method || member || classProp)
147          {
148             // Replace by this.[member]
149             exp.type = memberExp;
150             exp.member.member = id;
151             exp.member.memberType = unresolvedMember;
152             exp.member.exp = QMkExpId("this");
153             //exp.member.exp.loc = exp.loc;
154             exp.addedThis = true;
155          }
156          else if(_class && _class.templateParams.first)
157          {
158             Class sClass;
159             for(sClass = _class; sClass; sClass = sClass.base)
160             {
161                if(sClass.templateParams.first)
162                {
163                   ClassTemplateParameter param;
164                   for(param = sClass.templateParams.first; param; param = param.next)
165                   {
166                      if(param.type == expression && !strcmp(param.name, id.string))
167                      {
168                         Expression argExp = GetTemplateArgExpByName(param.name, _class, TemplateParameterType::expression);
169
170                         if(argExp)
171                         {
172                            Declarator decl;
173                            OldList * specs = MkList();
174
175                            FreeIdentifier(exp.member.member);
176
177                            ProcessExpressionType(argExp);
178
179                            decl = SpecDeclFromString(param.dataTypeString, specs, null);
180
181                            exp.expType = ProcessType(specs, decl);
182
183                            // *[expType] *[argExp]
184                            exp.type = bracketsExp;
185                            exp.list = MkListOne(MkExpOp(null, '*',
186                               MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpOp(null, '&', argExp))));
187                         }
188                      }
189                   }
190                }
191             }
192          }
193       }
194    }
195 }
196
197 ////////////////////////////////////////////////////////////////////////
198 // PRINTING ////////////////////////////////////////////////////////////
199 ////////////////////////////////////////////////////////////////////////
200
201 public char * PrintInt(int64 result)
202 {
203    char temp[100];
204    if(result > MAXINT64)
205       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
206    else
207       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
208    return CopyString(temp);
209 }
210
211 public char * PrintUInt(uint64 result)
212 {
213    char temp[100];
214    if(result > MAXDWORD)
215       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
216    else if(result > MAXINT)
217       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
218    else
219       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
220    return CopyString(temp);
221 }
222
223 public char * PrintInt64(int64 result)
224 {
225    char temp[100];
226    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
227    return CopyString(temp);
228 }
229
230 public char * PrintUInt64(uint64 result)
231 {
232    char temp[100];
233    if(result > MAXINT64)
234       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
235    else
236       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
237    return CopyString(temp);
238 }
239
240 public char * PrintHexUInt(uint64 result)
241 {
242    char temp[100];
243    if(result > MAXDWORD)
244       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
245    else
246       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
247    return CopyString(temp);
248 }
249
250 public char * PrintHexUInt64(uint64 result)
251 {
252    char temp[100];
253    if(result > MAXDWORD)
254       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
255    else
256       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
257    return CopyString(temp);
258 }
259
260 public char * PrintShort(short result)
261 {
262    char temp[100];
263    sprintf(temp, "%d", (unsigned short)result);
264    return CopyString(temp);
265 }
266
267 public char * PrintUShort(unsigned short result)
268 {
269    char temp[100];
270    if(result > 32767)
271       sprintf(temp, "0x%X", (int)result);
272    else
273       sprintf(temp, "%d", (int)result);
274    return CopyString(temp);
275 }
276
277 public char * PrintChar(char result)
278 {
279    char temp[100];
280    if(result > 0 && isprint(result))
281       sprintf(temp, "'%c'", result);
282    else if(result < 0)
283       sprintf(temp, "%d", (int)result);
284    else
285       //sprintf(temp, "%#X", result);
286       sprintf(temp, "0x%X", (unsigned char)result);
287    return CopyString(temp);
288 }
289
290 public char * PrintUChar(unsigned char result)
291 {
292    char temp[100];
293    sprintf(temp, "0x%X", result);
294    return CopyString(temp);
295 }
296
297 public char * PrintFloat(float result)
298 {
299    char temp[350];
300    sprintf(temp, "%.16ff", result);
301    return CopyString(temp);
302 }
303
304 public char * PrintDouble(double result)
305 {
306    char temp[350];
307    sprintf(temp, "%.16f", result);
308    return CopyString(temp);
309 }
310
311 ////////////////////////////////////////////////////////////////////////
312 ////////////////////////////////////////////////////////////////////////
313
314 //public Operand GetOperand(Expression exp);
315
316 #define GETVALUE(name, t) \
317    public bool Get##name(Expression exp, t * value2) \
318    {                                                        \
319       Operand op2 = GetOperand(exp);                        \
320       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
321       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
322       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
323       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
324       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
325       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
326       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
327       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
328       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
329       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
330       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
331       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
332       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
333       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
334       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
335       else                                                                          \
336          return false;                                                              \
337       return true;                                                                  \
338    }
339
340 // To help the deubugger currently not preprocessing...
341 #define HELP(x) x
342
343 GETVALUE(Int, HELP(int));
344 GETVALUE(UInt, HELP(unsigned int));
345 GETVALUE(Int64, HELP(int64));
346 GETVALUE(UInt64, HELP(uint64));
347 GETVALUE(IntPtr, HELP(intptr));
348 GETVALUE(UIntPtr, HELP(uintptr));
349 GETVALUE(IntSize, HELP(intsize));
350 GETVALUE(UIntSize, HELP(uintsize));
351 GETVALUE(Short, HELP(short));
352 GETVALUE(UShort, HELP(unsigned short));
353 GETVALUE(Char, HELP(char));
354 GETVALUE(UChar, HELP(unsigned char));
355 GETVALUE(Float, HELP(float));
356 GETVALUE(Double, HELP(double));
357
358 void ComputeExpression(Expression exp);
359
360 void ComputeClassMembers(Class _class, bool isMember)
361 {
362    DataMember member = isMember ? (DataMember) _class : null;
363    Context context = isMember ? null : SetupTemplatesContext(_class);
364    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
365                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
366    {
367       int c;
368       int unionMemberOffset = 0;
369       int bitFields = 0;
370
371       /*
372       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
373          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
374       */
375
376       if(member)
377       {
378          member.memberOffset = 0;
379          if(targetBits < sizeof(void *) * 8)
380             member.structAlignment = 0;
381       }
382       else if(targetBits < sizeof(void *) * 8)
383          _class.structAlignment = 0;
384
385       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
386       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
387          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
388
389       if(!member && _class.destructionWatchOffset)
390          _class.memberOffset += sizeof(OldList);
391
392       // To avoid reentrancy...
393       //_class.structSize = -1;
394
395       {
396          DataMember dataMember;
397          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
398          {
399             if(!dataMember.isProperty)
400             {
401                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
402                {
403                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
404                   /*if(!dataMember.dataType)
405                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
406                      */
407                }
408             }
409          }
410       }
411
412       {
413          DataMember dataMember;
414          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
415          {
416             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
417             {
418                if(!isMember && _class.type == bitClass && dataMember.dataType)
419                {
420                   BitMember bitMember = (BitMember) dataMember;
421                   uint64 mask = 0;
422                   int d;
423
424                   ComputeTypeSize(dataMember.dataType);
425
426                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
427                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
428
429                   _class.memberOffset = bitMember.pos + bitMember.size;
430                   for(d = 0; d<bitMember.size; d++)
431                   {
432                      if(d)
433                         mask <<= 1;
434                      mask |= 1;
435                   }
436                   bitMember.mask = mask << bitMember.pos;
437                }
438                else if(dataMember.type == normalMember && dataMember.dataType)
439                {
440                   int size;
441                   int alignment = 0;
442
443                   // Prevent infinite recursion
444                   if(dataMember.dataType.kind != classType ||
445                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
446                      _class.type != structClass)))
447                      ComputeTypeSize(dataMember.dataType);
448
449                   if(dataMember.dataType.bitFieldCount)
450                   {
451                      bitFields += dataMember.dataType.bitFieldCount;
452                      size = 0;
453                   }
454                   else
455                   {
456                      if(bitFields)
457                      {
458                         int size = (bitFields + 7) / 8;
459
460                         if(isMember)
461                         {
462                            // TESTING THIS PADDING CODE
463                            if(alignment)
464                            {
465                               member.structAlignment = Max(member.structAlignment, alignment);
466
467                               if(member.memberOffset % alignment)
468                                  member.memberOffset += alignment - (member.memberOffset % alignment);
469                            }
470
471                            dataMember.offset = member.memberOffset;
472                            if(member.type == unionMember)
473                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
474                            else
475                            {
476                               member.memberOffset += size;
477                            }
478                         }
479                         else
480                         {
481                            // TESTING THIS PADDING CODE
482                            if(alignment)
483                            {
484                               _class.structAlignment = Max(_class.structAlignment, alignment);
485
486                               if(_class.memberOffset % alignment)
487                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
488                            }
489
490                            dataMember.offset = _class.memberOffset;
491                            _class.memberOffset += size;
492                         }
493                         bitFields = 0;
494                      }
495                      size = dataMember.dataType.size;
496                      alignment = dataMember.dataType.alignment;
497                   }
498
499                   if(isMember)
500                   {
501                      // TESTING THIS PADDING CODE
502                      if(alignment)
503                      {
504                         member.structAlignment = Max(member.structAlignment, alignment);
505
506                         if(member.memberOffset % alignment)
507                            member.memberOffset += alignment - (member.memberOffset % alignment);
508                      }
509
510                      dataMember.offset = member.memberOffset;
511                      if(member.type == unionMember)
512                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
513                      else
514                      {
515                         member.memberOffset += size;
516                      }
517                   }
518                   else
519                   {
520                      // TESTING THIS PADDING CODE
521                      if(alignment)
522                      {
523                         _class.structAlignment = Max(_class.structAlignment, alignment);
524
525                         if(_class.memberOffset % alignment)
526                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
527                      }
528
529                      dataMember.offset = _class.memberOffset;
530                      _class.memberOffset += size;
531                   }
532                }
533                else
534                {
535                   int alignment;
536
537                   ComputeClassMembers((Class)dataMember, true);
538                   alignment = dataMember.structAlignment;
539
540                   if(isMember)
541                   {
542                      if(alignment)
543                      {
544                         if(member.memberOffset % alignment)
545                            member.memberOffset += alignment - (member.memberOffset % alignment);
546
547                         member.structAlignment = Max(member.structAlignment, alignment);
548                      }
549                      dataMember.offset = member.memberOffset;
550                      if(member.type == unionMember)
551                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
552                      else
553                         member.memberOffset += dataMember.memberOffset;
554                   }
555                   else
556                   {
557                      if(alignment)
558                      {
559                         if(_class.memberOffset % alignment)
560                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
561                         _class.structAlignment = Max(_class.structAlignment, alignment);
562                      }
563                      dataMember.offset = _class.memberOffset;
564                      _class.memberOffset += dataMember.memberOffset;
565                   }
566                }
567             }
568          }
569          if(bitFields)
570          {
571             int alignment = 0;
572             int size = (bitFields + 7) / 8;
573
574             if(isMember)
575             {
576                // TESTING THIS PADDING CODE
577                if(alignment)
578                {
579                   member.structAlignment = Max(member.structAlignment, alignment);
580
581                   if(member.memberOffset % alignment)
582                      member.memberOffset += alignment - (member.memberOffset % alignment);
583                }
584
585                if(member.type == unionMember)
586                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
587                else
588                {
589                   member.memberOffset += size;
590                }
591             }
592             else
593             {
594                // TESTING THIS PADDING CODE
595                if(alignment)
596                {
597                   _class.structAlignment = Max(_class.structAlignment, alignment);
598
599                   if(_class.memberOffset % alignment)
600                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
601                }
602                _class.memberOffset += size;
603             }
604             bitFields = 0;
605          }
606       }
607       if(member && member.type == unionMember)
608       {
609          member.memberOffset = unionMemberOffset;
610       }
611
612       if(!isMember)
613       {
614          /*if(_class.type == structClass)
615             _class.size = _class.memberOffset;
616          else
617          */
618
619          if(_class.type != bitClass)
620          {
621             int extra = 0;
622             if(_class.structAlignment)
623             {
624                if(_class.memberOffset % _class.structAlignment)
625                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
626             }
627             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
628             if(!member)
629             {
630                Property prop;
631                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
632                {
633                   if(prop.isProperty && prop.isWatchable)
634                   {
635                      prop.watcherOffset = _class.structSize;
636                      _class.structSize += sizeof(OldList);
637                   }
638                }
639             }
640
641             // Fix Derivatives
642             {
643                OldLink derivative;
644                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
645                {
646                   Class deriv = derivative.data;
647
648                   if(deriv.computeSize)
649                   {
650                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
651                      deriv.offset = /*_class.offset + */_class.structSize;
652                      deriv.memberOffset = 0;
653                      // ----------------------
654
655                      deriv.structSize = deriv.offset;
656
657                      ComputeClassMembers(deriv, false);
658                   }
659                }
660             }
661          }
662       }
663    }
664    if(context)
665       FinishTemplatesContext(context);
666 }
667
668 public void ComputeModuleClasses(Module module)
669 {
670    Class _class;
671    OldLink subModule;
672
673    for(subModule = module.modules.first; subModule; subModule = subModule.next)
674       ComputeModuleClasses(subModule.data);
675    for(_class = module.classes.first; _class; _class = _class.next)
676       ComputeClassMembers(_class, false);
677 }
678
679
680 public int ComputeTypeSize(Type type)
681 {
682    uint size = type ? type.size : 0;
683    if(!size && type && !type.computing)
684    {
685       type.computing = true;
686       switch(type.kind)
687       {
688          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
689          case charType: type.alignment = size = sizeof(char); break;
690          case intType: type.alignment = size = sizeof(int); break;
691          case int64Type: type.alignment = size = sizeof(int64); break;
692          case intPtrType: type.alignment = size = targetBits / 8; break;
693          case intSizeType: type.alignment = size = targetBits / 8; break;
694          case longType: type.alignment = size = sizeof(long); break;
695          case shortType: type.alignment = size = sizeof(short); break;
696          case floatType: type.alignment = size = sizeof(float); break;
697          case doubleType: type.alignment = size = sizeof(double); break;
698          case classType:
699          {
700             Class _class = type._class ? type._class.registered : null;
701
702             if(_class && _class.type == structClass)
703             {
704                // Ensure all members are properly registered
705                ComputeClassMembers(_class, false);
706                type.alignment = _class.structAlignment;
707                size = _class.structSize;
708                if(type.alignment && size % type.alignment)
709                   size += type.alignment - (size % type.alignment);
710
711             }
712             else if(_class && (_class.type == unitClass ||
713                    _class.type == enumClass ||
714                    _class.type == bitClass))
715             {
716                if(!_class.dataType)
717                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
718                size = type.alignment = ComputeTypeSize(_class.dataType);
719             }
720             else
721                size = type.alignment = targetBits / 8; // sizeof(Instance *);
722             break;
723          }
724          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
725          case arrayType:
726             if(type.arraySizeExp)
727             {
728                ProcessExpressionType(type.arraySizeExp);
729                ComputeExpression(type.arraySizeExp);
730                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType &&
731                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
732                {
733                   Location oldLoc = yylloc;
734                   // bool isConstant = type.arraySizeExp.isConstant;
735                   char expression[10240];
736                   expression[0] = '\0';
737                   type.arraySizeExp.expType = null;
738                   yylloc = type.arraySizeExp.loc;
739                   if(inCompiler)
740                      PrintExpression(type.arraySizeExp, expression);
741                   Compiler_Error($"Array size not constant int (%s)\n", expression);
742                   yylloc = oldLoc;
743                }
744                GetInt(type.arraySizeExp, &type.arraySize);
745             }
746             else if(type.enumClass)
747             {
748                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
749                {
750                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
751                }
752                else
753                   type.arraySize = 0;
754             }
755             else
756             {
757                // Unimplemented auto size
758                type.arraySize = 0;
759             }
760
761             size = ComputeTypeSize(type.type) * type.arraySize;
762             if(type.type)
763                type.alignment = type.type.alignment;
764
765             break;
766          case structType:
767          {
768             Type member;
769             for(member = type.members.first; member; member = member.next)
770             {
771                uint addSize = ComputeTypeSize(member);
772
773                member.offset = size;
774                if(member.alignment && size % member.alignment)
775                   member.offset += member.alignment - (size % member.alignment);
776                size = member.offset;
777
778                type.alignment = Max(type.alignment, member.alignment);
779                size += addSize;
780             }
781             if(type.alignment && size % type.alignment)
782                size += type.alignment - (size % type.alignment);
783             break;
784          }
785          case unionType:
786          {
787             Type member;
788             for(member = type.members.first; member; member = member.next)
789             {
790                uint addSize = ComputeTypeSize(member);
791
792                member.offset = size;
793                if(member.alignment && size % member.alignment)
794                   member.offset += member.alignment - (size % member.alignment);
795                size = member.offset;
796
797                type.alignment = Max(type.alignment, member.alignment);
798                size = Max(size, addSize);
799             }
800             if(type.alignment && size % type.alignment)
801                size += type.alignment - (size % type.alignment);
802             break;
803          }
804          case templateType:
805          {
806             TemplateParameter param = type.templateParameter;
807             Type baseType = ProcessTemplateParameterType(param);
808             if(baseType)
809             {
810                size = ComputeTypeSize(baseType);
811                type.alignment = baseType.alignment;
812             }
813             else
814                type.alignment = size = sizeof(uint64);
815             break;
816          }
817          case enumType:
818          {
819             type.alignment = size = sizeof(enum { test });
820             break;
821          }
822          case thisClassType:
823          {
824             type.alignment = size = targetBits / 8; //sizeof(void *);
825             break;
826          }
827       }
828       type.size = size;
829       type.computing = false;
830    }
831    return size;
832 }
833
834
835 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
836 {
837    // This function is in need of a major review when implementing private members etc.
838    DataMember topMember = isMember ? (DataMember) _class : null;
839    uint totalSize = 0;
840    uint maxSize = 0;
841    int alignment, size;
842    DataMember member;
843    Context context = isMember ? null : SetupTemplatesContext(_class);
844    if(addedPadding)
845       *addedPadding = false;
846
847    if(!isMember && _class.base)
848    {
849       maxSize = _class.structSize;
850       //if(_class.base.type != systemClass) // Commented out with new Instance _class
851       {
852          // DANGER: Testing this noHeadClass here...
853          if(_class.type == structClass || _class.type == noHeadClass)
854             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
855          else
856          {
857             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
858             if(maxSize > baseSize)
859                maxSize -= baseSize;
860             else
861                maxSize = 0;
862          }
863       }
864    }
865
866    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
867    {
868       if(!member.isProperty)
869       {
870          switch(member.type)
871          {
872             case normalMember:
873             {
874                if(member.dataTypeString)
875                {
876                   OldList * specs = MkList(), * decls = MkList();
877                   Declarator decl;
878
879                   decl = SpecDeclFromString(member.dataTypeString, specs,
880                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
881                   ListAdd(decls, MkStructDeclarator(decl, null));
882                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
883
884                   if(!member.dataType)
885                      member.dataType = ProcessType(specs, decl);
886
887                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
888
889                   {
890                      Type type = ProcessType(specs, decl);
891                      DeclareType(member.dataType, false, false);
892                      FreeType(type);
893                   }
894                   /*
895                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
896                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
897                      DeclareStruct(member.dataType._class.string, false);
898                   */
899
900                   ComputeTypeSize(member.dataType);
901                   size = member.dataType.size;
902                   alignment = member.dataType.alignment;
903
904                   if(alignment)
905                   {
906                      if(totalSize % alignment)
907                         totalSize += alignment - (totalSize % alignment);
908                   }
909                   totalSize += size;
910                }
911                break;
912             }
913             case unionMember:
914             case structMember:
915             {
916                OldList * specs = MkList(), * list = MkList();
917
918                size = 0;
919                AddMembers(list, (Class)member, true, &size, topClass, null);
920                ListAdd(specs,
921                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
922                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
923                alignment = member.structAlignment;
924
925                if(alignment)
926                {
927                   if(totalSize % alignment)
928                      totalSize += alignment - (totalSize % alignment);
929                }
930                totalSize += size;
931                break;
932             }
933          }
934       }
935    }
936    if(retSize)
937    {
938       if(topMember && topMember.type == unionMember)
939          *retSize = Max(*retSize, totalSize);
940       else
941          *retSize += totalSize;
942    }
943    else if(totalSize < maxSize && _class.type != systemClass)
944    {
945       int autoPadding = 0;
946       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
947          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
948       if(totalSize + autoPadding < maxSize)
949       {
950          char sizeString[50];
951          sprintf(sizeString, "%d", maxSize - totalSize);
952          ListAdd(declarations,
953             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
954             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
955          if(addedPadding)
956             *addedPadding = true;
957       }
958    }
959    if(context)
960       FinishTemplatesContext(context);
961    return topMember ? topMember.memberID : _class.memberID;
962 }
963
964 static int DeclareMembers(Class _class, bool isMember)
965 {
966    DataMember topMember = isMember ? (DataMember) _class : null;
967    uint totalSize = 0;
968    DataMember member;
969    Context context = isMember ? null : SetupTemplatesContext(_class);
970
971    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
972       DeclareMembers(_class.base, false);
973
974    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
975    {
976       if(!member.isProperty)
977       {
978          switch(member.type)
979          {
980             case normalMember:
981             {
982                /*
983                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
984                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
985                   DeclareStruct(member.dataType._class.string, false);
986                   */
987                if(!member.dataType && member.dataTypeString)
988                   member.dataType = ProcessTypeString(member.dataTypeString, false);
989                if(member.dataType)
990                   DeclareType(member.dataType, false, false);
991                break;
992             }
993             case unionMember:
994             case structMember:
995             {
996                DeclareMembers((Class)member, true);
997                break;
998             }
999          }
1000       }
1001    }
1002    if(context)
1003       FinishTemplatesContext(context);
1004
1005    return topMember ? topMember.memberID : _class.memberID;
1006 }
1007
1008 void DeclareStruct(char * name, bool skipNoHead)
1009 {
1010    External external = null;
1011    Symbol classSym = FindClass(name);
1012
1013    if(!inCompiler || !classSym) return;
1014
1015    // We don't need any declaration for bit classes...
1016    if(classSym.registered &&
1017       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1018       return;
1019
1020    /*if(classSym.registered.templateClass)
1021       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1022    */
1023
1024    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1025    {
1026       // Add typedef struct
1027       Declaration decl;
1028       OldList * specifiers, * declarators;
1029       OldList * declarations = null;
1030       char structName[1024];
1031       external = (classSym.registered && classSym.registered.type == structClass) ?
1032          classSym.pointerExternal : classSym.structExternal;
1033
1034       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1035       // Moved this one up because DeclareClass done later will need it
1036
1037       classSym.declaring++;
1038
1039       if(strchr(classSym.string, '<'))
1040       {
1041          if(classSym.registered.templateClass)
1042          {
1043             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1044             classSym.declaring--;
1045          }
1046          return;
1047       }
1048
1049       //if(!skipNoHead)
1050          DeclareMembers(classSym.registered, false);
1051
1052       structName[0] = 0;
1053       FullClassNameCat(structName, name, false);
1054
1055       /*if(!external)
1056          external = MkExternalDeclaration(null);*/
1057
1058       if(!skipNoHead)
1059       {
1060          bool addedPadding = false;
1061          classSym.declaredStructSym = true;
1062
1063          declarations = MkList();
1064
1065          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1066
1067          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1068          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1069
1070          if(!declarations->count || (declarations->count == 1 && addedPadding))
1071          {
1072             FreeList(declarations, FreeClassDef);
1073             declarations = null;
1074          }
1075       }
1076       if(skipNoHead || declarations)
1077       {
1078          if(external && external.declaration)
1079          {
1080             ((Specifier)external.declaration.specifiers->first).definitions = declarations;
1081
1082             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1083             {
1084                // TODO: Fix this
1085                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1086
1087                // DANGER
1088                if(classSym.structExternal)
1089                   ast->Move(classSym.structExternal, curExternal.prev);
1090                ast->Move(classSym.pointerExternal, curExternal.prev);
1091
1092                classSym.id = curExternal.symbol.idCode;
1093                classSym.idCode = curExternal.symbol.idCode;
1094                // external = classSym.pointerExternal;
1095                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1096             }
1097          }
1098          else
1099          {
1100             if(!external)
1101                external = MkExternalDeclaration(null);
1102
1103             specifiers = MkList();
1104             declarators = MkList();
1105             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1106
1107             /*
1108             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1109             ListAdd(declarators, MkInitDeclarator(d, null));
1110             */
1111             external.declaration = decl = MkDeclaration(specifiers, declarators);
1112             if(decl.symbol && !decl.symbol.pointerExternal)
1113                decl.symbol.pointerExternal = external;
1114
1115             // For simple classes, keep the declaration as the external to move around
1116             if(classSym.registered && classSym.registered.type == structClass)
1117             {
1118                char className[1024];
1119                strcpy(className, "__ecereClass_");
1120                FullClassNameCat(className, classSym.string, true);
1121                MangleClassName(className);
1122
1123                // Testing This
1124                DeclareClass(classSym, className);
1125
1126                external.symbol = classSym;
1127                classSym.pointerExternal = external;
1128                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1129                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1130             }
1131             else
1132             {
1133                char className[1024];
1134                strcpy(className, "__ecereClass_");
1135                FullClassNameCat(className, classSym.string, true);
1136                MangleClassName(className);
1137
1138                // TOFIX: TESTING THIS...
1139                classSym.structExternal = external;
1140                DeclareClass(classSym, className);
1141                external.symbol = classSym;
1142             }
1143
1144             //if(curExternal)
1145                ast->Insert(curExternal ? curExternal.prev : null, external);
1146          }
1147       }
1148
1149       classSym.declaring--;
1150    }
1151    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1152    {
1153       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1154       // Moved this one up because DeclareClass done later will need it
1155
1156       // TESTING THIS:
1157       classSym.declaring++;
1158
1159       //if(!skipNoHead)
1160       {
1161          if(classSym.registered)
1162             DeclareMembers(classSym.registered, false);
1163       }
1164
1165       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1166       {
1167          // TODO: Fix this
1168          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1169
1170          // DANGER
1171          if(classSym.structExternal)
1172             ast->Move(classSym.structExternal, curExternal.prev);
1173          ast->Move(classSym.pointerExternal, curExternal.prev);
1174
1175          classSym.id = curExternal.symbol.idCode;
1176          classSym.idCode = curExternal.symbol.idCode;
1177          // external = classSym.pointerExternal;
1178          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1179       }
1180
1181       classSym.declaring--;
1182    }
1183    //return external;
1184 }
1185
1186 void DeclareProperty(Property prop, char * setName, char * getName)
1187 {
1188    Symbol symbol = prop.symbol;
1189    char propName[1024];
1190
1191    strcpy(setName, "__ecereProp_");
1192    FullClassNameCat(setName, prop._class.fullName, false);
1193    strcat(setName, "_Set_");
1194    // strcat(setName, prop.name);
1195    FullClassNameCat(setName, prop.name, true);
1196
1197    strcpy(getName, "__ecereProp_");
1198    FullClassNameCat(getName, prop._class.fullName, false);
1199    strcat(getName, "_Get_");
1200    FullClassNameCat(getName, prop.name, true);
1201    // strcat(getName, prop.name);
1202
1203    strcpy(propName, "__ecereProp_");
1204    FullClassNameCat(propName, prop._class.fullName, false);
1205    strcat(propName, "_");
1206    FullClassNameCat(propName, prop.name, true);
1207    // strcat(propName, prop.name);
1208
1209    // To support "char *" property
1210    MangleClassName(getName);
1211    MangleClassName(setName);
1212    MangleClassName(propName);
1213
1214    if(prop._class.type == structClass)
1215       DeclareStruct(prop._class.fullName, false);
1216
1217    if(!symbol || curExternal.symbol.idCode < symbol.id)
1218    {
1219       bool imported = false;
1220       bool dllImport = false;
1221       if(!symbol || symbol._import)
1222       {
1223          if(!symbol)
1224          {
1225             Symbol classSym;
1226             if(!prop._class.symbol)
1227                prop._class.symbol = FindClass(prop._class.fullName);
1228             classSym = prop._class.symbol;
1229             if(classSym && !classSym._import)
1230             {
1231                ModuleImport module;
1232
1233                if(prop._class.module)
1234                   module = FindModule(prop._class.module);
1235                else
1236                   module = mainModule;
1237
1238                classSym._import = ClassImport
1239                {
1240                   name = CopyString(prop._class.fullName);
1241                   isRemote = prop._class.isRemote;
1242                };
1243                module.classes.Add(classSym._import);
1244             }
1245             symbol = prop.symbol = Symbol { };
1246             symbol._import = (ClassImport)PropertyImport
1247             {
1248                name = CopyString(prop.name);
1249                isVirtual = false; //prop.isVirtual;
1250                hasSet = prop.Set ? true : false;
1251                hasGet = prop.Get ? true : false;
1252             };
1253             if(classSym)
1254                classSym._import.properties.Add(symbol._import);
1255          }
1256          imported = true;
1257          if(prop._class.module != privateModule && prop._class.module.importType != staticImport)
1258             dllImport = true;
1259       }
1260
1261       if(!symbol.type)
1262       {
1263          Context context = SetupTemplatesContext(prop._class);
1264          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1265          FinishTemplatesContext(context);
1266       }
1267
1268       // Get
1269       if(prop.Get)
1270       {
1271          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1272          {
1273             Declaration decl;
1274             OldList * specifiers, * declarators;
1275             Declarator d;
1276             OldList * params;
1277             Specifier spec;
1278             External external;
1279             Declarator typeDecl;
1280             bool simple = false;
1281
1282             specifiers = MkList();
1283             declarators = MkList();
1284             params = MkList();
1285
1286             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1287                MkDeclaratorIdentifier(MkIdentifier("this"))));
1288
1289             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1290             //if(imported)
1291             if(dllImport)
1292                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1293
1294             {
1295                Context context = SetupTemplatesContext(prop._class);
1296                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1297                FinishTemplatesContext(context);
1298             }
1299
1300             // Make sure the simple _class's type is declared
1301             for(spec = specifiers->first; spec; spec = spec.next)
1302             {
1303                if(spec.type == nameSpecifier /*SpecifierClass*/)
1304                {
1305                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1306                   {
1307                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1308                      symbol._class = classSym.registered;
1309                      if(classSym.registered && classSym.registered.type == structClass)
1310                      {
1311                         DeclareStruct(spec.name, false);
1312                         simple = true;
1313                      }
1314                   }
1315                }
1316             }
1317
1318             if(!simple)
1319                d = PlugDeclarator(typeDecl, d);
1320             else
1321             {
1322                ListAdd(params, MkTypeName(specifiers,
1323                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1324                specifiers = MkList();
1325             }
1326
1327             d = MkDeclaratorFunction(d, params);
1328
1329             //if(imported)
1330             if(dllImport)
1331                specifiers->Insert(null, MkSpecifier(EXTERN));
1332             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1333                specifiers->Insert(null, MkSpecifier(STATIC));
1334             if(simple)
1335                ListAdd(specifiers, MkSpecifier(VOID));
1336
1337             ListAdd(declarators, MkInitDeclarator(d, null));
1338
1339             decl = MkDeclaration(specifiers, declarators);
1340
1341             external = MkExternalDeclaration(decl);
1342             ast->Insert(curExternal.prev, external);
1343             external.symbol = symbol;
1344             symbol.externalGet = external;
1345
1346             ReplaceThisClassSpecifiers(specifiers, prop._class);
1347
1348             if(typeDecl)
1349                FreeDeclarator(typeDecl);
1350          }
1351          else
1352          {
1353             // Move declaration higher...
1354             ast->Move(symbol.externalGet, curExternal.prev);
1355          }
1356       }
1357
1358       // Set
1359       if(prop.Set)
1360       {
1361          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1362          {
1363             Declaration decl;
1364             OldList * specifiers, * declarators;
1365             Declarator d;
1366             OldList * params;
1367             Specifier spec;
1368             External external;
1369             Declarator typeDecl;
1370
1371             declarators = MkList();
1372             params = MkList();
1373
1374             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1375             if(!prop.conversion || prop._class.type == structClass)
1376             {
1377                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1378                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1379             }
1380
1381             specifiers = MkList();
1382
1383             {
1384                Context context = SetupTemplatesContext(prop._class);
1385                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1386                   MkDeclaratorIdentifier(MkIdentifier("value")));
1387                FinishTemplatesContext(context);
1388             }
1389             ListAdd(params, MkTypeName(specifiers, d));
1390
1391             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1392             //if(imported)
1393             if(dllImport)
1394                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1395             d = MkDeclaratorFunction(d, params);
1396
1397             // Make sure the simple _class's type is declared
1398             for(spec = specifiers->first; spec; spec = spec.next)
1399             {
1400                if(spec.type == nameSpecifier /*SpecifierClass*/)
1401                {
1402                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1403                   {
1404                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1405                      symbol._class = classSym.registered;
1406                      if(classSym.registered && classSym.registered.type == structClass)
1407                         DeclareStruct(spec.name, false);
1408                   }
1409                }
1410             }
1411
1412             ListAdd(declarators, MkInitDeclarator(d, null));
1413
1414             specifiers = MkList();
1415             //if(imported)
1416             if(dllImport)
1417                specifiers->Insert(null, MkSpecifier(EXTERN));
1418             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1419                specifiers->Insert(null, MkSpecifier(STATIC));
1420
1421             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1422             if(!prop.conversion || prop._class.type == structClass)
1423                ListAdd(specifiers, MkSpecifier(VOID));
1424             else
1425                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1426
1427             decl = MkDeclaration(specifiers, declarators);
1428
1429             external = MkExternalDeclaration(decl);
1430             ast->Insert(curExternal.prev, external);
1431             external.symbol = symbol;
1432             symbol.externalSet = external;
1433
1434             ReplaceThisClassSpecifiers(specifiers, prop._class);
1435          }
1436          else
1437          {
1438             // Move declaration higher...
1439             ast->Move(symbol.externalSet, curExternal.prev);
1440          }
1441       }
1442
1443       // Property (for Watchers)
1444       if(!symbol.externalPtr)
1445       {
1446          Declaration decl;
1447          External external;
1448          OldList * specifiers = MkList();
1449
1450          if(imported)
1451             specifiers->Insert(null, MkSpecifier(EXTERN));
1452          else
1453             specifiers->Insert(null, MkSpecifier(STATIC));
1454
1455          ListAdd(specifiers, MkSpecifierName("Property"));
1456
1457          {
1458             OldList * list = MkList();
1459             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1460                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1461
1462             if(!imported)
1463             {
1464                strcpy(propName, "__ecerePropM_");
1465                FullClassNameCat(propName, prop._class.fullName, false);
1466                strcat(propName, "_");
1467                // strcat(propName, prop.name);
1468                FullClassNameCat(propName, prop.name, true);
1469
1470                MangleClassName(propName);
1471
1472                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1473                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1474             }
1475             decl = MkDeclaration(specifiers, list);
1476          }
1477
1478          external = MkExternalDeclaration(decl);
1479          ast->Insert(curExternal.prev, external);
1480          external.symbol = symbol;
1481          symbol.externalPtr = external;
1482       }
1483       else
1484       {
1485          // Move declaration higher...
1486          ast->Move(symbol.externalPtr, curExternal.prev);
1487       }
1488
1489       symbol.id = curExternal.symbol.idCode;
1490    }
1491 }
1492
1493 // ***************** EXPRESSION PROCESSING ***************************
1494 public Type Dereference(Type source)
1495 {
1496    Type type = null;
1497    if(source)
1498    {
1499       if(source.kind == pointerType || source.kind == arrayType)
1500       {
1501          type = source.type;
1502          source.type.refCount++;
1503       }
1504       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1505       {
1506          type = Type
1507          {
1508             kind = charType;
1509             refCount = 1;
1510          };
1511       }
1512       // Support dereferencing of no head classes for now...
1513       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1514       {
1515          type = source;
1516          source.refCount++;
1517       }
1518       else
1519          Compiler_Error($"cannot dereference type\n");
1520    }
1521    return type;
1522 }
1523
1524 static Type Reference(Type source)
1525 {
1526    Type type = null;
1527    if(source)
1528    {
1529       type = Type
1530       {
1531          kind = pointerType;
1532          type = source;
1533          refCount = 1;
1534       };
1535       source.refCount++;
1536    }
1537    return type;
1538 }
1539
1540 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1541 {
1542    Identifier ident = member.identifiers ? member.identifiers->first : null;
1543    bool found = false;
1544    DataMember dataMember = null;
1545    Method method = null;
1546    bool freeType = false;
1547
1548    yylloc = member.loc;
1549
1550    if(!ident)
1551    {
1552       if(curMember)
1553       {
1554          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1555          if(*curMember)
1556          {
1557             found = true;
1558             dataMember = *curMember;
1559          }
1560       }
1561    }
1562    else
1563    {
1564       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1565       DataMember _subMemberStack[256];
1566       int _subMemberStackPos = 0;
1567
1568       // FILL MEMBER STACK
1569       if(!thisMember)
1570          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1571       if(thisMember)
1572       {
1573          dataMember = thisMember;
1574          if(curMember && thisMember.memberAccess == publicAccess)
1575          {
1576             *curMember = thisMember;
1577             *curClass = thisMember._class;
1578             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1579             *subMemberStackPos = _subMemberStackPos;
1580          }
1581          found = true;
1582       }
1583       else
1584       {
1585          // Setting a method
1586          method = eClass_FindMethod(_class, ident.string, privateModule);
1587          if(method && method.type == virtualMethod)
1588             found = true;
1589          else
1590             method = null;
1591       }
1592    }
1593
1594    if(found)
1595    {
1596       Type type = null;
1597       if(dataMember)
1598       {
1599          if(!dataMember.dataType && dataMember.dataTypeString)
1600          {
1601             //Context context = SetupTemplatesContext(dataMember._class);
1602             Context context = SetupTemplatesContext(_class);
1603             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1604             FinishTemplatesContext(context);
1605          }
1606          type = dataMember.dataType;
1607       }
1608       else if(method)
1609       {
1610          // This is for destination type...
1611          if(!method.dataType)
1612             ProcessMethodType(method);
1613          //DeclareMethod(method);
1614          // method.dataType = ((Symbol)method.symbol)->type;
1615          type = method.dataType;
1616       }
1617
1618       if(ident && ident.next)
1619       {
1620          for(ident = ident.next; ident && type; ident = ident.next)
1621          {
1622             if(type.kind == classType)
1623             {
1624                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1625                if(!dataMember)
1626                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1627                if(dataMember)
1628                   type = dataMember.dataType;
1629             }
1630             else if(type.kind == structType || type.kind == unionType)
1631             {
1632                Type memberType;
1633                for(memberType = type.members.first; memberType; memberType = memberType.next)
1634                {
1635                   if(!strcmp(memberType.name, ident.string))
1636                   {
1637                      type = memberType;
1638                      break;
1639                   }
1640                }
1641             }
1642          }
1643       }
1644
1645       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1646       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1647       {
1648          int id = 0;
1649          ClassTemplateParameter curParam = null;
1650          Class sClass;
1651          for(sClass = _class; sClass; sClass = sClass.base)
1652          {
1653             id = 0;
1654             if(sClass.templateClass) sClass = sClass.templateClass;
1655             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1656             {
1657                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1658                {
1659                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1660                   {
1661                      if(sClass.templateClass) sClass = sClass.templateClass;
1662                      id += sClass.templateParams.count;
1663                   }
1664                   break;
1665                }
1666                id++;
1667             }
1668             if(curParam) break;
1669          }
1670
1671          if(curParam)
1672          {
1673             ClassTemplateArgument arg = _class.templateArgs[id];
1674             if(arg.dataTypeString)
1675             {
1676                // FreeType(type);
1677                type = ProcessTypeString(arg.dataTypeString, false);
1678                freeType = true;
1679                if(type && _class.templateClass)
1680                   type.passAsTemplate = true;
1681                if(type)
1682                {
1683                   // type.refCount++;
1684                   /*if(!exp.destType)
1685                   {
1686                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1687                      exp.destType.refCount++;
1688                   }*/
1689                }
1690             }
1691          }
1692       }
1693       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1694       {
1695          Class expClass = type._class.registered;
1696          Class cClass = null;
1697          int c;
1698          int paramCount = 0;
1699          int lastParam = -1;
1700
1701          char templateString[1024];
1702          ClassTemplateParameter param;
1703          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1704          for(cClass = expClass; cClass; cClass = cClass.base)
1705          {
1706             int p = 0;
1707             if(cClass.templateClass) cClass = cClass.templateClass;
1708             for(param = cClass.templateParams.first; param; param = param.next)
1709             {
1710                int id = p;
1711                Class sClass;
1712                ClassTemplateArgument arg;
1713                for(sClass = cClass.base; sClass; sClass = sClass.base)
1714                {
1715                   if(sClass.templateClass) sClass = sClass.templateClass;
1716                   id += sClass.templateParams.count;
1717                }
1718                arg = expClass.templateArgs[id];
1719
1720                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1721                {
1722                   ClassTemplateParameter cParam;
1723                   //int p = numParams - sClass.templateParams.count;
1724                   int p = 0;
1725                   Class nextClass;
1726                   if(sClass.templateClass) sClass = sClass.templateClass;
1727
1728                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1729                   {
1730                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1731                      p += nextClass.templateParams.count;
1732                   }
1733
1734                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1735                   {
1736                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1737                      {
1738                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1739                         {
1740                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1741                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1742                            break;
1743                         }
1744                      }
1745                   }
1746                }
1747
1748                {
1749                   char argument[256];
1750                   argument[0] = '\0';
1751                   /*if(arg.name)
1752                   {
1753                      strcat(argument, arg.name.string);
1754                      strcat(argument, " = ");
1755                   }*/
1756                   switch(param.type)
1757                   {
1758                      case expression:
1759                      {
1760                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1761                         char expString[1024];
1762                         OldList * specs = MkList();
1763                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1764                         Expression exp;
1765                         char * string = PrintHexUInt64(arg.expression.ui64);
1766                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1767                         delete string;
1768
1769                         ProcessExpressionType(exp);
1770                         ComputeExpression(exp);
1771                         expString[0] = '\0';
1772                         PrintExpression(exp, expString);
1773                         strcat(argument, expString);
1774                         //delete exp;
1775                         FreeExpression(exp);
1776                         break;
1777                      }
1778                      case identifier:
1779                      {
1780                         strcat(argument, arg.member.name);
1781                         break;
1782                      }
1783                      case TemplateParameterType::type:
1784                      {
1785                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1786                            strcat(argument, arg.dataTypeString);
1787                         break;
1788                      }
1789                   }
1790                   if(argument[0])
1791                   {
1792                      if(paramCount) strcat(templateString, ", ");
1793                      if(lastParam != p - 1)
1794                      {
1795                         strcat(templateString, param.name);
1796                         strcat(templateString, " = ");
1797                      }
1798                      strcat(templateString, argument);
1799                      paramCount++;
1800                      lastParam = p;
1801                   }
1802                   p++;
1803                }
1804             }
1805          }
1806          {
1807             int len = strlen(templateString);
1808             if(templateString[len-1] == '<')
1809                len--;
1810             else
1811             {
1812                if(templateString[len-1] == '>')
1813                   templateString[len++] = ' ';
1814                templateString[len++] = '>';
1815             }
1816             templateString[len++] = '\0';
1817          }
1818          {
1819             Context context = SetupTemplatesContext(_class);
1820             if(freeType) FreeType(type);
1821             type = ProcessTypeString(templateString, false);
1822             freeType = true;
1823             FinishTemplatesContext(context);
1824          }
1825       }
1826
1827       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1828       {
1829          ProcessExpressionType(member.initializer.exp);
1830          if(!member.initializer.exp.expType)
1831          {
1832             if(inCompiler)
1833             {
1834                char expString[10240];
1835                expString[0] = '\0';
1836                PrintExpression(member.initializer.exp, expString);
1837                ChangeCh(expString, '\n', ' ');
1838                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1839             }
1840          }
1841          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1842          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1843          {
1844             Compiler_Error($"incompatible instance method %s\n", ident.string);
1845          }
1846       }
1847       else if(member.initializer)
1848       {
1849          /*
1850          FreeType(member.exp.destType);
1851          member.exp.destType = type;
1852          if(member.exp.destType)
1853             member.exp.destType.refCount++;
1854          ProcessExpressionType(member.exp);
1855          */
1856
1857          ProcessInitializer(member.initializer, type);
1858       }
1859       if(freeType) FreeType(type);
1860    }
1861    else
1862    {
1863       if(_class && _class.type == unitClass)
1864       {
1865          if(member.initializer)
1866          {
1867             /*
1868             FreeType(member.exp.destType);
1869             member.exp.destType = MkClassType(_class.fullName);
1870             ProcessExpressionType(member.initializer, type);
1871             */
1872             Type type = MkClassType(_class.fullName);
1873             ProcessInitializer(member.initializer, type);
1874             FreeType(type);
1875          }
1876       }
1877       else
1878       {
1879          if(member.initializer)
1880          {
1881             //ProcessExpressionType(member.exp);
1882             ProcessInitializer(member.initializer, null);
1883          }
1884          if(ident)
1885          {
1886             if(method)
1887             {
1888                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1889             }
1890             else if(_class)
1891             {
1892                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1893                if(inCompiler)
1894                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1895             }
1896          }
1897          else if(_class)
1898             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1899       }
1900    }
1901 }
1902
1903 void ProcessInstantiationType(Instantiation inst)
1904 {
1905    yylloc = inst.loc;
1906    if(inst._class)
1907    {
1908       MembersInit members;
1909       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1910       Class _class;
1911
1912       /*if(!inst._class.symbol)
1913          inst._class.symbol = FindClass(inst._class.name);*/
1914       classSym = inst._class.symbol;
1915       _class = classSym ? classSym.registered : null;
1916
1917       // DANGER: Patch for mutex not declaring its struct when not needed
1918       if(!_class || _class.type != noHeadClass)
1919          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1920
1921       afterExternal = afterExternal ? afterExternal : curExternal;
1922
1923       if(inst.exp)
1924          ProcessExpressionType(inst.exp);
1925
1926       inst.isConstant = true;
1927       if(inst.members)
1928       {
1929          DataMember curMember = null;
1930          Class curClass = null;
1931          DataMember subMemberStack[256];
1932          int subMemberStackPos = 0;
1933
1934          for(members = inst.members->first; members; members = members.next)
1935          {
1936             switch(members.type)
1937             {
1938                case methodMembersInit:
1939                {
1940                   char name[1024];
1941                   static uint instMethodID = 0;
1942                   External external = curExternal;
1943                   Context context = curContext;
1944                   Declarator declarator = members.function.declarator;
1945                   Identifier nameID = GetDeclId(declarator);
1946                   char * unmangled = nameID ? nameID.string : null;
1947                   Expression exp;
1948                   External createdExternal = null;
1949
1950                   if(inCompiler)
1951                   {
1952                      char number[16];
1953                      //members.function.dontMangle = true;
1954                      strcpy(name, "__ecereInstMeth_");
1955                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
1956                      strcat(name, "_");
1957                      strcat(name, nameID.string);
1958                      strcat(name, "_");
1959                      sprintf(number, "_%08d", instMethodID++);
1960                      strcat(name, number);
1961                      nameID.string = CopyString(name);
1962                   }
1963
1964                   // Do modifications here...
1965                   if(declarator)
1966                   {
1967                      Symbol symbol = declarator.symbol;
1968                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
1969
1970                      if(method && method.type == virtualMethod)
1971                      {
1972                         symbol.method = method;
1973                         ProcessMethodType(method);
1974
1975                         if(!symbol.type.thisClass)
1976                         {
1977                            if(method.dataType.thisClass && currentClass &&
1978                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
1979                            {
1980                               if(!currentClass.symbol)
1981                                  currentClass.symbol = FindClass(currentClass.fullName);
1982                               symbol.type.thisClass = currentClass.symbol;
1983                            }
1984                            else
1985                            {
1986                               if(!_class.symbol)
1987                                  _class.symbol = FindClass(_class.fullName);
1988                               symbol.type.thisClass = _class.symbol;
1989                            }
1990                         }
1991                         // TESTING THIS HERE:
1992                         DeclareType(symbol.type, true, true);
1993
1994                      }
1995                      else if(classSym)
1996                      {
1997                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
1998                            unmangled, classSym.string);
1999                      }
2000                   }
2001
2002                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2003                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2004
2005                   if(nameID)
2006                   {
2007                      FreeSpecifier(nameID._class);
2008                      nameID._class = null;
2009                   }
2010
2011                   if(inCompiler)
2012                   {
2013
2014                      Type type = declarator.symbol.type;
2015                      External oldExternal = curExternal;
2016
2017                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2018                      // *** It was commented out for problems such as
2019                      /*
2020                            class VirtualDesktop : Window
2021                            {
2022                               clientSize = Size { };
2023                               Timer timer
2024                               {
2025                                  bool DelayExpired()
2026                                  {
2027                                     clientSize.w;
2028                                     return true;
2029                                  }
2030                               };
2031                            }
2032                      */
2033                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2034
2035                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2036
2037                      /*
2038                      if(strcmp(declarator.symbol.string, name))
2039                      {
2040                         printf("TOCHECK: Look out for this\n");
2041                         delete declarator.symbol.string;
2042                         declarator.symbol.string = CopyString(name);
2043                      }
2044
2045                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2046                      {
2047                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2048                         excludedSymbols->Remove(declarator.symbol);
2049                         globalContext.symbols.Add((BTNode)declarator.symbol);
2050                         if(strstr(declarator.symbol.string), "::")
2051                            globalContext.hasNameSpace = true;
2052
2053                      }
2054                      */
2055
2056                      //curExternal = curExternal.prev;
2057                      //afterExternal = afterExternal->next;
2058
2059                      //ProcessFunction(afterExternal->function);
2060
2061                      //curExternal = afterExternal;
2062                      {
2063                         External externalDecl;
2064                         externalDecl = MkExternalDeclaration(null);
2065                         ast->Insert(oldExternal.prev, externalDecl);
2066
2067                         // Which function does this process?
2068                         if(createdExternal.function)
2069                         {
2070                            ProcessFunction(createdExternal.function);
2071
2072                            //curExternal = oldExternal;
2073
2074                            {
2075                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2076
2077                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2078                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2079
2080                               //externalDecl = MkExternalDeclaration(decl);
2081
2082                               //***** ast->Insert(external.prev, externalDecl);
2083                               //ast->Insert(curExternal.prev, externalDecl);
2084                               externalDecl.declaration = decl;
2085                               if(decl.symbol && !decl.symbol.pointerExternal)
2086                                  decl.symbol.pointerExternal = externalDecl;
2087
2088                               // Trying this out...
2089                               declarator.symbol.pointerExternal = externalDecl;
2090                            }
2091                         }
2092                      }
2093                   }
2094                   else if(declarator)
2095                   {
2096                      curExternal = declarator.symbol.pointerExternal;
2097                      ProcessFunction((FunctionDefinition)members.function);
2098                   }
2099                   curExternal = external;
2100                   curContext = context;
2101
2102                   if(inCompiler)
2103                   {
2104                      FreeClassFunction(members.function);
2105
2106                      // In this pass, turn this into a MemberInitData
2107                      exp = QMkExpId(name);
2108                      members.type = dataMembersInit;
2109                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2110
2111                      delete unmangled;
2112                   }
2113                   break;
2114                }
2115                case dataMembersInit:
2116                {
2117                   if(members.dataMembers && classSym)
2118                   {
2119                      MemberInit member;
2120                      Location oldyyloc = yylloc;
2121                      for(member = members.dataMembers->first; member; member = member.next)
2122                      {
2123                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2124                         if(member.initializer && !member.initializer.isConstant)
2125                            inst.isConstant = false;
2126                      }
2127                      yylloc = oldyyloc;
2128                   }
2129                   break;
2130                }
2131             }
2132          }
2133       }
2134    }
2135 }
2136
2137 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2138 {
2139    // OPTIMIZATIONS: TESTING THIS...
2140    if(inCompiler)
2141    {
2142       if(type.kind == functionType)
2143       {
2144          Type param;
2145          if(declareParams)
2146          {
2147             for(param = type.params.first; param; param = param.next)
2148                DeclareType(param, declarePointers, true);
2149          }
2150          DeclareType(type.returnType, declarePointers, true);
2151       }
2152       else if(type.kind == pointerType && declarePointers)
2153          DeclareType(type.type, declarePointers, false);
2154       else if(type.kind == classType)
2155       {
2156          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2157             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2158       }
2159       else if(type.kind == structType || type.kind == unionType)
2160       {
2161          Type member;
2162          for(member = type.members.first; member; member = member.next)
2163             DeclareType(member, false, false);
2164       }
2165       else if(type.kind == arrayType)
2166          DeclareType(type.arrayType, declarePointers, false);
2167    }
2168 }
2169
2170 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2171 {
2172    ClassTemplateArgument * arg = null;
2173    int id = 0;
2174    ClassTemplateParameter curParam = null;
2175    Class sClass;
2176    for(sClass = _class; sClass; sClass = sClass.base)
2177    {
2178       id = 0;
2179       if(sClass.templateClass) sClass = sClass.templateClass;
2180       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2181       {
2182          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2183          {
2184             for(sClass = sClass.base; sClass; sClass = sClass.base)
2185             {
2186                if(sClass.templateClass) sClass = sClass.templateClass;
2187                id += sClass.templateParams.count;
2188             }
2189             break;
2190          }
2191          id++;
2192       }
2193       if(curParam) break;
2194    }
2195    if(curParam)
2196    {
2197       arg = &_class.templateArgs[id];
2198       if(arg && param.type == type)
2199          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2200    }
2201    return arg;
2202 }
2203
2204 public Context SetupTemplatesContext(Class _class)
2205 {
2206    Context context = PushContext();
2207    context.templateTypesOnly = true;
2208    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2209    {
2210       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2211       for(; param; param = param.next)
2212       {
2213          if(param.type == type && param.identifier)
2214          {
2215             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2216             curContext.templateTypes.Add((BTNode)type);
2217          }
2218       }
2219    }
2220    else if(_class)
2221    {
2222       Class sClass;
2223       for(sClass = _class; sClass; sClass = sClass.base)
2224       {
2225          ClassTemplateParameter p;
2226          for(p = sClass.templateParams.first; p; p = p.next)
2227          {
2228             //OldList * specs = MkList();
2229             //Declarator decl = null;
2230             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2231             if(p.type == type)
2232             {
2233                TemplateParameter param = p.param;
2234                TemplatedType type;
2235                if(!param)
2236                {
2237                   // ADD DATA TYPE HERE...
2238                   p.param = param = TemplateParameter
2239                   {
2240                      identifier = MkIdentifier(p.name), type = p.type,
2241                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2242                   };
2243                }
2244                type = TemplatedType { key = (uintptr)p.name, param = param };
2245                curContext.templateTypes.Add((BTNode)type);
2246             }
2247          }
2248       }
2249    }
2250    return context;
2251 }
2252
2253 public void FinishTemplatesContext(Context context)
2254 {
2255    PopContext(context);
2256    FreeContext(context);
2257    delete context;
2258 }
2259
2260 public void ProcessMethodType(Method method)
2261 {
2262    if(!method.dataType)
2263    {
2264       Context context = SetupTemplatesContext(method._class);
2265
2266       method.dataType = ProcessTypeString(method.dataTypeString, false);
2267
2268       FinishTemplatesContext(context);
2269
2270       if(method.type != virtualMethod && method.dataType)
2271       {
2272          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2273          {
2274             if(!method._class.symbol)
2275                method._class.symbol = FindClass(method._class.fullName);
2276             method.dataType.thisClass = method._class.symbol;
2277          }
2278       }
2279
2280       // Why was this commented out? Working fine without now...
2281
2282       /*
2283       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2284          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2285          */
2286    }
2287
2288    /*
2289    if(type)
2290    {
2291       char * par = strstr(type, "(");
2292       char * classOp = null;
2293       int classOpLen = 0;
2294       if(par)
2295       {
2296          int c;
2297          for(c = par-type-1; c >= 0; c++)
2298          {
2299             if(type[c] == ':' && type[c+1] == ':')
2300             {
2301                classOp = type + c - 1;
2302                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2303                {
2304                   classOp--;
2305                   classOpLen++;
2306                }
2307                break;
2308             }
2309             else if(!isspace(type[c]))
2310                break;
2311          }
2312       }
2313       if(classOp)
2314       {
2315          char temp[1024];
2316          int typeLen = strlen(type);
2317          memcpy(temp, classOp, classOpLen);
2318          temp[classOpLen] = '\0';
2319          if(temp[0])
2320             _class = eSystem_FindClass(module, temp);
2321          else
2322             _class = null;
2323          method.dataTypeString = new char[typeLen - classOpLen + 1];
2324          memcpy(method.dataTypeString, type, classOp - type);
2325          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2326       }
2327       else
2328          method.dataTypeString = type;
2329    }
2330    */
2331 }
2332
2333
2334 public void ProcessPropertyType(Property prop)
2335 {
2336    if(!prop.dataType)
2337    {
2338       Context context = SetupTemplatesContext(prop._class);
2339       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2340       FinishTemplatesContext(context);
2341    }
2342 }
2343
2344 public void DeclareMethod(Method method, char * name)
2345 {
2346    Symbol symbol = method.symbol;
2347    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2348    {
2349       bool imported = false;
2350       bool dllImport = false;
2351
2352       if(!method.dataType)
2353          method.dataType = ProcessTypeString(method.dataTypeString, false);
2354
2355       if(!symbol || symbol._import || method.type == virtualMethod)
2356       {
2357          if(!symbol || method.type == virtualMethod)
2358          {
2359             Symbol classSym;
2360             if(!method._class.symbol)
2361                method._class.symbol = FindClass(method._class.fullName);
2362             classSym = method._class.symbol;
2363             if(!classSym._import)
2364             {
2365                ModuleImport module;
2366
2367                if(method._class.module && method._class.module.name)
2368                   module = FindModule(method._class.module);
2369                else
2370                   module = mainModule;
2371                classSym._import = ClassImport
2372                {
2373                   name = CopyString(method._class.fullName);
2374                   isRemote = method._class.isRemote;
2375                };
2376                module.classes.Add(classSym._import);
2377             }
2378             if(!symbol)
2379             {
2380                symbol = method.symbol = Symbol { };
2381             }
2382             if(!symbol._import)
2383             {
2384                symbol._import = (ClassImport)MethodImport
2385                {
2386                   name = CopyString(method.name);
2387                   isVirtual = method.type == virtualMethod;
2388                };
2389                classSym._import.methods.Add(symbol._import);
2390             }
2391             if(!symbol)
2392             {
2393                // Set the symbol type
2394                /*
2395                if(!type.thisClass)
2396                {
2397                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2398                }
2399                else if(type.thisClass == (void *)-1)
2400                {
2401                   type.thisClass = null;
2402                }
2403                */
2404                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2405                symbol.type = method.dataType;
2406                if(symbol.type) symbol.type.refCount++;
2407             }
2408             /*
2409             if(!method.thisClass || strcmp(method.thisClass, "void"))
2410                symbol.type.params.Insert(null,
2411                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2412             */
2413          }
2414          if(!method.dataType.dllExport)
2415          {
2416             imported = true;
2417             if(method._class.module != privateModule && method._class.module.importType != staticImport)
2418                dllImport = true;
2419          }
2420       }
2421
2422       /* MOVING THIS UP
2423       if(!method.dataType)
2424          method.dataType = ((Symbol)method.symbol).type;
2425          //ProcessMethodType(method);
2426       */
2427
2428       if(method.type != virtualMethod && method.dataType)
2429          DeclareType(method.dataType, true, true);
2430
2431       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2432       {
2433          // We need a declaration here :)
2434          Declaration decl;
2435          OldList * specifiers, * declarators;
2436          Declarator d;
2437          Declarator funcDecl;
2438          External external;
2439
2440          specifiers = MkList();
2441          declarators = MkList();
2442
2443          //if(imported)
2444          if(dllImport)
2445             ListAdd(specifiers, MkSpecifier(EXTERN));
2446          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2447             ListAdd(specifiers, MkSpecifier(STATIC));
2448
2449          if(method.type == virtualMethod)
2450          {
2451             ListAdd(specifiers, MkSpecifier(INT));
2452             d = MkDeclaratorIdentifier(MkIdentifier(name));
2453          }
2454          else
2455          {
2456             d = MkDeclaratorIdentifier(MkIdentifier(name));
2457             //if(imported)
2458             if(dllImport)
2459                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2460             {
2461                Context context = SetupTemplatesContext(method._class);
2462                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2463                FinishTemplatesContext(context);
2464             }
2465             funcDecl = GetFuncDecl(d);
2466
2467             if(dllImport)
2468             {
2469                Specifier spec, next;
2470                for(spec = specifiers->first; spec; spec = next)
2471                {
2472                   next = spec.next;
2473                   if(spec.type == extendedSpecifier)
2474                   {
2475                      specifiers->Remove(spec);
2476                      FreeSpecifier(spec);
2477                   }
2478                }
2479             }
2480
2481             // Add this parameter if not a static method
2482             if(method.dataType && !method.dataType.staticMethod)
2483             {
2484                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2485                {
2486                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2487                   TypeName thisParam = MkTypeName(MkListOne(
2488                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2489                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2490                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2491                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2492
2493                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2494                   {
2495                      TypeName param = funcDecl.function.parameters->first;
2496                      funcDecl.function.parameters->Remove(param);
2497                      FreeTypeName(param);
2498                   }
2499
2500                   if(!funcDecl.function.parameters)
2501                      funcDecl.function.parameters = MkList();
2502                   funcDecl.function.parameters->Insert(null, thisParam);
2503                }
2504             }
2505             // Make sure we don't have empty parameter declarations for static methods...
2506             /*
2507             else if(!funcDecl.function.parameters)
2508             {
2509                funcDecl.function.parameters = MkList();
2510                funcDecl.function.parameters->Insert(null,
2511                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2512             }*/
2513          }
2514          // TESTING THIS:
2515          ProcessDeclarator(d);
2516
2517          ListAdd(declarators, MkInitDeclarator(d, null));
2518
2519          decl = MkDeclaration(specifiers, declarators);
2520
2521          ReplaceThisClassSpecifiers(specifiers, method._class);
2522
2523          // Keep a different symbol for the function definition than the declaration...
2524          if(symbol.pointerExternal)
2525          {
2526             Symbol functionSymbol { };
2527
2528             // Copy symbol
2529             {
2530                *functionSymbol = *symbol;
2531                functionSymbol.string = CopyString(symbol.string);
2532                if(functionSymbol.type)
2533                   functionSymbol.type.refCount++;
2534             }
2535
2536             excludedSymbols->Add(functionSymbol);
2537             symbol.pointerExternal.symbol = functionSymbol;
2538          }
2539          external = MkExternalDeclaration(decl);
2540          if(curExternal)
2541             ast->Insert(curExternal ? curExternal.prev : null, external);
2542          external.symbol = symbol;
2543          symbol.pointerExternal = external;
2544       }
2545       else if(ast)
2546       {
2547          // Move declaration higher...
2548          ast->Move(symbol.pointerExternal, curExternal.prev);
2549       }
2550
2551       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2552    }
2553 }
2554
2555 char * ReplaceThisClass(Class _class)
2556 {
2557    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2558    {
2559       bool first = true;
2560       int p = 0;
2561       ClassTemplateParameter param;
2562       int lastParam = -1;
2563
2564       char className[1024];
2565       strcpy(className, _class.fullName);
2566       for(param = _class.templateParams.first; param; param = param.next)
2567       {
2568          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2569          {
2570             if(first) strcat(className, "<");
2571             if(!first) strcat(className, ", ");
2572             if(lastParam + 1 != p)
2573             {
2574                strcat(className, param.name);
2575                strcat(className, " = ");
2576             }
2577             strcat(className, param.name);
2578             first = false;
2579             lastParam = p;
2580          }
2581          p++;
2582       }
2583       if(!first)
2584       {
2585          int len = strlen(className);
2586          if(className[len-1] == '>') className[len++] = ' ';
2587          className[len++] = '>';
2588          className[len++] = '\0';
2589       }
2590       return CopyString(className);
2591    }
2592    else
2593       return CopyString(_class.fullName);
2594 }
2595
2596 Type ReplaceThisClassType(Class _class)
2597 {
2598    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2599    {
2600       bool first = true;
2601       int p = 0;
2602       ClassTemplateParameter param;
2603       int lastParam = -1;
2604       char className[1024];
2605       strcpy(className, _class.fullName);
2606
2607       for(param = _class.templateParams.first; param; param = param.next)
2608       {
2609          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2610          {
2611             if(first) strcat(className, "<");
2612             if(!first) strcat(className, ", ");
2613             if(lastParam + 1 != p)
2614             {
2615                strcat(className, param.name);
2616                strcat(className, " = ");
2617             }
2618             strcat(className, param.name);
2619             first = false;
2620             lastParam = p;
2621          }
2622          p++;
2623       }
2624       if(!first)
2625       {
2626          int len = strlen(className);
2627          if(className[len-1] == '>') className[len++] = ' ';
2628          className[len++] = '>';
2629          className[len++] = '\0';
2630       }
2631       return MkClassType(className);
2632       //return ProcessTypeString(className, false);
2633    }
2634    else
2635    {
2636       return MkClassType(_class.fullName);
2637       //return ProcessTypeString(_class.fullName, false);
2638    }
2639 }
2640
2641 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2642 {
2643    if(specs != null && _class)
2644    {
2645       Specifier spec;
2646       for(spec = specs.first; spec; spec = spec.next)
2647       {
2648          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2649          {
2650             spec.type = nameSpecifier;
2651             spec.name = ReplaceThisClass(_class);
2652             spec.symbol = FindClass(spec.name); //_class.symbol;
2653          }
2654       }
2655    }
2656 }
2657
2658 // Returns imported or not
2659 bool DeclareFunction(GlobalFunction function, char * name)
2660 {
2661    Symbol symbol = function.symbol;
2662    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2663    {
2664       bool imported = false;
2665       bool dllImport = false;
2666
2667       if(!function.dataType)
2668       {
2669          function.dataType = ProcessTypeString(function.dataTypeString, false);
2670          if(!function.dataType.thisClass)
2671             function.dataType.staticMethod = true;
2672       }
2673
2674       if(inCompiler)
2675       {
2676          if(!symbol)
2677          {
2678             ModuleImport module = FindModule(function.module);
2679             // WARNING: This is not added anywhere...
2680             symbol = function.symbol = Symbol {  };
2681
2682             if(module.name)
2683             {
2684                if(!function.dataType.dllExport)
2685                {
2686                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2687                   module.functions.Add(symbol._import);
2688                }
2689             }
2690             // Set the symbol type
2691             {
2692                symbol.type = ProcessTypeString(function.dataTypeString, false);
2693                if(!symbol.type.thisClass)
2694                   symbol.type.staticMethod = true;
2695             }
2696          }
2697          imported = symbol._import ? true : false;
2698          if(imported && function.module != privateModule && function.module.importType != staticImport)
2699             dllImport = true;
2700       }
2701
2702       DeclareType(function.dataType, true, true);
2703
2704       if(inCompiler)
2705       {
2706          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2707          {
2708             // We need a declaration here :)
2709             Declaration decl;
2710             OldList * specifiers, * declarators;
2711             Declarator d;
2712             Declarator funcDecl;
2713             External external;
2714
2715             specifiers = MkList();
2716             declarators = MkList();
2717
2718             //if(imported)
2719                ListAdd(specifiers, MkSpecifier(EXTERN));
2720             /*
2721             else
2722                ListAdd(specifiers, MkSpecifier(STATIC));
2723             */
2724
2725             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2726             //if(imported)
2727             if(dllImport)
2728                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2729
2730             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2731             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2732             if(function.module.importType == staticImport)
2733             {
2734                Specifier spec;
2735                for(spec = specifiers->first; spec; spec = spec.next)
2736                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2737                   {
2738                      specifiers->Remove(spec);
2739                      FreeSpecifier(spec);
2740                      break;
2741                   }
2742             }
2743
2744             funcDecl = GetFuncDecl(d);
2745
2746             // Make sure we don't have empty parameter declarations for static methods...
2747             if(funcDecl && !funcDecl.function.parameters)
2748             {
2749                funcDecl.function.parameters = MkList();
2750                funcDecl.function.parameters->Insert(null,
2751                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2752             }
2753
2754             ListAdd(declarators, MkInitDeclarator(d, null));
2755
2756             {
2757                Context oldCtx = curContext;
2758                curContext = globalContext;
2759                decl = MkDeclaration(specifiers, declarators);
2760                curContext = oldCtx;
2761             }
2762
2763             // Keep a different symbol for the function definition than the declaration...
2764             if(symbol.pointerExternal)
2765             {
2766                Symbol functionSymbol { };
2767                // Copy symbol
2768                {
2769                   *functionSymbol = *symbol;
2770                   functionSymbol.string = CopyString(symbol.string);
2771                   if(functionSymbol.type)
2772                      functionSymbol.type.refCount++;
2773                }
2774
2775                excludedSymbols->Add(functionSymbol);
2776
2777                symbol.pointerExternal.symbol = functionSymbol;
2778             }
2779             external = MkExternalDeclaration(decl);
2780             if(curExternal)
2781                ast->Insert(curExternal.prev, external);
2782             external.symbol = symbol;
2783             symbol.pointerExternal = external;
2784          }
2785          else
2786          {
2787             // Move declaration higher...
2788             ast->Move(symbol.pointerExternal, curExternal.prev);
2789          }
2790
2791          if(curExternal)
2792             symbol.id = curExternal.symbol.idCode;
2793       }
2794    }
2795    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2796 }
2797
2798 void DeclareGlobalData(GlobalData data)
2799 {
2800    Symbol symbol = data.symbol;
2801    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2802    {
2803       if(inCompiler)
2804       {
2805          if(!symbol)
2806             symbol = data.symbol = Symbol { };
2807       }
2808       if(!data.dataType)
2809          data.dataType = ProcessTypeString(data.dataTypeString, false);
2810       DeclareType(data.dataType, true, true);
2811       if(inCompiler)
2812       {
2813          if(!symbol.pointerExternal)
2814          {
2815             // We need a declaration here :)
2816             Declaration decl;
2817             OldList * specifiers, * declarators;
2818             Declarator d;
2819             External external;
2820
2821             specifiers = MkList();
2822             declarators = MkList();
2823
2824             ListAdd(specifiers, MkSpecifier(EXTERN));
2825             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2826             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2827
2828             ListAdd(declarators, MkInitDeclarator(d, null));
2829
2830             decl = MkDeclaration(specifiers, declarators);
2831             external = MkExternalDeclaration(decl);
2832             if(curExternal)
2833                ast->Insert(curExternal.prev, external);
2834             external.symbol = symbol;
2835             symbol.pointerExternal = external;
2836          }
2837          else
2838          {
2839             // Move declaration higher...
2840             ast->Move(symbol.pointerExternal, curExternal.prev);
2841          }
2842
2843          if(curExternal)
2844             symbol.id = curExternal.symbol.idCode;
2845       }
2846    }
2847 }
2848
2849 class Conversion : struct
2850 {
2851    Conversion prev, next;
2852    Property convert;
2853    bool isGet;
2854    Type resultType;
2855 };
2856
2857 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2858 {
2859    if(source && dest)
2860    {
2861       // Property convert;
2862
2863       if(source.kind == templateType && dest.kind != templateType)
2864       {
2865          Type type = ProcessTemplateParameterType(source.templateParameter);
2866          if(type) source = type;
2867       }
2868
2869       if(dest.kind == templateType && source.kind != templateType)
2870       {
2871          Type type = ProcessTemplateParameterType(dest.templateParameter);
2872          if(type) dest = type;
2873       }
2874
2875       if(dest.classObjectType == typedObject)
2876       {
2877          if(source.classObjectType != anyObject)
2878             return true;
2879          else
2880          {
2881             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2882             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2883             {
2884                return true;
2885             }
2886          }
2887       }
2888       else
2889       {
2890          if(source.classObjectType == anyObject)
2891             return true;
2892          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2893             return true;
2894       }
2895
2896       if((dest.kind == structType && source.kind == structType) ||
2897          (dest.kind == unionType && source.kind == unionType))
2898       {
2899          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2900              (source.members.first && source.members.first == dest.members.first))
2901             return true;
2902       }
2903
2904       if(dest.kind == ellipsisType && source.kind != voidType)
2905          return true;
2906
2907       if(dest.kind == pointerType && dest.type.kind == voidType &&
2908          ((source.kind == classType && (!source._class || !source._class.registered || source._class.registered.type == structClass || source._class.registered.type == normalClass || source._class.registered.type == noHeadClass || source._class.registered.type == systemClass))
2909          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2910
2911          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2912
2913          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2914          return true;
2915       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2916          ((dest.kind == classType && (!dest._class || !dest._class.registered || dest._class.registered.type == structClass || dest._class.registered.type == normalClass || dest._class.registered.type == noHeadClass || dest._class.registered.type == systemClass))
2917          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2918
2919          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2920
2921          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2922          return true;
2923
2924       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2925       {
2926          if(source._class.registered && source._class.registered.type == unitClass)
2927          {
2928             if(conversions != null)
2929             {
2930                if(source._class.registered == dest._class.registered)
2931                   return true;
2932             }
2933             else
2934             {
2935                Class sourceBase, destBase;
2936                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2937                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2938                if(sourceBase == destBase)
2939                   return true;
2940             }
2941          }
2942          // Don't match enum inheriting from other enum if resolving enumeration values
2943          // TESTING: !dest.classObjectType
2944          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2945             (enumBaseType ||
2946                (!source._class.registered || source._class.registered.type != enumClass) ||
2947                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2948             return true;
2949          else
2950          {
2951             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2952             if(enumBaseType &&
2953                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2954                ((source._class && source._class.registered && source._class.registered.type != enumClass) || source.kind == classType)) // Added this here for a base enum to be acceptable for a derived enum (#139)
2955             {
2956                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2957                {
2958                   return true;
2959                }
2960             }
2961          }
2962       }
2963
2964       // JUST ADDED THIS...
2965       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
2966          return true;
2967
2968       if(doConversion)
2969       {
2970          // Just added this for Straight conversion of ColorAlpha => Color
2971          if(source.kind == classType)
2972          {
2973             Class _class;
2974             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
2975             {
2976                Property convert;
2977                for(convert = _class.conversions.first; convert; convert = convert.next)
2978                {
2979                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
2980                   {
2981                      Conversion after = (conversions != null) ? conversions.last : null;
2982
2983                      if(!convert.dataType)
2984                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
2985                      if(MatchTypes(convert.dataType, dest, conversions, null, null, false, true, false, true))
2986                      {
2987                         if(!conversions && !convert.Get)
2988                            return true;
2989                         else if(conversions != null)
2990                         {
2991                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
2992                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
2993                               (dest.kind != classType || dest._class.registered != _class.base))
2994                               return true;
2995                            else
2996                            {
2997                               Conversion conv { convert = convert, isGet = true };
2998                               // conversions.Add(conv);
2999                               conversions.Insert(after, conv);
3000                               return true;
3001                            }
3002                         }
3003                      }
3004                   }
3005                }
3006             }
3007          }
3008
3009          // MOVING THIS??
3010
3011          if(dest.kind == classType)
3012          {
3013             Class _class;
3014             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3015             {
3016                Property convert;
3017                for(convert = _class.conversions.first; convert; convert = convert.next)
3018                {
3019                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3020                   {
3021                      // Conversion after = (conversions != null) ? conversions.last : null;
3022
3023                      if(!convert.dataType)
3024                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3025                      // Just added this equality check to prevent recursion.... Make it safer?
3026                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3027                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3028                      {
3029                         if(!conversions && !convert.Set)
3030                            return true;
3031                         else if(conversions != null)
3032                         {
3033                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3034                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3035                               (source.kind != classType || source._class.registered != _class.base))
3036                               return true;
3037                            else
3038                            {
3039                               // *** Testing this! ***
3040                               Conversion conv { convert = convert };
3041                               conversions.Add(conv);
3042                               //conversions.Insert(after, conv);
3043                               return true;
3044                            }
3045                         }
3046                      }
3047                   }
3048                }
3049             }
3050             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3051             {
3052                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3053                   (source.kind != classType || source._class.registered.type != structClass))
3054                   return true;
3055             }*/
3056
3057             // TESTING THIS... IS THIS OK??
3058             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3059             {
3060                if(!dest._class.registered.dataType)
3061                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3062                // Only support this for classes...
3063                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3064                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3065                {
3066                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3067                   {
3068                      return true;
3069                   }
3070                }
3071             }
3072          }
3073
3074          // Moved this lower
3075          if(source.kind == classType)
3076          {
3077             Class _class;
3078             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3079             {
3080                Property convert;
3081                for(convert = _class.conversions.first; convert; convert = convert.next)
3082                {
3083                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3084                   {
3085                      Conversion after = (conversions != null) ? conversions.last : null;
3086
3087                      if(!convert.dataType)
3088                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3089                      if(convert.dataType != source && MatchTypes(convert.dataType, dest, conversions, null, null, true, true, false, true))
3090                      {
3091                         if(!conversions && !convert.Get)
3092                            return true;
3093                         else if(conversions != null)
3094                         {
3095                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3096                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3097                               (dest.kind != classType || dest._class.registered != _class.base))
3098                               return true;
3099                            else
3100                            {
3101                               Conversion conv { convert = convert, isGet = true };
3102
3103                               // conversions.Add(conv);
3104                               conversions.Insert(after, conv);
3105                               return true;
3106                            }
3107                         }
3108                      }
3109                   }
3110                }
3111             }
3112
3113             // TESTING THIS... IS THIS OK??
3114             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3115             {
3116                if(!source._class.registered.dataType)
3117                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3118                if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, true, false, false))
3119                {
3120                   return true;
3121                }
3122             }
3123          }
3124       }
3125
3126       if(source.kind == classType || source.kind == subClassType)
3127          ;
3128       else if(dest.kind == source.kind &&
3129          (dest.kind != structType && dest.kind != unionType &&
3130           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3131           return true;
3132       // RECENTLY ADDED THESE
3133       else if(dest.kind == doubleType && source.kind == floatType)
3134          return true;
3135       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3136          return true;
3137       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3138          return true;
3139       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3140          return true;
3141       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3142          return true;
3143       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3144          return true;
3145       else if(source.kind == enumType &&
3146          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3147           return true;
3148       else if(dest.kind == enumType &&
3149          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3150           return true;
3151       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3152               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3153       {
3154          Type paramSource, paramDest;
3155
3156          if(dest.kind == methodType)
3157             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3158          if(source.kind == methodType)
3159             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3160
3161          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3162          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3163          if(dest.kind == methodType)
3164             dest = dest.method.dataType;
3165          if(source.kind == methodType)
3166             source = source.method.dataType;
3167
3168          paramSource = source.params.first;
3169          if(paramSource && paramSource.kind == voidType) paramSource = null;
3170          paramDest = dest.params.first;
3171          if(paramDest && paramDest.kind == voidType) paramDest = null;
3172
3173
3174          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3175             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3176          {
3177             // Source thisClass must be derived from destination thisClass
3178             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3179                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3180             {
3181                if(paramDest && paramDest.kind == classType)
3182                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3183                else
3184                   Compiler_Error($"method class should not take an object\n");
3185                return false;
3186             }
3187             paramDest = paramDest.next;
3188          }
3189          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3190          {
3191             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3192             {
3193                if(dest.thisClass)
3194                {
3195                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3196                   {
3197                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3198                      return false;
3199                   }
3200                }
3201                else
3202                {
3203                   // THIS WAS BACKWARDS:
3204                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3205                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3206                   {
3207                      if(owningClassDest)
3208                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3209                      else
3210                         Compiler_Error($"overriding class expected to be derived from method class\n");
3211                      return false;
3212                   }
3213                }
3214                paramSource = paramSource.next;
3215             }
3216             else
3217             {
3218                if(dest.thisClass)
3219                {
3220                   // Source thisClass must be derived from destination thisClass
3221                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3222                   {
3223                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3224                      return false;
3225                   }
3226                }
3227                else
3228                {
3229                   // THIS WAS BACKWARDS TOO??
3230                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3231                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3232                   {
3233                      //if(owningClass)
3234                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3235                      //else
3236                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3237                      return false;
3238                   }
3239                }
3240             }
3241          }
3242
3243
3244          // Source return type must be derived from destination return type
3245          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3246          {
3247             Compiler_Warning($"incompatible return type for function\n");
3248             return false;
3249          }
3250
3251          // Check parameters
3252
3253          for(; paramDest; paramDest = paramDest.next)
3254          {
3255             if(!paramSource)
3256             {
3257                //Compiler_Warning($"not enough parameters\n");
3258                Compiler_Error($"not enough parameters\n");
3259                return false;
3260             }
3261             {
3262                Type paramDestType = paramDest;
3263                Type paramSourceType = paramSource;
3264                Type type = paramDestType;
3265
3266                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3267                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3268                   paramSource.kind != templateType)
3269                {
3270                   int id = 0;
3271                   ClassTemplateParameter curParam = null;
3272                   Class sClass;
3273                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3274                   {
3275                      id = 0;
3276                      if(sClass.templateClass) sClass = sClass.templateClass;
3277                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3278                      {
3279                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3280                         {
3281                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3282                            {
3283                               if(sClass.templateClass) sClass = sClass.templateClass;
3284                               id += sClass.templateParams.count;
3285                            }
3286                            break;
3287                         }
3288                         id++;
3289                      }
3290                      if(curParam) break;
3291                   }
3292
3293                   if(curParam)
3294                   {
3295                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3296                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3297                   }
3298                }
3299
3300                // paramDest must be derived from paramSource
3301                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) &&
3302                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3303                {
3304                   char type[1024];
3305                   type[0] = 0;
3306                   PrintType(paramDest, type, false, true);
3307                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3308
3309                   if(paramDestType != paramDest)
3310                      FreeType(paramDestType);
3311                   return false;
3312                }
3313                if(paramDestType != paramDest)
3314                   FreeType(paramDestType);
3315             }
3316
3317             paramSource = paramSource.next;
3318          }
3319          if(paramSource)
3320          {
3321             Compiler_Error($"too many parameters\n");
3322             return false;
3323          }
3324          return true;
3325       }
3326       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3327       {
3328          return true;
3329       }
3330       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3331          (source.kind == arrayType || source.kind == pointerType))
3332       {
3333          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3334             return true;
3335       }
3336    }
3337    return false;
3338 }
3339
3340 static void FreeConvert(Conversion convert)
3341 {
3342    if(convert.resultType)
3343       FreeType(convert.resultType);
3344 }
3345
3346 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3347                               char * string, OldList conversions)
3348 {
3349    BTNamedLink link;
3350
3351    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3352    {
3353       Class _class = link.data;
3354       if(_class.type == enumClass)
3355       {
3356          OldList converts { };
3357          Type type { };
3358          type.kind = classType;
3359
3360          if(!_class.symbol)
3361             _class.symbol = FindClass(_class.fullName);
3362          type._class = _class.symbol;
3363
3364          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3365          {
3366             NamedLink value;
3367             Class enumClass = eSystem_FindClass(privateModule, "enum");
3368             if(enumClass)
3369             {
3370                Class baseClass;
3371                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3372                {
3373                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3374                   for(value = e.values.first; value; value = value.next)
3375                   {
3376                      if(!strcmp(value.name, string))
3377                         break;
3378                   }
3379                   if(value)
3380                   {
3381                      FreeExpContents(sourceExp);
3382                      FreeType(sourceExp.expType);
3383
3384                      sourceExp.isConstant = true;
3385                      sourceExp.expType = MkClassType(baseClass.fullName);
3386                      //if(inCompiler)
3387                      {
3388                         char constant[256];
3389                         sourceExp.type = constantExp;
3390                         if(!strcmp(baseClass.dataTypeString, "int"))
3391                            sprintf(constant, "%d",(int)value.data);
3392                         else
3393                            sprintf(constant, "0x%X",(int)value.data);
3394                         sourceExp.constant = CopyString(constant);
3395                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3396                      }
3397
3398                      while(converts.first)
3399                      {
3400                         Conversion convert = converts.first;
3401                         converts.Remove(convert);
3402                         conversions.Add(convert);
3403                      }
3404                      delete type;
3405                      return true;
3406                   }
3407                }
3408             }
3409          }
3410          if(converts.first)
3411             converts.Free(FreeConvert);
3412          delete type;
3413       }
3414    }
3415    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3416       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3417          return true;
3418    return false;
3419 }
3420
3421 public bool ModuleVisibility(Module searchIn, Module searchFor)
3422 {
3423    SubModule subModule;
3424
3425    if(searchFor == searchIn)
3426       return true;
3427
3428    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3429    {
3430       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3431       {
3432          if(ModuleVisibility(subModule.module, searchFor))
3433             return true;
3434       }
3435    }
3436    return false;
3437 }
3438
3439 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3440 {
3441    Module module;
3442
3443    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3444       return true;
3445    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3446       return true;
3447    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3448       return true;
3449
3450    for(module = mainModule.application.allModules.first; module; module = module.next)
3451    {
3452       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3453          return true;
3454    }
3455    return false;
3456 }
3457
3458 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3459 {
3460    Type source = sourceExp.expType;
3461    Type realDest = dest;
3462    Type backupSourceExpType = null;
3463
3464    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3465       return true;
3466
3467    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3468    {
3469        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3470        {
3471           Class sourceBase, destBase;
3472           for(sourceBase = source._class.registered;
3473               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3474               sourceBase = sourceBase.base);
3475           for(destBase = dest._class.registered;
3476               destBase && destBase.base && destBase.base.type != systemClass;
3477               destBase = destBase.base);
3478           //if(source._class.registered == dest._class.registered)
3479           if(sourceBase == destBase)
3480              return true;
3481        }
3482    }
3483
3484    if(source)
3485    {
3486       OldList * specs;
3487       bool flag = false;
3488       int64 value = MAXINT;
3489
3490       source.refCount++;
3491       dest.refCount++;
3492
3493       if(sourceExp.type == constantExp)
3494       {
3495          if(source.isSigned)
3496             value = strtoll(sourceExp.constant, null, 0);
3497          else
3498             value = strtoull(sourceExp.constant, null, 0);
3499       }
3500       else if(sourceExp.type == opExp && sourceExp.op.op == '-' && !sourceExp.op.exp1 && sourceExp.op.exp2 && sourceExp.op.exp2.type == constantExp)
3501       {
3502          if(source.isSigned)
3503             value = -strtoll(sourceExp.op.exp2.constant, null, 0);
3504          else
3505             value = -strtoull(sourceExp.op.exp2.constant, null, 0);
3506       }
3507
3508       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3509          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3510       {
3511          FreeType(source);
3512          source = Type { kind = intType, isSigned = false, refCount = 1 };
3513       }
3514
3515       if(dest.kind == classType)
3516       {
3517          Class _class = dest._class ? dest._class.registered : null;
3518
3519          if(_class && _class.type == unitClass)
3520          {
3521             if(source.kind != classType)
3522             {
3523                Type tempType { };
3524                Type tempDest, tempSource;
3525
3526                for(; _class.base.type != systemClass; _class = _class.base);
3527                tempSource = dest;
3528                tempDest = tempType;
3529
3530                tempType.kind = classType;
3531                if(!_class.symbol)
3532                   _class.symbol = FindClass(_class.fullName);
3533
3534                tempType._class = _class.symbol;
3535                tempType.truth = dest.truth;
3536                if(tempType._class)
3537                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3538
3539                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3540                backupSourceExpType = sourceExp.expType;
3541                sourceExp.expType = dest; dest.refCount++;
3542                //sourceExp.expType = MkClassType(_class.fullName);
3543                flag = true;
3544
3545                delete tempType;
3546             }
3547          }
3548
3549
3550          // Why wasn't there something like this?
3551          if(_class && _class.type == bitClass && source.kind != classType)
3552          {
3553             if(!dest._class.registered.dataType)
3554                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3555             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3556             {
3557                FreeType(source);
3558                FreeType(sourceExp.expType);
3559                source = sourceExp.expType = MkClassType(dest._class.string);
3560                source.refCount++;
3561
3562                //source.kind = classType;
3563                //source._class = dest._class;
3564             }
3565          }
3566
3567          // Adding two enumerations
3568          /*
3569          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3570          {
3571             if(!source._class.registered.dataType)
3572                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3573             if(!dest._class.registered.dataType)
3574                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3575
3576             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3577             {
3578                FreeType(source);
3579                source = sourceExp.expType = MkClassType(dest._class.string);
3580                source.refCount++;
3581
3582                //source.kind = classType;
3583                //source._class = dest._class;
3584             }
3585          }*/
3586
3587          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3588          {
3589             OldList * specs = MkList();
3590             Declarator decl;
3591             char string[1024];
3592
3593             ReadString(string, sourceExp.string);
3594             decl = SpecDeclFromString(string, specs, null);
3595
3596             FreeExpContents(sourceExp);
3597             FreeType(sourceExp.expType);
3598
3599             sourceExp.type = classExp;
3600             sourceExp._classExp.specifiers = specs;
3601             sourceExp._classExp.decl = decl;
3602             sourceExp.expType = dest;
3603             dest.refCount++;
3604
3605             FreeType(source);
3606             FreeType(dest);
3607             if(backupSourceExpType) FreeType(backupSourceExpType);
3608             return true;
3609          }
3610       }
3611       else if(source.kind == classType)
3612       {
3613          Class _class = source._class ? source._class.registered : null;
3614
3615          if(_class && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3616          {
3617             /*
3618             if(dest.kind != classType)
3619             {
3620                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3621                if(!source._class.registered.dataType)
3622                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3623
3624                FreeType(dest);
3625                dest = MkClassType(source._class.string);
3626                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3627                //   dest = MkClassType(source._class.string);
3628             }
3629             */
3630
3631             if(dest.kind != classType)
3632             {
3633                Type tempType { };
3634                Type tempDest, tempSource;
3635
3636                if(!source._class.registered.dataType)
3637                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3638
3639                for(; _class.base.type != systemClass; _class = _class.base);
3640                tempDest = source;
3641                tempSource = tempType;
3642                tempType.kind = classType;
3643                tempType._class = FindClass(_class.fullName);
3644                tempType.truth = source.truth;
3645                tempType.classObjectType = source.classObjectType;
3646
3647                if(tempType._class)
3648                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3649
3650                // PUT THIS BACK TESTING UNITS?
3651                if(conversions.last)
3652                {
3653                   ((Conversion)(conversions.last)).resultType = dest;
3654                   dest.refCount++;
3655                }
3656
3657                FreeType(sourceExp.expType);
3658                sourceExp.expType = MkClassType(_class.fullName);
3659                sourceExp.expType.truth = source.truth;
3660                sourceExp.expType.classObjectType = source.classObjectType;
3661
3662                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3663
3664                if(!sourceExp.destType)
3665                {
3666                   FreeType(sourceExp.destType);
3667                   sourceExp.destType = sourceExp.expType;
3668                   if(sourceExp.expType)
3669                      sourceExp.expType.refCount++;
3670                }
3671                //flag = true;
3672                //source = _class.dataType;
3673
3674
3675                // TOCHECK: TESTING THIS NEW CODE
3676                if(!_class.dataType)
3677                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3678                FreeType(dest);
3679                dest = MkClassType(source._class.string);
3680                dest.truth = source.truth;
3681                dest.classObjectType = source.classObjectType;
3682
3683                FreeType(source);
3684                source = _class.dataType;
3685                source.refCount++;
3686
3687                delete tempType;
3688             }
3689          }
3690       }
3691
3692       if(!flag)
3693       {
3694          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3695          {
3696             FreeType(source);
3697             FreeType(dest);
3698             return true;
3699          }
3700       }
3701
3702       // Implicit Casts
3703       /*
3704       if(source.kind == classType)
3705       {
3706          Class _class = source._class.registered;
3707          if(_class.type == unitClass)
3708          {
3709             if(!_class.dataType)
3710                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3711             source = _class.dataType;
3712          }
3713       }*/
3714
3715       if(dest.kind == classType)
3716       {
3717          Class _class = dest._class ? dest._class.registered : null;
3718          if(_class && !dest.truth && (_class.type == unitClass || !strcmp(_class.fullName, "bool") ||
3719             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3720          {
3721             if(_class.type == normalClass || _class.type == noHeadClass)
3722             {
3723                Expression newExp { };
3724                *newExp = *sourceExp;
3725                if(sourceExp.destType) sourceExp.destType.refCount++;
3726                if(sourceExp.expType)  sourceExp.expType.refCount++;
3727                sourceExp.type = castExp;
3728                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3729                sourceExp.cast.exp = newExp;
3730                FreeType(sourceExp.expType);
3731                sourceExp.expType = null;
3732                ProcessExpressionType(sourceExp);
3733
3734                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3735                if(!inCompiler)
3736                {
3737                   FreeType(sourceExp.expType);
3738                   sourceExp.expType = dest;
3739                }
3740
3741                FreeType(source);
3742                if(inCompiler) FreeType(dest);
3743
3744                if(backupSourceExpType) FreeType(backupSourceExpType);
3745                return true;
3746             }
3747
3748             if(!_class.dataType)
3749                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3750             FreeType(dest);
3751             dest = _class.dataType;
3752             dest.refCount++;
3753          }
3754
3755          // Accept lower precision types for units, since we want to keep the unit type
3756          if(dest.kind == doubleType &&
3757             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3758              source.kind == charType || source.kind == _BoolType))
3759          {
3760             specs = MkListOne(MkSpecifier(DOUBLE));
3761          }
3762          else if(dest.kind == floatType &&
3763             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3764             source.kind == _BoolType || source.kind == doubleType))
3765          {
3766             specs = MkListOne(MkSpecifier(FLOAT));
3767          }
3768          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3769             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3770          {
3771             specs = MkList();
3772             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3773             ListAdd(specs, MkSpecifier(INT64));
3774          }
3775          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3776             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3777          {
3778             specs = MkList();
3779             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3780             ListAdd(specs, MkSpecifier(INT));
3781          }
3782          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3783             source.kind == floatType || source.kind == doubleType))
3784          {
3785             specs = MkList();
3786             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3787             ListAdd(specs, MkSpecifier(SHORT));
3788          }
3789          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3790             source.kind == floatType || source.kind == doubleType))
3791          {
3792             specs = MkList();
3793             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3794             ListAdd(specs, MkSpecifier(CHAR));
3795          }
3796          else
3797          {
3798             FreeType(source);
3799             FreeType(dest);
3800             if(backupSourceExpType)
3801             {
3802                // Failed to convert: revert previous exp type
3803                if(sourceExp.expType) FreeType(sourceExp.expType);
3804                sourceExp.expType = backupSourceExpType;
3805             }
3806             return false;
3807          }
3808       }
3809       else if(dest.kind == doubleType &&
3810          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3811           source.kind == _BoolType || source.kind == charType))
3812       {
3813          specs = MkListOne(MkSpecifier(DOUBLE));
3814       }
3815       else if(dest.kind == floatType &&
3816          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3817       {
3818          specs = MkListOne(MkSpecifier(FLOAT));
3819       }
3820       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3821          (value == 1 || value == 0))
3822       {
3823          specs = MkList();
3824          ListAdd(specs, MkSpecifier(BOOL));
3825       }
3826       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3827          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3828       {
3829          specs = MkList();
3830          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3831          ListAdd(specs, MkSpecifier(CHAR));
3832       }
3833       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3834          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3835       {
3836          specs = MkList();
3837          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3838          ListAdd(specs, MkSpecifier(SHORT));
3839       }
3840       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3841       {
3842          specs = MkList();
3843          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3844          ListAdd(specs, MkSpecifier(INT));
3845       }
3846       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3847       {
3848          specs = MkList();
3849          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3850          ListAdd(specs, MkSpecifier(INT64));
3851       }
3852       else if(dest.kind == enumType &&
3853          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3854       {
3855          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3856       }
3857       else
3858       {
3859          FreeType(source);
3860          FreeType(dest);
3861          if(backupSourceExpType)
3862          {
3863             // Failed to convert: revert previous exp type
3864             if(sourceExp.expType) FreeType(sourceExp.expType);
3865             sourceExp.expType = backupSourceExpType;
3866          }
3867          return false;
3868       }
3869
3870       if(!flag)
3871       {
3872          Expression newExp { };
3873          *newExp = *sourceExp;
3874          newExp.prev = null;
3875          newExp.next = null;
3876          if(sourceExp.destType) sourceExp.destType.refCount++;
3877          if(sourceExp.expType)  sourceExp.expType.refCount++;
3878
3879          sourceExp.type = castExp;
3880          if(realDest.kind == classType)
3881          {
3882             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3883             FreeList(specs, FreeSpecifier);
3884          }
3885          else
3886             sourceExp.cast.typeName = MkTypeName(specs, null);
3887          if(newExp.type == opExp)
3888          {
3889             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3890          }
3891          else
3892             sourceExp.cast.exp = newExp;
3893
3894          FreeType(sourceExp.expType);
3895          sourceExp.expType = null;
3896          ProcessExpressionType(sourceExp);
3897       }
3898       else
3899          FreeList(specs, FreeSpecifier);
3900
3901       FreeType(dest);
3902       FreeType(source);
3903       if(backupSourceExpType) FreeType(backupSourceExpType);
3904
3905       return true;
3906    }
3907    else
3908    {
3909       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
3910       if(sourceExp.type == identifierExp)
3911       {
3912          Identifier id = sourceExp.identifier;
3913          if(dest.kind == classType)
3914          {
3915             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3916             {
3917                Class _class = dest._class.registered;
3918                Class enumClass = eSystem_FindClass(privateModule, "enum");
3919                if(enumClass)
3920                {
3921                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
3922                   {
3923                      NamedLink value;
3924                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
3925                      for(value = e.values.first; value; value = value.next)
3926                      {
3927                         if(!strcmp(value.name, id.string))
3928                            break;
3929                      }
3930                      if(value)
3931                      {
3932                         FreeExpContents(sourceExp);
3933                         FreeType(sourceExp.expType);
3934
3935                         sourceExp.isConstant = true;
3936                         sourceExp.expType = MkClassType(_class.fullName);
3937                         //if(inCompiler)
3938                         {
3939                            char constant[256];
3940                            sourceExp.type = constantExp;
3941                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
3942                               sprintf(constant, "%d", (int) value.data);
3943                            else
3944                               sprintf(constant, "0x%X", (int) value.data);
3945                            sourceExp.constant = CopyString(constant);
3946                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
3947                         }
3948                         return true;
3949                      }
3950                   }
3951                }
3952             }
3953          }
3954
3955          // Loop through all enum classes
3956          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
3957             return true;
3958       }
3959    }
3960    return false;
3961 }
3962
3963 #define TERTIARY(o, name, m, t, p) \
3964    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
3965    {                                                              \
3966       exp.type = constantExp;                                    \
3967       exp.string = p(op1.m ? op2.m : op3.m);                     \
3968       if(!exp.expType) \
3969          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3970       return true;                                                \
3971    }
3972
3973 #define BINARY(o, name, m, t, p) \
3974    static bool name(Expression exp, Operand op1, Operand op2)   \
3975    {                                                              \
3976       t value2 = op2.m;                                           \
3977       exp.type = constantExp;                                    \
3978       exp.string = p(op1.m o value2);                     \
3979       if(!exp.expType) \
3980          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3981       return true;                                                \
3982    }
3983
3984 #define BINARY_DIVIDE(o, name, m, t, p) \
3985    static bool name(Expression exp, Operand op1, Operand op2)   \
3986    {                                                              \
3987       t value2 = op2.m;                                           \
3988       exp.type = constantExp;                                    \
3989       exp.string = p(value2 ? (op1.m o value2) : 0);             \
3990       if(!exp.expType) \
3991          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
3992       return true;                                                \
3993    }
3994
3995 #define UNARY(o, name, m, t, p) \
3996    static bool name(Expression exp, Operand op1)                \
3997    {                                                              \
3998       exp.type = constantExp;                                    \
3999       exp.string = p((t)(o op1.m));                                   \
4000       if(!exp.expType) \
4001          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4002       return true;                                                \
4003    }
4004
4005 #define OPERATOR_ALL(macro, o, name) \
4006    macro(o, Int##name, i, int, PrintInt) \
4007    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4008    macro(o, Int64##name, i64, int64, PrintInt64) \
4009    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4010    macro(o, Short##name, s, short, PrintShort) \
4011    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4012    macro(o, Char##name, c, char, PrintChar) \
4013    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4014    macro(o, Float##name, f, float, PrintFloat) \
4015    macro(o, Double##name, d, double, PrintDouble)
4016
4017 #define OPERATOR_INTTYPES(macro, o, name) \
4018    macro(o, Int##name, i, int, PrintInt) \
4019    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4020    macro(o, Int64##name, i64, int64, PrintInt64) \
4021    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4022    macro(o, Short##name, s, short, PrintShort) \
4023    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4024    macro(o, Char##name, c, char, PrintChar) \
4025    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4026
4027
4028 // binary arithmetic
4029 OPERATOR_ALL(BINARY, +, Add)
4030 OPERATOR_ALL(BINARY, -, Sub)
4031 OPERATOR_ALL(BINARY, *, Mul)
4032 OPERATOR_ALL(BINARY_DIVIDE, /, Div)
4033 OPERATOR_INTTYPES(BINARY_DIVIDE, %, Mod)
4034
4035 // unary arithmetic
4036 OPERATOR_ALL(UNARY, -, Neg)
4037
4038 // unary arithmetic increment and decrement
4039 OPERATOR_ALL(UNARY, ++, Inc)
4040 OPERATOR_ALL(UNARY, --, Dec)
4041
4042 // binary arithmetic assignment
4043 OPERATOR_ALL(BINARY, =, Asign)
4044 OPERATOR_ALL(BINARY, +=, AddAsign)
4045 OPERATOR_ALL(BINARY, -=, SubAsign)
4046 OPERATOR_ALL(BINARY, *=, MulAsign)
4047 OPERATOR_ALL(BINARY_DIVIDE, /=, DivAsign)
4048 OPERATOR_INTTYPES(BINARY_DIVIDE, %=, ModAsign)
4049
4050 // binary bitwise
4051 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4052 OPERATOR_INTTYPES(BINARY, |, BitOr)
4053 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4054 OPERATOR_INTTYPES(BINARY, <<, LShift)
4055 OPERATOR_INTTYPES(BINARY, >>, RShift)
4056
4057 // unary bitwise
4058 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4059
4060 // binary bitwise assignment
4061 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4062 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4063 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4064 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4065 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4066
4067 // unary logical negation
4068 OPERATOR_INTTYPES(UNARY, !, Not)
4069
4070 // binary logical equality
4071 OPERATOR_ALL(BINARY, ==, Equ)
4072 OPERATOR_ALL(BINARY, !=, Nqu)
4073
4074 // binary logical
4075 OPERATOR_ALL(BINARY, &&, And)
4076 OPERATOR_ALL(BINARY, ||, Or)
4077
4078 // binary logical relational
4079 OPERATOR_ALL(BINARY, >, Grt)
4080 OPERATOR_ALL(BINARY, <, Sma)
4081 OPERATOR_ALL(BINARY, >=, GrtEqu)
4082 OPERATOR_ALL(BINARY, <=, SmaEqu)
4083
4084 // tertiary condition operator
4085 OPERATOR_ALL(TERTIARY, ?, Cond)
4086
4087 //Add, Sub, Mul, Div, Mod,     , Neg,     Inc, Dec,    Asign, AddAsign, SubAsign, MulAsign, DivAsign, ModAsign,     BitAnd, BitOr, BitXor, LShift, RShift, BitNot,     AndAsign, OrAsign, XorAsign, LShiftAsign, RShiftAsign,     Not,     Equ, Nqu,     And, Or,     Grt, Sma, GrtEqu, SmaEqu
4088 #define OPERATOR_TABLE_ALL(name, type) \
4089     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4090                           type##Neg, \
4091                           type##Inc, type##Dec, \
4092                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4093                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4094                           type##BitNot, \
4095                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4096                           type##Not, \
4097                           type##Equ, type##Nqu, \
4098                           type##And, type##Or, \
4099                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4100                         }; \
4101
4102 #define OPERATOR_TABLE_INTTYPES(name, type) \
4103     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4104                           type##Neg, \
4105                           type##Inc, type##Dec, \
4106                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4107                           null, null, null, null, null, \
4108                           null, \
4109                           null, null, null, null, null, \
4110                           null, \
4111                           type##Equ, type##Nqu, \
4112                           type##And, type##Or, \
4113                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4114                         }; \
4115
4116 OPERATOR_TABLE_ALL(int, Int)
4117 OPERATOR_TABLE_ALL(uint, UInt)
4118 OPERATOR_TABLE_ALL(int64, Int64)
4119 OPERATOR_TABLE_ALL(uint64, UInt64)
4120 OPERATOR_TABLE_ALL(short, Short)
4121 OPERATOR_TABLE_ALL(ushort, UShort)
4122 OPERATOR_TABLE_INTTYPES(float, Float)
4123 OPERATOR_TABLE_INTTYPES(double, Double)
4124 OPERATOR_TABLE_ALL(char, Char)
4125 OPERATOR_TABLE_ALL(uchar, UChar)
4126
4127 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4128 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4129 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4130 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4131 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4132 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4133 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4134 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4135
4136 public void ReadString(char * output,  char * string)
4137 {
4138    int len = strlen(string);
4139    int c,d = 0;
4140    bool quoted = false, escaped = false;
4141    for(c = 0; c<len; c++)
4142    {
4143       char ch = string[c];
4144       if(escaped)
4145       {
4146          switch(ch)
4147          {
4148             case 'n': output[d] = '\n'; break;
4149             case 't': output[d] = '\t'; break;
4150             case 'a': output[d] = '\a'; break;
4151             case 'b': output[d] = '\b'; break;
4152             case 'f': output[d] = '\f'; break;
4153             case 'r': output[d] = '\r'; break;
4154             case 'v': output[d] = '\v'; break;
4155             case '\\': output[d] = '\\'; break;
4156             case '\"': output[d] = '\"'; break;
4157             case '\'': output[d] = '\''; break;
4158             default: output[d] = ch;
4159          }
4160          d++;
4161          escaped = false;
4162       }
4163       else
4164       {
4165          if(ch == '\"')
4166             quoted ^= true;
4167          else if(quoted)
4168          {
4169             if(ch == '\\')
4170                escaped = true;
4171             else
4172                output[d++] = ch;
4173          }
4174       }
4175    }
4176    output[d] = '\0';
4177 }
4178
4179 // String Unescape Copy
4180
4181 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4182 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4183 public int UnescapeString(char * d, char * s, int len)
4184 {
4185    int j = 0, k = 0;
4186    char ch;
4187    while(j < len && (ch = s[j]))
4188    {
4189       switch(ch)
4190       {
4191          case '\\':
4192             switch((ch = s[++j]))
4193             {
4194                case 'n': d[k] = '\n'; break;
4195                case 't': d[k] = '\t'; break;
4196                case 'a': d[k] = '\a'; break;
4197                case 'b': d[k] = '\b'; break;
4198                case 'f': d[k] = '\f'; break;
4199                case 'r': d[k] = '\r'; break;
4200                case 'v': d[k] = '\v'; break;
4201                case '\\': d[k] = '\\'; break;
4202                case '\"': d[k] = '\"'; break;
4203                case '\'': d[k] = '\''; break;
4204                default: d[k] = '\\'; d[k] = ch;
4205             }
4206             break;
4207          default:
4208             d[k] = ch;
4209       }
4210       j++, k++;
4211    }
4212    d[k] = '\0';
4213    return k;
4214 }
4215
4216 public char * OffsetEscapedString(char * s, int len, int offset)
4217 {
4218    char ch;
4219    int j = 0, k = 0;
4220    while(j < len && k < offset && (ch = s[j]))
4221    {
4222       if(ch == '\\') ++j;
4223       j++, k++;
4224    }
4225    return (k == offset) ? s + j : null;
4226 }
4227
4228 public Operand GetOperand(Expression exp)
4229 {
4230    Operand op { };
4231    Type type = exp.expType;
4232    if(type)
4233    {
4234       while(type.kind == classType &&
4235          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4236       {
4237          if(!type._class.registered.dataType)
4238             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4239          type = type._class.registered.dataType;
4240
4241       }
4242       op.kind = type.kind;
4243       op.type = exp.expType;
4244       if(exp.type == stringExp && op.kind == pointerType)
4245       {
4246          op.ui64 = (uint64)exp.string;
4247          op.kind = pointerType;
4248          op.ops = uint64Ops;
4249       }
4250       else if(exp.isConstant && exp.type == constantExp)
4251       {
4252          switch(op.kind)
4253          {
4254             case _BoolType:
4255             case charType:
4256             {
4257                if(exp.constant[0] == '\'')
4258                   op.c = exp.constant[1];
4259                else if(type.isSigned)
4260                {
4261                   op.c = (char)strtol(exp.constant, null, 0);
4262                   op.ops = charOps;
4263                }
4264                else
4265                {
4266                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4267                   op.ops = ucharOps;
4268                }
4269                break;
4270             }
4271             case shortType:
4272                if(type.isSigned)
4273                {
4274                   op.s = (short)strtol(exp.constant, null, 0);
4275                   op.ops = shortOps;
4276                }
4277                else
4278                {
4279                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4280                   op.ops = ushortOps;
4281                }
4282                break;
4283             case intType:
4284             case longType:
4285                if(type.isSigned)
4286                {
4287                   op.i = (int)strtol(exp.constant, null, 0);
4288                   op.ops = intOps;
4289                }
4290                else
4291                {
4292                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4293                   op.ops = uintOps;
4294                }
4295                op.kind = intType;
4296                break;
4297             case int64Type:
4298                if(type.isSigned)
4299                {
4300                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4301                   op.ops = intOps;
4302                }
4303                else
4304                {
4305                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4306                   op.ops = uintOps;
4307                }
4308                op.kind = int64Type;
4309                break;
4310             case intPtrType:
4311                if(type.isSigned)
4312                {
4313                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4314                   op.ops = int64Ops;
4315                }
4316                else
4317                {
4318                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4319                   op.ops = uint64Ops;
4320                }
4321                op.kind = int64Type;
4322                break;
4323             case intSizeType:
4324                if(type.isSigned)
4325                {
4326                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4327                   op.ops = int64Ops;
4328                }
4329                else
4330                {
4331                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4332                   op.ops = uint64Ops;
4333                }
4334                op.kind = int64Type;
4335                break;
4336             case floatType:
4337                op.f = (float)strtod(exp.constant, null);
4338                op.ops = floatOps;
4339                break;
4340             case doubleType:
4341                op.d = (double)strtod(exp.constant, null);
4342                op.ops = doubleOps;
4343                break;
4344             //case classType:    For when we have operator overloading...
4345             // Pointer additions
4346             //case functionType:
4347             case arrayType:
4348             case pointerType:
4349             case classType:
4350                op.ui64 = _strtoui64(exp.constant, null, 0);
4351                op.kind = pointerType;
4352                op.ops = uint64Ops;
4353                // op.ptrSize =
4354                break;
4355          }
4356       }
4357    }
4358    return op;
4359 }
4360
4361 static void UnusedFunction()
4362 {
4363    int a;
4364    a.OnGetString(0,0,0);
4365 }
4366 default:
4367 extern int __ecereVMethodID_class_OnGetString;
4368 public:
4369
4370 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4371 {
4372    DataMember dataMember;
4373    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4374    {
4375       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4376          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4377       else
4378       {
4379          Expression exp { };
4380          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4381          Type type;
4382          void * ptr = inst.data + dataMember.offset + offset;
4383          char * result = null;
4384          exp.loc = member.loc = inst.loc;
4385          ((Identifier)member.identifiers->first).loc = inst.loc;
4386
4387          if(!dataMember.dataType)
4388             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4389          type = dataMember.dataType;
4390          if(type.kind == classType)
4391          {
4392             Class _class = type._class.registered;
4393             if(_class.type == enumClass)
4394             {
4395                Class enumClass = eSystem_FindClass(privateModule, "enum");
4396                if(enumClass)
4397                {
4398                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4399                   NamedLink item;
4400                   for(item = e.values.first; item; item = item.next)
4401                   {
4402                      if((int)item.data == *(int *)ptr)
4403                      {
4404                         result = item.name;
4405                         break;
4406                      }
4407                   }
4408                   if(result)
4409                   {
4410                      exp.identifier = MkIdentifier(result);
4411                      exp.type = identifierExp;
4412                      exp.destType = MkClassType(_class.fullName);
4413                      ProcessExpressionType(exp);
4414                   }
4415                }
4416             }
4417             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4418             {
4419                if(!_class.dataType)
4420                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4421                type = _class.dataType;
4422             }
4423          }
4424          if(!result)
4425          {
4426             switch(type.kind)
4427             {
4428                case floatType:
4429                {
4430                   FreeExpContents(exp);
4431
4432                   exp.constant = PrintFloat(*(float*)ptr);
4433                   exp.type = constantExp;
4434                   break;
4435                }
4436                case doubleType:
4437                {
4438                   FreeExpContents(exp);
4439
4440                   exp.constant = PrintDouble(*(double*)ptr);
4441                   exp.type = constantExp;
4442                   break;
4443                }
4444                case intType:
4445                {
4446                   FreeExpContents(exp);
4447
4448                   exp.constant = PrintInt(*(int*)ptr);
4449                   exp.type = constantExp;
4450                   break;
4451                }
4452                case int64Type:
4453                {
4454                   FreeExpContents(exp);
4455
4456                   exp.constant = PrintInt64(*(int64*)ptr);
4457                   exp.type = constantExp;
4458                   break;
4459                }
4460                case intPtrType:
4461                {
4462                   FreeExpContents(exp);
4463                   // TODO: This should probably use proper type
4464                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4465                   exp.type = constantExp;
4466                   break;
4467                }
4468                case intSizeType:
4469                {
4470                   FreeExpContents(exp);
4471                   // TODO: This should probably use proper type
4472                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4473                   exp.type = constantExp;
4474                   break;
4475                }
4476                default:
4477                   Compiler_Error($"Unhandled type populating instance\n");
4478             }
4479          }
4480          ListAdd(memberList, member);
4481       }
4482
4483       if(parentDataMember.type == unionMember)
4484          break;
4485    }
4486 }
4487
4488 void PopulateInstance(Instantiation inst)
4489 {
4490    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4491    Class _class = classSym.registered;
4492    DataMember dataMember;
4493    OldList * memberList = MkList();
4494    // Added this check and ->Add to prevent memory leaks on bad code
4495    if(!inst.members)
4496       inst.members = MkListOne(MkMembersInitList(memberList));
4497    else
4498       inst.members->Add(MkMembersInitList(memberList));
4499    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4500    {
4501       if(!dataMember.isProperty)
4502       {
4503          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4504             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4505          else
4506          {
4507             Expression exp { };
4508             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4509             Type type;
4510             void * ptr = inst.data + dataMember.offset;
4511             char * result = null;
4512
4513             exp.loc = member.loc = inst.loc;
4514             ((Identifier)member.identifiers->first).loc = inst.loc;
4515
4516             if(!dataMember.dataType)
4517                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4518             type = dataMember.dataType;
4519             if(type.kind == classType)
4520             {
4521                Class _class = type._class.registered;
4522                if(_class.type == enumClass)
4523                {
4524                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4525                   if(enumClass)
4526                   {
4527                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4528                      NamedLink item;
4529                      for(item = e.values.first; item; item = item.next)
4530                      {
4531                         if((int)item.data == *(int *)ptr)
4532                         {
4533                            result = item.name;
4534                            break;
4535                         }
4536                      }
4537                   }
4538                   if(result)
4539                   {
4540                      exp.identifier = MkIdentifier(result);
4541                      exp.type = identifierExp;
4542                      exp.destType = MkClassType(_class.fullName);
4543                      ProcessExpressionType(exp);
4544                   }
4545                }
4546                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4547                {
4548                   if(!_class.dataType)
4549                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4550                   type = _class.dataType;
4551                }
4552             }
4553             if(!result)
4554             {
4555                switch(type.kind)
4556                {
4557                   case floatType:
4558                   {
4559                      exp.constant = PrintFloat(*(float*)ptr);
4560                      exp.type = constantExp;
4561                      break;
4562                   }
4563                   case doubleType:
4564                   {
4565                      exp.constant = PrintDouble(*(double*)ptr);
4566                      exp.type = constantExp;
4567                      break;
4568                   }
4569                   case intType:
4570                   {
4571                      exp.constant = PrintInt(*(int*)ptr);
4572                      exp.type = constantExp;
4573                      break;
4574                   }
4575                   case int64Type:
4576                   {
4577                      exp.constant = PrintInt64(*(int64*)ptr);
4578                      exp.type = constantExp;
4579                      break;
4580                   }
4581                   case intPtrType:
4582                   {
4583                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4584                      exp.type = constantExp;
4585                      break;
4586                   }
4587                   default:
4588                      Compiler_Error($"Unhandled type populating instance\n");
4589                }
4590             }
4591             ListAdd(memberList, member);
4592          }
4593       }
4594    }
4595 }
4596
4597 void ComputeInstantiation(Expression exp)
4598 {
4599    Instantiation inst = exp.instance;
4600    MembersInit members;
4601    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4602    Class _class = classSym ? classSym.registered : null;
4603    DataMember curMember = null;
4604    Class curClass = null;
4605    DataMember subMemberStack[256];
4606    int subMemberStackPos = 0;
4607    uint64 bits = 0;
4608
4609    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4610    {
4611       // Don't recompute the instantiation...
4612       // Non Simple classes will have become constants by now
4613       if(inst.data)
4614          return;
4615
4616       if(_class.type == normalClass || _class.type == noHeadClass)
4617       {
4618          inst.data = (byte *)eInstance_New(_class);
4619          if(_class.type == normalClass)
4620             ((Instance)inst.data)._refCount++;
4621       }
4622       else
4623          inst.data = new0 byte[_class.structSize];
4624    }
4625
4626    if(inst.members)
4627    {
4628       for(members = inst.members->first; members; members = members.next)
4629       {
4630          switch(members.type)
4631          {
4632             case dataMembersInit:
4633             {
4634                if(members.dataMembers)
4635                {
4636                   MemberInit member;
4637                   for(member = members.dataMembers->first; member; member = member.next)
4638                   {
4639                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4640                      bool found = false;
4641
4642                      Property prop = null;
4643                      DataMember dataMember = null;
4644                      Method method = null;
4645                      uint dataMemberOffset;
4646
4647                      if(!ident)
4648                      {
4649                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4650                         if(curMember)
4651                         {
4652                            if(curMember.isProperty)
4653                               prop = (Property)curMember;
4654                            else
4655                            {
4656                               dataMember = curMember;
4657
4658                               // CHANGED THIS HERE
4659                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4660
4661                               // 2013/17/29 -- It seems that this was missing here!
4662                               if(_class.type == normalClass)
4663                                  dataMemberOffset += _class.base.structSize;
4664                               // dataMemberOffset = dataMember.offset;
4665                            }
4666                            found = true;
4667                         }
4668                      }
4669                      else
4670                      {
4671                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4672                         if(prop)
4673                         {
4674                            found = true;
4675                            if(prop.memberAccess == publicAccess)
4676                            {
4677                               curMember = (DataMember)prop;
4678                               curClass = prop._class;
4679                            }
4680                         }
4681                         else
4682                         {
4683                            DataMember _subMemberStack[256];
4684                            int _subMemberStackPos = 0;
4685
4686                            // FILL MEMBER STACK
4687                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4688
4689                            if(dataMember)
4690                            {
4691                               found = true;
4692                               if(dataMember.memberAccess == publicAccess)
4693                               {
4694                                  curMember = dataMember;
4695                                  curClass = dataMember._class;
4696                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4697                                  subMemberStackPos = _subMemberStackPos;
4698                               }
4699                            }
4700                         }
4701                      }
4702
4703                      if(found && member.initializer && member.initializer.type == expInitializer)
4704                      {
4705                         Expression value = member.initializer.exp;
4706                         Type type = null;
4707                         bool deepMember = false;
4708                         if(prop)
4709                         {
4710                            type = prop.dataType;
4711                         }
4712                         else if(dataMember)
4713                         {
4714                            if(!dataMember.dataType)
4715                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4716
4717                            type = dataMember.dataType;
4718                         }
4719
4720                         if(ident && ident.next)
4721                         {
4722                            deepMember = true;
4723
4724                            // for(; ident && type; ident = ident.next)
4725                            for(ident = ident.next; ident && type; ident = ident.next)
4726                            {
4727                               if(type.kind == classType)
4728                               {
4729                                  prop = eClass_FindProperty(type._class.registered,
4730                                     ident.string, privateModule);
4731                                  if(prop)
4732                                     type = prop.dataType;
4733                                  else
4734                                  {
4735                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4736                                        ident.string, &dataMemberOffset, privateModule, null, null);
4737                                     if(dataMember)
4738                                        type = dataMember.dataType;
4739                                  }
4740                               }
4741                               else if(type.kind == structType || type.kind == unionType)
4742                               {
4743                                  Type memberType;
4744                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4745                                  {
4746                                     if(!strcmp(memberType.name, ident.string))
4747                                     {
4748                                        type = memberType;
4749                                        break;
4750                                     }
4751                                  }
4752                               }
4753                            }
4754                         }
4755                         if(value)
4756                         {
4757                            FreeType(value.destType);
4758                            value.destType = type;
4759                            if(type) type.refCount++;
4760                            ComputeExpression(value);
4761                         }
4762                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4763                         {
4764                            if(type.kind == classType)
4765                            {
4766                               Class _class = type._class.registered;
4767                               if(_class.type == bitClass || _class.type == unitClass ||
4768                                  _class.type == enumClass)
4769                               {
4770                                  if(!_class.dataType)
4771                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4772                                  type = _class.dataType;
4773                               }
4774                            }
4775
4776                            if(dataMember)
4777                            {
4778                               void * ptr = inst.data + dataMemberOffset;
4779
4780                               if(value.type == constantExp)
4781                               {
4782                                  switch(type.kind)
4783                                  {
4784                                     case intType:
4785                                     {
4786                                        GetInt(value, (int*)ptr);
4787                                        break;
4788                                     }
4789                                     case int64Type:
4790                                     {
4791                                        GetInt64(value, (int64*)ptr);
4792                                        break;
4793                                     }
4794                                     case intPtrType:
4795                                     {
4796                                        GetIntPtr(value, (intptr*)ptr);
4797                                        break;
4798                                     }
4799                                     case intSizeType:
4800                                     {
4801                                        GetIntSize(value, (intsize*)ptr);
4802                                        break;
4803                                     }
4804                                     case floatType:
4805                                     {
4806                                        GetFloat(value, (float*)ptr);
4807                                        break;
4808                                     }
4809                                     case doubleType:
4810                                     {
4811                                        GetDouble(value, (double *)ptr);
4812                                        break;
4813                                     }
4814                                  }
4815                               }
4816                               else if(value.type == instanceExp)
4817                               {
4818                                  if(type.kind == classType)
4819                                  {
4820                                     Class _class = type._class.registered;
4821                                     if(_class.type == structClass)
4822                                     {
4823                                        ComputeTypeSize(type);
4824                                        if(value.instance.data)
4825                                           memcpy(ptr, value.instance.data, type.size);
4826                                     }
4827                                  }
4828                               }
4829                            }
4830                            else if(prop)
4831                            {
4832                               if(value.type == instanceExp && value.instance.data)
4833                               {
4834                                  if(type.kind == classType)
4835                                  {
4836                                     Class _class = type._class.registered;
4837                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4838                                     {
4839                                        void (*Set)(void *, void *) = (void *)prop.Set;
4840                                        Set(inst.data, value.instance.data);
4841                                        PopulateInstance(inst);
4842                                     }
4843                                  }
4844                               }
4845                               else if(value.type == constantExp)
4846                               {
4847                                  switch(type.kind)
4848                                  {
4849                                     case doubleType:
4850                                     {
4851                                        void (*Set)(void *, double) = (void *)prop.Set;
4852                                        Set(inst.data, strtod(value.constant, null) );
4853                                        break;
4854                                     }
4855                                     case floatType:
4856                                     {
4857                                        void (*Set)(void *, float) = (void *)prop.Set;
4858                                        Set(inst.data, (float)(strtod(value.constant, null)));
4859                                        break;
4860                                     }
4861                                     case intType:
4862                                     {
4863                                        void (*Set)(void *, int) = (void *)prop.Set;
4864                                        Set(inst.data, (int)strtol(value.constant, null, 0));
4865                                        break;
4866                                     }
4867                                     case int64Type:
4868                                     {
4869                                        void (*Set)(void *, int64) = (void *)prop.Set;
4870                                        Set(inst.data, _strtoi64(value.constant, null, 0));
4871                                        break;
4872                                     }
4873                                     case intPtrType:
4874                                     {
4875                                        void (*Set)(void *, intptr) = (void *)prop.Set;
4876                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
4877                                        break;
4878                                     }
4879                                     case intSizeType:
4880                                     {
4881                                        void (*Set)(void *, intsize) = (void *)prop.Set;
4882                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
4883                                        break;
4884                                     }
4885                                  }
4886                               }
4887                               else if(value.type == stringExp)
4888                               {
4889                                  char temp[1024];
4890                                  ReadString(temp, value.string);
4891                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
4892                               }
4893                            }
4894                         }
4895                         else if(!deepMember && type && _class.type == unitClass)
4896                         {
4897                            if(prop)
4898                            {
4899                               // Only support converting units to units for now...
4900                               if(value.type == constantExp)
4901                               {
4902                                  if(type.kind == classType)
4903                                  {
4904                                     Class _class = type._class.registered;
4905                                     if(_class.type == unitClass)
4906                                     {
4907                                        if(!_class.dataType)
4908                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4909                                        type = _class.dataType;
4910                                     }
4911                                  }
4912                                  // TODO: Assuming same base type for units...
4913                                  switch(type.kind)
4914                                  {
4915                                     case floatType:
4916                                     {
4917                                        float fValue;
4918                                        float (*Set)(float) = (void *)prop.Set;
4919                                        GetFloat(member.initializer.exp, &fValue);
4920                                        exp.constant = PrintFloat(Set(fValue));
4921                                        exp.type = constantExp;
4922                                        break;
4923                                     }
4924                                     case doubleType:
4925                                     {
4926                                        double dValue;
4927                                        double (*Set)(double) = (void *)prop.Set;
4928                                        GetDouble(member.initializer.exp, &dValue);
4929                                        exp.constant = PrintDouble(Set(dValue));
4930                                        exp.type = constantExp;
4931                                        break;
4932                                     }
4933                                  }
4934                               }
4935                            }
4936                         }
4937                         else if(!deepMember && type && _class.type == bitClass)
4938                         {
4939                            if(prop)
4940                            {
4941                               if(value.type == instanceExp && value.instance.data)
4942                               {
4943                                  unsigned int (*Set)(void *) = (void *)prop.Set;
4944                                  bits = Set(value.instance.data);
4945                               }
4946                               else if(value.type == constantExp)
4947                               {
4948                               }
4949                            }
4950                            else if(dataMember)
4951                            {
4952                               BitMember bitMember = (BitMember) dataMember;
4953                               Type type;
4954                               int part = 0;
4955                               GetInt(value, &part);
4956                               bits = (bits & ~bitMember.mask);
4957                               if(!bitMember.dataType)
4958                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
4959
4960                               type = bitMember.dataType;
4961
4962                               if(type.kind == classType && type._class && type._class.registered)
4963                               {
4964                                  if(!type._class.registered.dataType)
4965                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4966                                  type = type._class.registered.dataType;
4967                               }
4968
4969                               switch(type.kind)
4970                               {
4971                                  case _BoolType:
4972                                  case charType:
4973                                     if(type.isSigned)
4974                                        bits |= ((char)part << bitMember.pos);
4975                                     else
4976                                        bits |= ((unsigned char)part << bitMember.pos);
4977                                     break;
4978                                  case shortType:
4979                                     if(type.isSigned)
4980                                        bits |= ((short)part << bitMember.pos);
4981                                     else
4982                                        bits |= ((unsigned short)part << bitMember.pos);
4983                                     break;
4984                                  case intType:
4985                                  case longType:
4986                                     if(type.isSigned)
4987                                        bits |= ((int)part << bitMember.pos);
4988                                     else
4989                                        bits |= ((unsigned int)part << bitMember.pos);
4990                                     break;
4991                                  case int64Type:
4992                                     if(type.isSigned)
4993                                        bits |= ((int64)part << bitMember.pos);
4994                                     else
4995                                        bits |= ((uint64)part << bitMember.pos);
4996                                     break;
4997                                  case intPtrType:
4998                                     if(type.isSigned)
4999                                     {
5000                                        bits |= ((intptr)part << bitMember.pos);
5001                                     }
5002                                     else
5003                                     {
5004                                        bits |= ((uintptr)part << bitMember.pos);
5005                                     }
5006                                     break;
5007                                  case intSizeType:
5008                                     if(type.isSigned)
5009                                     {
5010                                        bits |= ((ssize_t)(intsize)part << bitMember.pos);
5011                                     }
5012                                     else
5013                                     {
5014                                        bits |= ((size_t) (uintsize)part << bitMember.pos);
5015                                     }
5016                                     break;
5017                               }
5018                            }
5019                         }
5020                      }
5021                      else
5022                      {
5023                         if(_class && _class.type == unitClass)
5024                         {
5025                            ComputeExpression(member.initializer.exp);
5026                            exp.constant = member.initializer.exp.constant;
5027                            exp.type = constantExp;
5028
5029                            member.initializer.exp.constant = null;
5030                         }
5031                      }
5032                   }
5033                }
5034                break;
5035             }
5036          }
5037       }
5038    }
5039    if(_class && _class.type == bitClass)
5040    {
5041       exp.constant = PrintHexUInt(bits);
5042       exp.type = constantExp;
5043    }
5044    if(exp.type != instanceExp)
5045    {
5046       FreeInstance(inst);
5047    }
5048 }
5049
5050 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5051 {
5052    if(exp.op.op == SIZEOF)
5053    {
5054       FreeExpContents(exp);
5055       exp.type = constantExp;
5056       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5057    }
5058    else
5059    {
5060       if(!exp.op.exp1)
5061       {
5062          switch(exp.op.op)
5063          {
5064             // unary arithmetic
5065             case '+':
5066             {
5067                // Provide default unary +
5068                Expression exp2 = exp.op.exp2;
5069                exp.op.exp2 = null;
5070                FreeExpContents(exp);
5071                FreeType(exp.expType);
5072                FreeType(exp.destType);
5073                *exp = *exp2;
5074                delete exp2;
5075                break;
5076             }
5077             case '-':
5078                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5079                break;
5080             // unary arithmetic increment and decrement
5081                   //OPERATOR_ALL(UNARY, ++, Inc)
5082                   //OPERATOR_ALL(UNARY, --, Dec)
5083             // unary bitwise
5084             case '~':
5085                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5086                break;
5087             // unary logical negation
5088             case '!':
5089                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5090                break;
5091          }
5092       }
5093       else
5094       {
5095          switch(exp.op.op)
5096          {
5097             // binary arithmetic
5098             case '+':
5099                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5100                break;
5101             case '-':
5102                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5103                break;
5104             case '*':
5105                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5106                break;
5107             case '/':
5108                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5109                break;
5110             case '%':
5111                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5112                break;
5113             // binary arithmetic assignment
5114                   //OPERATOR_ALL(BINARY, =, Asign)
5115                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5116                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5117                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5118                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5119                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5120             // binary bitwise
5121             case '&':
5122                if(exp.op.exp2)
5123                {
5124                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5125                }
5126                break;
5127             case '|':
5128                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5129                break;
5130             case '^':
5131                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5132                break;
5133             case LEFT_OP:
5134                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5135                break;
5136             case RIGHT_OP:
5137                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5138                break;
5139             // binary bitwise assignment
5140                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5141                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5142                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5143                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5144                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5145             // binary logical equality
5146             case EQ_OP:
5147                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5148                break;
5149             case NE_OP:
5150                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5151                break;
5152             // binary logical
5153             case AND_OP:
5154                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5155                break;
5156             case OR_OP:
5157                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5158                break;
5159             // binary logical relational
5160             case '>':
5161                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5162                break;
5163             case '<':
5164                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5165                break;
5166             case GE_OP:
5167                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5168                break;
5169             case LE_OP:
5170                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5171                break;
5172          }
5173       }
5174    }
5175 }
5176
5177 void ComputeExpression(Expression exp)
5178 {
5179    char expString[10240];
5180    expString[0] = '\0';
5181 #ifdef _DEBUG
5182    PrintExpression(exp, expString);
5183 #endif
5184
5185    switch(exp.type)
5186    {
5187       case instanceExp:
5188       {
5189          ComputeInstantiation(exp);
5190          break;
5191       }
5192       /*
5193       case constantExp:
5194          break;
5195       */
5196       case opExp:
5197       {
5198          Expression exp1, exp2 = null;
5199          Operand op1 { };
5200          Operand op2 { };
5201
5202          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5203          if(exp.op.exp2)
5204          {
5205             Expression e = exp.op.exp2;
5206
5207             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5208             {
5209                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5210                {
5211                   if(e.type == extensionCompoundExp)
5212                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5213                   else
5214                      e = e.list->last;
5215                }
5216             }
5217             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5218             {
5219                if(e.type == stringExp && e.string)
5220                {
5221                   char * string = e.string;
5222                   int len = strlen(string);
5223                   char * tmp = new char[len-2+1];
5224                   len = UnescapeString(tmp, string + 1, len - 2);
5225                   delete tmp;
5226                   FreeExpContents(exp);
5227                   exp.type = constantExp;
5228                   exp.constant = PrintUInt(len + 1);
5229                }
5230                else
5231                {
5232                   Type type = e.expType;
5233                   type.refCount++;
5234                   FreeExpContents(exp);
5235                   exp.type = constantExp;
5236                   exp.constant = PrintUInt(ComputeTypeSize(type));
5237                   FreeType(type);
5238                }
5239                break;
5240             }
5241             else
5242                ComputeExpression(exp.op.exp2);
5243          }
5244          if(exp.op.exp1)
5245          {
5246             ComputeExpression(exp.op.exp1);
5247             exp1 = exp.op.exp1;
5248             exp2 = exp.op.exp2;
5249             op1 = GetOperand(exp1);
5250             if(op1.type) op1.type.refCount++;
5251             if(exp2)
5252             {
5253                op2 = GetOperand(exp2);
5254                if(op2.type) op2.type.refCount++;
5255             }
5256          }
5257          else
5258          {
5259             exp1 = exp.op.exp2;
5260             op1 = GetOperand(exp1);
5261             if(op1.type) op1.type.refCount++;
5262          }
5263
5264          CallOperator(exp, exp1, exp2, op1, op2);
5265          /*
5266          switch(exp.op.op)
5267          {
5268             // Unary operators
5269             case '&':
5270                // Also binary
5271                if(exp.op.exp1 && exp.op.exp2)
5272                {
5273                   // Binary And
5274                   if(op1.ops.BitAnd)
5275                   {
5276                      FreeExpContents(exp);
5277                      op1.ops.BitAnd(exp, op1, op2);
5278                   }
5279                }
5280                break;
5281             case '*':
5282                if(exp.op.exp1)
5283                {
5284                   if(op1.ops.Mul)
5285                   {
5286                      FreeExpContents(exp);
5287                      op1.ops.Mul(exp, op1, op2);
5288                   }
5289                }
5290                break;
5291             case '+':
5292                if(exp.op.exp1)
5293                {
5294                   if(op1.ops.Add)
5295                   {
5296                      FreeExpContents(exp);
5297                      op1.ops.Add(exp, op1, op2);
5298                   }
5299                }
5300                else
5301                {
5302                   // Provide default unary +
5303                   Expression exp2 = exp.op.exp2;
5304                   exp.op.exp2 = null;
5305                   FreeExpContents(exp);
5306                   FreeType(exp.expType);
5307                   FreeType(exp.destType);
5308
5309                   *exp = *exp2;
5310                   delete exp2;
5311                }
5312                break;
5313             case '-':
5314                if(exp.op.exp1)
5315                {
5316                   if(op1.ops.Sub)
5317                   {
5318                      FreeExpContents(exp);
5319                      op1.ops.Sub(exp, op1, op2);
5320                   }
5321                }
5322                else
5323                {
5324                   if(op1.ops.Neg)
5325                   {
5326                      FreeExpContents(exp);
5327                      op1.ops.Neg(exp, op1);
5328                   }
5329                }
5330                break;
5331             case '~':
5332                if(op1.ops.BitNot)
5333                {
5334                   FreeExpContents(exp);
5335                   op1.ops.BitNot(exp, op1);
5336                }
5337                break;
5338             case '!':
5339                if(op1.ops.Not)
5340                {
5341                   FreeExpContents(exp);
5342                   op1.ops.Not(exp, op1);
5343                }
5344                break;
5345             // Binary only operators
5346             case '/':
5347                if(op1.ops.Div)
5348                {
5349                   FreeExpContents(exp);
5350                   op1.ops.Div(exp, op1, op2);
5351                }
5352                break;
5353             case '%':
5354                if(op1.ops.Mod)
5355                {
5356                   FreeExpContents(exp);
5357                   op1.ops.Mod(exp, op1, op2);
5358                }
5359                break;
5360             case LEFT_OP:
5361                break;
5362             case RIGHT_OP:
5363                break;
5364             case '<':
5365                if(exp.op.exp1)
5366                {
5367                   if(op1.ops.Sma)
5368                   {
5369                      FreeExpContents(exp);
5370                      op1.ops.Sma(exp, op1, op2);
5371                   }
5372                }
5373                break;
5374             case '>':
5375                if(exp.op.exp1)
5376                {
5377                   if(op1.ops.Grt)
5378                   {
5379                      FreeExpContents(exp);
5380                      op1.ops.Grt(exp, op1, op2);
5381                   }
5382                }
5383                break;
5384             case LE_OP:
5385                if(exp.op.exp1)
5386                {
5387                   if(op1.ops.SmaEqu)
5388                   {
5389                      FreeExpContents(exp);
5390                      op1.ops.SmaEqu(exp, op1, op2);
5391                   }
5392                }
5393                break;
5394             case GE_OP:
5395                if(exp.op.exp1)
5396                {
5397                   if(op1.ops.GrtEqu)
5398                   {
5399                      FreeExpContents(exp);
5400                      op1.ops.GrtEqu(exp, op1, op2);
5401                   }
5402                }
5403                break;
5404             case EQ_OP:
5405                if(exp.op.exp1)
5406                {
5407                   if(op1.ops.Equ)
5408                   {
5409                      FreeExpContents(exp);
5410                      op1.ops.Equ(exp, op1, op2);
5411                   }
5412                }
5413                break;
5414             case NE_OP:
5415                if(exp.op.exp1)
5416                {
5417                   if(op1.ops.Nqu)
5418                   {
5419                      FreeExpContents(exp);
5420                      op1.ops.Nqu(exp, op1, op2);
5421                   }
5422                }
5423                break;
5424             case '|':
5425                if(op1.ops.BitOr)
5426                {
5427                   FreeExpContents(exp);
5428                   op1.ops.BitOr(exp, op1, op2);
5429                }
5430                break;
5431             case '^':
5432                if(op1.ops.BitXor)
5433                {
5434                   FreeExpContents(exp);
5435                   op1.ops.BitXor(exp, op1, op2);
5436                }
5437                break;
5438             case AND_OP:
5439                break;
5440             case OR_OP:
5441                break;
5442             case SIZEOF:
5443                FreeExpContents(exp);
5444                exp.type = constantExp;
5445                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5446                break;
5447          }
5448          */
5449          if(op1.type) FreeType(op1.type);
5450          if(op2.type) FreeType(op2.type);
5451          break;
5452       }
5453       case bracketsExp:
5454       case extensionExpressionExp:
5455       {
5456          Expression e, n;
5457          for(e = exp.list->first; e; e = n)
5458          {
5459             n = e.next;
5460             if(!n)
5461             {
5462                OldList * list = exp.list;
5463                ComputeExpression(e);
5464                //FreeExpContents(exp);
5465                FreeType(exp.expType);
5466                FreeType(exp.destType);
5467                *exp = *e;
5468                delete e;
5469                delete list;
5470             }
5471             else
5472             {
5473                FreeExpression(e);
5474             }
5475          }
5476          break;
5477       }
5478       /*
5479
5480       case ExpIndex:
5481       {
5482          Expression e;
5483          exp.isConstant = true;
5484
5485          ComputeExpression(exp.index.exp);
5486          if(!exp.index.exp.isConstant)
5487             exp.isConstant = false;
5488
5489          for(e = exp.index.index->first; e; e = e.next)
5490          {
5491             ComputeExpression(e);
5492             if(!e.next)
5493             {
5494                // Check if this type is int
5495             }
5496             if(!e.isConstant)
5497                exp.isConstant = false;
5498          }
5499          exp.expType = Dereference(exp.index.exp.expType);
5500          break;
5501       }
5502       */
5503       case memberExp:
5504       {
5505          Expression memberExp = exp.member.exp;
5506          Identifier memberID = exp.member.member;
5507
5508          Type type;
5509          ComputeExpression(exp.member.exp);
5510          type = exp.member.exp.expType;
5511          if(type)
5512          {
5513             Class _class = (exp.member.member && exp.member.member.classSym) ? exp.member.member.classSym.registered : (((type.kind == classType || type.kind == subClassType) && type._class) ? type._class.registered : null);
5514             Property prop = null;
5515             DataMember member = null;
5516             Class convertTo = null;
5517             if(type.kind == subClassType && exp.member.exp.type == classExp)
5518                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5519
5520             if(!_class)
5521             {
5522                char string[256];
5523                Symbol classSym;
5524                string[0] = '\0';
5525                PrintTypeNoConst(type, string, false, true);
5526                classSym = FindClass(string);
5527                _class = classSym ? classSym.registered : null;
5528             }
5529
5530             if(exp.member.member)
5531             {
5532                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5533                if(!prop)
5534                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5535             }
5536             if(!prop && !member && _class && exp.member.member)
5537             {
5538                Symbol classSym = FindClass(exp.member.member.string);
5539                convertTo = _class;
5540                _class = classSym ? classSym.registered : null;
5541                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5542             }
5543
5544             if(prop)
5545             {
5546                if(prop.compiled)
5547                {
5548                   Type type = prop.dataType;
5549                   // TODO: Assuming same base type for units...
5550                   if(_class.type == unitClass)
5551                   {
5552                      if(type.kind == classType)
5553                      {
5554                         Class _class = type._class.registered;
5555                         if(_class.type == unitClass)
5556                         {
5557                            if(!_class.dataType)
5558                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5559                            type = _class.dataType;
5560                         }
5561                      }
5562                      switch(type.kind)
5563                      {
5564                         case floatType:
5565                         {
5566                            float value;
5567                            float (*Get)(float) = (void *)prop.Get;
5568                            GetFloat(exp.member.exp, &value);
5569                            exp.constant = PrintFloat(Get ? Get(value) : value);
5570                            exp.type = constantExp;
5571                            break;
5572                         }
5573                         case doubleType:
5574                         {
5575                            double value;
5576                            double (*Get)(double);
5577                            GetDouble(exp.member.exp, &value);
5578
5579                            if(convertTo)
5580                               Get = (void *)prop.Set;
5581                            else
5582                               Get = (void *)prop.Get;
5583                            exp.constant = PrintDouble(Get ? Get(value) : value);
5584                            exp.type = constantExp;
5585                            break;
5586                         }
5587                      }
5588                   }
5589                   else
5590                   {
5591                      if(convertTo)
5592                      {
5593                         Expression value = exp.member.exp;
5594                         Type type;
5595                         if(!prop.dataType)
5596                            ProcessPropertyType(prop);
5597
5598                         type = prop.dataType;
5599                         if(!type)
5600                         {
5601                             // printf("Investigate this\n");
5602                         }
5603                         else if(_class.type == structClass)
5604                         {
5605                            switch(type.kind)
5606                            {
5607                               case classType:
5608                               {
5609                                  Class propertyClass = type._class.registered;
5610                                  if(propertyClass.type == structClass && value.type == instanceExp)
5611                                  {
5612                                     void (*Set)(void *, void *) = (void *)prop.Set;
5613                                     exp.instance = Instantiation { };
5614                                     exp.instance.data = new0 byte[_class.structSize];
5615                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5616                                     exp.instance.loc = exp.loc;
5617                                     exp.type = instanceExp;
5618                                     Set(exp.instance.data, value.instance.data);
5619                                     PopulateInstance(exp.instance);
5620                                  }
5621                                  break;
5622                               }
5623                               case intType:
5624                               {
5625                                  int intValue;
5626                                  void (*Set)(void *, int) = (void *)prop.Set;
5627
5628                                  exp.instance = Instantiation { };
5629                                  exp.instance.data = new0 byte[_class.structSize];
5630                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5631                                  exp.instance.loc = exp.loc;
5632                                  exp.type = instanceExp;
5633
5634                                  GetInt(value, &intValue);
5635
5636                                  Set(exp.instance.data, intValue);
5637                                  PopulateInstance(exp.instance);
5638                                  break;
5639                               }
5640                               case int64Type:
5641                               {
5642                                  int64 intValue;
5643                                  void (*Set)(void *, int64) = (void *)prop.Set;
5644
5645                                  exp.instance = Instantiation { };
5646                                  exp.instance.data = new0 byte[_class.structSize];
5647                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5648                                  exp.instance.loc = exp.loc;
5649                                  exp.type = instanceExp;
5650
5651                                  GetInt64(value, &intValue);
5652
5653                                  Set(exp.instance.data, intValue);
5654                                  PopulateInstance(exp.instance);
5655                                  break;
5656                               }
5657                               case intPtrType:
5658                               {
5659                                  // TOFIX:
5660                                  intptr intValue;
5661                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5662
5663                                  exp.instance = Instantiation { };
5664                                  exp.instance.data = new0 byte[_class.structSize];
5665                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5666                                  exp.instance.loc = exp.loc;
5667                                  exp.type = instanceExp;
5668
5669                                  GetIntPtr(value, &intValue);
5670
5671                                  Set(exp.instance.data, intValue);
5672                                  PopulateInstance(exp.instance);
5673                                  break;
5674                               }
5675                               case intSizeType:
5676                               {
5677                                  // TOFIX:
5678                                  intsize intValue;
5679                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5680
5681                                  exp.instance = Instantiation { };
5682                                  exp.instance.data = new0 byte[_class.structSize];
5683                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5684                                  exp.instance.loc = exp.loc;
5685                                  exp.type = instanceExp;
5686
5687                                  GetIntSize(value, &intValue);
5688
5689                                  Set(exp.instance.data, intValue);
5690                                  PopulateInstance(exp.instance);
5691                                  break;
5692                               }
5693                               case doubleType:
5694                               {
5695                                  double doubleValue;
5696                                  void (*Set)(void *, double) = (void *)prop.Set;
5697
5698                                  exp.instance = Instantiation { };
5699                                  exp.instance.data = new0 byte[_class.structSize];
5700                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5701                                  exp.instance.loc = exp.loc;
5702                                  exp.type = instanceExp;
5703
5704                                  GetDouble(value, &doubleValue);
5705
5706                                  Set(exp.instance.data, doubleValue);
5707                                  PopulateInstance(exp.instance);
5708                                  break;
5709                               }
5710                            }
5711                         }
5712                         else if(_class.type == bitClass)
5713                         {
5714                            switch(type.kind)
5715                            {
5716                               case classType:
5717                               {
5718                                  Class propertyClass = type._class.registered;
5719                                  if(propertyClass.type == structClass && value.instance.data)
5720                                  {
5721                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5722                                     unsigned int bits = Set(value.instance.data);
5723                                     exp.constant = PrintHexUInt(bits);
5724                                     exp.type = constantExp;
5725                                     break;
5726                                  }
5727                                  else if(_class.type == bitClass)
5728                                  {
5729                                     unsigned int value;
5730                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5731                                     unsigned int bits;
5732
5733                                     GetUInt(exp.member.exp, &value);
5734                                     bits = Set(value);
5735                                     exp.constant = PrintHexUInt(bits);
5736                                     exp.type = constantExp;
5737                                  }
5738                               }
5739                            }
5740                         }
5741                      }
5742                      else
5743                      {
5744                         if(_class.type == bitClass)
5745                         {
5746                            unsigned int value;
5747                            GetUInt(exp.member.exp, &value);
5748
5749                            switch(type.kind)
5750                            {
5751                               case classType:
5752                               {
5753                                  Class _class = type._class.registered;
5754                                  if(_class.type == structClass)
5755                                  {
5756                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5757
5758                                     exp.instance = Instantiation { };
5759                                     exp.instance.data = new0 byte[_class.structSize];
5760                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5761                                     exp.instance.loc = exp.loc;
5762                                     //exp.instance.fullSet = true;
5763                                     exp.type = instanceExp;
5764                                     Get(value, exp.instance.data);
5765                                     PopulateInstance(exp.instance);
5766                                  }
5767                                  else if(_class.type == bitClass)
5768                                  {
5769                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5770                                     uint64 bits = Get(value);
5771                                     exp.constant = PrintHexUInt64(bits);
5772                                     exp.type = constantExp;
5773                                  }
5774                                  break;
5775                               }
5776                            }
5777                         }
5778                         else if(_class.type == structClass)
5779                         {
5780                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5781                            switch(type.kind)
5782                            {
5783                               case classType:
5784                               {
5785                                  Class _class = type._class.registered;
5786                                  if(_class.type == structClass && value)
5787                                  {
5788                                     void (*Get)(void *, void *) = (void *)prop.Get;
5789
5790                                     exp.instance = Instantiation { };
5791                                     exp.instance.data = new0 byte[_class.structSize];
5792                                     exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5793                                     exp.instance.loc = exp.loc;
5794                                     //exp.instance.fullSet = true;
5795                                     exp.type = instanceExp;
5796                                     Get(value, exp.instance.data);
5797                                     PopulateInstance(exp.instance);
5798                                  }
5799                                  break;
5800                               }
5801                            }
5802                         }
5803                         /*else
5804                         {
5805                            char * value = exp.member.exp.instance.data;
5806                            switch(type.kind)
5807                            {
5808                               case classType:
5809                               {
5810                                  Class _class = type._class.registered;
5811                                  if(_class.type == normalClass)
5812                                  {
5813                                     void *(*Get)(void *) = (void *)prop.Get;
5814
5815                                     exp.instance = Instantiation { };
5816                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
5817                                     exp.type = instanceExp;
5818                                     exp.instance.data = Get(value, exp.instance.data);
5819                                  }
5820                                  break;
5821                               }
5822                            }
5823                         }
5824                         */
5825                      }
5826                   }
5827                }
5828                else
5829                {
5830                   exp.isConstant = false;
5831                }
5832             }
5833             else if(member)
5834             {
5835             }
5836          }
5837
5838          if(exp.type != ExpressionType::memberExp)
5839          {
5840             FreeExpression(memberExp);
5841             FreeIdentifier(memberID);
5842          }
5843          break;
5844       }
5845       case typeSizeExp:
5846       {
5847          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
5848          FreeExpContents(exp);
5849          exp.constant = PrintUInt(ComputeTypeSize(type));
5850          exp.type = constantExp;
5851          FreeType(type);
5852          break;
5853       }
5854       case classSizeExp:
5855       {
5856          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
5857          if(classSym && classSym.registered)
5858          {
5859             if(classSym.registered.fixed)
5860             {
5861                FreeSpecifier(exp._class);
5862                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
5863                exp.type = constantExp;
5864             }
5865             else
5866             {
5867                char className[1024];
5868                strcpy(className, "__ecereClass_");
5869                FullClassNameCat(className, classSym.string, true);
5870                MangleClassName(className);
5871
5872                DeclareClass(classSym, className);
5873
5874                FreeExpContents(exp);
5875                exp.type = pointerExp;
5876                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
5877                exp.member.member = MkIdentifier("structSize");
5878             }
5879          }
5880          break;
5881       }
5882       case castExp:
5883       //case constantExp:
5884       {
5885          Type type;
5886          Expression e = exp;
5887          if(exp.type == castExp)
5888          {
5889             if(exp.cast.exp)
5890                ComputeExpression(exp.cast.exp);
5891             e = exp.cast.exp;
5892          }
5893          if(e && exp.expType)
5894          {
5895             /*if(exp.destType)
5896                type = exp.destType;
5897             else*/
5898                type = exp.expType;
5899             if(type.kind == classType)
5900             {
5901                Class _class = type._class.registered;
5902                if(_class && (_class.type == unitClass || _class.type == bitClass))
5903                {
5904                   if(!_class.dataType)
5905                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5906                   type = _class.dataType;
5907                }
5908             }
5909
5910             switch(type.kind)
5911             {
5912                case _BoolType:
5913                case charType:
5914                   if(type.isSigned)
5915                   {
5916                      char value;
5917                      GetChar(e, &value);
5918                      FreeExpContents(exp);
5919                      exp.constant = PrintChar(value);
5920                      exp.type = constantExp;
5921                   }
5922                   else
5923                   {
5924                      unsigned char value;
5925                      GetUChar(e, &value);
5926                      FreeExpContents(exp);
5927                      exp.constant = PrintUChar(value);
5928                      exp.type = constantExp;
5929                   }
5930                   break;
5931                case shortType:
5932                   if(type.isSigned)
5933                   {
5934                      short value;
5935                      GetShort(e, &value);
5936                      FreeExpContents(exp);
5937                      exp.constant = PrintShort(value);
5938                      exp.type = constantExp;
5939                   }
5940                   else
5941                   {
5942                      unsigned short value;
5943                      GetUShort(e, &value);
5944                      FreeExpContents(exp);
5945                      exp.constant = PrintUShort(value);
5946                      exp.type = constantExp;
5947                   }
5948                   break;
5949                case intType:
5950                   if(type.isSigned)
5951                   {
5952                      int value;
5953                      GetInt(e, &value);
5954                      FreeExpContents(exp);
5955                      exp.constant = PrintInt(value);
5956                      exp.type = constantExp;
5957                   }
5958                   else
5959                   {
5960                      unsigned int value;
5961                      GetUInt(e, &value);
5962                      FreeExpContents(exp);
5963                      exp.constant = PrintUInt(value);
5964                      exp.type = constantExp;
5965                   }
5966                   break;
5967                case int64Type:
5968                   if(type.isSigned)
5969                   {
5970                      int64 value;
5971                      GetInt64(e, &value);
5972                      FreeExpContents(exp);
5973                      exp.constant = PrintInt64(value);
5974                      exp.type = constantExp;
5975                   }
5976                   else
5977                   {
5978                      uint64 value;
5979                      GetUInt64(e, &value);
5980                      FreeExpContents(exp);
5981                      exp.constant = PrintUInt64(value);
5982                      exp.type = constantExp;
5983                   }
5984                   break;
5985                case intPtrType:
5986                   if(type.isSigned)
5987                   {
5988                      intptr value;
5989                      GetIntPtr(e, &value);
5990                      FreeExpContents(exp);
5991                      exp.constant = PrintInt64((int64)value);
5992                      exp.type = constantExp;
5993                   }
5994                   else
5995                   {
5996                      uintptr value;
5997                      GetUIntPtr(e, &value);
5998                      FreeExpContents(exp);
5999                      exp.constant = PrintUInt64((uint64)value);
6000                      exp.type = constantExp;
6001                   }
6002                   break;
6003                case intSizeType:
6004                   if(type.isSigned)
6005                   {
6006                      intsize value;
6007                      GetIntSize(e, &value);
6008                      FreeExpContents(exp);
6009                      exp.constant = PrintInt64((int64)value);
6010                      exp.type = constantExp;
6011                   }
6012                   else
6013                   {
6014                      uintsize value;
6015                      GetUIntSize(e, &value);
6016                      FreeExpContents(exp);
6017                      exp.constant = PrintUInt64((uint64)value);
6018                      exp.type = constantExp;
6019                   }
6020                   break;
6021                case floatType:
6022                {
6023                   float value;
6024                   GetFloat(e, &value);
6025                   FreeExpContents(exp);
6026                   exp.constant = PrintFloat(value);
6027                   exp.type = constantExp;
6028                   break;
6029                }
6030                case doubleType:
6031                {
6032                   double value;
6033                   GetDouble(e, &value);
6034                   FreeExpContents(exp);
6035                   exp.constant = PrintDouble(value);
6036                   exp.type = constantExp;
6037                   break;
6038                }
6039             }
6040          }
6041          break;
6042       }
6043       case conditionExp:
6044       {
6045          Operand op1 { };
6046          Operand op2 { };
6047          Operand op3 { };
6048
6049          if(exp.cond.exp)
6050             // Caring only about last expression for now...
6051             ComputeExpression(exp.cond.exp->last);
6052          if(exp.cond.elseExp)
6053             ComputeExpression(exp.cond.elseExp);
6054          if(exp.cond.cond)
6055             ComputeExpression(exp.cond.cond);
6056
6057          op1 = GetOperand(exp.cond.cond);
6058          if(op1.type) op1.type.refCount++;
6059          op2 = GetOperand(exp.cond.exp->last);
6060          if(op2.type) op2.type.refCount++;
6061          op3 = GetOperand(exp.cond.elseExp);
6062          if(op3.type) op3.type.refCount++;
6063
6064          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6065          if(op1.type) FreeType(op1.type);
6066          if(op2.type) FreeType(op2.type);
6067          if(op3.type) FreeType(op3.type);
6068          break;
6069       }
6070    }
6071 }
6072
6073 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
6074 {
6075    bool result = true;
6076    if(destType)
6077    {
6078       OldList converts { };
6079       Conversion convert;
6080
6081       if(destType.kind == voidType)
6082          return false;
6083
6084       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
6085          result = false;
6086       if(converts.count)
6087       {
6088          // for(convert = converts.last; convert; convert = convert.prev)
6089          for(convert = converts.first; convert; convert = convert.next)
6090          {
6091             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6092             if(!empty)
6093             {
6094                Expression newExp { };
6095                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6096
6097                // TODO: Check this...
6098                *newExp = *exp;
6099                newExp.destType = null;
6100
6101                if(convert.isGet)
6102                {
6103                   // [exp].ColorRGB
6104                   exp.type = memberExp;
6105                   exp.addedThis = true;
6106                   exp.member.exp = newExp;
6107                   FreeType(exp.member.exp.expType);
6108
6109                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6110                   exp.member.exp.expType.classObjectType = objectType;
6111                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6112                   exp.member.memberType = propertyMember;
6113                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6114                   // TESTING THIS... for (int)degrees
6115                   exp.needCast = true;
6116                   if(exp.expType) exp.expType.refCount++;
6117                   ApplyAnyObjectLogic(exp.member.exp);
6118                }
6119                else
6120                {
6121
6122                   /*if(exp.isConstant)
6123                   {
6124                      // Color { ColorRGB = [exp] };
6125                      exp.type = instanceExp;
6126                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6127                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6128                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6129                   }
6130                   else*/
6131                   {
6132                      // If not constant, don't turn it yet into an instantiation
6133                      // (Go through the deep members system first)
6134                      exp.type = memberExp;
6135                      exp.addedThis = true;
6136                      exp.member.exp = newExp;
6137
6138                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6139                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6140                         newExp.expType._class.registered.type == noHeadClass)
6141                      {
6142                         newExp.byReference = true;
6143                      }
6144
6145                      FreeType(exp.member.exp.expType);
6146                      /*exp.member.exp.expType = convert.convert.dataType;
6147                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6148                      exp.member.exp.expType = null;
6149                      if(convert.convert.dataType)
6150                      {
6151                         exp.member.exp.expType = { };
6152                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6153                         exp.member.exp.expType.refCount = 1;
6154                         exp.member.exp.expType.classObjectType = objectType;
6155                         ApplyAnyObjectLogic(exp.member.exp);
6156                      }
6157
6158                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6159                      exp.member.memberType = reverseConversionMember;
6160                      exp.expType = convert.resultType ? convert.resultType :
6161                         MkClassType(convert.convert._class.fullName);
6162                      exp.needCast = true;
6163                      if(convert.resultType) convert.resultType.refCount++;
6164                   }
6165                }
6166             }
6167             else
6168             {
6169                FreeType(exp.expType);
6170                if(convert.isGet)
6171                {
6172                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6173                   exp.needCast = true;
6174                   if(exp.expType) exp.expType.refCount++;
6175                }
6176                else
6177                {
6178                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6179                   exp.needCast = true;
6180                   if(convert.resultType)
6181                      convert.resultType.refCount++;
6182                }
6183             }
6184          }
6185          if(exp.isConstant && inCompiler)
6186             ComputeExpression(exp);
6187
6188          converts.Free(FreeConvert);
6189       }
6190
6191       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6192       {
6193          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6194       }
6195       if(!result && exp.expType && exp.destType)
6196       {
6197          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6198              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6199             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6200             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6201             result = true;
6202       }
6203    }
6204    // if(result) CheckTemplateTypes(exp);
6205    return result;
6206 }
6207
6208 void CheckTemplateTypes(Expression exp)
6209 {
6210    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6211    {
6212       Expression newExp { };
6213       Statement compound;
6214       Context context;
6215       *newExp = *exp;
6216       if(exp.destType) exp.destType.refCount++;
6217       if(exp.expType)  exp.expType.refCount++;
6218       newExp.prev = null;
6219       newExp.next = null;
6220
6221       switch(exp.expType.kind)
6222       {
6223          case doubleType:
6224             if(exp.destType.classObjectType)
6225             {
6226                // We need to pass the address, just pass it along (Undo what was done above)
6227                if(exp.destType) exp.destType.refCount--;
6228                if(exp.expType)  exp.expType.refCount--;
6229                delete newExp;
6230             }
6231             else
6232             {
6233                // If we're looking for value:
6234                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6235                OldList * specs;
6236                OldList * unionDefs = MkList();
6237                OldList * statements = MkList();
6238                context = PushContext();
6239                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6240                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6241                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6242                exp.type = extensionCompoundExp;
6243                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6244                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6245                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6246                exp.compound.compound.context = context;
6247                PopContext(context);
6248             }
6249             break;
6250          default:
6251             exp.type = castExp;
6252             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6253             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6254             break;
6255       }
6256    }
6257    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6258    {
6259       Expression newExp { };
6260       Statement compound;
6261       Context context;
6262       *newExp = *exp;
6263       if(exp.destType) exp.destType.refCount++;
6264       if(exp.expType)  exp.expType.refCount++;
6265       newExp.prev = null;
6266       newExp.next = null;
6267
6268       switch(exp.expType.kind)
6269       {
6270          case doubleType:
6271             if(exp.destType.classObjectType)
6272             {
6273                // We need to pass the address, just pass it along (Undo what was done above)
6274                if(exp.destType) exp.destType.refCount--;
6275                if(exp.expType)  exp.expType.refCount--;
6276                delete newExp;
6277             }
6278             else
6279             {
6280                // If we're looking for value:
6281                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6282                OldList * specs;
6283                OldList * unionDefs = MkList();
6284                OldList * statements = MkList();
6285                context = PushContext();
6286                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6287                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6288                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6289                exp.type = extensionCompoundExp;
6290                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6291                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6292                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6293                exp.compound.compound.context = context;
6294                PopContext(context);
6295             }
6296             break;
6297          case classType:
6298          {
6299             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6300             {
6301                exp.type = bracketsExp;
6302                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6303                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6304                ProcessExpressionType(exp.list->first);
6305                break;
6306             }
6307             else
6308             {
6309                exp.type = bracketsExp;
6310                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6311                newExp.needCast = true;
6312                ProcessExpressionType(exp.list->first);
6313                break;
6314             }
6315          }
6316          default:
6317          {
6318             if(exp.expType.kind == templateType)
6319             {
6320                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6321                if(type)
6322                {
6323                   FreeType(exp.destType);
6324                   FreeType(exp.expType);
6325                   delete newExp;
6326                   break;
6327                }
6328             }
6329             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6330             {
6331                exp.type = opExp;
6332                exp.op.op = '*';
6333                exp.op.exp1 = null;
6334                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6335                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6336             }
6337             else
6338             {
6339                char typeString[1024];
6340                Declarator decl;
6341                OldList * specs = MkList();
6342                typeString[0] = '\0';
6343                PrintType(exp.expType, typeString, false, false);
6344                decl = SpecDeclFromString(typeString, specs, null);
6345
6346                exp.type = castExp;
6347                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6348                exp.cast.typeName = MkTypeName(specs, decl);
6349                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6350                exp.cast.exp.needCast = true;
6351             }
6352             break;
6353          }
6354       }
6355    }
6356 }
6357 // TODO: The Symbol tree should be reorganized by namespaces
6358 // Name Space:
6359 //    - Tree of all symbols within (stored without namespace)
6360 //    - Tree of sub-namespaces
6361
6362 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6363 {
6364    int nsLen = strlen(nameSpace);
6365    Symbol symbol;
6366    // Start at the name space prefix
6367    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6368    {
6369       char * s = symbol.string;
6370       if(!strncmp(s, nameSpace, nsLen))
6371       {
6372          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6373          int c;
6374          char * namePart;
6375          for(c = strlen(s)-1; c >= 0; c--)
6376             if(s[c] == ':')
6377                break;
6378
6379          namePart = s+c+1;
6380          if(!strcmp(namePart, name))
6381          {
6382             // TODO: Error on ambiguity
6383             return symbol;
6384          }
6385       }
6386       else
6387          break;
6388    }
6389    return null;
6390 }
6391
6392 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6393 {
6394    int c;
6395    char nameSpace[1024];
6396    char * namePart;
6397    bool gotColon = false;
6398
6399    nameSpace[0] = '\0';
6400    for(c = strlen(name)-1; c >= 0; c--)
6401       if(name[c] == ':')
6402       {
6403          gotColon = true;
6404          break;
6405       }
6406
6407    namePart = name+c+1;
6408    while(c >= 0 && name[c] == ':') c--;
6409    if(c >= 0)
6410    {
6411       // Try an exact match first
6412       Symbol symbol = (Symbol)tree.FindString(name);
6413       if(symbol)
6414          return symbol;
6415
6416       // Namespace specified
6417       memcpy(nameSpace, name, c + 1);
6418       nameSpace[c+1] = 0;
6419
6420       return ScanWithNameSpace(tree, nameSpace, namePart);
6421    }
6422    else if(gotColon)
6423    {
6424       // Looking for a global symbol, e.g. ::Sleep()
6425       Symbol symbol = (Symbol)tree.FindString(namePart);
6426       return symbol;
6427    }
6428    else
6429    {
6430       // Name only (no namespace specified)
6431       Symbol symbol = (Symbol)tree.FindString(namePart);
6432       if(symbol)
6433          return symbol;
6434       return ScanWithNameSpace(tree, "", namePart);
6435    }
6436    return null;
6437 }
6438
6439 static void ProcessDeclaration(Declaration decl);
6440
6441 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6442 {
6443 #ifdef _DEBUG
6444    //Time startTime = GetTime();
6445 #endif
6446    // Optimize this later? Do this before/less?
6447    Context ctx;
6448    Symbol symbol = null;
6449    // First, check if the identifier is declared inside the function
6450    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6451
6452    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6453    {
6454       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6455       {
6456          symbol = null;
6457          if(thisNameSpace)
6458          {
6459             char curName[1024];
6460             strcpy(curName, thisNameSpace);
6461             strcat(curName, "::");
6462             strcat(curName, name);
6463             // Try to resolve in current namespace first
6464             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6465          }
6466          if(!symbol)
6467             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6468       }
6469       else
6470          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6471
6472       if(symbol || ctx == endContext) break;
6473    }
6474    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6475    {
6476       if(symbol.pointerExternal.type == functionExternal)
6477       {
6478          FunctionDefinition function = symbol.pointerExternal.function;
6479
6480          // Modified this recently...
6481          Context tmpContext = curContext;
6482          curContext = null;
6483          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6484          curContext = tmpContext;
6485
6486          symbol.pointerExternal.symbol = symbol;
6487
6488          // TESTING THIS:
6489          DeclareType(symbol.type, true, true);
6490
6491          ast->Insert(curExternal.prev, symbol.pointerExternal);
6492
6493          symbol.id = curExternal.symbol.idCode;
6494
6495       }
6496       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6497       {
6498          ast->Move(symbol.pointerExternal, curExternal.prev);
6499          symbol.id = curExternal.symbol.idCode;
6500       }
6501    }
6502 #ifdef _DEBUG
6503    //findSymbolTotalTime += GetTime() - startTime;
6504 #endif
6505    return symbol;
6506 }
6507
6508 static void GetTypeSpecs(Type type, OldList * specs)
6509 {
6510    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6511    switch(type.kind)
6512    {
6513       case classType:
6514       {
6515          if(type._class.registered)
6516          {
6517             if(!type._class.registered.dataType)
6518                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6519             GetTypeSpecs(type._class.registered.dataType, specs);
6520          }
6521          break;
6522       }
6523       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6524       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6525       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6526       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6527       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6528       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6529       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6530       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6531       case intType:
6532       default:
6533          ListAdd(specs, MkSpecifier(INT)); break;
6534    }
6535 }
6536
6537 static void PrintArraySize(Type arrayType, char * string)
6538 {
6539    char size[256];
6540    size[0] = '\0';
6541    strcat(size, "[");
6542    if(arrayType.enumClass)
6543       strcat(size, arrayType.enumClass.string);
6544    else if(arrayType.arraySizeExp)
6545       PrintExpression(arrayType.arraySizeExp, size);
6546    strcat(size, "]");
6547    strcat(string, size);
6548 }
6549
6550 // WARNING : This function expects a null terminated string since it recursively concatenate...
6551 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6552 {
6553    if(type)
6554    {
6555       if(printConst && type.constant)
6556          strcat(string, "const ");
6557       switch(type.kind)
6558       {
6559          case classType:
6560          {
6561             Symbol c = type._class;
6562             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6563             //       look into merging with thisclass ?
6564             if(type.classObjectType == typedObject)
6565                strcat(string, "typed_object");
6566             else if(type.classObjectType == anyObject)
6567                strcat(string, "any_object");
6568             else
6569             {
6570                if(c && c.string)
6571                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6572             }
6573             if(type.byReference)
6574                strcat(string, " &");
6575             break;
6576          }
6577          case voidType: strcat(string, "void"); break;
6578          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6579          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6580          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6581          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6582          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6583          case _BoolType: strcat(string, "_Bool"); break;
6584          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6585          case floatType: strcat(string, "float"); break;
6586          case doubleType: strcat(string, "double"); break;
6587          case structType:
6588             if(type.enumName)
6589             {
6590                strcat(string, "struct ");
6591                strcat(string, type.enumName);
6592             }
6593             else if(type.typeName)
6594                strcat(string, type.typeName);
6595             else
6596             {
6597                Type member;
6598                strcat(string, "struct { ");
6599                for(member = type.members.first; member; member = member.next)
6600                {
6601                   PrintType(member, string, true, fullName);
6602                   strcat(string,"; ");
6603                }
6604                strcat(string,"}");
6605             }
6606             break;
6607          case unionType:
6608             if(type.enumName)
6609             {
6610                strcat(string, "union ");
6611                strcat(string, type.enumName);
6612             }
6613             else if(type.typeName)
6614                strcat(string, type.typeName);
6615             else
6616             {
6617                strcat(string, "union ");
6618                strcat(string,"(unnamed)");
6619             }
6620             break;
6621          case enumType:
6622             if(type.enumName)
6623             {
6624                strcat(string, "enum ");
6625                strcat(string, type.enumName);
6626             }
6627             else if(type.typeName)
6628                strcat(string, type.typeName);
6629             else
6630                strcat(string, "int"); // "enum");
6631             break;
6632          case ellipsisType:
6633             strcat(string, "...");
6634             break;
6635          case subClassType:
6636             strcat(string, "subclass(");
6637             strcat(string, type._class ? type._class.string : "int");
6638             strcat(string, ")");
6639             break;
6640          case templateType:
6641             strcat(string, type.templateParameter.identifier.string);
6642             break;
6643          case thisClassType:
6644             strcat(string, "thisclass");
6645             break;
6646          case vaListType:
6647             strcat(string, "__builtin_va_list");
6648             break;
6649       }
6650    }
6651 }
6652
6653 static void PrintName(Type type, char * string, bool fullName)
6654 {
6655    if(type.name && type.name[0])
6656    {
6657       if(fullName)
6658          strcat(string, type.name);
6659       else
6660       {
6661          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6662          if(name) name += 2; else name = type.name;
6663          strcat(string, name);
6664       }
6665    }
6666 }
6667
6668 static void PrintAttribs(Type type, char * string)
6669 {
6670    if(type)
6671    {
6672       if(type.dllExport)   strcat(string, "dllexport ");
6673       if(type.attrStdcall) strcat(string, "stdcall ");
6674    }
6675 }
6676
6677 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6678 {
6679    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6680    {
6681       Type attrType = null;
6682       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6683          PrintAttribs(type, string);
6684       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6685          strcat(string, " const");
6686       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6687       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6688          strcat(string, " (");
6689       if(type.kind == pointerType)
6690       {
6691          if(type.type.kind == functionType || type.type.kind == methodType)
6692             PrintAttribs(type.type, string);
6693       }
6694       if(type.kind == pointerType)
6695       {
6696          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6697             strcat(string, "*");
6698          else
6699             strcat(string, " *");
6700       }
6701       if(printConst && type.constant && type.kind == pointerType)
6702          strcat(string, " const");
6703    }
6704    else
6705       PrintTypeSpecs(type, string, fullName, printConst);
6706 }
6707
6708 static void PostPrintType(Type type, char * string, bool fullName)
6709 {
6710    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6711       strcat(string, ")");
6712    if(type.kind == arrayType)
6713       PrintArraySize(type, string);
6714    else if(type.kind == functionType)
6715    {
6716       Type param;
6717       strcat(string, "(");
6718       for(param = type.params.first; param; param = param.next)
6719       {
6720          PrintType(param, string, true, fullName);
6721          if(param.next) strcat(string, ", ");
6722       }
6723       strcat(string, ")");
6724    }
6725    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6726       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6727 }
6728
6729 // *****
6730 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6731 // *****
6732 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6733 {
6734    PrePrintType(type, string, fullName, null, printConst);
6735
6736    if(type.thisClass || (printName && type.name && type.name[0]))
6737       strcat(string, " ");
6738    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6739    {
6740       Symbol _class = type.thisClass;
6741       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6742       {
6743          if(type.classObjectType == classPointer)
6744             strcat(string, "class");
6745          else
6746             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6747       }
6748       else if(_class && _class.string)
6749       {
6750          String s = _class.string;
6751          if(fullName)
6752             strcat(string, s);
6753          else
6754          {
6755             char * name = RSearchString(s, "::", strlen(s), true, false);
6756             if(name) name += 2; else name = s;
6757             strcat(string, name);
6758          }
6759       }
6760       strcat(string, "::");
6761    }
6762
6763    if(printName && type.name)
6764       PrintName(type, string, fullName);
6765    PostPrintType(type, string, fullName);
6766    if(type.bitFieldCount)
6767    {
6768       char count[100];
6769       sprintf(count, ":%d", type.bitFieldCount);
6770       strcat(string, count);
6771    }
6772 }
6773
6774 void PrintType(Type type, char * string, bool printName, bool fullName)
6775 {
6776    _PrintType(type, string, printName, fullName, true);
6777 }
6778
6779 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
6780 {
6781    _PrintType(type, string, printName, fullName, false);
6782 }
6783
6784 static Type FindMember(Type type, char * string)
6785 {
6786    Type memberType;
6787    for(memberType = type.members.first; memberType; memberType = memberType.next)
6788    {
6789       if(!memberType.name)
6790       {
6791          Type subType = FindMember(memberType, string);
6792          if(subType)
6793             return subType;
6794       }
6795       else if(!strcmp(memberType.name, string))
6796          return memberType;
6797    }
6798    return null;
6799 }
6800
6801 Type FindMemberAndOffset(Type type, char * string, uint * offset)
6802 {
6803    Type memberType;
6804    for(memberType = type.members.first; memberType; memberType = memberType.next)
6805    {
6806       if(!memberType.name)
6807       {
6808          Type subType = FindMember(memberType, string);
6809          if(subType)
6810          {
6811             *offset += memberType.offset;
6812             return subType;
6813          }
6814       }
6815       else if(!strcmp(memberType.name, string))
6816       {
6817          *offset += memberType.offset;
6818          return memberType;
6819       }
6820    }
6821    return null;
6822 }
6823
6824 public bool GetParseError() { return parseError; }
6825
6826 Expression ParseExpressionString(char * expression)
6827 {
6828    parseError = false;
6829
6830    fileInput = TempFile { };
6831    fileInput.Write(expression, 1, strlen(expression));
6832    fileInput.Seek(0, start);
6833
6834    echoOn = false;
6835    parsedExpression = null;
6836    resetScanner();
6837    expression_yyparse();
6838    delete fileInput;
6839
6840    return parsedExpression;
6841 }
6842
6843 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
6844 {
6845    Identifier id = exp.identifier;
6846    Method method = null;
6847    Property prop = null;
6848    DataMember member = null;
6849    ClassProperty classProp = null;
6850
6851    if(_class && _class.type == enumClass)
6852    {
6853       NamedLink value = null;
6854       Class enumClass = eSystem_FindClass(privateModule, "enum");
6855       if(enumClass)
6856       {
6857          Class baseClass;
6858          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
6859          {
6860             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
6861             for(value = e.values.first; value; value = value.next)
6862             {
6863                if(!strcmp(value.name, id.string))
6864                   break;
6865             }
6866             if(value)
6867             {
6868                char constant[256];
6869
6870                FreeExpContents(exp);
6871
6872                exp.type = constantExp;
6873                exp.isConstant = true;
6874                if(!strcmp(baseClass.dataTypeString, "int"))
6875                   sprintf(constant, "%d",(int)value.data);
6876                else
6877                   sprintf(constant, "0x%X",(int)value.data);
6878                exp.constant = CopyString(constant);
6879                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
6880                exp.expType = MkClassType(baseClass.fullName);
6881                break;
6882             }
6883          }
6884       }
6885       if(value)
6886          return true;
6887    }
6888    if((method = eClass_FindMethod(_class, id.string, privateModule)))
6889    {
6890       ProcessMethodType(method);
6891       exp.expType = Type
6892       {
6893          refCount = 1;
6894          kind = methodType;
6895          method = method;
6896          // Crash here?
6897          // TOCHECK: Put it back to what it was...
6898          // methodClass = _class;
6899          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
6900       };
6901       //id._class = null;
6902       return true;
6903    }
6904    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
6905    {
6906       if(!prop.dataType)
6907          ProcessPropertyType(prop);
6908       exp.expType = prop.dataType;
6909       if(prop.dataType) prop.dataType.refCount++;
6910       return true;
6911    }
6912    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
6913    {
6914       if(!member.dataType)
6915          member.dataType = ProcessTypeString(member.dataTypeString, false);
6916       exp.expType = member.dataType;
6917       if(member.dataType) member.dataType.refCount++;
6918       return true;
6919    }
6920    else if((classProp = eClass_FindClassProperty(_class, id.string)))
6921    {
6922       if(!classProp.dataType)
6923          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
6924
6925       if(classProp.constant)
6926       {
6927          FreeExpContents(exp);
6928
6929          exp.isConstant = true;
6930          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
6931          {
6932             //char constant[256];
6933             exp.type = stringExp;
6934             exp.constant = QMkString((char *)classProp.Get(_class));
6935          }
6936          else
6937          {
6938             char constant[256];
6939             exp.type = constantExp;
6940             sprintf(constant, "%d", (int)classProp.Get(_class));
6941             exp.constant = CopyString(constant);
6942          }
6943       }
6944       else
6945       {
6946          // TO IMPLEMENT...
6947       }
6948
6949       exp.expType = classProp.dataType;
6950       if(classProp.dataType) classProp.dataType.refCount++;
6951       return true;
6952    }
6953    return false;
6954 }
6955
6956 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
6957 {
6958    BinaryTree * tree = &nameSpace.functions;
6959    GlobalData data = (GlobalData)tree->FindString(name);
6960    NameSpace * child;
6961    if(!data)
6962    {
6963       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
6964       {
6965          data = ScanGlobalData(child, name);
6966          if(data)
6967             break;
6968       }
6969    }
6970    return data;
6971 }
6972
6973 static GlobalData FindGlobalData(char * name)
6974 {
6975    int start = 0, c;
6976    NameSpace * nameSpace;
6977    nameSpace = globalData;
6978    for(c = 0; name[c]; c++)
6979    {
6980       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
6981       {
6982          NameSpace * newSpace;
6983          char * spaceName = new char[c - start + 1];
6984          strncpy(spaceName, name + start, c - start);
6985          spaceName[c-start] = '\0';
6986          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
6987          delete spaceName;
6988          if(!newSpace)
6989             return null;
6990          nameSpace = newSpace;
6991          if(name[c] == ':') c++;
6992          start = c+1;
6993       }
6994    }
6995    if(c - start)
6996    {
6997       return ScanGlobalData(nameSpace, name + start);
6998    }
6999    return null;
7000 }
7001
7002 static int definedExpStackPos;
7003 static void * definedExpStack[512];
7004
7005 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7006 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7007 {
7008    Expression prev = checkedExp.prev, next = checkedExp.next;
7009
7010    FreeExpContents(checkedExp);
7011    FreeType(checkedExp.expType);
7012    FreeType(checkedExp.destType);
7013
7014    *checkedExp = *newExp;
7015
7016    delete newExp;
7017
7018    checkedExp.prev = prev;
7019    checkedExp.next = next;
7020 }
7021
7022 void ApplyAnyObjectLogic(Expression e)
7023 {
7024    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7025 #ifdef _DEBUG
7026    char debugExpString[4096];
7027    debugExpString[0] = '\0';
7028    PrintExpression(e, debugExpString);
7029 #endif
7030
7031    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7032    {
7033       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7034       //ellipsisDestType = destType;
7035       if(e && e.expType)
7036       {
7037          Type type = e.expType;
7038          Class _class = null;
7039          //Type destType = e.destType;
7040
7041          if(type.kind == classType && type._class && type._class.registered)
7042          {
7043             _class = type._class.registered;
7044          }
7045          else if(type.kind == subClassType)
7046          {
7047             _class = FindClass("ecere::com::Class").registered;
7048          }
7049          else
7050          {
7051             char string[1024] = "";
7052             Symbol classSym;
7053
7054             PrintTypeNoConst(type, string, false, true);
7055             classSym = FindClass(string);
7056             if(classSym) _class = classSym.registered;
7057          }
7058
7059          if((_class && (_class.type == enumClass || _class.type == unitClass || _class.type == bitClass || _class.type == systemClass) && strcmp(_class.fullName, "class") && strcmp(_class.fullName, "uintptr") && strcmp(_class.fullName, "intptr")) || // Patched so that class isn't considered SYSTEM...
7060             (!e.expType.classObjectType && (((type.kind != pointerType && type.kind != intPtrType && type.kind != subClassType && (type.kind != classType || !type._class || !type._class.registered || type._class.registered.type == structClass))) ||
7061             destType.byReference)))
7062          {
7063             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7064             {
7065                Expression checkedExp = e, newExp;
7066
7067                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7068                {
7069                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7070                   {
7071                      if(checkedExp.type == extensionCompoundExp)
7072                      {
7073                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7074                      }
7075                      else
7076                         checkedExp = checkedExp.list->last;
7077                   }
7078                   else if(checkedExp.type == castExp)
7079                      checkedExp = checkedExp.cast.exp;
7080                }
7081
7082                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7083                {
7084                   newExp = checkedExp.op.exp2;
7085                   checkedExp.op.exp2 = null;
7086                   FreeExpContents(checkedExp);
7087
7088                   if(e.expType && e.expType.passAsTemplate)
7089                   {
7090                      char size[100];
7091                      ComputeTypeSize(e.expType);
7092                      sprintf(size, "%d", e.expType.size);
7093                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7094                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7095                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7096                   }
7097
7098                   ReplaceExpContents(checkedExp, newExp);
7099                   e.byReference = true;
7100                }
7101                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7102                {
7103                   Expression checkedExp, newExp;
7104
7105                   {
7106                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7107                      bool hasAddress =
7108                         e.type == identifierExp ||
7109                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7110                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7111                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7112                         e.type == indexExp;
7113
7114                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7115                      {
7116                         Context context = PushContext();
7117                         Declarator decl;
7118                         OldList * specs = MkList();
7119                         char typeString[1024];
7120                         Expression newExp { };
7121
7122                         typeString[0] = '\0';
7123                         *newExp = *e;
7124
7125                         //if(e.destType) e.destType.refCount++;
7126                         // if(exp.expType) exp.expType.refCount++;
7127                         newExp.prev = null;
7128                         newExp.next = null;
7129                         newExp.expType = null;
7130
7131                         PrintTypeNoConst(e.expType, typeString, false, true);
7132                         decl = SpecDeclFromString(typeString, specs, null);
7133                         newExp.destType = ProcessType(specs, decl);
7134
7135                         curContext = context;
7136
7137                         // We need a current compound for this
7138                         if(curCompound)
7139                         {
7140                            char name[100];
7141                            OldList * stmts = MkList();
7142                            e.type = extensionCompoundExp;
7143                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7144                            if(!curCompound.compound.declarations)
7145                               curCompound.compound.declarations = MkList();
7146                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7147                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7148                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7149                            e.compound = MkCompoundStmt(null, stmts);
7150                         }
7151                         else
7152                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7153
7154                         /*
7155                         e.compound = MkCompoundStmt(
7156                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7157                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7158
7159                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7160                         */
7161
7162                         {
7163                            Type type = e.destType;
7164                            e.destType = { };
7165                            CopyTypeInto(e.destType, type);
7166                            e.destType.refCount = 1;
7167                            e.destType.classObjectType = none;
7168                            FreeType(type);
7169                         }
7170
7171                         e.compound.compound.context = context;
7172                         PopContext(context);
7173                         curContext = context.parent;
7174                      }
7175                   }
7176
7177                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7178                   checkedExp = e;
7179                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7180                   {
7181                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7182                      {
7183                         if(checkedExp.type == extensionCompoundExp)
7184                         {
7185                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7186                         }
7187                         else
7188                            checkedExp = checkedExp.list->last;
7189                      }
7190                      else if(checkedExp.type == castExp)
7191                         checkedExp = checkedExp.cast.exp;
7192                   }
7193                   {
7194                      Expression operand { };
7195                      operand = *checkedExp;
7196                      checkedExp.destType = null;
7197                      checkedExp.expType = null;
7198                      checkedExp.Clear();
7199                      checkedExp.type = opExp;
7200                      checkedExp.op.op = '&';
7201                      checkedExp.op.exp1 = null;
7202                      checkedExp.op.exp2 = operand;
7203
7204                      //newExp = MkExpOp(null, '&', checkedExp);
7205                   }
7206                   //ReplaceExpContents(checkedExp, newExp);
7207                }
7208             }
7209          }
7210       }
7211    }
7212    {
7213       // If expression type is a simple class, make it an address
7214       // FixReference(e, true);
7215    }
7216 //#if 0
7217    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7218       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7219          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7220    {
7221       if(e.expType.classObjectType && destType && destType.classObjectType) //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class"))
7222       {
7223          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7224       }
7225       else
7226       {
7227          Expression thisExp { };
7228
7229          *thisExp = *e;
7230          thisExp.prev = null;
7231          thisExp.next = null;
7232          e.Clear();
7233
7234          e.type = bracketsExp;
7235          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7236          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7237             ((Expression)e.list->first).byReference = true;
7238
7239          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7240          {
7241             e.expType = thisExp.expType;
7242             e.expType.refCount++;
7243          }
7244          else*/
7245          {
7246             e.expType = { };
7247             CopyTypeInto(e.expType, thisExp.expType);
7248             e.expType.byReference = false;
7249             e.expType.refCount = 1;
7250
7251             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7252                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7253             {
7254                e.expType.classObjectType = none;
7255             }
7256          }
7257       }
7258    }
7259 // TOFIX: Try this for a nice IDE crash!
7260 //#endif
7261    // The other way around
7262    else
7263 //#endif
7264    if(destType && e.expType &&
7265          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7266          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7267          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7268    {
7269       if(destType.kind == ellipsisType)
7270       {
7271          Compiler_Error($"Unspecified type\n");
7272       }
7273       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7274       {
7275          bool byReference = e.expType.byReference;
7276          Expression thisExp { };
7277          Declarator decl;
7278          OldList * specs = MkList();
7279          char typeString[1024]; // Watch buffer overruns
7280          Type type;
7281          ClassObjectType backupClassObjectType;
7282          bool backupByReference;
7283
7284          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7285             type = e.expType;
7286          else
7287             type = destType;
7288
7289          backupClassObjectType = type.classObjectType;
7290          backupByReference = type.byReference;
7291
7292          type.classObjectType = none;
7293          type.byReference = false;
7294
7295          typeString[0] = '\0';
7296          PrintType(type, typeString, false, true);
7297          decl = SpecDeclFromString(typeString, specs, null);
7298
7299          type.classObjectType = backupClassObjectType;
7300          type.byReference = backupByReference;
7301
7302          *thisExp = *e;
7303          thisExp.prev = null;
7304          thisExp.next = null;
7305          e.Clear();
7306
7307          if( ( type.kind == classType && type._class && type._class.registered &&
7308                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7309                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7310              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7311              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7312          {
7313             e.type = opExp;
7314             e.op.op = '*';
7315             e.op.exp1 = null;
7316             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7317
7318             e.expType = { };
7319             CopyTypeInto(e.expType, type);
7320             e.expType.byReference = false;
7321             e.expType.refCount = 1;
7322          }
7323          else
7324          {
7325             e.type = castExp;
7326             e.cast.typeName = MkTypeName(specs, decl);
7327             e.cast.exp = thisExp;
7328             e.byReference = true;
7329             e.expType = type;
7330             type.refCount++;
7331          }
7332          e.destType = destType;
7333          destType.refCount++;
7334       }
7335    }
7336 }
7337
7338 void ProcessExpressionType(Expression exp)
7339 {
7340    bool unresolved = false;
7341    Location oldyylloc = yylloc;
7342    bool notByReference = false;
7343 #ifdef _DEBUG
7344    char debugExpString[4096];
7345    debugExpString[0] = '\0';
7346    PrintExpression(exp, debugExpString);
7347 #endif
7348    if(!exp || exp.expType)
7349       return;
7350
7351    //eSystem_Logf("%s\n", expString);
7352
7353    // Testing this here
7354    yylloc = exp.loc;
7355    switch(exp.type)
7356    {
7357       case identifierExp:
7358       {
7359          Identifier id = exp.identifier;
7360          if(!id || !topContext) return;
7361
7362          // DOING THIS LATER NOW...
7363          if(id._class && id._class.name)
7364          {
7365             id.classSym = id._class.symbol; // FindClass(id._class.name);
7366             /* TODO: Name Space Fix ups
7367             if(!id.classSym)
7368                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7369             */
7370          }
7371
7372          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7373          {
7374             exp.expType = ProcessTypeString("Module", true);
7375             break;
7376          }
7377          else */if(strstr(id.string, "__ecereClass") == id.string)
7378          {
7379             exp.expType = ProcessTypeString("ecere::com::Class", true);
7380             break;
7381          }
7382          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7383          {
7384             // Added this here as well
7385             ReplaceClassMembers(exp, thisClass);
7386             if(exp.type != identifierExp)
7387             {
7388                ProcessExpressionType(exp);
7389                break;
7390             }
7391
7392             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7393                break;
7394          }
7395          else
7396          {
7397             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7398             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7399             if(!symbol/* && exp.destType*/)
7400             {
7401                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7402                   break;
7403                else
7404                {
7405                   if(thisClass)
7406                   {
7407                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7408                      if(exp.type != identifierExp)
7409                      {
7410                         ProcessExpressionType(exp);
7411                         break;
7412                      }
7413                   }
7414                   // Static methods called from inside the _class
7415                   else if(currentClass && !id._class)
7416                   {
7417                      if(ResolveIdWithClass(exp, currentClass, true))
7418                         break;
7419                   }
7420                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7421                }
7422             }
7423
7424             // If we manage to resolve this symbol
7425             if(symbol)
7426             {
7427                Type type = symbol.type;
7428                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7429
7430                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7431                {
7432                   Context context = SetupTemplatesContext(_class);
7433                   type = ReplaceThisClassType(_class);
7434                   FinishTemplatesContext(context);
7435                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7436                }
7437
7438                FreeSpecifier(id._class);
7439                id._class = null;
7440                delete id.string;
7441                id.string = CopyString(symbol.string);
7442
7443                id.classSym = null;
7444                exp.expType = type;
7445                if(type)
7446                   type.refCount++;
7447                if(type && (type.kind == enumType || (_class && _class.type == enumClass)))
7448                   // Add missing cases here... enum Classes...
7449                   exp.isConstant = true;
7450
7451                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7452                if(symbol.isParam || !strcmp(id.string, "this"))
7453                {
7454                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7455                      exp.byReference = true;
7456
7457                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7458                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7459                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7460                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7461                   {
7462                      Identifier id = exp.identifier;
7463                      exp.type = bracketsExp;
7464                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7465                   }*/
7466                }
7467
7468                if(symbol.isIterator)
7469                {
7470                   if(symbol.isIterator == 3)
7471                   {
7472                      exp.type = bracketsExp;
7473                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7474                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7475                      exp.expType = null;
7476                      ProcessExpressionType(exp);
7477                   }
7478                   else if(symbol.isIterator != 4)
7479                   {
7480                      exp.type = memberExp;
7481                      exp.member.exp = MkExpIdentifier(exp.identifier);
7482                      exp.member.exp.expType = exp.expType;
7483                      /*if(symbol.isIterator == 6)
7484                         exp.member.member = MkIdentifier("key");
7485                      else*/
7486                         exp.member.member = MkIdentifier("data");
7487                      exp.expType = null;
7488                      ProcessExpressionType(exp);
7489                   }
7490                }
7491                break;
7492             }
7493             else
7494             {
7495                DefinedExpression definedExp = null;
7496                if(thisNameSpace && !(id._class && !id._class.name))
7497                {
7498                   char name[1024];
7499                   strcpy(name, thisNameSpace);
7500                   strcat(name, "::");
7501                   strcat(name, id.string);
7502                   definedExp = eSystem_FindDefine(privateModule, name);
7503                }
7504                if(!definedExp)
7505                   definedExp = eSystem_FindDefine(privateModule, id.string);
7506                if(definedExp)
7507                {
7508                   int c;
7509                   for(c = 0; c<definedExpStackPos; c++)
7510                      if(definedExpStack[c] == definedExp)
7511                         break;
7512                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7513                   {
7514                      Location backupYylloc = yylloc;
7515                      File backInput = fileInput;
7516                      definedExpStack[definedExpStackPos++] = definedExp;
7517
7518                      fileInput = TempFile { };
7519                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7520                      fileInput.Seek(0, start);
7521
7522                      echoOn = false;
7523                      parsedExpression = null;
7524                      resetScanner();
7525                      expression_yyparse();
7526                      delete fileInput;
7527                      if(backInput)
7528                         fileInput = backInput;
7529
7530                      yylloc = backupYylloc;
7531
7532                      if(parsedExpression)
7533                      {
7534                         FreeIdentifier(id);
7535                         exp.type = bracketsExp;
7536                         exp.list = MkListOne(parsedExpression);
7537                         parsedExpression.loc = yylloc;
7538                         ProcessExpressionType(exp);
7539                         definedExpStackPos--;
7540                         return;
7541                      }
7542                      definedExpStackPos--;
7543                   }
7544                   else
7545                   {
7546                      if(inCompiler)
7547                      {
7548                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7549                      }
7550                   }
7551                }
7552                else
7553                {
7554                   GlobalData data = null;
7555                   if(thisNameSpace && !(id._class && !id._class.name))
7556                   {
7557                      char name[1024];
7558                      strcpy(name, thisNameSpace);
7559                      strcat(name, "::");
7560                      strcat(name, id.string);
7561                      data = FindGlobalData(name);
7562                   }
7563                   if(!data)
7564                      data = FindGlobalData(id.string);
7565                   if(data)
7566                   {
7567                      DeclareGlobalData(data);
7568                      exp.expType = data.dataType;
7569                      if(data.dataType) data.dataType.refCount++;
7570
7571                      delete id.string;
7572                      id.string = CopyString(data.fullName);
7573                      FreeSpecifier(id._class);
7574                      id._class = null;
7575
7576                      break;
7577                   }
7578                   else
7579                   {
7580                      GlobalFunction function = null;
7581                      if(thisNameSpace && !(id._class && !id._class.name))
7582                      {
7583                         char name[1024];
7584                         strcpy(name, thisNameSpace);
7585                         strcat(name, "::");
7586                         strcat(name, id.string);
7587                         function = eSystem_FindFunction(privateModule, name);
7588                      }
7589                      if(!function)
7590                         function = eSystem_FindFunction(privateModule, id.string);
7591                      if(function)
7592                      {
7593                         char name[1024];
7594                         delete id.string;
7595                         id.string = CopyString(function.name);
7596                         name[0] = 0;
7597
7598                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7599                            strcpy(name, "__ecereFunction_");
7600                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7601                         if(DeclareFunction(function, name))
7602                         {
7603                            delete id.string;
7604                            id.string = CopyString(name);
7605                         }
7606                         exp.expType = function.dataType;
7607                         if(function.dataType) function.dataType.refCount++;
7608
7609                         FreeSpecifier(id._class);
7610                         id._class = null;
7611
7612                         break;
7613                      }
7614                   }
7615                }
7616             }
7617          }
7618          unresolved = true;
7619          break;
7620       }
7621       case instanceExp:
7622       {
7623          Class _class;
7624          // Symbol classSym;
7625
7626          if(!exp.instance._class)
7627          {
7628             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7629             {
7630                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7631             }
7632          }
7633
7634          //classSym = FindClass(exp.instance._class.fullName);
7635          //_class = classSym ? classSym.registered : null;
7636
7637          ProcessInstantiationType(exp.instance);
7638          exp.isConstant = exp.instance.isConstant;
7639
7640          /*
7641          if(_class.type == unitClass && _class.base.type != systemClass)
7642          {
7643             {
7644                Type destType = exp.destType;
7645
7646                exp.destType = MkClassType(_class.base.fullName);
7647                exp.expType = MkClassType(_class.fullName);
7648                CheckExpressionType(exp, exp.destType, true);
7649
7650                exp.destType = destType;
7651             }
7652             exp.expType = MkClassType(_class.fullName);
7653          }
7654          else*/
7655          if(exp.instance._class)
7656          {
7657             exp.expType = MkClassType(exp.instance._class.name);
7658             /*if(exp.expType._class && exp.expType._class.registered &&
7659                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7660                exp.expType.byReference = true;*/
7661          }
7662          break;
7663       }
7664       case constantExp:
7665       {
7666          if(!exp.expType)
7667          {
7668             char * constant = exp.constant;
7669             Type type
7670             {
7671                refCount = 1;
7672                constant = true;
7673             };
7674             exp.expType = type;
7675
7676             if(constant[0] == '\'')
7677             {
7678                if((int)((byte *)constant)[1] > 127)
7679                {
7680                   int nb;
7681                   unichar ch = UTF8GetChar(constant + 1, &nb);
7682                   if(nb < 2) ch = constant[1];
7683                   delete constant;
7684                   exp.constant = PrintUInt(ch);
7685                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7686                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7687                   type._class = FindClass("unichar");
7688
7689                   type.isSigned = false;
7690                }
7691                else
7692                {
7693                   type.kind = charType;
7694                   type.isSigned = true;
7695                }
7696             }
7697             else
7698             {
7699                char * dot = strchr(constant, '.');
7700                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
7701                char * exponent;
7702                if(isHex)
7703                {
7704                   exponent = strchr(constant, 'p');
7705                   if(!exponent) exponent = strchr(constant, 'P');
7706                }
7707                else
7708                {
7709                   exponent = strchr(constant, 'e');
7710                   if(!exponent) exponent = strchr(constant, 'E');
7711                }
7712
7713                if(dot || exponent)
7714                {
7715                   if(strchr(constant, 'f') || strchr(constant, 'F'))
7716                      type.kind = floatType;
7717                   else
7718                      type.kind = doubleType;
7719                   type.isSigned = true;
7720                }
7721                else
7722                {
7723                   bool isSigned = constant[0] == '-';
7724                   int64 i64 = strtoll(constant, null, 0);
7725                   uint64 ui64 = strtoull(constant, null, 0);
7726                   bool is64Bit = false;
7727                   if(isSigned)
7728                   {
7729                      if(i64 < MININT)
7730                         is64Bit = true;
7731                   }
7732                   else
7733                   {
7734                      if(ui64 > MAXINT)
7735                      {
7736                         if(ui64 > MAXDWORD)
7737                         {
7738                            is64Bit = true;
7739                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
7740                               isSigned = true;
7741                         }
7742                      }
7743                      else if(constant[0] != '0' || !constant[1])
7744                         isSigned = true;
7745                   }
7746                   type.kind = is64Bit ? int64Type : intType;
7747                   type.isSigned = isSigned;
7748                }
7749             }
7750             exp.isConstant = true;
7751             if(exp.destType && exp.destType.kind == doubleType)
7752                type.kind = doubleType;
7753             else if(exp.destType && exp.destType.kind == floatType)
7754                type.kind = floatType;
7755             else if(exp.destType && exp.destType.kind == int64Type)
7756                type.kind = int64Type;
7757          }
7758          break;
7759       }
7760       case stringExp:
7761       {
7762          exp.isConstant = true;      // Why wasn't this constant?
7763          exp.expType = Type
7764          {
7765             refCount = 1;
7766             kind = pointerType;
7767             type = Type
7768             {
7769                refCount = 1;
7770                kind = charType;
7771                constant = true;
7772                isSigned = true;
7773             }
7774          };
7775          break;
7776       }
7777       case newExp:
7778       case new0Exp:
7779          ProcessExpressionType(exp._new.size);
7780          exp.expType = Type
7781          {
7782             refCount = 1;
7783             kind = pointerType;
7784             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
7785          };
7786          DeclareType(exp.expType.type, false, false);
7787          break;
7788       case renewExp:
7789       case renew0Exp:
7790          ProcessExpressionType(exp._renew.size);
7791          ProcessExpressionType(exp._renew.exp);
7792          exp.expType = Type
7793          {
7794             refCount = 1;
7795             kind = pointerType;
7796             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
7797          };
7798          DeclareType(exp.expType.type, false, false);
7799          break;
7800       case opExp:
7801       {
7802          bool assign = false, boolResult = false, boolOps = false;
7803          Type type1 = null, type2 = null;
7804          bool useDestType = false, useSideType = false;
7805          Location oldyylloc = yylloc;
7806          bool useSideUnit = false;
7807
7808          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
7809          Type dummy
7810          {
7811             count = 1;
7812             refCount = 1;
7813          };
7814
7815          switch(exp.op.op)
7816          {
7817             // Assignment Operators
7818             case '=':
7819             case MUL_ASSIGN:
7820             case DIV_ASSIGN:
7821             case MOD_ASSIGN:
7822             case ADD_ASSIGN:
7823             case SUB_ASSIGN:
7824             case LEFT_ASSIGN:
7825             case RIGHT_ASSIGN:
7826             case AND_ASSIGN:
7827             case XOR_ASSIGN:
7828             case OR_ASSIGN:
7829                assign = true;
7830                break;
7831             // boolean Operators
7832             case '!':
7833                // Expect boolean operators
7834                //boolOps = true;
7835                //boolResult = true;
7836                break;
7837             case AND_OP:
7838             case OR_OP:
7839                // Expect boolean operands
7840                boolOps = true;
7841                boolResult = true;
7842                break;
7843             // Comparisons
7844             case EQ_OP:
7845             case '<':
7846             case '>':
7847             case LE_OP:
7848             case GE_OP:
7849             case NE_OP:
7850                // Gives boolean result
7851                boolResult = true;
7852                useSideType = true;
7853                break;
7854             case '+':
7855             case '-':
7856                useSideUnit = true;
7857
7858                // Just added these... testing
7859             case '|':
7860             case '&':
7861             case '^':
7862
7863             // DANGER: Verify units
7864             case '/':
7865             case '%':
7866             case '*':
7867
7868                if(exp.op.op != '*' || exp.op.exp1)
7869                {
7870                   useSideType = true;
7871                   useDestType = true;
7872                }
7873                break;
7874
7875             /*// Implement speed etc.
7876             case '*':
7877             case '/':
7878                break;
7879             */
7880          }
7881          if(exp.op.op == '&')
7882          {
7883             // Added this here earlier for Iterator address as key
7884             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
7885             {
7886                Identifier id = exp.op.exp2.identifier;
7887                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
7888                if(symbol && symbol.isIterator == 2)
7889                {
7890                   exp.type = memberExp;
7891                   exp.member.exp = exp.op.exp2;
7892                   exp.member.member = MkIdentifier("key");
7893                   exp.expType = null;
7894                   exp.op.exp2.expType = symbol.type;
7895                   symbol.type.refCount++;
7896                   ProcessExpressionType(exp);
7897                   FreeType(dummy);
7898                   break;
7899                }
7900                // exp.op.exp2.usage.usageRef = true;
7901             }
7902          }
7903
7904          //dummy.kind = TypeDummy;
7905
7906          if(exp.op.exp1)
7907          {
7908             if(exp.destType && exp.destType.kind == classType &&
7909                exp.destType._class && exp.destType._class.registered && useDestType &&
7910
7911               ((exp.destType._class.registered.type == unitClass && useSideUnit) ||
7912                exp.destType._class.registered.type == enumClass ||
7913                exp.destType._class.registered.type == bitClass
7914                ))
7915
7916               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
7917             {
7918                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7919                exp.op.exp1.destType = exp.destType;
7920                if(exp.destType)
7921                   exp.destType.refCount++;
7922             }
7923             else if(!assign)
7924             {
7925                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
7926                exp.op.exp1.destType = dummy;
7927                dummy.refCount++;
7928             }
7929
7930             // TESTING THIS HERE...
7931             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
7932             ProcessExpressionType(exp.op.exp1);
7933             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
7934
7935             if(exp.op.exp1.destType == dummy)
7936             {
7937                FreeType(dummy);
7938                exp.op.exp1.destType = null;
7939             }
7940             type1 = exp.op.exp1.expType;
7941          }
7942
7943          if(exp.op.exp2)
7944          {
7945             char expString[10240];
7946             expString[0] = '\0';
7947             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
7948             {
7949                if(exp.op.exp1)
7950                {
7951                   exp.op.exp2.destType = exp.op.exp1.expType;
7952                   if(exp.op.exp1.expType)
7953                      exp.op.exp1.expType.refCount++;
7954                }
7955                else
7956                {
7957                   exp.op.exp2.destType = exp.destType;
7958                   if(exp.destType)
7959                      exp.destType.refCount++;
7960                }
7961
7962                if(type1) type1.refCount++;
7963                exp.expType = type1;
7964             }
7965             else if(assign)
7966             {
7967                if(inCompiler)
7968                   PrintExpression(exp.op.exp2, expString);
7969
7970                if(type1 && type1.kind == pointerType)
7971                {
7972                   if(exp.op.op == MUL_ASSIGN || exp.op.op == DIV_ASSIGN ||exp.op.op == MOD_ASSIGN ||exp.op.op == LEFT_ASSIGN ||exp.op.op == RIGHT_ASSIGN ||
7973                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
7974                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
7975                   else if(exp.op.op == '=')
7976                   {
7977                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7978                      exp.op.exp2.destType = type1;
7979                      if(type1)
7980                         type1.refCount++;
7981                   }
7982                }
7983                else
7984                {
7985                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
7986                   if(exp.op.op == MUL_ASSIGN || exp.op.op == DIV_ASSIGN ||exp.op.op == MOD_ASSIGN ||exp.op.op == LEFT_ASSIGN ||exp.op.op == RIGHT_ASSIGN/* ||
7987                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
7988                   else
7989                   {
7990                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
7991                      exp.op.exp2.destType = type1;
7992                      if(type1)
7993                         type1.refCount++;
7994                   }
7995                }
7996                if(type1) type1.refCount++;
7997                exp.expType = type1;
7998             }
7999             else if(exp.destType && exp.destType.kind == classType &&
8000                exp.destType._class && exp.destType._class.registered &&
8001
8002                   ((exp.destType._class.registered.type == unitClass && useDestType && useSideUnit) ||
8003                   (exp.destType._class.registered.type == enumClass && useDestType))
8004                   )
8005             {
8006                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8007                exp.op.exp2.destType = exp.destType;
8008                if(exp.destType)
8009                   exp.destType.refCount++;
8010             }
8011             else
8012             {
8013                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8014                exp.op.exp2.destType = dummy;
8015                dummy.refCount++;
8016             }
8017
8018             // TESTING THIS HERE... (DANGEROUS)
8019             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8020                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8021             {
8022                FreeType(exp.op.exp2.destType);
8023                exp.op.exp2.destType = type1;
8024                type1.refCount++;
8025             }
8026             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8027             // Cannot lose the cast on a sizeof
8028             if(exp.op.op == SIZEOF)
8029             {
8030                Expression e = exp.op.exp2;
8031                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8032                {
8033                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8034                   {
8035                      if(e.type == extensionCompoundExp)
8036                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8037                      else
8038                         e = e.list->last;
8039                   }
8040                }
8041                if(e.type == castExp && e.cast.exp)
8042                   e.cast.exp.needCast = true;
8043             }
8044             ProcessExpressionType(exp.op.exp2);
8045             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8046
8047             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8048             {
8049                if(exp.op.exp2.expType.kind == intSizeType || exp.op.exp2.expType.kind == intPtrType || exp.op.exp2.expType.kind == int64Type || exp.op.exp2.expType.kind == intType || exp.op.exp2.expType.kind == shortType || exp.op.exp2.expType.kind == charType)
8050                {
8051                   if(exp.op.op != '=' && type1.type.kind == voidType)
8052                      Compiler_Error($"void *: unknown size\n");
8053                }
8054                else if(exp.op.exp2.expType.kind == pointerType || exp.op.exp2.expType.kind == arrayType || exp.op.exp2.expType.kind == functionType || exp.op.exp2.expType.kind == methodType||
8055                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8056                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8057                               exp.op.exp2.expType._class.registered.type == structClass ||
8058                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8059                {
8060                   if(exp.op.op == ADD_ASSIGN)
8061                      Compiler_Error($"cannot add two pointers\n");
8062                }
8063                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8064                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8065                {
8066                   if(exp.op.op == ADD_ASSIGN)
8067                      Compiler_Error($"cannot add two pointers\n");
8068                }
8069                else if(inCompiler)
8070                {
8071                   char type1String[1024];
8072                   char type2String[1024];
8073                   type1String[0] = '\0';
8074                   type2String[0] = '\0';
8075
8076                   PrintType(exp.op.exp2.expType, type1String, false, true);
8077                   PrintType(type1, type2String, false, true);
8078                   ChangeCh(expString, '\n', ' ');
8079                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8080                }
8081             }
8082
8083             if(exp.op.exp2.destType == dummy)
8084             {
8085                FreeType(dummy);
8086                exp.op.exp2.destType = null;
8087             }
8088
8089             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8090             {
8091                type2 = { };
8092                type2.refCount = 1;
8093                CopyTypeInto(type2, exp.op.exp2.expType);
8094                type2.isSigned = true;
8095             }
8096             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8097             {
8098                type2 = { kind = intType };
8099                type2.refCount = 1;
8100                type2.isSigned = true;
8101             }
8102             else
8103             {
8104                type2 = exp.op.exp2.expType;
8105                if(type2) type2.refCount++;
8106             }
8107          }
8108
8109          dummy.kind = voidType;
8110
8111          if(exp.op.op == SIZEOF)
8112          {
8113             exp.expType = Type
8114             {
8115                refCount = 1;
8116                kind = intType;
8117             };
8118             exp.isConstant = true;
8119          }
8120          // Get type of dereferenced pointer
8121          else if(exp.op.op == '*' && !exp.op.exp1)
8122          {
8123             exp.expType = Dereference(type2);
8124             if(type2 && type2.kind == classType)
8125                notByReference = true;
8126          }
8127          else if(exp.op.op == '&' && !exp.op.exp1)
8128             exp.expType = Reference(type2);
8129          else if(!assign)
8130          {
8131             if(boolOps)
8132             {
8133                if(exp.op.exp1)
8134                {
8135                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8136                   exp.op.exp1.destType = MkClassType("bool");
8137                   exp.op.exp1.destType.truth = true;
8138                   if(!exp.op.exp1.expType)
8139                      ProcessExpressionType(exp.op.exp1);
8140                   else
8141                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8142                   FreeType(exp.op.exp1.expType);
8143                   exp.op.exp1.expType = MkClassType("bool");
8144                   exp.op.exp1.expType.truth = true;
8145                }
8146                if(exp.op.exp2)
8147                {
8148                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8149                   exp.op.exp2.destType = MkClassType("bool");
8150                   exp.op.exp2.destType.truth = true;
8151                   if(!exp.op.exp2.expType)
8152                      ProcessExpressionType(exp.op.exp2);
8153                   else
8154                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8155                   FreeType(exp.op.exp2.expType);
8156                   exp.op.exp2.expType = MkClassType("bool");
8157                   exp.op.exp2.expType.truth = true;
8158                }
8159             }
8160             else if(exp.op.exp1 && exp.op.exp2 &&
8161                ((useSideType /*&&
8162                      (useSideUnit ||
8163                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8164                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8165                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8166                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8167             {
8168                if(type1 && type2 &&
8169                   // If either both are class or both are not class
8170                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8171                {
8172                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8173                   exp.op.exp2.destType = type1;
8174                   type1.refCount++;
8175                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8176                   exp.op.exp1.destType = type2;
8177                   type2.refCount++;
8178                   // Warning here for adding Radians + Degrees with no destination type
8179                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8180                      type1._class.registered && type1._class.registered.type == unitClass &&
8181                      type2._class.registered && type2._class.registered.type == unitClass &&
8182                      type1._class.registered != type2._class.registered)
8183                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8184                         type1._class.string, type2._class.string, type1._class.string);
8185
8186                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8187                   {
8188                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8189                      if(argExp)
8190                      {
8191                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8192
8193                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8194                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8195                            exp.op.exp1)));
8196
8197                         ProcessExpressionType(exp.op.exp1);
8198
8199                         if(type2.kind != pointerType)
8200                         {
8201                            ProcessExpressionType(classExp);
8202
8203                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*',
8204                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8205                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8206                                  // noHeadClass
8207                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8208                                     OR_OP,
8209                                  // normalClass
8210                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8211                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8212                                        MkPointer(null, null), null)))),
8213                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8214
8215                            if(!exp.op.exp2.expType)
8216                            {
8217                               if(type2)
8218                                  FreeType(type2);
8219                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8220                               type2.refCount++;
8221                            }
8222
8223                            ProcessExpressionType(exp.op.exp2);
8224                         }
8225                      }
8226                   }
8227
8228                   if(!boolResult && ((type1.kind == pointerType || type1.kind == arrayType || (type1.kind == classType && !strcmp(type1._class.string, "String"))) && (type2.kind == intSizeType || type2.kind == intPtrType || type2.kind == int64Type || type2.kind == intType || type2.kind == shortType || type2.kind == charType)))
8229                   {
8230                      if(type1.kind != classType && type1.type.kind == voidType)
8231                         Compiler_Error($"void *: unknown size\n");
8232                      exp.expType = type1;
8233                      if(type1) type1.refCount++;
8234                   }
8235                   else if(!boolResult && ((type2.kind == pointerType || type2.kind == arrayType || (type2.kind == classType && !strcmp(type2._class.string, "String"))) && (type1.kind == intSizeType || type1.kind == intPtrType || type1.kind == int64Type || type1.kind == intType || type1.kind == shortType || type1.kind == charType)))
8236                   {
8237                      if(type2.kind != classType && type2.type.kind == voidType)
8238                         Compiler_Error($"void *: unknown size\n");
8239                      exp.expType = type2;
8240                      if(type2) type2.refCount++;
8241                   }
8242                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8243                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8244                   {
8245                      Compiler_Warning($"different levels of indirection\n");
8246                   }
8247                   else
8248                   {
8249                      bool success = false;
8250                      if(type1.kind == pointerType && type2.kind == pointerType)
8251                      {
8252                         if(exp.op.op == '+')
8253                            Compiler_Error($"cannot add two pointers\n");
8254                         else if(exp.op.op == '-')
8255                         {
8256                            // Pointer Subtraction gives integer
8257                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8258                            {
8259                               exp.expType = Type
8260                               {
8261                                  kind = intType;
8262                                  refCount = 1;
8263                               };
8264                               success = true;
8265
8266                               if(type1.type.kind == templateType)
8267                               {
8268                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8269                                  if(argExp)
8270                                  {
8271                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8272
8273                                     ProcessExpressionType(classExp);
8274
8275                                     exp.type = bracketsExp;
8276                                     exp.list = MkListOne(MkExpOp(
8277                                        MkExpBrackets(MkListOne(MkExpOp(
8278                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8279                                              , exp.op.op,
8280                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8281
8282                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8283
8284                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8285                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8286                                                 // noHeadClass
8287                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8288                                                    OR_OP,
8289                                                 // normalClass
8290                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8291                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8292                                                       MkPointer(null, null), null)))),
8293                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8294
8295
8296                                              ));
8297
8298                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8299                                     FreeType(dummy);
8300                                     return;
8301                                  }
8302                               }
8303                            }
8304                         }
8305                      }
8306
8307                      if(!success && exp.op.exp1.type == constantExp)
8308                      {
8309                         // If first expression is constant, try to match that first
8310                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8311                         {
8312                            if(exp.expType) FreeType(exp.expType);
8313                            exp.expType = exp.op.exp1.destType;
8314                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8315                            success = true;
8316                         }
8317                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8318                         {
8319                            if(exp.expType) FreeType(exp.expType);
8320                            exp.expType = exp.op.exp2.destType;
8321                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8322                            success = true;
8323                         }
8324                      }
8325                      else if(!success)
8326                      {
8327                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8328                         {
8329                            if(exp.expType) FreeType(exp.expType);
8330                            exp.expType = exp.op.exp2.destType;
8331                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8332                            success = true;
8333                         }
8334                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8335                         {
8336                            if(exp.expType) FreeType(exp.expType);
8337                            exp.expType = exp.op.exp1.destType;
8338                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8339                            success = true;
8340                         }
8341                      }
8342                      if(!success)
8343                      {
8344                         char expString1[10240];
8345                         char expString2[10240];
8346                         char type1[1024];
8347                         char type2[1024];
8348                         expString1[0] = '\0';
8349                         expString2[0] = '\0';
8350                         type1[0] = '\0';
8351                         type2[0] = '\0';
8352                         if(inCompiler)
8353                         {
8354                            PrintExpression(exp.op.exp1, expString1);
8355                            ChangeCh(expString1, '\n', ' ');
8356                            PrintExpression(exp.op.exp2, expString2);
8357                            ChangeCh(expString2, '\n', ' ');
8358                            PrintType(exp.op.exp1.expType, type1, false, true);
8359                            PrintType(exp.op.exp2.expType, type2, false, true);
8360                         }
8361
8362                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8363                      }
8364                   }
8365                }
8366                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8367                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8368                {
8369                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8370                   // Convert e.g. / 4 into / 4.0
8371                   exp.op.exp1.destType = type2._class.registered.dataType;
8372                   if(type2._class.registered.dataType)
8373                      type2._class.registered.dataType.refCount++;
8374                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8375                   exp.expType = type2;
8376                   if(type2) type2.refCount++;
8377                }
8378                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8379                {
8380                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8381                   // Convert e.g. / 4 into / 4.0
8382                   exp.op.exp2.destType = type1._class.registered.dataType;
8383                   if(type1._class.registered.dataType)
8384                      type1._class.registered.dataType.refCount++;
8385                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8386                   exp.expType = type1;
8387                   if(type1) type1.refCount++;
8388                }
8389                else if(type1)
8390                {
8391                   bool valid = false;
8392
8393                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8394                   {
8395                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8396
8397                      if(!type1._class.registered.dataType)
8398                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8399                      exp.op.exp2.destType = type1._class.registered.dataType;
8400                      exp.op.exp2.destType.refCount++;
8401
8402                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8403                      if(type2)
8404                         FreeType(type2);
8405                      type2 = exp.op.exp2.destType;
8406                      if(type2) type2.refCount++;
8407
8408                      exp.expType = type2;
8409                      type2.refCount++;
8410                   }
8411
8412                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8413                   {
8414                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8415
8416                      if(!type2._class.registered.dataType)
8417                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8418                      exp.op.exp1.destType = type2._class.registered.dataType;
8419                      exp.op.exp1.destType.refCount++;
8420
8421                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8422                      type1 = exp.op.exp1.destType;
8423                      exp.expType = type1;
8424                      type1.refCount++;
8425                   }
8426
8427                   // TESTING THIS NEW CODE
8428                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<')
8429                   {
8430                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass && exp.op.exp2.expType)
8431                      {
8432                         if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8433                         {
8434                            if(exp.expType) FreeType(exp.expType);
8435                            exp.expType = exp.op.exp1.expType;
8436                            if(exp.op.exp2.expType) exp.op.exp1.expType.refCount++;
8437                            valid = true;
8438                         }
8439                      }
8440
8441                      else if(type2 && (type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass && exp.op.exp1.expType))
8442                      {
8443                         if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8444                         {
8445                            if(exp.expType) FreeType(exp.expType);
8446                            exp.expType = exp.op.exp2.expType;
8447                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8448                            valid = true;
8449                         }
8450                      }
8451                   }
8452
8453                   if(!valid)
8454                   {
8455                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8456                      exp.op.exp2.destType = type1;
8457                      type1.refCount++;
8458
8459                      /*
8460                      // Maybe this was meant to be an enum...
8461                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8462                      {
8463                         Type oldType = exp.op.exp2.expType;
8464                         exp.op.exp2.expType = null;
8465                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8466                            FreeType(oldType);
8467                         else
8468                            exp.op.exp2.expType = oldType;
8469                      }
8470                      */
8471
8472                      /*
8473                      // TESTING THIS HERE... LATEST ADDITION
8474                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8475                      {
8476                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8477                         exp.op.exp2.destType = type2._class.registered.dataType;
8478                         if(type2._class.registered.dataType)
8479                            type2._class.registered.dataType.refCount++;
8480                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8481
8482                         //exp.expType = type2._class.registered.dataType; //type2;
8483                         //if(type2) type2.refCount++;
8484                      }
8485
8486                      // TESTING THIS HERE... LATEST ADDITION
8487                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8488                      {
8489                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8490                         exp.op.exp1.destType = type1._class.registered.dataType;
8491                         if(type1._class.registered.dataType)
8492                            type1._class.registered.dataType.refCount++;
8493                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8494                         exp.expType = type1._class.registered.dataType; //type1;
8495                         if(type1) type1.refCount++;
8496                      }
8497                      */
8498
8499                      if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8500                      {
8501                         if(exp.expType) FreeType(exp.expType);
8502                         exp.expType = exp.op.exp2.destType;
8503                         if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8504                      }
8505                      else if(type1 && type2)
8506                      {
8507                         char expString1[10240];
8508                         char expString2[10240];
8509                         char type1String[1024];
8510                         char type2String[1024];
8511                         expString1[0] = '\0';
8512                         expString2[0] = '\0';
8513                         type1String[0] = '\0';
8514                         type2String[0] = '\0';
8515                         if(inCompiler)
8516                         {
8517                            PrintExpression(exp.op.exp1, expString1);
8518                            ChangeCh(expString1, '\n', ' ');
8519                            PrintExpression(exp.op.exp2, expString2);
8520                            ChangeCh(expString2, '\n', ' ');
8521                            PrintType(exp.op.exp1.expType, type1String, false, true);
8522                            PrintType(exp.op.exp2.expType, type2String, false, true);
8523                         }
8524
8525                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8526
8527                         if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8528                         {
8529                            exp.expType = exp.op.exp1.expType;
8530                            if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8531                         }
8532                         else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8533                         {
8534                            exp.expType = exp.op.exp2.expType;
8535                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8536                         }
8537                      }
8538                   }
8539                }
8540                else if(type2)
8541                {
8542                   // Maybe this was meant to be an enum...
8543                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8544                   {
8545                      Type oldType = exp.op.exp1.expType;
8546                      exp.op.exp1.expType = null;
8547                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8548                         FreeType(oldType);
8549                      else
8550                         exp.op.exp1.expType = oldType;
8551                   }
8552
8553                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8554                   exp.op.exp1.destType = type2;
8555                   type2.refCount++;
8556                   /*
8557                   // TESTING THIS HERE... LATEST ADDITION
8558                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8559                   {
8560                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8561                      exp.op.exp1.destType = type1._class.registered.dataType;
8562                      if(type1._class.registered.dataType)
8563                         type1._class.registered.dataType.refCount++;
8564                   }
8565
8566                   // TESTING THIS HERE... LATEST ADDITION
8567                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8568                   {
8569                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8570                      exp.op.exp2.destType = type2._class.registered.dataType;
8571                      if(type2._class.registered.dataType)
8572                         type2._class.registered.dataType.refCount++;
8573                   }
8574                   */
8575
8576                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8577                   {
8578                      if(exp.expType) FreeType(exp.expType);
8579                      exp.expType = exp.op.exp1.destType;
8580                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8581                   }
8582                }
8583             }
8584             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8585             {
8586                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8587                {
8588                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8589                   // Convert e.g. / 4 into / 4.0
8590                   exp.op.exp1.destType = type2._class.registered.dataType;
8591                   if(type2._class.registered.dataType)
8592                      type2._class.registered.dataType.refCount++;
8593                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8594                }
8595                if(exp.op.op == '!')
8596                {
8597                   exp.expType = MkClassType("bool");
8598                   exp.expType.truth = true;
8599                }
8600                else
8601                {
8602                   exp.expType = type2;
8603                   if(type2) type2.refCount++;
8604                }
8605             }
8606             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8607             {
8608                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8609                {
8610                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8611                   // Convert e.g. / 4 into / 4.0
8612                   exp.op.exp2.destType = type1._class.registered.dataType;
8613                   if(type1._class.registered.dataType)
8614                      type1._class.registered.dataType.refCount++;
8615                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8616                }
8617                exp.expType = type1;
8618                if(type1) type1.refCount++;
8619             }
8620          }
8621
8622          yylloc = exp.loc;
8623          if(exp.op.exp1 && !exp.op.exp1.expType)
8624          {
8625             char expString[10000];
8626             expString[0] = '\0';
8627             if(inCompiler)
8628             {
8629                PrintExpression(exp.op.exp1, expString);
8630                ChangeCh(expString, '\n', ' ');
8631             }
8632             if(expString[0])
8633                Compiler_Error($"couldn't determine type of %s\n", expString);
8634          }
8635          if(exp.op.exp2 && !exp.op.exp2.expType)
8636          {
8637             char expString[10240];
8638             expString[0] = '\0';
8639             if(inCompiler)
8640             {
8641                PrintExpression(exp.op.exp2, expString);
8642                ChangeCh(expString, '\n', ' ');
8643             }
8644             if(expString[0])
8645                Compiler_Error($"couldn't determine type of %s\n", expString);
8646          }
8647
8648          if(boolResult)
8649          {
8650             FreeType(exp.expType);
8651             exp.expType = MkClassType("bool");
8652             exp.expType.truth = true;
8653          }
8654
8655          if(exp.op.op != SIZEOF)
8656             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8657                (!exp.op.exp2 || exp.op.exp2.isConstant);
8658
8659          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8660          {
8661             DeclareType(exp.op.exp2.expType, false, false);
8662          }
8663
8664          yylloc = oldyylloc;
8665
8666          FreeType(dummy);
8667          if(type2)
8668             FreeType(type2);
8669          break;
8670       }
8671       case bracketsExp:
8672       case extensionExpressionExp:
8673       {
8674          Expression e;
8675          exp.isConstant = true;
8676          for(e = exp.list->first; e; e = e.next)
8677          {
8678             bool inced = false;
8679             if(!e.next)
8680             {
8681                FreeType(e.destType);
8682                e.destType = exp.destType;
8683                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
8684             }
8685             ProcessExpressionType(e);
8686             if(inced)
8687                exp.destType.count--;
8688             if(!exp.expType && !e.next)
8689             {
8690                exp.expType = e.expType;
8691                if(e.expType) e.expType.refCount++;
8692             }
8693             if(!e.isConstant)
8694                exp.isConstant = false;
8695          }
8696
8697          // In case a cast became a member...
8698          e = exp.list->first;
8699          if(!e.next && e.type == memberExp)
8700          {
8701             // Preserve prev, next
8702             Expression next = exp.next, prev = exp.prev;
8703
8704
8705             FreeType(exp.expType);
8706             FreeType(exp.destType);
8707             delete exp.list;
8708
8709             *exp = *e;
8710
8711             exp.prev = prev;
8712             exp.next = next;
8713
8714             delete e;
8715
8716             ProcessExpressionType(exp);
8717          }
8718          break;
8719       }
8720       case indexExp:
8721       {
8722          Expression e;
8723          exp.isConstant = true;
8724
8725          ProcessExpressionType(exp.index.exp);
8726          if(!exp.index.exp.isConstant)
8727             exp.isConstant = false;
8728
8729          if(exp.index.exp.expType)
8730          {
8731             Type source = exp.index.exp.expType;
8732             if(source.kind == classType && source._class && source._class.registered)
8733             {
8734                Class _class = source._class.registered;
8735                Class c = _class.templateClass ? _class.templateClass : _class;
8736                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
8737                {
8738                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
8739
8740                   if(exp.index.index && exp.index.index->last)
8741                   {
8742                      ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
8743                   }
8744                }
8745             }
8746          }
8747
8748          for(e = exp.index.index->first; e; e = e.next)
8749          {
8750             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
8751             {
8752                if(e.destType) FreeType(e.destType);
8753                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
8754             }
8755             ProcessExpressionType(e);
8756             if(!e.next)
8757             {
8758                // Check if this type is int
8759             }
8760             if(!e.isConstant)
8761                exp.isConstant = false;
8762          }
8763
8764          if(!exp.expType)
8765             exp.expType = Dereference(exp.index.exp.expType);
8766          if(exp.expType)
8767             DeclareType(exp.expType, false, false);
8768          break;
8769       }
8770       case callExp:
8771       {
8772          Expression e;
8773          Type functionType;
8774          Type methodType = null;
8775          char name[1024];
8776          name[0] = '\0';
8777
8778          if(inCompiler)
8779          {
8780             PrintExpression(exp.call.exp,  name);
8781             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
8782             {
8783                //exp.call.exp.expType = null;
8784                PrintExpression(exp.call.exp,  name);
8785             }
8786          }
8787          if(exp.call.exp.type == identifierExp)
8788          {
8789             Expression idExp = exp.call.exp;
8790             Identifier id = idExp.identifier;
8791             if(!strcmp(id.string, "__builtin_frame_address"))
8792             {
8793                exp.expType = ProcessTypeString("void *", true);
8794                if(exp.call.arguments && exp.call.arguments->first)
8795                   ProcessExpressionType(exp.call.arguments->first);
8796                break;
8797             }
8798             else if(!strcmp(id.string, "__ENDIAN_PAD"))
8799             {
8800                exp.expType = ProcessTypeString("int", true);
8801                if(exp.call.arguments && exp.call.arguments->first)
8802                   ProcessExpressionType(exp.call.arguments->first);
8803                break;
8804             }
8805             else if(!strcmp(id.string, "Max") ||
8806                !strcmp(id.string, "Min") ||
8807                !strcmp(id.string, "Sgn") ||
8808                !strcmp(id.string, "Abs"))
8809             {
8810                Expression a = null;
8811                Expression b = null;
8812                Expression tempExp1 = null, tempExp2 = null;
8813                if((!strcmp(id.string, "Max") ||
8814                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
8815                {
8816                   a = exp.call.arguments->first;
8817                   b = exp.call.arguments->last;
8818                   tempExp1 = a;
8819                   tempExp2 = b;
8820                }
8821                else if(exp.call.arguments->count == 1)
8822                {
8823                   a = exp.call.arguments->first;
8824                   tempExp1 = a;
8825                }
8826
8827                if(a)
8828                {
8829                   exp.call.arguments->Clear();
8830                   idExp.identifier = null;
8831
8832                   FreeExpContents(exp);
8833
8834                   ProcessExpressionType(a);
8835                   if(b)
8836                      ProcessExpressionType(b);
8837
8838                   exp.type = bracketsExp;
8839                   exp.list = MkList();
8840
8841                   if(a.expType && (!b || b.expType))
8842                   {
8843                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
8844                      {
8845                         // Use the simpleStruct name/ids for now...
8846                         if(inCompiler)
8847                         {
8848                            OldList * specs = MkList();
8849                            OldList * decls = MkList();
8850                            Declaration decl;
8851                            char temp1[1024], temp2[1024];
8852
8853                            GetTypeSpecs(a.expType, specs);
8854
8855                            if(a && !a.isConstant && a.type != identifierExp)
8856                            {
8857                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
8858                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
8859                               tempExp1 = QMkExpId(temp1);
8860                               tempExp1.expType = a.expType;
8861                               if(a.expType)
8862                                  a.expType.refCount++;
8863                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
8864                            }
8865                            if(b && !b.isConstant && b.type != identifierExp)
8866                            {
8867                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
8868                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
8869                               tempExp2 = QMkExpId(temp2);
8870                               tempExp2.expType = b.expType;
8871                               if(b.expType)
8872                                  b.expType.refCount++;
8873                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
8874                            }
8875
8876                            decl = MkDeclaration(specs, decls);
8877                            if(!curCompound.compound.declarations)
8878                               curCompound.compound.declarations = MkList();
8879                            curCompound.compound.declarations->Insert(null, decl);
8880                         }
8881                      }
8882                   }
8883
8884                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
8885                   {
8886                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
8887                      ListAdd(exp.list,
8888                         MkExpCondition(MkExpBrackets(MkListOne(
8889                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
8890                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
8891                      exp.expType = a.expType;
8892                      if(a.expType)
8893                         a.expType.refCount++;
8894                   }
8895                   else if(!strcmp(id.string, "Abs"))
8896                   {
8897                      ListAdd(exp.list,
8898                         MkExpCondition(MkExpBrackets(MkListOne(
8899                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8900                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
8901                      exp.expType = a.expType;
8902                      if(a.expType)
8903                         a.expType.refCount++;
8904                   }
8905                   else if(!strcmp(id.string, "Sgn"))
8906                   {
8907                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
8908                      ListAdd(exp.list,
8909                         MkExpCondition(MkExpBrackets(MkListOne(
8910                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
8911                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
8912                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
8913                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
8914                      exp.expType = ProcessTypeString("int", false);
8915                   }
8916
8917                   FreeExpression(tempExp1);
8918                   if(tempExp2) FreeExpression(tempExp2);
8919
8920                   FreeIdentifier(id);
8921                   break;
8922                }
8923             }
8924          }
8925
8926          {
8927             Type dummy
8928             {
8929                count = 1;
8930                refCount = 1;
8931             };
8932             if(!exp.call.exp.destType)
8933             {
8934                exp.call.exp.destType = dummy;
8935                dummy.refCount++;
8936             }
8937             ProcessExpressionType(exp.call.exp);
8938             if(exp.call.exp.destType == dummy)
8939             {
8940                FreeType(dummy);
8941                exp.call.exp.destType = null;
8942             }
8943             FreeType(dummy);
8944          }
8945
8946          // Check argument types against parameter types
8947          functionType = exp.call.exp.expType;
8948
8949          if(functionType && functionType.kind == TypeKind::methodType)
8950          {
8951             methodType = functionType;
8952             functionType = methodType.method.dataType;
8953
8954             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
8955             // TOCHECK: Instead of doing this here could this be done per param?
8956             if(exp.call.exp.expType.usedClass)
8957             {
8958                char typeString[1024];
8959                typeString[0] = '\0';
8960                {
8961                   Symbol back = functionType.thisClass;
8962                   // Do not output class specifier here (thisclass was added to this)
8963                   functionType.thisClass = null;
8964                   PrintType(functionType, typeString, true, true);
8965                   functionType.thisClass = back;
8966                }
8967                if(strstr(typeString, "thisclass"))
8968                {
8969                   OldList * specs = MkList();
8970                   Declarator decl;
8971                   {
8972                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
8973
8974                      decl = SpecDeclFromString(typeString, specs, null);
8975
8976                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
8977                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
8978                         exp.call.exp.expType.usedClass))
8979                         thisClassParams = false;
8980
8981                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
8982                      {
8983                         Class backupThisClass = thisClass;
8984                         thisClass = exp.call.exp.expType.usedClass;
8985                         ProcessDeclarator(decl);
8986                         thisClass = backupThisClass;
8987                      }
8988
8989                      thisClassParams = true;
8990
8991                      functionType = ProcessType(specs, decl);
8992                      functionType.refCount = 0;
8993                      FinishTemplatesContext(context);
8994                   }
8995
8996                   FreeList(specs, FreeSpecifier);
8997                   FreeDeclarator(decl);
8998                 }
8999             }
9000          }
9001          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9002          {
9003             Type type = functionType.type;
9004             if(!functionType.refCount)
9005             {
9006                functionType.type = null;
9007                FreeType(functionType);
9008             }
9009             //methodType = functionType;
9010             functionType = type;
9011          }
9012          if(functionType && functionType.kind != TypeKind::functionType)
9013          {
9014             Compiler_Error($"called object %s is not a function\n", name);
9015          }
9016          else if(functionType)
9017          {
9018             bool emptyParams = false, noParams = false;
9019             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9020             Type type = functionType.params.first;
9021             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9022             int extra = 0;
9023             Location oldyylloc = yylloc;
9024
9025             if(!type) emptyParams = true;
9026
9027             // WORKING ON THIS:
9028             if(functionType.extraParam && e && functionType.thisClass)
9029             {
9030                e.destType = MkClassType(functionType.thisClass.string);
9031                e = e.next;
9032             }
9033
9034             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9035             // Fixed #141 by adding '&& !functionType.extraParam'
9036             if(!functionType.staticMethod && !functionType.extraParam)
9037             {
9038                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9039                   memberExp.member.exp.expType._class)
9040                {
9041                   type = MkClassType(memberExp.member.exp.expType._class.string);
9042                   if(e)
9043                   {
9044                      e.destType = type;
9045                      e = e.next;
9046                      type = functionType.params.first;
9047                   }
9048                   else
9049                      type.refCount = 0;
9050                }
9051                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9052                {
9053                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9054                   type.byReference = functionType.byReference;
9055                   type.typedByReference = functionType.typedByReference;
9056                   if(e)
9057                   {
9058                      // Allow manually passing a class for typed object
9059                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9060                         e = e.next;
9061                      e.destType = type;
9062                      e = e.next;
9063                      type = functionType.params.first;
9064                   }
9065                   else
9066                      type.refCount = 0;
9067                   //extra = 1;
9068                }
9069             }
9070
9071             if(type && type.kind == voidType)
9072             {
9073                noParams = true;
9074                if(!type.refCount) FreeType(type);
9075                type = null;
9076             }
9077
9078             for( ; e; e = e.next)
9079             {
9080                if(!type && !emptyParams)
9081                {
9082                   yylloc = e.loc;
9083                   if(methodType && methodType.methodClass)
9084                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9085                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9086                         noParams ? 0 : functionType.params.count);
9087                   else
9088                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9089                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9090                         noParams ? 0 : functionType.params.count);
9091                   break;
9092                }
9093
9094                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9095                {
9096                   Type templatedType = null;
9097                   Class _class = methodType.usedClass;
9098                   ClassTemplateParameter curParam = null;
9099                   int id = 0;
9100                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9101                   {
9102                      Class sClass;
9103                      for(sClass = _class; sClass; sClass = sClass.base)
9104                      {
9105                         if(sClass.templateClass) sClass = sClass.templateClass;
9106                         id = 0;
9107                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9108                         {
9109                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9110                            {
9111                               Class nextClass;
9112                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9113                               {
9114                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9115                                  id += nextClass.templateParams.count;
9116                               }
9117                               break;
9118                            }
9119                            id++;
9120                         }
9121                         if(curParam) break;
9122                      }
9123                   }
9124                   if(curParam && _class.templateArgs[id].dataTypeString)
9125                   {
9126                      ClassTemplateArgument arg = _class.templateArgs[id];
9127                      {
9128                         Context context = SetupTemplatesContext(_class);
9129
9130                         /*if(!arg.dataType)
9131                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9132                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9133                         FinishTemplatesContext(context);
9134                      }
9135                      e.destType = templatedType;
9136                      if(templatedType)
9137                      {
9138                         templatedType.passAsTemplate = true;
9139                         // templatedType.refCount++;
9140                      }
9141                   }
9142                   else
9143                   {
9144                      e.destType = type;
9145                      if(type) type.refCount++;
9146                   }
9147                }
9148                else
9149                {
9150                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9151                   {
9152                      e.destType = type.prev;
9153                      e.destType.refCount++;
9154                   }
9155                   else
9156                   {
9157                      e.destType = type;
9158                      if(type) type.refCount++;
9159                   }
9160                }
9161                // Don't reach the end for the ellipsis
9162                if(type && type.kind != ellipsisType)
9163                {
9164                   Type next = type.next;
9165                   if(!type.refCount) FreeType(type);
9166                   type = next;
9167                }
9168             }
9169
9170             if(type && type.kind != ellipsisType)
9171             {
9172                if(methodType && methodType.methodClass)
9173                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9174                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9175                      functionType.params.count + extra);
9176                else
9177                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9178                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9179                      functionType.params.count + extra);
9180             }
9181             yylloc = oldyylloc;
9182             if(type && !type.refCount) FreeType(type);
9183          }
9184          else
9185          {
9186             functionType = Type
9187             {
9188                refCount = 0;
9189                kind = TypeKind::functionType;
9190             };
9191
9192             if(exp.call.exp.type == identifierExp)
9193             {
9194                char * string = exp.call.exp.identifier.string;
9195                if(inCompiler)
9196                {
9197                   Symbol symbol;
9198                   Location oldyylloc = yylloc;
9199
9200                   yylloc = exp.call.exp.identifier.loc;
9201                   if(strstr(string, "__builtin_") == string)
9202                   {
9203                      if(exp.destType)
9204                      {
9205                         functionType.returnType = exp.destType;
9206                         exp.destType.refCount++;
9207                      }
9208                   }
9209                   else
9210                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9211                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9212                   globalContext.symbols.Add((BTNode)symbol);
9213                   if(strstr(symbol.string, "::"))
9214                      globalContext.hasNameSpace = true;
9215
9216                   yylloc = oldyylloc;
9217                }
9218             }
9219             else if(exp.call.exp.type == memberExp)
9220             {
9221                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9222                   exp.call.exp.member.member.string);*/
9223             }
9224             else
9225                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9226
9227             if(!functionType.returnType)
9228             {
9229                functionType.returnType = Type
9230                {
9231                   refCount = 1;
9232                   kind = intType;
9233                };
9234             }
9235          }
9236          if(functionType && functionType.kind == TypeKind::functionType)
9237          {
9238             exp.expType = functionType.returnType;
9239
9240             if(functionType.returnType)
9241                functionType.returnType.refCount++;
9242
9243             if(!functionType.refCount)
9244                FreeType(functionType);
9245          }
9246
9247          if(exp.call.arguments)
9248          {
9249             for(e = exp.call.arguments->first; e; e = e.next)
9250             {
9251                Type destType = e.destType;
9252                ProcessExpressionType(e);
9253             }
9254          }
9255          break;
9256       }
9257       case memberExp:
9258       {
9259          Type type;
9260          Location oldyylloc = yylloc;
9261          bool thisPtr;
9262          Expression checkExp = exp.member.exp;
9263          while(checkExp)
9264          {
9265             if(checkExp.type == castExp)
9266                checkExp = checkExp.cast.exp;
9267             else if(checkExp.type == bracketsExp)
9268                checkExp = checkExp.list ? checkExp.list->first : null;
9269             else
9270                break;
9271          }
9272
9273          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9274          exp.thisPtr = thisPtr;
9275
9276          // DOING THIS LATER NOW...
9277          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9278          {
9279             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9280             /* TODO: Name Space Fix ups
9281             if(!exp.member.member.classSym)
9282                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9283             */
9284          }
9285
9286          ProcessExpressionType(exp.member.exp);
9287          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9288             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9289          {
9290             exp.isConstant = false;
9291          }
9292          else
9293             exp.isConstant = exp.member.exp.isConstant;
9294          type = exp.member.exp.expType;
9295
9296          yylloc = exp.loc;
9297
9298          if(type && (type.kind == templateType))
9299          {
9300             Class _class = thisClass ? thisClass : currentClass;
9301             ClassTemplateParameter param = null;
9302             if(_class)
9303             {
9304                for(param = _class.templateParams.first; param; param = param.next)
9305                {
9306                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9307                      break;
9308                }
9309             }
9310             if(param && param.defaultArg.member)
9311             {
9312                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9313                if(argExp)
9314                {
9315                   Expression expMember = exp.member.exp;
9316                   Declarator decl;
9317                   OldList * specs = MkList();
9318                   char thisClassTypeString[1024];
9319
9320                   FreeIdentifier(exp.member.member);
9321
9322                   ProcessExpressionType(argExp);
9323
9324                   {
9325                      char * colon = strstr(param.defaultArg.memberString, "::");
9326                      if(colon)
9327                      {
9328                         char className[1024];
9329                         Class sClass;
9330
9331                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9332                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9333                      }
9334                      else
9335                         strcpy(thisClassTypeString, _class.fullName);
9336                   }
9337
9338                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9339
9340                   exp.expType = ProcessType(specs, decl);
9341                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9342                   {
9343                      Class expClass = exp.expType._class.registered;
9344                      Class cClass = null;
9345                      int c;
9346                      int paramCount = 0;
9347                      int lastParam = -1;
9348
9349                      char templateString[1024];
9350                      ClassTemplateParameter param;
9351                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9352                      for(cClass = expClass; cClass; cClass = cClass.base)
9353                      {
9354                         int p = 0;
9355                         for(param = cClass.templateParams.first; param; param = param.next)
9356                         {
9357                            int id = p;
9358                            Class sClass;
9359                            ClassTemplateArgument arg;
9360                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9361                            arg = expClass.templateArgs[id];
9362
9363                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9364                            {
9365                               ClassTemplateParameter cParam;
9366                               //int p = numParams - sClass.templateParams.count;
9367                               int p = 0;
9368                               Class nextClass;
9369                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9370
9371                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9372                               {
9373                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9374                                  {
9375                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9376                                     {
9377                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9378                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9379                                        break;
9380                                     }
9381                                  }
9382                               }
9383                            }
9384
9385                            {
9386                               char argument[256];
9387                               argument[0] = '\0';
9388                               /*if(arg.name)
9389                               {
9390                                  strcat(argument, arg.name.string);
9391                                  strcat(argument, " = ");
9392                               }*/
9393                               switch(param.type)
9394                               {
9395                                  case expression:
9396                                  {
9397                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9398                                     char expString[1024];
9399                                     OldList * specs = MkList();
9400                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9401                                     Expression exp;
9402                                     char * string = PrintHexUInt64(arg.expression.ui64);
9403                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9404                                     delete string;
9405
9406                                     ProcessExpressionType(exp);
9407                                     ComputeExpression(exp);
9408                                     expString[0] = '\0';
9409                                     PrintExpression(exp, expString);
9410                                     strcat(argument, expString);
9411                                     // delete exp;
9412                                     FreeExpression(exp);
9413                                     break;
9414                                  }
9415                                  case identifier:
9416                                  {
9417                                     strcat(argument, arg.member.name);
9418                                     break;
9419                                  }
9420                                  case TemplateParameterType::type:
9421                                  {
9422                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9423                                     {
9424                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9425                                           strcat(argument, thisClassTypeString);
9426                                        else
9427                                           strcat(argument, arg.dataTypeString);
9428                                     }
9429                                     break;
9430                                  }
9431                               }
9432                               if(argument[0])
9433                               {
9434                                  if(paramCount) strcat(templateString, ", ");
9435                                  if(lastParam != p - 1)
9436                                  {
9437                                     strcat(templateString, param.name);
9438                                     strcat(templateString, " = ");
9439                                  }
9440                                  strcat(templateString, argument);
9441                                  paramCount++;
9442                                  lastParam = p;
9443                               }
9444                               p++;
9445                            }
9446                         }
9447                      }
9448                      {
9449                         int len = strlen(templateString);
9450                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9451                         templateString[len++] = '>';
9452                         templateString[len++] = '\0';
9453                      }
9454                      {
9455                         Context context = SetupTemplatesContext(_class);
9456                         FreeType(exp.expType);
9457                         exp.expType = ProcessTypeString(templateString, false);
9458                         FinishTemplatesContext(context);
9459                      }
9460                   }
9461
9462                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9463                   exp.type = bracketsExp;
9464                   exp.list = MkListOne(MkExpOp(null, '*',
9465                   /*opExp;
9466                   exp.op.op = '*';
9467                   exp.op.exp1 = null;
9468                   exp.op.exp2 = */
9469                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9470                      MkExpBrackets(MkListOne(
9471                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9472                            '+',
9473                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9474                            '+',
9475                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9476
9477                            ));
9478                }
9479             }
9480             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9481                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9482             {
9483                type = ProcessTemplateParameterType(type.templateParameter);
9484             }
9485          }
9486          // TODO: *** This seems to be where we should add method support for all basic types ***
9487          if(type && (type.kind == templateType));
9488          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9489                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9490                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9491                           (type.kind == pointerType && type.type.kind == charType)))
9492          {
9493             Identifier id = exp.member.member;
9494             TypeKind typeKind = type.kind;
9495             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9496             if(typeKind == subClassType && exp.member.exp.type == classExp)
9497             {
9498                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9499                typeKind = classType;
9500             }
9501
9502             if(id)
9503             {
9504                if(typeKind == intType || typeKind == enumType)
9505                   _class = eSystem_FindClass(privateModule, "int");
9506                else if(!_class)
9507                {
9508                   if(type.kind == classType && type._class && type._class.registered)
9509                   {
9510                      _class = type._class.registered;
9511                   }
9512                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9513                   {
9514                      _class = FindClass("char *").registered;
9515                   }
9516                   else if(type.kind == pointerType)
9517                   {
9518                      _class = eSystem_FindClass(privateModule, "uintptr");
9519                      FreeType(exp.expType);
9520                      exp.expType = ProcessTypeString("uintptr", false);
9521                      exp.byReference = true;
9522                   }
9523                   else
9524                   {
9525                      char string[1024] = "";
9526                      Symbol classSym;
9527                      PrintTypeNoConst(type, string, false, true);
9528                      classSym = FindClass(string);
9529                      if(classSym) _class = classSym.registered;
9530                   }
9531                }
9532             }
9533
9534             if(_class && id)
9535             {
9536                /*bool thisPtr =
9537                   (exp.member.exp.type == identifierExp &&
9538                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9539                Property prop = null;
9540                Method method = null;
9541                DataMember member = null;
9542                Property revConvert = null;
9543                ClassProperty classProp = null;
9544
9545                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9546                   exp.member.memberType = propertyMember;
9547
9548                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9549                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9550
9551                if(typeKind != subClassType)
9552                {
9553                   // Prioritize data members over properties for "this"
9554                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9555                   {
9556                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9557                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9558                      {
9559                         prop = eClass_FindProperty(_class, id.string, privateModule);
9560                         if(prop)
9561                            member = null;
9562                      }
9563                      if(!member && !prop)
9564                         prop = eClass_FindProperty(_class, id.string, privateModule);
9565                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9566                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9567                         exp.member.thisPtr = true;
9568                   }
9569                   // Prioritize properties over data members otherwise
9570                   else
9571                   {
9572                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9573                      if(!id.classSym)
9574                      {
9575                         prop = eClass_FindProperty(_class, id.string, null);
9576                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9577                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9578                      }
9579
9580                      if(!prop && !member)
9581                      {
9582                         method = eClass_FindMethod(_class, id.string, null);
9583                         if(!method)
9584                         {
9585                            prop = eClass_FindProperty(_class, id.string, privateModule);
9586                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9587                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9588                         }
9589                      }
9590
9591                      if(member && prop)
9592                      {
9593                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9594                            prop = null;
9595                         else
9596                            member = null;
9597                      }
9598                   }
9599                }
9600                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9601                   method = eClass_FindMethod(_class, id.string, privateModule);
9602                if(!prop && !member && !method)
9603                {
9604                   if(typeKind == subClassType)
9605                   {
9606                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9607                      if(classProp)
9608                      {
9609                         exp.member.memberType = classPropertyMember;
9610                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9611                      }
9612                      else
9613                      {
9614                         // Assume this is a class_data member
9615                         char structName[1024];
9616                         Identifier id = exp.member.member;
9617                         Expression classExp = exp.member.exp;
9618                         type.refCount++;
9619
9620                         FreeType(classExp.expType);
9621                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9622
9623                         strcpy(structName, "__ecereClassData_");
9624                         FullClassNameCat(structName, type._class.string, false);
9625                         exp.type = pointerExp;
9626                         exp.member.member = id;
9627
9628                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9629                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
9630                               MkExpBrackets(MkListOne(MkExpOp(
9631                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
9632                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9633                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9634                                  )));
9635
9636                         FreeType(type);
9637
9638                         ProcessExpressionType(exp);
9639                         return;
9640                      }
9641                   }
9642                   else
9643                   {
9644                      // Check for reverse conversion
9645                      // (Convert in an instantiation later, so that we can use
9646                      //  deep properties system)
9647                      Symbol classSym = FindClass(id.string);
9648                      if(classSym)
9649                      {
9650                         Class convertClass = classSym.registered;
9651                         if(convertClass)
9652                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9653                      }
9654                   }
9655                }
9656
9657                if(prop)
9658                {
9659                   exp.member.memberType = propertyMember;
9660                   if(!prop.dataType)
9661                      ProcessPropertyType(prop);
9662                   exp.expType = prop.dataType;
9663                   if(prop.dataType) prop.dataType.refCount++;
9664                }
9665                else if(member)
9666                {
9667                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9668                   {
9669                      FreeExpContents(exp);
9670                      exp.type = identifierExp;
9671                      exp.identifier = MkIdentifier("class");
9672                      ProcessExpressionType(exp);
9673                      return;
9674                   }
9675
9676                   exp.member.memberType = dataMember;
9677                   DeclareStruct(_class.fullName, false);
9678                   if(!member.dataType)
9679                   {
9680                      Context context = SetupTemplatesContext(_class);
9681                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9682                      FinishTemplatesContext(context);
9683                   }
9684                   exp.expType = member.dataType;
9685                   if(member.dataType) member.dataType.refCount++;
9686                }
9687                else if(revConvert)
9688                {
9689                   exp.member.memberType = reverseConversionMember;
9690                   exp.expType = MkClassType(revConvert._class.fullName);
9691                }
9692                else if(method)
9693                {
9694                   //if(inCompiler)
9695                   {
9696                      /*if(id._class)
9697                      {
9698                         exp.type = identifierExp;
9699                         exp.identifier = exp.member.member;
9700                      }
9701                      else*/
9702                         exp.member.memberType = methodMember;
9703                   }
9704                   if(!method.dataType)
9705                      ProcessMethodType(method);
9706                   exp.expType = Type
9707                   {
9708                      refCount = 1;
9709                      kind = methodType;
9710                      method = method;
9711                   };
9712
9713                   // Tricky spot here... To use instance versus class virtual table
9714                   // Put it back to what it was... What did we break?
9715
9716                   // Had to put it back for overriding Main of Thread global instance
9717
9718                   //exp.expType.methodClass = _class;
9719                   exp.expType.methodClass = (id && id._class) ? _class : null;
9720
9721                   // Need the actual class used for templated classes
9722                   exp.expType.usedClass = _class;
9723                }
9724                else if(!classProp)
9725                {
9726                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9727                   {
9728                      FreeExpContents(exp);
9729                      exp.type = identifierExp;
9730                      exp.identifier = MkIdentifier("class");
9731                      FreeType(exp.expType);
9732                      exp.expType = MkClassType("ecere::com::Class");
9733                      return;
9734                   }
9735                   yylloc = exp.member.member.loc;
9736                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
9737                   if(inCompiler)
9738                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
9739                }
9740
9741                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
9742                {
9743                   Class tClass;
9744
9745                   tClass = _class;
9746                   while(tClass && !tClass.templateClass) tClass = tClass.base;
9747
9748                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
9749                   {
9750                      int id = 0;
9751                      ClassTemplateParameter curParam = null;
9752                      Class sClass;
9753
9754                      for(sClass = tClass; sClass; sClass = sClass.base)
9755                      {
9756                         id = 0;
9757                         if(sClass.templateClass) sClass = sClass.templateClass;
9758                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9759                         {
9760                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
9761                            {
9762                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9763                                  id += sClass.templateParams.count;
9764                               break;
9765                            }
9766                            id++;
9767                         }
9768                         if(curParam) break;
9769                      }
9770
9771                      if(curParam && tClass.templateArgs[id].dataTypeString)
9772                      {
9773                         ClassTemplateArgument arg = tClass.templateArgs[id];
9774                         Context context = SetupTemplatesContext(tClass);
9775                         /*if(!arg.dataType)
9776                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9777                         FreeType(exp.expType);
9778                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
9779                         if(exp.expType)
9780                         {
9781                            if(exp.expType.kind == thisClassType)
9782                            {
9783                               FreeType(exp.expType);
9784                               exp.expType = ReplaceThisClassType(_class);
9785                            }
9786
9787                            if(tClass.templateClass)
9788                               exp.expType.passAsTemplate = true;
9789                            //exp.expType.refCount++;
9790                            if(!exp.destType)
9791                            {
9792                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
9793                               //exp.destType.refCount++;
9794
9795                               if(exp.destType.kind == thisClassType)
9796                               {
9797                                  FreeType(exp.destType);
9798                                  exp.destType = ReplaceThisClassType(_class);
9799                               }
9800                            }
9801                         }
9802                         FinishTemplatesContext(context);
9803                      }
9804                   }
9805                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
9806                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
9807                   {
9808                      int id = 0;
9809                      ClassTemplateParameter curParam = null;
9810                      Class sClass;
9811
9812                      for(sClass = tClass; sClass; sClass = sClass.base)
9813                      {
9814                         id = 0;
9815                         if(sClass.templateClass) sClass = sClass.templateClass;
9816                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9817                         {
9818                            if(curParam.type == TemplateParameterType::type &&
9819                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
9820                            {
9821                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9822                                  id += sClass.templateParams.count;
9823                               break;
9824                            }
9825                            id++;
9826                         }
9827                         if(curParam) break;
9828                      }
9829
9830                      if(curParam)
9831                      {
9832                         ClassTemplateArgument arg = tClass.templateArgs[id];
9833                         Context context = SetupTemplatesContext(tClass);
9834                         Type basicType;
9835                         /*if(!arg.dataType)
9836                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9837
9838                         basicType = ProcessTypeString(arg.dataTypeString, false);
9839                         if(basicType)
9840                         {
9841                            if(basicType.kind == thisClassType)
9842                            {
9843                               FreeType(basicType);
9844                               basicType = ReplaceThisClassType(_class);
9845                            }
9846
9847                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
9848                            if(tClass.templateClass)
9849                               basicType.passAsTemplate = true;
9850                            */
9851
9852                            FreeType(exp.expType);
9853
9854                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
9855                            //exp.expType.refCount++;
9856                            if(!exp.destType)
9857                            {
9858                               exp.destType = exp.expType;
9859                               exp.destType.refCount++;
9860                            }
9861
9862                            {
9863                               Expression newExp { };
9864                               OldList * specs = MkList();
9865                               Declarator decl;
9866                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
9867                               *newExp = *exp;
9868                               if(exp.destType) exp.destType.refCount++;
9869                               if(exp.expType)  exp.expType.refCount++;
9870                               exp.type = castExp;
9871                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
9872                               exp.cast.exp = newExp;
9873                               //FreeType(exp.expType);
9874                               //exp.expType = null;
9875                               //ProcessExpressionType(sourceExp);
9876                            }
9877                         }
9878                         FinishTemplatesContext(context);
9879                      }
9880                   }
9881                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
9882                   {
9883                      Class expClass = exp.expType._class.registered;
9884                      if(expClass)
9885                      {
9886                         Class cClass = null;
9887                         int c;
9888                         int p = 0;
9889                         int paramCount = 0;
9890                         int lastParam = -1;
9891                         char templateString[1024];
9892                         ClassTemplateParameter param;
9893                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
9894                         while(cClass != expClass)
9895                         {
9896                            Class sClass;
9897                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
9898                            cClass = sClass;
9899
9900                            for(param = cClass.templateParams.first; param; param = param.next)
9901                            {
9902                               Class cClassCur = null;
9903                               int c;
9904                               int cp = 0;
9905                               ClassTemplateParameter paramCur = null;
9906                               ClassTemplateArgument arg;
9907                               while(cClassCur != tClass && !paramCur)
9908                               {
9909                                  Class sClassCur;
9910                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
9911                                  cClassCur = sClassCur;
9912
9913                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
9914                                  {
9915                                     if(!strcmp(paramCur.name, param.name))
9916                                     {
9917
9918                                        break;
9919                                     }
9920                                     cp++;
9921                                  }
9922                               }
9923                               if(paramCur && paramCur.type == TemplateParameterType::type)
9924                                  arg = tClass.templateArgs[cp];
9925                               else
9926                                  arg = expClass.templateArgs[p];
9927
9928                               {
9929                                  char argument[256];
9930                                  argument[0] = '\0';
9931                                  /*if(arg.name)
9932                                  {
9933                                     strcat(argument, arg.name.string);
9934                                     strcat(argument, " = ");
9935                                  }*/
9936                                  switch(param.type)
9937                                  {
9938                                     case expression:
9939                                     {
9940                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9941                                        char expString[1024];
9942                                        OldList * specs = MkList();
9943                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9944                                        Expression exp;
9945                                        char * string = PrintHexUInt64(arg.expression.ui64);
9946                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9947                                        delete string;
9948
9949                                        ProcessExpressionType(exp);
9950                                        ComputeExpression(exp);
9951                                        expString[0] = '\0';
9952                                        PrintExpression(exp, expString);
9953                                        strcat(argument, expString);
9954                                        // delete exp;
9955                                        FreeExpression(exp);
9956                                        break;
9957                                     }
9958                                     case identifier:
9959                                     {
9960                                        strcat(argument, arg.member.name);
9961                                        break;
9962                                     }
9963                                     case TemplateParameterType::type:
9964                                     {
9965                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9966                                           strcat(argument, arg.dataTypeString);
9967                                        break;
9968                                     }
9969                                  }
9970                                  if(argument[0])
9971                                  {
9972                                     if(paramCount) strcat(templateString, ", ");
9973                                     if(lastParam != p - 1)
9974                                     {
9975                                        strcat(templateString, param.name);
9976                                        strcat(templateString, " = ");
9977                                     }
9978                                     strcat(templateString, argument);
9979                                     paramCount++;
9980                                     lastParam = p;
9981                                  }
9982                               }
9983                               p++;
9984                            }
9985                         }
9986                         {
9987                            int len = strlen(templateString);
9988                            if(templateString[len-1] == '>') templateString[len++] = ' ';
9989                            templateString[len++] = '>';
9990                            templateString[len++] = '\0';
9991                         }
9992
9993                         FreeType(exp.expType);
9994                         {
9995                            Context context = SetupTemplatesContext(tClass);
9996                            exp.expType = ProcessTypeString(templateString, false);
9997                            FinishTemplatesContext(context);
9998                         }
9999                      }
10000                   }
10001                }
10002             }
10003             else
10004                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10005          }
10006          else if(type && (type.kind == structType || type.kind == unionType))
10007          {
10008             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10009             if(memberType)
10010             {
10011                exp.expType = memberType;
10012                if(memberType)
10013                   memberType.refCount++;
10014             }
10015          }
10016          else
10017          {
10018             char expString[10240];
10019             expString[0] = '\0';
10020             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10021             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10022          }
10023
10024          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10025          {
10026             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10027             {
10028                Identifier id = exp.member.member;
10029                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10030                if(_class)
10031                {
10032                   FreeType(exp.expType);
10033                   exp.expType = ReplaceThisClassType(_class);
10034                }
10035             }
10036          }
10037          yylloc = oldyylloc;
10038          break;
10039       }
10040       // Convert x->y into (*x).y
10041       case pointerExp:
10042       {
10043          Type destType = exp.destType;
10044
10045          // DOING THIS LATER NOW...
10046          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10047          {
10048             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10049             /* TODO: Name Space Fix ups
10050             if(!exp.member.member.classSym)
10051                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10052             */
10053          }
10054
10055          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10056          exp.type = memberExp;
10057          if(destType)
10058             destType.count++;
10059          ProcessExpressionType(exp);
10060          if(destType)
10061             destType.count--;
10062          break;
10063       }
10064       case classSizeExp:
10065       {
10066          //ComputeExpression(exp);
10067
10068          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10069          if(classSym && classSym.registered)
10070          {
10071             if(classSym.registered.type == noHeadClass)
10072             {
10073                char name[1024];
10074                name[0] = '\0';
10075                DeclareStruct(classSym.string, false);
10076                FreeSpecifier(exp._class);
10077                exp.type = typeSizeExp;
10078                FullClassNameCat(name, classSym.string, false);
10079                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10080             }
10081             else
10082             {
10083                if(classSym.registered.fixed)
10084                {
10085                   FreeSpecifier(exp._class);
10086                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10087                   exp.type = constantExp;
10088                }
10089                else
10090                {
10091                   char className[1024];
10092                   strcpy(className, "__ecereClass_");
10093                   FullClassNameCat(className, classSym.string, true);
10094                   MangleClassName(className);
10095
10096                   DeclareClass(classSym, className);
10097
10098                   FreeExpContents(exp);
10099                   exp.type = pointerExp;
10100                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10101                   exp.member.member = MkIdentifier("structSize");
10102                }
10103             }
10104          }
10105
10106          exp.expType = Type
10107          {
10108             refCount = 1;
10109             kind = intType;
10110          };
10111          // exp.isConstant = true;
10112          break;
10113       }
10114       case typeSizeExp:
10115       {
10116          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10117
10118          exp.expType = Type
10119          {
10120             refCount = 1;
10121             kind = intType;
10122          };
10123          exp.isConstant = true;
10124
10125          DeclareType(type, false, false);
10126          FreeType(type);
10127          break;
10128       }
10129       case castExp:
10130       {
10131          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10132          type.count = 1;
10133          FreeType(exp.cast.exp.destType);
10134          exp.cast.exp.destType = type;
10135          type.refCount++;
10136          ProcessExpressionType(exp.cast.exp);
10137          type.count = 0;
10138          exp.expType = type;
10139          //type.refCount++;
10140
10141          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10142          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10143          {
10144             void * prev = exp.prev, * next = exp.next;
10145             Type expType = exp.cast.exp.destType;
10146             Expression castExp = exp.cast.exp;
10147             Type destType = exp.destType;
10148
10149             if(expType) expType.refCount++;
10150
10151             //FreeType(exp.destType);
10152             FreeType(exp.expType);
10153             FreeTypeName(exp.cast.typeName);
10154
10155             *exp = *castExp;
10156             FreeType(exp.expType);
10157             FreeType(exp.destType);
10158
10159             exp.expType = expType;
10160             exp.destType = destType;
10161
10162             delete castExp;
10163
10164             exp.prev = prev;
10165             exp.next = next;
10166
10167          }
10168          else
10169          {
10170             exp.isConstant = exp.cast.exp.isConstant;
10171          }
10172          //FreeType(type);
10173          break;
10174       }
10175       case extensionInitializerExp:
10176       {
10177          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10178          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10179          // ProcessInitializer(exp.initializer.initializer, type);
10180          exp.expType = type;
10181          break;
10182       }
10183       case vaArgExp:
10184       {
10185          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10186          ProcessExpressionType(exp.vaArg.exp);
10187          exp.expType = type;
10188          break;
10189       }
10190       case conditionExp:
10191       {
10192          Expression e;
10193          exp.isConstant = true;
10194
10195          FreeType(exp.cond.cond.destType);
10196          exp.cond.cond.destType = MkClassType("bool");
10197          exp.cond.cond.destType.truth = true;
10198          ProcessExpressionType(exp.cond.cond);
10199          if(!exp.cond.cond.isConstant)
10200             exp.isConstant = false;
10201          for(e = exp.cond.exp->first; e; e = e.next)
10202          {
10203             if(!e.next)
10204             {
10205                FreeType(e.destType);
10206                e.destType = exp.destType;
10207                if(e.destType) e.destType.refCount++;
10208             }
10209             ProcessExpressionType(e);
10210             if(!e.next)
10211             {
10212                exp.expType = e.expType;
10213                if(e.expType) e.expType.refCount++;
10214             }
10215             if(!e.isConstant)
10216                exp.isConstant = false;
10217          }
10218
10219          FreeType(exp.cond.elseExp.destType);
10220          // Added this check if we failed to find an expType
10221          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10222
10223          // Reversed it...
10224          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10225
10226          if(exp.cond.elseExp.destType)
10227             exp.cond.elseExp.destType.refCount++;
10228          ProcessExpressionType(exp.cond.elseExp);
10229
10230          // FIXED THIS: Was done before calling process on elseExp
10231          if(!exp.cond.elseExp.isConstant)
10232             exp.isConstant = false;
10233          break;
10234       }
10235       case extensionCompoundExp:
10236       {
10237          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10238          {
10239             Statement last = exp.compound.compound.statements->last;
10240             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10241             {
10242                ((Expression)last.expressions->last).destType = exp.destType;
10243                if(exp.destType)
10244                   exp.destType.refCount++;
10245             }
10246             ProcessStatement(exp.compound);
10247             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10248             if(exp.expType)
10249                exp.expType.refCount++;
10250          }
10251          break;
10252       }
10253       case classExp:
10254       {
10255          Specifier spec = exp._classExp.specifiers->first;
10256          if(spec && spec.type == nameSpecifier)
10257          {
10258             exp.expType = MkClassType(spec.name);
10259             exp.expType.kind = subClassType;
10260             exp.byReference = true;
10261          }
10262          else
10263          {
10264             exp.expType = MkClassType("ecere::com::Class");
10265             exp.byReference = true;
10266          }
10267          break;
10268       }
10269       case classDataExp:
10270       {
10271          Class _class = thisClass ? thisClass : currentClass;
10272          if(_class)
10273          {
10274             Identifier id = exp.classData.id;
10275             char structName[1024];
10276             Expression classExp;
10277             strcpy(structName, "__ecereClassData_");
10278             FullClassNameCat(structName, _class.fullName, false);
10279             exp.type = pointerExp;
10280             exp.member.member = id;
10281             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10282                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10283             else
10284                classExp = MkExpIdentifier(MkIdentifier("class"));
10285
10286             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10287                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10288                   MkExpBrackets(MkListOne(MkExpOp(
10289                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10290                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10291                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10292                      )));
10293
10294             ProcessExpressionType(exp);
10295             return;
10296          }
10297          break;
10298       }
10299       case arrayExp:
10300       {
10301          Type type = null;
10302          char * typeString = null;
10303          char typeStringBuf[1024];
10304          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10305             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10306          {
10307             Class templateClass = exp.destType._class.registered;
10308             typeString = templateClass.templateArgs[2].dataTypeString;
10309          }
10310          else if(exp.list)
10311          {
10312             // Guess type from expressions in the array
10313             Expression e;
10314             for(e = exp.list->first; e; e = e.next)
10315             {
10316                ProcessExpressionType(e);
10317                if(e.expType)
10318                {
10319                   if(!type) { type = e.expType; type.refCount++; }
10320                   else
10321                   {
10322                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10323                      if(!MatchTypeExpression(e, type, null, false))
10324                      {
10325                         FreeType(type);
10326                         type = e.expType;
10327                         e.expType = null;
10328
10329                         e = exp.list->first;
10330                         ProcessExpressionType(e);
10331                         if(e.expType)
10332                         {
10333                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10334                            if(!MatchTypeExpression(e, type, null, false))
10335                            {
10336                               FreeType(e.expType);
10337                               e.expType = null;
10338                               FreeType(type);
10339                               type = null;
10340                               break;
10341                            }
10342                         }
10343                      }
10344                   }
10345                   if(e.expType)
10346                   {
10347                      FreeType(e.expType);
10348                      e.expType = null;
10349                   }
10350                }
10351             }
10352             if(type)
10353             {
10354                typeStringBuf[0] = '\0';
10355                PrintTypeNoConst(type, typeStringBuf, false, true);
10356                typeString = typeStringBuf;
10357                FreeType(type);
10358                type = null;
10359             }
10360          }
10361          if(typeString)
10362          {
10363             /*
10364             (Container)& (struct BuiltInContainer)
10365             {
10366                ._vTbl = class(BuiltInContainer)._vTbl,
10367                ._class = class(BuiltInContainer),
10368                .refCount = 0,
10369                .data = (int[]){ 1, 7, 3, 4, 5 },
10370                .count = 5,
10371                .type = class(int),
10372             }
10373             */
10374             char templateString[1024];
10375             OldList * initializers = MkList();
10376             OldList * structInitializers = MkList();
10377             OldList * specs = MkList();
10378             Expression expExt;
10379             Declarator decl = SpecDeclFromString(typeString, specs, null);
10380             sprintf(templateString, "Container<%s>", typeString);
10381
10382             if(exp.list)
10383             {
10384                Expression e;
10385                type = ProcessTypeString(typeString, false);
10386                while(e = exp.list->first)
10387                {
10388                   exp.list->Remove(e);
10389                   e.destType = type;
10390                   type.refCount++;
10391                   ProcessExpressionType(e);
10392                   ListAdd(initializers, MkInitializerAssignment(e));
10393                }
10394                FreeType(type);
10395                delete exp.list;
10396             }
10397
10398             DeclareStruct("ecere::com::BuiltInContainer", false);
10399
10400             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10401                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10402             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10403                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10404             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10405                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10406             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10407                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10408                MkInitializerList(initializers))));
10409                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10410             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10411                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10412             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10413                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10414             exp.expType = ProcessTypeString(templateString, false);
10415             exp.type = bracketsExp;
10416             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10417                MkExpOp(null, '&',
10418                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10419                   MkInitializerList(structInitializers)))));
10420             ProcessExpressionType(expExt);
10421          }
10422          else
10423          {
10424             exp.expType = ProcessTypeString("Container", false);
10425             Compiler_Error($"Couldn't determine type of array elements\n");
10426          }
10427          break;
10428       }
10429    }
10430
10431    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10432    {
10433       FreeType(exp.expType);
10434       exp.expType = ReplaceThisClassType(thisClass);
10435    }
10436
10437    // Resolve structures here
10438    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10439    {
10440       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10441       // TODO: Fix members reference...
10442       if(symbol)
10443       {
10444          if(exp.expType.kind != enumType)
10445          {
10446             Type member;
10447             String enumName = CopyString(exp.expType.enumName);
10448
10449             // Fixed a memory leak on self-referencing C structs typedefs
10450             // by instantiating a new type rather than simply copying members
10451             // into exp.expType
10452             FreeType(exp.expType);
10453             exp.expType = Type { };
10454             exp.expType.kind = symbol.type.kind;
10455             exp.expType.refCount++;
10456             exp.expType.enumName = enumName;
10457
10458             exp.expType.members = symbol.type.members;
10459             for(member = symbol.type.members.first; member; member = member.next)
10460                member.refCount++;
10461          }
10462          else
10463          {
10464             NamedLink member;
10465             for(member = symbol.type.members.first; member; member = member.next)
10466             {
10467                NamedLink value { name = CopyString(member.name) };
10468                exp.expType.members.Add(value);
10469             }
10470          }
10471       }
10472    }
10473
10474    yylloc = exp.loc;
10475    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10476    else if(exp.destType && !exp.destType.keepCast)
10477    {
10478       if(!CheckExpressionType(exp, exp.destType, false))
10479       {
10480          if(!exp.destType.count || unresolved)
10481          {
10482             if(!exp.expType)
10483             {
10484                yylloc = exp.loc;
10485                if(exp.destType.kind != ellipsisType)
10486                {
10487                   char type2[1024];
10488                   type2[0] = '\0';
10489                   if(inCompiler)
10490                   {
10491                      char expString[10240];
10492                      expString[0] = '\0';
10493
10494                      PrintType(exp.destType, type2, false, true);
10495
10496                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10497                      if(unresolved)
10498                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10499                      else if(exp.type != dummyExp)
10500                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10501                   }
10502                }
10503                else
10504                {
10505                   char expString[10240] ;
10506                   expString[0] = '\0';
10507                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10508
10509                   if(unresolved)
10510                      Compiler_Error($"unresolved identifier %s\n", expString);
10511                   else if(exp.type != dummyExp)
10512                      Compiler_Error($"couldn't determine type of %s\n", expString);
10513                }
10514             }
10515             else
10516             {
10517                char type1[1024];
10518                char type2[1024];
10519                type1[0] = '\0';
10520                type2[0] = '\0';
10521                if(inCompiler)
10522                {
10523                   PrintType(exp.expType, type1, false, true);
10524                   PrintType(exp.destType, type2, false, true);
10525                }
10526
10527                //CheckExpressionType(exp, exp.destType, false);
10528
10529                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10530                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
10531                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10532                else
10533                {
10534                   char expString[10240];
10535                   expString[0] = '\0';
10536                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10537
10538 #ifdef _DEBUG
10539                   CheckExpressionType(exp, exp.destType, false);
10540 #endif
10541                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10542                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10543                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10544
10545                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10546                   FreeType(exp.expType);
10547                   exp.destType.refCount++;
10548                   exp.expType = exp.destType;
10549                }
10550             }
10551          }
10552       }
10553       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10554       {
10555          Expression newExp { };
10556          char typeString[1024];
10557          OldList * specs = MkList();
10558          Declarator decl;
10559
10560          typeString[0] = '\0';
10561
10562          *newExp = *exp;
10563
10564          if(exp.expType)  exp.expType.refCount++;
10565          if(exp.expType)  exp.expType.refCount++;
10566          exp.type = castExp;
10567          newExp.destType = exp.expType;
10568
10569          PrintType(exp.expType, typeString, false, false);
10570          decl = SpecDeclFromString(typeString, specs, null);
10571
10572          exp.cast.typeName = MkTypeName(specs, decl);
10573          exp.cast.exp = newExp;
10574       }
10575    }
10576    else if(unresolved)
10577    {
10578       if(exp.identifier._class && exp.identifier._class.name)
10579          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10580       else if(exp.identifier.string && exp.identifier.string[0])
10581          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10582    }
10583    else if(!exp.expType && exp.type != dummyExp)
10584    {
10585       char expString[10240];
10586       expString[0] = '\0';
10587       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10588       Compiler_Error($"couldn't determine type of %s\n", expString);
10589    }
10590
10591    // Let's try to support any_object & typed_object here:
10592    if(inCompiler)
10593       ApplyAnyObjectLogic(exp);
10594
10595    // Mark nohead classes as by reference, unless we're casting them to an integral type
10596    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10597       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
10598          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
10599           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
10600    {
10601       exp.byReference = true;
10602    }
10603    yylloc = oldyylloc;
10604 }
10605
10606 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10607 {
10608    // THIS CODE WILL FIND NEXT MEMBER...
10609    if(*curMember)
10610    {
10611       *curMember = (*curMember).next;
10612
10613       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10614       {
10615          *curMember = subMemberStack[--(*subMemberStackPos)];
10616          *curMember = (*curMember).next;
10617       }
10618
10619       // SKIP ALL PROPERTIES HERE...
10620       while((*curMember) && (*curMember).isProperty)
10621          *curMember = (*curMember).next;
10622
10623       if(subMemberStackPos)
10624       {
10625          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10626          {
10627             subMemberStack[(*subMemberStackPos)++] = *curMember;
10628
10629             *curMember = (*curMember).members.first;
10630             while(*curMember && (*curMember).isProperty)
10631                *curMember = (*curMember).next;
10632          }
10633       }
10634    }
10635    while(!*curMember)
10636    {
10637       if(!*curMember)
10638       {
10639          if(subMemberStackPos && *subMemberStackPos)
10640          {
10641             *curMember = subMemberStack[--(*subMemberStackPos)];
10642             *curMember = (*curMember).next;
10643          }
10644          else
10645          {
10646             Class lastCurClass = *curClass;
10647
10648             if(*curClass == _class) break;     // REACHED THE END
10649
10650             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10651             *curMember = (*curClass).membersAndProperties.first;
10652          }
10653
10654          while((*curMember) && (*curMember).isProperty)
10655             *curMember = (*curMember).next;
10656          if(subMemberStackPos)
10657          {
10658             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10659             {
10660                subMemberStack[(*subMemberStackPos)++] = *curMember;
10661
10662                *curMember = (*curMember).members.first;
10663                while(*curMember && (*curMember).isProperty)
10664                   *curMember = (*curMember).next;
10665             }
10666          }
10667       }
10668    }
10669 }
10670
10671
10672 static void ProcessInitializer(Initializer init, Type type)
10673 {
10674    switch(init.type)
10675    {
10676       case expInitializer:
10677          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10678          {
10679             // TESTING THIS FOR SHUTTING = 0 WARNING
10680             if(init.exp && !init.exp.destType)
10681             {
10682                FreeType(init.exp.destType);
10683                init.exp.destType = type;
10684                if(type) type.refCount++;
10685             }
10686             if(init.exp)
10687             {
10688                ProcessExpressionType(init.exp);
10689                init.isConstant = init.exp.isConstant;
10690             }
10691             break;
10692          }
10693          else
10694          {
10695             Expression exp = init.exp;
10696             Instantiation inst = exp.instance;
10697             MembersInit members;
10698
10699             init.type = listInitializer;
10700             init.list = MkList();
10701
10702             if(inst.members)
10703             {
10704                for(members = inst.members->first; members; members = members.next)
10705                {
10706                   if(members.type == dataMembersInit)
10707                   {
10708                      MemberInit member;
10709                      for(member = members.dataMembers->first; member; member = member.next)
10710                      {
10711                         ListAdd(init.list, member.initializer);
10712                         member.initializer = null;
10713                      }
10714                   }
10715                   // Discard all MembersInitMethod
10716                }
10717             }
10718             FreeExpression(exp);
10719          }
10720       case listInitializer:
10721       {
10722          Initializer i;
10723          Type initializerType = null;
10724          Class curClass = null;
10725          DataMember curMember = null;
10726          DataMember subMemberStack[256];
10727          int subMemberStackPos = 0;
10728
10729          if(type && type.kind == arrayType)
10730             initializerType = Dereference(type);
10731          else if(type && (type.kind == structType || type.kind == unionType))
10732             initializerType = type.members.first;
10733
10734          for(i = init.list->first; i; i = i.next)
10735          {
10736             if(type && type.kind == classType && type._class && type._class.registered)
10737             {
10738                // THIS IS FOR A C STYLE INSTANTIATION OF STRUCT CLASSES ONLY... WE ONLY CARE ABOUT DATA MEMBERS, AND ACTUAL MEMORY ORDER (PRIVATE MEMBERS ARE INCLUDED)
10739                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
10740                // TODO: Generate error on initializing a private data member this way from another module...
10741                if(curMember)
10742                {
10743                   if(!curMember.dataType)
10744                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
10745                   initializerType = curMember.dataType;
10746                }
10747             }
10748             ProcessInitializer(i, initializerType);
10749             if(initializerType && type && (type.kind == structType || type.kind == unionType))
10750                initializerType = initializerType.next;
10751             if(!i.isConstant)
10752                init.isConstant = false;
10753          }
10754
10755          if(type && type.kind == arrayType)
10756             FreeType(initializerType);
10757
10758          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
10759          {
10760             Compiler_Error($"Assigning list initializer to non list\n");
10761          }
10762          break;
10763       }
10764    }
10765 }
10766
10767 static void ProcessSpecifier(Specifier spec, bool declareStruct)
10768 {
10769    switch(spec.type)
10770    {
10771       case baseSpecifier:
10772       {
10773          if(spec.specifier == THISCLASS)
10774          {
10775             if(thisClass)
10776             {
10777                spec.type = nameSpecifier;
10778                spec.name = ReplaceThisClass(thisClass);
10779                spec.symbol = FindClass(spec.name);
10780                ProcessSpecifier(spec, declareStruct);
10781             }
10782          }
10783          break;
10784       }
10785       case nameSpecifier:
10786       {
10787          Symbol symbol = FindType(curContext, spec.name);
10788          if(symbol)
10789             DeclareType(symbol.type, true, true);
10790          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
10791             DeclareStruct(spec.name, false);
10792          break;
10793       }
10794       case enumSpecifier:
10795       {
10796          Enumerator e;
10797          if(spec.list)
10798          {
10799             for(e = spec.list->first; e; e = e.next)
10800             {
10801                if(e.exp)
10802                   ProcessExpressionType(e.exp);
10803             }
10804          }
10805          break;
10806       }
10807       case structSpecifier:
10808       case unionSpecifier:
10809       {
10810          if(spec.definitions)
10811          {
10812             ClassDef def;
10813             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
10814             //if(symbol)
10815                ProcessClass(spec.definitions, symbol);
10816             /*else
10817             {
10818                for(def = spec.definitions->first; def; def = def.next)
10819                {
10820                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
10821                      ProcessDeclaration(def.decl);
10822                }
10823             }*/
10824          }
10825          break;
10826       }
10827       /*
10828       case classSpecifier:
10829       {
10830          Symbol classSym = FindClass(spec.name);
10831          if(classSym && classSym.registered && classSym.registered.type == structClass)
10832             DeclareStruct(spec.name, false);
10833          break;
10834       }
10835       */
10836    }
10837 }
10838
10839
10840 static void ProcessDeclarator(Declarator decl)
10841 {
10842    switch(decl.type)
10843    {
10844       case identifierDeclarator:
10845          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
10846          {
10847             FreeSpecifier(decl.identifier._class);
10848             decl.identifier._class = null;
10849          }
10850          break;
10851       case arrayDeclarator:
10852          if(decl.array.exp)
10853             ProcessExpressionType(decl.array.exp);
10854       case structDeclarator:
10855       case bracketsDeclarator:
10856       case functionDeclarator:
10857       case pointerDeclarator:
10858       case extendedDeclarator:
10859       case extendedDeclaratorEnd:
10860          if(decl.declarator)
10861             ProcessDeclarator(decl.declarator);
10862          if(decl.type == functionDeclarator)
10863          {
10864             Identifier id = GetDeclId(decl);
10865             if(id && id._class)
10866             {
10867                TypeName param
10868                {
10869                   qualifiers = MkListOne(id._class);
10870                   declarator = null;
10871                };
10872                if(!decl.function.parameters)
10873                   decl.function.parameters = MkList();
10874                decl.function.parameters->Insert(null, param);
10875                id._class = null;
10876             }
10877             if(decl.function.parameters)
10878             {
10879                TypeName param;
10880
10881                for(param = decl.function.parameters->first; param; param = param.next)
10882                {
10883                   if(param.qualifiers && param.qualifiers->first)
10884                   {
10885                      Specifier spec = param.qualifiers->first;
10886                      if(spec && spec.specifier == TYPED_OBJECT)
10887                      {
10888                         Declarator d = param.declarator;
10889                         TypeName newParam
10890                         {
10891                            qualifiers = MkListOne(MkSpecifier(VOID));
10892                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10893                         };
10894
10895                         FreeList(param.qualifiers, FreeSpecifier);
10896
10897                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
10898                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
10899
10900                         decl.function.parameters->Insert(param, newParam);
10901                         param = newParam;
10902                      }
10903                      else if(spec && spec.specifier == ANY_OBJECT)
10904                      {
10905                         Declarator d = param.declarator;
10906
10907                         FreeList(param.qualifiers, FreeSpecifier);
10908
10909                         param.qualifiers = MkListOne(MkSpecifier(VOID));
10910                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
10911                      }
10912                      else if(spec.specifier == THISCLASS)
10913                      {
10914                         if(thisClass)
10915                         {
10916                            spec.type = nameSpecifier;
10917                            spec.name = ReplaceThisClass(thisClass);
10918                            spec.symbol = FindClass(spec.name);
10919                            ProcessSpecifier(spec, false);
10920                         }
10921                      }
10922                   }
10923
10924                   if(param.declarator)
10925                      ProcessDeclarator(param.declarator);
10926                }
10927             }
10928          }
10929          break;
10930    }
10931 }
10932
10933 static void ProcessDeclaration(Declaration decl)
10934 {
10935    yylloc = decl.loc;
10936    switch(decl.type)
10937    {
10938       case initDeclaration:
10939       {
10940          bool declareStruct = false;
10941          /*
10942          lineNum = decl.pos.line;
10943          column = decl.pos.col;
10944          */
10945
10946          if(decl.declarators)
10947          {
10948             InitDeclarator d;
10949
10950             for(d = decl.declarators->first; d; d = d.next)
10951             {
10952                Type type, subType;
10953                ProcessDeclarator(d.declarator);
10954
10955                type = ProcessType(decl.specifiers, d.declarator);
10956
10957                if(d.initializer)
10958                {
10959                   ProcessInitializer(d.initializer, type);
10960
10961                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
10962
10963                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
10964                      d.initializer.exp.type == instanceExp)
10965                   {
10966                      if(type.kind == classType && type._class ==
10967                         d.initializer.exp.expType._class)
10968                      {
10969                         Instantiation inst = d.initializer.exp.instance;
10970                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
10971
10972                         d.initializer.exp.instance = null;
10973                         if(decl.specifiers)
10974                            FreeList(decl.specifiers, FreeSpecifier);
10975                         FreeList(decl.declarators, FreeInitDeclarator);
10976
10977                         d = null;
10978
10979                         decl.type = instDeclaration;
10980                         decl.inst = inst;
10981                      }
10982                   }
10983                }
10984                for(subType = type; subType;)
10985                {
10986                   if(subType.kind == classType)
10987                   {
10988                      declareStruct = true;
10989                      break;
10990                   }
10991                   else if(subType.kind == pointerType)
10992                      break;
10993                   else if(subType.kind == arrayType)
10994                      subType = subType.arrayType;
10995                   else
10996                      break;
10997                }
10998
10999                FreeType(type);
11000                if(!d) break;
11001             }
11002          }
11003
11004          if(decl.specifiers)
11005          {
11006             Specifier s;
11007             for(s = decl.specifiers->first; s; s = s.next)
11008             {
11009                ProcessSpecifier(s, declareStruct);
11010             }
11011          }
11012          break;
11013       }
11014       case instDeclaration:
11015       {
11016          ProcessInstantiationType(decl.inst);
11017          break;
11018       }
11019       case structDeclaration:
11020       {
11021          Specifier spec;
11022          Declarator d;
11023          bool declareStruct = false;
11024
11025          if(decl.declarators)
11026          {
11027             for(d = decl.declarators->first; d; d = d.next)
11028             {
11029                Type type = ProcessType(decl.specifiers, d.declarator);
11030                Type subType;
11031                ProcessDeclarator(d);
11032                for(subType = type; subType;)
11033                {
11034                   if(subType.kind == classType)
11035                   {
11036                      declareStruct = true;
11037                      break;
11038                   }
11039                   else if(subType.kind == pointerType)
11040                      break;
11041                   else if(subType.kind == arrayType)
11042                      subType = subType.arrayType;
11043                   else
11044                      break;
11045                }
11046                FreeType(type);
11047             }
11048          }
11049          if(decl.specifiers)
11050          {
11051             for(spec = decl.specifiers->first; spec; spec = spec.next)
11052                ProcessSpecifier(spec, declareStruct);
11053          }
11054          break;
11055       }
11056    }
11057 }
11058
11059 static FunctionDefinition curFunction;
11060
11061 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11062 {
11063    char propName[1024], propNameM[1024];
11064    char getName[1024], setName[1024];
11065    OldList * args;
11066
11067    DeclareProperty(prop, setName, getName);
11068
11069    // eInstance_FireWatchers(object, prop);
11070    strcpy(propName, "__ecereProp_");
11071    FullClassNameCat(propName, prop._class.fullName, false);
11072    strcat(propName, "_");
11073    // strcat(propName, prop.name);
11074    FullClassNameCat(propName, prop.name, true);
11075    MangleClassName(propName);
11076
11077    strcpy(propNameM, "__ecerePropM_");
11078    FullClassNameCat(propNameM, prop._class.fullName, false);
11079    strcat(propNameM, "_");
11080    // strcat(propNameM, prop.name);
11081    FullClassNameCat(propNameM, prop.name, true);
11082    MangleClassName(propNameM);
11083
11084    if(prop.isWatchable)
11085    {
11086       args = MkList();
11087       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11088       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11089       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11090
11091       args = MkList();
11092       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11093       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11094       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11095    }
11096
11097
11098    {
11099       args = MkList();
11100       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11101       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11102       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11103
11104       args = MkList();
11105       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11106       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11107       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11108    }
11109
11110    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11111       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11112       curFunction.propSet.fireWatchersDone = true;
11113 }
11114
11115 static void ProcessStatement(Statement stmt)
11116 {
11117    yylloc = stmt.loc;
11118    /*
11119    lineNum = stmt.pos.line;
11120    column = stmt.pos.col;
11121    */
11122    switch(stmt.type)
11123    {
11124       case labeledStmt:
11125          ProcessStatement(stmt.labeled.stmt);
11126          break;
11127       case caseStmt:
11128          // This expression should be constant...
11129          if(stmt.caseStmt.exp)
11130          {
11131             FreeType(stmt.caseStmt.exp.destType);
11132             stmt.caseStmt.exp.destType = curSwitchType;
11133             if(curSwitchType) curSwitchType.refCount++;
11134             ProcessExpressionType(stmt.caseStmt.exp);
11135             ComputeExpression(stmt.caseStmt.exp);
11136          }
11137          if(stmt.caseStmt.stmt)
11138             ProcessStatement(stmt.caseStmt.stmt);
11139          break;
11140       case compoundStmt:
11141       {
11142          if(stmt.compound.context)
11143          {
11144             Declaration decl;
11145             Statement s;
11146
11147             Statement prevCompound = curCompound;
11148             Context prevContext = curContext;
11149
11150             if(!stmt.compound.isSwitch)
11151                curCompound = stmt;
11152             curContext = stmt.compound.context;
11153
11154             if(stmt.compound.declarations)
11155             {
11156                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11157                   ProcessDeclaration(decl);
11158             }
11159             if(stmt.compound.statements)
11160             {
11161                for(s = stmt.compound.statements->first; s; s = s.next)
11162                   ProcessStatement(s);
11163             }
11164
11165             curContext = prevContext;
11166             curCompound = prevCompound;
11167          }
11168          break;
11169       }
11170       case expressionStmt:
11171       {
11172          Expression exp;
11173          if(stmt.expressions)
11174          {
11175             for(exp = stmt.expressions->first; exp; exp = exp.next)
11176                ProcessExpressionType(exp);
11177          }
11178          break;
11179       }
11180       case ifStmt:
11181       {
11182          Expression exp;
11183
11184          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11185          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11186          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11187          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11188          {
11189             ProcessExpressionType(exp);
11190          }
11191          if(stmt.ifStmt.stmt)
11192             ProcessStatement(stmt.ifStmt.stmt);
11193          if(stmt.ifStmt.elseStmt)
11194             ProcessStatement(stmt.ifStmt.elseStmt);
11195          break;
11196       }
11197       case switchStmt:
11198       {
11199          Type oldSwitchType = curSwitchType;
11200          if(stmt.switchStmt.exp)
11201          {
11202             Expression exp;
11203             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11204             {
11205                if(!exp.next)
11206                {
11207                   /*
11208                   Type destType
11209                   {
11210                      kind = intType;
11211                      refCount = 1;
11212                   };
11213                   e.exp.destType = destType;
11214                   */
11215
11216                   ProcessExpressionType(exp);
11217                }
11218                if(!exp.next)
11219                   curSwitchType = exp.expType;
11220             }
11221          }
11222          ProcessStatement(stmt.switchStmt.stmt);
11223          curSwitchType = oldSwitchType;
11224          break;
11225       }
11226       case whileStmt:
11227       {
11228          if(stmt.whileStmt.exp)
11229          {
11230             Expression exp;
11231
11232             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11233             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11234             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11235             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11236             {
11237                ProcessExpressionType(exp);
11238             }
11239          }
11240          if(stmt.whileStmt.stmt)
11241             ProcessStatement(stmt.whileStmt.stmt);
11242          break;
11243       }
11244       case doWhileStmt:
11245       {
11246          if(stmt.doWhile.exp)
11247          {
11248             Expression exp;
11249
11250             if(stmt.doWhile.exp->last)
11251             {
11252                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11253                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11254                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11255             }
11256             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11257             {
11258                ProcessExpressionType(exp);
11259             }
11260          }
11261          if(stmt.doWhile.stmt)
11262             ProcessStatement(stmt.doWhile.stmt);
11263          break;
11264       }
11265       case forStmt:
11266       {
11267          Expression exp;
11268          if(stmt.forStmt.init)
11269             ProcessStatement(stmt.forStmt.init);
11270
11271          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11272          {
11273             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11274             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11275             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11276          }
11277
11278          if(stmt.forStmt.check)
11279             ProcessStatement(stmt.forStmt.check);
11280          if(stmt.forStmt.increment)
11281          {
11282             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11283                ProcessExpressionType(exp);
11284          }
11285
11286          if(stmt.forStmt.stmt)
11287             ProcessStatement(stmt.forStmt.stmt);
11288          break;
11289       }
11290       case forEachStmt:
11291       {
11292          Identifier id = stmt.forEachStmt.id;
11293          OldList * exp = stmt.forEachStmt.exp;
11294          OldList * filter = stmt.forEachStmt.filter;
11295          Statement block = stmt.forEachStmt.stmt;
11296          char iteratorType[1024];
11297          Type source;
11298          Expression e;
11299          bool isBuiltin = exp && exp->last &&
11300             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11301               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11302          Expression arrayExp;
11303          char * typeString = null;
11304          int builtinCount = 0;
11305
11306          for(e = exp ? exp->first : null; e; e = e.next)
11307          {
11308             if(!e.next)
11309             {
11310                FreeType(e.destType);
11311                e.destType = ProcessTypeString("Container", false);
11312             }
11313             if(!isBuiltin || e.next)
11314                ProcessExpressionType(e);
11315          }
11316
11317          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11318          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11319             eClass_IsDerived(source._class.registered, containerClass)))
11320          {
11321             Class _class = source ? source._class.registered : null;
11322             Symbol symbol;
11323             Expression expIt = null;
11324             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11325             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11326             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11327             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11328             stmt.type = compoundStmt;
11329
11330             stmt.compound.context = Context { };
11331             stmt.compound.context.parent = curContext;
11332             curContext = stmt.compound.context;
11333
11334             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11335             {
11336                Class mapClass = eSystem_FindClass(privateModule, "Map");
11337                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11338                isCustomAVLTree = true;
11339                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11340                   isAVLTree = true;
11341                else if(eClass_IsDerived(source._class.registered, mapClass))
11342                   isMap = true;
11343             }
11344             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11345             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11346             {
11347                Class listClass = eSystem_FindClass(privateModule, "List");
11348                isLinkList = true;
11349                isList = eClass_IsDerived(source._class.registered, listClass);
11350             }
11351
11352             if(isArray)
11353             {
11354                Declarator decl;
11355                OldList * specs = MkList();
11356                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11357                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11358                stmt.compound.declarations = MkListOne(
11359                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11360                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11361                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11362                      MkInitializerAssignment(MkExpBrackets(exp))))));
11363             }
11364             else if(isBuiltin)
11365             {
11366                Type type = null;
11367                char typeStringBuf[1024];
11368
11369                // TODO: Merge this code?
11370                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11371                if(((Expression)exp->last).type == castExp)
11372                {
11373                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11374                   if(typeName)
11375                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11376                }
11377
11378                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11379                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11380                   arrayExp.destType._class.registered.templateArgs)
11381                {
11382                   Class templateClass = arrayExp.destType._class.registered;
11383                   typeString = templateClass.templateArgs[2].dataTypeString;
11384                }
11385                else if(arrayExp.list)
11386                {
11387                   // Guess type from expressions in the array
11388                   Expression e;
11389                   for(e = arrayExp.list->first; e; e = e.next)
11390                   {
11391                      ProcessExpressionType(e);
11392                      if(e.expType)
11393                      {
11394                         if(!type) { type = e.expType; type.refCount++; }
11395                         else
11396                         {
11397                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11398                            if(!MatchTypeExpression(e, type, null, false))
11399                            {
11400                               FreeType(type);
11401                               type = e.expType;
11402                               e.expType = null;
11403
11404                               e = arrayExp.list->first;
11405                               ProcessExpressionType(e);
11406                               if(e.expType)
11407                               {
11408                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11409                                  if(!MatchTypeExpression(e, type, null, false))
11410                                  {
11411                                     FreeType(e.expType);
11412                                     e.expType = null;
11413                                     FreeType(type);
11414                                     type = null;
11415                                     break;
11416                                  }
11417                               }
11418                            }
11419                         }
11420                         if(e.expType)
11421                         {
11422                            FreeType(e.expType);
11423                            e.expType = null;
11424                         }
11425                      }
11426                   }
11427                   if(type)
11428                   {
11429                      typeStringBuf[0] = '\0';
11430                      PrintType(type, typeStringBuf, false, true);
11431                      typeString = typeStringBuf;
11432                      FreeType(type);
11433                   }
11434                }
11435                if(typeString)
11436                {
11437                   OldList * initializers = MkList();
11438                   Declarator decl;
11439                   OldList * specs = MkList();
11440                   if(arrayExp.list)
11441                   {
11442                      Expression e;
11443
11444                      builtinCount = arrayExp.list->count;
11445                      type = ProcessTypeString(typeString, false);
11446                      while(e = arrayExp.list->first)
11447                      {
11448                         arrayExp.list->Remove(e);
11449                         e.destType = type;
11450                         type.refCount++;
11451                         ProcessExpressionType(e);
11452                         ListAdd(initializers, MkInitializerAssignment(e));
11453                      }
11454                      FreeType(type);
11455                      delete arrayExp.list;
11456                   }
11457                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11458                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
11459                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11460
11461                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11462                      PlugDeclarator(
11463                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11464                         ), MkInitializerList(initializers)))));
11465                   FreeList(exp, FreeExpression);
11466                }
11467                else
11468                {
11469                   arrayExp.expType = ProcessTypeString("Container", false);
11470                   Compiler_Error($"Couldn't determine type of array elements\n");
11471                }
11472
11473                /*
11474                Declarator decl;
11475                OldList * specs = MkList();
11476
11477                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11478                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11479                stmt.compound.declarations = MkListOne(
11480                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11481                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11482                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
11483                      MkInitializerAssignment(MkExpBrackets(exp))))));
11484                */
11485             }
11486             else if(isLinkList && !isList)
11487             {
11488                Declarator decl;
11489                OldList * specs = MkList();
11490                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11491                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11492                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11493                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
11494                      MkInitializerAssignment(MkExpBrackets(exp))))));
11495             }
11496             /*else if(isCustomAVLTree)
11497             {
11498                Declarator decl;
11499                OldList * specs = MkList();
11500                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11501                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11502                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11503                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
11504                      MkInitializerAssignment(MkExpBrackets(exp))))));
11505             }*/
11506             else if(_class.templateArgs)
11507             {
11508                if(isMap)
11509                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11510                else
11511                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11512
11513                stmt.compound.declarations = MkListOne(
11514                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11515                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
11516                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11517             }
11518             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11519
11520             if(block)
11521             {
11522                // Reparent sub-contexts in this statement
11523                switch(block.type)
11524                {
11525                   case compoundStmt:
11526                      if(block.compound.context)
11527                         block.compound.context.parent = stmt.compound.context;
11528                      break;
11529                   case ifStmt:
11530                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
11531                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
11532                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
11533                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
11534                      break;
11535                   case switchStmt:
11536                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
11537                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
11538                      break;
11539                   case whileStmt:
11540                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
11541                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
11542                      break;
11543                   case doWhileStmt:
11544                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
11545                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
11546                      break;
11547                   case forStmt:
11548                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
11549                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
11550                      break;
11551                   case forEachStmt:
11552                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
11553                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
11554                      break;
11555                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
11556                   case labeledStmt:
11557                   case caseStmt
11558                   case expressionStmt:
11559                   case gotoStmt:
11560                   case continueStmt:
11561                   case breakStmt
11562                   case returnStmt:
11563                   case asmStmt:
11564                   case badDeclarationStmt:
11565                   case fireWatchersStmt:
11566                   case stopWatchingStmt:
11567                   case watchStmt:
11568                   */
11569                }
11570             }
11571             if(filter)
11572             {
11573                block = MkIfStmt(filter, block, null);
11574             }
11575             if(isArray)
11576             {
11577                stmt.compound.statements = MkListOne(MkForStmt(
11578                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11579                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11580                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11581                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11582                   block));
11583               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11584               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11585               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11586             }
11587             else if(isBuiltin)
11588             {
11589                char count[128];
11590                //OldList * specs = MkList();
11591                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11592
11593                sprintf(count, "%d", builtinCount);
11594
11595                stmt.compound.statements = MkListOne(MkForStmt(
11596                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11597                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11598                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11599                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11600                   block));
11601
11602                /*
11603                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11604                stmt.compound.statements = MkListOne(MkForStmt(
11605                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11606                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11607                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11608                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11609                   block));
11610               */
11611               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11612               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11613               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11614             }
11615             else if(isLinkList && !isList)
11616             {
11617                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11618                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11619                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
11620                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11621                {
11622                   stmt.compound.statements = MkListOne(MkForStmt(
11623                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11624                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11625                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11626                      block));
11627                }
11628                else
11629                {
11630                   OldList * specs = MkList();
11631                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11632                   stmt.compound.statements = MkListOne(MkForStmt(
11633                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11634                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11635                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11636                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11637                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11638                      block));
11639                }
11640                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11641                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11642                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11643             }
11644             /*else if(isCustomAVLTree)
11645             {
11646                stmt.compound.statements = MkListOne(MkForStmt(
11647                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11648                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11649                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11650                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11651                   block));
11652
11653                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11654                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11655                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11656             }*/
11657             else
11658             {
11659                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11660                   MkIdentifier("Next")), null)), block));
11661             }
11662             ProcessExpressionType(expIt);
11663             if(stmt.compound.declarations->first)
11664                ProcessDeclaration(stmt.compound.declarations->first);
11665
11666             if(symbol)
11667                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11668
11669             ProcessStatement(stmt);
11670             curContext = stmt.compound.context.parent;
11671             break;
11672          }
11673          else
11674          {
11675             Compiler_Error($"Expression is not a container\n");
11676          }
11677          break;
11678       }
11679       case gotoStmt:
11680          break;
11681       case continueStmt:
11682          break;
11683       case breakStmt:
11684          break;
11685       case returnStmt:
11686       {
11687          Expression exp;
11688          if(stmt.expressions)
11689          {
11690             for(exp = stmt.expressions->first; exp; exp = exp.next)
11691             {
11692                if(!exp.next)
11693                {
11694                   if(curFunction && !curFunction.type)
11695                      curFunction.type = ProcessType(
11696                         curFunction.specifiers, curFunction.declarator);
11697                   FreeType(exp.destType);
11698                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
11699                   if(exp.destType) exp.destType.refCount++;
11700                }
11701                ProcessExpressionType(exp);
11702             }
11703          }
11704          break;
11705       }
11706       case badDeclarationStmt:
11707       {
11708          ProcessDeclaration(stmt.decl);
11709          break;
11710       }
11711       case asmStmt:
11712       {
11713          AsmField field;
11714          if(stmt.asmStmt.inputFields)
11715          {
11716             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
11717                if(field.expression)
11718                   ProcessExpressionType(field.expression);
11719          }
11720          if(stmt.asmStmt.outputFields)
11721          {
11722             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
11723                if(field.expression)
11724                   ProcessExpressionType(field.expression);
11725          }
11726          if(stmt.asmStmt.clobberedFields)
11727          {
11728             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
11729             {
11730                if(field.expression)
11731                   ProcessExpressionType(field.expression);
11732             }
11733          }
11734          break;
11735       }
11736       case watchStmt:
11737       {
11738          PropertyWatch propWatch;
11739          OldList * watches = stmt._watch.watches;
11740          Expression object = stmt._watch.object;
11741          Expression watcher = stmt._watch.watcher;
11742          if(watcher)
11743             ProcessExpressionType(watcher);
11744          if(object)
11745             ProcessExpressionType(object);
11746
11747          if(inCompiler)
11748          {
11749             if(watcher || thisClass)
11750             {
11751                External external = curExternal;
11752                Context context = curContext;
11753
11754                stmt.type = expressionStmt;
11755                stmt.expressions = MkList();
11756
11757                curExternal = external.prev;
11758
11759                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11760                {
11761                   ClassFunction func;
11762                   char watcherName[1024];
11763                   Class watcherClass = watcher ?
11764                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
11765                   External createdExternal;
11766
11767                   // Create a declaration above
11768                   External externalDecl = MkExternalDeclaration(null);
11769                   ast->Insert(curExternal.prev, externalDecl);
11770
11771                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
11772                   if(propWatch.deleteWatch)
11773                      strcat(watcherName, "_delete");
11774                   else
11775                   {
11776                      Identifier propID;
11777                      for(propID = propWatch.properties->first; propID; propID = propID.next)
11778                      {
11779                         strcat(watcherName, "_");
11780                         strcat(watcherName, propID.string);
11781                      }
11782                   }
11783
11784                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
11785                   {
11786                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
11787                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
11788                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
11789                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
11790                      ProcessClassFunctionBody(func, propWatch.compound);
11791                      propWatch.compound = null;
11792
11793                      //afterExternal = afterExternal ? afterExternal : curExternal;
11794
11795                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
11796                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
11797                      // TESTING THIS...
11798                      createdExternal.symbol.idCode = external.symbol.idCode;
11799
11800                      curExternal = createdExternal;
11801                      ProcessFunction(createdExternal.function);
11802
11803
11804                      // Create a declaration above
11805                      {
11806                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
11807                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
11808                         externalDecl.declaration = decl;
11809                         if(decl.symbol && !decl.symbol.pointerExternal)
11810                            decl.symbol.pointerExternal = externalDecl;
11811                      }
11812
11813                      if(propWatch.deleteWatch)
11814                      {
11815                         OldList * args = MkList();
11816                         ListAdd(args, CopyExpression(object));
11817                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11818                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11819                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
11820                      }
11821                      else
11822                      {
11823                         Class _class = object.expType._class.registered;
11824                         Identifier propID;
11825
11826                         for(propID = propWatch.properties->first; propID; propID = propID.next)
11827                         {
11828                            char propName[1024];
11829                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11830                            if(prop)
11831                            {
11832                               char getName[1024], setName[1024];
11833                               OldList * args = MkList();
11834
11835                               DeclareProperty(prop, setName, getName);
11836
11837                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
11838                               strcpy(propName, "__ecereProp_");
11839                               FullClassNameCat(propName, prop._class.fullName, false);
11840                               strcat(propName, "_");
11841                               // strcat(propName, prop.name);
11842                               FullClassNameCat(propName, prop.name, true);
11843
11844                               ListAdd(args, CopyExpression(object));
11845                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11846                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11847                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11848
11849                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
11850                            }
11851                            else
11852                               Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
11853                         }
11854                      }
11855                   }
11856                   else
11857                      Compiler_Error($"Invalid watched object\n");
11858                }
11859
11860                curExternal = external;
11861                curContext = context;
11862
11863                if(watcher)
11864                   FreeExpression(watcher);
11865                if(object)
11866                   FreeExpression(object);
11867                FreeList(watches, FreePropertyWatch);
11868             }
11869             else
11870                Compiler_Error($"No observer specified and not inside a _class\n");
11871          }
11872          else
11873          {
11874             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11875             {
11876                ProcessStatement(propWatch.compound);
11877             }
11878
11879          }
11880          break;
11881       }
11882       case fireWatchersStmt:
11883       {
11884          OldList * watches = stmt._watch.watches;
11885          Expression object = stmt._watch.object;
11886          Class _class;
11887          // DEBUGGER BUG: Why doesn't watches evaluate to null??
11888          // printf("%X\n", watches);
11889          // printf("%X\n", stmt._watch.watches);
11890          if(object)
11891             ProcessExpressionType(object);
11892
11893          if(inCompiler)
11894          {
11895             _class = object ?
11896                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
11897
11898             if(_class)
11899             {
11900                Identifier propID;
11901
11902                stmt.type = expressionStmt;
11903                stmt.expressions = MkList();
11904
11905                // Check if we're inside a property set
11906                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11907                {
11908                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
11909                }
11910                else if(!watches)
11911                {
11912                   //Compiler_Error($"No property specified and not inside a property set\n");
11913                }
11914                if(watches)
11915                {
11916                   for(propID = watches->first; propID; propID = propID.next)
11917                   {
11918                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11919                      if(prop)
11920                      {
11921                         CreateFireWatcher(prop, object, stmt);
11922                      }
11923                      else
11924                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
11925                   }
11926                }
11927                else
11928                {
11929                   // Fire all properties!
11930                   Property prop;
11931                   Class base;
11932                   for(base = _class; base; base = base.base)
11933                   {
11934                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
11935                      {
11936                         if(prop.isProperty && prop.isWatchable)
11937                         {
11938                            CreateFireWatcher(prop, object, stmt);
11939                         }
11940                      }
11941                   }
11942                }
11943
11944                if(object)
11945                   FreeExpression(object);
11946                FreeList(watches, FreeIdentifier);
11947             }
11948             else
11949                Compiler_Error($"Invalid object specified and not inside a class\n");
11950          }
11951          break;
11952       }
11953       case stopWatchingStmt:
11954       {
11955          OldList * watches = stmt._watch.watches;
11956          Expression object = stmt._watch.object;
11957          Expression watcher = stmt._watch.watcher;
11958          Class _class;
11959          if(object)
11960             ProcessExpressionType(object);
11961          if(watcher)
11962             ProcessExpressionType(watcher);
11963          if(inCompiler)
11964          {
11965             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
11966
11967             if(watcher || thisClass)
11968             {
11969                if(_class)
11970                {
11971                   Identifier propID;
11972
11973                   stmt.type = expressionStmt;
11974                   stmt.expressions = MkList();
11975
11976                   if(!watches)
11977                   {
11978                      OldList * args;
11979                      // eInstance_StopWatching(object, null, watcher);
11980                      args = MkList();
11981                      ListAdd(args, CopyExpression(object));
11982                      ListAdd(args, MkExpConstant("0"));
11983                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11984                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
11985                   }
11986                   else
11987                   {
11988                      for(propID = watches->first; propID; propID = propID.next)
11989                      {
11990                         char propName[1024];
11991                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
11992                         if(prop)
11993                         {
11994                            char getName[1024], setName[1024];
11995                            OldList * args = MkList();
11996
11997                            DeclareProperty(prop, setName, getName);
11998
11999                            // eInstance_StopWatching(object, prop, watcher);
12000                            strcpy(propName, "__ecereProp_");
12001                            FullClassNameCat(propName, prop._class.fullName, false);
12002                            strcat(propName, "_");
12003                            // strcat(propName, prop.name);
12004                            FullClassNameCat(propName, prop.name, true);
12005                            MangleClassName(propName);
12006
12007                            ListAdd(args, CopyExpression(object));
12008                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12009                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12010                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12011                         }
12012                         else
12013                            Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
12014                      }
12015                   }
12016
12017                   if(object)
12018                      FreeExpression(object);
12019                   if(watcher)
12020                      FreeExpression(watcher);
12021                   FreeList(watches, FreeIdentifier);
12022                }
12023                else
12024                   Compiler_Error($"Invalid object specified and not inside a class\n");
12025             }
12026             else
12027                Compiler_Error($"No observer specified and not inside a class\n");
12028          }
12029          break;
12030       }
12031    }
12032 }
12033
12034 static void ProcessFunction(FunctionDefinition function)
12035 {
12036    Identifier id = GetDeclId(function.declarator);
12037    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12038    Type type = symbol ? symbol.type : null;
12039    Class oldThisClass = thisClass;
12040    Context oldTopContext = topContext;
12041
12042    yylloc = function.loc;
12043    // Process thisClass
12044
12045    if(type && type.thisClass)
12046    {
12047       Symbol classSym = type.thisClass;
12048       Class _class = type.thisClass.registered;
12049       char className[1024];
12050       char structName[1024];
12051       Declarator funcDecl;
12052       Symbol thisSymbol;
12053
12054       bool typedObject = false;
12055
12056       if(_class && !_class.base)
12057       {
12058          _class = currentClass;
12059          if(_class && !_class.symbol)
12060             _class.symbol = FindClass(_class.fullName);
12061          classSym = _class ? _class.symbol : null;
12062          typedObject = true;
12063       }
12064
12065       thisClass = _class;
12066
12067       if(inCompiler && _class)
12068       {
12069          if(type.kind == functionType)
12070          {
12071             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12072             {
12073                //TypeName param = symbol.type.params.first;
12074                Type param = symbol.type.params.first;
12075                symbol.type.params.Remove(param);
12076                //FreeTypeName(param);
12077                FreeType(param);
12078             }
12079             if(type.classObjectType != classPointer)
12080             {
12081                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12082                symbol.type.staticMethod = true;
12083                symbol.type.thisClass = null;
12084
12085                // HIGH DANGER: VERIFYING THIS...
12086                symbol.type.extraParam = false;
12087             }
12088          }
12089
12090          strcpy(className, "__ecereClass_");
12091          FullClassNameCat(className, _class.fullName, true);
12092
12093          MangleClassName(className);
12094
12095          structName[0] = 0;
12096          FullClassNameCat(structName, _class.fullName, false);
12097
12098          // [class] this
12099
12100
12101          funcDecl = GetFuncDecl(function.declarator);
12102          if(funcDecl)
12103          {
12104             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12105             {
12106                TypeName param = funcDecl.function.parameters->first;
12107                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12108                {
12109                   funcDecl.function.parameters->Remove(param);
12110                   FreeTypeName(param);
12111                }
12112             }
12113
12114             // DANGER: Watch for this... Check if it's a Conversion?
12115             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12116
12117             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12118             if(!function.propertyNoThis)
12119             {
12120                TypeName thisParam;
12121
12122                if(type.classObjectType != classPointer)
12123                {
12124                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12125                   if(!funcDecl.function.parameters)
12126                      funcDecl.function.parameters = MkList();
12127                   funcDecl.function.parameters->Insert(null, thisParam);
12128                }
12129
12130                if(typedObject)
12131                {
12132                   if(type.classObjectType != classPointer)
12133                   {
12134                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12135                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12136                   }
12137
12138                   thisParam = TypeName
12139                   {
12140                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12141                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12142                   };
12143                   funcDecl.function.parameters->Insert(null, thisParam);
12144                }
12145             }
12146          }
12147
12148          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12149          {
12150             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12151             funcDecl = GetFuncDecl(initDecl.declarator);
12152             if(funcDecl)
12153             {
12154                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12155                {
12156                   TypeName param = funcDecl.function.parameters->first;
12157                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12158                   {
12159                      funcDecl.function.parameters->Remove(param);
12160                      FreeTypeName(param);
12161                   }
12162                }
12163
12164                if(type.classObjectType != classPointer)
12165                {
12166                   // DANGER: Watch for this... Check if it's a Conversion?
12167                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12168                   {
12169                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12170
12171                      if(!funcDecl.function.parameters)
12172                         funcDecl.function.parameters = MkList();
12173                      funcDecl.function.parameters->Insert(null, thisParam);
12174                   }
12175                }
12176             }
12177          }
12178       }
12179
12180       // Add this to the context
12181       if(function.body)
12182       {
12183          if(type.classObjectType != classPointer)
12184          {
12185             thisSymbol = Symbol
12186             {
12187                string = CopyString("this");
12188                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12189             };
12190             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12191
12192             if(typedObject && thisSymbol.type)
12193             {
12194                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12195                thisSymbol.type.byReference = type.byReference;
12196                thisSymbol.type.typedByReference = type.byReference;
12197                /*
12198                thisSymbol = Symbol { string = CopyString("class") };
12199                function.body.compound.context.symbols.Add(thisSymbol);
12200                */
12201             }
12202          }
12203       }
12204
12205       // Pointer to class data
12206
12207       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12208       {
12209          DataMember member = null;
12210          {
12211             Class base;
12212             for(base = _class; base && base.type != systemClass; base = base.next)
12213             {
12214                for(member = base.membersAndProperties.first; member; member = member.next)
12215                   if(!member.isProperty)
12216                      break;
12217                if(member)
12218                   break;
12219             }
12220          }
12221          for(member = _class.membersAndProperties.first; member; member = member.next)
12222             if(!member.isProperty)
12223                break;
12224          if(member)
12225          {
12226             char pointerName[1024];
12227
12228             Declaration decl;
12229             Initializer initializer;
12230             Expression exp, bytePtr;
12231
12232             strcpy(pointerName, "__ecerePointer_");
12233             FullClassNameCat(pointerName, _class.fullName, false);
12234             {
12235                char className[1024];
12236                strcpy(className, "__ecereClass_");
12237                FullClassNameCat(className, classSym.string, true);
12238                MangleClassName(className);
12239
12240                // Testing This
12241                DeclareClass(classSym, className);
12242             }
12243
12244             // ((byte *) this)
12245             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12246
12247             if(_class.fixed)
12248             {
12249                char string[256];
12250                sprintf(string, "%d", _class.offset);
12251                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12252             }
12253             else
12254             {
12255                // ([bytePtr] + [className]->offset)
12256                exp = QBrackets(MkExpOp(bytePtr, '+',
12257                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12258             }
12259
12260             // (this ? [exp] : 0)
12261             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12262             exp.expType = Type
12263             {
12264                refCount = 1;
12265                kind = pointerType;
12266                type = Type { refCount = 1, kind = voidType };
12267             };
12268
12269             if(function.body)
12270             {
12271                yylloc = function.body.loc;
12272                // ([structName] *) [exp]
12273                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12274                initializer = MkInitializerAssignment(
12275                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12276
12277                // [structName] * [pointerName] = [initializer];
12278                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12279
12280                {
12281                   Context prevContext = curContext;
12282                   curContext = function.body.compound.context;
12283
12284                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12285                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12286
12287                   curContext = prevContext;
12288                }
12289
12290                // WHY?
12291                decl.symbol = null;
12292
12293                if(!function.body.compound.declarations)
12294                   function.body.compound.declarations = MkList();
12295                function.body.compound.declarations->Insert(null, decl);
12296             }
12297          }
12298       }
12299
12300
12301       // Loop through the function and replace undeclared identifiers
12302       // which are a member of the class (methods, properties or data)
12303       // by "this.[member]"
12304    }
12305    else
12306       thisClass = null;
12307
12308    if(id)
12309    {
12310       FreeSpecifier(id._class);
12311       id._class = null;
12312
12313       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12314       {
12315          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12316          id = GetDeclId(initDecl.declarator);
12317
12318          FreeSpecifier(id._class);
12319          id._class = null;
12320       }
12321    }
12322    if(function.body)
12323       topContext = function.body.compound.context;
12324    {
12325       FunctionDefinition oldFunction = curFunction;
12326       curFunction = function;
12327       if(function.body)
12328          ProcessStatement(function.body);
12329
12330       // If this is a property set and no firewatchers has been done yet, add one here
12331       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12332       {
12333          Statement prevCompound = curCompound;
12334          Context prevContext = curContext;
12335
12336          Statement fireWatchers = MkFireWatchersStmt(null, null);
12337          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12338          ListAdd(function.body.compound.statements, fireWatchers);
12339
12340          curCompound = function.body;
12341          curContext = function.body.compound.context;
12342
12343          ProcessStatement(fireWatchers);
12344
12345          curContext = prevContext;
12346          curCompound = prevCompound;
12347
12348       }
12349
12350       curFunction = oldFunction;
12351    }
12352
12353    if(function.declarator)
12354    {
12355       ProcessDeclarator(function.declarator);
12356    }
12357
12358    topContext = oldTopContext;
12359    thisClass = oldThisClass;
12360 }
12361
12362 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12363 static void ProcessClass(OldList definitions, Symbol symbol)
12364 {
12365    ClassDef def;
12366    External external = curExternal;
12367    Class regClass = symbol ? symbol.registered : null;
12368
12369    // Process all functions
12370    for(def = definitions.first; def; def = def.next)
12371    {
12372       if(def.type == functionClassDef)
12373       {
12374          if(def.function.declarator)
12375             curExternal = def.function.declarator.symbol.pointerExternal;
12376          else
12377             curExternal = external;
12378
12379          ProcessFunction((FunctionDefinition)def.function);
12380       }
12381       else if(def.type == declarationClassDef)
12382       {
12383          if(def.decl.type == instDeclaration)
12384          {
12385             thisClass = regClass;
12386             ProcessInstantiationType(def.decl.inst);
12387             thisClass = null;
12388          }
12389          // Testing this
12390          else
12391          {
12392             Class backThisClass = thisClass;
12393             if(regClass) thisClass = regClass;
12394             ProcessDeclaration(def.decl);
12395             thisClass = backThisClass;
12396          }
12397       }
12398       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12399       {
12400          MemberInit defProperty;
12401
12402          // Add this to the context
12403          Symbol thisSymbol = Symbol
12404          {
12405             string = CopyString("this");
12406             type = regClass ? MkClassType(regClass.fullName) : null;
12407          };
12408          globalContext.symbols.Add((BTNode)thisSymbol);
12409
12410          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12411          {
12412             thisClass = regClass;
12413             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12414             thisClass = null;
12415          }
12416
12417          globalContext.symbols.Remove((BTNode)thisSymbol);
12418          FreeSymbol(thisSymbol);
12419       }
12420       else if(def.type == propertyClassDef && def.propertyDef)
12421       {
12422          PropertyDef prop = def.propertyDef;
12423
12424          // Add this to the context
12425          /*
12426          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12427          globalContext.symbols.Add(thisSymbol);
12428          */
12429
12430          thisClass = regClass;
12431          if(prop.setStmt)
12432          {
12433             if(regClass)
12434             {
12435                Symbol thisSymbol
12436                {
12437                   string = CopyString("this");
12438                   type = MkClassType(regClass.fullName);
12439                };
12440                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12441             }
12442
12443             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12444             ProcessStatement(prop.setStmt);
12445          }
12446          if(prop.getStmt)
12447          {
12448             if(regClass)
12449             {
12450                Symbol thisSymbol
12451                {
12452                   string = CopyString("this");
12453                   type = MkClassType(regClass.fullName);
12454                };
12455                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12456             }
12457
12458             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12459             ProcessStatement(prop.getStmt);
12460          }
12461          if(prop.issetStmt)
12462          {
12463             if(regClass)
12464             {
12465                Symbol thisSymbol
12466                {
12467                   string = CopyString("this");
12468                   type = MkClassType(regClass.fullName);
12469                };
12470                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12471             }
12472
12473             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12474             ProcessStatement(prop.issetStmt);
12475          }
12476
12477          thisClass = null;
12478
12479          /*
12480          globalContext.symbols.Remove(thisSymbol);
12481          FreeSymbol(thisSymbol);
12482          */
12483       }
12484       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12485       {
12486          PropertyWatch propertyWatch = def.propertyWatch;
12487
12488          thisClass = regClass;
12489          if(propertyWatch.compound)
12490          {
12491             Symbol thisSymbol
12492             {
12493                string = CopyString("this");
12494                type = regClass ? MkClassType(regClass.fullName) : null;
12495             };
12496
12497             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12498
12499             curExternal = null;
12500             ProcessStatement(propertyWatch.compound);
12501          }
12502          thisClass = null;
12503       }
12504    }
12505 }
12506
12507 void DeclareFunctionUtil(String s)
12508 {
12509    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12510    if(function)
12511    {
12512       char name[1024];
12513       name[0] = 0;
12514       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12515          strcpy(name, "__ecereFunction_");
12516       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12517       DeclareFunction(function, name);
12518    }
12519 }
12520
12521 void ComputeDataTypes()
12522 {
12523    External external;
12524    External temp { };
12525    External after = null;
12526
12527    currentClass = null;
12528
12529    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12530
12531    for(external = ast->first; external; external = external.next)
12532    {
12533       if(external.type == declarationExternal)
12534       {
12535          Declaration decl = external.declaration;
12536          if(decl)
12537          {
12538             OldList * decls = decl.declarators;
12539             if(decls)
12540             {
12541                InitDeclarator initDecl = decls->first;
12542                if(initDecl)
12543                {
12544                   Declarator declarator = initDecl.declarator;
12545                   if(declarator && declarator.type == identifierDeclarator)
12546                   {
12547                      Identifier id = declarator.identifier;
12548                      if(id && id.string)
12549                      {
12550                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12551                         {
12552                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12553                            after = external;
12554                         }
12555                      }
12556                   }
12557                }
12558             }
12559          }
12560        }
12561    }
12562
12563    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12564    ast->Insert(after, temp);
12565    curExternal = temp;
12566
12567    DeclareFunctionUtil("eSystem_New");
12568    DeclareFunctionUtil("eSystem_New0");
12569    DeclareFunctionUtil("eSystem_Renew");
12570    DeclareFunctionUtil("eSystem_Renew0");
12571    DeclareFunctionUtil("eSystem_Delete");
12572    DeclareFunctionUtil("eClass_GetProperty");
12573    DeclareFunctionUtil("eInstance_FireSelfWatchers");
12574
12575    DeclareStruct("ecere::com::Class", false);
12576    DeclareStruct("ecere::com::Instance", false);
12577    DeclareStruct("ecere::com::Property", false);
12578    DeclareStruct("ecere::com::DataMember", false);
12579    DeclareStruct("ecere::com::Method", false);
12580    DeclareStruct("ecere::com::SerialBuffer", false);
12581    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12582
12583    ast->Remove(temp);
12584
12585    for(external = ast->first; external; external = external.next)
12586    {
12587       afterExternal = curExternal = external;
12588       if(external.type == functionExternal)
12589       {
12590          currentClass = external.function._class;
12591          ProcessFunction(external.function);
12592       }
12593       // There shouldn't be any _class member access here anyways...
12594       else if(external.type == declarationExternal)
12595       {
12596          currentClass = null;
12597          ProcessDeclaration(external.declaration);
12598       }
12599       else if(external.type == classExternal)
12600       {
12601          ClassDefinition _class = external._class;
12602          currentClass = external.symbol.registered;
12603          if(_class.definitions)
12604          {
12605             ProcessClass(_class.definitions, _class.symbol);
12606          }
12607          if(inCompiler)
12608          {
12609             // Free class data...
12610             ast->Remove(external);
12611             delete external;
12612          }
12613       }
12614       else if(external.type == nameSpaceExternal)
12615       {
12616          thisNameSpace = external.id.string;
12617       }
12618    }
12619    currentClass = null;
12620    thisNameSpace = null;
12621
12622    delete temp.symbol;
12623    delete temp;
12624 }