ide/debugger/watches: Improved + and - pointer operations
[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 > MAXINT)
205       sprintf(temp, FORMAT64HEX /*"0x%I64XLL"*/, result);
206    else
207       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
208    if(result > MAXINT || result < MININT)
209       strcat(temp, "LL");
210    return CopyString(temp);
211 }
212
213 public char * PrintUInt(uint64 result)
214 {
215    char temp[100];
216    if(result > MAXDWORD)
217       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
218    else if(result > MAXINT)
219       sprintf(temp, FORMAT64HEX /*"0x%I64X"*/, result);
220    else
221       sprintf(temp, FORMAT64D /*"%I64d"*/, result);
222    return CopyString(temp);
223 }
224
225 public char * PrintInt64(int64 result)
226 {
227    char temp[100];
228    sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
229    return CopyString(temp);
230 }
231
232 public char * PrintUInt64(uint64 result)
233 {
234    char temp[100];
235    if(result > MAXINT64)
236       sprintf(temp, FORMAT64HEXLL /*"0x%I64XLL"*/, result);
237    else
238       sprintf(temp, FORMAT64DLL /*"%I64d"*/, result);
239    return CopyString(temp);
240 }
241
242 public char * PrintHexUInt(uint64 result)
243 {
244    char temp[100];
245    if(result > MAXDWORD)
246       sprintf(temp, FORMAT64HEX /*"0x%I64xLL"*/, result);
247    else
248       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
249    if(result > MAXDWORD)
250       strcat(temp, "LL");
251    return CopyString(temp);
252 }
253
254 public char * PrintHexUInt64(uint64 result)
255 {
256    char temp[100];
257    if(result > MAXDWORD)
258       sprintf(temp, FORMAT64HEXLL /*"0x%I64xLL"*/, result);
259    else
260       sprintf(temp, FORMAT64HEX /*"0x%I64x"*/, result);
261    return CopyString(temp);
262 }
263
264 public char * PrintShort(short result)
265 {
266    char temp[100];
267    sprintf(temp, "%d", (unsigned short)result);
268    return CopyString(temp);
269 }
270
271 public char * PrintUShort(unsigned short result)
272 {
273    char temp[100];
274    if(result > 32767)
275       sprintf(temp, "0x%X", (int)result);
276    else
277       sprintf(temp, "%d", (int)result);
278    return CopyString(temp);
279 }
280
281 public char * PrintChar(char result)
282 {
283    char temp[100];
284    if(result > 0 && isprint(result))
285       sprintf(temp, "'%c'", result);
286    else if(result < 0)
287       sprintf(temp, "%d", (int)result);
288    else
289       //sprintf(temp, "%#X", result);
290       sprintf(temp, "0x%X", (unsigned char)result);
291    return CopyString(temp);
292 }
293
294 public char * PrintUChar(unsigned char result)
295 {
296    char temp[100];
297    sprintf(temp, "0x%X", result);
298    return CopyString(temp);
299 }
300
301 public char * PrintFloat(float result)
302 {
303    char temp[350];
304    if(result.isInf)
305    {
306       if(result.signBit)
307          strcpy(temp, "-inf");
308       else
309          strcpy(temp, "inf");
310    }
311    else if(result.isNan)
312    {
313       if(result.signBit)
314          strcpy(temp, "-nan");
315       else
316          strcpy(temp, "nan");
317    }
318    else
319       sprintf(temp, "%.16ff", result);
320    return CopyString(temp);
321 }
322
323 public char * PrintDouble(double result)
324 {
325    char temp[350];
326    if(result.isInf)
327    {
328       if(result.signBit)
329          strcpy(temp, "-inf");
330       else
331          strcpy(temp, "inf");
332    }
333    else if(result.isNan)
334    {
335       if(result.signBit)
336          strcpy(temp, "-nan");
337       else
338          strcpy(temp, "nan");
339    }
340    else
341       sprintf(temp, "%.16f", result);
342    return CopyString(temp);
343 }
344
345 ////////////////////////////////////////////////////////////////////////
346 ////////////////////////////////////////////////////////////////////////
347
348 //public Operand GetOperand(Expression exp);
349
350 #define GETVALUE(name, t) \
351    public bool GetOp##name(Operand op2, t * value2) \
352    {                                                        \
353       if(op2.kind == intType && op2.type.isSigned) *value2 = (t) op2.i; \
354       else if(op2.kind == intType) *value2 = (t) op2.ui;                 \
355       else if(op2.kind == int64Type && op2.type.isSigned) *value2 = (t) op2.i64; \
356       else if(op2.kind == int64Type) *value2 = (t) op2.ui64;                 \
357       else if(op2.kind == intSizeType && op2.type.isSigned) *value2 = (t) op2.i64; \
358       else if(op2.kind == intSizeType) *value2 = (t) op2.ui64; \
359       else if(op2.kind == intPtrType && op2.type.isSigned) *value2 = (t) op2.i64; \
360       else if(op2.kind == intPtrType) *value2 = (t) op2.ui64;                 \
361       else if(op2.kind == shortType && op2.type.isSigned) *value2 = (t) op2.s;   \
362       else if(op2.kind == shortType) *value2 = (t) op2.us;                        \
363       else if(op2.kind == charType && op2.type.isSigned) *value2 = (t) op2.c;    \
364       else if(op2.kind == _BoolType || op2.kind == charType) *value2 = (t) op2.uc; \
365       else if(op2.kind == floatType) *value2 = (t) op2.f;                         \
366       else if(op2.kind == doubleType) *value2 = (t) op2.d;                        \
367       else if(op2.kind == pointerType) *value2 = (t) op2.ui64;                    \
368       else                                                                          \
369          return false;                                                              \
370       return true;                                                                  \
371    } \
372    public bool Get##name(Expression exp, t * value2) \
373    {                                                        \
374       Operand op2 = GetOperand(exp);                        \
375       return GetOp##name(op2, value2); \
376    }
377
378 // To help the deubugger currently not preprocessing...
379 #define HELP(x) x
380
381 GETVALUE(Int, HELP(int));
382 GETVALUE(UInt, HELP(unsigned int));
383 GETVALUE(Int64, HELP(int64));
384 GETVALUE(UInt64, HELP(uint64));
385 GETVALUE(IntPtr, HELP(intptr));
386 GETVALUE(UIntPtr, HELP(uintptr));
387 GETVALUE(IntSize, HELP(intsize));
388 GETVALUE(UIntSize, HELP(uintsize));
389 GETVALUE(Short, HELP(short));
390 GETVALUE(UShort, HELP(unsigned short));
391 GETVALUE(Char, HELP(char));
392 GETVALUE(UChar, HELP(unsigned char));
393 GETVALUE(Float, HELP(float));
394 GETVALUE(Double, HELP(double));
395
396 void ComputeExpression(Expression exp);
397
398 void ComputeClassMembers(Class _class, bool isMember)
399 {
400    DataMember member = isMember ? (DataMember) _class : null;
401    Context context = isMember ? null : SetupTemplatesContext(_class);
402    if(member || ((_class.type == bitClass || _class.type == normalClass || _class.type == structClass || _class.type == noHeadClass) &&
403                  (_class.type == bitClass || (!_class.structSize || _class.structSize == _class.offset)) && _class.computeSize))
404    {
405       int c;
406       int unionMemberOffset = 0;
407       int bitFields = 0;
408
409       /*
410       if(!member && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass) && _class.memberOffset && _class.memberOffset > _class.base.structSize)
411          _class.memberOffset = (_class.base && _class.base.type != systemClass) ? _class.base.structSize : 0;
412       */
413
414       if(member)
415       {
416          member.memberOffset = 0;
417          if(targetBits < sizeof(void *) * 8)
418             member.structAlignment = 0;
419       }
420       else if(targetBits < sizeof(void *) * 8)
421          _class.structAlignment = 0;
422
423       // Confusion here: non struct classes seem to have their memberOffset restart at 0 at each hierarchy level
424       if(!member && ((_class.type == normalClass || _class.type == noHeadClass) || (_class.type == structClass && _class.memberOffset && _class.memberOffset > _class.base.structSize)))
425          _class.memberOffset = (_class.base && _class.type == structClass) ? _class.base.structSize : 0;
426
427       if(!member && _class.destructionWatchOffset)
428          _class.memberOffset += sizeof(OldList);
429
430       // To avoid reentrancy...
431       //_class.structSize = -1;
432
433       {
434          DataMember dataMember;
435          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
436          {
437             if(!dataMember.isProperty)
438             {
439                if(dataMember.type == normalMember && dataMember.dataTypeString && !dataMember.dataType)
440                {
441                   dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
442                   /*if(!dataMember.dataType)
443                      dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
444                      */
445                }
446             }
447          }
448       }
449
450       {
451          DataMember dataMember;
452          for(dataMember = member ? member.members.first : _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
453          {
454             if(!dataMember.isProperty && (dataMember.type != normalMember || dataMember.dataTypeString))
455             {
456                if(!isMember && _class.type == bitClass && dataMember.dataType)
457                {
458                   BitMember bitMember = (BitMember) dataMember;
459                   uint64 mask = 0;
460                   int d;
461
462                   ComputeTypeSize(dataMember.dataType);
463
464                   if(bitMember.pos == -1) bitMember.pos = _class.memberOffset;
465                   if(!bitMember.size) bitMember.size = dataMember.dataType.size * 8;
466
467                   _class.memberOffset = bitMember.pos + bitMember.size;
468                   for(d = 0; d<bitMember.size; d++)
469                   {
470                      if(d)
471                         mask <<= 1;
472                      mask |= 1;
473                   }
474                   bitMember.mask = mask << bitMember.pos;
475                }
476                else if(dataMember.type == normalMember && dataMember.dataType)
477                {
478                   int size;
479                   int alignment = 0;
480
481                   // Prevent infinite recursion
482                   if(dataMember.dataType.kind != classType ||
483                      ((!dataMember.dataType._class || !dataMember.dataType._class.registered || dataMember.dataType._class.registered != _class ||
484                      _class.type != structClass)))
485                      ComputeTypeSize(dataMember.dataType);
486
487                   if(dataMember.dataType.bitFieldCount)
488                   {
489                      bitFields += dataMember.dataType.bitFieldCount;
490                      size = 0;
491                   }
492                   else
493                   {
494                      if(bitFields)
495                      {
496                         int size = (bitFields + 7) / 8;
497
498                         if(isMember)
499                         {
500                            // TESTING THIS PADDING CODE
501                            if(alignment)
502                            {
503                               member.structAlignment = Max(member.structAlignment, alignment);
504
505                               if(member.memberOffset % alignment)
506                                  member.memberOffset += alignment - (member.memberOffset % alignment);
507                            }
508
509                            dataMember.offset = member.memberOffset;
510                            if(member.type == unionMember)
511                               unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
512                            else
513                            {
514                               member.memberOffset += size;
515                            }
516                         }
517                         else
518                         {
519                            // TESTING THIS PADDING CODE
520                            if(alignment)
521                            {
522                               _class.structAlignment = Max(_class.structAlignment, alignment);
523
524                               if(_class.memberOffset % alignment)
525                                  _class.memberOffset += alignment - (_class.memberOffset % alignment);
526                            }
527
528                            dataMember.offset = _class.memberOffset;
529                            _class.memberOffset += size;
530                         }
531                         bitFields = 0;
532                      }
533                      size = dataMember.dataType.size;
534                      alignment = dataMember.dataType.alignment;
535                   }
536
537                   if(isMember)
538                   {
539                      // TESTING THIS PADDING CODE
540                      if(alignment)
541                      {
542                         member.structAlignment = Max(member.structAlignment, alignment);
543
544                         if(member.memberOffset % alignment)
545                            member.memberOffset += alignment - (member.memberOffset % alignment);
546                      }
547
548                      dataMember.offset = member.memberOffset;
549                      if(member.type == unionMember)
550                         unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
551                      else
552                      {
553                         member.memberOffset += size;
554                      }
555                   }
556                   else
557                   {
558                      // TESTING THIS PADDING CODE
559                      if(alignment)
560                      {
561                         _class.structAlignment = Max(_class.structAlignment, alignment);
562
563                         if(_class.memberOffset % alignment)
564                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
565                      }
566
567                      dataMember.offset = _class.memberOffset;
568                      _class.memberOffset += size;
569                   }
570                }
571                else
572                {
573                   int alignment;
574
575                   ComputeClassMembers((Class)dataMember, true);
576                   alignment = dataMember.structAlignment;
577
578                   if(isMember)
579                   {
580                      if(alignment)
581                      {
582                         if(member.memberOffset % alignment)
583                            member.memberOffset += alignment - (member.memberOffset % alignment);
584
585                         member.structAlignment = Max(member.structAlignment, alignment);
586                      }
587                      dataMember.offset = member.memberOffset;
588                      if(member.type == unionMember)
589                         unionMemberOffset = Max(unionMemberOffset, dataMember.memberOffset);
590                      else
591                         member.memberOffset += dataMember.memberOffset;
592                   }
593                   else
594                   {
595                      if(alignment)
596                      {
597                         if(_class.memberOffset % alignment)
598                            _class.memberOffset += alignment - (_class.memberOffset % alignment);
599                         _class.structAlignment = Max(_class.structAlignment, alignment);
600                      }
601                      dataMember.offset = _class.memberOffset;
602                      _class.memberOffset += dataMember.memberOffset;
603                   }
604                }
605             }
606          }
607          if(bitFields)
608          {
609             int alignment = 0;
610             int size = (bitFields + 7) / 8;
611
612             if(isMember)
613             {
614                // TESTING THIS PADDING CODE
615                if(alignment)
616                {
617                   member.structAlignment = Max(member.structAlignment, alignment);
618
619                   if(member.memberOffset % alignment)
620                      member.memberOffset += alignment - (member.memberOffset % alignment);
621                }
622
623                if(member.type == unionMember)
624                   unionMemberOffset = Max(unionMemberOffset, dataMember.dataType.size);
625                else
626                {
627                   member.memberOffset += size;
628                }
629             }
630             else
631             {
632                // TESTING THIS PADDING CODE
633                if(alignment)
634                {
635                   _class.structAlignment = Max(_class.structAlignment, alignment);
636
637                   if(_class.memberOffset % alignment)
638                      _class.memberOffset += alignment - (_class.memberOffset % alignment);
639                }
640                _class.memberOffset += size;
641             }
642             bitFields = 0;
643          }
644       }
645       if(member && member.type == unionMember)
646       {
647          member.memberOffset = unionMemberOffset;
648       }
649
650       if(!isMember)
651       {
652          /*if(_class.type == structClass)
653             _class.size = _class.memberOffset;
654          else
655          */
656
657          if(_class.type != bitClass)
658          {
659             int extra = 0;
660             if(_class.structAlignment)
661             {
662                if(_class.memberOffset % _class.structAlignment)
663                   extra += _class.structAlignment - (_class.memberOffset % _class.structAlignment);
664             }
665             _class.structSize = (_class.base ? (_class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize) : 0) + _class.memberOffset + extra;
666             if(!member)
667             {
668                Property prop;
669                for(prop = _class.membersAndProperties.first; prop; prop = prop.next)
670                {
671                   if(prop.isProperty && prop.isWatchable)
672                   {
673                      prop.watcherOffset = _class.structSize;
674                      _class.structSize += sizeof(OldList);
675                   }
676                }
677             }
678
679             // Fix Derivatives
680             {
681                OldLink derivative;
682                for(derivative = _class.derivatives.first; derivative; derivative = derivative.next)
683                {
684                   Class deriv = derivative.data;
685
686                   if(deriv.computeSize)
687                   {
688                      // TESTING THIS NEW CODE HERE... TRYING TO FIX ScrollBar MEMBERS DEBUGGING
689                      deriv.offset = /*_class.offset + */_class.structSize;
690                      deriv.memberOffset = 0;
691                      // ----------------------
692
693                      deriv.structSize = deriv.offset;
694
695                      ComputeClassMembers(deriv, false);
696                   }
697                }
698             }
699          }
700       }
701    }
702    if(context)
703       FinishTemplatesContext(context);
704 }
705
706 public void ComputeModuleClasses(Module module)
707 {
708    Class _class;
709    OldLink subModule;
710
711    for(subModule = module.modules.first; subModule; subModule = subModule.next)
712       ComputeModuleClasses(subModule.data);
713    for(_class = module.classes.first; _class; _class = _class.next)
714       ComputeClassMembers(_class, false);
715 }
716
717
718 public int ComputeTypeSize(Type type)
719 {
720    uint size = type ? type.size : 0;
721    if(!size && type && !type.computing)
722    {
723       type.computing = true;
724       switch(type.kind)
725       {
726          case _BoolType: type.alignment = size = sizeof(char); break;   // Assuming 1 byte _Bool
727          case charType: type.alignment = size = sizeof(char); break;
728          case intType: type.alignment = size = sizeof(int); break;
729          case int64Type: type.alignment = size = sizeof(int64); break;
730          case intPtrType: type.alignment = size = targetBits / 8; break;
731          case intSizeType: type.alignment = size = targetBits / 8; break;
732          case longType: type.alignment = size = sizeof(long); break;
733          case shortType: type.alignment = size = sizeof(short); break;
734          case floatType: type.alignment = size = sizeof(float); break;
735          case doubleType: type.alignment = size = sizeof(double); break;
736          case classType:
737          {
738             Class _class = type._class ? type._class.registered : null;
739
740             if(_class && _class.type == structClass)
741             {
742                // Ensure all members are properly registered
743                ComputeClassMembers(_class, false);
744                type.alignment = _class.structAlignment;
745                size = _class.structSize;
746                if(type.alignment && size % type.alignment)
747                   size += type.alignment - (size % type.alignment);
748
749             }
750             else if(_class && (_class.type == unitClass ||
751                    _class.type == enumClass ||
752                    _class.type == bitClass))
753             {
754                if(!_class.dataType)
755                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
756                size = type.alignment = ComputeTypeSize(_class.dataType);
757             }
758             else
759                size = type.alignment = targetBits / 8; // sizeof(Instance *);
760             break;
761          }
762          case pointerType: case subClassType: size = type.alignment = targetBits / 8; /*sizeof(void *); */break;
763          case arrayType:
764             if(type.arraySizeExp)
765             {
766                ProcessExpressionType(type.arraySizeExp);
767                ComputeExpression(type.arraySizeExp);
768                if(!type.arraySizeExp.isConstant || (type.arraySizeExp.expType.kind != intType && type.arraySizeExp.expType.kind != enumType &&
769                   (type.arraySizeExp.expType.kind != classType || !type.arraySizeExp.expType._class.registered || type.arraySizeExp.expType._class.registered.type != enumClass)))
770                {
771                   Location oldLoc = yylloc;
772                   // bool isConstant = type.arraySizeExp.isConstant;
773                   char expression[10240];
774                   expression[0] = '\0';
775                   type.arraySizeExp.expType = null;
776                   yylloc = type.arraySizeExp.loc;
777                   if(inCompiler)
778                      PrintExpression(type.arraySizeExp, expression);
779                   Compiler_Error($"Array size not constant int (%s)\n", expression);
780                   yylloc = oldLoc;
781                }
782                GetInt(type.arraySizeExp, &type.arraySize);
783             }
784             else if(type.enumClass)
785             {
786                if(type.enumClass && type.enumClass.registered && type.enumClass.registered.type == enumClass)
787                {
788                   type.arraySize = (int)eClass_GetProperty(type.enumClass.registered, "enumSize");
789                }
790                else
791                   type.arraySize = 0;
792             }
793             else
794             {
795                // Unimplemented auto size
796                type.arraySize = 0;
797             }
798
799             size = ComputeTypeSize(type.type) * type.arraySize;
800             if(type.type)
801                type.alignment = type.type.alignment;
802
803             break;
804          case structType:
805          {
806             Type member;
807             for(member = type.members.first; member; member = member.next)
808             {
809                uint addSize = ComputeTypeSize(member);
810
811                member.offset = size;
812                if(member.alignment && size % member.alignment)
813                   member.offset += member.alignment - (size % member.alignment);
814                size = member.offset;
815
816                type.alignment = Max(type.alignment, member.alignment);
817                size += addSize;
818             }
819             if(type.alignment && size % type.alignment)
820                size += type.alignment - (size % type.alignment);
821             break;
822          }
823          case unionType:
824          {
825             Type member;
826             for(member = type.members.first; member; member = member.next)
827             {
828                uint addSize = ComputeTypeSize(member);
829
830                member.offset = size;
831                if(member.alignment && size % member.alignment)
832                   member.offset += member.alignment - (size % member.alignment);
833                size = member.offset;
834
835                type.alignment = Max(type.alignment, member.alignment);
836                size = Max(size, addSize);
837             }
838             if(type.alignment && size % type.alignment)
839                size += type.alignment - (size % type.alignment);
840             break;
841          }
842          case templateType:
843          {
844             TemplateParameter param = type.templateParameter;
845             Type baseType = ProcessTemplateParameterType(param);
846             if(baseType)
847             {
848                size = ComputeTypeSize(baseType);
849                type.alignment = baseType.alignment;
850             }
851             else
852                type.alignment = size = sizeof(uint64);
853             break;
854          }
855          case enumType:
856          {
857             type.alignment = size = sizeof(enum { test });
858             break;
859          }
860          case thisClassType:
861          {
862             type.alignment = size = targetBits / 8; //sizeof(void *);
863             break;
864          }
865       }
866       type.size = size;
867       type.computing = false;
868    }
869    return size;
870 }
871
872
873 /*static */int AddMembers(OldList * declarations, Class _class, bool isMember, uint * retSize, Class topClass, bool *addedPadding)
874 {
875    // This function is in need of a major review when implementing private members etc.
876    DataMember topMember = isMember ? (DataMember) _class : null;
877    uint totalSize = 0;
878    uint maxSize = 0;
879    int alignment, size;
880    DataMember member;
881    Context context = isMember ? null : SetupTemplatesContext(_class);
882    if(addedPadding)
883       *addedPadding = false;
884
885    if(!isMember && _class.base)
886    {
887       maxSize = _class.structSize;
888       //if(_class.base.type != systemClass) // Commented out with new Instance _class
889       {
890          // DANGER: Testing this noHeadClass here...
891          if(_class.type == structClass || _class.type == noHeadClass)
892             /*totalSize = */AddMembers(declarations, _class.base, false, &totalSize, topClass, null);
893          else
894          {
895             uint baseSize = _class.base.templateClass ? _class.base.templateClass.structSize : _class.base.structSize;
896             if(maxSize > baseSize)
897                maxSize -= baseSize;
898             else
899                maxSize = 0;
900          }
901       }
902    }
903
904    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
905    {
906       if(!member.isProperty)
907       {
908          switch(member.type)
909          {
910             case normalMember:
911             {
912                if(member.dataTypeString)
913                {
914                   OldList * specs = MkList(), * decls = MkList();
915                   Declarator decl;
916
917                   decl = SpecDeclFromString(member.dataTypeString, specs,
918                      MkDeclaratorIdentifier(MkIdentifier(member.name)));
919                   ListAdd(decls, MkStructDeclarator(decl, null));
920                   ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, decls, null)));
921
922                   if(!member.dataType)
923                      member.dataType = ProcessType(specs, decl);
924
925                   ReplaceThisClassSpecifiers(specs, topClass /*member._class*/);
926
927                   {
928                      Type type = ProcessType(specs, decl);
929                      DeclareType(member.dataType, false, false);
930                      FreeType(type);
931                   }
932                   /*
933                   if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
934                      member.dataType._class.registered && member.dataType._class.registered.type == structClass)
935                      DeclareStruct(member.dataType._class.string, false);
936                   */
937
938                   ComputeTypeSize(member.dataType);
939                   size = member.dataType.size;
940                   alignment = member.dataType.alignment;
941
942                   if(alignment)
943                   {
944                      if(totalSize % alignment)
945                         totalSize += alignment - (totalSize % alignment);
946                   }
947                   totalSize += size;
948                }
949                break;
950             }
951             case unionMember:
952             case structMember:
953             {
954                OldList * specs = MkList(), * list = MkList();
955
956                size = 0;
957                AddMembers(list, (Class)member, true, &size, topClass, null);
958                ListAdd(specs,
959                   MkStructOrUnion((member.type == unionMember)?unionSpecifier:structSpecifier, null, list));
960                ListAdd(declarations, MkClassDefDeclaration(MkStructDeclaration(specs, null, null)));
961                alignment = member.structAlignment;
962
963                if(alignment)
964                {
965                   if(totalSize % alignment)
966                      totalSize += alignment - (totalSize % alignment);
967                }
968                totalSize += size;
969                break;
970             }
971          }
972       }
973    }
974    if(retSize)
975    {
976       if(topMember && topMember.type == unionMember)
977          *retSize = Max(*retSize, totalSize);
978       else
979          *retSize += totalSize;
980    }
981    else if(totalSize < maxSize && _class.type != systemClass)
982    {
983       int autoPadding = 0;
984       if(!isMember && _class.structAlignment && totalSize % _class.structAlignment)
985          autoPadding = _class.structAlignment - (totalSize % _class.structAlignment);
986       if(totalSize + autoPadding < maxSize)
987       {
988          char sizeString[50];
989          sprintf(sizeString, "%d", maxSize - totalSize);
990          ListAdd(declarations,
991             MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(CHAR)),
992             MkListOne(MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__ecere_padding")), MkExpConstant(sizeString))), null)));
993          if(addedPadding)
994             *addedPadding = true;
995       }
996    }
997    if(context)
998       FinishTemplatesContext(context);
999    return topMember ? topMember.memberID : _class.memberID;
1000 }
1001
1002 static int DeclareMembers(Class _class, bool isMember)
1003 {
1004    DataMember topMember = isMember ? (DataMember) _class : null;
1005    uint totalSize = 0;
1006    DataMember member;
1007    Context context = isMember ? null : SetupTemplatesContext(_class);
1008
1009    if(!isMember && (_class.type == structClass || _class.type == noHeadClass) && _class.base.type != systemClass)
1010       DeclareMembers(_class.base, false);
1011
1012    for(member = isMember ? topMember.members.first : _class.membersAndProperties.first; member; member = member.next)
1013    {
1014       if(!member.isProperty)
1015       {
1016          switch(member.type)
1017          {
1018             case normalMember:
1019             {
1020                /*
1021                if(member.dataType && member.dataType.kind == classType && member.dataType._class &&
1022                   member.dataType._class.registered && member.dataType._class.registered.type == structClass)
1023                   DeclareStruct(member.dataType._class.string, false);
1024                   */
1025                if(!member.dataType && member.dataTypeString)
1026                   member.dataType = ProcessTypeString(member.dataTypeString, false);
1027                if(member.dataType)
1028                   DeclareType(member.dataType, false, false);
1029                break;
1030             }
1031             case unionMember:
1032             case structMember:
1033             {
1034                DeclareMembers((Class)member, true);
1035                break;
1036             }
1037          }
1038       }
1039    }
1040    if(context)
1041       FinishTemplatesContext(context);
1042
1043    return topMember ? topMember.memberID : _class.memberID;
1044 }
1045
1046 void DeclareStruct(char * name, bool skipNoHead)
1047 {
1048    External external = null;
1049    Symbol classSym = FindClass(name);
1050
1051    if(!inCompiler || !classSym) return;
1052
1053    // We don't need any declaration for bit classes...
1054    if(classSym.registered &&
1055       (classSym.registered.type == bitClass || classSym.registered.type == unitClass || classSym.registered.type == enumClass))
1056       return;
1057
1058    /*if(classSym.registered.templateClass)
1059       return DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1060    */
1061
1062    if(classSym.registered && classSym.imported && !classSym.declaredStructSym)
1063    {
1064       // Add typedef struct
1065       Declaration decl;
1066       OldList * specifiers, * declarators;
1067       OldList * declarations = null;
1068       char structName[1024];
1069       external = (classSym.registered && classSym.registered.type == structClass) ?
1070          classSym.pointerExternal : classSym.structExternal;
1071
1072       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1073       // Moved this one up because DeclareClass done later will need it
1074
1075       classSym.declaring++;
1076
1077       if(strchr(classSym.string, '<'))
1078       {
1079          if(classSym.registered.templateClass)
1080          {
1081             DeclareStruct(classSym.registered.templateClass.fullName, skipNoHead);
1082             classSym.declaring--;
1083          }
1084          return;
1085       }
1086
1087       //if(!skipNoHead)
1088          DeclareMembers(classSym.registered, false);
1089
1090       structName[0] = 0;
1091       FullClassNameCat(structName, name, false);
1092
1093       /*if(!external)
1094          external = MkExternalDeclaration(null);*/
1095
1096       if(!skipNoHead)
1097       {
1098          bool addedPadding = false;
1099          classSym.declaredStructSym = true;
1100
1101          declarations = MkList();
1102
1103          AddMembers(declarations, classSym.registered, false, null, classSym.registered, &addedPadding);
1104
1105          //ListAdd(specifiers, MkSpecifier(TYPEDEF));
1106          //ListAdd(specifiers, MkStructOrUnion(structSpecifier, null, declarations));
1107
1108          if(!declarations->count || (declarations->count == 1 && addedPadding))
1109          {
1110             FreeList(declarations, FreeClassDef);
1111             declarations = null;
1112          }
1113       }
1114       if(skipNoHead || declarations)
1115       {
1116          if(external && external.declaration)
1117          {
1118             ((Specifier)external.declaration.specifiers->first).definitions = declarations;
1119
1120             if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1121             {
1122                // TODO: Fix this
1123                //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1124
1125                // DANGER
1126                if(classSym.structExternal)
1127                   ast->Move(classSym.structExternal, curExternal.prev);
1128                ast->Move(classSym.pointerExternal, curExternal.prev);
1129
1130                classSym.id = curExternal.symbol.idCode;
1131                classSym.idCode = curExternal.symbol.idCode;
1132                // external = classSym.pointerExternal;
1133                //external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1134             }
1135          }
1136          else
1137          {
1138             if(!external)
1139                external = MkExternalDeclaration(null);
1140
1141             specifiers = MkList();
1142             declarators = MkList();
1143             ListAdd(specifiers, MkStructOrUnion(structSpecifier, MkIdentifier(structName), declarations));
1144
1145             /*
1146             d = MkDeclaratorIdentifier(MkIdentifier(structName));
1147             ListAdd(declarators, MkInitDeclarator(d, null));
1148             */
1149             external.declaration = decl = MkDeclaration(specifiers, declarators);
1150             if(decl.symbol && !decl.symbol.pointerExternal)
1151                decl.symbol.pointerExternal = external;
1152
1153             // For simple classes, keep the declaration as the external to move around
1154             if(classSym.registered && classSym.registered.type == structClass)
1155             {
1156                char className[1024];
1157                strcpy(className, "__ecereClass_");
1158                FullClassNameCat(className, classSym.string, true);
1159                MangleClassName(className);
1160
1161                // Testing This
1162                DeclareClass(classSym, className);
1163
1164                external.symbol = classSym;
1165                classSym.pointerExternal = external;
1166                classSym.id = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1167                classSym.idCode = (curExternal && curExternal.symbol) ? curExternal.symbol.idCode : 0;
1168             }
1169             else
1170             {
1171                char className[1024];
1172                strcpy(className, "__ecereClass_");
1173                FullClassNameCat(className, classSym.string, true);
1174                MangleClassName(className);
1175
1176                // TOFIX: TESTING THIS...
1177                classSym.structExternal = external;
1178                DeclareClass(classSym, className);
1179                external.symbol = classSym;
1180             }
1181
1182             //if(curExternal)
1183                ast->Insert(curExternal ? curExternal.prev : null, external);
1184          }
1185       }
1186
1187       classSym.declaring--;
1188    }
1189    else if(curExternal && curExternal.symbol && curExternal.symbol.idCode < classSym.id)
1190    {
1191       // TEMPORARY HACK: Pass 3 will move up struct declarations without moving members
1192       // Moved this one up because DeclareClass done later will need it
1193
1194       // TESTING THIS:
1195       classSym.declaring++;
1196
1197       //if(!skipNoHead)
1198       {
1199          if(classSym.registered)
1200             DeclareMembers(classSym.registered, false);
1201       }
1202
1203       if(classSym.registered && (classSym.registered.type == structClass || classSym.registered.type == noHeadClass))
1204       {
1205          // TODO: Fix this
1206          //ast->Move(classSym.structExternal ? classSym.structExternal : classSym.pointerExternal, curExternal.prev);
1207
1208          // DANGER
1209          if(classSym.structExternal)
1210             ast->Move(classSym.structExternal, curExternal.prev);
1211          ast->Move(classSym.pointerExternal, curExternal.prev);
1212
1213          classSym.id = curExternal.symbol.idCode;
1214          classSym.idCode = curExternal.symbol.idCode;
1215          // external = classSym.pointerExternal;
1216          // external = classSym.structExternal ? classSym.structExternal : classSym.pointerExternal;
1217       }
1218
1219       classSym.declaring--;
1220    }
1221    //return external;
1222 }
1223
1224 void DeclareProperty(Property prop, char * setName, char * getName)
1225 {
1226    Symbol symbol = prop.symbol;
1227    char propName[1024];
1228
1229    strcpy(setName, "__ecereProp_");
1230    FullClassNameCat(setName, prop._class.fullName, false);
1231    strcat(setName, "_Set_");
1232    // strcat(setName, prop.name);
1233    FullClassNameCat(setName, prop.name, true);
1234
1235    strcpy(getName, "__ecereProp_");
1236    FullClassNameCat(getName, prop._class.fullName, false);
1237    strcat(getName, "_Get_");
1238    FullClassNameCat(getName, prop.name, true);
1239    // strcat(getName, prop.name);
1240
1241    strcpy(propName, "__ecereProp_");
1242    FullClassNameCat(propName, prop._class.fullName, false);
1243    strcat(propName, "_");
1244    FullClassNameCat(propName, prop.name, true);
1245    // strcat(propName, prop.name);
1246
1247    // To support "char *" property
1248    MangleClassName(getName);
1249    MangleClassName(setName);
1250    MangleClassName(propName);
1251
1252    if(prop._class.type == structClass)
1253       DeclareStruct(prop._class.fullName, false);
1254
1255    if(!symbol || curExternal.symbol.idCode < symbol.id)
1256    {
1257       bool imported = false;
1258       bool dllImport = false;
1259       if(!symbol || symbol._import)
1260       {
1261          if(!symbol)
1262          {
1263             Symbol classSym;
1264             if(!prop._class.symbol)
1265                prop._class.symbol = FindClass(prop._class.fullName);
1266             classSym = prop._class.symbol;
1267             if(classSym && !classSym._import)
1268             {
1269                ModuleImport module;
1270
1271                if(prop._class.module)
1272                   module = FindModule(prop._class.module);
1273                else
1274                   module = mainModule;
1275
1276                classSym._import = ClassImport
1277                {
1278                   name = CopyString(prop._class.fullName);
1279                   isRemote = prop._class.isRemote;
1280                };
1281                module.classes.Add(classSym._import);
1282             }
1283             symbol = prop.symbol = Symbol { };
1284             symbol._import = (ClassImport)PropertyImport
1285             {
1286                name = CopyString(prop.name);
1287                isVirtual = false; //prop.isVirtual;
1288                hasSet = prop.Set ? true : false;
1289                hasGet = prop.Get ? true : false;
1290             };
1291             if(classSym)
1292                classSym._import.properties.Add(symbol._import);
1293          }
1294          imported = true;
1295          // Ugly work around for isNan properties declared within float/double classes which are initialized with ecereCOM
1296          if((prop._class.module != privateModule || !strcmp(prop._class.name, "float") || !strcmp(prop._class.name, "double")) &&
1297             prop._class.module.importType != staticImport)
1298             dllImport = true;
1299       }
1300
1301       if(!symbol.type)
1302       {
1303          Context context = SetupTemplatesContext(prop._class);
1304          symbol.type = ProcessTypeString(prop.dataTypeString, false);
1305          FinishTemplatesContext(context);
1306       }
1307
1308       // Get
1309       if(prop.Get)
1310       {
1311          if(!symbol.externalGet || symbol.externalGet.type == functionExternal)
1312          {
1313             Declaration decl;
1314             OldList * specifiers, * declarators;
1315             Declarator d;
1316             OldList * params;
1317             Specifier spec;
1318             External external;
1319             Declarator typeDecl;
1320             bool simple = false;
1321
1322             specifiers = MkList();
1323             declarators = MkList();
1324             params = MkList();
1325
1326             ListAdd(params, MkTypeName(MkListOne(MkSpecifierName /*MkClassName*/(prop._class.fullName)),
1327                MkDeclaratorIdentifier(MkIdentifier("this"))));
1328
1329             d = MkDeclaratorIdentifier(MkIdentifier(getName));
1330             //if(imported)
1331             if(dllImport)
1332                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1333
1334             {
1335                Context context = SetupTemplatesContext(prop._class);
1336                typeDecl = SpecDeclFromString(prop.dataTypeString, specifiers, null);
1337                FinishTemplatesContext(context);
1338             }
1339
1340             // Make sure the simple _class's type is declared
1341             for(spec = specifiers->first; spec; spec = spec.next)
1342             {
1343                if(spec.type == nameSpecifier /*SpecifierClass*/)
1344                {
1345                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1346                   {
1347                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1348                      symbol._class = classSym.registered;
1349                      if(classSym.registered && classSym.registered.type == structClass)
1350                      {
1351                         DeclareStruct(spec.name, false);
1352                         simple = true;
1353                      }
1354                   }
1355                }
1356             }
1357
1358             if(!simple)
1359                d = PlugDeclarator(typeDecl, d);
1360             else
1361             {
1362                ListAdd(params, MkTypeName(specifiers,
1363                   PlugDeclarator(typeDecl, MkDeclaratorIdentifier(MkIdentifier("value")))));
1364                specifiers = MkList();
1365             }
1366
1367             d = MkDeclaratorFunction(d, params);
1368
1369             //if(imported)
1370             if(dllImport)
1371                specifiers->Insert(null, MkSpecifier(EXTERN));
1372             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1373                specifiers->Insert(null, MkSpecifier(STATIC));
1374             if(simple)
1375                ListAdd(specifiers, MkSpecifier(VOID));
1376
1377             ListAdd(declarators, MkInitDeclarator(d, null));
1378
1379             decl = MkDeclaration(specifiers, declarators);
1380
1381             external = MkExternalDeclaration(decl);
1382             ast->Insert(curExternal.prev, external);
1383             external.symbol = symbol;
1384             symbol.externalGet = external;
1385
1386             ReplaceThisClassSpecifiers(specifiers, prop._class);
1387
1388             if(typeDecl)
1389                FreeDeclarator(typeDecl);
1390          }
1391          else
1392          {
1393             // Move declaration higher...
1394             ast->Move(symbol.externalGet, curExternal.prev);
1395          }
1396       }
1397
1398       // Set
1399       if(prop.Set)
1400       {
1401          if(!symbol.externalSet || symbol.externalSet.type == functionExternal)
1402          {
1403             Declaration decl;
1404             OldList * specifiers, * declarators;
1405             Declarator d;
1406             OldList * params;
1407             Specifier spec;
1408             External external;
1409             Declarator typeDecl;
1410
1411             declarators = MkList();
1412             params = MkList();
1413
1414             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1415             if(!prop.conversion || prop._class.type == structClass)
1416             {
1417                ListAdd(params, MkTypeName(MkListOne(MkSpecifierName/*MkClassName*/(prop._class.fullName)),
1418                   MkDeclaratorIdentifier(MkIdentifier("this"))));
1419             }
1420
1421             specifiers = MkList();
1422
1423             {
1424                Context context = SetupTemplatesContext(prop._class);
1425                typeDecl = d = SpecDeclFromString(prop.dataTypeString, specifiers,
1426                   MkDeclaratorIdentifier(MkIdentifier("value")));
1427                FinishTemplatesContext(context);
1428             }
1429             ListAdd(params, MkTypeName(specifiers, d));
1430
1431             d = MkDeclaratorIdentifier(MkIdentifier(setName));
1432             //if(imported)
1433             if(dllImport)
1434                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
1435             d = MkDeclaratorFunction(d, params);
1436
1437             // Make sure the simple _class's type is declared
1438             for(spec = specifiers->first; spec; spec = spec.next)
1439             {
1440                if(spec.type == nameSpecifier /*SpecifierClass*/)
1441                {
1442                   if((!typeDecl || typeDecl.type == identifierDeclarator))
1443                   {
1444                      Symbol classSym = spec.symbol; // FindClass(spec.name);
1445                      symbol._class = classSym.registered;
1446                      if(classSym.registered && classSym.registered.type == structClass)
1447                         DeclareStruct(spec.name, false);
1448                   }
1449                }
1450             }
1451
1452             ListAdd(declarators, MkInitDeclarator(d, null));
1453
1454             specifiers = MkList();
1455             //if(imported)
1456             if(dllImport)
1457                specifiers->Insert(null, MkSpecifier(EXTERN));
1458             else if(prop._class.symbol && ((Symbol)prop._class.symbol).isStatic)
1459                specifiers->Insert(null, MkSpecifier(STATIC));
1460
1461             // TESTING COMMENTING THIS FIRST LINE OUT, what was the problem? Trying to add noHeadClass here ...
1462             if(!prop.conversion || prop._class.type == structClass)
1463                ListAdd(specifiers, MkSpecifier(VOID));
1464             else
1465                ListAdd(specifiers, MkSpecifierName/*MkClassName*/(prop._class.fullName));
1466
1467             decl = MkDeclaration(specifiers, declarators);
1468
1469             external = MkExternalDeclaration(decl);
1470             ast->Insert(curExternal.prev, external);
1471             external.symbol = symbol;
1472             symbol.externalSet = external;
1473
1474             ReplaceThisClassSpecifiers(specifiers, prop._class);
1475          }
1476          else
1477          {
1478             // Move declaration higher...
1479             ast->Move(symbol.externalSet, curExternal.prev);
1480          }
1481       }
1482
1483       // Property (for Watchers)
1484       if(!symbol.externalPtr)
1485       {
1486          Declaration decl;
1487          External external;
1488          OldList * specifiers = MkList();
1489
1490          if(imported)
1491             specifiers->Insert(null, MkSpecifier(EXTERN));
1492          else
1493             specifiers->Insert(null, MkSpecifier(STATIC));
1494
1495          ListAdd(specifiers, MkSpecifierName("Property"));
1496
1497          {
1498             OldList * list = MkList();
1499             ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1500                   MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1501
1502             if(!imported)
1503             {
1504                strcpy(propName, "__ecerePropM_");
1505                FullClassNameCat(propName, prop._class.fullName, false);
1506                strcat(propName, "_");
1507                // strcat(propName, prop.name);
1508                FullClassNameCat(propName, prop.name, true);
1509
1510                MangleClassName(propName);
1511
1512                ListAdd(list, MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null),
1513                      MkDeclaratorIdentifier(MkIdentifier(propName))), null));
1514             }
1515             decl = MkDeclaration(specifiers, list);
1516          }
1517
1518          external = MkExternalDeclaration(decl);
1519          ast->Insert(curExternal.prev, external);
1520          external.symbol = symbol;
1521          symbol.externalPtr = external;
1522       }
1523       else
1524       {
1525          // Move declaration higher...
1526          ast->Move(symbol.externalPtr, curExternal.prev);
1527       }
1528
1529       symbol.id = curExternal.symbol.idCode;
1530    }
1531 }
1532
1533 // ***************** EXPRESSION PROCESSING ***************************
1534 public Type Dereference(Type source)
1535 {
1536    Type type = null;
1537    if(source)
1538    {
1539       if(source.kind == pointerType || source.kind == arrayType)
1540       {
1541          type = source.type;
1542          source.type.refCount++;
1543       }
1544       else if(source.kind == classType && !strcmp(source._class.string, "String"))
1545       {
1546          type = Type
1547          {
1548             kind = charType;
1549             refCount = 1;
1550          };
1551       }
1552       // Support dereferencing of no head classes for now...
1553       else if(source.kind == classType && source._class && source._class.registered && source._class.registered.type == noHeadClass)
1554       {
1555          type = source;
1556          source.refCount++;
1557       }
1558       else
1559          Compiler_Error($"cannot dereference type\n");
1560    }
1561    return type;
1562 }
1563
1564 static Type Reference(Type source)
1565 {
1566    Type type = null;
1567    if(source)
1568    {
1569       type = Type
1570       {
1571          kind = pointerType;
1572          type = source;
1573          refCount = 1;
1574       };
1575       source.refCount++;
1576    }
1577    return type;
1578 }
1579
1580 void ProcessMemberInitData(MemberInit member, Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
1581 {
1582    Identifier ident = member.identifiers ? member.identifiers->first : null;
1583    bool found = false;
1584    DataMember dataMember = null;
1585    Method method = null;
1586    bool freeType = false;
1587
1588    yylloc = member.loc;
1589
1590    if(!ident)
1591    {
1592       if(curMember)
1593       {
1594          eClass_FindNextMember(_class, curClass, curMember, subMemberStack, subMemberStackPos);
1595          if(*curMember)
1596          {
1597             found = true;
1598             dataMember = *curMember;
1599          }
1600       }
1601    }
1602    else
1603    {
1604       DataMember thisMember = (DataMember)eClass_FindProperty(_class, ident.string, privateModule);
1605       DataMember _subMemberStack[256];
1606       int _subMemberStackPos = 0;
1607
1608       // FILL MEMBER STACK
1609       if(!thisMember)
1610          thisMember = eClass_FindDataMember(_class, ident.string, privateModule, _subMemberStack, &_subMemberStackPos);
1611       if(thisMember)
1612       {
1613          dataMember = thisMember;
1614          if(curMember && thisMember.memberAccess == publicAccess)
1615          {
1616             *curMember = thisMember;
1617             *curClass = thisMember._class;
1618             memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
1619             *subMemberStackPos = _subMemberStackPos;
1620          }
1621          found = true;
1622       }
1623       else
1624       {
1625          // Setting a method
1626          method = eClass_FindMethod(_class, ident.string, privateModule);
1627          if(method && method.type == virtualMethod)
1628             found = true;
1629          else
1630             method = null;
1631       }
1632    }
1633
1634    if(found)
1635    {
1636       Type type = null;
1637       if(dataMember)
1638       {
1639          if(!dataMember.dataType && dataMember.dataTypeString)
1640          {
1641             //Context context = SetupTemplatesContext(dataMember._class);
1642             Context context = SetupTemplatesContext(_class);
1643             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
1644             FinishTemplatesContext(context);
1645          }
1646          type = dataMember.dataType;
1647       }
1648       else if(method)
1649       {
1650          // This is for destination type...
1651          if(!method.dataType)
1652             ProcessMethodType(method);
1653          //DeclareMethod(method);
1654          // method.dataType = ((Symbol)method.symbol)->type;
1655          type = method.dataType;
1656       }
1657
1658       if(ident && ident.next)
1659       {
1660          for(ident = ident.next; ident && type; ident = ident.next)
1661          {
1662             if(type.kind == classType)
1663             {
1664                dataMember = (DataMember)eClass_FindProperty(type._class.registered, ident.string, privateModule);
1665                if(!dataMember)
1666                   dataMember = eClass_FindDataMember(type._class.registered, ident.string, privateModule, null, null);
1667                if(dataMember)
1668                   type = dataMember.dataType;
1669             }
1670             else if(type.kind == structType || type.kind == unionType)
1671             {
1672                Type memberType;
1673                for(memberType = type.members.first; memberType; memberType = memberType.next)
1674                {
1675                   if(!strcmp(memberType.name, ident.string))
1676                   {
1677                      type = memberType;
1678                      break;
1679                   }
1680                }
1681             }
1682          }
1683       }
1684
1685       // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
1686       if(type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type && _class.templateArgs /* TODO: Watch out for these _class.templateClass*/)
1687       {
1688          int id = 0;
1689          ClassTemplateParameter curParam = null;
1690          Class sClass;
1691          for(sClass = _class; sClass; sClass = sClass.base)
1692          {
1693             id = 0;
1694             if(sClass.templateClass) sClass = sClass.templateClass;
1695             for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
1696             {
1697                if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
1698                {
1699                   for(sClass = sClass.base; sClass; sClass = sClass.base)
1700                   {
1701                      if(sClass.templateClass) sClass = sClass.templateClass;
1702                      id += sClass.templateParams.count;
1703                   }
1704                   break;
1705                }
1706                id++;
1707             }
1708             if(curParam) break;
1709          }
1710
1711          if(curParam)
1712          {
1713             ClassTemplateArgument arg = _class.templateArgs[id];
1714             if(arg.dataTypeString)
1715             {
1716                // FreeType(type);
1717                type = ProcessTypeString(arg.dataTypeString, false);
1718                freeType = true;
1719                if(type && _class.templateClass)
1720                   type.passAsTemplate = true;
1721                if(type)
1722                {
1723                   // type.refCount++;
1724                   /*if(!exp.destType)
1725                   {
1726                      exp.destType = ProcessTypeString(arg.dataTypeString, false);
1727                      exp.destType.refCount++;
1728                   }*/
1729                }
1730             }
1731          }
1732       }
1733       if(type && type.kind == classType && type._class && type._class.registered && strchr(type._class.registered.fullName, '<'))
1734       {
1735          Class expClass = type._class.registered;
1736          Class cClass = null;
1737          int c;
1738          int paramCount = 0;
1739          int lastParam = -1;
1740
1741          char templateString[1024];
1742          ClassTemplateParameter param;
1743          sprintf(templateString, "%s<", expClass.templateClass.fullName);
1744          for(cClass = expClass; cClass; cClass = cClass.base)
1745          {
1746             int p = 0;
1747             if(cClass.templateClass) cClass = cClass.templateClass;
1748             for(param = cClass.templateParams.first; param; param = param.next)
1749             {
1750                int id = p;
1751                Class sClass;
1752                ClassTemplateArgument arg;
1753                for(sClass = cClass.base; sClass; sClass = sClass.base)
1754                {
1755                   if(sClass.templateClass) sClass = sClass.templateClass;
1756                   id += sClass.templateParams.count;
1757                }
1758                arg = expClass.templateArgs[id];
1759
1760                for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
1761                {
1762                   ClassTemplateParameter cParam;
1763                   //int p = numParams - sClass.templateParams.count;
1764                   int p = 0;
1765                   Class nextClass;
1766                   if(sClass.templateClass) sClass = sClass.templateClass;
1767
1768                   for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
1769                   {
1770                      if(nextClass.templateClass) nextClass = nextClass.templateClass;
1771                      p += nextClass.templateParams.count;
1772                   }
1773
1774                   for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
1775                   {
1776                      if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
1777                      {
1778                         if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1779                         {
1780                            arg.dataTypeString = _class.templateArgs[p].dataTypeString;
1781                            arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
1782                            break;
1783                         }
1784                      }
1785                   }
1786                }
1787
1788                {
1789                   char argument[256];
1790                   argument[0] = '\0';
1791                   /*if(arg.name)
1792                   {
1793                      strcat(argument, arg.name.string);
1794                      strcat(argument, " = ");
1795                   }*/
1796                   switch(param.type)
1797                   {
1798                      case expression:
1799                      {
1800                         // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
1801                         char expString[1024];
1802                         OldList * specs = MkList();
1803                         Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
1804                         Expression exp;
1805                         char * string = PrintHexUInt64(arg.expression.ui64);
1806                         exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
1807                         delete string;
1808
1809                         ProcessExpressionType(exp);
1810                         ComputeExpression(exp);
1811                         expString[0] = '\0';
1812                         PrintExpression(exp, expString);
1813                         strcat(argument, expString);
1814                         //delete exp;
1815                         FreeExpression(exp);
1816                         break;
1817                      }
1818                      case identifier:
1819                      {
1820                         strcat(argument, arg.member.name);
1821                         break;
1822                      }
1823                      case TemplateParameterType::type:
1824                      {
1825                         if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
1826                            strcat(argument, arg.dataTypeString);
1827                         break;
1828                      }
1829                   }
1830                   if(argument[0])
1831                   {
1832                      if(paramCount) strcat(templateString, ", ");
1833                      if(lastParam != p - 1)
1834                      {
1835                         strcat(templateString, param.name);
1836                         strcat(templateString, " = ");
1837                      }
1838                      strcat(templateString, argument);
1839                      paramCount++;
1840                      lastParam = p;
1841                   }
1842                   p++;
1843                }
1844             }
1845          }
1846          {
1847             int len = strlen(templateString);
1848             if(templateString[len-1] == '<')
1849                len--;
1850             else
1851             {
1852                if(templateString[len-1] == '>')
1853                   templateString[len++] = ' ';
1854                templateString[len++] = '>';
1855             }
1856             templateString[len++] = '\0';
1857          }
1858          {
1859             Context context = SetupTemplatesContext(_class);
1860             if(freeType) FreeType(type);
1861             type = ProcessTypeString(templateString, false);
1862             freeType = true;
1863             FinishTemplatesContext(context);
1864          }
1865       }
1866
1867       if(method && member.initializer && member.initializer.type == expInitializer && member.initializer.exp)
1868       {
1869          ProcessExpressionType(member.initializer.exp);
1870          if(!member.initializer.exp.expType)
1871          {
1872             if(inCompiler)
1873             {
1874                char expString[10240];
1875                expString[0] = '\0';
1876                PrintExpression(member.initializer.exp, expString);
1877                ChangeCh(expString, '\n', ' ');
1878                Compiler_Error($"unresolved symbol used as an instance method %s\n", expString);
1879             }
1880          }
1881          //else if(!MatchTypes(member.exp.expType, type, null, _class, null, true, true, false, false))
1882          else if(!MatchTypes(member.initializer.exp.expType, type, null, null, _class, true, true, false, false))
1883          {
1884             Compiler_Error($"incompatible instance method %s\n", ident.string);
1885          }
1886       }
1887       else if(member.initializer)
1888       {
1889          /*
1890          FreeType(member.exp.destType);
1891          member.exp.destType = type;
1892          if(member.exp.destType)
1893             member.exp.destType.refCount++;
1894          ProcessExpressionType(member.exp);
1895          */
1896
1897          ProcessInitializer(member.initializer, type);
1898       }
1899       if(freeType) FreeType(type);
1900    }
1901    else
1902    {
1903       if(_class && _class.type == unitClass)
1904       {
1905          if(member.initializer)
1906          {
1907             /*
1908             FreeType(member.exp.destType);
1909             member.exp.destType = MkClassType(_class.fullName);
1910             ProcessExpressionType(member.initializer, type);
1911             */
1912             Type type = MkClassType(_class.fullName);
1913             ProcessInitializer(member.initializer, type);
1914             FreeType(type);
1915          }
1916       }
1917       else
1918       {
1919          if(member.initializer)
1920          {
1921             //ProcessExpressionType(member.exp);
1922             ProcessInitializer(member.initializer, null);
1923          }
1924          if(ident)
1925          {
1926             if(method)
1927             {
1928                Compiler_Error($"couldn't find virtual method %s in class %s\n", ident.string, _class.fullName);
1929             }
1930             else if(_class)
1931             {
1932                Compiler_Error($"couldn't find member %s in class %s\n", ident.string, _class.fullName);
1933                if(inCompiler)
1934                   eClass_AddDataMember(_class, ident.string, "int", 0, 0, publicAccess);
1935             }
1936          }
1937          else if(_class)
1938             Compiler_Error($"too many initializers for instantiation of class %s\n", _class.fullName);
1939       }
1940    }
1941 }
1942
1943 void ProcessInstantiationType(Instantiation inst)
1944 {
1945    yylloc = inst.loc;
1946    if(inst._class)
1947    {
1948       MembersInit members;
1949       Symbol classSym; // = inst._class.symbol; // FindClass(inst._class.name);
1950       Class _class;
1951
1952       /*if(!inst._class.symbol)
1953          inst._class.symbol = FindClass(inst._class.name);*/
1954       classSym = inst._class.symbol;
1955       _class = classSym ? classSym.registered : null;
1956
1957       // DANGER: Patch for mutex not declaring its struct when not needed
1958       if(!_class || _class.type != noHeadClass)
1959          DeclareStruct(inst._class.name, false); //_class && _class.type == noHeadClass);
1960
1961       afterExternal = afterExternal ? afterExternal : curExternal;
1962
1963       if(inst.exp)
1964          ProcessExpressionType(inst.exp);
1965
1966       inst.isConstant = true;
1967       if(inst.members)
1968       {
1969          DataMember curMember = null;
1970          Class curClass = null;
1971          DataMember subMemberStack[256];
1972          int subMemberStackPos = 0;
1973
1974          for(members = inst.members->first; members; members = members.next)
1975          {
1976             switch(members.type)
1977             {
1978                case methodMembersInit:
1979                {
1980                   char name[1024];
1981                   static uint instMethodID = 0;
1982                   External external = curExternal;
1983                   Context context = curContext;
1984                   Declarator declarator = members.function.declarator;
1985                   Identifier nameID = GetDeclId(declarator);
1986                   char * unmangled = nameID ? nameID.string : null;
1987                   Expression exp;
1988                   External createdExternal = null;
1989
1990                   if(inCompiler)
1991                   {
1992                      char number[16];
1993                      //members.function.dontMangle = true;
1994                      strcpy(name, "__ecereInstMeth_");
1995                      FullClassNameCat(name, _class ? _class.fullName : "_UNKNOWNCLASS", false);
1996                      strcat(name, "_");
1997                      strcat(name, nameID.string);
1998                      strcat(name, "_");
1999                      sprintf(number, "_%08d", instMethodID++);
2000                      strcat(name, number);
2001                      nameID.string = CopyString(name);
2002                   }
2003
2004                   // Do modifications here...
2005                   if(declarator)
2006                   {
2007                      Symbol symbol = declarator.symbol;
2008                      Method method = eClass_FindMethod(_class, unmangled, privateModule);
2009
2010                      if(method && method.type == virtualMethod)
2011                      {
2012                         symbol.method = method;
2013                         ProcessMethodType(method);
2014
2015                         if(!symbol.type.thisClass)
2016                         {
2017                            if(method.dataType.thisClass && currentClass &&
2018                               eClass_IsDerived(currentClass, method.dataType.thisClass.registered))
2019                            {
2020                               if(!currentClass.symbol)
2021                                  currentClass.symbol = FindClass(currentClass.fullName);
2022                               symbol.type.thisClass = currentClass.symbol;
2023                            }
2024                            else
2025                            {
2026                               if(!_class.symbol)
2027                                  _class.symbol = FindClass(_class.fullName);
2028                               symbol.type.thisClass = _class.symbol;
2029                            }
2030                         }
2031                         // TESTING THIS HERE:
2032                         DeclareType(symbol.type, true, true);
2033
2034                      }
2035                      else if(classSym)
2036                      {
2037                         Compiler_Error($"couldn't find virtual method %s in class %s\n",
2038                            unmangled, classSym.string);
2039                      }
2040                   }
2041
2042                   //declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2043                   createdExternal = ProcessClassFunction(classSym ? classSym.registered : null, members.function, ast, afterExternal, true);
2044
2045                   if(nameID)
2046                   {
2047                      FreeSpecifier(nameID._class);
2048                      nameID._class = null;
2049                   }
2050
2051                   if(inCompiler)
2052                   {
2053
2054                      Type type = declarator.symbol.type;
2055                      External oldExternal = curExternal;
2056
2057                      // *** Commented this out... Any negative impact? Yes: makes double prototypes declarations... Why was it commented out?
2058                      // *** It was commented out for problems such as
2059                      /*
2060                            class VirtualDesktop : Window
2061                            {
2062                               clientSize = Size { };
2063                               Timer timer
2064                               {
2065                                  bool DelayExpired()
2066                                  {
2067                                     clientSize.w;
2068                                     return true;
2069                                  }
2070                               };
2071                            }
2072                      */
2073                      // Commented Out: Good for bet.ec in Poker (Otherwise: obj\bet.c:187: error: `currentBet' undeclared (first use in this function))
2074
2075                      declarator.symbol.id = declarator.symbol.idCode = curExternal.symbol.idCode;
2076
2077                      /*
2078                      if(strcmp(declarator.symbol.string, name))
2079                      {
2080                         printf("TOCHECK: Look out for this\n");
2081                         delete declarator.symbol.string;
2082                         declarator.symbol.string = CopyString(name);
2083                      }
2084
2085                      if(!declarator.symbol.parent && globalContext.symbols.root != (BTNode)declarator.symbol)
2086                      {
2087                         printf("TOCHECK: Will this ever be in a list? Yes.\n");
2088                         excludedSymbols->Remove(declarator.symbol);
2089                         globalContext.symbols.Add((BTNode)declarator.symbol);
2090                         if(strstr(declarator.symbol.string), "::")
2091                            globalContext.hasNameSpace = true;
2092
2093                      }
2094                      */
2095
2096                      //curExternal = curExternal.prev;
2097                      //afterExternal = afterExternal->next;
2098
2099                      //ProcessFunction(afterExternal->function);
2100
2101                      //curExternal = afterExternal;
2102                      {
2103                         External externalDecl;
2104                         externalDecl = MkExternalDeclaration(null);
2105                         ast->Insert(oldExternal.prev, externalDecl);
2106
2107                         // Which function does this process?
2108                         if(createdExternal.function)
2109                         {
2110                            ProcessFunction(createdExternal.function);
2111
2112                            //curExternal = oldExternal;
2113
2114                            {
2115                               //Declaration decl = MkDeclaration(members.function.specifiers, MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2116
2117                               Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
2118                                  MkListOne(MkInitDeclarator(CopyDeclarator(declarator), null)));
2119
2120                               //externalDecl = MkExternalDeclaration(decl);
2121
2122                               //***** ast->Insert(external.prev, externalDecl);
2123                               //ast->Insert(curExternal.prev, externalDecl);
2124                               externalDecl.declaration = decl;
2125                               if(decl.symbol && !decl.symbol.pointerExternal)
2126                                  decl.symbol.pointerExternal = externalDecl;
2127
2128                               // Trying this out...
2129                               declarator.symbol.pointerExternal = externalDecl;
2130                            }
2131                         }
2132                      }
2133                   }
2134                   else if(declarator)
2135                   {
2136                      curExternal = declarator.symbol.pointerExternal;
2137                      ProcessFunction((FunctionDefinition)members.function);
2138                   }
2139                   curExternal = external;
2140                   curContext = context;
2141
2142                   if(inCompiler)
2143                   {
2144                      FreeClassFunction(members.function);
2145
2146                      // In this pass, turn this into a MemberInitData
2147                      exp = QMkExpId(name);
2148                      members.type = dataMembersInit;
2149                      members.dataMembers = MkListOne(MkMemberInit(MkListOne(MkIdentifier(unmangled)), MkInitializerAssignment(exp)));
2150
2151                      delete unmangled;
2152                   }
2153                   break;
2154                }
2155                case dataMembersInit:
2156                {
2157                   if(members.dataMembers && classSym)
2158                   {
2159                      MemberInit member;
2160                      Location oldyyloc = yylloc;
2161                      for(member = members.dataMembers->first; member; member = member.next)
2162                      {
2163                         ProcessMemberInitData(member, classSym.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
2164                         if(member.initializer && !member.initializer.isConstant)
2165                            inst.isConstant = false;
2166                      }
2167                      yylloc = oldyyloc;
2168                   }
2169                   break;
2170                }
2171             }
2172          }
2173       }
2174    }
2175 }
2176
2177 static void DeclareType(Type type, bool declarePointers, bool declareParams)
2178 {
2179    // OPTIMIZATIONS: TESTING THIS...
2180    if(inCompiler)
2181    {
2182       if(type.kind == functionType)
2183       {
2184          Type param;
2185          if(declareParams)
2186          {
2187             for(param = type.params.first; param; param = param.next)
2188                DeclareType(param, declarePointers, true);
2189          }
2190          DeclareType(type.returnType, declarePointers, true);
2191       }
2192       else if(type.kind == pointerType && declarePointers)
2193          DeclareType(type.type, declarePointers, false);
2194       else if(type.kind == classType)
2195       {
2196          if(type._class.registered && (type._class.registered.type == structClass || type._class.registered.type == noHeadClass) && !type._class.declaring)
2197             DeclareStruct(type._class.registered.fullName, type._class.registered.type == noHeadClass);
2198       }
2199       else if(type.kind == structType || type.kind == unionType)
2200       {
2201          Type member;
2202          for(member = type.members.first; member; member = member.next)
2203             DeclareType(member, false, false);
2204       }
2205       else if(type.kind == arrayType)
2206          DeclareType(type.arrayType, declarePointers, false);
2207    }
2208 }
2209
2210 ClassTemplateArgument * FindTemplateArg(Class _class, TemplateParameter param)
2211 {
2212    ClassTemplateArgument * arg = null;
2213    int id = 0;
2214    ClassTemplateParameter curParam = null;
2215    Class sClass;
2216    for(sClass = _class; sClass; sClass = sClass.base)
2217    {
2218       id = 0;
2219       if(sClass.templateClass) sClass = sClass.templateClass;
2220       for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
2221       {
2222          if(curParam.type == TemplateParameterType::type && !strcmp(param.identifier.string, curParam.name))
2223          {
2224             for(sClass = sClass.base; sClass; sClass = sClass.base)
2225             {
2226                if(sClass.templateClass) sClass = sClass.templateClass;
2227                id += sClass.templateParams.count;
2228             }
2229             break;
2230          }
2231          id++;
2232       }
2233       if(curParam) break;
2234    }
2235    if(curParam)
2236    {
2237       arg = &_class.templateArgs[id];
2238       if(arg && param.type == type)
2239          arg->dataTypeClass = eSystem_FindClass(_class.module, arg->dataTypeString);
2240    }
2241    return arg;
2242 }
2243
2244 public Context SetupTemplatesContext(Class _class)
2245 {
2246    Context context = PushContext();
2247    context.templateTypesOnly = true;
2248    if(_class.symbol && ((Symbol)_class.symbol).templateParams)
2249    {
2250       TemplateParameter param = ((Symbol)_class.symbol).templateParams->first;
2251       for(; param; param = param.next)
2252       {
2253          if(param.type == type && param.identifier)
2254          {
2255             TemplatedType type { key = (uintptr)param.identifier.string, param = param };
2256             curContext.templateTypes.Add((BTNode)type);
2257          }
2258       }
2259    }
2260    else if(_class)
2261    {
2262       Class sClass;
2263       for(sClass = _class; sClass; sClass = sClass.base)
2264       {
2265          ClassTemplateParameter p;
2266          for(p = sClass.templateParams.first; p; p = p.next)
2267          {
2268             //OldList * specs = MkList();
2269             //Declarator decl = null;
2270             //decl = SpecDeclFromString(p.dataTypeString, specs, null);
2271             if(p.type == type)
2272             {
2273                TemplateParameter param = p.param;
2274                TemplatedType type;
2275                if(!param)
2276                {
2277                   // ADD DATA TYPE HERE...
2278                   p.param = param = TemplateParameter
2279                   {
2280                      identifier = MkIdentifier(p.name), type = p.type,
2281                      dataTypeString = p.dataTypeString /*, dataType = { specs, decl }*/
2282                   };
2283                }
2284                type = TemplatedType { key = (uintptr)p.name, param = param };
2285                curContext.templateTypes.Add((BTNode)type);
2286             }
2287          }
2288       }
2289    }
2290    return context;
2291 }
2292
2293 public void FinishTemplatesContext(Context context)
2294 {
2295    PopContext(context);
2296    FreeContext(context);
2297    delete context;
2298 }
2299
2300 public void ProcessMethodType(Method method)
2301 {
2302    if(!method.dataType)
2303    {
2304       Context context = SetupTemplatesContext(method._class);
2305
2306       method.dataType = ProcessTypeString(method.dataTypeString, false);
2307
2308       FinishTemplatesContext(context);
2309
2310       if(method.type != virtualMethod && method.dataType)
2311       {
2312          if(!method.dataType.thisClass && !method.dataType.staticMethod)
2313          {
2314             if(!method._class.symbol)
2315                method._class.symbol = FindClass(method._class.fullName);
2316             method.dataType.thisClass = method._class.symbol;
2317          }
2318       }
2319
2320       // Why was this commented out? Working fine without now...
2321
2322       /*
2323       if(method.dataType.kind == functionType && !method.dataType.staticMethod && !method.dataType.thisClass)
2324          method.dataType.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2325          */
2326    }
2327
2328    /*
2329    if(type)
2330    {
2331       char * par = strstr(type, "(");
2332       char * classOp = null;
2333       int classOpLen = 0;
2334       if(par)
2335       {
2336          int c;
2337          for(c = par-type-1; c >= 0; c++)
2338          {
2339             if(type[c] == ':' && type[c+1] == ':')
2340             {
2341                classOp = type + c - 1;
2342                for(c = c-1; c >=0 && !isspace(type[c]); c--)
2343                {
2344                   classOp--;
2345                   classOpLen++;
2346                }
2347                break;
2348             }
2349             else if(!isspace(type[c]))
2350                break;
2351          }
2352       }
2353       if(classOp)
2354       {
2355          char temp[1024];
2356          int typeLen = strlen(type);
2357          memcpy(temp, classOp, classOpLen);
2358          temp[classOpLen] = '\0';
2359          if(temp[0])
2360             _class = eSystem_FindClass(module, temp);
2361          else
2362             _class = null;
2363          method.dataTypeString = new char[typeLen - classOpLen + 1];
2364          memcpy(method.dataTypeString, type, classOp - type);
2365          memcpy(method.dataTypeString + (classOp - type), classOp + classOpLen, typeLen - (classOp - type + classOpLen));
2366       }
2367       else
2368          method.dataTypeString = type;
2369    }
2370    */
2371 }
2372
2373
2374 public void ProcessPropertyType(Property prop)
2375 {
2376    if(!prop.dataType)
2377    {
2378       Context context = SetupTemplatesContext(prop._class);
2379       prop.dataType = ProcessTypeString(prop.dataTypeString, false);
2380       FinishTemplatesContext(context);
2381    }
2382 }
2383
2384 public void DeclareMethod(Method method, char * name)
2385 {
2386    Symbol symbol = method.symbol;
2387    if(!symbol || (!symbol.pointerExternal && method.type == virtualMethod) || symbol.id > (curExternal ? curExternal.symbol.idCode : -1))
2388    {
2389       bool imported = false;
2390       bool dllImport = false;
2391
2392       if(!method.dataType)
2393          method.dataType = ProcessTypeString(method.dataTypeString, false);
2394
2395       if(!symbol || symbol._import || method.type == virtualMethod)
2396       {
2397          if(!symbol || method.type == virtualMethod)
2398          {
2399             Symbol classSym;
2400             if(!method._class.symbol)
2401                method._class.symbol = FindClass(method._class.fullName);
2402             classSym = method._class.symbol;
2403             if(!classSym._import)
2404             {
2405                ModuleImport module;
2406
2407                if(method._class.module && method._class.module.name)
2408                   module = FindModule(method._class.module);
2409                else
2410                   module = mainModule;
2411                classSym._import = ClassImport
2412                {
2413                   name = CopyString(method._class.fullName);
2414                   isRemote = method._class.isRemote;
2415                };
2416                module.classes.Add(classSym._import);
2417             }
2418             if(!symbol)
2419             {
2420                symbol = method.symbol = Symbol { };
2421             }
2422             if(!symbol._import)
2423             {
2424                symbol._import = (ClassImport)MethodImport
2425                {
2426                   name = CopyString(method.name);
2427                   isVirtual = method.type == virtualMethod;
2428                };
2429                classSym._import.methods.Add(symbol._import);
2430             }
2431             if(!symbol)
2432             {
2433                // Set the symbol type
2434                /*
2435                if(!type.thisClass)
2436                {
2437                   type.thisClass = method._class.symbol; // FindClass(method._class.fullName);
2438                }
2439                else if(type.thisClass == (void *)-1)
2440                {
2441                   type.thisClass = null;
2442                }
2443                */
2444                // symbol.type = ProcessTypeString(method.dataTypeString, false);
2445                symbol.type = method.dataType;
2446                if(symbol.type) symbol.type.refCount++;
2447             }
2448             /*
2449             if(!method.thisClass || strcmp(method.thisClass, "void"))
2450                symbol.type.params.Insert(null,
2451                   MkClassType(method.thisClass ? method.thisClass : method._class.fullName));
2452             */
2453          }
2454          if(!method.dataType.dllExport)
2455          {
2456             imported = true;
2457             if((method._class.module != privateModule || !strcmp(method._class.name, "float") || !strcmp(method._class.name, "double")) && method._class.module.importType != staticImport)
2458                dllImport = true;
2459          }
2460       }
2461
2462       /* MOVING THIS UP
2463       if(!method.dataType)
2464          method.dataType = ((Symbol)method.symbol).type;
2465          //ProcessMethodType(method);
2466       */
2467
2468       if(method.type != virtualMethod && method.dataType)
2469          DeclareType(method.dataType, true, true);
2470
2471       if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2472       {
2473          // We need a declaration here :)
2474          Declaration decl;
2475          OldList * specifiers, * declarators;
2476          Declarator d;
2477          Declarator funcDecl;
2478          External external;
2479
2480          specifiers = MkList();
2481          declarators = MkList();
2482
2483          //if(imported)
2484          if(dllImport)
2485             ListAdd(specifiers, MkSpecifier(EXTERN));
2486          else if(method._class.symbol && ((Symbol)method._class.symbol).isStatic)
2487             ListAdd(specifiers, MkSpecifier(STATIC));
2488
2489          if(method.type == virtualMethod)
2490          {
2491             ListAdd(specifiers, MkSpecifier(INT));
2492             d = MkDeclaratorIdentifier(MkIdentifier(name));
2493          }
2494          else
2495          {
2496             d = MkDeclaratorIdentifier(MkIdentifier(name));
2497             //if(imported)
2498             if(dllImport)
2499                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2500             {
2501                Context context = SetupTemplatesContext(method._class);
2502                d = SpecDeclFromString(method.dataTypeString, specifiers, d);
2503                FinishTemplatesContext(context);
2504             }
2505             funcDecl = GetFuncDecl(d);
2506
2507             if(dllImport)
2508             {
2509                Specifier spec, next;
2510                for(spec = specifiers->first; spec; spec = next)
2511                {
2512                   next = spec.next;
2513                   if(spec.type == extendedSpecifier)
2514                   {
2515                      specifiers->Remove(spec);
2516                      FreeSpecifier(spec);
2517                   }
2518                }
2519             }
2520
2521             // Add this parameter if not a static method
2522             if(method.dataType && !method.dataType.staticMethod)
2523             {
2524                if(funcDecl && funcDecl.function.parameters && funcDecl.function.parameters->count)
2525                {
2526                   Class _class = method.dataType.thisClass ? method.dataType.thisClass.registered : method._class;
2527                   TypeName thisParam = MkTypeName(MkListOne(
2528                      MkSpecifierName/*MkClassName*/(method.dataType.thisClass ? method.dataType.thisClass.string : method._class.fullName)),
2529                      (_class && _class.type == systemClass) ? MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("this"))) : MkDeclaratorIdentifier(MkIdentifier("this")));
2530                   TypeName firstParam = ((TypeName)funcDecl.function.parameters->first);
2531                   Specifier firstSpec = firstParam.qualifiers ? firstParam.qualifiers->first : null;
2532
2533                   if(firstSpec && firstSpec.type == baseSpecifier && firstSpec.specifier == VOID && !firstParam.declarator)
2534                   {
2535                      TypeName param = funcDecl.function.parameters->first;
2536                      funcDecl.function.parameters->Remove(param);
2537                      FreeTypeName(param);
2538                   }
2539
2540                   if(!funcDecl.function.parameters)
2541                      funcDecl.function.parameters = MkList();
2542                   funcDecl.function.parameters->Insert(null, thisParam);
2543                }
2544             }
2545             // Make sure we don't have empty parameter declarations for static methods...
2546             /*
2547             else if(!funcDecl.function.parameters)
2548             {
2549                funcDecl.function.parameters = MkList();
2550                funcDecl.function.parameters->Insert(null,
2551                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2552             }*/
2553          }
2554          // TESTING THIS:
2555          ProcessDeclarator(d);
2556
2557          ListAdd(declarators, MkInitDeclarator(d, null));
2558
2559          decl = MkDeclaration(specifiers, declarators);
2560
2561          ReplaceThisClassSpecifiers(specifiers, method._class);
2562
2563          // Keep a different symbol for the function definition than the declaration...
2564          if(symbol.pointerExternal)
2565          {
2566             Symbol functionSymbol { };
2567
2568             // Copy symbol
2569             {
2570                *functionSymbol = *symbol;
2571                functionSymbol.string = CopyString(symbol.string);
2572                if(functionSymbol.type)
2573                   functionSymbol.type.refCount++;
2574             }
2575
2576             excludedSymbols->Add(functionSymbol);
2577             symbol.pointerExternal.symbol = functionSymbol;
2578          }
2579          external = MkExternalDeclaration(decl);
2580          if(curExternal)
2581             ast->Insert(curExternal ? curExternal.prev : null, external);
2582          external.symbol = symbol;
2583          symbol.pointerExternal = external;
2584       }
2585       else if(ast)
2586       {
2587          // Move declaration higher...
2588          ast->Move(symbol.pointerExternal, curExternal.prev);
2589       }
2590
2591       symbol.id = curExternal ? curExternal.symbol.idCode : MAXINT;
2592    }
2593 }
2594
2595 char * ReplaceThisClass(Class _class)
2596 {
2597    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2598    {
2599       bool first = true;
2600       int p = 0;
2601       ClassTemplateParameter param;
2602       int lastParam = -1;
2603
2604       char className[1024];
2605       strcpy(className, _class.fullName);
2606       for(param = _class.templateParams.first; param; param = param.next)
2607       {
2608          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2609          {
2610             if(first) strcat(className, "<");
2611             if(!first) strcat(className, ", ");
2612             if(lastParam + 1 != p)
2613             {
2614                strcat(className, param.name);
2615                strcat(className, " = ");
2616             }
2617             strcat(className, param.name);
2618             first = false;
2619             lastParam = p;
2620          }
2621          p++;
2622       }
2623       if(!first)
2624       {
2625          int len = strlen(className);
2626          if(className[len-1] == '>') className[len++] = ' ';
2627          className[len++] = '>';
2628          className[len++] = '\0';
2629       }
2630       return CopyString(className);
2631    }
2632    else
2633       return CopyString(_class.fullName);
2634 }
2635
2636 Type ReplaceThisClassType(Class _class)
2637 {
2638    if(thisClassParams && _class.templateParams.count && !_class.templateClass)
2639    {
2640       bool first = true;
2641       int p = 0;
2642       ClassTemplateParameter param;
2643       int lastParam = -1;
2644       char className[1024];
2645       strcpy(className, _class.fullName);
2646
2647       for(param = _class.templateParams.first; param; param = param.next)
2648       {
2649          // if((!param.defaultArg.dataTypeString && !param.defaultArg.expression.ui64))
2650          {
2651             if(first) strcat(className, "<");
2652             if(!first) strcat(className, ", ");
2653             if(lastParam + 1 != p)
2654             {
2655                strcat(className, param.name);
2656                strcat(className, " = ");
2657             }
2658             strcat(className, param.name);
2659             first = false;
2660             lastParam = p;
2661          }
2662          p++;
2663       }
2664       if(!first)
2665       {
2666          int len = strlen(className);
2667          if(className[len-1] == '>') className[len++] = ' ';
2668          className[len++] = '>';
2669          className[len++] = '\0';
2670       }
2671       return MkClassType(className);
2672       //return ProcessTypeString(className, false);
2673    }
2674    else
2675    {
2676       return MkClassType(_class.fullName);
2677       //return ProcessTypeString(_class.fullName, false);
2678    }
2679 }
2680
2681 void ReplaceThisClassSpecifiers(OldList specs, Class _class)
2682 {
2683    if(specs != null && _class)
2684    {
2685       Specifier spec;
2686       for(spec = specs.first; spec; spec = spec.next)
2687       {
2688          if(spec.type == baseSpecifier && spec.specifier == THISCLASS)
2689          {
2690             spec.type = nameSpecifier;
2691             spec.name = ReplaceThisClass(_class);
2692             spec.symbol = FindClass(spec.name); //_class.symbol;
2693          }
2694       }
2695    }
2696 }
2697
2698 // Returns imported or not
2699 bool DeclareFunction(GlobalFunction function, char * name)
2700 {
2701    Symbol symbol = function.symbol;
2702    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2703    {
2704       bool imported = false;
2705       bool dllImport = false;
2706
2707       if(!function.dataType)
2708       {
2709          function.dataType = ProcessTypeString(function.dataTypeString, false);
2710          if(!function.dataType.thisClass)
2711             function.dataType.staticMethod = true;
2712       }
2713
2714       if(inCompiler)
2715       {
2716          if(!symbol)
2717          {
2718             ModuleImport module = FindModule(function.module);
2719             // WARNING: This is not added anywhere...
2720             symbol = function.symbol = Symbol {  };
2721
2722             if(module.name)
2723             {
2724                if(!function.dataType.dllExport)
2725                {
2726                   symbol._import = (ClassImport)FunctionImport { name = CopyString(function.name) };
2727                   module.functions.Add(symbol._import);
2728                }
2729             }
2730             // Set the symbol type
2731             {
2732                symbol.type = ProcessTypeString(function.dataTypeString, false);
2733                if(!symbol.type.thisClass)
2734                   symbol.type.staticMethod = true;
2735             }
2736          }
2737          imported = symbol._import ? true : false;
2738          if(imported && function.module != privateModule && function.module.importType != staticImport)
2739             dllImport = true;
2740       }
2741
2742       DeclareType(function.dataType, true, true);
2743
2744       if(inCompiler)
2745       {
2746          if(!symbol.pointerExternal || symbol.pointerExternal.type == functionExternal)
2747          {
2748             // We need a declaration here :)
2749             Declaration decl;
2750             OldList * specifiers, * declarators;
2751             Declarator d;
2752             Declarator funcDecl;
2753             External external;
2754
2755             specifiers = MkList();
2756             declarators = MkList();
2757
2758             //if(imported)
2759                ListAdd(specifiers, MkSpecifier(EXTERN));
2760             /*
2761             else
2762                ListAdd(specifiers, MkSpecifier(STATIC));
2763             */
2764
2765             d = MkDeclaratorIdentifier(MkIdentifier(imported ? name : function.name));
2766             //if(imported)
2767             if(dllImport)
2768                d = MkDeclaratorBrackets(MkDeclaratorPointer(MkPointer(null, null), d));
2769
2770             d = SpecDeclFromString(function.dataTypeString, specifiers, d);
2771             // TAKE OUT THE DLL EXPORT IF STATICALLY IMPORTED:
2772             if(function.module.importType == staticImport)
2773             {
2774                Specifier spec;
2775                for(spec = specifiers->first; spec; spec = spec.next)
2776                   if(spec.type == extendedSpecifier && spec.extDecl && spec.extDecl.type == extDeclString && !strcmp(spec.extDecl.s, "dllexport"))
2777                   {
2778                      specifiers->Remove(spec);
2779                      FreeSpecifier(spec);
2780                      break;
2781                   }
2782             }
2783
2784             funcDecl = GetFuncDecl(d);
2785
2786             // Make sure we don't have empty parameter declarations for static methods...
2787             if(funcDecl && !funcDecl.function.parameters)
2788             {
2789                funcDecl.function.parameters = MkList();
2790                funcDecl.function.parameters->Insert(null,
2791                   MkTypeName(MkListOne(MkSpecifier(VOID)),null));
2792             }
2793
2794             ListAdd(declarators, MkInitDeclarator(d, null));
2795
2796             {
2797                Context oldCtx = curContext;
2798                curContext = globalContext;
2799                decl = MkDeclaration(specifiers, declarators);
2800                curContext = oldCtx;
2801             }
2802
2803             // Keep a different symbol for the function definition than the declaration...
2804             if(symbol.pointerExternal)
2805             {
2806                Symbol functionSymbol { };
2807                // Copy symbol
2808                {
2809                   *functionSymbol = *symbol;
2810                   functionSymbol.string = CopyString(symbol.string);
2811                   if(functionSymbol.type)
2812                      functionSymbol.type.refCount++;
2813                }
2814
2815                excludedSymbols->Add(functionSymbol);
2816
2817                symbol.pointerExternal.symbol = functionSymbol;
2818             }
2819             external = MkExternalDeclaration(decl);
2820             if(curExternal)
2821                ast->Insert(curExternal.prev, external);
2822             external.symbol = symbol;
2823             symbol.pointerExternal = external;
2824          }
2825          else
2826          {
2827             // Move declaration higher...
2828             ast->Move(symbol.pointerExternal, curExternal.prev);
2829          }
2830
2831          if(curExternal)
2832             symbol.id = curExternal.symbol.idCode;
2833       }
2834    }
2835    return (symbol && symbol._import && function.module != privateModule && function.module.importType != staticImport) ? true : false;
2836 }
2837
2838 void DeclareGlobalData(GlobalData data)
2839 {
2840    Symbol symbol = data.symbol;
2841    if(curExternal && (!symbol || symbol.id > curExternal.symbol.idCode))
2842    {
2843       if(inCompiler)
2844       {
2845          if(!symbol)
2846             symbol = data.symbol = Symbol { };
2847       }
2848       if(!data.dataType)
2849          data.dataType = ProcessTypeString(data.dataTypeString, false);
2850       DeclareType(data.dataType, true, true);
2851       if(inCompiler)
2852       {
2853          if(!symbol.pointerExternal)
2854          {
2855             // We need a declaration here :)
2856             Declaration decl;
2857             OldList * specifiers, * declarators;
2858             Declarator d;
2859             External external;
2860
2861             specifiers = MkList();
2862             declarators = MkList();
2863
2864             ListAdd(specifiers, MkSpecifier(EXTERN));
2865             d = MkDeclaratorIdentifier(MkIdentifier(data.fullName));
2866             d = SpecDeclFromString(data.dataTypeString, specifiers, d);
2867
2868             ListAdd(declarators, MkInitDeclarator(d, null));
2869
2870             decl = MkDeclaration(specifiers, declarators);
2871             external = MkExternalDeclaration(decl);
2872             if(curExternal)
2873                ast->Insert(curExternal.prev, external);
2874             external.symbol = symbol;
2875             symbol.pointerExternal = external;
2876          }
2877          else
2878          {
2879             // Move declaration higher...
2880             ast->Move(symbol.pointerExternal, curExternal.prev);
2881          }
2882
2883          if(curExternal)
2884             symbol.id = curExternal.symbol.idCode;
2885       }
2886    }
2887 }
2888
2889 class Conversion : struct
2890 {
2891    Conversion prev, next;
2892    Property convert;
2893    bool isGet;
2894    Type resultType;
2895 };
2896
2897 public bool MatchTypes(Type source, Type dest, OldList conversions, Class owningClassSource, Class owningClassDest, bool doConversion, bool enumBaseType, bool acceptReversedParams, bool isConversionExploration)
2898 {
2899    if(source && dest)
2900    {
2901       // Property convert;
2902
2903       if(source.kind == templateType && dest.kind != templateType)
2904       {
2905          Type type = ProcessTemplateParameterType(source.templateParameter);
2906          if(type) source = type;
2907       }
2908
2909       if(dest.kind == templateType && source.kind != templateType)
2910       {
2911          Type type = ProcessTemplateParameterType(dest.templateParameter);
2912          if(type) dest = type;
2913       }
2914
2915       if(dest.classObjectType == typedObject)
2916       {
2917          if(source.classObjectType != anyObject)
2918             return true;
2919          else
2920          {
2921             // If either the source or the destination defines the class, accepts any_object as compatible for a typed_object
2922             if((dest._class && strcmp(dest._class.string, "class")) || (source._class && strcmp(source._class.string, "class")))
2923             {
2924                return true;
2925             }
2926          }
2927       }
2928       else
2929       {
2930          if(source.classObjectType == anyObject)
2931             return true;
2932          if(dest.classObjectType == anyObject && source.classObjectType != typedObject)
2933             return true;
2934       }
2935
2936       if((dest.kind == structType && source.kind == structType) ||
2937          (dest.kind == unionType && source.kind == unionType))
2938       {
2939          if((dest.enumName && source.enumName && !strcmp(dest.enumName, source.enumName)) ||
2940              (source.members.first && source.members.first == dest.members.first))
2941             return true;
2942       }
2943
2944       if(dest.kind == ellipsisType && source.kind != voidType)
2945          return true;
2946
2947       if(dest.kind == pointerType && dest.type.kind == voidType &&
2948          ((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))
2949          || source.kind == subClassType || source.kind == pointerType || source.kind == arrayType || source.kind == functionType || source.kind == thisClassType)
2950
2951          /*source.kind != voidType && source.kind != structType && source.kind != unionType  */
2952
2953          /*&& (source.kind != classType /-*|| source._class.registered.type != structClass)*/)
2954          return true;
2955       if(!isConversionExploration && source.kind == pointerType && source.type.kind == voidType &&
2956          ((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))
2957          || dest.kind == subClassType || dest.kind == pointerType || dest.kind == arrayType || dest.kind == functionType || dest.kind == thisClassType)
2958
2959          /* dest.kind != voidType && dest.kind != structType && dest.kind != unionType  */
2960
2961          /*&& (dest.kind != classType || dest._class.registered.type != structClass)*/)
2962          return true;
2963
2964       if(((source.kind == classType && dest.kind == classType) || (source.kind == subClassType && dest.kind == subClassType)) && source._class)
2965       {
2966          if(source._class.registered && source._class.registered.type == unitClass)
2967          {
2968             if(conversions != null)
2969             {
2970                if(source._class.registered == dest._class.registered)
2971                   return true;
2972             }
2973             else
2974             {
2975                Class sourceBase, destBase;
2976                for(sourceBase = source._class.registered; sourceBase && sourceBase.base.type != systemClass; sourceBase = sourceBase.base);
2977                for(destBase = dest._class.registered; destBase && destBase.base.type != systemClass; destBase = destBase.base);
2978                if(sourceBase == destBase)
2979                   return true;
2980             }
2981          }
2982          // Don't match enum inheriting from other enum if resolving enumeration values
2983          // TESTING: !dest.classObjectType
2984          else if(source._class && dest._class && (dest.classObjectType == source.classObjectType || !dest.classObjectType) &&
2985             (enumBaseType ||
2986                (!source._class.registered || source._class.registered.type != enumClass) ||
2987                (!dest._class.registered || dest._class.registered.type != enumClass)) && eClass_IsDerived(source._class.registered, dest._class.registered))
2988             return true;
2989          else
2990          {
2991             // Added this so that DefinedColor = Color doesn't go through ColorRGB property
2992             if(enumBaseType &&
2993                dest._class && dest._class.registered && dest._class.registered.type == enumClass &&
2994                ((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)
2995             {
2996                if(eClass_IsDerived(dest._class.registered, source._class.registered))
2997                {
2998                   return true;
2999                }
3000             }
3001          }
3002       }
3003
3004       // JUST ADDED THIS...
3005       if(source.kind == subClassType && dest.kind == classType && dest._class && !strcmp(dest._class.string, "ecere::com::Class"))
3006          return true;
3007
3008       if(doConversion)
3009       {
3010          // Just added this for Straight conversion of ColorAlpha => Color
3011          if(source.kind == classType)
3012          {
3013             Class _class;
3014             for(_class = source._class ? source._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                      if(MatchTypes(convert.dataType, dest, conversions, null, null, false, true, false, true))
3026                      {
3027                         if(!conversions && !convert.Get)
3028                            return true;
3029                         else if(conversions != null)
3030                         {
3031                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3032                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3033                               (dest.kind != classType || dest._class.registered != _class.base))
3034                               return true;
3035                            else
3036                            {
3037                               Conversion conv { convert = convert, isGet = true };
3038                               // conversions.Add(conv);
3039                               conversions.Insert(after, conv);
3040                               return true;
3041                            }
3042                         }
3043                      }
3044                   }
3045                }
3046             }
3047          }
3048
3049          // MOVING THIS??
3050
3051          if(dest.kind == classType)
3052          {
3053             Class _class;
3054             for(_class = dest._class ? dest._class.registered : null; _class; _class = _class.base)
3055             {
3056                Property convert;
3057                for(convert = _class.conversions.first; convert; convert = convert.next)
3058                {
3059                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3060                   {
3061                      // Conversion after = (conversions != null) ? conversions.last : null;
3062
3063                      if(!convert.dataType)
3064                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3065                      // Just added this equality check to prevent recursion.... Make it safer?
3066                      // Changed enumBaseType to false here to prevent all int-compatible enums to show up in AnchorValues
3067                      if(convert.dataType != dest && MatchTypes(source, convert.dataType, conversions, null, null, true, false /*true*/, false, true))
3068                      {
3069                         if(!conversions && !convert.Set)
3070                            return true;
3071                         else if(conversions != null)
3072                         {
3073                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3074                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3075                               (source.kind != classType || source._class.registered != _class.base))
3076                               return true;
3077                            else
3078                            {
3079                               // *** Testing this! ***
3080                               Conversion conv { convert = convert };
3081                               conversions.Add(conv);
3082                               //conversions.Insert(after, conv);
3083                               return true;
3084                            }
3085                         }
3086                      }
3087                   }
3088                }
3089             }
3090             /*if(dest._class.registered && !strcmp(dest._class.registered.name, "bool"))
3091             {
3092                if(source.kind != voidType && source.kind != structType && source.kind != unionType &&
3093                   (source.kind != classType || source._class.registered.type != structClass))
3094                   return true;
3095             }*/
3096
3097             // TESTING THIS... IS THIS OK??
3098             if(enumBaseType && dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3099             {
3100                if(!dest._class.registered.dataType)
3101                   dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3102                // Only support this for classes...
3103                if(dest._class.registered.dataType.kind == classType || source.truth || dest.truth/* ||
3104                   !strcmp(dest._class.registered.name, "bool") || (source.kind == classType && !strcmp(source._class.string, "bool"))*/)
3105                {
3106                   if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3107                   {
3108                      return true;
3109                   }
3110                }
3111             }
3112          }
3113
3114          // Moved this lower
3115          if(source.kind == classType)
3116          {
3117             Class _class;
3118             for(_class = source._class ? source._class.registered : null; _class; _class = _class.base)
3119             {
3120                Property convert;
3121                for(convert = _class.conversions.first; convert; convert = convert.next)
3122                {
3123                   if(convert.memberAccess == publicAccess || _class.module == privateModule)
3124                   {
3125                      Conversion after = (conversions != null) ? conversions.last : null;
3126
3127                      if(!convert.dataType)
3128                         convert.dataType = ProcessTypeString(convert.dataTypeString, false);
3129                      if(convert.dataType != source && MatchTypes(convert.dataType, dest, conversions, null, null, true, true, false, true))
3130                      {
3131                         if(!conversions && !convert.Get)
3132                            return true;
3133                         else if(conversions != null)
3134                         {
3135                            if(_class.type == unitClass && convert.dataType.kind == classType && convert.dataType._class &&
3136                               convert.dataType._class.registered && _class.base == convert.dataType._class.registered.base &&
3137                               (dest.kind != classType || dest._class.registered != _class.base))
3138                               return true;
3139                            else
3140                            {
3141                               Conversion conv { convert = convert, isGet = true };
3142
3143                               // conversions.Add(conv);
3144                               conversions.Insert(after, conv);
3145                               return true;
3146                            }
3147                         }
3148                      }
3149                   }
3150                }
3151             }
3152
3153             // TESTING THIS... IS THIS OK??
3154             if(enumBaseType && source._class && source._class.registered && source._class.registered.type == enumClass)
3155             {
3156                if(!source._class.registered.dataType)
3157                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3158                if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, true, false, false))
3159                {
3160                   return true;
3161                }
3162             }
3163          }
3164       }
3165
3166       if(source.kind == classType || source.kind == subClassType)
3167          ;
3168       else if(dest.kind == source.kind &&
3169          (dest.kind != structType && dest.kind != unionType &&
3170           dest.kind != functionType && dest.kind != arrayType && dest.kind != pointerType && dest.kind != methodType))
3171           return true;
3172       // RECENTLY ADDED THESE
3173       else if(dest.kind == doubleType && source.kind == floatType)
3174          return true;
3175       else if(dest.kind == shortType && (source.kind == charType || source.kind == _BoolType))
3176          return true;
3177       else if(dest.kind == intType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intSizeType /* Exception here for size_t */))
3178          return true;
3179       else if(dest.kind == int64Type && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intPtrType || source.kind == intSizeType))
3180          return true;
3181       else if(dest.kind == intPtrType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == intSizeType || source.kind == int64Type))
3182          return true;
3183       else if(dest.kind == intSizeType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType || source.kind == int64Type || source.kind == intPtrType))
3184          return true;
3185       else if(source.kind == enumType &&
3186          (dest.kind == intType || dest.kind == shortType || dest.kind == charType || source.kind == _BoolType || dest.kind == longType || dest.kind == int64Type || dest.kind == intPtrType || dest.kind == intSizeType))
3187           return true;
3188       else if(dest.kind == enumType &&
3189          (source.kind == intType || source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == longType || source.kind == int64Type || source.kind == intPtrType || source.kind == intSizeType))
3190           return true;
3191       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) &&
3192               ((source.kind == functionType || (source.kind == pointerType && source.type.kind == functionType) || source.kind == methodType)))
3193       {
3194          Type paramSource, paramDest;
3195
3196          if(dest.kind == methodType)
3197             owningClassDest = dest.methodClass ? dest.methodClass : dest.method._class;
3198          if(source.kind == methodType)
3199             owningClassSource = source.methodClass ? source.methodClass : source.method._class;
3200
3201          if(dest.kind == pointerType && dest.type.kind == functionType) dest = dest.type;
3202          if(source.kind == pointerType && source.type.kind == functionType) source = source.type;
3203          if(dest.kind == methodType)
3204             dest = dest.method.dataType;
3205          if(source.kind == methodType)
3206             source = source.method.dataType;
3207
3208          paramSource = source.params.first;
3209          if(paramSource && paramSource.kind == voidType) paramSource = null;
3210          paramDest = dest.params.first;
3211          if(paramDest && paramDest.kind == voidType) paramDest = null;
3212
3213
3214          if((dest.staticMethod || (!dest.thisClass && !owningClassDest)) &&
3215             !(source.staticMethod || (!source.thisClass && !owningClassSource)))
3216          {
3217             // Source thisClass must be derived from destination thisClass
3218             if(!paramDest || (!(paramDest.kind == pointerType && paramDest.type && paramDest.type.kind == voidType) && (paramDest.kind != classType ||
3219                !eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource,paramDest._class.registered))))
3220             {
3221                if(paramDest && paramDest.kind == classType)
3222                   Compiler_Error($"method class must be derived from %s\n", paramDest._class.string);
3223                else
3224                   Compiler_Error($"method class should not take an object\n");
3225                return false;
3226             }
3227             paramDest = paramDest.next;
3228          }
3229          else if(!dest.staticMethod && (dest.thisClass || owningClassDest))
3230          {
3231             if((source.staticMethod || (!source.thisClass && !owningClassSource)))
3232             {
3233                if(dest.thisClass)
3234                {
3235                   if(!paramSource || paramSource.kind != classType || !eClass_IsDerived(paramSource._class.registered,dest.thisClass.registered))
3236                   {
3237                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3238                      return false;
3239                   }
3240                }
3241                else
3242                {
3243                   // THIS WAS BACKWARDS:
3244                   // if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(owningClassDest, paramSource._class.registered)))
3245                   if(!paramSource || paramSource.kind != classType || (owningClassDest && !eClass_IsDerived(paramSource._class.registered, owningClassDest)))
3246                   {
3247                      if(owningClassDest)
3248                        Compiler_Error($"%s expected to be derived from method class\n", owningClassDest.fullName);
3249                      else
3250                         Compiler_Error($"overriding class expected to be derived from method class\n");
3251                      return false;
3252                   }
3253                }
3254                paramSource = paramSource.next;
3255             }
3256             else
3257             {
3258                if(dest.thisClass)
3259                {
3260                   // Source thisClass must be derived from destination thisClass
3261                   if(!eClass_IsDerived(source.thisClass ? source.thisClass.registered : owningClassSource, dest.thisClass.registered))
3262                   {
3263                      Compiler_Error($"method class must be derived from %s\n", dest.thisClass.string);
3264                      return false;
3265                   }
3266                }
3267                else
3268                {
3269                   // THIS WAS BACKWARDS TOO??
3270                   // if(source.thisClass && owningClassDest && !eClass_IsDerived(owningClassDest, source.thisClass.registered))
3271                   if(source.thisClass && source.thisClass.registered && owningClassDest && !eClass_IsDerived(source.thisClass.registered, owningClassDest))
3272                   {
3273                      //if(owningClass)
3274                         Compiler_Error($"%s expected to be derived from method class\n", /*owningClass.name*/ source.thisClass.registered.fullName);
3275                      //else
3276                         //Compiler_Error($"overriding class expected to be derived from method class\n");
3277                      return false;
3278                   }
3279                }
3280             }
3281          }
3282
3283
3284          // Source return type must be derived from destination return type
3285          if(!MatchTypes(source.returnType, dest.returnType, null, null, null, true, true, false, false))
3286          {
3287             Compiler_Warning($"incompatible return type for function\n");
3288             return false;
3289          }
3290
3291          // Check parameters
3292
3293          for(; paramDest; paramDest = paramDest.next)
3294          {
3295             if(!paramSource)
3296             {
3297                //Compiler_Warning($"not enough parameters\n");
3298                Compiler_Error($"not enough parameters\n");
3299                return false;
3300             }
3301             {
3302                Type paramDestType = paramDest;
3303                Type paramSourceType = paramSource;
3304                Type type = paramDestType;
3305
3306                // *** WORKING CODE: TESTING THIS HERE FOR TEMPLATES ***
3307                if(paramDest.kind == templateType && paramDest.templateParameter.type == TemplateParameterType::type && owningClassSource &&
3308                   paramSource.kind != templateType)
3309                {
3310                   int id = 0;
3311                   ClassTemplateParameter curParam = null;
3312                   Class sClass;
3313                   for(sClass = owningClassSource; sClass; sClass = sClass.base)
3314                   {
3315                      id = 0;
3316                      if(sClass.templateClass) sClass = sClass.templateClass;
3317                      for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
3318                      {
3319                         if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
3320                         {
3321                            for(sClass = sClass.base; sClass; sClass = sClass.base)
3322                            {
3323                               if(sClass.templateClass) sClass = sClass.templateClass;
3324                               id += sClass.templateParams.count;
3325                            }
3326                            break;
3327                         }
3328                         id++;
3329                      }
3330                      if(curParam) break;
3331                   }
3332
3333                   if(curParam)
3334                   {
3335                      ClassTemplateArgument arg = owningClassSource.templateArgs[id];
3336                      paramDestType = type = ProcessTypeString(arg.dataTypeString, false);
3337                   }
3338                }
3339
3340                // paramDest must be derived from paramSource
3341                if(!MatchTypes(paramDestType, paramSourceType, null, null, null, true, true, false, false) &&
3342                   (!acceptReversedParams || !MatchTypes(paramSourceType, paramDestType, null, null, null, true, true, false, false)))
3343                {
3344                   char type[1024];
3345                   type[0] = 0;
3346                   PrintType(paramDest, type, false, true);
3347                   Compiler_Warning($"incompatible parameter %s (expected %s)\n", paramSource.name, type);
3348
3349                   if(paramDestType != paramDest)
3350                      FreeType(paramDestType);
3351                   return false;
3352                }
3353                if(paramDestType != paramDest)
3354                   FreeType(paramDestType);
3355             }
3356
3357             paramSource = paramSource.next;
3358          }
3359          if(paramSource)
3360          {
3361             Compiler_Error($"too many parameters\n");
3362             return false;
3363          }
3364          return true;
3365       }
3366       else if((dest.kind == functionType || (dest.kind == pointerType && dest.type.kind == functionType) || dest.kind == methodType) && (source.kind == pointerType && source.type.kind == voidType))
3367       {
3368          return true;
3369       }
3370       else if((dest.kind == pointerType || dest.kind == arrayType) &&
3371          (source.kind == arrayType || source.kind == pointerType))
3372       {
3373          if(MatchTypes(source.type, dest.type, null, null, null, true, true, false, false))
3374             return true;
3375       }
3376    }
3377    return false;
3378 }
3379
3380 static void FreeConvert(Conversion convert)
3381 {
3382    if(convert.resultType)
3383       FreeType(convert.resultType);
3384 }
3385
3386 bool MatchWithEnums_NameSpace(NameSpace nameSpace, Expression sourceExp, Type dest,
3387                               char * string, OldList conversions)
3388 {
3389    BTNamedLink link;
3390
3391    for(link = (BTNamedLink)nameSpace.classes.first; link; link = (BTNamedLink)((BTNode)link).next)
3392    {
3393       Class _class = link.data;
3394       if(_class.type == enumClass)
3395       {
3396          OldList converts { };
3397          Type type { };
3398          type.kind = classType;
3399
3400          if(!_class.symbol)
3401             _class.symbol = FindClass(_class.fullName);
3402          type._class = _class.symbol;
3403
3404          if(MatchTypes(type, dest, &converts, null, null, true, false, false, false))
3405          {
3406             NamedLink value;
3407             Class enumClass = eSystem_FindClass(privateModule, "enum");
3408             if(enumClass)
3409             {
3410                Class baseClass;
3411                for(baseClass = _class ; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
3412                {
3413                   EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
3414                   for(value = e.values.first; value; value = value.next)
3415                   {
3416                      if(!strcmp(value.name, string))
3417                         break;
3418                   }
3419                   if(value)
3420                   {
3421                      FreeExpContents(sourceExp);
3422                      FreeType(sourceExp.expType);
3423
3424                      sourceExp.isConstant = true;
3425                      sourceExp.expType = MkClassType(baseClass.fullName);
3426                      //if(inCompiler)
3427                      {
3428                         char constant[256];
3429                         sourceExp.type = constantExp;
3430                         if(!strcmp(baseClass.dataTypeString, "int"))
3431                            sprintf(constant, "%d",(int)value.data);
3432                         else
3433                            sprintf(constant, "0x%X",(int)value.data);
3434                         sourceExp.constant = CopyString(constant);
3435                         //for(;baseClass.base && baseClass.base.type != systemClass; baseClass = baseClass.base);
3436                      }
3437
3438                      while(converts.first)
3439                      {
3440                         Conversion convert = converts.first;
3441                         converts.Remove(convert);
3442                         conversions.Add(convert);
3443                      }
3444                      delete type;
3445                      return true;
3446                   }
3447                }
3448             }
3449          }
3450          if(converts.first)
3451             converts.Free(FreeConvert);
3452          delete type;
3453       }
3454    }
3455    for(nameSpace = (NameSpace *)nameSpace.nameSpaces.first; nameSpace != null; nameSpace = (NameSpace *)((BTNode)nameSpace).next)
3456       if(MatchWithEnums_NameSpace(nameSpace, sourceExp, dest, string, conversions))
3457          return true;
3458    return false;
3459 }
3460
3461 public bool ModuleVisibility(Module searchIn, Module searchFor)
3462 {
3463    SubModule subModule;
3464
3465    if(searchFor == searchIn)
3466       return true;
3467
3468    for(subModule = searchIn.modules.first; subModule; subModule = subModule.next)
3469    {
3470       if(subModule.importMode == publicAccess || searchIn == searchIn.application)
3471       {
3472          if(ModuleVisibility(subModule.module, searchFor))
3473             return true;
3474       }
3475    }
3476    return false;
3477 }
3478
3479 bool MatchWithEnums_Module(Module mainModule, Expression sourceExp, Type dest, char * string, OldList conversions)
3480 {
3481    Module module;
3482
3483    if(MatchWithEnums_NameSpace(mainModule.application.systemNameSpace, sourceExp, dest, string, conversions))
3484       return true;
3485    if(MatchWithEnums_NameSpace(mainModule.application.privateNameSpace, sourceExp, dest, string, conversions))
3486       return true;
3487    if(MatchWithEnums_NameSpace(mainModule.application.publicNameSpace, sourceExp, dest, string, conversions))
3488       return true;
3489
3490    for(module = mainModule.application.allModules.first; module; module = module.next)
3491    {
3492       if(ModuleVisibility(mainModule, module) && MatchWithEnums_NameSpace(module.publicNameSpace, sourceExp, dest, string, conversions))
3493          return true;
3494    }
3495    return false;
3496 }
3497
3498 bool MatchTypeExpression(Expression sourceExp, Type dest, OldList conversions, bool skipUnitBla)
3499 {
3500    Type source = sourceExp.expType;
3501    Type realDest = dest;
3502    Type backupSourceExpType = null;
3503
3504    if(dest.kind == pointerType && sourceExp.type == constantExp && !strtoul(sourceExp.constant, null, 0))
3505       return true;
3506
3507    if(!skipUnitBla && source && dest && source.kind == classType && dest.kind == classType)
3508    {
3509        if(source._class && source._class.registered && source._class.registered.type == unitClass)
3510        {
3511           Class sourceBase, destBase;
3512           for(sourceBase = source._class.registered;
3513               sourceBase && sourceBase.base && sourceBase.base.type != systemClass;
3514               sourceBase = sourceBase.base);
3515           for(destBase = dest._class.registered;
3516               destBase && destBase.base && destBase.base.type != systemClass;
3517               destBase = destBase.base);
3518           //if(source._class.registered == dest._class.registered)
3519           if(sourceBase == destBase)
3520              return true;
3521        }
3522    }
3523
3524    if(source)
3525    {
3526       OldList * specs;
3527       bool flag = false;
3528       int64 value = MAXINT;
3529
3530       source.refCount++;
3531       dest.refCount++;
3532
3533       if(sourceExp.type == constantExp)
3534       {
3535          if(source.isSigned)
3536             value = strtoll(sourceExp.constant, null, 0);
3537          else
3538             value = strtoull(sourceExp.constant, null, 0);
3539       }
3540       else if(sourceExp.type == opExp && sourceExp.op.op == '-' && !sourceExp.op.exp1 && sourceExp.op.exp2 && sourceExp.op.exp2.type == constantExp)
3541       {
3542          if(source.isSigned)
3543             value = -strtoll(sourceExp.op.exp2.constant, null, 0);
3544          else
3545             value = -strtoull(sourceExp.op.exp2.constant, null, 0);
3546       }
3547
3548       if(dest.kind != classType && source.kind == classType && source._class && source._class.registered &&
3549          !strcmp(source._class.registered.fullName, "ecere::com::unichar"))
3550       {
3551          FreeType(source);
3552          source = Type { kind = intType, isSigned = false, refCount = 1 };
3553       }
3554
3555       if(dest.kind == classType)
3556       {
3557          Class _class = dest._class ? dest._class.registered : null;
3558
3559          if(_class && _class.type == unitClass)
3560          {
3561             if(source.kind != classType)
3562             {
3563                Type tempType { };
3564                Type tempDest, tempSource;
3565
3566                for(; _class.base.type != systemClass; _class = _class.base);
3567                tempSource = dest;
3568                tempDest = tempType;
3569
3570                tempType.kind = classType;
3571                if(!_class.symbol)
3572                   _class.symbol = FindClass(_class.fullName);
3573
3574                tempType._class = _class.symbol;
3575                tempType.truth = dest.truth;
3576                if(tempType._class)
3577                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3578
3579                // NOTE: To handle bad warnings on int64 vs 32 bit eda::Id incompatibilities
3580                backupSourceExpType = sourceExp.expType;
3581                sourceExp.expType = dest; dest.refCount++;
3582                //sourceExp.expType = MkClassType(_class.fullName);
3583                flag = true;
3584
3585                delete tempType;
3586             }
3587          }
3588
3589
3590          // Why wasn't there something like this?
3591          if(_class && _class.type == bitClass && source.kind != classType)
3592          {
3593             if(!dest._class.registered.dataType)
3594                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3595             if(MatchTypes(source, dest._class.registered.dataType, conversions, null, null, true, true, false, false))
3596             {
3597                FreeType(source);
3598                FreeType(sourceExp.expType);
3599                source = sourceExp.expType = MkClassType(dest._class.string);
3600                source.refCount++;
3601
3602                //source.kind = classType;
3603                //source._class = dest._class;
3604             }
3605          }
3606
3607          // Adding two enumerations
3608          /*
3609          if(_class && _class.type == enumClass && source.kind == classType && source._class && source._class.registered && source._class.registered.type == enumClass)
3610          {
3611             if(!source._class.registered.dataType)
3612                source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3613             if(!dest._class.registered.dataType)
3614                dest._class.registered.dataType = ProcessTypeString(dest._class.registered.dataTypeString, false);
3615
3616             if(MatchTypes(source._class.registered.dataType, dest._class.registered.dataType, conversions, null, null, true, false, false))
3617             {
3618                FreeType(source);
3619                source = sourceExp.expType = MkClassType(dest._class.string);
3620                source.refCount++;
3621
3622                //source.kind = classType;
3623                //source._class = dest._class;
3624             }
3625          }*/
3626
3627          if(_class && !strcmp(_class.fullName, "ecere::com::Class") && source.kind == pointerType && source.type && source.type.kind == charType && sourceExp.type == stringExp)
3628          {
3629             OldList * specs = MkList();
3630             Declarator decl;
3631             char string[1024];
3632
3633             ReadString(string, sourceExp.string);
3634             decl = SpecDeclFromString(string, specs, null);
3635
3636             FreeExpContents(sourceExp);
3637             FreeType(sourceExp.expType);
3638
3639             sourceExp.type = classExp;
3640             sourceExp._classExp.specifiers = specs;
3641             sourceExp._classExp.decl = decl;
3642             sourceExp.expType = dest;
3643             dest.refCount++;
3644
3645             FreeType(source);
3646             FreeType(dest);
3647             if(backupSourceExpType) FreeType(backupSourceExpType);
3648             return true;
3649          }
3650       }
3651       else if(source.kind == classType)
3652       {
3653          Class _class = source._class ? source._class.registered : null;
3654
3655          if(_class && (_class.type == unitClass || !strcmp(_class.fullName, "bool") || /*_class.type == enumClass || */_class.type == bitClass ))  // TOCHECK: enumClass, bitClass is new here...
3656          {
3657             /*
3658             if(dest.kind != classType)
3659             {
3660                // Testing this simpler piece of code... (Broke Units Conversion to no unit Logic)
3661                if(!source._class.registered.dataType)
3662                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3663
3664                FreeType(dest);
3665                dest = MkClassType(source._class.string);
3666                //if(MatchTypes(source._class.registered.dataType, dest, conversions, null, null, true, false, false))
3667                //   dest = MkClassType(source._class.string);
3668             }
3669             */
3670
3671             if(dest.kind != classType)
3672             {
3673                Type tempType { };
3674                Type tempDest, tempSource;
3675
3676                if(!source._class.registered.dataType)
3677                   source._class.registered.dataType = ProcessTypeString(source._class.registered.dataTypeString, false);
3678
3679                for(; _class.base.type != systemClass; _class = _class.base);
3680                tempDest = source;
3681                tempSource = tempType;
3682                tempType.kind = classType;
3683                tempType._class = FindClass(_class.fullName);
3684                tempType.truth = source.truth;
3685                tempType.classObjectType = source.classObjectType;
3686
3687                if(tempType._class)
3688                   MatchTypes(tempSource, tempDest, conversions, null, null, true, true, false, false);
3689
3690                // PUT THIS BACK TESTING UNITS?
3691                if(conversions.last)
3692                {
3693                   ((Conversion)(conversions.last)).resultType = dest;
3694                   dest.refCount++;
3695                }
3696
3697                FreeType(sourceExp.expType);
3698                sourceExp.expType = MkClassType(_class.fullName);
3699                sourceExp.expType.truth = source.truth;
3700                sourceExp.expType.classObjectType = source.classObjectType;
3701
3702                // *** This if was commented out, put it back because "int a =^ Destroy()" shows up bool enum values in autocomplete ***
3703
3704                if(!sourceExp.destType)
3705                {
3706                   FreeType(sourceExp.destType);
3707                   sourceExp.destType = sourceExp.expType;
3708                   if(sourceExp.expType)
3709                      sourceExp.expType.refCount++;
3710                }
3711                //flag = true;
3712                //source = _class.dataType;
3713
3714
3715                // TOCHECK: TESTING THIS NEW CODE
3716                if(!_class.dataType)
3717                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3718                FreeType(dest);
3719                dest = MkClassType(source._class.string);
3720                dest.truth = source.truth;
3721                dest.classObjectType = source.classObjectType;
3722
3723                FreeType(source);
3724                source = _class.dataType;
3725                source.refCount++;
3726
3727                delete tempType;
3728             }
3729          }
3730       }
3731
3732       if(!flag)
3733       {
3734          if(MatchTypes(source, dest, conversions, null, null, true, true, false, false))
3735          {
3736             FreeType(source);
3737             FreeType(dest);
3738             return true;
3739          }
3740       }
3741
3742       // Implicit Casts
3743       /*
3744       if(source.kind == classType)
3745       {
3746          Class _class = source._class.registered;
3747          if(_class.type == unitClass)
3748          {
3749             if(!_class.dataType)
3750                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3751             source = _class.dataType;
3752          }
3753       }*/
3754
3755       if(dest.kind == classType)
3756       {
3757          Class _class = dest._class ? dest._class.registered : null;
3758          if(_class && !dest.truth && (_class.type == unitClass || !strcmp(_class.fullName, "bool") ||
3759             (/*_class.type == enumClass*/_class.type != structClass && !value && source.kind == intType) || _class.type == bitClass))   // TOCHECK: enumClass, bitClass is new here...
3760          {
3761             if(_class.type == normalClass || _class.type == noHeadClass)
3762             {
3763                Expression newExp { };
3764                *newExp = *sourceExp;
3765                if(sourceExp.destType) sourceExp.destType.refCount++;
3766                if(sourceExp.expType)  sourceExp.expType.refCount++;
3767                sourceExp.type = castExp;
3768                sourceExp.cast.typeName = MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(MkPointer(null, null), null));
3769                sourceExp.cast.exp = newExp;
3770                FreeType(sourceExp.expType);
3771                sourceExp.expType = null;
3772                ProcessExpressionType(sourceExp);
3773
3774                // In Debugger, this helps with addresses (e.g. null pointers) that end up casted to a void *: keeps a classType instead of a pointerType
3775                if(!inCompiler)
3776                {
3777                   FreeType(sourceExp.expType);
3778                   sourceExp.expType = dest;
3779                }
3780
3781                FreeType(source);
3782                if(inCompiler) FreeType(dest);
3783
3784                if(backupSourceExpType) FreeType(backupSourceExpType);
3785                return true;
3786             }
3787
3788             if(!_class.dataType)
3789                _class.dataType = ProcessTypeString(_class.dataTypeString, false);
3790             FreeType(dest);
3791             dest = _class.dataType;
3792             dest.refCount++;
3793          }
3794
3795          // Accept lower precision types for units, since we want to keep the unit type
3796          if(dest.kind == doubleType &&
3797             (source.kind == doubleType || source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType ||
3798              source.kind == charType || source.kind == _BoolType))
3799          {
3800             specs = MkListOne(MkSpecifier(DOUBLE));
3801          }
3802          else if(dest.kind == floatType &&
3803             (source.kind == floatType || dest.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3804             source.kind == _BoolType || source.kind == doubleType))
3805          {
3806             specs = MkListOne(MkSpecifier(FLOAT));
3807          }
3808          else if(dest.kind == int64Type && (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == charType ||
3809             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3810          {
3811             specs = MkList();
3812             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3813             ListAdd(specs, MkSpecifier(INT64));
3814          }
3815          else if(dest.kind == intType && (source.kind == intType || source.kind == shortType || source.kind == charType ||
3816             source.kind == _BoolType || source.kind == floatType || source.kind == doubleType))
3817          {
3818             specs = MkList();
3819             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3820             ListAdd(specs, MkSpecifier(INT));
3821          }
3822          else if(dest.kind == shortType && (source.kind == shortType || source.kind == charType || source.kind == _BoolType || source.kind == intType ||
3823             source.kind == floatType || source.kind == doubleType))
3824          {
3825             specs = MkList();
3826             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3827             ListAdd(specs, MkSpecifier(SHORT));
3828          }
3829          else if(dest.kind == charType && (source.kind == charType || source.kind == _BoolType || source.kind == shortType || source.kind == intType ||
3830             source.kind == floatType || source.kind == doubleType))
3831          {
3832             specs = MkList();
3833             if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3834             ListAdd(specs, MkSpecifier(CHAR));
3835          }
3836          else
3837          {
3838             FreeType(source);
3839             FreeType(dest);
3840             if(backupSourceExpType)
3841             {
3842                // Failed to convert: revert previous exp type
3843                if(sourceExp.expType) FreeType(sourceExp.expType);
3844                sourceExp.expType = backupSourceExpType;
3845             }
3846             return false;
3847          }
3848       }
3849       else if(dest.kind == doubleType &&
3850          (source.kind == doubleType || source.kind == floatType || source.kind == int64Type || source.kind == intType || source.kind == enumType || source.kind == shortType ||
3851           source.kind == _BoolType || source.kind == charType))
3852       {
3853          specs = MkListOne(MkSpecifier(DOUBLE));
3854       }
3855       else if(dest.kind == floatType &&
3856          (source.kind == floatType || source.kind == enumType || source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3857       {
3858          specs = MkListOne(MkSpecifier(FLOAT));
3859       }
3860       else if(dest.kind == _BoolType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3861          (value == 1 || value == 0))
3862       {
3863          specs = MkList();
3864          ListAdd(specs, MkSpecifier(BOOL));
3865       }
3866       else if(dest.kind == charType && (source.kind == _BoolType || source.kind == charType || source.kind == enumType || source.kind == shortType || source.kind == intType) &&
3867          (dest.isSigned ? (value >= -128 && value <= 127) : (value >= 0 && value <= 255)))
3868       {
3869          specs = MkList();
3870          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3871          ListAdd(specs, MkSpecifier(CHAR));
3872       }
3873       else if(dest.kind == shortType && (source.kind == enumType || source.kind == _BoolType || source.kind == charType || source.kind == shortType ||
3874          (source.kind == intType && (dest.isSigned ? (value >= -32768 && value <= 32767) : (value >= 0 && value <= 65535)))))
3875       {
3876          specs = MkList();
3877          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3878          ListAdd(specs, MkSpecifier(SHORT));
3879       }
3880       else if(dest.kind == intType && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType))
3881       {
3882          specs = MkList();
3883          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3884          ListAdd(specs, MkSpecifier(INT));
3885       }
3886       else if(dest.kind == int64Type && (source.kind == enumType || source.kind == shortType || source.kind == _BoolType || source.kind == charType || source.kind == intType || source.kind == int64Type))
3887       {
3888          specs = MkList();
3889          if(!dest.isSigned) ListAdd(specs, MkSpecifier(UNSIGNED));
3890          ListAdd(specs, MkSpecifier(INT64));
3891       }
3892       else if(dest.kind == enumType &&
3893          (source.kind == int64Type || source.kind == intType || source.kind == shortType || source.kind == _BoolType || source.kind == charType))
3894       {
3895          specs = MkListOne(MkEnum(MkIdentifier(dest.enumName), null));
3896       }
3897       else
3898       {
3899          FreeType(source);
3900          FreeType(dest);
3901          if(backupSourceExpType)
3902          {
3903             // Failed to convert: revert previous exp type
3904             if(sourceExp.expType) FreeType(sourceExp.expType);
3905             sourceExp.expType = backupSourceExpType;
3906          }
3907          return false;
3908       }
3909
3910       if(!flag)
3911       {
3912          Expression newExp { };
3913          *newExp = *sourceExp;
3914          newExp.prev = null;
3915          newExp.next = null;
3916          if(sourceExp.destType) sourceExp.destType.refCount++;
3917          if(sourceExp.expType)  sourceExp.expType.refCount++;
3918
3919          sourceExp.type = castExp;
3920          if(realDest.kind == classType)
3921          {
3922             sourceExp.cast.typeName = QMkClass(realDest._class.string, null);
3923             FreeList(specs, FreeSpecifier);
3924          }
3925          else
3926             sourceExp.cast.typeName = MkTypeName(specs, null);
3927          if(newExp.type == opExp)
3928          {
3929             sourceExp.cast.exp = MkExpBrackets(MkListOne(newExp));
3930          }
3931          else
3932             sourceExp.cast.exp = newExp;
3933
3934          FreeType(sourceExp.expType);
3935          sourceExp.expType = null;
3936          ProcessExpressionType(sourceExp);
3937       }
3938       else
3939          FreeList(specs, FreeSpecifier);
3940
3941       FreeType(dest);
3942       FreeType(source);
3943       if(backupSourceExpType) FreeType(backupSourceExpType);
3944
3945       return true;
3946    }
3947    else
3948    {
3949       while((sourceExp.type == bracketsExp || sourceExp.type == extensionExpressionExp) && sourceExp.list) sourceExp = sourceExp.list->last;
3950       if(sourceExp.type == identifierExp)
3951       {
3952          Identifier id = sourceExp.identifier;
3953          if(dest.kind == classType)
3954          {
3955             if(dest._class && dest._class.registered && dest._class.registered.type == enumClass)
3956             {
3957                Class _class = dest._class.registered;
3958                Class enumClass = eSystem_FindClass(privateModule, "enum");
3959                if(enumClass)
3960                {
3961                   for( ; _class && _class.type == ClassType::enumClass; _class = _class.base)
3962                   {
3963                      NamedLink value;
3964                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
3965                      for(value = e.values.first; value; value = value.next)
3966                      {
3967                         if(!strcmp(value.name, id.string))
3968                            break;
3969                      }
3970                      if(value)
3971                      {
3972                         FreeExpContents(sourceExp);
3973                         FreeType(sourceExp.expType);
3974
3975                         sourceExp.isConstant = true;
3976                         sourceExp.expType = MkClassType(_class.fullName);
3977                         //if(inCompiler)
3978                         {
3979                            char constant[256];
3980                            sourceExp.type = constantExp;
3981                            if(/*_class && */_class.dataTypeString && !strcmp(_class.dataTypeString, "int")) // _class cannot be null here!
3982                               sprintf(constant, "%d", (int) value.data);
3983                            else
3984                               sprintf(constant, "0x%X", (int) value.data);
3985                            sourceExp.constant = CopyString(constant);
3986                            //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
3987                         }
3988                         return true;
3989                      }
3990                   }
3991                }
3992             }
3993          }
3994
3995          // Loop through all enum classes
3996          if(dest.classObjectType != typedObject && dest.kind == classType /*!= ellipsisType */&& MatchWithEnums_Module(privateModule, sourceExp, dest, id.string, conversions))
3997             return true;
3998       }
3999    }
4000    return false;
4001 }
4002
4003 #define TERTIARY(o, name, m, t, p) \
4004    static bool name(Expression exp, Operand op1, Operand op2, Operand op3)   \
4005    {                                                              \
4006       exp.type = constantExp;                                    \
4007       exp.string = p(op1.m ? op2.m : op3.m);                     \
4008       if(!exp.expType) \
4009          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4010       return true;                                                \
4011    }
4012
4013 #define BINARY(o, name, m, t, p) \
4014    static bool name(Expression exp, Operand op1, Operand op2)   \
4015    {                                                              \
4016       t value2 = op2.m;                                           \
4017       exp.type = constantExp;                                    \
4018       exp.string = p(op1.m o value2);                     \
4019       if(!exp.expType) \
4020          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4021       return true;                                                \
4022    }
4023
4024 #define BINARY_DIVIDEINT(o, name, m, t, p) \
4025    static bool name(Expression exp, Operand op1, Operand op2)   \
4026    {                                                              \
4027       t value2 = op2.m;                                           \
4028       exp.type = constantExp;                                    \
4029       exp.string = p(value2 ? (op1.m o value2) : 0);             \
4030       if(!exp.expType) \
4031          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4032       return true;                                                \
4033    }
4034
4035 #define BINARY_DIVIDEREAL(o, name, m, t, p) \
4036    static bool name(Expression exp, Operand op1, Operand op2)   \
4037    {                                                              \
4038       t value2 = op2.m;                                           \
4039       exp.type = constantExp;                                    \
4040       exp.string = p(op1.m o value2);             \
4041       if(!exp.expType) \
4042          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4043       return true;                                                \
4044    }
4045
4046 #define UNARY(o, name, m, t, p) \
4047    static bool name(Expression exp, Operand op1)                \
4048    {                                                              \
4049       exp.type = constantExp;                                    \
4050       exp.string = p((t)(o op1.m));                                   \
4051       if(!exp.expType) \
4052          { exp.expType = op1.type; if(op1.type) op1.type.refCount++; } \
4053       return true;                                                \
4054    }
4055
4056 #define OPERATOR_ALL(macro, o, name) \
4057    macro(o, Int##name, i, int, PrintInt) \
4058    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4059    macro(o, Int64##name, i64, int64, PrintInt64) \
4060    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4061    macro(o, Short##name, s, short, PrintShort) \
4062    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4063    macro(o, Char##name, c, char, PrintChar) \
4064    macro(o, UChar##name, uc, unsigned char, PrintUChar) \
4065    macro(o, Float##name, f, float, PrintFloat) \
4066    macro(o, Double##name, d, double, PrintDouble)
4067
4068 #define OPERATOR_INTTYPES(macro, o, name) \
4069    macro(o, Int##name, i, int, PrintInt) \
4070    macro(o, UInt##name, ui, unsigned int, PrintUInt) \
4071    macro(o, Int64##name, i64, int64, PrintInt64) \
4072    macro(o, UInt64##name, ui64, uint64, PrintUInt64) \
4073    macro(o, Short##name, s, short, PrintShort) \
4074    macro(o, UShort##name, us, unsigned short, PrintUShort) \
4075    macro(o, Char##name, c, char, PrintChar) \
4076    macro(o, UChar##name, uc, unsigned char, PrintUChar)
4077
4078 #define OPERATOR_REALTYPES(macro, o, name) \
4079    macro(o, Float##name, f, float, PrintFloat) \
4080    macro(o, Double##name, d, double, PrintDouble)
4081
4082 // binary arithmetic
4083 OPERATOR_ALL(BINARY, +, Add)
4084 OPERATOR_ALL(BINARY, -, Sub)
4085 OPERATOR_ALL(BINARY, *, Mul)
4086 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /, Div)
4087 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /, Div)
4088 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %, Mod)
4089
4090 // unary arithmetic
4091 OPERATOR_ALL(UNARY, -, Neg)
4092
4093 // unary arithmetic increment and decrement
4094 OPERATOR_ALL(UNARY, ++, Inc)
4095 OPERATOR_ALL(UNARY, --, Dec)
4096
4097 // binary arithmetic assignment
4098 OPERATOR_ALL(BINARY, =, Asign)
4099 OPERATOR_ALL(BINARY, +=, AddAsign)
4100 OPERATOR_ALL(BINARY, -=, SubAsign)
4101 OPERATOR_ALL(BINARY, *=, MulAsign)
4102 OPERATOR_INTTYPES(BINARY_DIVIDEINT, /=, DivAsign)
4103 OPERATOR_REALTYPES(BINARY_DIVIDEREAL, /=, DivAsign)
4104 OPERATOR_INTTYPES(BINARY_DIVIDEINT, %=, ModAsign)
4105
4106 // binary bitwise
4107 OPERATOR_INTTYPES(BINARY, &, BitAnd)
4108 OPERATOR_INTTYPES(BINARY, |, BitOr)
4109 OPERATOR_INTTYPES(BINARY, ^, BitXor)
4110 OPERATOR_INTTYPES(BINARY, <<, LShift)
4111 OPERATOR_INTTYPES(BINARY, >>, RShift)
4112
4113 // unary bitwise
4114 OPERATOR_INTTYPES(UNARY, ~, BitNot)
4115
4116 // binary bitwise assignment
4117 OPERATOR_INTTYPES(BINARY, &=, AndAsign)
4118 OPERATOR_INTTYPES(BINARY, |=, OrAsign)
4119 OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
4120 OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
4121 OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
4122
4123 // unary logical negation
4124 OPERATOR_INTTYPES(UNARY, !, Not)
4125
4126 // binary logical equality
4127 OPERATOR_ALL(BINARY, ==, Equ)
4128 OPERATOR_ALL(BINARY, !=, Nqu)
4129
4130 // binary logical
4131 OPERATOR_ALL(BINARY, &&, And)
4132 OPERATOR_ALL(BINARY, ||, Or)
4133
4134 // binary logical relational
4135 OPERATOR_ALL(BINARY, >, Grt)
4136 OPERATOR_ALL(BINARY, <, Sma)
4137 OPERATOR_ALL(BINARY, >=, GrtEqu)
4138 OPERATOR_ALL(BINARY, <=, SmaEqu)
4139
4140 // tertiary condition operator
4141 OPERATOR_ALL(TERTIARY, ?, Cond)
4142
4143 //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
4144 #define OPERATOR_TABLE_ALL(name, type) \
4145     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, type##Mod, \
4146                           type##Neg, \
4147                           type##Inc, type##Dec, \
4148                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, type##ModAsign, \
4149                           type##BitAnd, type##BitOr, type##BitXor, type##LShift, type##RShift, \
4150                           type##BitNot, \
4151                           type##AndAsign, type##OrAsign, type##XorAsign, type##LShiftAsign, type##RShiftAsign, \
4152                           type##Not, \
4153                           type##Equ, type##Nqu, \
4154                           type##And, type##Or, \
4155                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu, type##Cond \
4156                         }; \
4157
4158 #define OPERATOR_TABLE_INTTYPES(name, type) \
4159     OpTable name##Ops = { type##Add, type##Sub, type##Mul, type##Div, null, \
4160                           type##Neg, \
4161                           type##Inc, type##Dec, \
4162                           type##Asign, type##AddAsign, type##SubAsign, type##MulAsign, type##DivAsign, null, \
4163                           null, null, null, null, null, \
4164                           null, \
4165                           null, null, null, null, null, \
4166                           null, \
4167                           type##Equ, type##Nqu, \
4168                           type##And, type##Or, \
4169                           type##Grt, type##Sma, type##GrtEqu, type##SmaEqu \
4170                         }; \
4171
4172 OPERATOR_TABLE_ALL(int, Int)
4173 OPERATOR_TABLE_ALL(uint, UInt)
4174 OPERATOR_TABLE_ALL(int64, Int64)
4175 OPERATOR_TABLE_ALL(uint64, UInt64)
4176 OPERATOR_TABLE_ALL(short, Short)
4177 OPERATOR_TABLE_ALL(ushort, UShort)
4178 OPERATOR_TABLE_INTTYPES(float, Float)
4179 OPERATOR_TABLE_INTTYPES(double, Double)
4180 OPERATOR_TABLE_ALL(char, Char)
4181 OPERATOR_TABLE_ALL(uchar, UChar)
4182
4183 //OpTable intOps =    {    IntAdd,    IntSub,    IntMul,    IntDiv,    IntMod,    IntExp,    IntNot,    IntBwn,    IntOr,    IntAnd,    IntEqu,    IntNqu,    IntGrt,    IntSma,    IntGrtEqu,    IntSmaEqu,    IntNeg,    IntLBitSft,    IntRBitSft };
4184 //OpTable uintOps =   {   UIntAdd,   UIntSub,   UIntMul,   UIntDiv,   UIntMod,   UIntExp,   UIntNot,   UIntBwn,   UIntOr,   UIntAnd,   UIntEqu,   UIntNqu,   UIntGrt,   UIntSma,   UIntGrtEqu,   UIntSmaEqu,   UIntNeg,   UIntLBitSft,   UIntRBitSft };
4185 //OpTable shortOps =  {  ShortAdd,  ShortSub,  ShortMul,  ShortDiv,  ShortMod,  ShortExp,  ShortNot,  ShortBwn,  ShortOr,  ShortAnd,  ShortEqu,  ShortNqu,  ShortGrt,  ShortSma,  ShortGrtEqu,  ShortSmaEqu,  ShortNeg,  ShortLBitSft,  ShortRBitSft };
4186 //OpTable ushortOps = { UShortAdd, UShortSub, UShortMul, UShortDiv, UShortMod, UShortExp, UShortNot, UShortBwn, UShortOr, UShortAnd, UShortEqu, UShortNqu, UShortGrt, UShortSma, UShortGrtEqu, UShortSmaEqu, UShortNeg, UShortLBitSft, UShortRBitSft };
4187 //OpTable floatOps =  {  FloatAdd,  FloatSub,  FloatMul,  FloatDiv,      null,      null,      null,      null,     null,      null,  FloatEqu,  FloatNqu,  FloatGrt,  FloatSma,  FloatGrtEqu,  FloatSmaEqu,  FloatNeg,          null,          null };
4188 //OpTable doubleOps = { DoubleAdd, DoubleSub, DoubleMul, DoubleDiv,      null,      null,      null,      null,     null,      null, DoubleEqu, DoubleNqu, DoubleGrt, DoubleSma, DoubleGrtEqu, DoubleSmaEqu, DoubleNeg,          null,          null };
4189 //OpTable charOps =   {   CharAdd,   CharSub,   CharMul,   CharDiv,   CharMod,   CharExp,   CharNot,   CharBwn,   CharOr,   CharAnd,   CharEqu,   CharNqu,   CharGrt,   CharSma,   CharGrtEqu,   CharSmaEqu,   CharNeg,   CharLBitSft,   CharRBitSft };
4190 //OpTable ucharOps =  {  UCharAdd,  UCharSub,  UCharMul,  UCharDiv,  UCharMod,  UCharExp,  UCharNot,  UCharBwn,  UCharOr,  UCharAnd,  UCharEqu,  UCharNqu,  UCharGrt,  UCharSma,  UCharGrtEqu,  UCharSmaEqu,  UCharNeg,  UCharLBitSft,  UCharRBitSft };
4191
4192 public void ReadString(char * output,  char * string)
4193 {
4194    int len = strlen(string);
4195    int c,d = 0;
4196    bool quoted = false, escaped = false;
4197    for(c = 0; c<len; c++)
4198    {
4199       char ch = string[c];
4200       if(escaped)
4201       {
4202          switch(ch)
4203          {
4204             case 'n': output[d] = '\n'; break;
4205             case 't': output[d] = '\t'; break;
4206             case 'a': output[d] = '\a'; break;
4207             case 'b': output[d] = '\b'; break;
4208             case 'f': output[d] = '\f'; break;
4209             case 'r': output[d] = '\r'; break;
4210             case 'v': output[d] = '\v'; break;
4211             case '\\': output[d] = '\\'; break;
4212             case '\"': output[d] = '\"'; break;
4213             case '\'': output[d] = '\''; break;
4214             default: output[d] = ch;
4215          }
4216          d++;
4217          escaped = false;
4218       }
4219       else
4220       {
4221          if(ch == '\"')
4222             quoted ^= true;
4223          else if(quoted)
4224          {
4225             if(ch == '\\')
4226                escaped = true;
4227             else
4228                output[d++] = ch;
4229          }
4230       }
4231    }
4232    output[d] = '\0';
4233 }
4234
4235 // String Unescape Copy
4236
4237 // TOFIX: THIS DOESN'T HANDLE NUMERIC ESCAPE CODES (OCTAL/HEXADECIMAL...)?
4238 // This is the same as ReadString above (which also misses numeric escape codes) except it doesn't handle external quotes
4239 public int UnescapeString(char * d, char * s, int len)
4240 {
4241    int j = 0, k = 0;
4242    char ch;
4243    while(j < len && (ch = s[j]))
4244    {
4245       switch(ch)
4246       {
4247          case '\\':
4248             switch((ch = s[++j]))
4249             {
4250                case 'n': d[k] = '\n'; break;
4251                case 't': d[k] = '\t'; break;
4252                case 'a': d[k] = '\a'; break;
4253                case 'b': d[k] = '\b'; break;
4254                case 'f': d[k] = '\f'; break;
4255                case 'r': d[k] = '\r'; break;
4256                case 'v': d[k] = '\v'; break;
4257                case '\\': d[k] = '\\'; break;
4258                case '\"': d[k] = '\"'; break;
4259                case '\'': d[k] = '\''; break;
4260                default: d[k] = '\\'; d[k] = ch;
4261             }
4262             break;
4263          default:
4264             d[k] = ch;
4265       }
4266       j++, k++;
4267    }
4268    d[k] = '\0';
4269    return k;
4270 }
4271
4272 public char * OffsetEscapedString(char * s, int len, int offset)
4273 {
4274    char ch;
4275    int j = 0, k = 0;
4276    while(j < len && k < offset && (ch = s[j]))
4277    {
4278       if(ch == '\\') ++j;
4279       j++, k++;
4280    }
4281    return (k == offset) ? s + j : null;
4282 }
4283
4284 public Operand GetOperand(Expression exp)
4285 {
4286    Operand op { };
4287    Type type = exp.expType;
4288    if(type)
4289    {
4290       while(type.kind == classType &&
4291          type._class.registered && (type._class.registered.type == bitClass || type._class.registered.type == unitClass || type._class.registered.type == enumClass))
4292       {
4293          if(!type._class.registered.dataType)
4294             type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
4295          type = type._class.registered.dataType;
4296
4297       }
4298       if(exp.type == stringExp && op.kind == pointerType)
4299       {
4300          op.ui64 = (uint64)exp.string;
4301          op.kind = pointerType;
4302          op.ops = uint64Ops;
4303       }
4304       else if(exp.isConstant && exp.type == constantExp)
4305       {
4306          op.kind = type.kind;
4307          op.type = exp.expType;
4308
4309          switch(op.kind)
4310          {
4311             case _BoolType:
4312             case charType:
4313             {
4314                if(exp.constant[0] == '\'')
4315                {
4316                   op.c = exp.constant[1];
4317                   op.ops = charOps;
4318                }
4319                else if(type.isSigned)
4320                {
4321                   op.c = (char)strtol(exp.constant, null, 0);
4322                   op.ops = charOps;
4323                }
4324                else
4325                {
4326                   op.uc = (unsigned char)strtoul(exp.constant, null, 0);
4327                   op.ops = ucharOps;
4328                }
4329                break;
4330             }
4331             case shortType:
4332                if(type.isSigned)
4333                {
4334                   op.s = (short)strtol(exp.constant, null, 0);
4335                   op.ops = shortOps;
4336                }
4337                else
4338                {
4339                   op.us = (unsigned short)strtoul(exp.constant, null, 0);
4340                   op.ops = ushortOps;
4341                }
4342                break;
4343             case intType:
4344             case longType:
4345                if(type.isSigned)
4346                {
4347                   op.i = (int)strtol(exp.constant, null, 0);
4348                   op.ops = intOps;
4349                }
4350                else
4351                {
4352                   op.ui = (unsigned int)strtoul(exp.constant, null, 0);
4353                   op.ops = uintOps;
4354                }
4355                op.kind = intType;
4356                break;
4357             case int64Type:
4358                if(type.isSigned)
4359                {
4360                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4361                   op.ops = int64Ops;
4362                }
4363                else
4364                {
4365                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4366                   op.ops = uint64Ops;
4367                }
4368                op.kind = int64Type;
4369                break;
4370             case intPtrType:
4371                if(type.isSigned)
4372                {
4373                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4374                   op.ops = int64Ops;
4375                }
4376                else
4377                {
4378                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4379                   op.ops = uint64Ops;
4380                }
4381                op.kind = int64Type;
4382                break;
4383             case intSizeType:
4384                if(type.isSigned)
4385                {
4386                   op.i64 = (int64)_strtoi64(exp.constant, null, 0);
4387                   op.ops = int64Ops;
4388                }
4389                else
4390                {
4391                   op.ui64 = (uint64)_strtoui64(exp.constant, null, 0);
4392                   op.ops = uint64Ops;
4393                }
4394                op.kind = int64Type;
4395                break;
4396             case floatType:
4397                if(!strcmp(exp.constant, "inf")) op.f = float::inf();
4398                else if(!strcmp(exp.constant, "-inf")) op.f = -float::inf();
4399                else if(!strcmp(exp.constant, "nan")) op.f = float::nan();
4400                else if(!strcmp(exp.constant, "-nan")) op.f = -float::nan();
4401                else
4402                   op.f = (float)strtod(exp.constant, null);
4403                op.ops = floatOps;
4404                break;
4405             case doubleType:
4406                if(!strcmp(exp.constant, "inf")) op.d = double::inf();
4407                else if(!strcmp(exp.constant, "-inf")) op.d = -double::inf();
4408                else if(!strcmp(exp.constant, "nan")) op.d = double::nan();
4409                else if(!strcmp(exp.constant, "-nan")) op.d = -double::nan();
4410                else
4411                   op.d = (double)strtod(exp.constant, null);
4412                op.ops = doubleOps;
4413                break;
4414             //case classType:    For when we have operator overloading...
4415             // Pointer additions
4416             //case functionType:
4417             case arrayType:
4418             case pointerType:
4419             case classType:
4420                op.ui64 = _strtoui64(exp.constant, null, 0);
4421                op.kind = pointerType;
4422                op.ops = uint64Ops;
4423                // op.ptrSize =
4424                break;
4425          }
4426       }
4427    }
4428    return op;
4429 }
4430
4431 static void UnusedFunction()
4432 {
4433    int a;
4434    a.OnGetString(0,0,0);
4435 }
4436 default:
4437 extern int __ecereVMethodID_class_OnGetString;
4438 public:
4439
4440 static void PopulateInstanceProcessMember(Instantiation inst, OldList * memberList, DataMember parentDataMember, uint offset)
4441 {
4442    DataMember dataMember;
4443    for(dataMember = parentDataMember.members.first; dataMember; dataMember = dataMember.next)
4444    {
4445       if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4446          PopulateInstanceProcessMember(inst, memberList, dataMember, offset + dataMember.offset);
4447       else
4448       {
4449          Expression exp { };
4450          MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4451          Type type;
4452          void * ptr = inst.data + dataMember.offset + offset;
4453          char * result = null;
4454          exp.loc = member.loc = inst.loc;
4455          ((Identifier)member.identifiers->first).loc = inst.loc;
4456
4457          if(!dataMember.dataType)
4458             dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4459          type = dataMember.dataType;
4460          if(type.kind == classType)
4461          {
4462             Class _class = type._class.registered;
4463             if(_class.type == enumClass)
4464             {
4465                Class enumClass = eSystem_FindClass(privateModule, "enum");
4466                if(enumClass)
4467                {
4468                   EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4469                   NamedLink item;
4470                   for(item = e.values.first; item; item = item.next)
4471                   {
4472                      if((int)item.data == *(int *)ptr)
4473                      {
4474                         result = item.name;
4475                         break;
4476                      }
4477                   }
4478                   if(result)
4479                   {
4480                      exp.identifier = MkIdentifier(result);
4481                      exp.type = identifierExp;
4482                      exp.destType = MkClassType(_class.fullName);
4483                      ProcessExpressionType(exp);
4484                   }
4485                }
4486             }
4487             if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4488             {
4489                if(!_class.dataType)
4490                   _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4491                type = _class.dataType;
4492             }
4493          }
4494          if(!result)
4495          {
4496             switch(type.kind)
4497             {
4498                case floatType:
4499                {
4500                   FreeExpContents(exp);
4501
4502                   exp.constant = PrintFloat(*(float*)ptr);
4503                   exp.type = constantExp;
4504                   break;
4505                }
4506                case doubleType:
4507                {
4508                   FreeExpContents(exp);
4509
4510                   exp.constant = PrintDouble(*(double*)ptr);
4511                   exp.type = constantExp;
4512                   break;
4513                }
4514                case intType:
4515                {
4516                   FreeExpContents(exp);
4517
4518                   exp.constant = PrintInt(*(int*)ptr);
4519                   exp.type = constantExp;
4520                   break;
4521                }
4522                case int64Type:
4523                {
4524                   FreeExpContents(exp);
4525
4526                   exp.constant = PrintInt64(*(int64*)ptr);
4527                   exp.type = constantExp;
4528                   break;
4529                }
4530                case intPtrType:
4531                {
4532                   FreeExpContents(exp);
4533                   // TODO: This should probably use proper type
4534                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4535                   exp.type = constantExp;
4536                   break;
4537                }
4538                case intSizeType:
4539                {
4540                   FreeExpContents(exp);
4541                   // TODO: This should probably use proper type
4542                   exp.constant = PrintInt64((int64)*(intptr*)ptr);
4543                   exp.type = constantExp;
4544                   break;
4545                }
4546                default:
4547                   Compiler_Error($"Unhandled type populating instance\n");
4548             }
4549          }
4550          ListAdd(memberList, member);
4551       }
4552
4553       if(parentDataMember.type == unionMember)
4554          break;
4555    }
4556 }
4557
4558 void PopulateInstance(Instantiation inst)
4559 {
4560    Symbol classSym = inst._class.symbol; // FindClass(inst._class.name);
4561    Class _class = classSym.registered;
4562    DataMember dataMember;
4563    OldList * memberList = MkList();
4564    // Added this check and ->Add to prevent memory leaks on bad code
4565    if(!inst.members)
4566       inst.members = MkListOne(MkMembersInitList(memberList));
4567    else
4568       inst.members->Add(MkMembersInitList(memberList));
4569    for(dataMember = _class.membersAndProperties.first; dataMember; dataMember = dataMember.next)
4570    {
4571       if(!dataMember.isProperty)
4572       {
4573          if(!dataMember.name && (dataMember.type == structMember || dataMember.type == unionMember))
4574             PopulateInstanceProcessMember(inst, memberList, dataMember, dataMember.offset);
4575          else
4576          {
4577             Expression exp { };
4578             MemberInit member = MkMemberInit(MkListOne(MkIdentifier(dataMember.name)), MkInitializerAssignment(exp));
4579             Type type;
4580             void * ptr = inst.data + dataMember.offset;
4581             char * result = null;
4582
4583             exp.loc = member.loc = inst.loc;
4584             ((Identifier)member.identifiers->first).loc = inst.loc;
4585
4586             if(!dataMember.dataType)
4587                dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4588             type = dataMember.dataType;
4589             if(type.kind == classType)
4590             {
4591                Class _class = type._class.registered;
4592                if(_class.type == enumClass)
4593                {
4594                   Class enumClass = eSystem_FindClass(privateModule, "enum");
4595                   if(enumClass)
4596                   {
4597                      EnumClassData e = ACCESS_CLASSDATA(_class, enumClass);
4598                      NamedLink item;
4599                      for(item = e.values.first; item; item = item.next)
4600                      {
4601                         if((int)item.data == *(int *)ptr)
4602                         {
4603                            result = item.name;
4604                            break;
4605                         }
4606                      }
4607                   }
4608                   if(result)
4609                   {
4610                      exp.identifier = MkIdentifier(result);
4611                      exp.type = identifierExp;
4612                      exp.destType = MkClassType(_class.fullName);
4613                      ProcessExpressionType(exp);
4614                   }
4615                }
4616                if(_class.type == enumClass || _class.type == unitClass || _class.type == bitClass)
4617                {
4618                   if(!_class.dataType)
4619                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4620                   type = _class.dataType;
4621                }
4622             }
4623             if(!result)
4624             {
4625                switch(type.kind)
4626                {
4627                   case floatType:
4628                   {
4629                      exp.constant = PrintFloat(*(float*)ptr);
4630                      exp.type = constantExp;
4631                      break;
4632                   }
4633                   case doubleType:
4634                   {
4635                      exp.constant = PrintDouble(*(double*)ptr);
4636                      exp.type = constantExp;
4637                      break;
4638                   }
4639                   case intType:
4640                   {
4641                      exp.constant = PrintInt(*(int*)ptr);
4642                      exp.type = constantExp;
4643                      break;
4644                   }
4645                   case int64Type:
4646                   {
4647                      exp.constant = PrintInt64(*(int64*)ptr);
4648                      exp.type = constantExp;
4649                      break;
4650                   }
4651                   case intPtrType:
4652                   {
4653                      exp.constant = PrintInt64((int64)*(intptr*)ptr);
4654                      exp.type = constantExp;
4655                      break;
4656                   }
4657                   default:
4658                      Compiler_Error($"Unhandled type populating instance\n");
4659                }
4660             }
4661             ListAdd(memberList, member);
4662          }
4663       }
4664    }
4665 }
4666
4667 void ComputeInstantiation(Expression exp)
4668 {
4669    Instantiation inst = exp.instance;
4670    MembersInit members;
4671    Symbol classSym = inst._class ? inst._class.symbol : null; // FindClass(inst._class.name);
4672    Class _class = classSym ? classSym.registered : null;
4673    DataMember curMember = null;
4674    Class curClass = null;
4675    DataMember subMemberStack[256];
4676    int subMemberStackPos = 0;
4677    uint64 bits = 0;
4678
4679    if(_class && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass ))
4680    {
4681       // Don't recompute the instantiation...
4682       // Non Simple classes will have become constants by now
4683       if(inst.data)
4684          return;
4685
4686       if(_class.type == normalClass || _class.type == noHeadClass)
4687       {
4688          inst.data = (byte *)eInstance_New(_class);
4689          if(_class.type == normalClass)
4690             ((Instance)inst.data)._refCount++;
4691       }
4692       else
4693          inst.data = new0 byte[_class.structSize];
4694    }
4695
4696    if(inst.members)
4697    {
4698       for(members = inst.members->first; members; members = members.next)
4699       {
4700          switch(members.type)
4701          {
4702             case dataMembersInit:
4703             {
4704                if(members.dataMembers)
4705                {
4706                   MemberInit member;
4707                   for(member = members.dataMembers->first; member; member = member.next)
4708                   {
4709                      Identifier ident = member.identifiers ? member.identifiers->first : null;
4710                      bool found = false;
4711
4712                      Property prop = null;
4713                      DataMember dataMember = null;
4714                      Method method = null;
4715                      uint dataMemberOffset;
4716
4717                      if(!ident)
4718                      {
4719                         eClass_FindNextMember(_class, &curClass, &curMember, subMemberStack, &subMemberStackPos);
4720                         if(curMember)
4721                         {
4722                            if(curMember.isProperty)
4723                               prop = (Property)curMember;
4724                            else
4725                            {
4726                               dataMember = curMember;
4727
4728                               // CHANGED THIS HERE
4729                               eClass_FindDataMemberAndOffset(_class, dataMember.name, &dataMemberOffset, privateModule, null, null);
4730
4731                               // 2013/17/29 -- It seems that this was missing here!
4732                               if(_class.type == normalClass)
4733                                  dataMemberOffset += _class.base.structSize;
4734                               // dataMemberOffset = dataMember.offset;
4735                            }
4736                            found = true;
4737                         }
4738                      }
4739                      else
4740                      {
4741                         prop = eClass_FindProperty(_class, ident.string, privateModule);
4742                         if(prop)
4743                         {
4744                            found = true;
4745                            if(prop.memberAccess == publicAccess)
4746                            {
4747                               curMember = (DataMember)prop;
4748                               curClass = prop._class;
4749                            }
4750                         }
4751                         else
4752                         {
4753                            DataMember _subMemberStack[256];
4754                            int _subMemberStackPos = 0;
4755
4756                            // FILL MEMBER STACK
4757                            dataMember = eClass_FindDataMemberAndOffset(_class, ident.string, &dataMemberOffset, privateModule, _subMemberStack, &_subMemberStackPos);
4758
4759                            if(dataMember)
4760                            {
4761                               found = true;
4762                               if(dataMember.memberAccess == publicAccess)
4763                               {
4764                                  curMember = dataMember;
4765                                  curClass = dataMember._class;
4766                                  memcpy(subMemberStack, _subMemberStack, sizeof(DataMember) * _subMemberStackPos);
4767                                  subMemberStackPos = _subMemberStackPos;
4768                               }
4769                            }
4770                         }
4771                      }
4772
4773                      if(found && member.initializer && member.initializer.type == expInitializer)
4774                      {
4775                         Expression value = member.initializer.exp;
4776                         Type type = null;
4777                         bool deepMember = false;
4778                         if(prop)
4779                         {
4780                            type = prop.dataType;
4781                         }
4782                         else if(dataMember)
4783                         {
4784                            if(!dataMember.dataType)
4785                               dataMember.dataType = ProcessTypeString(dataMember.dataTypeString, false);
4786
4787                            type = dataMember.dataType;
4788                         }
4789
4790                         if(ident && ident.next)
4791                         {
4792                            deepMember = true;
4793
4794                            // for(; ident && type; ident = ident.next)
4795                            for(ident = ident.next; ident && type; ident = ident.next)
4796                            {
4797                               if(type.kind == classType)
4798                               {
4799                                  prop = eClass_FindProperty(type._class.registered,
4800                                     ident.string, privateModule);
4801                                  if(prop)
4802                                     type = prop.dataType;
4803                                  else
4804                                  {
4805                                     dataMember = eClass_FindDataMemberAndOffset(type._class.registered,
4806                                        ident.string, &dataMemberOffset, privateModule, null, null);
4807                                     if(dataMember)
4808                                        type = dataMember.dataType;
4809                                  }
4810                               }
4811                               else if(type.kind == structType || type.kind == unionType)
4812                               {
4813                                  Type memberType;
4814                                  for(memberType = type.members.first; memberType; memberType = memberType.next)
4815                                  {
4816                                     if(!strcmp(memberType.name, ident.string))
4817                                     {
4818                                        type = memberType;
4819                                        break;
4820                                     }
4821                                  }
4822                               }
4823                            }
4824                         }
4825                         if(value)
4826                         {
4827                            FreeType(value.destType);
4828                            value.destType = type;
4829                            if(type) type.refCount++;
4830                            ComputeExpression(value);
4831                         }
4832                         if(!deepMember && type && value && (_class.type == structClass || _class.type == normalClass || _class.type == noHeadClass /*&& value.expType.kind == type.kind*/))
4833                         {
4834                            if(type.kind == classType)
4835                            {
4836                               Class _class = type._class.registered;
4837                               if(_class.type == bitClass || _class.type == unitClass ||
4838                                  _class.type == enumClass)
4839                               {
4840                                  if(!_class.dataType)
4841                                     _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4842                                  type = _class.dataType;
4843                               }
4844                            }
4845
4846                            if(dataMember)
4847                            {
4848                               void * ptr = inst.data + dataMemberOffset;
4849
4850                               if(value.type == constantExp)
4851                               {
4852                                  switch(type.kind)
4853                                  {
4854                                     case intType:
4855                                     {
4856                                        GetInt(value, (int*)ptr);
4857                                        break;
4858                                     }
4859                                     case int64Type:
4860                                     {
4861                                        GetInt64(value, (int64*)ptr);
4862                                        break;
4863                                     }
4864                                     case intPtrType:
4865                                     {
4866                                        GetIntPtr(value, (intptr*)ptr);
4867                                        break;
4868                                     }
4869                                     case intSizeType:
4870                                     {
4871                                        GetIntSize(value, (intsize*)ptr);
4872                                        break;
4873                                     }
4874                                     case floatType:
4875                                     {
4876                                        GetFloat(value, (float*)ptr);
4877                                        break;
4878                                     }
4879                                     case doubleType:
4880                                     {
4881                                        GetDouble(value, (double *)ptr);
4882                                        break;
4883                                     }
4884                                  }
4885                               }
4886                               else if(value.type == instanceExp)
4887                               {
4888                                  if(type.kind == classType)
4889                                  {
4890                                     Class _class = type._class.registered;
4891                                     if(_class.type == structClass)
4892                                     {
4893                                        ComputeTypeSize(type);
4894                                        if(value.instance.data)
4895                                           memcpy(ptr, value.instance.data, type.size);
4896                                     }
4897                                  }
4898                               }
4899                            }
4900                            else if(prop)
4901                            {
4902                               if(value.type == instanceExp && value.instance.data)
4903                               {
4904                                  if(type.kind == classType)
4905                                  {
4906                                     Class _class = type._class.registered;
4907                                     if(_class && (_class.type != normalClass || eClass_IsDerived(((Instance)value.instance.data)._class, _class)))
4908                                     {
4909                                        void (*Set)(void *, void *) = (void *)prop.Set;
4910                                        Set(inst.data, value.instance.data);
4911                                        PopulateInstance(inst);
4912                                     }
4913                                  }
4914                               }
4915                               else if(value.type == constantExp)
4916                               {
4917                                  switch(type.kind)
4918                                  {
4919                                     case doubleType:
4920                                     {
4921                                        void (*Set)(void *, double) = (void *)prop.Set;
4922                                        Set(inst.data, strtod(value.constant, null) );
4923                                        break;
4924                                     }
4925                                     case floatType:
4926                                     {
4927                                        void (*Set)(void *, float) = (void *)prop.Set;
4928                                        Set(inst.data, (float)(strtod(value.constant, null)));
4929                                        break;
4930                                     }
4931                                     case intType:
4932                                     {
4933                                        void (*Set)(void *, int) = (void *)prop.Set;
4934                                        Set(inst.data, (int)strtol(value.constant, null, 0));
4935                                        break;
4936                                     }
4937                                     case int64Type:
4938                                     {
4939                                        void (*Set)(void *, int64) = (void *)prop.Set;
4940                                        Set(inst.data, _strtoi64(value.constant, null, 0));
4941                                        break;
4942                                     }
4943                                     case intPtrType:
4944                                     {
4945                                        void (*Set)(void *, intptr) = (void *)prop.Set;
4946                                        Set(inst.data, (intptr)_strtoi64(value.constant, null, 0));
4947                                        break;
4948                                     }
4949                                     case intSizeType:
4950                                     {
4951                                        void (*Set)(void *, intsize) = (void *)prop.Set;
4952                                        Set(inst.data, (intsize)_strtoi64(value.constant, null, 0));
4953                                        break;
4954                                     }
4955                                  }
4956                               }
4957                               else if(value.type == stringExp)
4958                               {
4959                                  char temp[1024];
4960                                  ReadString(temp, value.string);
4961                                  ((void (*)(void *, void *))(void *)prop.Set)(inst.data, temp);
4962                               }
4963                            }
4964                         }
4965                         else if(!deepMember && type && _class.type == unitClass)
4966                         {
4967                            if(prop)
4968                            {
4969                               // Only support converting units to units for now...
4970                               if(value.type == constantExp)
4971                               {
4972                                  if(type.kind == classType)
4973                                  {
4974                                     Class _class = type._class.registered;
4975                                     if(_class.type == unitClass)
4976                                     {
4977                                        if(!_class.dataType)
4978                                           _class.dataType = ProcessTypeString(_class.dataTypeString, false);
4979                                        type = _class.dataType;
4980                                     }
4981                                  }
4982                                  // TODO: Assuming same base type for units...
4983                                  switch(type.kind)
4984                                  {
4985                                     case floatType:
4986                                     {
4987                                        float fValue;
4988                                        float (*Set)(float) = (void *)prop.Set;
4989                                        GetFloat(member.initializer.exp, &fValue);
4990                                        exp.constant = PrintFloat(Set(fValue));
4991                                        exp.type = constantExp;
4992                                        break;
4993                                     }
4994                                     case doubleType:
4995                                     {
4996                                        double dValue;
4997                                        double (*Set)(double) = (void *)prop.Set;
4998                                        GetDouble(member.initializer.exp, &dValue);
4999                                        exp.constant = PrintDouble(Set(dValue));
5000                                        exp.type = constantExp;
5001                                        break;
5002                                     }
5003                                  }
5004                               }
5005                            }
5006                         }
5007                         else if(!deepMember && type && _class.type == bitClass)
5008                         {
5009                            if(prop)
5010                            {
5011                               if(value.type == instanceExp && value.instance.data)
5012                               {
5013                                  unsigned int (*Set)(void *) = (void *)prop.Set;
5014                                  bits = Set(value.instance.data);
5015                               }
5016                               else if(value.type == constantExp)
5017                               {
5018                               }
5019                            }
5020                            else if(dataMember)
5021                            {
5022                               BitMember bitMember = (BitMember) dataMember;
5023                               Type type;
5024                               int part = 0;
5025                               GetInt(value, &part);
5026                               bits = (bits & ~bitMember.mask);
5027                               if(!bitMember.dataType)
5028                                  bitMember.dataType = ProcessTypeString(bitMember.dataTypeString, false);
5029
5030                               type = bitMember.dataType;
5031
5032                               if(type.kind == classType && type._class && type._class.registered)
5033                               {
5034                                  if(!type._class.registered.dataType)
5035                                     type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
5036                                  type = type._class.registered.dataType;
5037                               }
5038
5039                               switch(type.kind)
5040                               {
5041                                  case _BoolType:
5042                                  case charType:
5043                                     if(type.isSigned)
5044                                        bits |= ((char)part << bitMember.pos);
5045                                     else
5046                                        bits |= ((unsigned char)part << bitMember.pos);
5047                                     break;
5048                                  case shortType:
5049                                     if(type.isSigned)
5050                                        bits |= ((short)part << bitMember.pos);
5051                                     else
5052                                        bits |= ((unsigned short)part << bitMember.pos);
5053                                     break;
5054                                  case intType:
5055                                  case longType:
5056                                     if(type.isSigned)
5057                                        bits |= ((int)part << bitMember.pos);
5058                                     else
5059                                        bits |= ((unsigned int)part << bitMember.pos);
5060                                     break;
5061                                  case int64Type:
5062                                     if(type.isSigned)
5063                                        bits |= ((int64)part << bitMember.pos);
5064                                     else
5065                                        bits |= ((uint64)part << bitMember.pos);
5066                                     break;
5067                                  case intPtrType:
5068                                     if(type.isSigned)
5069                                     {
5070                                        bits |= ((intptr)part << bitMember.pos);
5071                                     }
5072                                     else
5073                                     {
5074                                        bits |= ((uintptr)part << bitMember.pos);
5075                                     }
5076                                     break;
5077                                  case intSizeType:
5078                                     if(type.isSigned)
5079                                     {
5080                                        bits |= ((ssize_t)(intsize)part << bitMember.pos);
5081                                     }
5082                                     else
5083                                     {
5084                                        bits |= ((size_t) (uintsize)part << bitMember.pos);
5085                                     }
5086                                     break;
5087                               }
5088                            }
5089                         }
5090                      }
5091                      else
5092                      {
5093                         if(_class && _class.type == unitClass)
5094                         {
5095                            ComputeExpression(member.initializer.exp);
5096                            exp.constant = member.initializer.exp.constant;
5097                            exp.type = constantExp;
5098
5099                            member.initializer.exp.constant = null;
5100                         }
5101                      }
5102                   }
5103                }
5104                break;
5105             }
5106          }
5107       }
5108    }
5109    if(_class && _class.type == bitClass)
5110    {
5111       exp.constant = PrintHexUInt(bits);
5112       exp.type = constantExp;
5113    }
5114    if(exp.type != instanceExp)
5115    {
5116       FreeInstance(inst);
5117    }
5118 }
5119
5120 static bool Promote(Operand op, TypeKind kind, bool isSigned)
5121 {
5122    bool result = false;
5123    switch(kind)
5124    {
5125       case shortType:
5126          if(op.kind == charType || op.kind == enumType || op.kind == _BoolType)
5127             result = isSigned ? GetOpShort(op, &op.s) : GetOpUShort(op, &op.us);
5128          break;
5129       case intType:
5130       case longType:
5131          if(op.kind == charType || op.kind == shortType || op.kind == enumType || op.kind == _BoolType)
5132             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5133          break;
5134       case int64Type:
5135          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5136             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5137             result = isSigned ? GetOpInt64(op, &op.i64) : GetOpUInt64(op, &op.ui64);
5138          break;
5139       case floatType:
5140          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType ||
5141             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5142             result = GetOpFloat(op, &op.f);
5143          break;
5144       case doubleType:
5145          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType ||
5146             op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5147             result = GetOpDouble(op, &op.d);
5148          break;
5149       case pointerType:
5150          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5151             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5152             result = GetOpUIntPtr(op, &op.ui64);
5153          break;
5154       case enumType:
5155          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == int64Type || op.kind == longType || op.kind == floatType || op.kind == doubleType ||
5156             op.kind == pointerType || op.kind == enumType || op.kind == intPtrType || op.kind == intSizeType || op.kind == _BoolType)
5157             result = isSigned ? GetOpInt(op, &op.i) : GetOpUInt(op, &op.ui);
5158          break;
5159       case intPtrType:
5160          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5161             result = isSigned ? GetOpIntPtr(op, &op.i64) : GetOpUIntPtr(op, &op.i64);
5162          break;
5163       case intSizeType:
5164          if(op.kind == charType || op.kind == shortType || op.kind == intType || op.kind == longType || op.kind == enumType || op.kind == _BoolType)
5165             result = isSigned ? GetOpIntSize(op, &op.ui64) : GetOpUIntSize(op, &op.ui64);
5166          break;
5167    }
5168    return result;
5169 }
5170
5171 void CallOperator(Expression exp, Expression exp1, Expression exp2, Operand op1, Operand op2)
5172 {
5173    if(exp.op.op == SIZEOF)
5174    {
5175       FreeExpContents(exp);
5176       exp.type = constantExp;
5177       exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5178    }
5179    else
5180    {
5181       if(!exp.op.exp1)
5182       {
5183          switch(exp.op.op)
5184          {
5185             // unary arithmetic
5186             case '+':
5187             {
5188                // Provide default unary +
5189                Expression exp2 = exp.op.exp2;
5190                exp.op.exp2 = null;
5191                FreeExpContents(exp);
5192                FreeType(exp.expType);
5193                FreeType(exp.destType);
5194                *exp = *exp2;
5195                delete exp2;
5196                break;
5197             }
5198             case '-':
5199                if(op1.ops.Neg) { FreeExpContents(exp); op1.ops.Neg(exp, op1); }
5200                break;
5201             // unary arithmetic increment and decrement
5202                   //OPERATOR_ALL(UNARY, ++, Inc)
5203                   //OPERATOR_ALL(UNARY, --, Dec)
5204             // unary bitwise
5205             case '~':
5206                if(op1.ops.BitNot) { FreeExpContents(exp); op1.ops.BitNot(exp, op1); }
5207                break;
5208             // unary logical negation
5209             case '!':
5210                if(op1.ops.Not) { FreeExpContents(exp); op1.ops.Not(exp, op1); }
5211                break;
5212          }
5213       }
5214       else
5215       {
5216          if(op1 && op2 && op1.type && op2.type && op1.kind != op2.kind)
5217          {
5218             if(Promote(op2, op1.kind, op1.type.isSigned))
5219                op2.kind = op1.kind, op2.ops = op1.ops;
5220             else if(Promote(op1, op2.kind, op2.type.isSigned))
5221                op1.kind = op2.kind, op1.ops = op2.ops;
5222          }
5223          switch(exp.op.op)
5224          {
5225             // binary arithmetic
5226             case '+':
5227                if(op1.ops.Add) { FreeExpContents(exp); op1.ops.Add(exp, op1, op2); }
5228                break;
5229             case '-':
5230                if(op1.ops.Sub) { FreeExpContents(exp); op1.ops.Sub(exp, op1, op2); }
5231                break;
5232             case '*':
5233                if(op1.ops.Mul) { FreeExpContents(exp); op1.ops.Mul(exp, op1, op2); }
5234                break;
5235             case '/':
5236                if(op1.ops.Div) { FreeExpContents(exp); op1.ops.Div(exp, op1, op2); }
5237                break;
5238             case '%':
5239                if(op1.ops.Mod) { FreeExpContents(exp); op1.ops.Mod(exp, op1, op2); }
5240                break;
5241             // binary arithmetic assignment
5242                   //OPERATOR_ALL(BINARY, =, Asign)
5243                   //OPERATOR_ALL(BINARY, +=, AddAsign)
5244                   //OPERATOR_ALL(BINARY, -=, SubAsign)
5245                   //OPERATOR_ALL(BINARY, *=, MulAsign)
5246                   //OPERATOR_ALL(BINARY, /=, DivAsign)
5247                   //OPERATOR_ALL(BINARY, %=, ModAsign)
5248             // binary bitwise
5249             case '&':
5250                if(exp.op.exp2)
5251                {
5252                   if(op1.ops.BitAnd) { FreeExpContents(exp); op1.ops.BitAnd(exp, op1, op2); }
5253                }
5254                break;
5255             case '|':
5256                if(op1.ops.BitOr) { FreeExpContents(exp); op1.ops.BitOr(exp, op1, op2); }
5257                break;
5258             case '^':
5259                if(op1.ops.BitXor) { FreeExpContents(exp); op1.ops.BitXor(exp, op1, op2); }
5260                break;
5261             case LEFT_OP:
5262                if(op1.ops.LShift) { FreeExpContents(exp); op1.ops.LShift(exp, op1, op2); }
5263                break;
5264             case RIGHT_OP:
5265                if(op1.ops.RShift) { FreeExpContents(exp); op1.ops.RShift(exp, op1, op2); }
5266                break;
5267             // binary bitwise assignment
5268                   //OPERATOR_INTTYPES(BINARY, &=, AndAsign)
5269                   //OPERATOR_INTTYPES(BINARY, |=, OrAsign)
5270                   //OPERATOR_INTTYPES(BINARY, ^=, XorAsign)
5271                   //OPERATOR_INTTYPES(BINARY, <<=, LShiftAsign)
5272                   //OPERATOR_INTTYPES(BINARY, >>=, RShiftAsign)
5273             // binary logical equality
5274             case EQ_OP:
5275                if(op1.ops.Equ) { FreeExpContents(exp); op1.ops.Equ(exp, op1, op2); }
5276                break;
5277             case NE_OP:
5278                if(op1.ops.Nqu) { FreeExpContents(exp); op1.ops.Nqu(exp, op1, op2); }
5279                break;
5280             // binary logical
5281             case AND_OP:
5282                if(op1.ops.And) { FreeExpContents(exp); op1.ops.And(exp, op1, op2); }
5283                break;
5284             case OR_OP:
5285                if(op1.ops.Or) { FreeExpContents(exp); op1.ops.Or(exp, op1, op2); }
5286                break;
5287             // binary logical relational
5288             case '>':
5289                if(op1.ops.Grt) { FreeExpContents(exp); op1.ops.Grt(exp, op1, op2); }
5290                break;
5291             case '<':
5292                if(op1.ops.Sma) { FreeExpContents(exp); op1.ops.Sma(exp, op1, op2); }
5293                break;
5294             case GE_OP:
5295                if(op1.ops.GrtEqu) { FreeExpContents(exp); op1.ops.GrtEqu(exp, op1, op2); }
5296                break;
5297             case LE_OP:
5298                if(op1.ops.SmaEqu) { FreeExpContents(exp); op1.ops.SmaEqu(exp, op1, op2); }
5299                break;
5300          }
5301       }
5302    }
5303 }
5304
5305 void ComputeExpression(Expression exp)
5306 {
5307    char expString[10240];
5308    expString[0] = '\0';
5309 #ifdef _DEBUG
5310    PrintExpression(exp, expString);
5311 #endif
5312
5313    switch(exp.type)
5314    {
5315       case instanceExp:
5316       {
5317          ComputeInstantiation(exp);
5318          break;
5319       }
5320       /*
5321       case constantExp:
5322          break;
5323       */
5324       case opExp:
5325       {
5326          Expression exp1, exp2 = null;
5327          Operand op1 { };
5328          Operand op2 { };
5329
5330          // We don't care about operations with only exp2 (INC_OP, DEC_OP...)
5331          if(exp.op.exp2)
5332          {
5333             Expression e = exp.op.exp2;
5334
5335             while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
5336             {
5337                if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
5338                {
5339                   if(e.type == extensionCompoundExp)
5340                      e = ((Statement)e.compound.compound.statements->last).expressions->last;
5341                   else
5342                      e = e.list->last;
5343                }
5344             }
5345             if(exp.op.op == TokenType::sizeOf && e && e.expType)
5346             {
5347                if(e.type == stringExp && e.string)
5348                {
5349                   char * string = e.string;
5350                   int len = strlen(string);
5351                   char * tmp = new char[len-2+1];
5352                   len = UnescapeString(tmp, string + 1, len - 2);
5353                   delete tmp;
5354                   FreeExpContents(exp);
5355                   exp.type = constantExp;
5356                   exp.constant = PrintUInt(len + 1);
5357                }
5358                else
5359                {
5360                   Type type = e.expType;
5361                   type.refCount++;
5362                   FreeExpContents(exp);
5363                   exp.type = constantExp;
5364                   exp.constant = PrintUInt(ComputeTypeSize(type));
5365                   FreeType(type);
5366                }
5367                break;
5368             }
5369             else
5370                ComputeExpression(exp.op.exp2);
5371          }
5372          if(exp.op.exp1)
5373          {
5374             ComputeExpression(exp.op.exp1);
5375             exp1 = exp.op.exp1;
5376             exp2 = exp.op.exp2;
5377             op1 = GetOperand(exp1);
5378             if(op1.type) op1.type.refCount++;
5379             if(exp2)
5380             {
5381                op2 = GetOperand(exp2);
5382                if(op2.type) op2.type.refCount++;
5383             }
5384          }
5385          else
5386          {
5387             exp1 = exp.op.exp2;
5388             op1 = GetOperand(exp1);
5389             if(op1.type) op1.type.refCount++;
5390          }
5391
5392          CallOperator(exp, exp1, exp2, op1, op2);
5393          /*
5394          switch(exp.op.op)
5395          {
5396             // Unary operators
5397             case '&':
5398                // Also binary
5399                if(exp.op.exp1 && exp.op.exp2)
5400                {
5401                   // Binary And
5402                   if(op1.ops.BitAnd)
5403                   {
5404                      FreeExpContents(exp);
5405                      op1.ops.BitAnd(exp, op1, op2);
5406                   }
5407                }
5408                break;
5409             case '*':
5410                if(exp.op.exp1)
5411                {
5412                   if(op1.ops.Mul)
5413                   {
5414                      FreeExpContents(exp);
5415                      op1.ops.Mul(exp, op1, op2);
5416                   }
5417                }
5418                break;
5419             case '+':
5420                if(exp.op.exp1)
5421                {
5422                   if(op1.ops.Add)
5423                   {
5424                      FreeExpContents(exp);
5425                      op1.ops.Add(exp, op1, op2);
5426                   }
5427                }
5428                else
5429                {
5430                   // Provide default unary +
5431                   Expression exp2 = exp.op.exp2;
5432                   exp.op.exp2 = null;
5433                   FreeExpContents(exp);
5434                   FreeType(exp.expType);
5435                   FreeType(exp.destType);
5436
5437                   *exp = *exp2;
5438                   delete exp2;
5439                }
5440                break;
5441             case '-':
5442                if(exp.op.exp1)
5443                {
5444                   if(op1.ops.Sub)
5445                   {
5446                      FreeExpContents(exp);
5447                      op1.ops.Sub(exp, op1, op2);
5448                   }
5449                }
5450                else
5451                {
5452                   if(op1.ops.Neg)
5453                   {
5454                      FreeExpContents(exp);
5455                      op1.ops.Neg(exp, op1);
5456                   }
5457                }
5458                break;
5459             case '~':
5460                if(op1.ops.BitNot)
5461                {
5462                   FreeExpContents(exp);
5463                   op1.ops.BitNot(exp, op1);
5464                }
5465                break;
5466             case '!':
5467                if(op1.ops.Not)
5468                {
5469                   FreeExpContents(exp);
5470                   op1.ops.Not(exp, op1);
5471                }
5472                break;
5473             // Binary only operators
5474             case '/':
5475                if(op1.ops.Div)
5476                {
5477                   FreeExpContents(exp);
5478                   op1.ops.Div(exp, op1, op2);
5479                }
5480                break;
5481             case '%':
5482                if(op1.ops.Mod)
5483                {
5484                   FreeExpContents(exp);
5485                   op1.ops.Mod(exp, op1, op2);
5486                }
5487                break;
5488             case LEFT_OP:
5489                break;
5490             case RIGHT_OP:
5491                break;
5492             case '<':
5493                if(exp.op.exp1)
5494                {
5495                   if(op1.ops.Sma)
5496                   {
5497                      FreeExpContents(exp);
5498                      op1.ops.Sma(exp, op1, op2);
5499                   }
5500                }
5501                break;
5502             case '>':
5503                if(exp.op.exp1)
5504                {
5505                   if(op1.ops.Grt)
5506                   {
5507                      FreeExpContents(exp);
5508                      op1.ops.Grt(exp, op1, op2);
5509                   }
5510                }
5511                break;
5512             case LE_OP:
5513                if(exp.op.exp1)
5514                {
5515                   if(op1.ops.SmaEqu)
5516                   {
5517                      FreeExpContents(exp);
5518                      op1.ops.SmaEqu(exp, op1, op2);
5519                   }
5520                }
5521                break;
5522             case GE_OP:
5523                if(exp.op.exp1)
5524                {
5525                   if(op1.ops.GrtEqu)
5526                   {
5527                      FreeExpContents(exp);
5528                      op1.ops.GrtEqu(exp, op1, op2);
5529                   }
5530                }
5531                break;
5532             case EQ_OP:
5533                if(exp.op.exp1)
5534                {
5535                   if(op1.ops.Equ)
5536                   {
5537                      FreeExpContents(exp);
5538                      op1.ops.Equ(exp, op1, op2);
5539                   }
5540                }
5541                break;
5542             case NE_OP:
5543                if(exp.op.exp1)
5544                {
5545                   if(op1.ops.Nqu)
5546                   {
5547                      FreeExpContents(exp);
5548                      op1.ops.Nqu(exp, op1, op2);
5549                   }
5550                }
5551                break;
5552             case '|':
5553                if(op1.ops.BitOr)
5554                {
5555                   FreeExpContents(exp);
5556                   op1.ops.BitOr(exp, op1, op2);
5557                }
5558                break;
5559             case '^':
5560                if(op1.ops.BitXor)
5561                {
5562                   FreeExpContents(exp);
5563                   op1.ops.BitXor(exp, op1, op2);
5564                }
5565                break;
5566             case AND_OP:
5567                break;
5568             case OR_OP:
5569                break;
5570             case SIZEOF:
5571                FreeExpContents(exp);
5572                exp.type = constantExp;
5573                exp.constant = PrintUInt(ComputeTypeSize(op1.type));
5574                break;
5575          }
5576          */
5577          if(op1.type) FreeType(op1.type);
5578          if(op2.type) FreeType(op2.type);
5579          break;
5580       }
5581       case bracketsExp:
5582       case extensionExpressionExp:
5583       {
5584          Expression e, n;
5585          for(e = exp.list->first; e; e = n)
5586          {
5587             n = e.next;
5588             if(!n)
5589             {
5590                OldList * list = exp.list;
5591                ComputeExpression(e);
5592                //FreeExpContents(exp);
5593                FreeType(exp.expType);
5594                FreeType(exp.destType);
5595                *exp = *e;
5596                delete e;
5597                delete list;
5598             }
5599             else
5600             {
5601                FreeExpression(e);
5602             }
5603          }
5604          break;
5605       }
5606       /*
5607
5608       case ExpIndex:
5609       {
5610          Expression e;
5611          exp.isConstant = true;
5612
5613          ComputeExpression(exp.index.exp);
5614          if(!exp.index.exp.isConstant)
5615             exp.isConstant = false;
5616
5617          for(e = exp.index.index->first; e; e = e.next)
5618          {
5619             ComputeExpression(e);
5620             if(!e.next)
5621             {
5622                // Check if this type is int
5623             }
5624             if(!e.isConstant)
5625                exp.isConstant = false;
5626          }
5627          exp.expType = Dereference(exp.index.exp.expType);
5628          break;
5629       }
5630       */
5631       case memberExp:
5632       {
5633          Expression memberExp = exp.member.exp;
5634          Identifier memberID = exp.member.member;
5635
5636          Type type;
5637          ComputeExpression(exp.member.exp);
5638          type = exp.member.exp.expType;
5639          if(type)
5640          {
5641             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);
5642             Property prop = null;
5643             DataMember member = null;
5644             Class convertTo = null;
5645             if(type.kind == subClassType && exp.member.exp.type == classExp)
5646                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
5647
5648             if(!_class)
5649             {
5650                char string[256];
5651                Symbol classSym;
5652                string[0] = '\0';
5653                PrintTypeNoConst(type, string, false, true);
5654                classSym = FindClass(string);
5655                _class = classSym ? classSym.registered : null;
5656             }
5657
5658             if(exp.member.member)
5659             {
5660                prop = eClass_FindProperty(_class, exp.member.member.string, privateModule);
5661                if(!prop)
5662                   member = eClass_FindDataMember(_class, exp.member.member.string, privateModule, null, null);
5663             }
5664             if(!prop && !member && _class && exp.member.member)
5665             {
5666                Symbol classSym = FindClass(exp.member.member.string);
5667                convertTo = _class;
5668                _class = classSym ? classSym.registered : null;
5669                prop = eClass_FindProperty(_class, convertTo.fullName, privateModule);
5670             }
5671
5672             if(prop)
5673             {
5674                if(prop.compiled)
5675                {
5676                   Type type = prop.dataType;
5677                   // TODO: Assuming same base type for units...
5678                   if(_class.type == unitClass)
5679                   {
5680                      if(type.kind == classType)
5681                      {
5682                         Class _class = type._class.registered;
5683                         if(_class.type == unitClass)
5684                         {
5685                            if(!_class.dataType)
5686                               _class.dataType = ProcessTypeString(_class.dataTypeString, false);
5687                            type = _class.dataType;
5688                         }
5689                      }
5690                      switch(type.kind)
5691                      {
5692                         case floatType:
5693                         {
5694                            float value;
5695                            float (*Get)(float) = (void *)prop.Get;
5696                            GetFloat(exp.member.exp, &value);
5697                            exp.constant = PrintFloat(Get ? Get(value) : value);
5698                            exp.type = constantExp;
5699                            break;
5700                         }
5701                         case doubleType:
5702                         {
5703                            double value;
5704                            double (*Get)(double);
5705                            GetDouble(exp.member.exp, &value);
5706
5707                            if(convertTo)
5708                               Get = (void *)prop.Set;
5709                            else
5710                               Get = (void *)prop.Get;
5711                            exp.constant = PrintDouble(Get ? Get(value) : value);
5712                            exp.type = constantExp;
5713                            break;
5714                         }
5715                      }
5716                   }
5717                   else
5718                   {
5719                      if(convertTo)
5720                      {
5721                         Expression value = exp.member.exp;
5722                         Type type;
5723                         if(!prop.dataType)
5724                            ProcessPropertyType(prop);
5725
5726                         type = prop.dataType;
5727                         if(!type)
5728                         {
5729                             // printf("Investigate this\n");
5730                         }
5731                         else if(_class.type == structClass)
5732                         {
5733                            switch(type.kind)
5734                            {
5735                               case classType:
5736                               {
5737                                  Class propertyClass = type._class.registered;
5738                                  if(propertyClass.type == structClass && value.type == instanceExp)
5739                                  {
5740                                     void (*Set)(void *, void *) = (void *)prop.Set;
5741                                     exp.instance = Instantiation { };
5742                                     exp.instance.data = new0 byte[_class.structSize];
5743                                     exp.instance._class = MkSpecifierName(_class.fullName);
5744                                     exp.instance.loc = exp.loc;
5745                                     exp.type = instanceExp;
5746                                     Set(exp.instance.data, value.instance.data);
5747                                     PopulateInstance(exp.instance);
5748                                  }
5749                                  break;
5750                               }
5751                               case intType:
5752                               {
5753                                  int intValue;
5754                                  void (*Set)(void *, int) = (void *)prop.Set;
5755
5756                                  exp.instance = Instantiation { };
5757                                  exp.instance.data = new0 byte[_class.structSize];
5758                                  exp.instance._class = MkSpecifierName(_class.fullName);
5759                                  exp.instance.loc = exp.loc;
5760                                  exp.type = instanceExp;
5761
5762                                  GetInt(value, &intValue);
5763
5764                                  Set(exp.instance.data, intValue);
5765                                  PopulateInstance(exp.instance);
5766                                  break;
5767                               }
5768                               case int64Type:
5769                               {
5770                                  int64 intValue;
5771                                  void (*Set)(void *, int64) = (void *)prop.Set;
5772
5773                                  exp.instance = Instantiation { };
5774                                  exp.instance.data = new0 byte[_class.structSize];
5775                                  exp.instance._class = MkSpecifierName/*MkClassName*/(_class.fullName);
5776                                  exp.instance.loc = exp.loc;
5777                                  exp.type = instanceExp;
5778
5779                                  GetInt64(value, &intValue);
5780
5781                                  Set(exp.instance.data, intValue);
5782                                  PopulateInstance(exp.instance);
5783                                  break;
5784                               }
5785                               case intPtrType:
5786                               {
5787                                  // TOFIX:
5788                                  intptr intValue;
5789                                  void (*Set)(void *, intptr) = (void *)prop.Set;
5790
5791                                  exp.instance = Instantiation { };
5792                                  exp.instance.data = new0 byte[_class.structSize];
5793                                  exp.instance._class = MkSpecifierName(_class.fullName);
5794                                  exp.instance.loc = exp.loc;
5795                                  exp.type = instanceExp;
5796
5797                                  GetIntPtr(value, &intValue);
5798
5799                                  Set(exp.instance.data, intValue);
5800                                  PopulateInstance(exp.instance);
5801                                  break;
5802                               }
5803                               case intSizeType:
5804                               {
5805                                  // TOFIX:
5806                                  intsize intValue;
5807                                  void (*Set)(void *, intsize) = (void *)prop.Set;
5808
5809                                  exp.instance = Instantiation { };
5810                                  exp.instance.data = new0 byte[_class.structSize];
5811                                  exp.instance._class = MkSpecifierName(_class.fullName);
5812                                  exp.instance.loc = exp.loc;
5813                                  exp.type = instanceExp;
5814
5815                                  GetIntSize(value, &intValue);
5816
5817                                  Set(exp.instance.data, intValue);
5818                                  PopulateInstance(exp.instance);
5819                                  break;
5820                               }
5821                               case floatType:
5822                               {
5823                                  float floatValue;
5824                                  void (*Set)(void *, float) = (void *)prop.Set;
5825
5826                                  exp.instance = Instantiation { };
5827                                  exp.instance.data = new0 byte[_class.structSize];
5828                                  exp.instance._class = MkSpecifierName(_class.fullName);
5829                                  exp.instance.loc = exp.loc;
5830                                  exp.type = instanceExp;
5831
5832                                  GetFloat(value, &floatValue);
5833
5834                                  Set(exp.instance.data, floatValue);
5835                                  PopulateInstance(exp.instance);
5836                                  break;
5837                               }
5838                               case doubleType:
5839                               {
5840                                  double doubleValue;
5841                                  void (*Set)(void *, double) = (void *)prop.Set;
5842
5843                                  exp.instance = Instantiation { };
5844                                  exp.instance.data = new0 byte[_class.structSize];
5845                                  exp.instance._class = MkSpecifierName(_class.fullName);
5846                                  exp.instance.loc = exp.loc;
5847                                  exp.type = instanceExp;
5848
5849                                  GetDouble(value, &doubleValue);
5850
5851                                  Set(exp.instance.data, doubleValue);
5852                                  PopulateInstance(exp.instance);
5853                                  break;
5854                               }
5855                            }
5856                         }
5857                         else if(_class.type == bitClass)
5858                         {
5859                            switch(type.kind)
5860                            {
5861                               case classType:
5862                               {
5863                                  Class propertyClass = type._class.registered;
5864                                  if(propertyClass.type == structClass && value.instance.data)
5865                                  {
5866                                     unsigned int (*Set)(void *) = (void *)prop.Set;
5867                                     unsigned int bits = Set(value.instance.data);
5868                                     exp.constant = PrintHexUInt(bits);
5869                                     exp.type = constantExp;
5870                                     break;
5871                                  }
5872                                  else if(_class.type == bitClass)
5873                                  {
5874                                     unsigned int value;
5875                                     unsigned int (*Set)(unsigned int) = (void *)prop.Set;
5876                                     unsigned int bits;
5877
5878                                     GetUInt(exp.member.exp, &value);
5879                                     bits = Set(value);
5880                                     exp.constant = PrintHexUInt(bits);
5881                                     exp.type = constantExp;
5882                                  }
5883                               }
5884                            }
5885                         }
5886                      }
5887                      else
5888                      {
5889                         if(_class.type == bitClass)
5890                         {
5891                            unsigned int value;
5892                            GetUInt(exp.member.exp, &value);
5893
5894                            switch(type.kind)
5895                            {
5896                               case classType:
5897                               {
5898                                  Class _class = type._class.registered;
5899                                  if(_class.type == structClass)
5900                                  {
5901                                     void (*Get)(unsigned int, void *) = (void *)prop.Get;
5902
5903                                     exp.instance = Instantiation { };
5904                                     exp.instance.data = new0 byte[_class.structSize];
5905                                     exp.instance._class = MkSpecifierName(_class.fullName);
5906                                     exp.instance.loc = exp.loc;
5907                                     //exp.instance.fullSet = true;
5908                                     exp.type = instanceExp;
5909                                     Get(value, exp.instance.data);
5910                                     PopulateInstance(exp.instance);
5911                                  }
5912                                  else if(_class.type == bitClass)
5913                                  {
5914                                     unsigned int (*Get)(unsigned int) = (void *)prop.Get;
5915                                     uint64 bits = Get(value);
5916                                     exp.constant = PrintHexUInt64(bits);
5917                                     exp.type = constantExp;
5918                                  }
5919                                  break;
5920                               }
5921                            }
5922                         }
5923                         else if(_class.type == structClass)
5924                         {
5925                            char * value = (exp.member.exp.type == instanceExp ) ? exp.member.exp.instance.data : null;
5926                            switch(type.kind)
5927                            {
5928                               case classType:
5929                               {
5930                                  Class _class = type._class.registered;
5931                                  if(_class.type == structClass && value)
5932                                  {
5933                                     void (*Get)(void *, void *) = (void *)prop.Get;
5934
5935                                     exp.instance = Instantiation { };
5936                                     exp.instance.data = new0 byte[_class.structSize];
5937                                     exp.instance._class = MkSpecifierName(_class.fullName);
5938                                     exp.instance.loc = exp.loc;
5939                                     //exp.instance.fullSet = true;
5940                                     exp.type = instanceExp;
5941                                     Get(value, exp.instance.data);
5942                                     PopulateInstance(exp.instance);
5943                                  }
5944                                  break;
5945                               }
5946                            }
5947                         }
5948                         /*else
5949                         {
5950                            char * value = exp.member.exp.instance.data;
5951                            switch(type.kind)
5952                            {
5953                               case classType:
5954                               {
5955                                  Class _class = type._class.registered;
5956                                  if(_class.type == normalClass)
5957                                  {
5958                                     void *(*Get)(void *) = (void *)prop.Get;
5959
5960                                     exp.instance = Instantiation { };
5961                                     exp.instance._class = MkSpecifierName(_class.fullName); //MkClassName(_class.fullName);
5962                                     exp.type = instanceExp;
5963                                     exp.instance.data = Get(value, exp.instance.data);
5964                                  }
5965                                  break;
5966                               }
5967                            }
5968                         }
5969                         */
5970                      }
5971                   }
5972                }
5973                else
5974                {
5975                   exp.isConstant = false;
5976                }
5977             }
5978             else if(member)
5979             {
5980             }
5981          }
5982
5983          if(exp.type != ExpressionType::memberExp)
5984          {
5985             FreeExpression(memberExp);
5986             FreeIdentifier(memberID);
5987          }
5988          break;
5989       }
5990       case typeSizeExp:
5991       {
5992          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
5993          FreeExpContents(exp);
5994          exp.constant = PrintUInt(ComputeTypeSize(type));
5995          exp.type = constantExp;
5996          FreeType(type);
5997          break;
5998       }
5999       case classSizeExp:
6000       {
6001          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
6002          if(classSym && classSym.registered)
6003          {
6004             if(classSym.registered.fixed)
6005             {
6006                FreeSpecifier(exp._class);
6007                exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
6008                exp.type = constantExp;
6009             }
6010             else
6011             {
6012                char className[1024];
6013                strcpy(className, "__ecereClass_");
6014                FullClassNameCat(className, classSym.string, true);
6015                MangleClassName(className);
6016
6017                DeclareClass(classSym, className);
6018
6019                FreeExpContents(exp);
6020                exp.type = pointerExp;
6021                exp.member.exp = MkExpIdentifier(MkIdentifier(className));
6022                exp.member.member = MkIdentifier("structSize");
6023             }
6024          }
6025          break;
6026       }
6027       case castExp:
6028       //case constantExp:
6029       {
6030          Type type;
6031          Expression e = exp;
6032          if(exp.type == castExp)
6033          {
6034             if(exp.cast.exp)
6035                ComputeExpression(exp.cast.exp);
6036             e = exp.cast.exp;
6037          }
6038          if(e && exp.expType)
6039          {
6040             /*if(exp.destType)
6041                type = exp.destType;
6042             else*/
6043                type = exp.expType;
6044             if(type.kind == classType)
6045             {
6046                Class _class = type._class.registered;
6047                if(_class && (_class.type == unitClass || _class.type == bitClass))
6048                {
6049                   if(!_class.dataType)
6050                      _class.dataType = ProcessTypeString(_class.dataTypeString, false);
6051                   type = _class.dataType;
6052                }
6053             }
6054
6055             switch(type.kind)
6056             {
6057                case _BoolType:
6058                case charType:
6059                   if(type.isSigned)
6060                   {
6061                      char value = 0;
6062                      if(GetChar(e, &value))
6063                      {
6064                         FreeExpContents(exp);
6065                         exp.constant = PrintChar(value);
6066                         exp.type = constantExp;
6067                      }
6068                   }
6069                   else
6070                   {
6071                      unsigned char value = 0;
6072                      if(GetUChar(e, &value))
6073                      {
6074                         FreeExpContents(exp);
6075                         exp.constant = PrintUChar(value);
6076                         exp.type = constantExp;
6077                      }
6078                   }
6079                   break;
6080                case shortType:
6081                   if(type.isSigned)
6082                   {
6083                      short value = 0;
6084                      if(GetShort(e, &value))
6085                      {
6086                         FreeExpContents(exp);
6087                         exp.constant = PrintShort(value);
6088                         exp.type = constantExp;
6089                      }
6090                   }
6091                   else
6092                   {
6093                      unsigned short value = 0;
6094                      if(GetUShort(e, &value))
6095                      {
6096                         FreeExpContents(exp);
6097                         exp.constant = PrintUShort(value);
6098                         exp.type = constantExp;
6099                      }
6100                   }
6101                   break;
6102                case intType:
6103                   if(type.isSigned)
6104                   {
6105                      int value = 0;
6106                      if(GetInt(e, &value))
6107                      {
6108                         FreeExpContents(exp);
6109                         exp.constant = PrintInt(value);
6110                         exp.type = constantExp;
6111                      }
6112                   }
6113                   else
6114                   {
6115                      unsigned int value = 0;
6116                      if(GetUInt(e, &value))
6117                      {
6118                         FreeExpContents(exp);
6119                         exp.constant = PrintUInt(value);
6120                         exp.type = constantExp;
6121                      }
6122                   }
6123                   break;
6124                case int64Type:
6125                   if(type.isSigned)
6126                   {
6127                      int64 value = 0;
6128                      if(GetInt64(e, &value))
6129                      {
6130                         FreeExpContents(exp);
6131                         exp.constant = PrintInt64(value);
6132                         exp.type = constantExp;
6133                      }
6134                   }
6135                   else
6136                   {
6137                      uint64 value = 0;
6138                      if(GetUInt64(e, &value))
6139                      {
6140                         FreeExpContents(exp);
6141                         exp.constant = PrintUInt64(value);
6142                         exp.type = constantExp;
6143                      }
6144                   }
6145                   break;
6146                case intPtrType:
6147                   if(type.isSigned)
6148                   {
6149                      intptr value = 0;
6150                      if(GetIntPtr(e, &value))
6151                      {
6152                         FreeExpContents(exp);
6153                         exp.constant = PrintInt64((int64)value);
6154                         exp.type = constantExp;
6155                      }
6156                   }
6157                   else
6158                   {
6159                      uintptr value = 0;
6160                      if(GetUIntPtr(e, &value))
6161                      {
6162                         FreeExpContents(exp);
6163                         exp.constant = PrintUInt64((uint64)value);
6164                         exp.type = constantExp;
6165                      }
6166                   }
6167                   break;
6168                case intSizeType:
6169                   if(type.isSigned)
6170                   {
6171                      intsize value = 0;
6172                      if(GetIntSize(e, &value))
6173                      {
6174                         FreeExpContents(exp);
6175                         exp.constant = PrintInt64((int64)value);
6176                         exp.type = constantExp;
6177                      }
6178                   }
6179                   else
6180                   {
6181                      uintsize value = 0;
6182                      if(GetUIntSize(e, &value))
6183                      {
6184                         FreeExpContents(exp);
6185                         exp.constant = PrintUInt64((uint64)value);
6186                         exp.type = constantExp;
6187                      }
6188                   }
6189                   break;
6190                case floatType:
6191                {
6192                   float value = 0;
6193                   if(GetFloat(e, &value))
6194                   {
6195                      FreeExpContents(exp);
6196                      exp.constant = PrintFloat(value);
6197                      exp.type = constantExp;
6198                   }
6199                   break;
6200                }
6201                case doubleType:
6202                {
6203                   double value = 0;
6204                   if(GetDouble(e, &value))
6205                   {
6206                      FreeExpContents(exp);
6207                      exp.constant = PrintDouble(value);
6208                      exp.type = constantExp;
6209                   }
6210                   break;
6211                }
6212             }
6213          }
6214          break;
6215       }
6216       case conditionExp:
6217       {
6218          Operand op1 { };
6219          Operand op2 { };
6220          Operand op3 { };
6221
6222          if(exp.cond.exp)
6223             // Caring only about last expression for now...
6224             ComputeExpression(exp.cond.exp->last);
6225          if(exp.cond.elseExp)
6226             ComputeExpression(exp.cond.elseExp);
6227          if(exp.cond.cond)
6228             ComputeExpression(exp.cond.cond);
6229
6230          op1 = GetOperand(exp.cond.cond);
6231          if(op1.type) op1.type.refCount++;
6232          op2 = GetOperand(exp.cond.exp->last);
6233          if(op2.type) op2.type.refCount++;
6234          op3 = GetOperand(exp.cond.elseExp);
6235          if(op3.type) op3.type.refCount++;
6236
6237          if(op1.ops.Cond) { FreeExpContents(exp); op1.ops.Cond(exp, op1, op2, op3); }
6238          if(op1.type) FreeType(op1.type);
6239          if(op2.type) FreeType(op2.type);
6240          if(op3.type) FreeType(op3.type);
6241          break;
6242       }
6243    }
6244 }
6245
6246 static bool CheckExpressionType(Expression exp, Type destType, bool skipUnitBla)
6247 {
6248    bool result = true;
6249    if(destType)
6250    {
6251       OldList converts { };
6252       Conversion convert;
6253
6254       if(destType.kind == voidType)
6255          return false;
6256
6257       if(!MatchTypeExpression(exp, destType, &converts, skipUnitBla))
6258          result = false;
6259       if(converts.count)
6260       {
6261          // for(convert = converts.last; convert; convert = convert.prev)
6262          for(convert = converts.first; convert; convert = convert.next)
6263          {
6264             bool empty = !(convert.isGet ? (void *)convert.convert.Get : (void *)convert.convert.Set);
6265             if(!empty)
6266             {
6267                Expression newExp { };
6268                ClassObjectType objectType = exp.expType ? exp.expType.classObjectType : none;
6269
6270                // TODO: Check this...
6271                *newExp = *exp;
6272                newExp.destType = null;
6273
6274                if(convert.isGet)
6275                {
6276                   // [exp].ColorRGB
6277                   exp.type = memberExp;
6278                   exp.addedThis = true;
6279                   exp.member.exp = newExp;
6280                   FreeType(exp.member.exp.expType);
6281
6282                   exp.member.exp.expType = MkClassType(convert.convert._class.fullName);
6283                   exp.member.exp.expType.classObjectType = objectType;
6284                   exp.member.member = MkIdentifier(convert.convert.dataTypeString);
6285                   exp.member.memberType = propertyMember;
6286                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6287                   // TESTING THIS... for (int)degrees
6288                   exp.needCast = true;
6289                   if(exp.expType) exp.expType.refCount++;
6290                   ApplyAnyObjectLogic(exp.member.exp);
6291                }
6292                else
6293                {
6294
6295                   /*if(exp.isConstant)
6296                   {
6297                      // Color { ColorRGB = [exp] };
6298                      exp.type = instanceExp;
6299                      exp.instance = MkInstantiation(MkSpecifierName((convert.convert._class.fullName), //MkClassName(convert.convert._class.fullName),
6300                         null, MkListOne(MkMembersInitList(MkListOne(MkMemberInit(
6301                         MkListOne(MkIdentifier(convert.convert.dataTypeString)), newExp)))));
6302                   }
6303                   else*/
6304                   {
6305                      // If not constant, don't turn it yet into an instantiation
6306                      // (Go through the deep members system first)
6307                      exp.type = memberExp;
6308                      exp.addedThis = true;
6309                      exp.member.exp = newExp;
6310
6311                      // ADDED THIS HERE TO SOLVE PROPERTY ISSUES WITH NOHEAD CLASSES
6312                      if(/*!notByReference && */newExp.expType && newExp.expType.kind == classType && newExp.expType._class && newExp.expType._class.registered &&
6313                         newExp.expType._class.registered.type == noHeadClass)
6314                      {
6315                         newExp.byReference = true;
6316                      }
6317
6318                      FreeType(exp.member.exp.expType);
6319                      /*exp.member.exp.expType = convert.convert.dataType;
6320                      if(convert.convert.dataType) convert.convert.dataType.refCount++;*/
6321                      exp.member.exp.expType = null;
6322                      if(convert.convert.dataType)
6323                      {
6324                         exp.member.exp.expType = { };
6325                         CopyTypeInto(exp.member.exp.expType, convert.convert.dataType);
6326                         exp.member.exp.expType.refCount = 1;
6327                         exp.member.exp.expType.classObjectType = objectType;
6328                         ApplyAnyObjectLogic(exp.member.exp);
6329                      }
6330
6331                      exp.member.member = MkIdentifier(convert.convert._class.fullName);
6332                      exp.member.memberType = reverseConversionMember;
6333                      exp.expType = convert.resultType ? convert.resultType :
6334                         MkClassType(convert.convert._class.fullName);
6335                      exp.needCast = true;
6336                      if(convert.resultType) convert.resultType.refCount++;
6337                   }
6338                }
6339             }
6340             else
6341             {
6342                FreeType(exp.expType);
6343                if(convert.isGet)
6344                {
6345                   exp.expType = convert.resultType ? convert.resultType : convert.convert.dataType;
6346                   exp.needCast = true;
6347                   if(exp.expType) exp.expType.refCount++;
6348                }
6349                else
6350                {
6351                   exp.expType = convert.resultType ? convert.resultType : MkClassType(convert.convert._class.fullName);
6352                   exp.needCast = true;
6353                   if(convert.resultType)
6354                      convert.resultType.refCount++;
6355                }
6356             }
6357          }
6358          if(exp.isConstant && inCompiler)
6359             ComputeExpression(exp);
6360
6361          converts.Free(FreeConvert);
6362       }
6363
6364       if(!result && exp.expType && converts.count)      // TO TEST: Added converts.count here to avoid a double warning with function type
6365       {
6366          result = MatchTypes(exp.expType, exp.destType, null, null, null, true, true, false, false);
6367       }
6368       if(!result && exp.expType && exp.destType)
6369       {
6370          if((exp.destType.kind == classType && exp.expType.kind == pointerType &&
6371              exp.expType.type.kind == classType && exp.expType.type._class == exp.destType._class && exp.destType._class.registered && exp.destType._class.registered.type == structClass) ||
6372             (exp.expType.kind == classType && exp.destType.kind == pointerType &&
6373             exp.destType.type.kind == classType && exp.destType.type._class == exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass))
6374             result = true;
6375       }
6376    }
6377    // if(result) CheckTemplateTypes(exp);
6378    return result;
6379 }
6380
6381 void CheckTemplateTypes(Expression exp)
6382 {
6383    if(exp.destType && exp.destType.passAsTemplate && exp.expType && exp.expType.kind != templateType && !exp.expType.passAsTemplate)
6384    {
6385       Expression newExp { };
6386       Statement compound;
6387       Context context;
6388       *newExp = *exp;
6389       if(exp.destType) exp.destType.refCount++;
6390       if(exp.expType)  exp.expType.refCount++;
6391       newExp.prev = null;
6392       newExp.next = null;
6393
6394       switch(exp.expType.kind)
6395       {
6396          case doubleType:
6397             if(exp.destType.classObjectType)
6398             {
6399                // We need to pass the address, just pass it along (Undo what was done above)
6400                if(exp.destType) exp.destType.refCount--;
6401                if(exp.expType)  exp.expType.refCount--;
6402                delete newExp;
6403             }
6404             else
6405             {
6406                // If we're looking for value:
6407                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6408                OldList * specs;
6409                OldList * unionDefs = MkList();
6410                OldList * statements = MkList();
6411                context = PushContext();
6412                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6413                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6414                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6415                exp.type = extensionCompoundExp;
6416                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6417                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")), '=', newExp))));
6418                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")))));
6419                exp.compound.compound.context = context;
6420                PopContext(context);
6421             }
6422             break;
6423          default:
6424             exp.type = castExp;
6425             exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6426             exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6427             break;
6428       }
6429    }
6430    else if(exp.expType && exp.expType.passAsTemplate && exp.destType && exp.usage.usageGet && exp.destType.kind != templateType && !exp.destType.passAsTemplate)
6431    {
6432       Expression newExp { };
6433       Statement compound;
6434       Context context;
6435       *newExp = *exp;
6436       if(exp.destType) exp.destType.refCount++;
6437       if(exp.expType)  exp.expType.refCount++;
6438       newExp.prev = null;
6439       newExp.next = null;
6440
6441       switch(exp.expType.kind)
6442       {
6443          case doubleType:
6444             if(exp.destType.classObjectType)
6445             {
6446                // We need to pass the address, just pass it along (Undo what was done above)
6447                if(exp.destType) exp.destType.refCount--;
6448                if(exp.expType)  exp.expType.refCount--;
6449                delete newExp;
6450             }
6451             else
6452             {
6453                // If we're looking for value:
6454                // ({ union { double d; uint64 i; } u; u.i = [newExp]; u.d; })
6455                OldList * specs;
6456                OldList * unionDefs = MkList();
6457                OldList * statements = MkList();
6458                context = PushContext();
6459                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifier(DOUBLE)), MkListOne(MkDeclaratorIdentifier(MkIdentifier("d"))), null)));
6460                ListAdd(unionDefs, MkClassDefDeclaration(MkStructDeclaration(MkListOne(MkSpecifierName("uint64")), MkListOne(MkDeclaratorIdentifier(MkIdentifier("i"))), null)));
6461                specs = MkListOne(MkStructOrUnion(unionSpecifier, null, unionDefs ));
6462                exp.type = extensionCompoundExp;
6463                exp.compound = MkCompoundStmt(MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internal_union")), null)))),statements);
6464                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("i")), '=', newExp))));
6465                ListAdd(statements, MkExpressionStmt(MkListOne(MkExpMember(MkExpIdentifier(MkIdentifier("__internal_union")), MkIdentifier("d")))));
6466                exp.compound.compound.context = context;
6467                PopContext(context);
6468             }
6469             break;
6470          case classType:
6471          {
6472             if(exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type == structClass)
6473             {
6474                exp.type = bracketsExp;
6475                exp.list = MkListOne(MkExpOp(null, '*', MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)),
6476                   MkDeclaratorPointer(MkPointer(null, null), null)), newExp)));
6477                ProcessExpressionType(exp.list->first);
6478                break;
6479             }
6480             else
6481             {
6482                exp.type = bracketsExp;
6483                exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(exp.expType._class.string)), null), newExp));
6484                newExp.needCast = true;
6485                ProcessExpressionType(exp.list->first);
6486                break;
6487             }
6488          }
6489          default:
6490          {
6491             if(exp.expType.kind == templateType)
6492             {
6493                Type type = ProcessTemplateParameterType(exp.expType.templateParameter);
6494                if(type)
6495                {
6496                   FreeType(exp.destType);
6497                   FreeType(exp.expType);
6498                   delete newExp;
6499                   break;
6500                }
6501             }
6502             if(newExp.type == memberExp && newExp.member.memberType == dataMember)
6503             {
6504                exp.type = opExp;
6505                exp.op.op = '*';
6506                exp.op.exp1 = null;
6507                exp.op.exp2 = MkExpCast(MkTypeName(MkListOne(MkSpecifierName("uint64")), MkDeclaratorPointer(MkPointer(null, null), null)),
6508                   MkExpBrackets(MkListOne(MkExpOp(null, '&', newExp))));
6509             }
6510             else
6511             {
6512                char typeString[1024];
6513                Declarator decl;
6514                OldList * specs = MkList();
6515                typeString[0] = '\0';
6516                PrintType(exp.expType, typeString, false, false);
6517                decl = SpecDeclFromString(typeString, specs, null);
6518
6519                exp.type = castExp;
6520                //exp.cast.typeName = MkTypeName(MkListOne(MkSpecifierName("uint64")), null);
6521                exp.cast.typeName = MkTypeName(specs, decl);
6522                exp.cast.exp = MkExpBrackets(MkListOne(newExp));
6523                exp.cast.exp.needCast = true;
6524             }
6525             break;
6526          }
6527       }
6528    }
6529 }
6530 // TODO: The Symbol tree should be reorganized by namespaces
6531 // Name Space:
6532 //    - Tree of all symbols within (stored without namespace)
6533 //    - Tree of sub-namespaces
6534
6535 static Symbol ScanWithNameSpace(BinaryTree tree, char * nameSpace, char * name)
6536 {
6537    int nsLen = strlen(nameSpace);
6538    Symbol symbol;
6539    // Start at the name space prefix
6540    for(symbol = (Symbol)tree.FindPrefix(nameSpace); symbol; symbol = (Symbol)((BTNode)symbol).next)
6541    {
6542       char * s = symbol.string;
6543       if(!strncmp(s, nameSpace, nsLen))
6544       {
6545          // This supports e.g. matching ecere::Socket to ecere::net::Socket
6546          int c;
6547          char * namePart;
6548          for(c = strlen(s)-1; c >= 0; c--)
6549             if(s[c] == ':')
6550                break;
6551
6552          namePart = s+c+1;
6553          if(!strcmp(namePart, name))
6554          {
6555             // TODO: Error on ambiguity
6556             return symbol;
6557          }
6558       }
6559       else
6560          break;
6561    }
6562    return null;
6563 }
6564
6565 static Symbol FindWithNameSpace(BinaryTree tree, char * name)
6566 {
6567    int c;
6568    char nameSpace[1024];
6569    char * namePart;
6570    bool gotColon = false;
6571
6572    nameSpace[0] = '\0';
6573    for(c = strlen(name)-1; c >= 0; c--)
6574       if(name[c] == ':')
6575       {
6576          gotColon = true;
6577          break;
6578       }
6579
6580    namePart = name+c+1;
6581    while(c >= 0 && name[c] == ':') c--;
6582    if(c >= 0)
6583    {
6584       // Try an exact match first
6585       Symbol symbol = (Symbol)tree.FindString(name);
6586       if(symbol)
6587          return symbol;
6588
6589       // Namespace specified
6590       memcpy(nameSpace, name, c + 1);
6591       nameSpace[c+1] = 0;
6592
6593       return ScanWithNameSpace(tree, nameSpace, namePart);
6594    }
6595    else if(gotColon)
6596    {
6597       // Looking for a global symbol, e.g. ::Sleep()
6598       Symbol symbol = (Symbol)tree.FindString(namePart);
6599       return symbol;
6600    }
6601    else
6602    {
6603       // Name only (no namespace specified)
6604       Symbol symbol = (Symbol)tree.FindString(namePart);
6605       if(symbol)
6606          return symbol;
6607       return ScanWithNameSpace(tree, "", namePart);
6608    }
6609    return null;
6610 }
6611
6612 static void ProcessDeclaration(Declaration decl);
6613
6614 /*static */Symbol FindSymbol(char * name, Context startContext, Context endContext, bool isStruct, bool globalNameSpace)
6615 {
6616 #ifdef _DEBUG
6617    //Time startTime = GetTime();
6618 #endif
6619    // Optimize this later? Do this before/less?
6620    Context ctx;
6621    Symbol symbol = null;
6622    // First, check if the identifier is declared inside the function
6623    //for(ctx = curContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6624
6625    for(ctx = startContext; ctx /*!= topContext.parent */&& !symbol; ctx = ctx.parent)
6626    {
6627       if(ctx == globalContext && !globalNameSpace && ctx.hasNameSpace)
6628       {
6629          symbol = null;
6630          if(thisNameSpace)
6631          {
6632             char curName[1024];
6633             strcpy(curName, thisNameSpace);
6634             strcat(curName, "::");
6635             strcat(curName, name);
6636             // Try to resolve in current namespace first
6637             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, curName);
6638          }
6639          if(!symbol)
6640             symbol = FindWithNameSpace(isStruct ? ctx.structSymbols : ctx.symbols, name);
6641       }
6642       else
6643          symbol = (Symbol)(isStruct ? ctx.structSymbols : ctx.symbols).FindString(name);
6644
6645       if(symbol || ctx == endContext) break;
6646    }
6647    if(inCompiler && curExternal && symbol && ctx == globalContext && curExternal.symbol && symbol.id > curExternal.symbol.idCode && symbol.pointerExternal)
6648    {
6649       if(symbol.pointerExternal.type == functionExternal)
6650       {
6651          FunctionDefinition function = symbol.pointerExternal.function;
6652
6653          // Modified this recently...
6654          Context tmpContext = curContext;
6655          curContext = null;
6656          symbol.pointerExternal = MkExternalDeclaration(MkDeclaration(CopyList(function.specifiers, CopySpecifier), MkListOne(MkInitDeclarator(CopyDeclarator(function.declarator), null))));
6657          curContext = tmpContext;
6658
6659          symbol.pointerExternal.symbol = symbol;
6660
6661          // TESTING THIS:
6662          DeclareType(symbol.type, true, true);
6663
6664          ast->Insert(curExternal.prev, symbol.pointerExternal);
6665
6666          symbol.id = curExternal.symbol.idCode;
6667
6668       }
6669       else if(symbol.pointerExternal.type == declarationExternal && curExternal.symbol.idCode < symbol.pointerExternal.symbol.id) // Added id comparison because Global Function prototypes were broken
6670       {
6671          ast->Move(symbol.pointerExternal, curExternal.prev);
6672          symbol.id = curExternal.symbol.idCode;
6673       }
6674    }
6675 #ifdef _DEBUG
6676    //findSymbolTotalTime += GetTime() - startTime;
6677 #endif
6678    return symbol;
6679 }
6680
6681 static void GetTypeSpecs(Type type, OldList * specs)
6682 {
6683    if(!type.isSigned && type.kind != intPtrType && type.kind != intSizeType) ListAdd(specs, MkSpecifier(UNSIGNED));
6684    switch(type.kind)
6685    {
6686       case classType:
6687       {
6688          if(type._class.registered)
6689          {
6690             if(!type._class.registered.dataType)
6691                type._class.registered.dataType = ProcessTypeString(type._class.registered.dataTypeString, false);
6692             GetTypeSpecs(type._class.registered.dataType, specs);
6693          }
6694          break;
6695       }
6696       case doubleType: ListAdd(specs, MkSpecifier(DOUBLE)); break;
6697       case floatType: ListAdd(specs, MkSpecifier(FLOAT)); break;
6698       case charType: ListAdd(specs, MkSpecifier(CHAR)); break;
6699       case _BoolType: ListAdd(specs, MkSpecifier(_BOOL)); break;
6700       case shortType: ListAdd(specs, MkSpecifier(SHORT)); break;
6701       case int64Type: ListAdd(specs, MkSpecifier(INT64)); break;
6702       case intPtrType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intptr" : "uintptr")); break;
6703       case intSizeType: ListAdd(specs, MkSpecifierName(type.isSigned ? "intsize" : "uintsize")); break;
6704       case intType:
6705       default:
6706          ListAdd(specs, MkSpecifier(INT)); break;
6707    }
6708 }
6709
6710 static void PrintArraySize(Type arrayType, char * string)
6711 {
6712    char size[256];
6713    size[0] = '\0';
6714    strcat(size, "[");
6715    if(arrayType.enumClass)
6716       strcat(size, arrayType.enumClass.string);
6717    else if(arrayType.arraySizeExp)
6718       PrintExpression(arrayType.arraySizeExp, size);
6719    strcat(size, "]");
6720    strcat(string, size);
6721 }
6722
6723 // WARNING : This function expects a null terminated string since it recursively concatenate...
6724 static void PrintTypeSpecs(Type type, char * string, bool fullName, bool printConst)
6725 {
6726    if(type)
6727    {
6728       if(printConst && type.constant)
6729          strcat(string, "const ");
6730       switch(type.kind)
6731       {
6732          case classType:
6733          {
6734             Symbol c = type._class;
6735             // TODO: typed_object does not fully qualify the type, as it may have taken up an actual class (Stored in _class) from overriding
6736             //       look into merging with thisclass ?
6737             if(type.classObjectType == typedObject)
6738                strcat(string, "typed_object");
6739             else if(type.classObjectType == anyObject)
6740                strcat(string, "any_object");
6741             else
6742             {
6743                if(c && c.string)
6744                   strcat(string, (fullName || !c.registered) ? c.string : c.registered.name);
6745             }
6746             if(type.byReference)
6747                strcat(string, " &");
6748             break;
6749          }
6750          case voidType: strcat(string, "void"); break;
6751          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
6752          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
6753          case intPtrType:  strcat(string, type.isSigned ? "intptr" : "uintptr"); break;
6754          case intSizeType:  strcat(string, type.isSigned ? "intsize" : "uintsize"); break;
6755          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
6756          case _BoolType: strcat(string, "_Bool"); break;
6757          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
6758          case floatType: strcat(string, "float"); break;
6759          case doubleType: strcat(string, "double"); break;
6760          case structType:
6761             if(type.enumName)
6762             {
6763                strcat(string, "struct ");
6764                strcat(string, type.enumName);
6765             }
6766             else if(type.typeName)
6767                strcat(string, type.typeName);
6768             else
6769             {
6770                Type member;
6771                strcat(string, "struct { ");
6772                for(member = type.members.first; member; member = member.next)
6773                {
6774                   PrintType(member, string, true, fullName);
6775                   strcat(string,"; ");
6776                }
6777                strcat(string,"}");
6778             }
6779             break;
6780          case unionType:
6781             if(type.enumName)
6782             {
6783                strcat(string, "union ");
6784                strcat(string, type.enumName);
6785             }
6786             else if(type.typeName)
6787                strcat(string, type.typeName);
6788             else
6789             {
6790                strcat(string, "union ");
6791                strcat(string,"(unnamed)");
6792             }
6793             break;
6794          case enumType:
6795             if(type.enumName)
6796             {
6797                strcat(string, "enum ");
6798                strcat(string, type.enumName);
6799             }
6800             else if(type.typeName)
6801                strcat(string, type.typeName);
6802             else
6803                strcat(string, "int"); // "enum");
6804             break;
6805          case ellipsisType:
6806             strcat(string, "...");
6807             break;
6808          case subClassType:
6809             strcat(string, "subclass(");
6810             strcat(string, type._class ? type._class.string : "int");
6811             strcat(string, ")");
6812             break;
6813          case templateType:
6814             strcat(string, type.templateParameter.identifier.string);
6815             break;
6816          case thisClassType:
6817             strcat(string, "thisclass");
6818             break;
6819          case vaListType:
6820             strcat(string, "__builtin_va_list");
6821             break;
6822       }
6823    }
6824 }
6825
6826 static void PrintName(Type type, char * string, bool fullName)
6827 {
6828    if(type.name && type.name[0])
6829    {
6830       if(fullName)
6831          strcat(string, type.name);
6832       else
6833       {
6834          char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
6835          if(name) name += 2; else name = type.name;
6836          strcat(string, name);
6837       }
6838    }
6839 }
6840
6841 static void PrintAttribs(Type type, char * string)
6842 {
6843    if(type)
6844    {
6845       if(type.dllExport)   strcat(string, "dllexport ");
6846       if(type.attrStdcall) strcat(string, "stdcall ");
6847    }
6848 }
6849
6850 static void PrePrintType(Type type, char * string, bool fullName, Type parentType, bool printConst)
6851 {
6852    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6853    {
6854       Type attrType = null;
6855       if((type.kind == functionType || type.kind == methodType) && (!parentType || parentType.kind != pointerType))
6856          PrintAttribs(type, string);
6857       if(printConst && type.constant && (type.kind == functionType || type.kind == methodType))
6858          strcat(string, " const");
6859       PrePrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName, type, printConst);
6860       if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6861          strcat(string, " (");
6862       if(type.kind == pointerType)
6863       {
6864          if(type.type.kind == functionType || type.type.kind == methodType)
6865             PrintAttribs(type.type, string);
6866       }
6867       if(type.kind == pointerType)
6868       {
6869          if(type.type.kind == functionType || type.type.kind == methodType || type.type.kind == arrayType)
6870             strcat(string, "*");
6871          else
6872             strcat(string, " *");
6873       }
6874       if(printConst && type.constant && type.kind == pointerType)
6875          strcat(string, " const");
6876    }
6877    else
6878       PrintTypeSpecs(type, string, fullName, printConst);
6879 }
6880
6881 static void PostPrintType(Type type, char * string, bool fullName)
6882 {
6883    if(type.kind == pointerType && (type.type.kind == arrayType || type.type.kind == functionType || type.type.kind == methodType))
6884       strcat(string, ")");
6885    if(type.kind == arrayType)
6886       PrintArraySize(type, string);
6887    else if(type.kind == functionType)
6888    {
6889       Type param;
6890       strcat(string, "(");
6891       for(param = type.params.first; param; param = param.next)
6892       {
6893          PrintType(param, string, true, fullName);
6894          if(param.next) strcat(string, ", ");
6895       }
6896       strcat(string, ")");
6897    }
6898    if(type.kind == arrayType || type.kind == pointerType || type.kind == functionType || type.kind == methodType)
6899       PostPrintType(type.kind == methodType ? type.method.dataType : type.type, string, fullName);
6900 }
6901
6902 // *****
6903 // TODO: Add a max buffer size to avoid overflows. This function is used with static size char arrays.
6904 // *****
6905 static void _PrintType(Type type, char * string, bool printName, bool fullName, bool printConst)
6906 {
6907    PrePrintType(type, string, fullName, null, printConst);
6908
6909    if(type.thisClass || (printName && type.name && type.name[0]))
6910       strcat(string, " ");
6911    if(/*(type.kind == methodType || type.kind == functionType) && */(type.thisClass || type.staticMethod))
6912    {
6913       Symbol _class = type.thisClass;
6914       if((type.classObjectType == typedObject || type.classObjectType == classPointer) || (_class && !strcmp(_class.string, "class")))
6915       {
6916          if(type.classObjectType == classPointer)
6917             strcat(string, "class");
6918          else
6919             strcat(string, type.byReference ? "typed_object&" : "typed_object");
6920       }
6921       else if(_class && _class.string)
6922       {
6923          String s = _class.string;
6924          if(fullName)
6925             strcat(string, s);
6926          else
6927          {
6928             char * name = RSearchString(s, "::", strlen(s), true, false);
6929             if(name) name += 2; else name = s;
6930             strcat(string, name);
6931          }
6932       }
6933       strcat(string, "::");
6934    }
6935
6936    if(printName && type.name)
6937       PrintName(type, string, fullName);
6938    PostPrintType(type, string, fullName);
6939    if(type.bitFieldCount)
6940    {
6941       char count[100];
6942       sprintf(count, ":%d", type.bitFieldCount);
6943       strcat(string, count);
6944    }
6945 }
6946
6947 void PrintType(Type type, char * string, bool printName, bool fullName)
6948 {
6949    _PrintType(type, string, printName, fullName, true);
6950 }
6951
6952 void PrintTypeNoConst(Type type, char * string, bool printName, bool fullName)
6953 {
6954    _PrintType(type, string, printName, fullName, false);
6955 }
6956
6957 static Type FindMember(Type type, char * string)
6958 {
6959    Type memberType;
6960    for(memberType = type.members.first; memberType; memberType = memberType.next)
6961    {
6962       if(!memberType.name)
6963       {
6964          Type subType = FindMember(memberType, string);
6965          if(subType)
6966             return subType;
6967       }
6968       else if(!strcmp(memberType.name, string))
6969          return memberType;
6970    }
6971    return null;
6972 }
6973
6974 Type FindMemberAndOffset(Type type, char * string, uint * offset)
6975 {
6976    Type memberType;
6977    for(memberType = type.members.first; memberType; memberType = memberType.next)
6978    {
6979       if(!memberType.name)
6980       {
6981          Type subType = FindMember(memberType, string);
6982          if(subType)
6983          {
6984             *offset += memberType.offset;
6985             return subType;
6986          }
6987       }
6988       else if(!strcmp(memberType.name, string))
6989       {
6990          *offset += memberType.offset;
6991          return memberType;
6992       }
6993    }
6994    return null;
6995 }
6996
6997 public bool GetParseError() { return parseError; }
6998
6999 Expression ParseExpressionString(char * expression)
7000 {
7001    parseError = false;
7002
7003    fileInput = TempFile { };
7004    fileInput.Write(expression, 1, strlen(expression));
7005    fileInput.Seek(0, start);
7006
7007    echoOn = false;
7008    parsedExpression = null;
7009    resetScanner();
7010    expression_yyparse();
7011    delete fileInput;
7012
7013    return parsedExpression;
7014 }
7015
7016 static bool ResolveIdWithClass(Expression exp, Class _class, bool skipIDClassCheck)
7017 {
7018    Identifier id = exp.identifier;
7019    Method method = null;
7020    Property prop = null;
7021    DataMember member = null;
7022    ClassProperty classProp = null;
7023
7024    if(_class && _class.type == enumClass)
7025    {
7026       NamedLink value = null;
7027       Class enumClass = eSystem_FindClass(privateModule, "enum");
7028       if(enumClass)
7029       {
7030          Class baseClass;
7031          for(baseClass = _class; baseClass && baseClass.type == ClassType::enumClass; baseClass = baseClass.base)
7032          {
7033             EnumClassData e = ACCESS_CLASSDATA(baseClass, enumClass);
7034             for(value = e.values.first; value; value = value.next)
7035             {
7036                if(!strcmp(value.name, id.string))
7037                   break;
7038             }
7039             if(value)
7040             {
7041                char constant[256];
7042
7043                FreeExpContents(exp);
7044
7045                exp.type = constantExp;
7046                exp.isConstant = true;
7047                if(!strcmp(baseClass.dataTypeString, "int"))
7048                   sprintf(constant, "%d",(int)value.data);
7049                else
7050                   sprintf(constant, "0x%X",(int)value.data);
7051                exp.constant = CopyString(constant);
7052                //for(;_class.base && _class.base.type != systemClass; _class = _class.base);
7053                exp.expType = MkClassType(baseClass.fullName);
7054                break;
7055             }
7056          }
7057       }
7058       if(value)
7059          return true;
7060    }
7061    if((method = eClass_FindMethod(_class, id.string, privateModule)))
7062    {
7063       ProcessMethodType(method);
7064       exp.expType = Type
7065       {
7066          refCount = 1;
7067          kind = methodType;
7068          method = method;
7069          // Crash here?
7070          // TOCHECK: Put it back to what it was...
7071          // methodClass = _class;
7072          methodClass = (skipIDClassCheck || (id && id._class)) ? _class : null;
7073       };
7074       //id._class = null;
7075       return true;
7076    }
7077    else if((prop = eClass_FindProperty(_class, id.string, privateModule)))
7078    {
7079       if(!prop.dataType)
7080          ProcessPropertyType(prop);
7081       exp.expType = prop.dataType;
7082       if(prop.dataType) prop.dataType.refCount++;
7083       return true;
7084    }
7085    else if((member = eClass_FindDataMember(_class, id.string, privateModule, null, null)))
7086    {
7087       if(!member.dataType)
7088          member.dataType = ProcessTypeString(member.dataTypeString, false);
7089       exp.expType = member.dataType;
7090       if(member.dataType) member.dataType.refCount++;
7091       return true;
7092    }
7093    else if((classProp = eClass_FindClassProperty(_class, id.string)))
7094    {
7095       if(!classProp.dataType)
7096          classProp.dataType = ProcessTypeString(classProp.dataTypeString, false);
7097
7098       if(classProp.constant)
7099       {
7100          FreeExpContents(exp);
7101
7102          exp.isConstant = true;
7103          if(classProp.dataType.kind == pointerType && classProp.dataType.type.kind == charType)
7104          {
7105             //char constant[256];
7106             exp.type = stringExp;
7107             exp.constant = QMkString((char *)classProp.Get(_class));
7108          }
7109          else
7110          {
7111             char constant[256];
7112             exp.type = constantExp;
7113             sprintf(constant, "%d", (int)classProp.Get(_class));
7114             exp.constant = CopyString(constant);
7115          }
7116       }
7117       else
7118       {
7119          // TO IMPLEMENT...
7120       }
7121
7122       exp.expType = classProp.dataType;
7123       if(classProp.dataType) classProp.dataType.refCount++;
7124       return true;
7125    }
7126    return false;
7127 }
7128
7129 static GlobalData ScanGlobalData(NameSpace nameSpace, char * name)
7130 {
7131    BinaryTree * tree = &nameSpace.functions;
7132    GlobalData data = (GlobalData)tree->FindString(name);
7133    NameSpace * child;
7134    if(!data)
7135    {
7136       for(child = (NameSpace *)nameSpace.nameSpaces.first; child; child = (NameSpace *)((BTNode)child).next)
7137       {
7138          data = ScanGlobalData(child, name);
7139          if(data)
7140             break;
7141       }
7142    }
7143    return data;
7144 }
7145
7146 static GlobalData FindGlobalData(char * name)
7147 {
7148    int start = 0, c;
7149    NameSpace * nameSpace;
7150    nameSpace = globalData;
7151    for(c = 0; name[c]; c++)
7152    {
7153       if(name[c] == '.' || (name[c] == ':' && name[c+1] == ':'))
7154       {
7155          NameSpace * newSpace;
7156          char * spaceName = new char[c - start + 1];
7157          strncpy(spaceName, name + start, c - start);
7158          spaceName[c-start] = '\0';
7159          newSpace = (NameSpace *)nameSpace->nameSpaces.FindString(spaceName);
7160          delete spaceName;
7161          if(!newSpace)
7162             return null;
7163          nameSpace = newSpace;
7164          if(name[c] == ':') c++;
7165          start = c+1;
7166       }
7167    }
7168    if(c - start)
7169    {
7170       return ScanGlobalData(nameSpace, name + start);
7171    }
7172    return null;
7173 }
7174
7175 static int definedExpStackPos;
7176 static void * definedExpStack[512];
7177
7178 // This function makes checkedExp equivalent to newExp, ending up freeing newExp
7179 void ReplaceExpContents(Expression checkedExp, Expression newExp)
7180 {
7181    Expression prev = checkedExp.prev, next = checkedExp.next;
7182
7183    FreeExpContents(checkedExp);
7184    FreeType(checkedExp.expType);
7185    FreeType(checkedExp.destType);
7186
7187    *checkedExp = *newExp;
7188
7189    delete newExp;
7190
7191    checkedExp.prev = prev;
7192    checkedExp.next = next;
7193 }
7194
7195 void ApplyAnyObjectLogic(Expression e)
7196 {
7197    Type destType = /*(e.destType && e.destType.kind == ellipsisType) ? ellipsisDestType : */e.destType;
7198 #ifdef _DEBUG
7199    char debugExpString[4096];
7200    debugExpString[0] = '\0';
7201    PrintExpression(e, debugExpString);
7202 #endif
7203
7204    if(destType && (/*destType.classObjectType == ClassObjectType::typedObject || */destType.classObjectType == anyObject))
7205    {
7206       //if(e.destType && e.destType.kind == ellipsisType) usedEllipsis = true;
7207       //ellipsisDestType = destType;
7208       if(e && e.expType)
7209       {
7210          Type type = e.expType;
7211          Class _class = null;
7212          //Type destType = e.destType;
7213
7214          if(type.kind == classType && type._class && type._class.registered)
7215          {
7216             _class = type._class.registered;
7217          }
7218          else if(type.kind == subClassType)
7219          {
7220             _class = FindClass("ecere::com::Class").registered;
7221          }
7222          else
7223          {
7224             char string[1024] = "";
7225             Symbol classSym;
7226
7227             PrintTypeNoConst(type, string, false, true);
7228             classSym = FindClass(string);
7229             if(classSym) _class = classSym.registered;
7230          }
7231
7232          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...
7233             (!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))) ||
7234             destType.byReference)))
7235          {
7236             if(!_class || strcmp(_class.fullName, "char *"))     // TESTING THIS WITH NEW String class...
7237             {
7238                Expression checkedExp = e, newExp;
7239
7240                while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7241                {
7242                   if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7243                   {
7244                      if(checkedExp.type == extensionCompoundExp)
7245                      {
7246                         checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7247                      }
7248                      else
7249                         checkedExp = checkedExp.list->last;
7250                   }
7251                   else if(checkedExp.type == castExp)
7252                      checkedExp = checkedExp.cast.exp;
7253                }
7254
7255                if(checkedExp && checkedExp.type == opExp && checkedExp.op.op == '*' && !checkedExp.op.exp1)
7256                {
7257                   newExp = checkedExp.op.exp2;
7258                   checkedExp.op.exp2 = null;
7259                   FreeExpContents(checkedExp);
7260
7261                   if(e.expType && e.expType.passAsTemplate)
7262                   {
7263                      char size[100];
7264                      ComputeTypeSize(e.expType);
7265                      sprintf(size, "%d", e.expType.size);
7266                      newExp = MkExpBrackets(MkListOne(MkExpOp(MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)),
7267                         MkDeclaratorPointer(MkPointer(null, null), null)), newExp), '+',
7268                            MkExpCall(MkExpIdentifier(MkIdentifier("__ENDIAN_PAD")), MkListOne(MkExpConstant(size))))));
7269                   }
7270
7271                   ReplaceExpContents(checkedExp, newExp);
7272                   e.byReference = true;
7273                }
7274                else if(!e.byReference || (_class && _class.type == noHeadClass))     // TESTING THIS HERE...
7275                {
7276                   Expression checkedExp, newExp;
7277
7278                   {
7279                      // TODO: Move code from debugTools.ec for hasAddress flag, this is just temporary
7280                      bool hasAddress =
7281                         e.type == identifierExp ||
7282                         (e.type == ExpressionType::memberExp && e.member.memberType == dataMember) ||
7283                         (e.type == ExpressionType::pointerExp && e.member.memberType == dataMember) ||
7284                         (e.type == opExp && !e.op.exp1 && e.op.op == '*') ||
7285                         e.type == indexExp;
7286
7287                      if(_class && _class.type != noHeadClass && _class.type != normalClass && _class.type != structClass && !hasAddress)
7288                      {
7289                         Context context = PushContext();
7290                         Declarator decl;
7291                         OldList * specs = MkList();
7292                         char typeString[1024];
7293                         Expression newExp { };
7294
7295                         typeString[0] = '\0';
7296                         *newExp = *e;
7297
7298                         //if(e.destType) e.destType.refCount++;
7299                         // if(exp.expType) exp.expType.refCount++;
7300                         newExp.prev = null;
7301                         newExp.next = null;
7302                         newExp.expType = null;
7303
7304                         PrintTypeNoConst(e.expType, typeString, false, true);
7305                         decl = SpecDeclFromString(typeString, specs, null);
7306                         newExp.destType = ProcessType(specs, decl);
7307
7308                         curContext = context;
7309
7310                         // We need a current compound for this
7311                         if(curCompound)
7312                         {
7313                            char name[100];
7314                            OldList * stmts = MkList();
7315                            e.type = extensionCompoundExp;
7316                            sprintf(name, "__internalValue%03X", internalValueCounter++);
7317                            if(!curCompound.compound.declarations)
7318                               curCompound.compound.declarations = MkList();
7319                            curCompound.compound.declarations->Insert(null, MkDeclaration(specs, MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(name)), null))));
7320                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(MkIdentifier(name)), '=', newExp))));
7321                            ListAdd(stmts, MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier(name)))));
7322                            e.compound = MkCompoundStmt(null, stmts);
7323                         }
7324                         else
7325                            printf("libec: compiler error, curCompound is null in ApplyAnyObjectLogic\n");
7326
7327                         /*
7328                         e.compound = MkCompoundStmt(
7329                            MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(
7330                               MkDeclaratorIdentifier(MkIdentifier("__internalValue")), MkInitializerAssignment(newExp))))),
7331
7332                            MkListOne(MkExpressionStmt(MkListOne(MkExpIdentifier(MkIdentifier("__internalValue"))))));
7333                         */
7334
7335                         {
7336                            Type type = e.destType;
7337                            e.destType = { };
7338                            CopyTypeInto(e.destType, type);
7339                            e.destType.refCount = 1;
7340                            e.destType.classObjectType = none;
7341                            FreeType(type);
7342                         }
7343
7344                         e.compound.compound.context = context;
7345                         PopContext(context);
7346                         curContext = context.parent;
7347                      }
7348                   }
7349
7350                   // TODO: INTEGRATE THIS WITH VERSION ABOVE WHICH WAS ADDED TO ENCOMPASS OTHER CASE (*pointer)
7351                   checkedExp = e;
7352                   while(((checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp) && checkedExp.list) || checkedExp.type == castExp)
7353                   {
7354                      if(checkedExp.type == bracketsExp || checkedExp.type == extensionExpressionExp || checkedExp.type == extensionCompoundExp)
7355                      {
7356                         if(checkedExp.type == extensionCompoundExp)
7357                         {
7358                            checkedExp = ((Statement)checkedExp.compound.compound.statements->last).expressions->last;
7359                         }
7360                         else
7361                            checkedExp = checkedExp.list->last;
7362                      }
7363                      else if(checkedExp.type == castExp)
7364                         checkedExp = checkedExp.cast.exp;
7365                   }
7366                   {
7367                      Expression operand { };
7368                      operand = *checkedExp;
7369                      checkedExp.destType = null;
7370                      checkedExp.expType = null;
7371                      checkedExp.Clear();
7372                      checkedExp.type = opExp;
7373                      checkedExp.op.op = '&';
7374                      checkedExp.op.exp1 = null;
7375                      checkedExp.op.exp2 = operand;
7376
7377                      //newExp = MkExpOp(null, '&', checkedExp);
7378                   }
7379                   //ReplaceExpContents(checkedExp, newExp);
7380                }
7381             }
7382          }
7383       }
7384    }
7385    {
7386       // If expression type is a simple class, make it an address
7387       // FixReference(e, true);
7388    }
7389 //#if 0
7390    if((!destType || destType.kind == ellipsisType || destType.kind == voidType) && e.expType && (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7391       (e.expType.byReference || (e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7392          (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass ) )))
7393    {
7394       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"))
7395       {
7396          return;  // LEAVE THIS CASE (typed_object & :: methods 's this) TO PASS 2 FOR NOW
7397       }
7398       else
7399       {
7400          Expression thisExp { };
7401
7402          *thisExp = *e;
7403          thisExp.prev = null;
7404          thisExp.next = null;
7405          e.Clear();
7406
7407          e.type = bracketsExp;
7408          e.list = MkListOne(MkExpOp(null, '*', thisExp.type == identifierExp ? thisExp : MkExpBrackets(MkListOne(thisExp))));
7409          if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && thisExp.expType._class.registered.type == noHeadClass)
7410             ((Expression)e.list->first).byReference = true;
7411
7412          /*if(thisExp.expType.kind == classType && thisExp.expType._class && thisExp.expType._class.registered && !strcmp(thisExp.expType._class.registered.name, "class"))
7413          {
7414             e.expType = thisExp.expType;
7415             e.expType.refCount++;
7416          }
7417          else*/
7418          {
7419             e.expType = { };
7420             CopyTypeInto(e.expType, thisExp.expType);
7421             e.expType.byReference = false;
7422             e.expType.refCount = 1;
7423
7424             if(e.expType.kind == classType && e.expType._class && e.expType._class.registered &&
7425                (e.expType._class.registered.type == bitClass || e.expType._class.registered.type == enumClass || e.expType._class.registered.type == unitClass))
7426             {
7427                e.expType.classObjectType = none;
7428             }
7429          }
7430       }
7431    }
7432 // TOFIX: Try this for a nice IDE crash!
7433 //#endif
7434    // The other way around
7435    else
7436 //#endif
7437    if(destType && e.expType &&
7438          //e.expType.kind == classType && e.expType._class && e.expType._class.registered && !strcmp(e.expType._class.registered.name, "class") &&
7439          (e.expType.classObjectType == anyObject || e.expType.classObjectType == typedObject) &&
7440          !destType.classObjectType && /*(destType.kind != pointerType || !destType.type || destType.type.kind != voidType) &&*/ destType.kind != voidType)
7441    {
7442       if(destType.kind == ellipsisType)
7443       {
7444          Compiler_Error($"Unspecified type\n");
7445       }
7446       else if(!(destType.truth && e.expType.kind == classType && e.expType._class && e.expType._class.registered && e.expType._class.registered.type == structClass))
7447       {
7448          bool byReference = e.expType.byReference;
7449          Expression thisExp { };
7450          Declarator decl;
7451          OldList * specs = MkList();
7452          char typeString[1024]; // Watch buffer overruns
7453          Type type;
7454          ClassObjectType backupClassObjectType;
7455          bool backupByReference;
7456
7457          if(e.expType.kind == classType && e.expType._class && e.expType._class.registered && strcmp(e.expType._class.registered.name, "class"))
7458             type = e.expType;
7459          else
7460             type = destType;
7461
7462          backupClassObjectType = type.classObjectType;
7463          backupByReference = type.byReference;
7464
7465          type.classObjectType = none;
7466          type.byReference = false;
7467
7468          typeString[0] = '\0';
7469          PrintType(type, typeString, false, true);
7470          decl = SpecDeclFromString(typeString, specs, null);
7471
7472          type.classObjectType = backupClassObjectType;
7473          type.byReference = backupByReference;
7474
7475          *thisExp = *e;
7476          thisExp.prev = null;
7477          thisExp.next = null;
7478          e.Clear();
7479
7480          if( ( type.kind == classType && type._class && type._class.registered &&
7481                    (type._class.registered.type == systemClass || type._class.registered.type == bitClass ||
7482                     type._class.registered.type == enumClass || type._class.registered.type == unitClass) ) ||
7483              (type.kind != pointerType && type.kind != intPtrType && type.kind != arrayType && type.kind != classType) ||
7484              (!destType.byReference && byReference && (destType.kind != pointerType || type.kind != pointerType)))
7485          {
7486             e.type = opExp;
7487             e.op.op = '*';
7488             e.op.exp1 = null;
7489             e.op.exp2 = MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), thisExp);
7490
7491             e.expType = { };
7492             CopyTypeInto(e.expType, type);
7493             e.expType.byReference = false;
7494             e.expType.refCount = 1;
7495          }
7496          else
7497          {
7498             e.type = castExp;
7499             e.cast.typeName = MkTypeName(specs, decl);
7500             e.cast.exp = thisExp;
7501             e.byReference = true;
7502             e.expType = type;
7503             type.refCount++;
7504          }
7505          e.destType = destType;
7506          destType.refCount++;
7507       }
7508    }
7509 }
7510
7511 void ProcessExpressionType(Expression exp)
7512 {
7513    bool unresolved = false;
7514    Location oldyylloc = yylloc;
7515    bool notByReference = false;
7516 #ifdef _DEBUG
7517    char debugExpString[4096];
7518    debugExpString[0] = '\0';
7519    PrintExpression(exp, debugExpString);
7520 #endif
7521    if(!exp || exp.expType)
7522       return;
7523
7524    //eSystem_Logf("%s\n", expString);
7525
7526    // Testing this here
7527    yylloc = exp.loc;
7528    switch(exp.type)
7529    {
7530       case identifierExp:
7531       {
7532          Identifier id = exp.identifier;
7533          if(!id || !topContext) return;
7534
7535          // DOING THIS LATER NOW...
7536          if(id._class && id._class.name)
7537          {
7538             id.classSym = id._class.symbol; // FindClass(id._class.name);
7539             /* TODO: Name Space Fix ups
7540             if(!id.classSym)
7541                id.nameSpace = eSystem_FindNameSpace(privateModule, id._class.name);
7542             */
7543          }
7544
7545          /* WHY WAS THIS COMMENTED OUT? if(!strcmp(id.string, "__thisModule"))
7546          {
7547             exp.expType = ProcessTypeString("Module", true);
7548             break;
7549          }
7550          else */if(strstr(id.string, "__ecereClass") == id.string)
7551          {
7552             exp.expType = ProcessTypeString("ecere::com::Class", true);
7553             break;
7554          }
7555          else if(id._class && (id.classSym || (id._class.name && !strcmp(id._class.name, "property"))))
7556          {
7557             // Added this here as well
7558             ReplaceClassMembers(exp, thisClass);
7559             if(exp.type != identifierExp)
7560             {
7561                ProcessExpressionType(exp);
7562                break;
7563             }
7564
7565             if(id.classSym && ResolveIdWithClass(exp, id.classSym.registered, false))
7566                break;
7567          }
7568          else
7569          {
7570             Symbol symbol = FindSymbol(id.string, curContext, topContext /*exp.destType ? topContext : globalContext*/, false, id._class && id._class.name == null);
7571             // Enums should be resolved here (Special pass in opExp to fix identifiers not seen as enum on the first pass)
7572             if(!symbol/* && exp.destType*/)
7573             {
7574                if(exp.destType && CheckExpressionType(exp, exp.destType, false))
7575                   break;
7576                else
7577                {
7578                   if(thisClass)
7579                   {
7580                      ReplaceClassMembers(exp, thisClass ? thisClass : currentClass);
7581                      if(exp.type != identifierExp)
7582                      {
7583                         ProcessExpressionType(exp);
7584                         break;
7585                      }
7586                   }
7587                   // Static methods called from inside the _class
7588                   else if(currentClass && !id._class)
7589                   {
7590                      if(ResolveIdWithClass(exp, currentClass, true))
7591                         break;
7592                   }
7593                   symbol = FindSymbol(id.string, topContext.parent, globalContext, false, id._class && id._class.name == null);
7594                }
7595             }
7596
7597             // If we manage to resolve this symbol
7598             if(symbol)
7599             {
7600                Type type = symbol.type;
7601                Class _class = (type && type.kind == classType && type._class) ? type._class.registered : null;
7602
7603                if(_class && !strcmp(id.string, "this") && !type.classObjectType)
7604                {
7605                   Context context = SetupTemplatesContext(_class);
7606                   type = ReplaceThisClassType(_class);
7607                   FinishTemplatesContext(context);
7608                   if(type) type.refCount = 0;   // We'll be incrementing it right below...
7609                }
7610
7611                FreeSpecifier(id._class);
7612                id._class = null;
7613                delete id.string;
7614                id.string = CopyString(symbol.string);
7615
7616                id.classSym = null;
7617                exp.expType = type;
7618                if(type)
7619                   type.refCount++;
7620                if(type && (type.kind == enumType || (_class && _class.type == enumClass)))
7621                   // Add missing cases here... enum Classes...
7622                   exp.isConstant = true;
7623
7624                // TOCHECK: Why was !strcmp(id.string, "this") commented out?
7625                if(symbol.isParam || !strcmp(id.string, "this"))
7626                {
7627                   if(_class && _class.type == structClass && !type.declaredWithStruct)
7628                      exp.byReference = true;
7629
7630                   //TESTING COMMENTING THIS OUT IN FAVOR OF ApplyAnyObjectLogic
7631                   /*if(type && _class && (type.classObjectType == typedObject || type.classObjectType == anyObject) &&
7632                      ((_class.type == unitClass || _class.type == enumClass || _class.type == bitClass) ||
7633                      (type.byReference && (_class.type == normalClass || _class.type == noHeadClass))))
7634                   {
7635                      Identifier id = exp.identifier;
7636                      exp.type = bracketsExp;
7637                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(id)));
7638                   }*/
7639                }
7640
7641                if(symbol.isIterator)
7642                {
7643                   if(symbol.isIterator == 3)
7644                   {
7645                      exp.type = bracketsExp;
7646                      exp.list = MkListOne(MkExpOp(null, '*', MkExpIdentifier(exp.identifier)));
7647                      ((Expression)exp.list->first).op.exp2.expType = exp.expType;
7648                      exp.expType = null;
7649                      ProcessExpressionType(exp);
7650                   }
7651                   else if(symbol.isIterator != 4)
7652                   {
7653                      exp.type = memberExp;
7654                      exp.member.exp = MkExpIdentifier(exp.identifier);
7655                      exp.member.exp.expType = exp.expType;
7656                      /*if(symbol.isIterator == 6)
7657                         exp.member.member = MkIdentifier("key");
7658                      else*/
7659                         exp.member.member = MkIdentifier("data");
7660                      exp.expType = null;
7661                      ProcessExpressionType(exp);
7662                   }
7663                }
7664                break;
7665             }
7666             else
7667             {
7668                DefinedExpression definedExp = null;
7669                if(thisNameSpace && !(id._class && !id._class.name))
7670                {
7671                   char name[1024];
7672                   strcpy(name, thisNameSpace);
7673                   strcat(name, "::");
7674                   strcat(name, id.string);
7675                   definedExp = eSystem_FindDefine(privateModule, name);
7676                }
7677                if(!definedExp)
7678                   definedExp = eSystem_FindDefine(privateModule, id.string);
7679                if(definedExp)
7680                {
7681                   int c;
7682                   for(c = 0; c<definedExpStackPos; c++)
7683                      if(definedExpStack[c] == definedExp)
7684                         break;
7685                   if(c == definedExpStackPos && c < sizeof(definedExpStack) / sizeof(void *))
7686                   {
7687                      Location backupYylloc = yylloc;
7688                      File backInput = fileInput;
7689                      definedExpStack[definedExpStackPos++] = definedExp;
7690
7691                      fileInput = TempFile { };
7692                      fileInput.Write(definedExp.value, 1, strlen(definedExp.value));
7693                      fileInput.Seek(0, start);
7694
7695                      echoOn = false;
7696                      parsedExpression = null;
7697                      resetScanner();
7698                      expression_yyparse();
7699                      delete fileInput;
7700                      if(backInput)
7701                         fileInput = backInput;
7702
7703                      yylloc = backupYylloc;
7704
7705                      if(parsedExpression)
7706                      {
7707                         FreeIdentifier(id);
7708                         exp.type = bracketsExp;
7709                         exp.list = MkListOne(parsedExpression);
7710                         parsedExpression.loc = yylloc;
7711                         ProcessExpressionType(exp);
7712                         definedExpStackPos--;
7713                         return;
7714                      }
7715                      definedExpStackPos--;
7716                   }
7717                   else
7718                   {
7719                      if(inCompiler)
7720                      {
7721                         Compiler_Error($"Recursion in defined expression %s\n", id.string);
7722                      }
7723                   }
7724                }
7725                else
7726                {
7727                   GlobalData data = null;
7728                   if(thisNameSpace && !(id._class && !id._class.name))
7729                   {
7730                      char name[1024];
7731                      strcpy(name, thisNameSpace);
7732                      strcat(name, "::");
7733                      strcat(name, id.string);
7734                      data = FindGlobalData(name);
7735                   }
7736                   if(!data)
7737                      data = FindGlobalData(id.string);
7738                   if(data)
7739                   {
7740                      DeclareGlobalData(data);
7741                      exp.expType = data.dataType;
7742                      if(data.dataType) data.dataType.refCount++;
7743
7744                      delete id.string;
7745                      id.string = CopyString(data.fullName);
7746                      FreeSpecifier(id._class);
7747                      id._class = null;
7748
7749                      break;
7750                   }
7751                   else
7752                   {
7753                      GlobalFunction function = null;
7754                      if(thisNameSpace && !(id._class && !id._class.name))
7755                      {
7756                         char name[1024];
7757                         strcpy(name, thisNameSpace);
7758                         strcat(name, "::");
7759                         strcat(name, id.string);
7760                         function = eSystem_FindFunction(privateModule, name);
7761                      }
7762                      if(!function)
7763                         function = eSystem_FindFunction(privateModule, id.string);
7764                      if(function)
7765                      {
7766                         char name[1024];
7767                         delete id.string;
7768                         id.string = CopyString(function.name);
7769                         name[0] = 0;
7770
7771                         if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
7772                            strcpy(name, "__ecereFunction_");
7773                         FullClassNameCat(name, id.string, false); // Why is this using FullClassNameCat ?
7774                         if(DeclareFunction(function, name))
7775                         {
7776                            delete id.string;
7777                            id.string = CopyString(name);
7778                         }
7779                         exp.expType = function.dataType;
7780                         if(function.dataType) function.dataType.refCount++;
7781
7782                         FreeSpecifier(id._class);
7783                         id._class = null;
7784
7785                         break;
7786                      }
7787                   }
7788                }
7789             }
7790          }
7791          unresolved = true;
7792          break;
7793       }
7794       case instanceExp:
7795       {
7796          Class _class;
7797          // Symbol classSym;
7798
7799          if(!exp.instance._class)
7800          {
7801             if(exp.destType && exp.destType.kind == classType && exp.destType._class)
7802             {
7803                exp.instance._class = MkSpecifierName(exp.destType._class.string);
7804             }
7805          }
7806
7807          //classSym = FindClass(exp.instance._class.fullName);
7808          //_class = classSym ? classSym.registered : null;
7809
7810          ProcessInstantiationType(exp.instance);
7811          exp.isConstant = exp.instance.isConstant;
7812
7813          /*
7814          if(_class.type == unitClass && _class.base.type != systemClass)
7815          {
7816             {
7817                Type destType = exp.destType;
7818
7819                exp.destType = MkClassType(_class.base.fullName);
7820                exp.expType = MkClassType(_class.fullName);
7821                CheckExpressionType(exp, exp.destType, true);
7822
7823                exp.destType = destType;
7824             }
7825             exp.expType = MkClassType(_class.fullName);
7826          }
7827          else*/
7828          if(exp.instance._class)
7829          {
7830             exp.expType = MkClassType(exp.instance._class.name);
7831             /*if(exp.expType._class && exp.expType._class.registered &&
7832                (exp.expType._class.registered.type == normalClass || exp.expType._class.registered.type == noHeadClass))
7833                exp.expType.byReference = true;*/
7834          }
7835          break;
7836       }
7837       case constantExp:
7838       {
7839          if(!exp.expType)
7840          {
7841             char * constant = exp.constant;
7842             Type type
7843             {
7844                refCount = 1;
7845                constant = true;
7846             };
7847             exp.expType = type;
7848
7849             if(constant[0] == '\'')
7850             {
7851                if((int)((byte *)constant)[1] > 127)
7852                {
7853                   int nb;
7854                   unichar ch = UTF8GetChar(constant + 1, &nb);
7855                   if(nb < 2) ch = constant[1];
7856                   delete constant;
7857                   exp.constant = PrintUInt(ch);
7858                   // type.kind = (ch > 0xFFFF) ? intType : shortType;
7859                   type.kind = classType; //(ch > 0xFFFF) ? intType : shortType;
7860                   type._class = FindClass("unichar");
7861
7862                   type.isSigned = false;
7863                }
7864                else
7865                {
7866                   type.kind = charType;
7867                   type.isSigned = true;
7868                }
7869             }
7870             else
7871             {
7872                char * dot = strchr(constant, '.');
7873                bool isHex = (constant[0] == '0' && (constant[1] == 'x' || constant[1] == 'X'));
7874                char * exponent;
7875                if(isHex)
7876                {
7877                   exponent = strchr(constant, 'p');
7878                   if(!exponent) exponent = strchr(constant, 'P');
7879                }
7880                else
7881                {
7882                   exponent = strchr(constant, 'e');
7883                   if(!exponent) exponent = strchr(constant, 'E');
7884                }
7885
7886                if(dot || exponent)
7887                {
7888                   if(strchr(constant, 'f') || strchr(constant, 'F'))
7889                      type.kind = floatType;
7890                   else
7891                      type.kind = doubleType;
7892                   type.isSigned = true;
7893                }
7894                else
7895                {
7896                   bool isSigned = constant[0] == '-';
7897                   char * endP = null;
7898                   int64 i64 = strtoll(constant, &endP, 0);
7899                   uint64 ui64 = strtoull(constant, &endP, 0);
7900                   bool is64Bit = endP && (!strcmp(endP, "LL") || !strcmp(endP, "ll"));
7901                   if(isSigned)
7902                   {
7903                      if(i64 < MININT)
7904                         is64Bit = true;
7905                   }
7906                   else
7907                   {
7908                      if(ui64 > MAXINT)
7909                      {
7910                         if(ui64 > MAXDWORD)
7911                         {
7912                            is64Bit = true;
7913                            if(ui64 <= MAXINT64 && (constant[0] != '0' || !constant[1]))
7914                               isSigned = true;
7915                         }
7916                      }
7917                      else if(constant[0] != '0' || !constant[1])
7918                         isSigned = true;
7919                   }
7920                   type.kind = is64Bit ? int64Type : intType;
7921                   type.isSigned = isSigned;
7922                }
7923             }
7924             exp.isConstant = true;
7925             if(exp.destType && exp.destType.kind == doubleType)
7926                type.kind = doubleType;
7927             else if(exp.destType && exp.destType.kind == floatType)
7928                type.kind = floatType;
7929             else if(exp.destType && exp.destType.kind == int64Type)
7930                type.kind = int64Type;
7931          }
7932          break;
7933       }
7934       case stringExp:
7935       {
7936          exp.isConstant = true;      // Why wasn't this constant?
7937          exp.expType = Type
7938          {
7939             refCount = 1;
7940             kind = pointerType;
7941             type = Type
7942             {
7943                refCount = 1;
7944                kind = charType;
7945                constant = true;
7946                isSigned = true;
7947             }
7948          };
7949          break;
7950       }
7951       case newExp:
7952       case new0Exp:
7953          ProcessExpressionType(exp._new.size);
7954          exp.expType = Type
7955          {
7956             refCount = 1;
7957             kind = pointerType;
7958             type = ProcessType(exp._new.typeName.qualifiers, exp._new.typeName.declarator);
7959          };
7960          DeclareType(exp.expType.type, false, false);
7961          break;
7962       case renewExp:
7963       case renew0Exp:
7964          ProcessExpressionType(exp._renew.size);
7965          ProcessExpressionType(exp._renew.exp);
7966          exp.expType = Type
7967          {
7968             refCount = 1;
7969             kind = pointerType;
7970             type = ProcessType(exp._renew.typeName.qualifiers, exp._renew.typeName.declarator);
7971          };
7972          DeclareType(exp.expType.type, false, false);
7973          break;
7974       case opExp:
7975       {
7976          bool assign = false, boolResult = false, boolOps = false;
7977          Type type1 = null, type2 = null;
7978          bool useDestType = false, useSideType = false;
7979          Location oldyylloc = yylloc;
7980          bool useSideUnit = false;
7981
7982          // Dummy type to prevent ProcessExpression of operands to say unresolved identifiers yet
7983          Type dummy
7984          {
7985             count = 1;
7986             refCount = 1;
7987          };
7988
7989          switch(exp.op.op)
7990          {
7991             // Assignment Operators
7992             case '=':
7993             case MUL_ASSIGN:
7994             case DIV_ASSIGN:
7995             case MOD_ASSIGN:
7996             case ADD_ASSIGN:
7997             case SUB_ASSIGN:
7998             case LEFT_ASSIGN:
7999             case RIGHT_ASSIGN:
8000             case AND_ASSIGN:
8001             case XOR_ASSIGN:
8002             case OR_ASSIGN:
8003                assign = true;
8004                break;
8005             // boolean Operators
8006             case '!':
8007                // Expect boolean operators
8008                //boolOps = true;
8009                //boolResult = true;
8010                break;
8011             case AND_OP:
8012             case OR_OP:
8013                // Expect boolean operands
8014                boolOps = true;
8015                boolResult = true;
8016                break;
8017             // Comparisons
8018             case EQ_OP:
8019             case '<':
8020             case '>':
8021             case LE_OP:
8022             case GE_OP:
8023             case NE_OP:
8024                // Gives boolean result
8025                boolResult = true;
8026                useSideType = true;
8027                break;
8028             case '+':
8029             case '-':
8030                useSideUnit = true;
8031
8032                // Just added these... testing
8033             case '|':
8034             case '&':
8035             case '^':
8036
8037             // DANGER: Verify units
8038             case '/':
8039             case '%':
8040             case '*':
8041
8042                if(exp.op.op != '*' || exp.op.exp1)
8043                {
8044                   useSideType = true;
8045                   useDestType = true;
8046                }
8047                break;
8048
8049             /*// Implement speed etc.
8050             case '*':
8051             case '/':
8052                break;
8053             */
8054          }
8055          if(exp.op.op == '&')
8056          {
8057             // Added this here earlier for Iterator address as key
8058             if(!exp.op.exp1 && exp.op.exp2 && exp.op.exp2.type == identifierExp && exp.op.exp2.identifier)
8059             {
8060                Identifier id = exp.op.exp2.identifier;
8061                Symbol symbol = FindSymbol(id.string, curContext, topContext, false, id._class && id._class.name == null);
8062                if(symbol && symbol.isIterator == 2)
8063                {
8064                   exp.type = memberExp;
8065                   exp.member.exp = exp.op.exp2;
8066                   exp.member.member = MkIdentifier("key");
8067                   exp.expType = null;
8068                   exp.op.exp2.expType = symbol.type;
8069                   symbol.type.refCount++;
8070                   ProcessExpressionType(exp);
8071                   FreeType(dummy);
8072                   break;
8073                }
8074                // exp.op.exp2.usage.usageRef = true;
8075             }
8076          }
8077
8078          //dummy.kind = TypeDummy;
8079
8080          if(exp.op.exp1)
8081          {
8082             if(exp.destType && exp.destType.kind == classType &&
8083                exp.destType._class && exp.destType._class.registered && useDestType &&
8084
8085               ((exp.destType._class.registered.type == unitClass && useSideUnit) ||
8086                exp.destType._class.registered.type == enumClass ||
8087                exp.destType._class.registered.type == bitClass
8088                ))
8089
8090               //(exp.destType._class.registered.type == unitClass || exp.destType._class.registered.type == enumClass) && useDestType)
8091             {
8092                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8093                exp.op.exp1.destType = exp.destType;
8094                if(exp.destType)
8095                   exp.destType.refCount++;
8096             }
8097             else if(!assign)
8098             {
8099                if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8100                exp.op.exp1.destType = dummy;
8101                dummy.refCount++;
8102             }
8103
8104             // TESTING THIS HERE...
8105             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count++;
8106             ProcessExpressionType(exp.op.exp1);
8107             if(exp.op.exp1.destType && exp.op.op != '=') exp.op.exp1.destType.count--;
8108
8109             if(exp.op.exp1.destType == dummy)
8110             {
8111                FreeType(dummy);
8112                exp.op.exp1.destType = null;
8113             }
8114             type1 = exp.op.exp1.expType;
8115          }
8116
8117          if(exp.op.exp2)
8118          {
8119             char expString[10240];
8120             expString[0] = '\0';
8121             if(exp.op.exp2.type == instanceExp && !exp.op.exp2.instance._class)
8122             {
8123                if(exp.op.exp1)
8124                {
8125                   exp.op.exp2.destType = exp.op.exp1.expType;
8126                   if(exp.op.exp1.expType)
8127                      exp.op.exp1.expType.refCount++;
8128                }
8129                else
8130                {
8131                   exp.op.exp2.destType = exp.destType;
8132                   if(exp.destType)
8133                      exp.destType.refCount++;
8134                }
8135
8136                if(type1) type1.refCount++;
8137                exp.expType = type1;
8138             }
8139             else if(assign)
8140             {
8141                if(inCompiler)
8142                   PrintExpression(exp.op.exp2, expString);
8143
8144                if(type1 && type1.kind == pointerType)
8145                {
8146                   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 ||
8147                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN)
8148                      Compiler_Error($"operator %s illegal on pointer\n", exp.op.op);
8149                   else if(exp.op.op == '=')
8150                   {
8151                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8152                      exp.op.exp2.destType = type1;
8153                      if(type1)
8154                         type1.refCount++;
8155                   }
8156                }
8157                else
8158                {
8159                   // Don't convert to the type for those... (e.g.: Degrees a; a /= 2;)
8160                   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/* ||
8161                      exp.op.op == AND_ASSIGN || exp.op.op == OR_ASSIGN*/);
8162                   else
8163                   {
8164                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8165                      exp.op.exp2.destType = type1;
8166                      if(type1)
8167                         type1.refCount++;
8168                   }
8169                }
8170                if(type1) type1.refCount++;
8171                exp.expType = type1;
8172             }
8173             else if(exp.destType && exp.destType.kind == classType &&
8174                exp.destType._class && exp.destType._class.registered &&
8175
8176                   ((exp.destType._class.registered.type == unitClass && useDestType && useSideUnit) ||
8177                   (exp.destType._class.registered.type == enumClass && useDestType))
8178                   )
8179             {
8180                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8181                exp.op.exp2.destType = exp.destType;
8182                if(exp.destType)
8183                   exp.destType.refCount++;
8184             }
8185             else
8186             {
8187                if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8188                exp.op.exp2.destType = dummy;
8189                dummy.refCount++;
8190             }
8191
8192             // TESTING THIS HERE... (DANGEROUS)
8193             if(type1 && boolResult && useSideType && type1.kind == classType && type1._class && type1._class.registered &&
8194                (type1._class.registered.type == bitClass || type1._class.registered.type == enumClass))
8195             {
8196                FreeType(exp.op.exp2.destType);
8197                exp.op.exp2.destType = type1;
8198                type1.refCount++;
8199             }
8200             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count++;
8201             // Cannot lose the cast on a sizeof
8202             if(exp.op.op == SIZEOF)
8203             {
8204                Expression e = exp.op.exp2;
8205                while((e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp) && e.list)
8206                {
8207                   if(e.type == bracketsExp || e.type == extensionExpressionExp || e.type == extensionCompoundExp)
8208                   {
8209                      if(e.type == extensionCompoundExp)
8210                         e = ((Statement)e.compound.compound.statements->last).expressions->last;
8211                      else
8212                         e = e.list->last;
8213                   }
8214                }
8215                if(e.type == castExp && e.cast.exp)
8216                   e.cast.exp.needCast = true;
8217             }
8218             ProcessExpressionType(exp.op.exp2);
8219             if(exp.op.exp2.destType && exp.op.op != '=') exp.op.exp2.destType.count--;
8220
8221             if(assign && type1 && type1.kind == pointerType && exp.op.exp2.expType)
8222             {
8223                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)
8224                {
8225                   if(exp.op.op != '=' && type1.type.kind == voidType)
8226                      Compiler_Error($"void *: unknown size\n");
8227                }
8228                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||
8229                            (type1.type.kind == voidType && exp.op.exp2.expType.kind == classType && exp.op.exp2.expType._class.registered &&
8230                               (exp.op.exp2.expType._class.registered.type == normalClass ||
8231                               exp.op.exp2.expType._class.registered.type == structClass ||
8232                               exp.op.exp2.expType._class.registered.type == noHeadClass)))
8233                {
8234                   if(exp.op.op == ADD_ASSIGN)
8235                      Compiler_Error($"cannot add two pointers\n");
8236                }
8237                else if((exp.op.exp2.expType.kind == classType && type1.kind == pointerType && type1.type.kind == classType &&
8238                   type1.type._class == exp.op.exp2.expType._class && exp.op.exp2.expType._class.registered && exp.op.exp2.expType._class.registered.type == structClass))
8239                {
8240                   if(exp.op.op == ADD_ASSIGN)
8241                      Compiler_Error($"cannot add two pointers\n");
8242                }
8243                else if(inCompiler)
8244                {
8245                   char type1String[1024];
8246                   char type2String[1024];
8247                   type1String[0] = '\0';
8248                   type2String[0] = '\0';
8249
8250                   PrintType(exp.op.exp2.expType, type1String, false, true);
8251                   PrintType(type1, type2String, false, true);
8252                   ChangeCh(expString, '\n', ' ');
8253                   Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1String, type2String);
8254                }
8255             }
8256
8257             if(exp.op.exp2.destType == dummy)
8258             {
8259                FreeType(dummy);
8260                exp.op.exp2.destType = null;
8261             }
8262
8263             if(exp.op.op == '-' && !exp.op.exp1 && exp.op.exp2.expType && !exp.op.exp2.expType.isSigned)
8264             {
8265                type2 = { };
8266                type2.refCount = 1;
8267                CopyTypeInto(type2, exp.op.exp2.expType);
8268                type2.isSigned = true;
8269             }
8270             else if(exp.op.op == '~' && !exp.op.exp1 && exp.op.exp2.expType && (!exp.op.exp2.expType.isSigned || exp.op.exp2.expType.kind != intType))
8271             {
8272                type2 = { kind = intType };
8273                type2.refCount = 1;
8274                type2.isSigned = true;
8275             }
8276             else
8277             {
8278                type2 = exp.op.exp2.expType;
8279                if(type2) type2.refCount++;
8280             }
8281          }
8282
8283          dummy.kind = voidType;
8284
8285          if(exp.op.op == SIZEOF)
8286          {
8287             exp.expType = Type
8288             {
8289                refCount = 1;
8290                kind = intType;
8291             };
8292             exp.isConstant = true;
8293          }
8294          // Get type of dereferenced pointer
8295          else if(exp.op.op == '*' && !exp.op.exp1)
8296          {
8297             exp.expType = Dereference(type2);
8298             if(type2 && type2.kind == classType)
8299                notByReference = true;
8300          }
8301          else if(exp.op.op == '&' && !exp.op.exp1)
8302             exp.expType = Reference(type2);
8303          else if(!assign)
8304          {
8305             if(boolOps)
8306             {
8307                if(exp.op.exp1)
8308                {
8309                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8310                   exp.op.exp1.destType = MkClassType("bool");
8311                   exp.op.exp1.destType.truth = true;
8312                   if(!exp.op.exp1.expType)
8313                      ProcessExpressionType(exp.op.exp1);
8314                   else
8315                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8316                   FreeType(exp.op.exp1.expType);
8317                   exp.op.exp1.expType = MkClassType("bool");
8318                   exp.op.exp1.expType.truth = true;
8319                }
8320                if(exp.op.exp2)
8321                {
8322                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8323                   exp.op.exp2.destType = MkClassType("bool");
8324                   exp.op.exp2.destType.truth = true;
8325                   if(!exp.op.exp2.expType)
8326                      ProcessExpressionType(exp.op.exp2);
8327                   else
8328                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8329                   FreeType(exp.op.exp2.expType);
8330                   exp.op.exp2.expType = MkClassType("bool");
8331                   exp.op.exp2.expType.truth = true;
8332                }
8333             }
8334             else if(exp.op.exp1 && exp.op.exp2 &&
8335                ((useSideType /*&&
8336                      (useSideUnit ||
8337                         ((!type1 || type1.kind != classType || type1._class.registered.type != unitClass) &&
8338                          (!type2 || type2.kind != classType || type2._class.registered.type != unitClass)))*/) ||
8339                   ((!type1 || type1.kind != classType || !strcmp(type1._class.string, "String")) &&
8340                   (!type2 || type2.kind != classType || !strcmp(type2._class.string, "String")))))
8341             {
8342                if(type1 && type2 &&
8343                   // If either both are class or both are not class
8344                   ((type1.kind == classType && type1._class && strcmp(type1._class.string, "String")) == (type2.kind == classType && type2._class && strcmp(type2._class.string, "String"))))
8345                {
8346                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8347                   exp.op.exp2.destType = type1;
8348                   type1.refCount++;
8349                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8350                   exp.op.exp1.destType = type2;
8351                   type2.refCount++;
8352                   // Warning here for adding Radians + Degrees with no destination type
8353                   if(!boolResult && type1.kind == classType && (!exp.destType || exp.destType.kind != classType) &&
8354                      type1._class.registered && type1._class.registered.type == unitClass &&
8355                      type2._class.registered && type2._class.registered.type == unitClass &&
8356                      type1._class.registered != type2._class.registered)
8357                      Compiler_Warning($"operating on %s and %s with an untyped result, assuming %s\n",
8358                         type1._class.string, type2._class.string, type1._class.string);
8359
8360                   if(type1.kind == pointerType && type1.type.kind == templateType && type2.kind != pointerType)
8361                   {
8362                      Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8363                      if(argExp)
8364                      {
8365                         Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8366
8367                         exp.op.exp1 = MkExpBrackets(MkListOne(MkExpCast(
8368                            MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)),
8369                            exp.op.exp1)));
8370
8371                         ProcessExpressionType(exp.op.exp1);
8372
8373                         if(type2.kind != pointerType)
8374                         {
8375                            ProcessExpressionType(classExp);
8376
8377                            exp.op.exp2 = MkExpBrackets(MkListOne(MkExpOp(exp.op.exp2, '*',
8378                               // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8379                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8380                                  // noHeadClass
8381                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("5")),
8382                                     OR_OP,
8383                                  // normalClass
8384                                  MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpConstant("0"))))),
8385                                     MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8386                                        MkPointer(null, null), null)))),
8387                                        MkExpMember(classExp, MkIdentifier("typeSize"))))))));
8388
8389                            if(!exp.op.exp2.expType)
8390                            {
8391                               if(type2)
8392                                  FreeType(type2);
8393                               type2 = exp.op.exp2.expType = ProcessTypeString("int", false);
8394                               type2.refCount++;
8395                            }
8396
8397                            ProcessExpressionType(exp.op.exp2);
8398                         }
8399                      }
8400                   }
8401
8402                   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)))
8403                   {
8404                      if(type1.kind != classType && type1.type.kind == voidType)
8405                         Compiler_Error($"void *: unknown size\n");
8406                      exp.expType = type1;
8407                      if(type1) type1.refCount++;
8408                   }
8409                   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)))
8410                   {
8411                      if(type2.kind != classType && type2.type.kind == voidType)
8412                         Compiler_Error($"void *: unknown size\n");
8413                      exp.expType = type2;
8414                      if(type2) type2.refCount++;
8415                   }
8416                   else if((type1.kind == pointerType && type2.kind != pointerType && type2.kind != arrayType && type2.kind != functionType && type2.kind != methodType && type2.kind != classType && type2.kind != subClassType) ||
8417                           (type2.kind == pointerType && type1.kind != pointerType && type1.kind != arrayType && type1.kind != functionType && type1.kind != methodType && type1.kind != classType && type1.kind != subClassType))
8418                   {
8419                      Compiler_Warning($"different levels of indirection\n");
8420                   }
8421                   else
8422                   {
8423                      bool success = false;
8424                      if(type1.kind == pointerType && type2.kind == pointerType)
8425                      {
8426                         if(exp.op.op == '+')
8427                            Compiler_Error($"cannot add two pointers\n");
8428                         else if(exp.op.op == '-')
8429                         {
8430                            // Pointer Subtraction gives integer
8431                            if(MatchTypes(type1.type, type2.type, null, null, null, false, false, false, false))
8432                            {
8433                               exp.expType = Type
8434                               {
8435                                  kind = intType;
8436                                  refCount = 1;
8437                               };
8438                               success = true;
8439
8440                               if(type1.type.kind == templateType)
8441                               {
8442                                  Expression argExp = GetTemplateArgExp(type1.type.templateParameter, thisClass, true);
8443                                  if(argExp)
8444                                  {
8445                                     Expression classExp = MkExpMember(argExp, MkIdentifier("dataTypeClass"));
8446
8447                                     ProcessExpressionType(classExp);
8448
8449                                     exp.type = bracketsExp;
8450                                     exp.list = MkListOne(MkExpOp(
8451                                        MkExpBrackets(MkListOne(MkExpOp(
8452                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp1)))
8453                                              , exp.op.op,
8454                                              MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), MkExpBrackets(MkListOne(exp.op.exp2)))))), '/',
8455
8456                                              //MkExpMember(classExp, MkIdentifier("typeSize"))
8457
8458                                              // ((_class.type == noHeadClass || _class.type == normalClass) ? sizeof(void *) : type.size)
8459                                              MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(MkExpOp(
8460                                                 // noHeadClass
8461                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("noHeadClass"))),
8462                                                    OR_OP,
8463                                                 // normalClass
8464                                                 MkExpOp(MkExpMember(CopyExpression(classExp), MkIdentifier("type")), EQ_OP, MkExpIdentifier(MkIdentifier("normalClass")))))),
8465                                                    MkListOne(MkExpTypeSize(MkTypeName(MkListOne(MkSpecifier(VOID)), MkDeclaratorPointer(
8466                                                       MkPointer(null, null), null)))),
8467                                                       MkExpMember(classExp, MkIdentifier("typeSize")))))
8468
8469
8470                                              ));
8471
8472                                     ProcessExpressionType(((Expression)exp.list->first).op.exp2);
8473                                     FreeType(dummy);
8474                                     return;
8475                                  }
8476                               }
8477                            }
8478                         }
8479                      }
8480
8481                      if(!success && exp.op.exp1.type == constantExp)
8482                      {
8483                         // If first expression is constant, try to match that first
8484                         if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8485                         {
8486                            if(exp.expType) FreeType(exp.expType);
8487                            exp.expType = exp.op.exp1.destType;
8488                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8489                            success = true;
8490                         }
8491                         else if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8492                         {
8493                            if(exp.expType) FreeType(exp.expType);
8494                            exp.expType = exp.op.exp2.destType;
8495                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8496                            success = true;
8497                         }
8498                      }
8499                      else if(!success)
8500                      {
8501                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8502                         {
8503                            if(exp.expType) FreeType(exp.expType);
8504                            exp.expType = exp.op.exp2.destType;
8505                            if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8506                            success = true;
8507                         }
8508                         else if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8509                         {
8510                            if(exp.expType) FreeType(exp.expType);
8511                            exp.expType = exp.op.exp1.destType;
8512                            if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8513                            success = true;
8514                         }
8515                      }
8516                      if(!success)
8517                      {
8518                         char expString1[10240];
8519                         char expString2[10240];
8520                         char type1[1024];
8521                         char type2[1024];
8522                         expString1[0] = '\0';
8523                         expString2[0] = '\0';
8524                         type1[0] = '\0';
8525                         type2[0] = '\0';
8526                         if(inCompiler)
8527                         {
8528                            PrintExpression(exp.op.exp1, expString1);
8529                            ChangeCh(expString1, '\n', ' ');
8530                            PrintExpression(exp.op.exp2, expString2);
8531                            ChangeCh(expString2, '\n', ' ');
8532                            PrintType(exp.op.exp1.expType, type1, false, true);
8533                            PrintType(exp.op.exp2.expType, type2, false, true);
8534                         }
8535
8536                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1, expString2, type2);
8537                      }
8538                   }
8539                }
8540                // ADDED THESE TWO FROM OUTSIDE useSideType CHECK
8541                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type2 && type1 && type2.kind == classType && type1.kind != classType && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8542                {
8543                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8544                   // Convert e.g. / 4 into / 4.0
8545                   exp.op.exp1.destType = type2._class.registered.dataType;
8546                   if(type2._class.registered.dataType)
8547                      type2._class.registered.dataType.refCount++;
8548                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8549                   exp.expType = type2;
8550                   if(type2) type2.refCount++;
8551                }
8552                else if(!boolResult && (!useSideUnit /*|| exp.destType*/) && type1 && type2 && type1.kind == classType && type2.kind != classType && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8553                {
8554                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8555                   // Convert e.g. / 4 into / 4.0
8556                   exp.op.exp2.destType = type1._class.registered.dataType;
8557                   if(type1._class.registered.dataType)
8558                      type1._class.registered.dataType.refCount++;
8559                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8560                   exp.expType = type1;
8561                   if(type1) type1.refCount++;
8562                }
8563                else if(type1)
8564                {
8565                   bool valid = false;
8566
8567                   if(!boolResult && useSideUnit && type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8568                   {
8569                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8570
8571                      if(!type1._class.registered.dataType)
8572                         type1._class.registered.dataType = ProcessTypeString(type1._class.registered.dataTypeString, false);
8573                      exp.op.exp2.destType = type1._class.registered.dataType;
8574                      exp.op.exp2.destType.refCount++;
8575
8576                      CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8577                      if(type2)
8578                         FreeType(type2);
8579                      type2 = exp.op.exp2.destType;
8580                      if(type2) type2.refCount++;
8581
8582                      exp.expType = type2;
8583                      type2.refCount++;
8584                   }
8585
8586                   if(!boolResult && useSideUnit && type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8587                   {
8588                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8589
8590                      if(!type2._class.registered.dataType)
8591                         type2._class.registered.dataType = ProcessTypeString(type2._class.registered.dataTypeString, false);
8592                      exp.op.exp1.destType = type2._class.registered.dataType;
8593                      exp.op.exp1.destType.refCount++;
8594
8595                      CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8596                      type1 = exp.op.exp1.destType;
8597                      exp.expType = type1;
8598                      type1.refCount++;
8599                   }
8600
8601                   // TESTING THIS NEW CODE
8602                   if(!boolResult || exp.op.op == '>' || exp.op.op == '<')
8603                   {
8604                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass && exp.op.exp2.expType)
8605                      {
8606                         if(CheckExpressionType(exp.op.exp1, exp.op.exp2.expType, false))
8607                         {
8608                            if(exp.expType) FreeType(exp.expType);
8609                            exp.expType = exp.op.exp1.expType;
8610                            if(exp.op.exp2.expType) exp.op.exp1.expType.refCount++;
8611                            valid = true;
8612                         }
8613                      }
8614
8615                      else if(type2 && (type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass && exp.op.exp1.expType))
8616                      {
8617                         if(CheckExpressionType(exp.op.exp2, exp.op.exp1.expType, false))
8618                         {
8619                            if(exp.expType) FreeType(exp.expType);
8620                            exp.expType = exp.op.exp2.expType;
8621                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8622                            valid = true;
8623                         }
8624                      }
8625                   }
8626
8627                   if(!valid)
8628                   {
8629                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8630                      exp.op.exp2.destType = type1;
8631                      type1.refCount++;
8632
8633                      /*
8634                      // Maybe this was meant to be an enum...
8635                      if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8636                      {
8637                         Type oldType = exp.op.exp2.expType;
8638                         exp.op.exp2.expType = null;
8639                         if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8640                            FreeType(oldType);
8641                         else
8642                            exp.op.exp2.expType = oldType;
8643                      }
8644                      */
8645
8646                      /*
8647                      // TESTING THIS HERE... LATEST ADDITION
8648                      if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8649                      {
8650                         if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8651                         exp.op.exp2.destType = type2._class.registered.dataType;
8652                         if(type2._class.registered.dataType)
8653                            type2._class.registered.dataType.refCount++;
8654                         CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8655
8656                         //exp.expType = type2._class.registered.dataType; //type2;
8657                         //if(type2) type2.refCount++;
8658                      }
8659
8660                      // TESTING THIS HERE... LATEST ADDITION
8661                      if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8662                      {
8663                         if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8664                         exp.op.exp1.destType = type1._class.registered.dataType;
8665                         if(type1._class.registered.dataType)
8666                            type1._class.registered.dataType.refCount++;
8667                         CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8668                         exp.expType = type1._class.registered.dataType; //type1;
8669                         if(type1) type1.refCount++;
8670                      }
8671                      */
8672
8673                      if(CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false))
8674                      {
8675                         if(exp.expType) FreeType(exp.expType);
8676                         exp.expType = exp.op.exp2.destType;
8677                         if(exp.op.exp2.destType) exp.op.exp2.destType.refCount++;
8678                      }
8679                      else if(type1 && type2)
8680                      {
8681                         char expString1[10240];
8682                         char expString2[10240];
8683                         char type1String[1024];
8684                         char type2String[1024];
8685                         expString1[0] = '\0';
8686                         expString2[0] = '\0';
8687                         type1String[0] = '\0';
8688                         type2String[0] = '\0';
8689                         if(inCompiler)
8690                         {
8691                            PrintExpression(exp.op.exp1, expString1);
8692                            ChangeCh(expString1, '\n', ' ');
8693                            PrintExpression(exp.op.exp2, expString2);
8694                            ChangeCh(expString2, '\n', ' ');
8695                            PrintType(exp.op.exp1.expType, type1String, false, true);
8696                            PrintType(exp.op.exp2.expType, type2String, false, true);
8697                         }
8698
8699                         Compiler_Warning($"incompatible expressions %s (%s) and %s (%s)\n", expString1, type1String, expString2, type2String);
8700
8701                         if(type1.kind == classType && type1._class && type1._class.registered && type1._class.registered.type == enumClass)
8702                         {
8703                            exp.expType = exp.op.exp1.expType;
8704                            if(exp.op.exp1.expType) exp.op.exp1.expType.refCount++;
8705                         }
8706                         else if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8707                         {
8708                            exp.expType = exp.op.exp2.expType;
8709                            if(exp.op.exp2.expType) exp.op.exp2.expType.refCount++;
8710                         }
8711                      }
8712                   }
8713                }
8714                else if(type2)
8715                {
8716                   // Maybe this was meant to be an enum...
8717                   if(type2.kind == classType && type2._class && type2._class.registered && type2._class.registered.type == enumClass)
8718                   {
8719                      Type oldType = exp.op.exp1.expType;
8720                      exp.op.exp1.expType = null;
8721                      if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8722                         FreeType(oldType);
8723                      else
8724                         exp.op.exp1.expType = oldType;
8725                   }
8726
8727                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8728                   exp.op.exp1.destType = type2;
8729                   type2.refCount++;
8730                   /*
8731                   // TESTING THIS HERE... LATEST ADDITION
8732                   if(type1 && type1.kind == classType && type1._class.registered && type1._class.registered.type == unitClass && type2 && type2.kind != classType)
8733                   {
8734                      if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8735                      exp.op.exp1.destType = type1._class.registered.dataType;
8736                      if(type1._class.registered.dataType)
8737                         type1._class.registered.dataType.refCount++;
8738                   }
8739
8740                   // TESTING THIS HERE... LATEST ADDITION
8741                   if(type2 && type2.kind == classType && type2._class.registered && type2._class.registered.type == unitClass && type1 && type1.kind != classType)
8742                   {
8743                      if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8744                      exp.op.exp2.destType = type2._class.registered.dataType;
8745                      if(type2._class.registered.dataType)
8746                         type2._class.registered.dataType.refCount++;
8747                   }
8748                   */
8749
8750                   if(CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false))
8751                   {
8752                      if(exp.expType) FreeType(exp.expType);
8753                      exp.expType = exp.op.exp1.destType;
8754                      if(exp.op.exp1.destType) exp.op.exp1.destType.refCount++;
8755                   }
8756                }
8757             }
8758             else if(type2 && (!type1 || (type2.kind == classType && type1.kind != classType)))
8759             {
8760                if(type1 && type2._class && type2._class.registered && type2._class.registered.type == unitClass)
8761                {
8762                   if(exp.op.exp1.destType) FreeType(exp.op.exp1.destType);
8763                   // Convert e.g. / 4 into / 4.0
8764                   exp.op.exp1.destType = type2._class.registered.dataType;
8765                   if(type2._class.registered.dataType)
8766                      type2._class.registered.dataType.refCount++;
8767                   CheckExpressionType(exp.op.exp1, exp.op.exp1.destType, false);
8768                }
8769                if(exp.op.op == '!')
8770                {
8771                   exp.expType = MkClassType("bool");
8772                   exp.expType.truth = true;
8773                }
8774                else
8775                {
8776                   exp.expType = type2;
8777                   if(type2) type2.refCount++;
8778                }
8779             }
8780             else if(type1 && (!type2 || (type1.kind == classType && type2.kind != classType)))
8781             {
8782                if(type2 && type1._class && type1._class.registered && type1._class.registered.type == unitClass)
8783                {
8784                   if(exp.op.exp2.destType) FreeType(exp.op.exp2.destType);
8785                   // Convert e.g. / 4 into / 4.0
8786                   exp.op.exp2.destType = type1._class.registered.dataType;
8787                   if(type1._class.registered.dataType)
8788                      type1._class.registered.dataType.refCount++;
8789                   CheckExpressionType(exp.op.exp2, exp.op.exp2.destType, false);
8790                }
8791                exp.expType = type1;
8792                if(type1) type1.refCount++;
8793             }
8794          }
8795
8796          yylloc = exp.loc;
8797          if(exp.op.exp1 && !exp.op.exp1.expType)
8798          {
8799             char expString[10000];
8800             expString[0] = '\0';
8801             if(inCompiler)
8802             {
8803                PrintExpression(exp.op.exp1, expString);
8804                ChangeCh(expString, '\n', ' ');
8805             }
8806             if(expString[0])
8807                Compiler_Error($"couldn't determine type of %s\n", expString);
8808          }
8809          if(exp.op.exp2 && !exp.op.exp2.expType)
8810          {
8811             char expString[10240];
8812             expString[0] = '\0';
8813             if(inCompiler)
8814             {
8815                PrintExpression(exp.op.exp2, expString);
8816                ChangeCh(expString, '\n', ' ');
8817             }
8818             if(expString[0])
8819                Compiler_Error($"couldn't determine type of %s\n", expString);
8820          }
8821
8822          if(boolResult)
8823          {
8824             FreeType(exp.expType);
8825             exp.expType = MkClassType("bool");
8826             exp.expType.truth = true;
8827          }
8828
8829          if(exp.op.op != SIZEOF)
8830             exp.isConstant = (!exp.op.exp1 || exp.op.exp1.isConstant) &&
8831                (!exp.op.exp2 || exp.op.exp2.isConstant);
8832
8833          if(exp.op.op == SIZEOF && exp.op.exp2.expType)
8834          {
8835             DeclareType(exp.op.exp2.expType, false, false);
8836          }
8837
8838          yylloc = oldyylloc;
8839
8840          FreeType(dummy);
8841          if(type2)
8842             FreeType(type2);
8843          break;
8844       }
8845       case bracketsExp:
8846       case extensionExpressionExp:
8847       {
8848          Expression e;
8849          exp.isConstant = true;
8850          for(e = exp.list->first; e; e = e.next)
8851          {
8852             bool inced = false;
8853             if(!e.next)
8854             {
8855                FreeType(e.destType);
8856                e.destType = exp.destType;
8857                if(e.destType) { exp.destType.refCount++; e.destType.count++; inced = true; }
8858             }
8859             ProcessExpressionType(e);
8860             if(inced)
8861                exp.destType.count--;
8862             if(!exp.expType && !e.next)
8863             {
8864                exp.expType = e.expType;
8865                if(e.expType) e.expType.refCount++;
8866             }
8867             if(!e.isConstant)
8868                exp.isConstant = false;
8869          }
8870
8871          // In case a cast became a member...
8872          e = exp.list->first;
8873          if(!e.next && e.type == memberExp)
8874          {
8875             // Preserve prev, next
8876             Expression next = exp.next, prev = exp.prev;
8877
8878
8879             FreeType(exp.expType);
8880             FreeType(exp.destType);
8881             delete exp.list;
8882
8883             *exp = *e;
8884
8885             exp.prev = prev;
8886             exp.next = next;
8887
8888             delete e;
8889
8890             ProcessExpressionType(exp);
8891          }
8892          break;
8893       }
8894       case indexExp:
8895       {
8896          Expression e;
8897          exp.isConstant = true;
8898
8899          ProcessExpressionType(exp.index.exp);
8900          if(!exp.index.exp.isConstant)
8901             exp.isConstant = false;
8902
8903          if(exp.index.exp.expType)
8904          {
8905             Type source = exp.index.exp.expType;
8906             if(source.kind == classType && source._class && source._class.registered)
8907             {
8908                Class _class = source._class.registered;
8909                Class c = _class.templateClass ? _class.templateClass : _class;
8910                if(_class != containerClass && eClass_IsDerived(c, containerClass) && _class.templateArgs)
8911                {
8912                   exp.expType = ProcessTypeString(_class.templateArgs[2].dataTypeString, false);
8913
8914                   if(exp.index.index && exp.index.index->last)
8915                   {
8916                      ((Expression)exp.index.index->last).destType = ProcessTypeString(_class.templateArgs[1].dataTypeString, false);
8917                   }
8918                }
8919             }
8920          }
8921
8922          for(e = exp.index.index->first; e; e = e.next)
8923          {
8924             if(!e.next && exp.index.exp.expType && exp.index.exp.expType.kind == arrayType && exp.index.exp.expType.enumClass)
8925             {
8926                if(e.destType) FreeType(e.destType);
8927                e.destType = MkClassType(exp.index.exp.expType.enumClass.string);
8928             }
8929             ProcessExpressionType(e);
8930             if(!e.next)
8931             {
8932                // Check if this type is int
8933             }
8934             if(!e.isConstant)
8935                exp.isConstant = false;
8936          }
8937
8938          if(!exp.expType)
8939             exp.expType = Dereference(exp.index.exp.expType);
8940          if(exp.expType)
8941             DeclareType(exp.expType, false, false);
8942          break;
8943       }
8944       case callExp:
8945       {
8946          Expression e;
8947          Type functionType;
8948          Type methodType = null;
8949          char name[1024];
8950          name[0] = '\0';
8951
8952          if(inCompiler)
8953          {
8954             PrintExpression(exp.call.exp,  name);
8955             if(exp.call.exp.expType && !exp.call.exp.expType.returnType)
8956             {
8957                //exp.call.exp.expType = null;
8958                PrintExpression(exp.call.exp,  name);
8959             }
8960          }
8961          if(exp.call.exp.type == identifierExp)
8962          {
8963             Expression idExp = exp.call.exp;
8964             Identifier id = idExp.identifier;
8965             if(!strcmp(id.string, "__builtin_frame_address"))
8966             {
8967                exp.expType = ProcessTypeString("void *", true);
8968                if(exp.call.arguments && exp.call.arguments->first)
8969                   ProcessExpressionType(exp.call.arguments->first);
8970                break;
8971             }
8972             else if(!strcmp(id.string, "__ENDIAN_PAD"))
8973             {
8974                exp.expType = ProcessTypeString("int", true);
8975                if(exp.call.arguments && exp.call.arguments->first)
8976                   ProcessExpressionType(exp.call.arguments->first);
8977                break;
8978             }
8979             else if(!strcmp(id.string, "Max") ||
8980                !strcmp(id.string, "Min") ||
8981                !strcmp(id.string, "Sgn") ||
8982                !strcmp(id.string, "Abs"))
8983             {
8984                Expression a = null;
8985                Expression b = null;
8986                Expression tempExp1 = null, tempExp2 = null;
8987                if((!strcmp(id.string, "Max") ||
8988                   !strcmp(id.string, "Min")) && exp.call.arguments->count == 2)
8989                {
8990                   a = exp.call.arguments->first;
8991                   b = exp.call.arguments->last;
8992                   tempExp1 = a;
8993                   tempExp2 = b;
8994                }
8995                else if(exp.call.arguments->count == 1)
8996                {
8997                   a = exp.call.arguments->first;
8998                   tempExp1 = a;
8999                }
9000
9001                if(a)
9002                {
9003                   exp.call.arguments->Clear();
9004                   idExp.identifier = null;
9005
9006                   FreeExpContents(exp);
9007
9008                   ProcessExpressionType(a);
9009                   if(b)
9010                      ProcessExpressionType(b);
9011
9012                   exp.type = bracketsExp;
9013                   exp.list = MkList();
9014
9015                   if(a.expType && (!b || b.expType))
9016                   {
9017                      if((!a.isConstant && a.type != identifierExp) || (b && !b.isConstant && b.type != identifierExp))
9018                      {
9019                         // Use the simpleStruct name/ids for now...
9020                         if(inCompiler)
9021                         {
9022                            OldList * specs = MkList();
9023                            OldList * decls = MkList();
9024                            Declaration decl;
9025                            char temp1[1024], temp2[1024];
9026
9027                            GetTypeSpecs(a.expType, specs);
9028
9029                            if(a && !a.isConstant && a.type != identifierExp)
9030                            {
9031                               sprintf(temp1, "__simpleStruct%d", curContext.simpleID++);
9032                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp1)), null));
9033                               tempExp1 = QMkExpId(temp1);
9034                               tempExp1.expType = a.expType;
9035                               if(a.expType)
9036                                  a.expType.refCount++;
9037                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp1), '=', a));
9038                            }
9039                            if(b && !b.isConstant && b.type != identifierExp)
9040                            {
9041                               sprintf(temp2, "__simpleStruct%d", curContext.simpleID++);
9042                               ListAdd(decls, MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier(temp2)), null));
9043                               tempExp2 = QMkExpId(temp2);
9044                               tempExp2.expType = b.expType;
9045                               if(b.expType)
9046                                  b.expType.refCount++;
9047                               ListAdd(exp.list, MkExpOp(CopyExpression(tempExp2), '=', b));
9048                            }
9049
9050                            decl = MkDeclaration(specs, decls);
9051                            if(!curCompound.compound.declarations)
9052                               curCompound.compound.declarations = MkList();
9053                            curCompound.compound.declarations->Insert(null, decl);
9054                         }
9055                      }
9056                   }
9057
9058                   if(!strcmp(id.string, "Max") || !strcmp(id.string, "Min"))
9059                   {
9060                      int op = (!strcmp(id.string, "Max")) ? '>' : '<';
9061                      ListAdd(exp.list,
9062                         MkExpCondition(MkExpBrackets(MkListOne(
9063                            MkExpOp(CopyExpression(tempExp1), op, CopyExpression(tempExp2)))),
9064                            MkListOne(CopyExpression(tempExp1)), CopyExpression(tempExp2)));
9065                      exp.expType = a.expType;
9066                      if(a.expType)
9067                         a.expType.refCount++;
9068                   }
9069                   else if(!strcmp(id.string, "Abs"))
9070                   {
9071                      ListAdd(exp.list,
9072                         MkExpCondition(MkExpBrackets(MkListOne(
9073                            MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9074                            MkListOne(MkExpOp(null, '-', CopyExpression(tempExp1))), CopyExpression(tempExp1)));
9075                      exp.expType = a.expType;
9076                      if(a.expType)
9077                         a.expType.refCount++;
9078                   }
9079                   else if(!strcmp(id.string, "Sgn"))
9080                   {
9081                      // ((!(a))?(0):(((a)<0)?(-1):(1)))
9082                      ListAdd(exp.list,
9083                         MkExpCondition(MkExpBrackets(MkListOne(
9084                            MkExpOp(null, '!', CopyExpression(tempExp1)))), MkListOne(MkExpConstant("0")),
9085                               MkExpBrackets(MkListOne(MkExpCondition(MkExpBrackets(MkListOne(
9086                                  MkExpOp(CopyExpression(tempExp1), '<', MkExpConstant("0")))),
9087                                  MkListOne(MkExpConstant("-1")), MkExpConstant("1"))))));
9088                      exp.expType = ProcessTypeString("int", false);
9089                   }
9090
9091                   FreeExpression(tempExp1);
9092                   if(tempExp2) FreeExpression(tempExp2);
9093
9094                   FreeIdentifier(id);
9095                   break;
9096                }
9097             }
9098          }
9099
9100          {
9101             Type dummy
9102             {
9103                count = 1;
9104                refCount = 1;
9105             };
9106             if(!exp.call.exp.destType)
9107             {
9108                exp.call.exp.destType = dummy;
9109                dummy.refCount++;
9110             }
9111             ProcessExpressionType(exp.call.exp);
9112             if(exp.call.exp.destType == dummy)
9113             {
9114                FreeType(dummy);
9115                exp.call.exp.destType = null;
9116             }
9117             FreeType(dummy);
9118          }
9119
9120          // Check argument types against parameter types
9121          functionType = exp.call.exp.expType;
9122
9123          if(functionType && functionType.kind == TypeKind::methodType)
9124          {
9125             methodType = functionType;
9126             functionType = methodType.method.dataType;
9127
9128             //if(functionType.returnType && functionType.returnType.kind == thisClassType)
9129             // TOCHECK: Instead of doing this here could this be done per param?
9130             if(exp.call.exp.expType.usedClass)
9131             {
9132                char typeString[1024];
9133                typeString[0] = '\0';
9134                {
9135                   Symbol back = functionType.thisClass;
9136                   // Do not output class specifier here (thisclass was added to this)
9137                   functionType.thisClass = null;
9138                   PrintType(functionType, typeString, true, true);
9139                   functionType.thisClass = back;
9140                }
9141                if(strstr(typeString, "thisclass"))
9142                {
9143                   OldList * specs = MkList();
9144                   Declarator decl;
9145                   {
9146                      Context context = SetupTemplatesContext(exp.call.exp.expType.usedClass);
9147
9148                      decl = SpecDeclFromString(typeString, specs, null);
9149
9150                      // SET THIS TO FALSE WHEN PROCESSING THISCLASS OUTSIDE THE CLASS
9151                      if(thisClass != (exp.call.exp.expType.usedClass.templateClass ? exp.call.exp.expType.usedClass.templateClass :
9152                         exp.call.exp.expType.usedClass))
9153                         thisClassParams = false;
9154
9155                      ReplaceThisClassSpecifiers(specs, exp.call.exp.expType.usedClass);
9156                      {
9157                         Class backupThisClass = thisClass;
9158                         thisClass = exp.call.exp.expType.usedClass;
9159                         ProcessDeclarator(decl);
9160                         thisClass = backupThisClass;
9161                      }
9162
9163                      thisClassParams = true;
9164
9165                      functionType = ProcessType(specs, decl);
9166                      functionType.refCount = 0;
9167                      FinishTemplatesContext(context);
9168                   }
9169
9170                   FreeList(specs, FreeSpecifier);
9171                   FreeDeclarator(decl);
9172                 }
9173             }
9174          }
9175          if(functionType && functionType.kind == pointerType && functionType.type && functionType.type.kind == TypeKind::functionType)
9176          {
9177             Type type = functionType.type;
9178             if(!functionType.refCount)
9179             {
9180                functionType.type = null;
9181                FreeType(functionType);
9182             }
9183             //methodType = functionType;
9184             functionType = type;
9185          }
9186          if(functionType && functionType.kind != TypeKind::functionType)
9187          {
9188             Compiler_Error($"called object %s is not a function\n", name);
9189          }
9190          else if(functionType)
9191          {
9192             bool emptyParams = false, noParams = false;
9193             Expression e = exp.call.arguments ? exp.call.arguments->first : null;
9194             Type type = functionType.params.first;
9195             Expression memberExp = (exp.call.exp.type == ExpressionType::memberExp) ? exp.call.exp : null;
9196             int extra = 0;
9197             Location oldyylloc = yylloc;
9198
9199             if(!type) emptyParams = true;
9200
9201             // WORKING ON THIS:
9202             if(functionType.extraParam && e && functionType.thisClass)
9203             {
9204                e.destType = MkClassType(functionType.thisClass.string);
9205                e = e.next;
9206             }
9207
9208             // WHY WAS THIS COMMENTED OUT ? Broke DisplaySystem::FontExtent(this ? displaySystem : null, font, text, len, width, height);
9209             // Fixed #141 by adding '&& !functionType.extraParam'
9210             if(!functionType.staticMethod && !functionType.extraParam)
9211             {
9212                if(memberExp && memberExp.member.exp && memberExp.member.exp.expType && memberExp.member.exp.expType.kind == subClassType &&
9213                   memberExp.member.exp.expType._class)
9214                {
9215                   type = MkClassType(memberExp.member.exp.expType._class.string);
9216                   if(e)
9217                   {
9218                      e.destType = type;
9219                      e = e.next;
9220                      type = functionType.params.first;
9221                   }
9222                   else
9223                      type.refCount = 0;
9224                }
9225                else if(!memberExp && (functionType.thisClass || (methodType && methodType.methodClass)))
9226                {
9227                   type = MkClassType(functionType.thisClass ? functionType.thisClass.string : (methodType ? methodType.methodClass.fullName : null));
9228                   type.byReference = functionType.byReference;
9229                   type.typedByReference = functionType.typedByReference;
9230                   if(e)
9231                   {
9232                      // Allow manually passing a class for typed object
9233                      if(e.next && type.kind == classType && (functionType && functionType.thisClass) && functionType.classObjectType == typedObject)
9234                         e = e.next;
9235                      e.destType = type;
9236                      e = e.next;
9237                      type = functionType.params.first;
9238                   }
9239                   else
9240                      type.refCount = 0;
9241                   //extra = 1;
9242                }
9243             }
9244
9245             if(type && type.kind == voidType)
9246             {
9247                noParams = true;
9248                if(!type.refCount) FreeType(type);
9249                type = null;
9250             }
9251
9252             for( ; e; e = e.next)
9253             {
9254                if(!type && !emptyParams)
9255                {
9256                   yylloc = e.loc;
9257                   if(methodType && methodType.methodClass)
9258                      Compiler_Error($"too many arguments for method %s::%s (%d given, expected %d)\n",
9259                         methodType.methodClass.fullName, methodType.method.name, exp.call.arguments->count,
9260                         noParams ? 0 : functionType.params.count);
9261                   else
9262                      Compiler_Error($"too many arguments for function %s (%d given, expected %d)\n",
9263                         name /*exp.call.exp.identifier.string*/, exp.call.arguments->count,
9264                         noParams ? 0 : functionType.params.count);
9265                   break;
9266                }
9267
9268                if(methodType && type && type.kind == templateType && type.templateParameter.type == TemplateParameterType::type)
9269                {
9270                   Type templatedType = null;
9271                   Class _class = methodType.usedClass;
9272                   ClassTemplateParameter curParam = null;
9273                   int id = 0;
9274                   if(_class && _class.templateArgs /*&& _class.templateClass*/)
9275                   {
9276                      Class sClass;
9277                      for(sClass = _class; sClass; sClass = sClass.base)
9278                      {
9279                         if(sClass.templateClass) sClass = sClass.templateClass;
9280                         id = 0;
9281                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9282                         {
9283                            if(curParam.type == TemplateParameterType::type && !strcmp(type.templateParameter.identifier.string, curParam.name))
9284                            {
9285                               Class nextClass;
9286                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base)
9287                               {
9288                                  if(nextClass.templateClass) nextClass = nextClass.templateClass;
9289                                  id += nextClass.templateParams.count;
9290                               }
9291                               break;
9292                            }
9293                            id++;
9294                         }
9295                         if(curParam) break;
9296                      }
9297                   }
9298                   if(curParam && _class.templateArgs[id].dataTypeString)
9299                   {
9300                      ClassTemplateArgument arg = _class.templateArgs[id];
9301                      {
9302                         Context context = SetupTemplatesContext(_class);
9303
9304                         /*if(!arg.dataType)
9305                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9306                         templatedType = ProcessTypeString(arg.dataTypeString, false);
9307                         FinishTemplatesContext(context);
9308                      }
9309                      e.destType = templatedType;
9310                      if(templatedType)
9311                      {
9312                         templatedType.passAsTemplate = true;
9313                         // templatedType.refCount++;
9314                      }
9315                   }
9316                   else
9317                   {
9318                      e.destType = type;
9319                      if(type) type.refCount++;
9320                   }
9321                }
9322                else
9323                {
9324                   if(type && type.kind == ellipsisType && type.prev && type.prev.kind == classType && type.prev.classObjectType)
9325                   {
9326                      e.destType = type.prev;
9327                      e.destType.refCount++;
9328                   }
9329                   else
9330                   {
9331                      e.destType = type;
9332                      if(type) type.refCount++;
9333                   }
9334                }
9335                // Don't reach the end for the ellipsis
9336                if(type && type.kind != ellipsisType)
9337                {
9338                   Type next = type.next;
9339                   if(!type.refCount) FreeType(type);
9340                   type = next;
9341                }
9342             }
9343
9344             if(type && type.kind != ellipsisType)
9345             {
9346                if(methodType && methodType.methodClass)
9347                   Compiler_Warning($"not enough arguments for method %s::%s (%d given, expected %d)\n",
9348                      methodType.methodClass.fullName, methodType.method.name, exp.call.arguments ? exp.call.arguments->count : 0,
9349                      functionType.params.count + extra);
9350                else
9351                   Compiler_Warning($"not enough arguments for function %s (%d given, expected %d)\n",
9352                      name /*exp.call.exp.identifier.string*/, exp.call.arguments ? exp.call.arguments->count : 0,
9353                      functionType.params.count + extra);
9354             }
9355             yylloc = oldyylloc;
9356             if(type && !type.refCount) FreeType(type);
9357          }
9358          else
9359          {
9360             functionType = Type
9361             {
9362                refCount = 0;
9363                kind = TypeKind::functionType;
9364             };
9365
9366             if(exp.call.exp.type == identifierExp)
9367             {
9368                char * string = exp.call.exp.identifier.string;
9369                if(inCompiler)
9370                {
9371                   Symbol symbol;
9372                   Location oldyylloc = yylloc;
9373
9374                   yylloc = exp.call.exp.identifier.loc;
9375                   if(strstr(string, "__builtin_") == string)
9376                   {
9377                      if(exp.destType)
9378                      {
9379                         functionType.returnType = exp.destType;
9380                         exp.destType.refCount++;
9381                      }
9382                   }
9383                   else
9384                      Compiler_Warning($"%s undefined; assuming extern returning int\n", string);
9385                   symbol = Symbol { string = CopyString(string), type = ProcessTypeString("int()", true) };
9386                   globalContext.symbols.Add((BTNode)symbol);
9387                   if(strstr(symbol.string, "::"))
9388                      globalContext.hasNameSpace = true;
9389
9390                   yylloc = oldyylloc;
9391                }
9392             }
9393             else if(exp.call.exp.type == memberExp)
9394             {
9395                /*Compiler_Warning($"%s undefined; assuming returning int\n",
9396                   exp.call.exp.member.member.string);*/
9397             }
9398             else
9399                Compiler_Warning($"callable object undefined; extern assuming returning int\n");
9400
9401             if(!functionType.returnType)
9402             {
9403                functionType.returnType = Type
9404                {
9405                   refCount = 1;
9406                   kind = intType;
9407                };
9408             }
9409          }
9410          if(functionType && functionType.kind == TypeKind::functionType)
9411          {
9412             exp.expType = functionType.returnType;
9413
9414             if(functionType.returnType)
9415                functionType.returnType.refCount++;
9416
9417             if(!functionType.refCount)
9418                FreeType(functionType);
9419          }
9420
9421          if(exp.call.arguments)
9422          {
9423             for(e = exp.call.arguments->first; e; e = e.next)
9424             {
9425                Type destType = e.destType;
9426                ProcessExpressionType(e);
9427             }
9428          }
9429          break;
9430       }
9431       case memberExp:
9432       {
9433          Type type;
9434          Location oldyylloc = yylloc;
9435          bool thisPtr;
9436          Expression checkExp = exp.member.exp;
9437          while(checkExp)
9438          {
9439             if(checkExp.type == castExp)
9440                checkExp = checkExp.cast.exp;
9441             else if(checkExp.type == bracketsExp)
9442                checkExp = checkExp.list ? checkExp.list->first : null;
9443             else
9444                break;
9445          }
9446
9447          thisPtr = (checkExp && checkExp.type == identifierExp && !strcmp(checkExp.identifier.string, "this"));
9448          exp.thisPtr = thisPtr;
9449
9450          // DOING THIS LATER NOW...
9451          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
9452          {
9453             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
9454             /* TODO: Name Space Fix ups
9455             if(!exp.member.member.classSym)
9456                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.fullName);
9457             */
9458          }
9459
9460          ProcessExpressionType(exp.member.exp);
9461          if(exp.member.exp.expType && exp.member.exp.expType.kind == classType && exp.member.exp.expType._class &&
9462             exp.member.exp.expType._class.registered && exp.member.exp.expType._class.registered.type == normalClass)
9463          {
9464             exp.isConstant = false;
9465          }
9466          else
9467             exp.isConstant = exp.member.exp.isConstant;
9468          type = exp.member.exp.expType;
9469
9470          yylloc = exp.loc;
9471
9472          if(type && (type.kind == templateType))
9473          {
9474             Class _class = thisClass ? thisClass : currentClass;
9475             ClassTemplateParameter param = null;
9476             if(_class)
9477             {
9478                for(param = _class.templateParams.first; param; param = param.next)
9479                {
9480                   if(param.type == identifier && exp.member.member && exp.member.member.string && !strcmp(param.name, exp.member.member.string))
9481                      break;
9482                }
9483             }
9484             if(param && param.defaultArg.member)
9485             {
9486                Expression argExp = GetTemplateArgExpByName(param.name, thisClass, TemplateParameterType::identifier);
9487                if(argExp)
9488                {
9489                   Expression expMember = exp.member.exp;
9490                   Declarator decl;
9491                   OldList * specs = MkList();
9492                   char thisClassTypeString[1024];
9493
9494                   FreeIdentifier(exp.member.member);
9495
9496                   ProcessExpressionType(argExp);
9497
9498                   {
9499                      char * colon = strstr(param.defaultArg.memberString, "::");
9500                      if(colon)
9501                      {
9502                         char className[1024];
9503                         Class sClass;
9504
9505                         memcpy(thisClassTypeString, param.defaultArg.memberString, colon - param.defaultArg.memberString);
9506                         thisClassTypeString[colon - param.defaultArg.memberString] = '\0';
9507                      }
9508                      else
9509                         strcpy(thisClassTypeString, _class.fullName);
9510                   }
9511
9512                   decl = SpecDeclFromString(param.defaultArg.member.dataTypeString, specs, null);
9513
9514                   exp.expType = ProcessType(specs, decl);
9515                   if(exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.templateClass)
9516                   {
9517                      Class expClass = exp.expType._class.registered;
9518                      Class cClass = null;
9519                      int c;
9520                      int paramCount = 0;
9521                      int lastParam = -1;
9522
9523                      char templateString[1024];
9524                      ClassTemplateParameter param;
9525                      sprintf(templateString, "%s<", expClass.templateClass.fullName);
9526                      for(cClass = expClass; cClass; cClass = cClass.base)
9527                      {
9528                         int p = 0;
9529                         for(param = cClass.templateParams.first; param; param = param.next)
9530                         {
9531                            int id = p;
9532                            Class sClass;
9533                            ClassTemplateArgument arg;
9534                            for(sClass = cClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
9535                            arg = expClass.templateArgs[id];
9536
9537                            for(sClass = _class /*expClass*/; sClass; sClass = sClass.base)
9538                            {
9539                               ClassTemplateParameter cParam;
9540                               //int p = numParams - sClass.templateParams.count;
9541                               int p = 0;
9542                               Class nextClass;
9543                               for(nextClass = sClass.base; nextClass; nextClass = nextClass.base) p += nextClass.templateParams.count;
9544
9545                               for(cParam = sClass.templateParams.first; cParam; cParam = cParam.next, p++)
9546                               {
9547                                  if(cParam.type == TemplateParameterType::type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
9548                                  {
9549                                     if(_class.templateArgs && arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9550                                     {
9551                                        arg.dataTypeString = _class.templateArgs[p].dataTypeString;
9552                                        arg.dataTypeClass = _class.templateArgs[p].dataTypeClass;
9553                                        break;
9554                                     }
9555                                  }
9556                               }
9557                            }
9558
9559                            {
9560                               char argument[256];
9561                               argument[0] = '\0';
9562                               /*if(arg.name)
9563                               {
9564                                  strcat(argument, arg.name.string);
9565                                  strcat(argument, " = ");
9566                               }*/
9567                               switch(param.type)
9568                               {
9569                                  case expression:
9570                                  {
9571                                     // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
9572                                     char expString[1024];
9573                                     OldList * specs = MkList();
9574                                     Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
9575                                     Expression exp;
9576                                     char * string = PrintHexUInt64(arg.expression.ui64);
9577                                     exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
9578                                     delete string;
9579
9580                                     ProcessExpressionType(exp);
9581                                     ComputeExpression(exp);
9582                                     expString[0] = '\0';
9583                                     PrintExpression(exp, expString);
9584                                     strcat(argument, expString);
9585                                     // delete exp;
9586                                     FreeExpression(exp);
9587                                     break;
9588                                  }
9589                                  case identifier:
9590                                  {
9591                                     strcat(argument, arg.member.name);
9592                                     break;
9593                                  }
9594                                  case TemplateParameterType::type:
9595                                  {
9596                                     if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
9597                                     {
9598                                        if(!strcmp(arg.dataTypeString, "thisclass"))
9599                                           strcat(argument, thisClassTypeString);
9600                                        else
9601                                           strcat(argument, arg.dataTypeString);
9602                                     }
9603                                     break;
9604                                  }
9605                               }
9606                               if(argument[0])
9607                               {
9608                                  if(paramCount) strcat(templateString, ", ");
9609                                  if(lastParam != p - 1)
9610                                  {
9611                                     strcat(templateString, param.name);
9612                                     strcat(templateString, " = ");
9613                                  }
9614                                  strcat(templateString, argument);
9615                                  paramCount++;
9616                                  lastParam = p;
9617                               }
9618                               p++;
9619                            }
9620                         }
9621                      }
9622                      {
9623                         int len = strlen(templateString);
9624                         if(templateString[len-1] == '>') templateString[len++] = ' ';
9625                         templateString[len++] = '>';
9626                         templateString[len++] = '\0';
9627                      }
9628                      {
9629                         Context context = SetupTemplatesContext(_class);
9630                         FreeType(exp.expType);
9631                         exp.expType = ProcessTypeString(templateString, false);
9632                         FinishTemplatesContext(context);
9633                      }
9634                   }
9635
9636                   // *([expType] *)(((byte *)[exp.member.exp]) + [argExp].member.offset)
9637                   exp.type = bracketsExp;
9638                   exp.list = MkListOne(MkExpOp(null, '*',
9639                   /*opExp;
9640                   exp.op.op = '*';
9641                   exp.op.exp1 = null;
9642                   exp.op.exp2 = */
9643                   MkExpCast(MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl)), MkExpBrackets(MkListOne(MkExpOp(
9644                      MkExpBrackets(MkListOne(
9645                         MkExpCast(MkTypeName(MkListOne(MkSpecifierName("byte")), MkDeclaratorPointer(MkPointer(null, null), null)), expMember))),
9646                            '+',
9647                            MkExpOp(MkExpMember(MkExpMember(argExp, MkIdentifier("member")), MkIdentifier("offset")),
9648                            '+',
9649                            MkExpMember(MkExpMember(MkExpMember(CopyExpression(argExp), MkIdentifier("member")), MkIdentifier("_class")), MkIdentifier("offset")))))))
9650
9651                            ));
9652                }
9653             }
9654             else if(type.templateParameter && type.templateParameter.type == TemplateParameterType::type &&
9655                (type.templateParameter.dataType || type.templateParameter.dataTypeString))
9656             {
9657                type = ProcessTemplateParameterType(type.templateParameter);
9658             }
9659          }
9660          // TODO: *** This seems to be where we should add method support for all basic types ***
9661          if(type && (type.kind == templateType));
9662          else if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType ||
9663                           type.kind == int64Type || type.kind == shortType || type.kind == longType || type.kind == charType || type.kind == _BoolType ||
9664                           type.kind == intPtrType || type.kind == intSizeType || type.kind == floatType || type.kind == doubleType ||
9665                           (type.kind == pointerType && type.type.kind == charType)))
9666          {
9667             Identifier id = exp.member.member;
9668             TypeKind typeKind = type.kind;
9669             Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
9670             if(typeKind == subClassType && exp.member.exp.type == classExp)
9671             {
9672                _class = eSystem_FindClass(privateModule, "ecere::com::Class");
9673                typeKind = classType;
9674             }
9675
9676             if(id)
9677             {
9678                if(typeKind == intType || typeKind == enumType)
9679                   _class = eSystem_FindClass(privateModule, "int");
9680                else if(!_class)
9681                {
9682                   if(type.kind == classType && type._class && type._class.registered)
9683                   {
9684                      _class = type._class.registered;
9685                   }
9686                   else if((type.kind == arrayType || type.kind == pointerType) && type.type && type.type.kind == charType)
9687                   {
9688                      _class = FindClass("char *").registered;
9689                   }
9690                   else if(type.kind == pointerType)
9691                   {
9692                      _class = eSystem_FindClass(privateModule, "uintptr");
9693                      FreeType(exp.expType);
9694                      exp.expType = ProcessTypeString("uintptr", false);
9695                      exp.byReference = true;
9696                   }
9697                   else
9698                   {
9699                      char string[1024] = "";
9700                      Symbol classSym;
9701                      PrintTypeNoConst(type, string, false, true);
9702                      classSym = FindClass(string);
9703                      if(classSym) _class = classSym.registered;
9704                   }
9705                }
9706             }
9707
9708             if(_class && id)
9709             {
9710                /*bool thisPtr =
9711                   (exp.member.exp.type == identifierExp &&
9712                   !strcmp(exp.member.exp.identifier.string, "this"));*/
9713                Property prop = null;
9714                Method method = null;
9715                DataMember member = null;
9716                Property revConvert = null;
9717                ClassProperty classProp = null;
9718
9719                if(id && id._class && id._class.name && !strcmp(id._class.name, "property"))
9720                   exp.member.memberType = propertyMember;
9721
9722                if(id && id._class && type._class && !eClass_IsDerived(type._class.registered, _class))
9723                   Compiler_Error($"invalid class specifier %s for object of class %s\n", _class.fullName, type._class.string);
9724
9725                if(typeKind != subClassType)
9726                {
9727                   // Prioritize data members over properties for "this"
9728                   if((exp.member.memberType == unresolvedMember && thisPtr) || exp.member.memberType == dataMember)
9729                   {
9730                      member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9731                      if(member && member._class != (_class.templateClass ? _class.templateClass : _class) && exp.member.memberType != dataMember)
9732                      {
9733                         prop = eClass_FindProperty(_class, id.string, privateModule);
9734                         if(prop)
9735                            member = null;
9736                      }
9737                      if(!member && !prop)
9738                         prop = eClass_FindProperty(_class, id.string, privateModule);
9739                      if((member && member._class == (_class.templateClass ? _class.templateClass : _class)) ||
9740                         (prop && prop._class == (_class.templateClass ? _class.templateClass : _class)))
9741                         exp.member.thisPtr = true;
9742                   }
9743                   // Prioritize properties over data members otherwise
9744                   else
9745                   {
9746                      // First look for Public Members (Unless class specifier is provided, which skips public priority)
9747                      if(!id.classSym)
9748                      {
9749                         prop = eClass_FindProperty(_class, id.string, null);
9750                         if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9751                            member = eClass_FindDataMember(_class, id.string, null, null, null);
9752                      }
9753
9754                      if(!prop && !member)
9755                      {
9756                         method = eClass_FindMethod(_class, id.string, null);
9757                         if(!method)
9758                         {
9759                            prop = eClass_FindProperty(_class, id.string, privateModule);
9760                            if(!id._class || !id._class.name || strcmp(id._class.name, "property"))
9761                               member = eClass_FindDataMember(_class, id.string, privateModule, null, null);
9762                         }
9763                      }
9764
9765                      if(member && prop)
9766                      {
9767                         if(member._class != prop._class && !id._class && eClass_IsDerived(member._class, prop._class))
9768                            prop = null;
9769                         else
9770                            member = null;
9771                      }
9772                   }
9773                }
9774                if(!prop && !member && !method)     // NOTE: Recently added the !method here, causes private methods to unprioritized
9775                   method = eClass_FindMethod(_class, id.string, privateModule);
9776                if(!prop && !member && !method)
9777                {
9778                   if(typeKind == subClassType)
9779                   {
9780                      classProp = eClass_FindClassProperty(type._class.registered, exp.member.member.string);
9781                      if(classProp)
9782                      {
9783                         exp.member.memberType = classPropertyMember;
9784                         exp.expType = ProcessTypeString(classProp.dataTypeString, false);
9785                      }
9786                      else
9787                      {
9788                         // Assume this is a class_data member
9789                         char structName[1024];
9790                         Identifier id = exp.member.member;
9791                         Expression classExp = exp.member.exp;
9792                         type.refCount++;
9793
9794                         FreeType(classExp.expType);
9795                         classExp.expType = ProcessTypeString("ecere::com::Class", false);
9796
9797                         strcpy(structName, "__ecereClassData_");
9798                         FullClassNameCat(structName, type._class.string, false);
9799                         exp.type = pointerExp;
9800                         exp.member.member = id;
9801
9802                         exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
9803                            MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
9804                               MkExpBrackets(MkListOne(MkExpOp(
9805                                  MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
9806                                     MkExpMember(classExp, MkIdentifier("data"))), '+',
9807                                        MkExpMember(MkExpClass(MkListOne(MkSpecifierName(type._class.string)), null), MkIdentifier("offsetClass")))))
9808                                  )));
9809
9810                         FreeType(type);
9811
9812                         ProcessExpressionType(exp);
9813                         return;
9814                      }
9815                   }
9816                   else
9817                   {
9818                      // Check for reverse conversion
9819                      // (Convert in an instantiation later, so that we can use
9820                      //  deep properties system)
9821                      Symbol classSym = FindClass(id.string);
9822                      if(classSym)
9823                      {
9824                         Class convertClass = classSym.registered;
9825                         if(convertClass)
9826                            revConvert = eClass_FindProperty(convertClass, _class.fullName, privateModule);
9827                      }
9828                   }
9829                }
9830
9831                if(prop)
9832                {
9833                   exp.member.memberType = propertyMember;
9834                   if(!prop.dataType)
9835                      ProcessPropertyType(prop);
9836                   exp.expType = prop.dataType;
9837                   if(prop.dataType) prop.dataType.refCount++;
9838                }
9839                else if(member)
9840                {
9841                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9842                   {
9843                      FreeExpContents(exp);
9844                      exp.type = identifierExp;
9845                      exp.identifier = MkIdentifier("class");
9846                      ProcessExpressionType(exp);
9847                      return;
9848                   }
9849
9850                   exp.member.memberType = dataMember;
9851                   DeclareStruct(_class.fullName, false);
9852                   if(!member.dataType)
9853                   {
9854                      Context context = SetupTemplatesContext(_class);
9855                      member.dataType = ProcessTypeString(member.dataTypeString, false);
9856                      FinishTemplatesContext(context);
9857                   }
9858                   exp.expType = member.dataType;
9859                   if(member.dataType) member.dataType.refCount++;
9860                }
9861                else if(revConvert)
9862                {
9863                   exp.member.memberType = reverseConversionMember;
9864                   exp.expType = MkClassType(revConvert._class.fullName);
9865                }
9866                else if(method)
9867                {
9868                   //if(inCompiler)
9869                   {
9870                      /*if(id._class)
9871                      {
9872                         exp.type = identifierExp;
9873                         exp.identifier = exp.member.member;
9874                      }
9875                      else*/
9876                         exp.member.memberType = methodMember;
9877                   }
9878                   if(!method.dataType)
9879                      ProcessMethodType(method);
9880                   exp.expType = Type
9881                   {
9882                      refCount = 1;
9883                      kind = methodType;
9884                      method = method;
9885                   };
9886
9887                   // Tricky spot here... To use instance versus class virtual table
9888                   // Put it back to what it was... What did we break?
9889
9890                   // Had to put it back for overriding Main of Thread global instance
9891
9892                   //exp.expType.methodClass = _class;
9893                   exp.expType.methodClass = (id && id._class) ? _class : null;
9894
9895                   // Need the actual class used for templated classes
9896                   exp.expType.usedClass = _class;
9897                }
9898                else if(!classProp)
9899                {
9900                   if(exp.member.exp.expType.classObjectType == typedObject && !strcmp(exp.member.member.string, "_class"))
9901                   {
9902                      FreeExpContents(exp);
9903                      exp.type = identifierExp;
9904                      exp.identifier = MkIdentifier("class");
9905                      FreeType(exp.expType);
9906                      exp.expType = MkClassType("ecere::com::Class");
9907                      return;
9908                   }
9909                   yylloc = exp.member.member.loc;
9910                   Compiler_Error($"couldn't find member %s in class %s\n", id.string, _class.fullName);
9911                   if(inCompiler)
9912                      eClass_AddDataMember(_class, id.string, "int", 0, 0, publicAccess);
9913                }
9914
9915                if(_class && /*(_class.templateClass || _class.templateArgs) && */exp.expType)
9916                {
9917                   Class tClass;
9918
9919                   tClass = _class;
9920                   while(tClass && !tClass.templateClass) tClass = tClass.base;
9921
9922                   if(tClass && exp.expType.kind == templateType && exp.expType.templateParameter.type == TemplateParameterType::type)
9923                   {
9924                      int id = 0;
9925                      ClassTemplateParameter curParam = null;
9926                      Class sClass;
9927
9928                      for(sClass = tClass; sClass; sClass = sClass.base)
9929                      {
9930                         id = 0;
9931                         if(sClass.templateClass) sClass = sClass.templateClass;
9932                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9933                         {
9934                            if(curParam.type == TemplateParameterType::type && !strcmp(exp.expType.templateParameter.identifier.string, curParam.name))
9935                            {
9936                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9937                                  id += sClass.templateParams.count;
9938                               break;
9939                            }
9940                            id++;
9941                         }
9942                         if(curParam) break;
9943                      }
9944
9945                      if(curParam && tClass.templateArgs[id].dataTypeString)
9946                      {
9947                         ClassTemplateArgument arg = tClass.templateArgs[id];
9948                         Context context = SetupTemplatesContext(tClass);
9949                         /*if(!arg.dataType)
9950                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
9951                         FreeType(exp.expType);
9952                         exp.expType = ProcessTypeString(arg.dataTypeString, false);
9953                         if(exp.expType)
9954                         {
9955                            if(exp.expType.kind == thisClassType)
9956                            {
9957                               FreeType(exp.expType);
9958                               exp.expType = ReplaceThisClassType(_class);
9959                            }
9960
9961                            if(tClass.templateClass)
9962                               exp.expType.passAsTemplate = true;
9963                            //exp.expType.refCount++;
9964                            if(!exp.destType)
9965                            {
9966                               exp.destType = ProcessTypeString(arg.dataTypeString, false);
9967                               //exp.destType.refCount++;
9968
9969                               if(exp.destType.kind == thisClassType)
9970                               {
9971                                  FreeType(exp.destType);
9972                                  exp.destType = ReplaceThisClassType(_class);
9973                               }
9974                            }
9975                         }
9976                         FinishTemplatesContext(context);
9977                      }
9978                   }
9979                   // TODO: MORE GENERIC SUPPORT FOR DEEPER TYPES
9980                   else if(tClass && exp.expType.kind == pointerType && exp.expType.type && exp.expType.type.kind == templateType && exp.expType.type.templateParameter.type == TemplateParameterType::type)
9981                   {
9982                      int id = 0;
9983                      ClassTemplateParameter curParam = null;
9984                      Class sClass;
9985
9986                      for(sClass = tClass; sClass; sClass = sClass.base)
9987                      {
9988                         id = 0;
9989                         if(sClass.templateClass) sClass = sClass.templateClass;
9990                         for(curParam = sClass.templateParams.first; curParam; curParam = curParam.next)
9991                         {
9992                            if(curParam.type == TemplateParameterType::type &&
9993                               !strcmp(exp.expType.type.templateParameter.identifier.string, curParam.name))
9994                            {
9995                               for(sClass = sClass.base; sClass; sClass = sClass.base)
9996                                  id += sClass.templateParams.count;
9997                               break;
9998                            }
9999                            id++;
10000                         }
10001                         if(curParam) break;
10002                      }
10003
10004                      if(curParam)
10005                      {
10006                         ClassTemplateArgument arg = tClass.templateArgs[id];
10007                         Context context = SetupTemplatesContext(tClass);
10008                         Type basicType;
10009                         /*if(!arg.dataType)
10010                            arg.dataType = ProcessTypeString(arg.dataTypeString, false);*/
10011
10012                         basicType = ProcessTypeString(arg.dataTypeString, false);
10013                         if(basicType)
10014                         {
10015                            if(basicType.kind == thisClassType)
10016                            {
10017                               FreeType(basicType);
10018                               basicType = ReplaceThisClassType(_class);
10019                            }
10020
10021                            /*    DO WE REALLY WANT THIS HERE? IT SEEMS TO BE ONLY USED WITH Array::array which was causing bug 135
10022                            if(tClass.templateClass)
10023                               basicType.passAsTemplate = true;
10024                            */
10025
10026                            FreeType(exp.expType);
10027
10028                            exp.expType = Type { refCount = 1, kind = pointerType, type = basicType };
10029                            //exp.expType.refCount++;
10030                            if(!exp.destType)
10031                            {
10032                               exp.destType = exp.expType;
10033                               exp.destType.refCount++;
10034                            }
10035
10036                            {
10037                               Expression newExp { };
10038                               OldList * specs = MkList();
10039                               Declarator decl;
10040                               decl = SpecDeclFromString(arg.dataTypeString, specs, null);
10041                               *newExp = *exp;
10042                               if(exp.destType) exp.destType.refCount++;
10043                               if(exp.expType)  exp.expType.refCount++;
10044                               exp.type = castExp;
10045                               exp.cast.typeName = MkTypeName(specs, MkDeclaratorPointer(MkPointer(null, null), decl));
10046                               exp.cast.exp = newExp;
10047                               //FreeType(exp.expType);
10048                               //exp.expType = null;
10049                               //ProcessExpressionType(sourceExp);
10050                            }
10051                         }
10052                         FinishTemplatesContext(context);
10053                      }
10054                   }
10055                   else if(tClass && exp.expType.kind == classType && exp.expType._class && strchr(exp.expType._class.string, '<'))
10056                   {
10057                      Class expClass = exp.expType._class.registered;
10058                      if(expClass)
10059                      {
10060                         Class cClass = null;
10061                         int c;
10062                         int p = 0;
10063                         int paramCount = 0;
10064                         int lastParam = -1;
10065                         char templateString[1024];
10066                         ClassTemplateParameter param;
10067                         sprintf(templateString, "%s<", expClass.templateClass.fullName);
10068                         while(cClass != expClass)
10069                         {
10070                            Class sClass;
10071                            for(sClass = expClass; sClass && sClass.base != cClass; sClass = sClass.base);
10072                            cClass = sClass;
10073
10074                            for(param = cClass.templateParams.first; param; param = param.next)
10075                            {
10076                               Class cClassCur = null;
10077                               int c;
10078                               int cp = 0;
10079                               ClassTemplateParameter paramCur = null;
10080                               ClassTemplateArgument arg;
10081                               while(cClassCur != tClass && !paramCur)
10082                               {
10083                                  Class sClassCur;
10084                                  for(sClassCur = tClass; sClassCur && sClassCur.base != cClassCur; sClassCur = sClassCur.base);
10085                                  cClassCur = sClassCur;
10086
10087                                  for(paramCur = cClassCur.templateParams.first; paramCur; paramCur = paramCur.next)
10088                                  {
10089                                     if(!strcmp(paramCur.name, param.name))
10090                                     {
10091
10092                                        break;
10093                                     }
10094                                     cp++;
10095                                  }
10096                               }
10097                               if(paramCur && paramCur.type == TemplateParameterType::type)
10098                                  arg = tClass.templateArgs[cp];
10099                               else
10100                                  arg = expClass.templateArgs[p];
10101
10102                               {
10103                                  char argument[256];
10104                                  argument[0] = '\0';
10105                                  /*if(arg.name)
10106                                  {
10107                                     strcat(argument, arg.name.string);
10108                                     strcat(argument, " = ");
10109                                  }*/
10110                                  switch(param.type)
10111                                  {
10112                                     case expression:
10113                                     {
10114                                        // THIS WHOLE THING IS A WILD GUESS... FIX IT UP
10115                                        char expString[1024];
10116                                        OldList * specs = MkList();
10117                                        Declarator decl = SpecDeclFromString(param.dataTypeString, specs, null);
10118                                        Expression exp;
10119                                        char * string = PrintHexUInt64(arg.expression.ui64);
10120                                        exp = MkExpCast(MkTypeName(specs, decl), MkExpConstant(string));
10121                                        delete string;
10122
10123                                        ProcessExpressionType(exp);
10124                                        ComputeExpression(exp);
10125                                        expString[0] = '\0';
10126                                        PrintExpression(exp, expString);
10127                                        strcat(argument, expString);
10128                                        // delete exp;
10129                                        FreeExpression(exp);
10130                                        break;
10131                                     }
10132                                     case identifier:
10133                                     {
10134                                        strcat(argument, arg.member.name);
10135                                        break;
10136                                     }
10137                                     case TemplateParameterType::type:
10138                                     {
10139                                        if(arg.dataTypeString && (!param.defaultArg.dataTypeString || strcmp(arg.dataTypeString, param.defaultArg.dataTypeString)))
10140                                           strcat(argument, arg.dataTypeString);
10141                                        break;
10142                                     }
10143                                  }
10144                                  if(argument[0])
10145                                  {
10146                                     if(paramCount) strcat(templateString, ", ");
10147                                     if(lastParam != p - 1)
10148                                     {
10149                                        strcat(templateString, param.name);
10150                                        strcat(templateString, " = ");
10151                                     }
10152                                     strcat(templateString, argument);
10153                                     paramCount++;
10154                                     lastParam = p;
10155                                  }
10156                               }
10157                               p++;
10158                            }
10159                         }
10160                         {
10161                            int len = strlen(templateString);
10162                            if(templateString[len-1] == '>') templateString[len++] = ' ';
10163                            templateString[len++] = '>';
10164                            templateString[len++] = '\0';
10165                         }
10166
10167                         FreeType(exp.expType);
10168                         {
10169                            Context context = SetupTemplatesContext(tClass);
10170                            exp.expType = ProcessTypeString(templateString, false);
10171                            FinishTemplatesContext(context);
10172                         }
10173                      }
10174                   }
10175                }
10176             }
10177             else
10178                Compiler_Error($"undefined class %s\n", (id && (!id._class || id._class.name))? (id.classSym ? id.classSym.string : (type._class ? type._class.string : null)) : "(null)");
10179          }
10180          else if(type && (type.kind == structType || type.kind == unionType))
10181          {
10182             Type memberType = exp.member.member ? FindMember(type, exp.member.member.string) : null;
10183             if(memberType)
10184             {
10185                exp.expType = memberType;
10186                if(memberType)
10187                   memberType.refCount++;
10188             }
10189          }
10190          else
10191          {
10192             char expString[10240];
10193             expString[0] = '\0';
10194             if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10195             Compiler_Error($"member operator on non-structure type expression %s\n", expString);
10196          }
10197
10198          if(exp.expType && exp.expType.kind == thisClassType && (!exp.destType || exp.destType.kind != thisClassType))
10199          {
10200             if(type && (type.kind == classType || type.kind == subClassType || type.kind == intType || type.kind == enumType))
10201             {
10202                Identifier id = exp.member.member;
10203                Class _class = (id && (!id._class || id._class.name))? ( id.classSym ? id.classSym.registered : (type._class ? type._class.registered : null)) : null;
10204                if(_class)
10205                {
10206                   FreeType(exp.expType);
10207                   exp.expType = ReplaceThisClassType(_class);
10208                }
10209             }
10210          }
10211          yylloc = oldyylloc;
10212          break;
10213       }
10214       // Convert x->y into (*x).y
10215       case pointerExp:
10216       {
10217          Type destType = exp.destType;
10218
10219          // DOING THIS LATER NOW...
10220          if(exp.member.member && exp.member.member._class && exp.member.member._class.name)
10221          {
10222             exp.member.member.classSym = exp.member.member._class.symbol; // FindClass(exp.member.member._class.name);
10223             /* TODO: Name Space Fix ups
10224             if(!exp.member.member.classSym)
10225                exp.member.member.nameSpace = eSystem_FindNameSpace(privateModule, exp.member.member._class.name);
10226             */
10227          }
10228
10229          exp.member.exp = MkExpBrackets(MkListOne(MkExpOp(null, '*', exp.member.exp)));
10230          exp.type = memberExp;
10231          if(destType)
10232             destType.count++;
10233          ProcessExpressionType(exp);
10234          if(destType)
10235             destType.count--;
10236          break;
10237       }
10238       case classSizeExp:
10239       {
10240          //ComputeExpression(exp);
10241
10242          Symbol classSym = exp._class.symbol; // FindClass(exp._class.name);
10243          if(classSym && classSym.registered)
10244          {
10245             if(classSym.registered.type == noHeadClass)
10246             {
10247                char name[1024];
10248                name[0] = '\0';
10249                DeclareStruct(classSym.string, false);
10250                FreeSpecifier(exp._class);
10251                exp.type = typeSizeExp;
10252                FullClassNameCat(name, classSym.string, false);
10253                exp.typeName = MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(name), null)), null);
10254             }
10255             else
10256             {
10257                if(classSym.registered.fixed)
10258                {
10259                   FreeSpecifier(exp._class);
10260                   exp.constant = PrintUInt(classSym.registered.templateClass ? classSym.registered.templateClass.structSize : classSym.registered.structSize);
10261                   exp.type = constantExp;
10262                }
10263                else
10264                {
10265                   char className[1024];
10266                   strcpy(className, "__ecereClass_");
10267                   FullClassNameCat(className, classSym.string, true);
10268                   MangleClassName(className);
10269
10270                   DeclareClass(classSym, className);
10271
10272                   FreeExpContents(exp);
10273                   exp.type = pointerExp;
10274                   exp.member.exp = MkExpIdentifier(MkIdentifier(className));
10275                   exp.member.member = MkIdentifier("structSize");
10276                }
10277             }
10278          }
10279
10280          exp.expType = Type
10281          {
10282             refCount = 1;
10283             kind = intType;
10284          };
10285          // exp.isConstant = true;
10286          break;
10287       }
10288       case typeSizeExp:
10289       {
10290          Type type = ProcessType(exp.typeName.qualifiers, exp.typeName.declarator);
10291
10292          exp.expType = Type
10293          {
10294             refCount = 1;
10295             kind = intType;
10296          };
10297          exp.isConstant = true;
10298
10299          DeclareType(type, false, false);
10300          FreeType(type);
10301          break;
10302       }
10303       case castExp:
10304       {
10305          Type type = ProcessType(exp.cast.typeName.qualifiers, exp.cast.typeName.declarator);
10306          type.count = 1;
10307          FreeType(exp.cast.exp.destType);
10308          exp.cast.exp.destType = type;
10309          type.refCount++;
10310          ProcessExpressionType(exp.cast.exp);
10311          type.count = 0;
10312          exp.expType = type;
10313          //type.refCount++;
10314
10315          // if(!NeedCast(exp.cast.exp.expType, exp.cast.exp.destType))
10316          if(!exp.cast.exp.needCast && !NeedCast(exp.cast.exp.expType, type))
10317          {
10318             void * prev = exp.prev, * next = exp.next;
10319             Type expType = exp.cast.exp.destType;
10320             Expression castExp = exp.cast.exp;
10321             Type destType = exp.destType;
10322
10323             if(expType) expType.refCount++;
10324
10325             //FreeType(exp.destType);
10326             FreeType(exp.expType);
10327             FreeTypeName(exp.cast.typeName);
10328
10329             *exp = *castExp;
10330             FreeType(exp.expType);
10331             FreeType(exp.destType);
10332
10333             exp.expType = expType;
10334             exp.destType = destType;
10335
10336             delete castExp;
10337
10338             exp.prev = prev;
10339             exp.next = next;
10340
10341          }
10342          else
10343          {
10344             exp.isConstant = exp.cast.exp.isConstant;
10345          }
10346          //FreeType(type);
10347          break;
10348       }
10349       case extensionInitializerExp:
10350       {
10351          Type type = ProcessType(exp.initializer.typeName.qualifiers, exp.initializer.typeName.declarator);
10352          // We have yet to support this... ( { } initializers are currently processed inside ProcessDeclaration()'s initDeclaration case statement
10353          // ProcessInitializer(exp.initializer.initializer, type);
10354          exp.expType = type;
10355          break;
10356       }
10357       case vaArgExp:
10358       {
10359          Type type = ProcessType(exp.vaArg.typeName.qualifiers, exp.vaArg.typeName.declarator);
10360          ProcessExpressionType(exp.vaArg.exp);
10361          exp.expType = type;
10362          break;
10363       }
10364       case conditionExp:
10365       {
10366          Expression e;
10367          exp.isConstant = true;
10368
10369          FreeType(exp.cond.cond.destType);
10370          exp.cond.cond.destType = MkClassType("bool");
10371          exp.cond.cond.destType.truth = true;
10372          ProcessExpressionType(exp.cond.cond);
10373          if(!exp.cond.cond.isConstant)
10374             exp.isConstant = false;
10375          for(e = exp.cond.exp->first; e; e = e.next)
10376          {
10377             if(!e.next)
10378             {
10379                FreeType(e.destType);
10380                e.destType = exp.destType;
10381                if(e.destType) e.destType.refCount++;
10382             }
10383             ProcessExpressionType(e);
10384             if(!e.next)
10385             {
10386                exp.expType = e.expType;
10387                if(e.expType) e.expType.refCount++;
10388             }
10389             if(!e.isConstant)
10390                exp.isConstant = false;
10391          }
10392
10393          FreeType(exp.cond.elseExp.destType);
10394          // Added this check if we failed to find an expType
10395          // exp.cond.elseExp.destType = exp.expType ? exp.expType : exp.destType;
10396
10397          // Reversed it...
10398          exp.cond.elseExp.destType = exp.destType ? exp.destType : exp.expType;
10399
10400          if(exp.cond.elseExp.destType)
10401             exp.cond.elseExp.destType.refCount++;
10402          ProcessExpressionType(exp.cond.elseExp);
10403
10404          // FIXED THIS: Was done before calling process on elseExp
10405          if(!exp.cond.elseExp.isConstant)
10406             exp.isConstant = false;
10407          break;
10408       }
10409       case extensionCompoundExp:
10410       {
10411          if(exp.compound && exp.compound.compound.statements && exp.compound.compound.statements->last)
10412          {
10413             Statement last = exp.compound.compound.statements->last;
10414             if(last.type == expressionStmt && last.expressions && last.expressions->last)
10415             {
10416                ((Expression)last.expressions->last).destType = exp.destType;
10417                if(exp.destType)
10418                   exp.destType.refCount++;
10419             }
10420             ProcessStatement(exp.compound);
10421             exp.expType = (last.expressions && last.expressions->last) ? ((Expression)last.expressions->last).expType : null;
10422             if(exp.expType)
10423                exp.expType.refCount++;
10424          }
10425          break;
10426       }
10427       case classExp:
10428       {
10429          Specifier spec = exp._classExp.specifiers->first;
10430          if(spec && spec.type == nameSpecifier)
10431          {
10432             exp.expType = MkClassType(spec.name);
10433             exp.expType.kind = subClassType;
10434             exp.byReference = true;
10435          }
10436          else
10437          {
10438             exp.expType = MkClassType("ecere::com::Class");
10439             exp.byReference = true;
10440          }
10441          break;
10442       }
10443       case classDataExp:
10444       {
10445          Class _class = thisClass ? thisClass : currentClass;
10446          if(_class)
10447          {
10448             Identifier id = exp.classData.id;
10449             char structName[1024];
10450             Expression classExp;
10451             strcpy(structName, "__ecereClassData_");
10452             FullClassNameCat(structName, _class.fullName, false);
10453             exp.type = pointerExp;
10454             exp.member.member = id;
10455             if(curCompound && FindSymbol("this", curContext, curCompound.compound.context, false, false))
10456                classExp = MkExpMember(MkExpIdentifier(MkIdentifier("this")), MkIdentifier("_class"));
10457             else
10458                classExp = MkExpIdentifier(MkIdentifier("class"));
10459
10460             exp.member.exp = MkExpBrackets(MkListOne(MkExpCast(
10461                MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)),
10462                   MkExpBrackets(MkListOne(MkExpOp(
10463                      MkExpCast(MkTypeName(MkListOne(MkSpecifier(CHAR)), MkDeclaratorPointer(MkPointer(null,null), null)),
10464                         MkExpMember(classExp, MkIdentifier("data"))), '+',
10465                            MkExpMember(MkExpClass(MkListOne(MkSpecifierName(_class.fullName)), null), MkIdentifier("offsetClass")))))
10466                      )));
10467
10468             ProcessExpressionType(exp);
10469             return;
10470          }
10471          break;
10472       }
10473       case arrayExp:
10474       {
10475          Type type = null;
10476          char * typeString = null;
10477          char typeStringBuf[1024];
10478          if(exp.destType && exp.destType.kind == classType && exp.destType._class && exp.destType._class.registered &&
10479             exp.destType._class.registered != containerClass && eClass_IsDerived(exp.destType._class.registered, containerClass))
10480          {
10481             Class templateClass = exp.destType._class.registered;
10482             typeString = templateClass.templateArgs[2].dataTypeString;
10483          }
10484          else if(exp.list)
10485          {
10486             // Guess type from expressions in the array
10487             Expression e;
10488             for(e = exp.list->first; e; e = e.next)
10489             {
10490                ProcessExpressionType(e);
10491                if(e.expType)
10492                {
10493                   if(!type) { type = e.expType; type.refCount++; }
10494                   else
10495                   {
10496                      // if(!MatchType(e.expType, type, null, null, null, false, false, false))
10497                      if(!MatchTypeExpression(e, type, null, false))
10498                      {
10499                         FreeType(type);
10500                         type = e.expType;
10501                         e.expType = null;
10502
10503                         e = exp.list->first;
10504                         ProcessExpressionType(e);
10505                         if(e.expType)
10506                         {
10507                            //if(!MatchTypes(e.expType, type, null, null, null, false, false, false))
10508                            if(!MatchTypeExpression(e, type, null, false))
10509                            {
10510                               FreeType(e.expType);
10511                               e.expType = null;
10512                               FreeType(type);
10513                               type = null;
10514                               break;
10515                            }
10516                         }
10517                      }
10518                   }
10519                   if(e.expType)
10520                   {
10521                      FreeType(e.expType);
10522                      e.expType = null;
10523                   }
10524                }
10525             }
10526             if(type)
10527             {
10528                typeStringBuf[0] = '\0';
10529                PrintTypeNoConst(type, typeStringBuf, false, true);
10530                typeString = typeStringBuf;
10531                FreeType(type);
10532                type = null;
10533             }
10534          }
10535          if(typeString)
10536          {
10537             /*
10538             (Container)& (struct BuiltInContainer)
10539             {
10540                ._vTbl = class(BuiltInContainer)._vTbl,
10541                ._class = class(BuiltInContainer),
10542                .refCount = 0,
10543                .data = (int[]){ 1, 7, 3, 4, 5 },
10544                .count = 5,
10545                .type = class(int),
10546             }
10547             */
10548             char templateString[1024];
10549             OldList * initializers = MkList();
10550             OldList * structInitializers = MkList();
10551             OldList * specs = MkList();
10552             Expression expExt;
10553             Declarator decl = SpecDeclFromString(typeString, specs, null);
10554             sprintf(templateString, "Container<%s>", typeString);
10555
10556             if(exp.list)
10557             {
10558                Expression e;
10559                type = ProcessTypeString(typeString, false);
10560                while(e = exp.list->first)
10561                {
10562                   exp.list->Remove(e);
10563                   e.destType = type;
10564                   type.refCount++;
10565                   ProcessExpressionType(e);
10566                   ListAdd(initializers, MkInitializerAssignment(e));
10567                }
10568                FreeType(type);
10569                delete exp.list;
10570             }
10571
10572             DeclareStruct("ecere::com::BuiltInContainer", false);
10573
10574             ListAdd(structInitializers, /*MkIdentifier("_vTbl")*/    MkInitializerAssignment(MkExpMember(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null), MkIdentifier("_vTbl"))));
10575                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10576             ListAdd(structInitializers, /*MkIdentifier("_class")*/   MkInitializerAssignment(MkExpClass(MkListOne(MkSpecifierName("BuiltInContainer")), null)));
10577                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10578             ListAdd(structInitializers, /*MkIdentifier("_refCount")*/MkInitializerAssignment(MkExpConstant("0")));
10579                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10580             ListAdd(structInitializers, /*MkIdentifier("data")*/     MkInitializerAssignment(MkExpExtensionInitializer(
10581                MkTypeName(specs, MkDeclaratorArray(decl, null)),
10582                MkInitializerList(initializers))));
10583                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10584             ListAdd(structInitializers, /*MkIdentifier("count")*/    MkInitializerAssignment({ type = constantExp, constant = PrintString(initializers->count) }));
10585                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10586             ListAdd(structInitializers, /*MkIdentifier("type")*/     MkInitializerAssignment(MkExpClass(CopyList(specs, CopySpecifier), CopyDeclarator(decl))));
10587                ProcessExpressionType(((Initializer)structInitializers->last).exp);
10588             exp.expType = ProcessTypeString(templateString, false);
10589             exp.type = bracketsExp;
10590             exp.list = MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName(templateString)), null),
10591                MkExpOp(null, '&',
10592                expExt = MkExpExtensionInitializer(MkTypeName(MkListOne(MkSpecifierName("BuiltInContainer")), null),
10593                   MkInitializerList(structInitializers)))));
10594             ProcessExpressionType(expExt);
10595          }
10596          else
10597          {
10598             exp.expType = ProcessTypeString("Container", false);
10599             Compiler_Error($"Couldn't determine type of array elements\n");
10600          }
10601          break;
10602       }
10603    }
10604
10605    if(exp.expType && exp.expType.kind == thisClassType && thisClass && (!exp.destType || exp.destType.kind != thisClassType))
10606    {
10607       FreeType(exp.expType);
10608       exp.expType = ReplaceThisClassType(thisClass);
10609    }
10610
10611    // Resolve structures here
10612    if(exp.expType && (exp.expType.kind == structType || exp.expType.kind == unionType || exp.expType.kind == enumType) && !exp.expType.members.first && exp.expType.enumName)
10613    {
10614       Symbol symbol = FindSymbol(exp.expType.enumName, curContext, globalContext, true, false);
10615       // TODO: Fix members reference...
10616       if(symbol)
10617       {
10618          if(exp.expType.kind != enumType)
10619          {
10620             Type member;
10621             String enumName = CopyString(exp.expType.enumName);
10622
10623             // Fixed a memory leak on self-referencing C structs typedefs
10624             // by instantiating a new type rather than simply copying members
10625             // into exp.expType
10626             FreeType(exp.expType);
10627             exp.expType = Type { };
10628             exp.expType.kind = symbol.type.kind;
10629             exp.expType.refCount++;
10630             exp.expType.enumName = enumName;
10631
10632             exp.expType.members = symbol.type.members;
10633             for(member = symbol.type.members.first; member; member = member.next)
10634                member.refCount++;
10635          }
10636          else
10637          {
10638             NamedLink member;
10639             for(member = symbol.type.members.first; member; member = member.next)
10640             {
10641                NamedLink value { name = CopyString(member.name) };
10642                exp.expType.members.Add(value);
10643             }
10644          }
10645       }
10646    }
10647
10648    yylloc = exp.loc;
10649    if(exp.destType && (exp.destType.kind == voidType || exp.destType.kind == dummyType) );
10650    else if(exp.destType && !exp.destType.keepCast)
10651    {
10652       if(!CheckExpressionType(exp, exp.destType, false))
10653       {
10654          if(!exp.destType.count || unresolved)
10655          {
10656             if(!exp.expType)
10657             {
10658                yylloc = exp.loc;
10659                if(exp.destType.kind != ellipsisType)
10660                {
10661                   char type2[1024];
10662                   type2[0] = '\0';
10663                   if(inCompiler)
10664                   {
10665                      char expString[10240];
10666                      expString[0] = '\0';
10667
10668                      PrintType(exp.destType, type2, false, true);
10669
10670                      if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10671                      if(unresolved)
10672                         Compiler_Error($"unresolved identifier %s; expected %s\n", expString, type2);
10673                      else if(exp.type != dummyExp)
10674                         Compiler_Error($"couldn't determine type of %s; expected %s\n", expString, type2);
10675                   }
10676                }
10677                else
10678                {
10679                   char expString[10240] ;
10680                   expString[0] = '\0';
10681                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10682
10683                   if(unresolved)
10684                      Compiler_Error($"unresolved identifier %s\n", expString);
10685                   else if(exp.type != dummyExp)
10686                      Compiler_Error($"couldn't determine type of %s\n", expString);
10687                }
10688             }
10689             else
10690             {
10691                char type1[1024];
10692                char type2[1024];
10693                type1[0] = '\0';
10694                type2[0] = '\0';
10695                if(inCompiler)
10696                {
10697                   PrintType(exp.expType, type1, false, true);
10698                   PrintType(exp.destType, type2, false, true);
10699                }
10700
10701                //CheckExpressionType(exp, exp.destType, false);
10702
10703                if(exp.destType.truth && exp.destType._class && exp.destType._class.registered && !strcmp(exp.destType._class.registered.name, "bool") &&
10704                   exp.expType.kind != voidType && exp.expType.kind != structType && exp.expType.kind != unionType &&
10705                   (exp.expType.kind != classType || exp.expType.classObjectType || (exp.expType._class && exp.expType._class.registered && exp.expType._class.registered.type != structClass)));
10706                else
10707                {
10708                   char expString[10240];
10709                   expString[0] = '\0';
10710                   if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10711
10712 #ifdef _DEBUG
10713                   CheckExpressionType(exp, exp.destType, false);
10714 #endif
10715                   // Flex & Bison generate code that triggers this, so we ignore it for a quiet sdk build:
10716                   if(!sourceFile || (strcmp(sourceFile, "src\\lexer.ec") && strcmp(sourceFile, "src/lexer.ec") && strcmp(sourceFile, "src\\grammar.ec") && strcmp(sourceFile, "src/grammar.ec")))
10717                      Compiler_Warning($"incompatible expression %s (%s); expected %s\n", expString, type1, type2);
10718
10719                   // TO CHECK: FORCING HERE TO HELP DEBUGGER
10720                   FreeType(exp.expType);
10721                   exp.destType.refCount++;
10722                   exp.expType = exp.destType;
10723                }
10724             }
10725          }
10726       }
10727       else if(exp.destType && exp.destType.kind == ellipsisType && exp.expType && exp.expType.passAsTemplate)
10728       {
10729          Expression newExp { };
10730          char typeString[1024];
10731          OldList * specs = MkList();
10732          Declarator decl;
10733
10734          typeString[0] = '\0';
10735
10736          *newExp = *exp;
10737
10738          if(exp.expType)  exp.expType.refCount++;
10739          if(exp.expType)  exp.expType.refCount++;
10740          exp.type = castExp;
10741          newExp.destType = exp.expType;
10742
10743          PrintType(exp.expType, typeString, false, false);
10744          decl = SpecDeclFromString(typeString, specs, null);
10745
10746          exp.cast.typeName = MkTypeName(specs, decl);
10747          exp.cast.exp = newExp;
10748       }
10749    }
10750    else if(unresolved)
10751    {
10752       if(exp.identifier._class && exp.identifier._class.name)
10753          Compiler_Error($"unresolved identifier %s::%s\n", exp.identifier._class.name, exp.identifier.string);
10754       else if(exp.identifier.string && exp.identifier.string[0])
10755          Compiler_Error($"unresolved identifier %s\n", exp.identifier.string);
10756    }
10757    else if(!exp.expType && exp.type != dummyExp)
10758    {
10759       char expString[10240];
10760       expString[0] = '\0';
10761       if(inCompiler) { PrintExpression(exp, expString); ChangeCh(expString, '\n', ' '); }
10762       Compiler_Error($"couldn't determine type of %s\n", expString);
10763    }
10764
10765    // Let's try to support any_object & typed_object here:
10766    if(inCompiler)
10767       ApplyAnyObjectLogic(exp);
10768
10769    // Mark nohead classes as by reference, unless we're casting them to an integral type
10770    if(!notByReference && exp.expType && exp.expType.kind == classType && exp.expType._class && exp.expType._class.registered &&
10771       exp.expType._class.registered.type == noHeadClass && (!exp.destType ||
10772          (exp.destType.kind != intType && exp.destType.kind != int64Type && exp.destType.kind != intPtrType && exp.destType.kind != intSizeType &&
10773           exp.destType.kind != longType && exp.destType.kind != shortType && exp.destType.kind != charType && exp.destType.kind != _BoolType)))
10774    {
10775       exp.byReference = true;
10776    }
10777    yylloc = oldyylloc;
10778 }
10779
10780 static void FindNextDataMember(Class _class, Class * curClass, DataMember * curMember, DataMember * subMemberStack, int * subMemberStackPos)
10781 {
10782    // THIS CODE WILL FIND NEXT MEMBER...
10783    if(*curMember)
10784    {
10785       *curMember = (*curMember).next;
10786
10787       if(subMemberStackPos && *subMemberStackPos > 0 && subMemberStack[*subMemberStackPos-1].type == unionMember)
10788       {
10789          *curMember = subMemberStack[--(*subMemberStackPos)];
10790          *curMember = (*curMember).next;
10791       }
10792
10793       // SKIP ALL PROPERTIES HERE...
10794       while((*curMember) && (*curMember).isProperty)
10795          *curMember = (*curMember).next;
10796
10797       if(subMemberStackPos)
10798       {
10799          while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10800          {
10801             subMemberStack[(*subMemberStackPos)++] = *curMember;
10802
10803             *curMember = (*curMember).members.first;
10804             while(*curMember && (*curMember).isProperty)
10805                *curMember = (*curMember).next;
10806          }
10807       }
10808    }
10809    while(!*curMember)
10810    {
10811       if(!*curMember)
10812       {
10813          if(subMemberStackPos && *subMemberStackPos)
10814          {
10815             *curMember = subMemberStack[--(*subMemberStackPos)];
10816             *curMember = (*curMember).next;
10817          }
10818          else
10819          {
10820             Class lastCurClass = *curClass;
10821
10822             if(*curClass == _class) break;     // REACHED THE END
10823
10824             for(*curClass = _class; (*curClass).base != lastCurClass && (*curClass).base.type != systemClass; *curClass = (*curClass).base);
10825             *curMember = (*curClass).membersAndProperties.first;
10826          }
10827
10828          while((*curMember) && (*curMember).isProperty)
10829             *curMember = (*curMember).next;
10830          if(subMemberStackPos)
10831          {
10832             while((*curMember) && !(*curMember).isProperty && !(*curMember).name && ((*curMember).type == structMember || (*curMember).type == unionMember))
10833             {
10834                subMemberStack[(*subMemberStackPos)++] = *curMember;
10835
10836                *curMember = (*curMember).members.first;
10837                while(*curMember && (*curMember).isProperty)
10838                   *curMember = (*curMember).next;
10839             }
10840          }
10841       }
10842    }
10843 }
10844
10845
10846 static void ProcessInitializer(Initializer init, Type type)
10847 {
10848    switch(init.type)
10849    {
10850       case expInitializer:
10851          if(!init.exp || init.exp.type != instanceExp || !init.exp.instance || init.exp.instance._class || !type || type.kind == classType)
10852          {
10853             // TESTING THIS FOR SHUTTING = 0 WARNING
10854             if(init.exp && !init.exp.destType)
10855             {
10856                FreeType(init.exp.destType);
10857                init.exp.destType = type;
10858                if(type) type.refCount++;
10859             }
10860             if(init.exp)
10861             {
10862                ProcessExpressionType(init.exp);
10863                init.isConstant = init.exp.isConstant;
10864             }
10865             break;
10866          }
10867          else
10868          {
10869             Expression exp = init.exp;
10870             Instantiation inst = exp.instance;
10871             MembersInit members;
10872
10873             init.type = listInitializer;
10874             init.list = MkList();
10875
10876             if(inst.members)
10877             {
10878                for(members = inst.members->first; members; members = members.next)
10879                {
10880                   if(members.type == dataMembersInit)
10881                   {
10882                      MemberInit member;
10883                      for(member = members.dataMembers->first; member; member = member.next)
10884                      {
10885                         ListAdd(init.list, member.initializer);
10886                         member.initializer = null;
10887                      }
10888                   }
10889                   // Discard all MembersInitMethod
10890                }
10891             }
10892             FreeExpression(exp);
10893          }
10894       case listInitializer:
10895       {
10896          Initializer i;
10897          Type initializerType = null;
10898          Class curClass = null;
10899          DataMember curMember = null;
10900          DataMember subMemberStack[256];
10901          int subMemberStackPos = 0;
10902
10903          if(type && type.kind == arrayType)
10904             initializerType = Dereference(type);
10905          else if(type && (type.kind == structType || type.kind == unionType))
10906             initializerType = type.members.first;
10907
10908          for(i = init.list->first; i; i = i.next)
10909          {
10910             if(type && type.kind == classType && type._class && type._class.registered)
10911             {
10912                // 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)
10913                FindNextDataMember(type._class.registered, &curClass, &curMember, subMemberStack, &subMemberStackPos);
10914                // TODO: Generate error on initializing a private data member this way from another module...
10915                if(curMember)
10916                {
10917                   if(!curMember.dataType)
10918                      curMember.dataType = ProcessTypeString(curMember.dataTypeString, false);
10919                   initializerType = curMember.dataType;
10920                }
10921             }
10922             ProcessInitializer(i, initializerType);
10923             if(initializerType && type && (type.kind == structType || type.kind == unionType))
10924                initializerType = initializerType.next;
10925             if(!i.isConstant)
10926                init.isConstant = false;
10927          }
10928
10929          if(type && type.kind == arrayType)
10930             FreeType(initializerType);
10931
10932          if(type && type.kind != arrayType && type.kind != structType && type.kind != unionType && (type.kind != classType || !type._class.registered || type._class.registered.type != structClass))
10933          {
10934             Compiler_Error($"Assigning list initializer to non list\n");
10935          }
10936          break;
10937       }
10938    }
10939 }
10940
10941 static void ProcessSpecifier(Specifier spec, bool declareStruct)
10942 {
10943    switch(spec.type)
10944    {
10945       case baseSpecifier:
10946       {
10947          if(spec.specifier == THISCLASS)
10948          {
10949             if(thisClass)
10950             {
10951                spec.type = nameSpecifier;
10952                spec.name = ReplaceThisClass(thisClass);
10953                spec.symbol = FindClass(spec.name);
10954                ProcessSpecifier(spec, declareStruct);
10955             }
10956          }
10957          break;
10958       }
10959       case nameSpecifier:
10960       {
10961          Symbol symbol = FindType(curContext, spec.name);
10962          if(symbol)
10963             DeclareType(symbol.type, true, true);
10964          else if((symbol = spec.symbol /*FindClass(spec.name)*/) && symbol.registered && symbol.registered.type == structClass && declareStruct)
10965             DeclareStruct(spec.name, false);
10966          break;
10967       }
10968       case enumSpecifier:
10969       {
10970          Enumerator e;
10971          if(spec.list)
10972          {
10973             for(e = spec.list->first; e; e = e.next)
10974             {
10975                if(e.exp)
10976                   ProcessExpressionType(e.exp);
10977             }
10978          }
10979          break;
10980       }
10981       case structSpecifier:
10982       case unionSpecifier:
10983       {
10984          if(spec.definitions)
10985          {
10986             ClassDef def;
10987             Symbol symbol = spec.id ? FindClass(spec.id.string) : null;
10988             //if(symbol)
10989                ProcessClass(spec.definitions, symbol);
10990             /*else
10991             {
10992                for(def = spec.definitions->first; def; def = def.next)
10993                {
10994                   //if(def.type == declarationClassDef && def.decl && def.decl.type == DeclarationStruct)
10995                      ProcessDeclaration(def.decl);
10996                }
10997             }*/
10998          }
10999          break;
11000       }
11001       /*
11002       case classSpecifier:
11003       {
11004          Symbol classSym = FindClass(spec.name);
11005          if(classSym && classSym.registered && classSym.registered.type == structClass)
11006             DeclareStruct(spec.name, false);
11007          break;
11008       }
11009       */
11010    }
11011 }
11012
11013
11014 static void ProcessDeclarator(Declarator decl)
11015 {
11016    switch(decl.type)
11017    {
11018       case identifierDeclarator:
11019          if(decl.identifier.classSym /* TODO: Name Space Fix ups  || decl.identifier.nameSpace*/)
11020          {
11021             FreeSpecifier(decl.identifier._class);
11022             decl.identifier._class = null;
11023          }
11024          break;
11025       case arrayDeclarator:
11026          if(decl.array.exp)
11027             ProcessExpressionType(decl.array.exp);
11028       case structDeclarator:
11029       case bracketsDeclarator:
11030       case functionDeclarator:
11031       case pointerDeclarator:
11032       case extendedDeclarator:
11033       case extendedDeclaratorEnd:
11034          if(decl.declarator)
11035             ProcessDeclarator(decl.declarator);
11036          if(decl.type == functionDeclarator)
11037          {
11038             Identifier id = GetDeclId(decl);
11039             if(id && id._class)
11040             {
11041                TypeName param
11042                {
11043                   qualifiers = MkListOne(id._class);
11044                   declarator = null;
11045                };
11046                if(!decl.function.parameters)
11047                   decl.function.parameters = MkList();
11048                decl.function.parameters->Insert(null, param);
11049                id._class = null;
11050             }
11051             if(decl.function.parameters)
11052             {
11053                TypeName param;
11054
11055                for(param = decl.function.parameters->first; param; param = param.next)
11056                {
11057                   if(param.qualifiers && param.qualifiers->first)
11058                   {
11059                      Specifier spec = param.qualifiers->first;
11060                      if(spec && spec.specifier == TYPED_OBJECT)
11061                      {
11062                         Declarator d = param.declarator;
11063                         TypeName newParam
11064                         {
11065                            qualifiers = MkListOne(MkSpecifier(VOID));
11066                            declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11067                         };
11068
11069                         FreeList(param.qualifiers, FreeSpecifier);
11070
11071                         param.qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
11072                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
11073
11074                         decl.function.parameters->Insert(param, newParam);
11075                         param = newParam;
11076                      }
11077                      else if(spec && spec.specifier == ANY_OBJECT)
11078                      {
11079                         Declarator d = param.declarator;
11080
11081                         FreeList(param.qualifiers, FreeSpecifier);
11082
11083                         param.qualifiers = MkListOne(MkSpecifier(VOID));
11084                         param.declarator = MkDeclaratorPointer(MkPointer(null,null), d);
11085                      }
11086                      else if(spec.specifier == THISCLASS)
11087                      {
11088                         if(thisClass)
11089                         {
11090                            spec.type = nameSpecifier;
11091                            spec.name = ReplaceThisClass(thisClass);
11092                            spec.symbol = FindClass(spec.name);
11093                            ProcessSpecifier(spec, false);
11094                         }
11095                      }
11096                   }
11097
11098                   if(param.declarator)
11099                      ProcessDeclarator(param.declarator);
11100                }
11101             }
11102          }
11103          break;
11104    }
11105 }
11106
11107 static void ProcessDeclaration(Declaration decl)
11108 {
11109    yylloc = decl.loc;
11110    switch(decl.type)
11111    {
11112       case initDeclaration:
11113       {
11114          bool declareStruct = false;
11115          /*
11116          lineNum = decl.pos.line;
11117          column = decl.pos.col;
11118          */
11119
11120          if(decl.declarators)
11121          {
11122             InitDeclarator d;
11123
11124             for(d = decl.declarators->first; d; d = d.next)
11125             {
11126                Type type, subType;
11127                ProcessDeclarator(d.declarator);
11128
11129                type = ProcessType(decl.specifiers, d.declarator);
11130
11131                if(d.initializer)
11132                {
11133                   ProcessInitializer(d.initializer, type);
11134
11135                   // Change "ColorRGB a = ColorRGB { 1,2,3 } => ColorRGB a { 1,2,3 }
11136
11137                   if(decl.declarators->count == 1 && d.initializer.type == expInitializer &&
11138                      d.initializer.exp.type == instanceExp)
11139                   {
11140                      if(type.kind == classType && type._class ==
11141                         d.initializer.exp.expType._class)
11142                      {
11143                         Instantiation inst = d.initializer.exp.instance;
11144                         inst.exp = MkExpIdentifier(CopyIdentifier(GetDeclId(d.declarator)));
11145
11146                         d.initializer.exp.instance = null;
11147                         if(decl.specifiers)
11148                            FreeList(decl.specifiers, FreeSpecifier);
11149                         FreeList(decl.declarators, FreeInitDeclarator);
11150
11151                         d = null;
11152
11153                         decl.type = instDeclaration;
11154                         decl.inst = inst;
11155                      }
11156                   }
11157                }
11158                for(subType = type; subType;)
11159                {
11160                   if(subType.kind == classType)
11161                   {
11162                      declareStruct = true;
11163                      break;
11164                   }
11165                   else if(subType.kind == pointerType)
11166                      break;
11167                   else if(subType.kind == arrayType)
11168                      subType = subType.arrayType;
11169                   else
11170                      break;
11171                }
11172
11173                FreeType(type);
11174                if(!d) break;
11175             }
11176          }
11177
11178          if(decl.specifiers)
11179          {
11180             Specifier s;
11181             for(s = decl.specifiers->first; s; s = s.next)
11182             {
11183                ProcessSpecifier(s, declareStruct);
11184             }
11185          }
11186          break;
11187       }
11188       case instDeclaration:
11189       {
11190          ProcessInstantiationType(decl.inst);
11191          break;
11192       }
11193       case structDeclaration:
11194       {
11195          Specifier spec;
11196          Declarator d;
11197          bool declareStruct = false;
11198
11199          if(decl.declarators)
11200          {
11201             for(d = decl.declarators->first; d; d = d.next)
11202             {
11203                Type type = ProcessType(decl.specifiers, d.declarator);
11204                Type subType;
11205                ProcessDeclarator(d);
11206                for(subType = type; subType;)
11207                {
11208                   if(subType.kind == classType)
11209                   {
11210                      declareStruct = true;
11211                      break;
11212                   }
11213                   else if(subType.kind == pointerType)
11214                      break;
11215                   else if(subType.kind == arrayType)
11216                      subType = subType.arrayType;
11217                   else
11218                      break;
11219                }
11220                FreeType(type);
11221             }
11222          }
11223          if(decl.specifiers)
11224          {
11225             for(spec = decl.specifiers->first; spec; spec = spec.next)
11226                ProcessSpecifier(spec, declareStruct);
11227          }
11228          break;
11229       }
11230    }
11231 }
11232
11233 static FunctionDefinition curFunction;
11234
11235 static void CreateFireWatcher(Property prop, Expression object, Statement stmt)
11236 {
11237    char propName[1024], propNameM[1024];
11238    char getName[1024], setName[1024];
11239    OldList * args;
11240
11241    DeclareProperty(prop, setName, getName);
11242
11243    // eInstance_FireWatchers(object, prop);
11244    strcpy(propName, "__ecereProp_");
11245    FullClassNameCat(propName, prop._class.fullName, false);
11246    strcat(propName, "_");
11247    // strcat(propName, prop.name);
11248    FullClassNameCat(propName, prop.name, true);
11249    MangleClassName(propName);
11250
11251    strcpy(propNameM, "__ecerePropM_");
11252    FullClassNameCat(propNameM, prop._class.fullName, false);
11253    strcat(propNameM, "_");
11254    // strcat(propNameM, prop.name);
11255    FullClassNameCat(propNameM, prop.name, true);
11256    MangleClassName(propNameM);
11257
11258    if(prop.isWatchable)
11259    {
11260       args = MkList();
11261       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11262       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11263       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11264
11265       args = MkList();
11266       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11267       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11268       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireWatchers")), args));
11269    }
11270
11271
11272    {
11273       args = MkList();
11274       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11275       ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
11276       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11277
11278       args = MkList();
11279       ListAdd(args, object ? CopyExpression(object) : MkExpIdentifier(MkIdentifier("this")));
11280       ListAdd(args, MkExpIdentifier(MkIdentifier(propNameM)));
11281       ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_FireSelfWatchers")), args));
11282    }
11283
11284    if(curFunction.propSet && !strcmp(curFunction.propSet.string, prop.name) &&
11285       (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
11286       curFunction.propSet.fireWatchersDone = true;
11287 }
11288
11289 static void ProcessStatement(Statement stmt)
11290 {
11291    yylloc = stmt.loc;
11292    /*
11293    lineNum = stmt.pos.line;
11294    column = stmt.pos.col;
11295    */
11296    switch(stmt.type)
11297    {
11298       case labeledStmt:
11299          ProcessStatement(stmt.labeled.stmt);
11300          break;
11301       case caseStmt:
11302          // This expression should be constant...
11303          if(stmt.caseStmt.exp)
11304          {
11305             FreeType(stmt.caseStmt.exp.destType);
11306             stmt.caseStmt.exp.destType = curSwitchType;
11307             if(curSwitchType) curSwitchType.refCount++;
11308             ProcessExpressionType(stmt.caseStmt.exp);
11309             ComputeExpression(stmt.caseStmt.exp);
11310          }
11311          if(stmt.caseStmt.stmt)
11312             ProcessStatement(stmt.caseStmt.stmt);
11313          break;
11314       case compoundStmt:
11315       {
11316          if(stmt.compound.context)
11317          {
11318             Declaration decl;
11319             Statement s;
11320
11321             Statement prevCompound = curCompound;
11322             Context prevContext = curContext;
11323
11324             if(!stmt.compound.isSwitch)
11325                curCompound = stmt;
11326             curContext = stmt.compound.context;
11327
11328             if(stmt.compound.declarations)
11329             {
11330                for(decl = stmt.compound.declarations->first; decl; decl = decl.next)
11331                   ProcessDeclaration(decl);
11332             }
11333             if(stmt.compound.statements)
11334             {
11335                for(s = stmt.compound.statements->first; s; s = s.next)
11336                   ProcessStatement(s);
11337             }
11338
11339             curContext = prevContext;
11340             curCompound = prevCompound;
11341          }
11342          break;
11343       }
11344       case expressionStmt:
11345       {
11346          Expression exp;
11347          if(stmt.expressions)
11348          {
11349             for(exp = stmt.expressions->first; exp; exp = exp.next)
11350                ProcessExpressionType(exp);
11351          }
11352          break;
11353       }
11354       case ifStmt:
11355       {
11356          Expression exp;
11357
11358          FreeType(((Expression)stmt.ifStmt.exp->last).destType);
11359          ((Expression)stmt.ifStmt.exp->last).destType = MkClassType("bool");
11360          ((Expression)stmt.ifStmt.exp->last).destType.truth = true;
11361          for(exp = stmt.ifStmt.exp->first; exp; exp = exp.next)
11362          {
11363             ProcessExpressionType(exp);
11364          }
11365          if(stmt.ifStmt.stmt)
11366             ProcessStatement(stmt.ifStmt.stmt);
11367          if(stmt.ifStmt.elseStmt)
11368             ProcessStatement(stmt.ifStmt.elseStmt);
11369          break;
11370       }
11371       case switchStmt:
11372       {
11373          Type oldSwitchType = curSwitchType;
11374          if(stmt.switchStmt.exp)
11375          {
11376             Expression exp;
11377             for(exp = stmt.switchStmt.exp->first; exp; exp = exp.next)
11378             {
11379                if(!exp.next)
11380                {
11381                   /*
11382                   Type destType
11383                   {
11384                      kind = intType;
11385                      refCount = 1;
11386                   };
11387                   e.exp.destType = destType;
11388                   */
11389
11390                   ProcessExpressionType(exp);
11391                }
11392                if(!exp.next)
11393                   curSwitchType = exp.expType;
11394             }
11395          }
11396          ProcessStatement(stmt.switchStmt.stmt);
11397          curSwitchType = oldSwitchType;
11398          break;
11399       }
11400       case whileStmt:
11401       {
11402          if(stmt.whileStmt.exp)
11403          {
11404             Expression exp;
11405
11406             FreeType(((Expression)stmt.whileStmt.exp->last).destType);
11407             ((Expression)stmt.whileStmt.exp->last).destType = MkClassType("bool");
11408             ((Expression)stmt.whileStmt.exp->last).destType.truth = true;
11409             for(exp = stmt.whileStmt.exp->first; exp; exp = exp.next)
11410             {
11411                ProcessExpressionType(exp);
11412             }
11413          }
11414          if(stmt.whileStmt.stmt)
11415             ProcessStatement(stmt.whileStmt.stmt);
11416          break;
11417       }
11418       case doWhileStmt:
11419       {
11420          if(stmt.doWhile.exp)
11421          {
11422             Expression exp;
11423
11424             if(stmt.doWhile.exp->last)
11425             {
11426                FreeType(((Expression)stmt.doWhile.exp->last).destType);
11427                ((Expression)stmt.doWhile.exp->last).destType = MkClassType("bool");
11428                ((Expression)stmt.doWhile.exp->last).destType.truth = true;
11429             }
11430             for(exp = stmt.doWhile.exp->first; exp; exp = exp.next)
11431             {
11432                ProcessExpressionType(exp);
11433             }
11434          }
11435          if(stmt.doWhile.stmt)
11436             ProcessStatement(stmt.doWhile.stmt);
11437          break;
11438       }
11439       case forStmt:
11440       {
11441          Expression exp;
11442          if(stmt.forStmt.init)
11443             ProcessStatement(stmt.forStmt.init);
11444
11445          if(stmt.forStmt.check && stmt.forStmt.check.expressions)
11446          {
11447             FreeType(((Expression)stmt.forStmt.check.expressions->last).destType);
11448             ((Expression)stmt.forStmt.check.expressions->last).destType = MkClassType("bool");
11449             ((Expression)stmt.forStmt.check.expressions->last).destType.truth = true;
11450          }
11451
11452          if(stmt.forStmt.check)
11453             ProcessStatement(stmt.forStmt.check);
11454          if(stmt.forStmt.increment)
11455          {
11456             for(exp = stmt.forStmt.increment->first; exp; exp = exp.next)
11457                ProcessExpressionType(exp);
11458          }
11459
11460          if(stmt.forStmt.stmt)
11461             ProcessStatement(stmt.forStmt.stmt);
11462          break;
11463       }
11464       case forEachStmt:
11465       {
11466          Identifier id = stmt.forEachStmt.id;
11467          OldList * exp = stmt.forEachStmt.exp;
11468          OldList * filter = stmt.forEachStmt.filter;
11469          Statement block = stmt.forEachStmt.stmt;
11470          char iteratorType[1024];
11471          Type source;
11472          Expression e;
11473          bool isBuiltin = exp && exp->last &&
11474             (((Expression)exp->last).type == ExpressionType::arrayExp ||
11475               (((Expression)exp->last).type == castExp && ((Expression)exp->last).cast.exp.type == ExpressionType::arrayExp));
11476          Expression arrayExp;
11477          char * typeString = null;
11478          int builtinCount = 0;
11479
11480          for(e = exp ? exp->first : null; e; e = e.next)
11481          {
11482             if(!e.next)
11483             {
11484                FreeType(e.destType);
11485                e.destType = ProcessTypeString("Container", false);
11486             }
11487             if(!isBuiltin || e.next)
11488                ProcessExpressionType(e);
11489          }
11490
11491          source = (exp && exp->last) ? ((Expression)exp->last).expType : null;
11492          if(isBuiltin || (source && source.kind == classType && source._class && source._class.registered && source._class.registered != containerClass &&
11493             eClass_IsDerived(source._class.registered, containerClass)))
11494          {
11495             Class _class = source ? source._class.registered : null;
11496             Symbol symbol;
11497             Expression expIt = null;
11498             bool isMap = false, isArray = false, isLinkList = false, isList = false, isCustomAVLTree = false, isAVLTree = false;
11499             Class arrayClass = eSystem_FindClass(privateModule, "Array");
11500             Class linkListClass = eSystem_FindClass(privateModule, "LinkList");
11501             Class customAVLTreeClass = eSystem_FindClass(privateModule, "CustomAVLTree");
11502             stmt.type = compoundStmt;
11503
11504             stmt.compound.context = Context { };
11505             stmt.compound.context.parent = curContext;
11506             curContext = stmt.compound.context;
11507
11508             if(source && eClass_IsDerived(source._class.registered, customAVLTreeClass))
11509             {
11510                Class mapClass = eSystem_FindClass(privateModule, "Map");
11511                Class avlTreeClass = eSystem_FindClass(privateModule, "AVLTree");
11512                isCustomAVLTree = true;
11513                if(eClass_IsDerived(source._class.registered, avlTreeClass))
11514                   isAVLTree = true;
11515                else if(eClass_IsDerived(source._class.registered, mapClass))
11516                   isMap = true;
11517             }
11518             else if(source && eClass_IsDerived(source._class.registered, arrayClass)) isArray = true;
11519             else if(source && eClass_IsDerived(source._class.registered, linkListClass))
11520             {
11521                Class listClass = eSystem_FindClass(privateModule, "List");
11522                isLinkList = true;
11523                isList = eClass_IsDerived(source._class.registered, listClass);
11524             }
11525
11526             if(isArray)
11527             {
11528                Declarator decl;
11529                OldList * specs = MkList();
11530                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11531                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11532                stmt.compound.declarations = MkListOne(
11533                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11534                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11535                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalArray")),
11536                      MkInitializerAssignment(MkExpBrackets(exp))))));
11537             }
11538             else if(isBuiltin)
11539             {
11540                Type type = null;
11541                char typeStringBuf[1024];
11542
11543                // TODO: Merge this code?
11544                arrayExp = (((Expression)exp->last).type == ExpressionType::arrayExp) ? (Expression)exp->last : ((Expression)exp->last).cast.exp;
11545                if(((Expression)exp->last).type == castExp)
11546                {
11547                   TypeName typeName = ((Expression)exp->last).cast.typeName;
11548                   if(typeName)
11549                      arrayExp.destType = ProcessType(typeName.qualifiers, typeName.declarator);
11550                }
11551
11552                if(arrayExp.destType && arrayExp.destType.kind == classType && arrayExp.destType._class && arrayExp.destType._class.registered &&
11553                   arrayExp.destType._class.registered != containerClass && eClass_IsDerived(arrayExp.destType._class.registered, containerClass) &&
11554                   arrayExp.destType._class.registered.templateArgs)
11555                {
11556                   Class templateClass = arrayExp.destType._class.registered;
11557                   typeString = templateClass.templateArgs[2].dataTypeString;
11558                }
11559                else if(arrayExp.list)
11560                {
11561                   // Guess type from expressions in the array
11562                   Expression e;
11563                   for(e = arrayExp.list->first; e; e = e.next)
11564                   {
11565                      ProcessExpressionType(e);
11566                      if(e.expType)
11567                      {
11568                         if(!type) { type = e.expType; type.refCount++; }
11569                         else
11570                         {
11571                            // if(!MatchType(e.expType, type, null, null, null, false, false, false))
11572                            if(!MatchTypeExpression(e, type, null, false))
11573                            {
11574                               FreeType(type);
11575                               type = e.expType;
11576                               e.expType = null;
11577
11578                               e = arrayExp.list->first;
11579                               ProcessExpressionType(e);
11580                               if(e.expType)
11581                               {
11582                                  //if(!MatchTypes(e.expType, type, null, null, null, false, false, false, false))
11583                                  if(!MatchTypeExpression(e, type, null, false))
11584                                  {
11585                                     FreeType(e.expType);
11586                                     e.expType = null;
11587                                     FreeType(type);
11588                                     type = null;
11589                                     break;
11590                                  }
11591                               }
11592                            }
11593                         }
11594                         if(e.expType)
11595                         {
11596                            FreeType(e.expType);
11597                            e.expType = null;
11598                         }
11599                      }
11600                   }
11601                   if(type)
11602                   {
11603                      typeStringBuf[0] = '\0';
11604                      PrintType(type, typeStringBuf, false, true);
11605                      typeString = typeStringBuf;
11606                      FreeType(type);
11607                   }
11608                }
11609                if(typeString)
11610                {
11611                   OldList * initializers = MkList();
11612                   Declarator decl;
11613                   OldList * specs = MkList();
11614                   if(arrayExp.list)
11615                   {
11616                      Expression e;
11617
11618                      builtinCount = arrayExp.list->count;
11619                      type = ProcessTypeString(typeString, false);
11620                      while(e = arrayExp.list->first)
11621                      {
11622                         arrayExp.list->Remove(e);
11623                         e.destType = type;
11624                         type.refCount++;
11625                         ProcessExpressionType(e);
11626                         ListAdd(initializers, MkInitializerAssignment(e));
11627                      }
11628                      FreeType(type);
11629                      delete arrayExp.list;
11630                   }
11631                   decl = SpecDeclFromString(typeString, specs, MkDeclaratorIdentifier(id));
11632                   stmt.compound.declarations = MkListOne(MkDeclaration(CopyList(specs, CopySpecifier),
11633                      MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), /*CopyDeclarator(*/decl/*)*/), null))));
11634
11635                   ListAdd(stmt.compound.declarations, MkDeclaration(specs, MkListOne(MkInitDeclarator(
11636                      PlugDeclarator(
11637                         /*CopyDeclarator(*/decl/*)*/, MkDeclaratorArray(MkDeclaratorIdentifier(MkIdentifier("__internalArray")), null)
11638                         ), MkInitializerList(initializers)))));
11639                   FreeList(exp, FreeExpression);
11640                }
11641                else
11642                {
11643                   arrayExp.expType = ProcessTypeString("Container", false);
11644                   Compiler_Error($"Couldn't determine type of array elements\n");
11645                }
11646
11647                /*
11648                Declarator decl;
11649                OldList * specs = MkList();
11650
11651                decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs,
11652                   MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(id)));
11653                stmt.compound.declarations = MkListOne(
11654                   MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11655                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName("BuiltInContainer")),
11656                   MkListOne(MkInitDeclarator(MkDeclaratorPointer(MkPointer(null, null), MkDeclaratorIdentifier(MkIdentifier("__internalArray"))),
11657                      MkInitializerAssignment(MkExpBrackets(exp))))));
11658                */
11659             }
11660             else if(isLinkList && !isList)
11661             {
11662                Declarator decl;
11663                OldList * specs = MkList();
11664                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11665                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11666                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11667                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalLinkList")),
11668                      MkInitializerAssignment(MkExpBrackets(exp))))));
11669             }
11670             /*else if(isCustomAVLTree)
11671             {
11672                Declarator decl;
11673                OldList * specs = MkList();
11674                decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, MkDeclaratorIdentifier(id));
11675                stmt.compound.declarations = MkListOne(MkDeclaration(specs, MkListOne(MkInitDeclarator(decl, null))));
11676                ListAdd(stmt.compound.declarations, MkDeclaration(MkListOne(MkSpecifierName(source._class.registered.fullName)),
11677                   MkListOne(MkInitDeclarator(MkDeclaratorIdentifier(MkIdentifier("__internalTree")),
11678                      MkInitializerAssignment(MkExpBrackets(exp))))));
11679             }*/
11680             else if(_class.templateArgs)
11681             {
11682                if(isMap)
11683                   sprintf(iteratorType, "MapIterator<%s, %s >", _class.templateArgs[5].dataTypeString, _class.templateArgs[6].dataTypeString);
11684                else
11685                   sprintf(iteratorType, "Iterator<%s, %s >", _class.templateArgs[2].dataTypeString, _class.templateArgs[1].dataTypeString);
11686
11687                stmt.compound.declarations = MkListOne(
11688                   MkDeclarationInst(MkInstantiationNamed(MkListOne(MkSpecifierName(iteratorType)),
11689                   MkExpIdentifier(id), MkListOne(MkMembersInitList(MkListOne(MkMemberInit(isMap ? MkListOne(MkIdentifier("map")) : null,
11690                   MkInitializerAssignment(MkExpBrackets(exp)))))))));
11691             }
11692             symbol = FindSymbol(id.string, curContext, curContext, false, false);
11693
11694             if(block)
11695             {
11696                // Reparent sub-contexts in this statement
11697                switch(block.type)
11698                {
11699                   case compoundStmt:
11700                      if(block.compound.context)
11701                         block.compound.context.parent = stmt.compound.context;
11702                      break;
11703                   case ifStmt:
11704                      if(block.ifStmt.stmt && block.ifStmt.stmt.type == compoundStmt && block.ifStmt.stmt.compound.context)
11705                         block.ifStmt.stmt.compound.context.parent = stmt.compound.context;
11706                      if(block.ifStmt.elseStmt && block.ifStmt.elseStmt.type == compoundStmt && block.ifStmt.elseStmt.compound.context)
11707                         block.ifStmt.elseStmt.compound.context.parent = stmt.compound.context;
11708                      break;
11709                   case switchStmt:
11710                      if(block.switchStmt.stmt && block.switchStmt.stmt.type == compoundStmt && block.switchStmt.stmt.compound.context)
11711                         block.switchStmt.stmt.compound.context.parent = stmt.compound.context;
11712                      break;
11713                   case whileStmt:
11714                      if(block.whileStmt.stmt && block.whileStmt.stmt.type == compoundStmt && block.whileStmt.stmt.compound.context)
11715                         block.whileStmt.stmt.compound.context.parent = stmt.compound.context;
11716                      break;
11717                   case doWhileStmt:
11718                      if(block.doWhile.stmt && block.doWhile.stmt.type == compoundStmt && block.doWhile.stmt.compound.context)
11719                         block.doWhile.stmt.compound.context.parent = stmt.compound.context;
11720                      break;
11721                   case forStmt:
11722                      if(block.forStmt.stmt && block.forStmt.stmt.type == compoundStmt && block.forStmt.stmt.compound.context)
11723                         block.forStmt.stmt.compound.context.parent = stmt.compound.context;
11724                      break;
11725                   case forEachStmt:
11726                      if(block.forEachStmt.stmt && block.forEachStmt.stmt.type == compoundStmt && block.forEachStmt.stmt.compound.context)
11727                         block.forEachStmt.stmt.compound.context.parent = stmt.compound.context;
11728                      break;
11729                   /* Only handle those with compound blocks for now... (Potential limitation on compound statements within expressions)
11730                   case labeledStmt:
11731                   case caseStmt
11732                   case expressionStmt:
11733                   case gotoStmt:
11734                   case continueStmt:
11735                   case breakStmt
11736                   case returnStmt:
11737                   case asmStmt:
11738                   case badDeclarationStmt:
11739                   case fireWatchersStmt:
11740                   case stopWatchingStmt:
11741                   case watchStmt:
11742                   */
11743                }
11744             }
11745             if(filter)
11746             {
11747                block = MkIfStmt(filter, block, null);
11748             }
11749             if(isArray)
11750             {
11751                stmt.compound.statements = MkListOne(MkForStmt(
11752                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array"))))),
11753                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11754                      MkExpOp(MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("array")), '+', MkExpMember(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11755                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11756                   block));
11757               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11758               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11759               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11760             }
11761             else if(isBuiltin)
11762             {
11763                char count[128];
11764                //OldList * specs = MkList();
11765                // Declarator decl = SpecDeclFromString(typeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11766
11767                sprintf(count, "%d", builtinCount);
11768
11769                stmt.compound.statements = MkListOne(MkForStmt(
11770                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpIdentifier(MkIdentifier("__internalArray"))))),
11771                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11772                      MkExpOp(MkExpIdentifier(MkIdentifier("__internalArray")), '+', MkExpConstant(count))))),
11773                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11774                   block));
11775
11776                /*
11777                Declarator decl = SpecDeclFromString(_class.templateArgs[2].dataTypeString, specs, MkDeclaratorPointer(MkPointer(null, null), null));
11778                stmt.compound.statements = MkListOne(MkForStmt(
11779                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))))),
11780                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '<',
11781                      MkExpOp(MkExpCast(MkTypeName(specs, decl), MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("data"))), '+', MkExpPointer(MkExpIdentifier(MkIdentifier("__internalArray")), MkIdentifier("count")))))),
11782                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), INC_OP, null)),
11783                   block));
11784               */
11785               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11786               ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11787               ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11788             }
11789             else if(isLinkList && !isList)
11790             {
11791                Class typeClass = eSystem_FindClass(_class.module, _class.templateArgs[3].dataTypeString);
11792                Class listItemClass = eSystem_FindClass(_class.module, "ListItem");
11793                if(typeClass && eClass_IsDerived(typeClass, listItemClass) && _class.templateArgs[5].dataTypeString &&
11794                   !strcmp(_class.templateArgs[5].dataTypeString, "LT::link"))
11795                {
11796                   stmt.compound.statements = MkListOne(MkForStmt(
11797                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11798                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11799                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11800                      block));
11801                }
11802                else
11803                {
11804                   OldList * specs = MkList();
11805                   Declarator decl = SpecDeclFromString(_class.templateArgs[3].dataTypeString, specs, null);
11806                   stmt.compound.statements = MkListOne(MkForStmt(
11807                      MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("first"))))),
11808                      MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11809                      MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpCast(MkTypeName(specs, decl), MkExpCall(
11810                         MkExpMember(MkExpIdentifier(MkIdentifier("__internalLinkList")), MkIdentifier("GetNext")),
11811                            MkListOne(MkExpCast(MkTypeName(MkListOne(MkSpecifierName("IteratorPointer")), null), MkExpIdentifier(CopyIdentifier(id)))))))),
11812                      block));
11813                }
11814                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11815                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11816                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11817             }
11818             /*else if(isCustomAVLTree)
11819             {
11820                stmt.compound.statements = MkListOne(MkForStmt(
11821                   MkExpressionStmt(MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpMember(MkExpIdentifier(
11822                      MkIdentifier("__internalTree")), MkIdentifier("root")), MkIdentifier("minimum"))))),
11823                   MkExpressionStmt(MkListOne(MkExpIdentifier(CopyIdentifier(id)))),
11824                   MkListOne(MkExpOp(MkExpIdentifier(CopyIdentifier(id)), '=', MkExpMember(MkExpIdentifier(CopyIdentifier(id)), MkIdentifier("next")))),
11825                   block));
11826
11827                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.init);
11828                ProcessStatement(((Statement)stmt.compound.statements->first).forStmt.check);
11829                ProcessExpressionType(((Statement)stmt.compound.statements->first).forStmt.increment->first);
11830             }*/
11831             else
11832             {
11833                stmt.compound.statements = MkListOne(MkWhileStmt(MkListOne(MkExpCall(MkExpMember(expIt = MkExpIdentifier(CopyIdentifier(id)),
11834                   MkIdentifier("Next")), null)), block));
11835             }
11836             ProcessExpressionType(expIt);
11837             if(stmt.compound.declarations->first)
11838                ProcessDeclaration(stmt.compound.declarations->first);
11839
11840             if(symbol)
11841                symbol.isIterator = isMap ? 2 : ((isArray || isBuiltin) ? 3 : (isLinkList ? (isList ? 5 : 4) : (isCustomAVLTree ? 6 : 1)));
11842
11843             ProcessStatement(stmt);
11844             curContext = stmt.compound.context.parent;
11845             break;
11846          }
11847          else
11848          {
11849             Compiler_Error($"Expression is not a container\n");
11850          }
11851          break;
11852       }
11853       case gotoStmt:
11854          break;
11855       case continueStmt:
11856          break;
11857       case breakStmt:
11858          break;
11859       case returnStmt:
11860       {
11861          Expression exp;
11862          if(stmt.expressions)
11863          {
11864             for(exp = stmt.expressions->first; exp; exp = exp.next)
11865             {
11866                if(!exp.next)
11867                {
11868                   if(curFunction && !curFunction.type)
11869                      curFunction.type = ProcessType(
11870                         curFunction.specifiers, curFunction.declarator);
11871                   FreeType(exp.destType);
11872                   exp.destType = (curFunction && curFunction.type && curFunction.type.kind == functionType) ? curFunction.type.returnType : null;
11873                   if(exp.destType) exp.destType.refCount++;
11874                }
11875                ProcessExpressionType(exp);
11876             }
11877          }
11878          break;
11879       }
11880       case badDeclarationStmt:
11881       {
11882          ProcessDeclaration(stmt.decl);
11883          break;
11884       }
11885       case asmStmt:
11886       {
11887          AsmField field;
11888          if(stmt.asmStmt.inputFields)
11889          {
11890             for(field = stmt.asmStmt.inputFields->first; field; field = field.next)
11891                if(field.expression)
11892                   ProcessExpressionType(field.expression);
11893          }
11894          if(stmt.asmStmt.outputFields)
11895          {
11896             for(field = stmt.asmStmt.outputFields->first; field; field = field.next)
11897                if(field.expression)
11898                   ProcessExpressionType(field.expression);
11899          }
11900          if(stmt.asmStmt.clobberedFields)
11901          {
11902             for(field = stmt.asmStmt.clobberedFields->first; field; field = field.next)
11903             {
11904                if(field.expression)
11905                   ProcessExpressionType(field.expression);
11906             }
11907          }
11908          break;
11909       }
11910       case watchStmt:
11911       {
11912          PropertyWatch propWatch;
11913          OldList * watches = stmt._watch.watches;
11914          Expression object = stmt._watch.object;
11915          Expression watcher = stmt._watch.watcher;
11916          if(watcher)
11917             ProcessExpressionType(watcher);
11918          if(object)
11919             ProcessExpressionType(object);
11920
11921          if(inCompiler)
11922          {
11923             if(watcher || thisClass)
11924             {
11925                External external = curExternal;
11926                Context context = curContext;
11927
11928                stmt.type = expressionStmt;
11929                stmt.expressions = MkList();
11930
11931                curExternal = external.prev;
11932
11933                for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
11934                {
11935                   ClassFunction func;
11936                   char watcherName[1024];
11937                   Class watcherClass = watcher ?
11938                      ((watcher.expType && watcher.expType.kind == classType && watcher.expType._class) ? watcher.expType._class.registered : null) : thisClass;
11939                   External createdExternal;
11940
11941                   // Create a declaration above
11942                   External externalDecl = MkExternalDeclaration(null);
11943                   ast->Insert(curExternal.prev, externalDecl);
11944
11945                   sprintf(watcherName,"__ecerePropertyWatcher_%d", propWatcherID++);
11946                   if(propWatch.deleteWatch)
11947                      strcat(watcherName, "_delete");
11948                   else
11949                   {
11950                      Identifier propID;
11951                      for(propID = propWatch.properties->first; propID; propID = propID.next)
11952                      {
11953                         strcat(watcherName, "_");
11954                         strcat(watcherName, propID.string);
11955                      }
11956                   }
11957
11958                   if(object && object.expType && object.expType.kind == classType && object.expType._class && object.expType._class.registered)
11959                   {
11960                      // TESTING THIS STUFF... BEWARE OF SYMBOL ID ISSUES
11961                      func = MkClassFunction(MkListOne(MkSpecifier(VOID)), null, MkDeclaratorFunction(MkDeclaratorIdentifier(MkIdentifier(watcherName)),
11962                         //MkListOne(MkTypeName(MkListOne(MkSpecifier(VOID)), null))), null);
11963                         MkListOne(MkTypeName(MkListOne(MkSpecifierName(object.expType._class.string)), MkDeclaratorIdentifier(MkIdentifier("value"))))), null);
11964                      ProcessClassFunctionBody(func, propWatch.compound);
11965                      propWatch.compound = null;
11966
11967                      //afterExternal = afterExternal ? afterExternal : curExternal;
11968
11969                      //createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal.prev);
11970                      createdExternal = ProcessClassFunction(watcherClass, func, ast, curExternal, true);
11971                      // TESTING THIS...
11972                      createdExternal.symbol.idCode = external.symbol.idCode;
11973
11974                      curExternal = createdExternal;
11975                      ProcessFunction(createdExternal.function);
11976
11977
11978                      // Create a declaration above
11979                      {
11980                         Declaration decl = MkDeclaration(CopyList(createdExternal.function.specifiers, CopySpecifier),
11981                            MkListOne(MkInitDeclarator(CopyDeclarator(createdExternal.function.declarator), null)));
11982                         externalDecl.declaration = decl;
11983                         if(decl.symbol && !decl.symbol.pointerExternal)
11984                            decl.symbol.pointerExternal = externalDecl;
11985                      }
11986
11987                      if(propWatch.deleteWatch)
11988                      {
11989                         OldList * args = MkList();
11990                         ListAdd(args, CopyExpression(object));
11991                         ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
11992                         ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
11993                         ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_WatchDestruction")), args));
11994                      }
11995                      else
11996                      {
11997                         Class _class = object.expType._class.registered;
11998                         Identifier propID;
11999
12000                         for(propID = propWatch.properties->first; propID; propID = propID.next)
12001                         {
12002                            char propName[1024];
12003                            Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12004                            if(prop)
12005                            {
12006                               char getName[1024], setName[1024];
12007                               OldList * args = MkList();
12008
12009                               DeclareProperty(prop, setName, getName);
12010
12011                               // eInstance_Watch(stmt.watch.object, prop, stmt.watch.watcher, callback);
12012                               strcpy(propName, "__ecereProp_");
12013                               FullClassNameCat(propName, prop._class.fullName, false);
12014                               strcat(propName, "_");
12015                               // strcat(propName, prop.name);
12016                               FullClassNameCat(propName, prop.name, true);
12017
12018                               ListAdd(args, CopyExpression(object));
12019                               ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12020                               ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12021                               ListAdd(args, MkExpIdentifier(MkIdentifier(watcherName)));
12022
12023                               ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_Watch")), args));
12024                            }
12025                            else
12026                               Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
12027                         }
12028                      }
12029                   }
12030                   else
12031                      Compiler_Error($"Invalid watched object\n");
12032                }
12033
12034                curExternal = external;
12035                curContext = context;
12036
12037                if(watcher)
12038                   FreeExpression(watcher);
12039                if(object)
12040                   FreeExpression(object);
12041                FreeList(watches, FreePropertyWatch);
12042             }
12043             else
12044                Compiler_Error($"No observer specified and not inside a _class\n");
12045          }
12046          else
12047          {
12048             for(propWatch = watches->first; propWatch; propWatch = propWatch.next)
12049             {
12050                ProcessStatement(propWatch.compound);
12051             }
12052
12053          }
12054          break;
12055       }
12056       case fireWatchersStmt:
12057       {
12058          OldList * watches = stmt._watch.watches;
12059          Expression object = stmt._watch.object;
12060          Class _class;
12061          // DEBUGGER BUG: Why doesn't watches evaluate to null??
12062          // printf("%X\n", watches);
12063          // printf("%X\n", stmt._watch.watches);
12064          if(object)
12065             ProcessExpressionType(object);
12066
12067          if(inCompiler)
12068          {
12069             _class = object ?
12070                   ((object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null) : thisClass;
12071
12072             if(_class)
12073             {
12074                Identifier propID;
12075
12076                stmt.type = expressionStmt;
12077                stmt.expressions = MkList();
12078
12079                // Check if we're inside a property set
12080                if(!watches && curFunction.propSet && (!object || (object.type == identifierExp && !strcmp(object.identifier.string, "this"))))
12081                {
12082                   watches = MkListOne(MkIdentifier(curFunction.propSet.string));
12083                }
12084                else if(!watches)
12085                {
12086                   //Compiler_Error($"No property specified and not inside a property set\n");
12087                }
12088                if(watches)
12089                {
12090                   for(propID = watches->first; propID; propID = propID.next)
12091                   {
12092                      Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12093                      if(prop)
12094                      {
12095                         CreateFireWatcher(prop, object, stmt);
12096                      }
12097                      else
12098                         Compiler_Error($"Property %s not found in class %s\n", propID.string, _class.fullName);
12099                   }
12100                }
12101                else
12102                {
12103                   // Fire all properties!
12104                   Property prop;
12105                   Class base;
12106                   for(base = _class; base; base = base.base)
12107                   {
12108                      for(prop = base.membersAndProperties.first; prop; prop = prop.next)
12109                      {
12110                         if(prop.isProperty && prop.isWatchable)
12111                         {
12112                            CreateFireWatcher(prop, object, stmt);
12113                         }
12114                      }
12115                   }
12116                }
12117
12118                if(object)
12119                   FreeExpression(object);
12120                FreeList(watches, FreeIdentifier);
12121             }
12122             else
12123                Compiler_Error($"Invalid object specified and not inside a class\n");
12124          }
12125          break;
12126       }
12127       case stopWatchingStmt:
12128       {
12129          OldList * watches = stmt._watch.watches;
12130          Expression object = stmt._watch.object;
12131          Expression watcher = stmt._watch.watcher;
12132          Class _class;
12133          if(object)
12134             ProcessExpressionType(object);
12135          if(watcher)
12136             ProcessExpressionType(watcher);
12137          if(inCompiler)
12138          {
12139             _class = (object && object.expType && object.expType.kind == classType && object.expType._class) ? object.expType._class.registered : null;
12140
12141             if(watcher || thisClass)
12142             {
12143                if(_class)
12144                {
12145                   Identifier propID;
12146
12147                   stmt.type = expressionStmt;
12148                   stmt.expressions = MkList();
12149
12150                   if(!watches)
12151                   {
12152                      OldList * args;
12153                      // eInstance_StopWatching(object, null, watcher);
12154                      args = MkList();
12155                      ListAdd(args, CopyExpression(object));
12156                      ListAdd(args, MkExpConstant("0"));
12157                      ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12158                      ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12159                   }
12160                   else
12161                   {
12162                      for(propID = watches->first; propID; propID = propID.next)
12163                      {
12164                         char propName[1024];
12165                         Property prop = eClass_FindProperty(_class, propID.string, privateModule);
12166                         if(prop)
12167                         {
12168                            char getName[1024], setName[1024];
12169                            OldList * args = MkList();
12170
12171                            DeclareProperty(prop, setName, getName);
12172
12173                            // eInstance_StopWatching(object, prop, watcher);
12174                            strcpy(propName, "__ecereProp_");
12175                            FullClassNameCat(propName, prop._class.fullName, false);
12176                            strcat(propName, "_");
12177                            // strcat(propName, prop.name);
12178                            FullClassNameCat(propName, prop.name, true);
12179                            MangleClassName(propName);
12180
12181                            ListAdd(args, CopyExpression(object));
12182                            ListAdd(args, MkExpIdentifier(MkIdentifier(propName)));
12183                            ListAdd(args, watcher ? CopyExpression(watcher) : MkExpIdentifier(MkIdentifier("this")));
12184                            ListAdd(stmt.expressions, MkExpCall(MkExpIdentifier(MkIdentifier("ecere::com::eInstance_StopWatching")), args));
12185                         }
12186                         else
12187                            Compiler_Error($"Property %s not found in class %s\n", prop.name, _class.fullName);
12188                      }
12189                   }
12190
12191                   if(object)
12192                      FreeExpression(object);
12193                   if(watcher)
12194                      FreeExpression(watcher);
12195                   FreeList(watches, FreeIdentifier);
12196                }
12197                else
12198                   Compiler_Error($"Invalid object specified and not inside a class\n");
12199             }
12200             else
12201                Compiler_Error($"No observer specified and not inside a class\n");
12202          }
12203          break;
12204       }
12205    }
12206 }
12207
12208 static void ProcessFunction(FunctionDefinition function)
12209 {
12210    Identifier id = GetDeclId(function.declarator);
12211    Symbol symbol = function.declarator ? function.declarator.symbol : null;
12212    Type type = symbol ? symbol.type : null;
12213    Class oldThisClass = thisClass;
12214    Context oldTopContext = topContext;
12215
12216    yylloc = function.loc;
12217    // Process thisClass
12218
12219    if(type && type.thisClass)
12220    {
12221       Symbol classSym = type.thisClass;
12222       Class _class = type.thisClass.registered;
12223       char className[1024];
12224       char structName[1024];
12225       Declarator funcDecl;
12226       Symbol thisSymbol;
12227
12228       bool typedObject = false;
12229
12230       if(_class && !_class.base)
12231       {
12232          _class = currentClass;
12233          if(_class && !_class.symbol)
12234             _class.symbol = FindClass(_class.fullName);
12235          classSym = _class ? _class.symbol : null;
12236          typedObject = true;
12237       }
12238
12239       thisClass = _class;
12240
12241       if(inCompiler && _class)
12242       {
12243          if(type.kind == functionType)
12244          {
12245             if(symbol.type.params.count == 1 && ((Type)symbol.type.params.first).kind == voidType)
12246             {
12247                //TypeName param = symbol.type.params.first;
12248                Type param = symbol.type.params.first;
12249                symbol.type.params.Remove(param);
12250                //FreeTypeName(param);
12251                FreeType(param);
12252             }
12253             if(type.classObjectType != classPointer)
12254             {
12255                symbol.type.params.Insert(null, MkClassType(_class.fullName));
12256                symbol.type.staticMethod = true;
12257                symbol.type.thisClass = null;
12258
12259                // HIGH DANGER: VERIFYING THIS...
12260                symbol.type.extraParam = false;
12261             }
12262          }
12263
12264          strcpy(className, "__ecereClass_");
12265          FullClassNameCat(className, _class.fullName, true);
12266
12267          MangleClassName(className);
12268
12269          structName[0] = 0;
12270          FullClassNameCat(structName, _class.fullName, false);
12271
12272          // [class] this
12273
12274
12275          funcDecl = GetFuncDecl(function.declarator);
12276          if(funcDecl)
12277          {
12278             if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12279             {
12280                TypeName param = funcDecl.function.parameters->first;
12281                if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12282                {
12283                   funcDecl.function.parameters->Remove(param);
12284                   FreeTypeName(param);
12285                }
12286             }
12287
12288             // DANGER: Watch for this... Check if it's a Conversion?
12289             // if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12290
12291             // WAS TRYING THIS FOR CONVERSION PROPERTIES ON NOHEAD CLASSES: if((_class.type == structClass) || function != (FunctionDefinition)symbol.externalSet)
12292             if(!function.propertyNoThis)
12293             {
12294                TypeName thisParam;
12295
12296                if(type.classObjectType != classPointer)
12297                {
12298                   thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12299                   if(!funcDecl.function.parameters)
12300                      funcDecl.function.parameters = MkList();
12301                   funcDecl.function.parameters->Insert(null, thisParam);
12302                }
12303
12304                if(typedObject)
12305                {
12306                   if(type.classObjectType != classPointer)
12307                   {
12308                      if(type.byReference || _class.type == unitClass || _class.type == systemClass || _class.type == enumClass || _class.type == bitClass)
12309                         thisParam.declarator = MkDeclaratorPointer(MkPointer(null,null), thisParam.declarator);
12310                   }
12311
12312                   thisParam = TypeName
12313                   {
12314                      declarator = MkDeclaratorPointer(MkPointer(null,null), MkDeclaratorIdentifier(MkIdentifier("class")));
12315                      qualifiers = MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier("__ecereNameSpace__ecere__com__Class"), null));
12316                   };
12317                   funcDecl.function.parameters->Insert(null, thisParam);
12318                }
12319             }
12320          }
12321
12322          if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12323          {
12324             InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12325             funcDecl = GetFuncDecl(initDecl.declarator);
12326             if(funcDecl)
12327             {
12328                if(funcDecl.function.parameters && funcDecl.function.parameters->count == 1)
12329                {
12330                   TypeName param = funcDecl.function.parameters->first;
12331                   if(param.qualifiers && param.qualifiers->count == 1 && ((Specifier)param.qualifiers->first).specifier == VOID && !param.declarator)
12332                   {
12333                      funcDecl.function.parameters->Remove(param);
12334                      FreeTypeName(param);
12335                   }
12336                }
12337
12338                if(type.classObjectType != classPointer)
12339                {
12340                   // DANGER: Watch for this... Check if it's a Conversion?
12341                   if((_class.type != bitClass && _class.type != unitClass && _class.type != enumClass) || function != (FunctionDefinition)symbol.externalSet)
12342                   {
12343                      TypeName thisParam = QMkClass(_class.fullName, MkDeclaratorIdentifier(MkIdentifier("this")));
12344
12345                      if(!funcDecl.function.parameters)
12346                         funcDecl.function.parameters = MkList();
12347                      funcDecl.function.parameters->Insert(null, thisParam);
12348                   }
12349                }
12350             }
12351          }
12352       }
12353
12354       // Add this to the context
12355       if(function.body)
12356       {
12357          if(type.classObjectType != classPointer)
12358          {
12359             thisSymbol = Symbol
12360             {
12361                string = CopyString("this");
12362                type = classSym ? MkClassType(classSym.string) : null; //_class.fullName);
12363             };
12364             function.body.compound.context.symbols.Add((BTNode)thisSymbol);
12365
12366             if(typedObject && thisSymbol.type)
12367             {
12368                thisSymbol.type.classObjectType = ClassObjectType::typedObject;
12369                thisSymbol.type.byReference = type.byReference;
12370                thisSymbol.type.typedByReference = type.byReference;
12371                /*
12372                thisSymbol = Symbol { string = CopyString("class") };
12373                function.body.compound.context.symbols.Add(thisSymbol);
12374                */
12375             }
12376          }
12377       }
12378
12379       // Pointer to class data
12380
12381       if(inCompiler && _class && (_class.type == normalClass /*|| _class.type == noHeadClass*/) && type.classObjectType != classPointer)
12382       {
12383          DataMember member = null;
12384          {
12385             Class base;
12386             for(base = _class; base && base.type != systemClass; base = base.next)
12387             {
12388                for(member = base.membersAndProperties.first; member; member = member.next)
12389                   if(!member.isProperty)
12390                      break;
12391                if(member)
12392                   break;
12393             }
12394          }
12395          for(member = _class.membersAndProperties.first; member; member = member.next)
12396             if(!member.isProperty)
12397                break;
12398          if(member)
12399          {
12400             char pointerName[1024];
12401
12402             Declaration decl;
12403             Initializer initializer;
12404             Expression exp, bytePtr;
12405
12406             strcpy(pointerName, "__ecerePointer_");
12407             FullClassNameCat(pointerName, _class.fullName, false);
12408             {
12409                char className[1024];
12410                strcpy(className, "__ecereClass_");
12411                FullClassNameCat(className, classSym.string, true);
12412                MangleClassName(className);
12413
12414                // Testing This
12415                DeclareClass(classSym, className);
12416             }
12417
12418             // ((byte *) this)
12419             bytePtr = QBrackets(MkExpCast(QMkType("char", QMkPtrDecl(null)), QMkExpId("this")));
12420
12421             if(_class.fixed)
12422             {
12423                char string[256];
12424                sprintf(string, "%d", _class.offset);
12425                exp = QBrackets(MkExpOp(bytePtr, '+', MkExpConstant(string)));
12426             }
12427             else
12428             {
12429                // ([bytePtr] + [className]->offset)
12430                exp = QBrackets(MkExpOp(bytePtr, '+',
12431                   MkExpPointer(QMkExpId(className), MkIdentifier("offset"))));
12432             }
12433
12434             // (this ? [exp] : 0)
12435             exp = QBrackets(QMkExpCond(QMkExpId("this"), exp, MkExpConstant("0")));
12436             exp.expType = Type
12437             {
12438                refCount = 1;
12439                kind = pointerType;
12440                type = Type { refCount = 1, kind = voidType };
12441             };
12442
12443             if(function.body)
12444             {
12445                yylloc = function.body.loc;
12446                // ([structName] *) [exp]
12447                // initializer = MkInitializerAssignment(MkExpCast(QMkType(structName, QMkPtrDecl(null)), exp));
12448                initializer = MkInitializerAssignment(
12449                   MkExpCast(MkTypeName(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)), MkDeclaratorPointer(MkPointer(null, null), null)), exp));
12450
12451                // [structName] * [pointerName] = [initializer];
12452                // decl = QMkDeclaration(structName, MkInitDeclarator(QMkPtrDecl(pointerName), initializer));
12453
12454                {
12455                   Context prevContext = curContext;
12456                   curContext = function.body.compound.context;
12457
12458                   decl = MkDeclaration(MkListOne(MkStructOrUnion(structSpecifier, MkIdentifier(structName), null)),
12459                      MkListOne(MkInitDeclarator(QMkPtrDecl(pointerName), initializer)));
12460
12461                   curContext = prevContext;
12462                }
12463
12464                // WHY?
12465                decl.symbol = null;
12466
12467                if(!function.body.compound.declarations)
12468                   function.body.compound.declarations = MkList();
12469                function.body.compound.declarations->Insert(null, decl);
12470             }
12471          }
12472       }
12473
12474
12475       // Loop through the function and replace undeclared identifiers
12476       // which are a member of the class (methods, properties or data)
12477       // by "this.[member]"
12478    }
12479    else
12480       thisClass = null;
12481
12482    if(id)
12483    {
12484       FreeSpecifier(id._class);
12485       id._class = null;
12486
12487       if(symbol && symbol.pointerExternal && symbol.pointerExternal.type == declarationExternal)
12488       {
12489          InitDeclarator initDecl = symbol.pointerExternal.declaration.declarators->first;
12490          id = GetDeclId(initDecl.declarator);
12491
12492          FreeSpecifier(id._class);
12493          id._class = null;
12494       }
12495    }
12496    if(function.body)
12497       topContext = function.body.compound.context;
12498    {
12499       FunctionDefinition oldFunction = curFunction;
12500       curFunction = function;
12501       if(function.body)
12502          ProcessStatement(function.body);
12503
12504       // If this is a property set and no firewatchers has been done yet, add one here
12505       if(inCompiler && function.propSet && !function.propSet.fireWatchersDone)
12506       {
12507          Statement prevCompound = curCompound;
12508          Context prevContext = curContext;
12509
12510          Statement fireWatchers = MkFireWatchersStmt(null, null);
12511          if(!function.body.compound.statements) function.body.compound.statements = MkList();
12512          ListAdd(function.body.compound.statements, fireWatchers);
12513
12514          curCompound = function.body;
12515          curContext = function.body.compound.context;
12516
12517          ProcessStatement(fireWatchers);
12518
12519          curContext = prevContext;
12520          curCompound = prevCompound;
12521
12522       }
12523
12524       curFunction = oldFunction;
12525    }
12526
12527    if(function.declarator)
12528    {
12529       ProcessDeclarator(function.declarator);
12530    }
12531
12532    topContext = oldTopContext;
12533    thisClass = oldThisClass;
12534 }
12535
12536 /////////// INSTANTIATIONS / DATA TYPES PASS /////////////////////////////////////////////
12537 static void ProcessClass(OldList definitions, Symbol symbol)
12538 {
12539    ClassDef def;
12540    External external = curExternal;
12541    Class regClass = symbol ? symbol.registered : null;
12542
12543    // Process all functions
12544    for(def = definitions.first; def; def = def.next)
12545    {
12546       if(def.type == functionClassDef)
12547       {
12548          if(def.function.declarator)
12549             curExternal = def.function.declarator.symbol.pointerExternal;
12550          else
12551             curExternal = external;
12552
12553          ProcessFunction((FunctionDefinition)def.function);
12554       }
12555       else if(def.type == declarationClassDef)
12556       {
12557          if(def.decl.type == instDeclaration)
12558          {
12559             thisClass = regClass;
12560             ProcessInstantiationType(def.decl.inst);
12561             thisClass = null;
12562          }
12563          // Testing this
12564          else
12565          {
12566             Class backThisClass = thisClass;
12567             if(regClass) thisClass = regClass;
12568             ProcessDeclaration(def.decl);
12569             thisClass = backThisClass;
12570          }
12571       }
12572       else if(def.type == defaultPropertiesClassDef && def.defProperties)
12573       {
12574          MemberInit defProperty;
12575
12576          // Add this to the context
12577          Symbol thisSymbol = Symbol
12578          {
12579             string = CopyString("this");
12580             type = regClass ? MkClassType(regClass.fullName) : null;
12581          };
12582          globalContext.symbols.Add((BTNode)thisSymbol);
12583
12584          for(defProperty = def.defProperties->first; defProperty; defProperty = defProperty.next)
12585          {
12586             thisClass = regClass;
12587             ProcessMemberInitData(defProperty, regClass, null, null, null, null);
12588             thisClass = null;
12589          }
12590
12591          globalContext.symbols.Remove((BTNode)thisSymbol);
12592          FreeSymbol(thisSymbol);
12593       }
12594       else if(def.type == propertyClassDef && def.propertyDef)
12595       {
12596          PropertyDef prop = def.propertyDef;
12597
12598          // Add this to the context
12599          /*
12600          Symbol thisSymbol = Symbol { string = CopyString("this"), type = MkClassType(regClass.fullName) };
12601          globalContext.symbols.Add(thisSymbol);
12602          */
12603
12604          thisClass = regClass;
12605          if(prop.setStmt)
12606          {
12607             if(regClass)
12608             {
12609                Symbol thisSymbol
12610                {
12611                   string = CopyString("this");
12612                   type = MkClassType(regClass.fullName);
12613                };
12614                prop.setStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12615             }
12616
12617             curExternal = prop.symbol ? prop.symbol.externalSet : null;
12618             ProcessStatement(prop.setStmt);
12619          }
12620          if(prop.getStmt)
12621          {
12622             if(regClass)
12623             {
12624                Symbol thisSymbol
12625                {
12626                   string = CopyString("this");
12627                   type = MkClassType(regClass.fullName);
12628                };
12629                prop.getStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12630             }
12631
12632             curExternal = prop.symbol ? prop.symbol.externalGet : null;
12633             ProcessStatement(prop.getStmt);
12634          }
12635          if(prop.issetStmt)
12636          {
12637             if(regClass)
12638             {
12639                Symbol thisSymbol
12640                {
12641                   string = CopyString("this");
12642                   type = MkClassType(regClass.fullName);
12643                };
12644                prop.issetStmt.compound.context.symbols.Add((BTNode)thisSymbol);
12645             }
12646
12647             curExternal = prop.symbol ? prop.symbol.externalIsSet : null;
12648             ProcessStatement(prop.issetStmt);
12649          }
12650
12651          thisClass = null;
12652
12653          /*
12654          globalContext.symbols.Remove(thisSymbol);
12655          FreeSymbol(thisSymbol);
12656          */
12657       }
12658       else if(def.type == propertyWatchClassDef && def.propertyWatch)
12659       {
12660          PropertyWatch propertyWatch = def.propertyWatch;
12661
12662          thisClass = regClass;
12663          if(propertyWatch.compound)
12664          {
12665             Symbol thisSymbol
12666             {
12667                string = CopyString("this");
12668                type = regClass ? MkClassType(regClass.fullName) : null;
12669             };
12670
12671             propertyWatch.compound.compound.context.symbols.Add((BTNode)thisSymbol);
12672
12673             curExternal = null;
12674             ProcessStatement(propertyWatch.compound);
12675          }
12676          thisClass = null;
12677       }
12678    }
12679 }
12680
12681 void DeclareFunctionUtil(String s)
12682 {
12683    GlobalFunction function = eSystem_FindFunction(privateModule, s);
12684    if(function)
12685    {
12686       char name[1024];
12687       name[0] = 0;
12688       if(function.module.importType != staticImport && (!function.dataType || !function.dataType.dllExport))
12689          strcpy(name, "__ecereFunction_");
12690       FullClassNameCat(name, s, false); // Why is this using FullClassNameCat ?
12691       DeclareFunction(function, name);
12692    }
12693 }
12694
12695 void ComputeDataTypes()
12696 {
12697    External external;
12698    External temp { };
12699    External after = null;
12700
12701    currentClass = null;
12702
12703    containerClass = eSystem_FindClass(GetPrivateModule(), "Container");
12704
12705    for(external = ast->first; external; external = external.next)
12706    {
12707       if(external.type == declarationExternal)
12708       {
12709          Declaration decl = external.declaration;
12710          if(decl)
12711          {
12712             OldList * decls = decl.declarators;
12713             if(decls)
12714             {
12715                InitDeclarator initDecl = decls->first;
12716                if(initDecl)
12717                {
12718                   Declarator declarator = initDecl.declarator;
12719                   if(declarator && declarator.type == identifierDeclarator)
12720                   {
12721                      Identifier id = declarator.identifier;
12722                      if(id && id.string)
12723                      {
12724                         if(!strcmp(id.string, "uintptr_t") || !strcmp(id.string, "intptr_t") || !strcmp(id.string, "size_t") || !strcmp(id.string, "ssize_t"))
12725                         {
12726                            external.symbol.id = -1001, external.symbol.idCode = -1001;
12727                            after = external;
12728                         }
12729                      }
12730                   }
12731                }
12732             }
12733          }
12734        }
12735    }
12736
12737    temp.symbol = Symbol { id = -1000, idCode = -1000 };
12738    ast->Insert(after, temp);
12739    curExternal = temp;
12740
12741    DeclareFunctionUtil("eSystem_New");
12742    DeclareFunctionUtil("eSystem_New0");
12743    DeclareFunctionUtil("eSystem_Renew");
12744    DeclareFunctionUtil("eSystem_Renew0");
12745    DeclareFunctionUtil("eSystem_Delete");
12746    DeclareFunctionUtil("eClass_GetProperty");
12747    DeclareFunctionUtil("eInstance_FireSelfWatchers");
12748
12749    DeclareStruct("ecere::com::Class", false);
12750    DeclareStruct("ecere::com::Instance", false);
12751    DeclareStruct("ecere::com::Property", false);
12752    DeclareStruct("ecere::com::DataMember", false);
12753    DeclareStruct("ecere::com::Method", false);
12754    DeclareStruct("ecere::com::SerialBuffer", false);
12755    DeclareStruct("ecere::com::ClassTemplateArgument", false);
12756
12757    ast->Remove(temp);
12758
12759    for(external = ast->first; external; external = external.next)
12760    {
12761       afterExternal = curExternal = external;
12762       if(external.type == functionExternal)
12763       {
12764          currentClass = external.function._class;
12765          ProcessFunction(external.function);
12766       }
12767       // There shouldn't be any _class member access here anyways...
12768       else if(external.type == declarationExternal)
12769       {
12770          currentClass = null;
12771          ProcessDeclaration(external.declaration);
12772       }
12773       else if(external.type == classExternal)
12774       {
12775          ClassDefinition _class = external._class;
12776          currentClass = external.symbol.registered;
12777          if(_class.definitions)
12778          {
12779             ProcessClass(_class.definitions, _class.symbol);
12780          }
12781          if(inCompiler)
12782          {
12783             // Free class data...
12784             ast->Remove(external);
12785             delete external;
12786          }
12787       }
12788       else if(external.type == nameSpaceExternal)
12789       {
12790          thisNameSpace = external.id.string;
12791       }
12792    }
12793    currentClass = null;
12794    thisNameSpace = null;
12795
12796    delete temp.symbol;
12797    delete temp;
12798 }